Python keras.layers.core.Reshape() Examples
The following are 30
code examples of keras.layers.core.Reshape().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.layers.core
, or try the search function
.
Example #1
Source File: model_zoo.py From visual_turing_test-tutorial with MIT License | 6 votes |
def textual_embedding(self, language_model, mask_zero): """ Note: * mask_zero only makes sense if embedding is learnt """ if self._config.textual_embedding_dim > 0: print('Textual Embedding is on') language_model.add(Embedding( self._config.input_dim, self._config.textual_embedding_dim, mask_zero=mask_zero)) else: print('Textual Embedding is off') language_model.add(Reshape( input_shape=(self._config.max_input_time_steps, self._config.input_dim), dims=(self._config.max_input_time_steps, self._config.input_dim))) if mask_zero: language_model.add(Masking(0)) return language_model
Example #2
Source File: model_zoo.py From visual_turing_test-tutorial with MIT License | 6 votes |
def textual_embedding_fixed_length(self, language_model, mask_zero): """ In contrast to textual_embedding, it produces a fixed length output. """ if self._config.textual_embedding_dim > 0: print('Textual Embedding with fixed length is on') language_model.add(Embedding( self._config.input_dim, self._config.textual_embedding_dim, input_length=self._config.max_input_time_steps, mask_zero=mask_zero)) else: print('Textual Embedding with fixed length is off') language_model.add(Reshape( input_shape=(self._config.max_input_time_steps, self._config.input_dim), dims=(self._config.max_input_time_steps, self._config.input_dim))) if mask_zero: language_model.add(Masking(0)) return language_model
Example #3
Source File: model.py From enet-keras with MIT License | 6 votes |
def build(nc, w, h, loss='categorical_crossentropy', # optimizer='adadelta'): optimizer='adam', metrics=None, **kwargs): data_shape = w * h if None not in (w, h) else -1 # TODO: -1 or None? inp = Input(shape=(h, w, 3), name='image') enet = encoder.build(inp) enet = decoder.build(enet, nc=nc) name = 'enet_unpooling' # TODO: need to remove data_shape for multi-scale training enet = Reshape((data_shape, nc))(enet) enet = Activation('softmax', name='output')(enet) model = Model(inputs=inp, outputs=enet) if metrics is None: metrics = ['accuracy'] model.compile(optimizer=optimizer, loss=loss, metrics=metrics) return model, name
Example #4
Source File: model.py From enet-keras with MIT License | 6 votes |
def build(nc, w, h, loss='categorical_crossentropy', optimizer='adam', **kwargs): data_shape = w * h if None not in (w, h) else -1 # TODO: -1 or None? inp = Input(shape=(h, w, 3)) enet = encoder.build(inp) enet = decoder.build(enet, nc=nc) name = 'enet_naive_upsampling' enet = Reshape((data_shape, nc))(enet) # TODO: need to remove data_shape for multi-scale training enet = Activation('softmax')(enet) model = Model(inputs=inp, outputs=enet) model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy', 'mean_squared_error']) return model, name
Example #5
Source File: MaskRCNN.py From PyTorch-Luna16 with Apache License 2.0 | 6 votes |
def call(self, inputs): rois = inputs[0] mrcnn_class = inputs[1] mrcnn_bbox = inputs[2] image_meta = inputs[3] # Run detection refinement graph on each item in the batch _, _, window, _ = parse_image_meta_graph(image_meta) detections_batch = utils.batch_slice( [rois, mrcnn_class, mrcnn_bbox, window], lambda x, y, w, z: refine_detections_graph(x, y, w, z, self.config), self.config.IMAGES_PER_GPU) # Reshape output # [batch, num_detections, (y1, x1, y2, x2, class_score)] in pixels return tf.reshape( detections_batch, [self.config.BATCH_SIZE, self.config.DETECTION_MAX_INSTANCES, 6])
Example #6
Source File: resnet.py From Hands-On-Generative-Adversarial-Networks-with-Keras with MIT License | 6 votes |
def build_cifar10_resnet_generator(input_shape, n_filters, n_residual_blocks, n_channels): """ adapted from wgan-gp """ inputs = Input(shape=input_shape) # Dense 1: 4 x 4 x n_filters x = Dense(4 * 4 * n_filters, input_shape=input_shape)(inputs) x = Reshape((4, 4, n_filters))(x) # ResNet blocks x = resnet_block_generator(x, n_residual_blocks, n_filters) x = BatchNormalization()(x) x = LeakyReLU(0.2)(x) # Output layer x = Conv2D(filters=n_channels, kernel_size=(3, 3), padding='same')(x) x = Activation('tanh')(x) # create model graph model = Model(inputs=inputs, outputs=x, name='Generator') print("\nGenerator ResNet") model.summary() return model
Example #7
Source File: models.py From Hands-On-Generative-Adversarial-Networks-with-Keras with MIT License | 6 votes |
def build_resnet_generator(input_shape, n_filters, n_residual_blocks, seq_len, vocabulary_size): inputs = Input(shape=input_shape) # Dense 1: 1 x seq_len x n_filters x = Dense(1 * seq_len * n_filters, input_shape=input_shape)(inputs) x = Reshape((1, seq_len, n_filters))(x) # ResNet blocks x = resnet_block(x, n_residual_blocks, n_filters) # Output layer x = Conv2D(filters=vocabulary_size, kernel_size=1, padding='same')(x) x = Softmax(axis=3)(x) # create model graph model = Model(inputs=inputs, outputs=x, name='Generator') print("\nGenerator ResNet") model.summary() return model
Example #8
Source File: topcoder_crnn.py From crnn-lid with GNU General Public License v3.0 | 5 votes |
def create_model(input_shape, config, is_training=True): weight_decay = 0.001 model = Sequential() model.add(Convolution2D(16, 7, 7, W_regularizer=l2(weight_decay), activation="relu", input_shape=input_shape)) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Convolution2D(32, 5, 5, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Convolution2D(64, 3, 3, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Convolution2D(128, 3, 3, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Convolution2D(256, 3, 3, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # (bs, y, x, c) --> (bs, x, y, c) model.add(Permute((2, 1, 3))) # (bs, x, y, c) --> (bs, x, y * c) bs, x, y, c = model.layers[-1].output_shape model.add(Reshape((x, y*c))) model.add(Bidirectional(LSTM(512, return_sequences=False), merge_mode="concat")) model.add(Dense(config["num_classes"], activation="softmax")) return model
Example #9
Source File: model.py From enet-keras with MIT License | 5 votes |
def build(nc, w, h, loss='categorical_crossentropy', optimizer='adadelta', plot=False, **kwargs): # data_shape = input_shape[0] * input_shape[1] if input_shape and None not in input_shape else None data_shape = w * h if None not in (w, h) else -1 # TODO: -1 or None? inp = Input(shape=(h, w, 3)) shapes = valid_shapes(inp) if h < 161 or w < 161: errmsg = 'Input image tensor must be at least 161pxs in both width and height' raise ValueError(errmsg) out = encoder.build(inp, valid_shapes=shapes) out = decoder.build(inp=inp, encoder=out, nc=nc, valid_shapes=shapes) out = Reshape((data_shape, nc))(out) # TODO: need to remove data_shape for multi-scale training out = Activation('softmax')(out) model = Model(inputs=inp, outputs=out) model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy', 'mean_squared_error']) name = 'icnet' if plot: plot_model(model, to_file='{}.png'.format(name), show_shapes=True) return model, name
Example #10
Source File: MaskRCNN.py From PyTorch-Luna16 with Apache License 2.0 | 5 votes |
def mrcnn_bbox_loss_graph(target_bbox, target_class_ids, pred_bbox): """Loss for Mask R-CNN bounding box refinement. target_bbox: [batch, num_rois, (dy, dx, log(dh), log(dw))] target_class_ids: [batch, num_rois]. Integer class IDs. pred_bbox: [batch, num_rois, num_classes, (dy, dx, log(dh), log(dw))] """ # Reshape to merge batch and roi dimensions for simplicity. target_class_ids = K.reshape(target_class_ids, (-1,)) target_bbox = K.reshape(target_bbox, (-1, 4)) pred_bbox = K.reshape(pred_bbox, (-1, K.int_shape(pred_bbox)[2], 4)) # Only positive ROIs contribute to the loss. And only # the right class_id of each ROI. Get their indicies. positive_roi_ix = tf.where(target_class_ids > 0)[:, 0] positive_roi_class_ids = tf.cast( tf.gather(target_class_ids, positive_roi_ix), tf.int64) indices = tf.stack([positive_roi_ix, positive_roi_class_ids], axis=1) # Gather the deltas (predicted and true) that contribute to loss target_bbox = tf.gather(target_bbox, positive_roi_ix) pred_bbox = tf.gather_nd(pred_bbox, indices) # Smooth-L1 Loss loss = K.switch(tf.size(target_bbox) > 0, smooth_l1_loss(y_true=target_bbox, y_pred=pred_bbox), tf.constant(0.0)) loss = K.mean(loss) loss = K.reshape(loss, [1, 1]) return loss
Example #11
Source File: Segmentation_Models.py From brain_segmentation with MIT License | 5 votes |
def comp_double(self): ''' double model. Simialar to two-pathway, except takes in a 4x33x33 patch and it's center 4x5x5 patch. merges paths at flatten layer. ''' print 'Compiling double model...' single = Sequential() single.add(Convolution2D(64, 7, 7, border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01), input_shape=(4,33,33))) single.add(Activation('relu')) single.add(BatchNormalization(mode=0, axis=1)) single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1))) single.add(Dropout(0.5)) single.add(Convolution2D(nb_filter=128, nb_row=5, nb_col=5, activation='relu', border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01))) single.add(BatchNormalization(mode=0, axis=1)) single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1))) single.add(Dropout(0.5)) single.add(Convolution2D(nb_filter=256, nb_row=5, nb_col=5, activation='relu', border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01))) single.add(BatchNormalization(mode=0, axis=1)) single.add(MaxPooling2D(pool_size=(2,2), strides=(1,1))) single.add(Dropout(0.5)) single.add(Convolution2D(nb_filter=128, nb_row=3, nb_col=3, activation='relu', border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01))) single.add(Dropout(0.25)) single.add(Flatten()) # add small patch to train on five = Sequential() five.add(Reshape((100,1), input_shape = (4,5,5))) five.add(Flatten()) five.add(MaxoutDense(128, nb_feature=5)) five.add(Dropout(0.5)) model = Sequential() # merge both paths model.add(Merge([five, single], mode='concat', concat_axis=1)) model.add(Dense(5)) model.add(Activation('softmax')) sgd = SGD(lr=0.001, decay=0.01, momentum=0.9) model.compile(loss='categorical_crossentropy', optimizer='sgd') print 'Done.' return model
Example #12
Source File: visual_model_zoo.py From visual_turing_test-tutorial with MIT License | 5 votes |
def create(self): model = Sequential() model.add(Reshape( input_shape=(self._visual_dim,), dims=(self._visual_dim,))) return model
Example #13
Source File: test.py From MRI-tumor-segmentation-Brats with MIT License | 5 votes |
def dense_model(patch_size, num_classes): merged_inputs = Input(shape=patch_size + (4,), name='merged_inputs') flair = Reshape(patch_size + (1,))( Lambda( lambda l: l[:, :, :, :, 0], output_shape=patch_size + (1,))(merged_inputs), ) t2 = Reshape(patch_size + (1,))( Lambda(lambda l: l[:, :, :, :, 1], output_shape=patch_size + (1,))(merged_inputs) ) t1 = Lambda(lambda l: l[:, :, :, :, 2:], output_shape=patch_size + (2,))(merged_inputs) flair = dense_net(flair) t2 = dense_net(t2) t1 = dense_net(t1) t2 = concatenate([flair, t2]) t1 = concatenate([t2, t1]) tumor = Conv3D(2, kernel_size=1, strides=1, name='tumor')(flair) core = Conv3D(3, kernel_size=1, strides=1, name='core')(t2) enhancing = Conv3D(num_classes, kernel_size=1, strides=1, name='enhancing')(t1) net = Model(inputs=merged_inputs, outputs=[tumor, core, enhancing]) return net
Example #14
Source File: bbox_3D_net.py From 3D_detection with MIT License | 5 votes |
def bbox_3D_net(input_shape=(224, 224, 3), vgg_weights=None, freeze_vgg=False, bin_num=6): vgg16_model = VGG16(include_top=False, weights=vgg_weights, input_shape=input_shape) if freeze_vgg: for layer in vgg16_model.layers: layer.trainable = False x = Flatten()(vgg16_model.output) dimension = Dense(512)(x) dimension = LeakyReLU(alpha=0.1)(dimension) dimension = Dropout(0.5)(dimension) dimension = Dense(3)(dimension) dimension = LeakyReLU(alpha=0.1, name='dimension')(dimension) orientation = Dense(256)(x) orientation = LeakyReLU(alpha=0.1)(orientation) orientation = Dropout(0.5)(orientation) orientation = Dense(bin_num * 2)(orientation) orientation = LeakyReLU(alpha=0.1)(orientation) orientation = Reshape((bin_num, -1))(orientation) orientation = Lambda(l2_normalize, name='orientation')(orientation) confidence = Dense(256)(x) confidence = LeakyReLU(alpha=0.1)(confidence) confidence = Dropout(0.5)(confidence) confidence = Dense(bin_num, activation='softmax', name='confidence')(confidence) model = Model(vgg16_model.input, outputs=[dimension, orientation, confidence]) return model
Example #15
Source File: my_models.py From Audio-Vision with MIT License | 5 votes |
def visual_lstm(img_vec_dim, activation_1,activation_2, dropout, vocabulary_size, num_hidden_units_lstm, max_ques_length, word_emb_dim, num_hidden_layers_mlp, num_hidden_units_mlp, nb_classes, class_activation,embedding_matrix): # Make image model inpx1=Input(shape=(img_vec_dim,)) x1=Dense(embedding_matrix.shape[1], activation='tanh')(inpx1) x1=Reshape((1,embedding_matrix.shape[1]))(x1) image_model = Model([inpx1],x1) image_model.summary() # Make language Model inpx0=Input(shape=(max_ques_length,)) x0=Embedding(vocabulary_size, word_emb_dim, weights=[embedding_matrix], trainable=False)(inpx0) x2=Dense(embedding_matrix.shape[1],activation='tanh')(x0) x2=Dropout(dropout)(x2) # Make embedding_model embedding_model = Model([inpx0],x2) embedding_model.summary() # Make combined model model = Sequential() model.add(Merge([image_model,embedding_model],mode = 'concat', concat_axis=1)) model.add(LSTM(num_hidden_units_lstm, return_sequences=False, go_backwards=True)) model.add(Dense(num_hidden_units_mlp)) model.add(Activation('relu')) model.add(Dropout(dropout)) model.summary() model.add(Dense(nb_classes)) model.add(Activation(class_activation)) return model
Example #16
Source File: my_models.py From Audio-Vision with MIT License | 5 votes |
def visual_lstm2(img_vec_dim, activation_1,activation_2, dropout, vocabulary_size, num_hidden_units_lstm, max_ques_length, word_emb_dim, num_hidden_layers_mlp, num_hidden_units_mlp, nb_classes, class_activation,embedding_matrix): # Make image model inpx1=Input(shape=(img_vec_dim,)) x1=Dense(embedding_matrix.shape[1], activation=activation_1)(inpx1) x1=Reshape((1,embedding_matrix.shape[1]))(x1) image_model = Model([inpx1],x1) image_model.summary() # Make language Model inpx0=Input(shape=(max_ques_length,)) x0=Embedding(vocabulary_size, word_emb_dim, weights=[embedding_matrix], trainable=False)(inpx0) x2=Dense(embedding_matrix.shape[1],activation=activation_2)(x0) x2=Dropout(dropout)(x2) # Make embedding_model embedding_model = Model([inpx0],x2) embedding_model.summary() inpx2=Input(shape=(img_vec_dim,)) x1=Dense(embedding_matrix.shape[1], activation=activation_1)(inpx1) x3=Reshape((1,embedding_matrix.shape[1]))(x1) image_model2 = Model([inpx2],x3) image_model2.summary() # Make combined model model = Sequential() model.add(Merge([image_model,embedding_model, image_model2],mode = 'concat', concat_axis=1)) model.add(Bidirectional(LSTM(num_hidden_units_lstm, return_sequences=False))) model.add(Dense(num_hidden_units_mlp)) model.add(Activation(activation_1)) model.add(Dropout(dropout)) model.summary() model.add(Dense(nb_classes)) model.add(Activation(class_activation)) return model
Example #17
Source File: test_core.py From CAPTCHA-breaking with MIT License | 5 votes |
def test_reshape(self): layer = core.Reshape(10, 10) self._runner(layer)
Example #18
Source File: my_models.py From Audio-Vision with MIT License | 5 votes |
def basic_mlp(img_vec_dim, vocabulary_size, word_emb_dim, max_ques_length, num_hidden_units_lstm, num_hidden_layers_mlp, num_hidden_units_mlp, dropout, nb_classes, class_activation): # Image model model_image = Sequential() model_image.add(Reshape((img_vec_dim,), input_shape=(img_vec_dim,))) # Language Model model_language = Sequential() model_language.add(Embedding(vocabulary_size, word_emb_dim, input_length=max_ques_length)) model_language.add(LSTM(num_hidden_units_lstm, return_sequences=True, input_shape=(max_ques_length, word_emb_dim))) model_language.add(LSTM(num_hidden_units_lstm, return_sequences=True)) model_language.add(LSTM(num_hidden_units_lstm, return_sequences=False)) # combined model model = Sequential() model.add(Merge([model_language, model_image], mode='concat', concat_axis=1)) for i in xrange(num_hidden_layers_mlp): model.add(Dense(num_hidden_units_mlp)) model.add(Dropout(dropout)) model.add(Dense(nb_classes)) model.add(Activation(class_activation)) return model
Example #19
Source File: network.py From shopping-classification with Apache License 2.0 | 5 votes |
def get_model(self, num_classes, activation='sigmoid'): max_len = opt.max_len voca_size = opt.unigram_hash_size + 1 with tf.device('/gpu:0'): embd = Embedding(voca_size, opt.embd_size, name='uni_embd') t_uni = Input((max_len,), name="input_1") t_uni_embd = embd(t_uni) # token w_uni = Input((max_len,), name="input_2") w_uni_mat = Reshape((max_len, 1))(w_uni) # weight uni_embd_mat = dot([t_uni_embd, w_uni_mat], axes=1) uni_embd = Reshape((opt.embd_size, ))(uni_embd_mat) embd_out = Dropout(rate=0.5)(uni_embd) relu = Activation('relu', name='relu1')(embd_out) outputs = Dense(num_classes, activation=activation)(relu) model = Model(inputs=[t_uni, w_uni], outputs=outputs) optm = keras.optimizers.Nadam(opt.lr) model.compile(loss='binary_crossentropy', optimizer=optm, metrics=[top1_acc]) model.summary(print_fn=lambda x: self.logger.info(x)) return model
Example #20
Source File: model.py From DeepLearn with MIT License | 4 votes |
def cnn(embedding_matrix, dimx=50, dimy=50, nb_filter = 120, embedding_dim = 50,filter_length = (50,4), vocab_size = 8000, depth = 1): print 'Model Uses Basic CNN......' inpx = Input(shape=(dimx,),dtype='int32',name='inpx') inpy = Input(shape=(dimy,),dtype='int32',name='inpy') x = word2vec_embedding_layer(embedding_matrix,train=False)(inpx) y = word2vec_embedding_layer(embedding_matrix,train=False)(inpy) x = Permute((2,1))(x) y = Permute((2,1))(y) conv1 = Reshape((embedding_dim,dimx,1))(x) conv2 = Reshape((embedding_dim,dimy,1))(y) channel_1, channel_2 = [], [] for dep in range(depth): #conv1 = ZeroPadding2D((filter_width - 1, 0))(conv1) #conv2 = ZeroPadding2D((filter_width - 1, 0))(conv2) ques = Conv2D(nb_filter=nb_filter, kernel_size = filter_length, activation='relu', data_format = 'channels_last',border_mode="valid")(conv1) ans = Conv2D(nb_filter, kernel_size = filter_length, activation='relu', data_format="channels_last",border_mode="valid")(conv2) #conv1 = GlobalMaxPooling2D()(ques) #conv2 = GlobalMaxPooling2D()(ans) #conv1 = MaxPooling2D()(ques) #conv2 = MaxPooling2D()(ans) channel_1.append(GlobalMaxPooling2D()(ques)) channel_2.append(GlobalMaxPooling2D()(ans)) #channel_1.append(GlobalAveragePooling2D()(ques)) #channel_2.append(GlobalAveragePooling2D()(ans)) h1 = channel_1.pop(-1) if channel_1: h1 = merge([h1] + channel_1, mode="concat") h2 = channel_2.pop(-1) if channel_2: h2 = merge([h2] + channel_2, mode="concat") h = Merge(mode="concat",name='h')([h1, h2]) #h = Dropout(0.2)(h) #h = Dense(50, kernel_regularizer=regularizers.l2(reg2),activation='relu')(h) #wrap = Dropout(0.5)(h) #wrap = Dense(64, activation='tanh')(h) score = Dense(2,activation='softmax',name='score')(h) model = Model([inpx, inpy],[score]) model.compile( loss='categorical_crossentropy',optimizer='adam') return model
Example #21
Source File: MaskRCNN.py From PyTorch-Luna16 with Apache License 2.0 | 4 votes |
def fpn_classifier_graph(rois, feature_maps, image_shape, pool_size, num_classes): """Builds the computation graph of the feature pyramid network classifier and regressor heads. rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized coordinates. feature_maps: List of feature maps from diffent layers of the pyramid, [P2, P3, P4, P5]. Each has a different resolution. image_shape: [height, width, depth] pool_size: The width of the square feature map generated from ROI Pooling. num_classes: number of classes, which determines the depth of the results Returns: logits: [N, NUM_CLASSES] classifier logits (before softmax) probs: [N, NUM_CLASSES] classifier probabilities bbox_deltas: [N, (dy, dx, log(dh), log(dw))] Deltas to apply to proposal boxes """ # ROI Pooling # Shape: [batch, num_boxes, pool_height, pool_width, channels] x = PyramidROIAlign([pool_size, pool_size], image_shape, name="roi_align_classifier")([rois] + feature_maps) # Two 1024 FC layers (implemented with Conv2D for consistency) x = TimeDistributed(Conv2D(1024, (pool_size, pool_size), padding="valid"), name="mrcnn_class_conv1")(x) x = TimeDistributed(BatchNorm(axis=3), name='mrcnn_class_bn1')(x) x = Activation('relu')(x) x = TimeDistributed(Conv2D(1024, (1, 1)), name="mrcnn_class_conv2")(x) x = TimeDistributed(BatchNorm(axis=3), name='mrcnn_class_bn2')(x) x = Activation('relu')(x) shared = Lambda(lambda x: K.squeeze(K.squeeze(x, 3), 2), name="pool_squeeze")(x) # Classifier head mrcnn_class_logits = TimeDistributed(Dense(num_classes), name='mrcnn_class_logits')(shared) mrcnn_probs = TimeDistributed(Activation("softmax"), name="mrcnn_class")(mrcnn_class_logits) # BBox head # [batch, boxes, num_classes * (dy, dx, log(dh), log(dw))] x = TimeDistributed(Dense(num_classes * 4, activation='linear'), name='mrcnn_bbox_fc')(shared) # Reshape to [batch, boxes, num_classes, (dy, dx, log(dh), log(dw))] s = K.int_shape(x) mrcnn_bbox = Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) return mrcnn_class_logits, mrcnn_probs, mrcnn_bbox
Example #22
Source File: model.py From DeepLearn with MIT License | 4 votes |
def cnn(embedding_matrix, dimx=50, dimy=50, nb_filter = 120, embedding_dim = 50,filter_length = (50,4), vocab_size = 8000, depth = 1): print 'Model Uses Basic CNN......' inpx = Input(shape=(dimx,),dtype='int32',name='inpx') inpy = Input(shape=(dimy,),dtype='int32',name='inpy') x = word2vec_embedding_layer(embedding_matrix,train=False)(inpx) y = word2vec_embedding_layer(embedding_matrix,train=False)(inpy) x = Permute((2,1))(x) y = Permute((2,1))(y) conv1 = Reshape((embedding_dim,dimx,1))(x) conv2 = Reshape((embedding_dim,dimy,1))(y) channel_1, channel_2 = [], [] for dep in range(depth): #conv1 = ZeroPadding2D((filter_width - 1, 0))(conv1) #conv2 = ZeroPadding2D((filter_width - 1, 0))(conv2) ques = Conv2D(nb_filter=nb_filter, kernel_size = filter_length, activation='relu', data_format = 'channels_last',border_mode="valid")(conv1) ans = Conv2D(nb_filter, kernel_size = filter_length, activation='relu', data_format="channels_last",border_mode="valid")(conv2) #conv1 = GlobalMaxPooling2D()(ques) #conv2 = GlobalMaxPooling2D()(ans) #conv1 = MaxPooling2D()(ques) #conv2 = MaxPooling2D()(ans) channel_1.append(GlobalMaxPooling2D()(ques)) channel_2.append(GlobalMaxPooling2D()(ans)) #channel_1.append(GlobalAveragePooling2D()(ques)) #channel_2.append(GlobalAveragePooling2D()(ans)) h1 = channel_1.pop(-1) if channel_1: h1 = merge([h1] + channel_1, mode="concat") h2 = channel_2.pop(-1) if channel_2: h2 = merge([h2] + channel_2, mode="concat") h = Merge(mode="concat",name='h')([h1, h2]) #h = Dropout(0.2)(h) #h = Dense(50, kernel_regularizer=regularizers.l2(reg2),activation='relu')(h) #wrap = Dropout(0.5)(h) #wrap = Dense(64, activation='tanh')(h) score = Dense(2,activation='softmax',name='score')(h) model = Model([inpx, inpy],[score]) model.compile( loss='categorical_crossentropy',optimizer='adam') return model
Example #23
Source File: MaskRCNN.py From PyTorch-Luna16 with Apache License 2.0 | 4 votes |
def mrcnn_mask_loss_graph(target_masks, target_class_ids, pred_masks): """Mask binary cross-entropy loss for the masks head. target_masks: [batch, num_rois, height, width]. A float32 tensor of values 0 or 1. Uses zero padding to fill array. target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded. pred_masks: [batch, proposals, height, width, num_classes] float32 tensor with values from 0 to 1. """ # Reshape for simplicity. Merge first two dimensions into one. target_class_ids = K.reshape(target_class_ids, (-1,)) mask_shape = tf.shape(target_masks) target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3])) pred_shape = tf.shape(pred_masks) pred_masks = K.reshape(pred_masks, (-1, pred_shape[2], pred_shape[3], pred_shape[4])) # Permute predicted masks to [N, num_classes, height, width] pred_masks = tf.transpose(pred_masks, [0, 3, 1, 2]) # Only positive ROIs contribute to the loss. And only # the class specific mask of each ROI. positive_ix = tf.where(target_class_ids > 0)[:, 0] positive_class_ids = tf.cast( tf.gather(target_class_ids, positive_ix), tf.int64) indices = tf.stack([positive_ix, positive_class_ids], axis=1) # Gather the masks (predicted and true) that contribute to loss y_true = tf.gather(target_masks, positive_ix) y_pred = tf.gather_nd(pred_masks, indices) # Compute binary cross entropy. If no positive ROIs, then return 0. # shape: [batch, roi, num_classes] loss = K.switch(tf.size(y_true) > 0, K.binary_crossentropy(target=y_true, output=y_pred), tf.constant(0.0)) loss = K.mean(loss) loss = K.reshape(loss, [1, 1]) return loss ############################################################ # Data Generator ############################################################
Example #24
Source File: CorrMCNN_Arch2.py From DeepLearn with MIT License | 4 votes |
def buildModel(loss_type,lamda): inpx = Input(shape=(dimx,)) inpy = Input(shape=(dimy,)) hx = Reshape((28,14,1))(inpx) hx = Conv2D(128, (3, 3), activation='relu', padding='same')(hx) hx = MaxPooling2D((2, 2), padding='same')(hx) hx = Conv2D(64, (3, 3), activation='relu', padding='same')(hx) hx = MaxPooling2D((2, 2), padding='same')(hx) hx = Conv2D(49, (3, 3), activation='relu', padding='same')(hx) hx = MaxPooling2D((2, 2), padding='same')(hx) hx = Flatten()(hx) hx1 = Dense(hdim_deep,activation='sigmoid')(hx) hx2 = Dense(hdim_deep2, activation='sigmoid',name='hid_l1')(hx1) hx = Dense(hdim, activation='sigmoid',name='hid_l')(hx2) hy = Reshape((28,14,1))(inpy) hy = Conv2D(128, (3, 3), activation='relu', padding='same')(hy) hy = MaxPooling2D((2, 2), padding='same')(hy) hy = Conv2D(64, (3, 3), activation='relu', padding='same')(hy) hy = MaxPooling2D((2, 2), padding='same')(hy) hy = Conv2D(49, (3, 3), activation='relu', padding='same')(hy) hy = MaxPooling2D((2, 2), padding='same')(hy) hy = Flatten()(hy) hy1 = Dense(hdim_deep,activation='sigmoid')(hy) hy2 = Dense(hdim_deep2, activation='sigmoid',name='hid_r1')(hy1) hy = Dense(hdim, activation='sigmoid',name='hid_r')(hy2) h = Merge(mode="sum")([hx,hy]) recx = Dense(dimx)(h) recy = Dense(dimy)(h) branchModel = Model( [inpx,inpy],[recx,recy,h,hx1,hy1,hx2,hy2]) [recx1,recy1,h1,_,_,_,_] = branchModel( [inpx, ZeroPadding()(inpy)]) [recx2,recy2,h2,_,_,_,_] = branchModel( [ZeroPadding()(inpx), inpy ]) #you may probably add a reconstruction from combined [recx3,recy3,h3,hx_1,hy_1,hx_2,hy_2] = branchModel([inpx, inpy]) lamda2,lamda3 = 0.001,0.05 corr1=CorrnetCost(-lamda)([h1,h2]) corr2=CorrnetCost(-lamda2)([hx_1,hy_1]) corr3=CorrnetCost(-lamda3)([hx_2,hy_2]) model = Model( [inpx,inpy],[recy1,recx2,recx1,recy2,corr1,corr2,corr3]) model.compile( loss=["mse","mse","mse","mse",corr_loss,corr_loss,corr_loss],optimizer="rmsprop") return model, branchModel
Example #25
Source File: fdssc_model.py From FDSSC with MIT License | 4 votes |
def build(input_shape, num_outputs): global growth_rate growth_rate = 12 _handle_dim_ordering() if len(input_shape) != 4: raise Exception("Input shape should be a tuple (nb_channels, kernel_dim1, kernel_dim2, kernel_dim3)") # Permute dimension order if necessary if K.image_dim_ordering() == 'tf': input_shape = (input_shape[1], input_shape[2], input_shape[3], input_shape[0]) input = Input(shape=input_shape) print("the dim of input:", input._keras_shape[3]) # Dense spectral block x1_0 = Conv3D(kernel_initializer='he_normal', strides=(1, 1, 2), kernel_regularizer=regularizers.l2(0.0001), filters=24, kernel_size=(1, 1, 7), padding='valid')(input) x1_1 = spectral_conv(x1_0) x1_1_ = concatenate([x1_0, x1_1], axis=CHANNEL_AXIS) x1_2 = spectral_conv(x1_1_) x1_2_ = concatenate([x1_0, x1_1, x1_2], axis=CHANNEL_AXIS) x1_3 = spectral_conv(x1_2_) x1 = concatenate([x1_0, x1_1, x1_2, x1_3], axis=CHANNEL_AXIS) x1 = bn_prelu(x1) print('the output of dense spectral block:', x1._keras_shape) # Reducing dimension layer # x1 = Conv3D(kernel_initializer='he_normal', strides=(1, 1, 1), kernel_regularizer=regularizers.l2(0.0001), # filters=24, kernel_size=(1, 1, 1), padding='valid')(x1) tran1 = Conv3D(kernel_initializer='he_normal', strides=(1, 1, 1), kernel_regularizer=regularizers.l2(0.0001), filters=200, kernel_size=(1, 1, x1._keras_shape[CONV_DIM3]), padding='valid')(x1) print(tran1._keras_shape) tran1 = bn_prelu(tran1) tran2 = Reshape((tran1._keras_shape[CONV_DIM1], tran1._keras_shape[CONV_DIM2], tran1._keras_shape[CHANNEL_AXIS], 1))(tran1) x2_0 = Conv3D(kernel_initializer='he_normal', strides=(1, 1, 1), kernel_regularizer=regularizers.l2(0.0001), filters=24, kernel_size=(3, 3, 200), padding='valid')(tran2) print('the input of dense spatial block:', x2_0._keras_shape) # Dense spatial block x2_1 = spatial_conv(x2_0) x2_1_ = concatenate([x2_0, x2_1], axis=CHANNEL_AXIS) x2_2 = spatial_conv(x2_1_) x2_2_ = concatenate([x2_0, x2_1, x2_2], axis=CHANNEL_AXIS) x2_3 = spatial_conv(x2_2_) x2 = concatenate([x2_0, x2_1, x2_2, x2_3], axis=CHANNEL_AXIS) print('the output of dense spectral block is:', x2._keras_shape) x2 = bn_prelu(x2) # Classifier block pool1 = AveragePooling3D(pool_size=(x2._keras_shape[1], x2._keras_shape[2], 1), strides=(1, 1, 1))(x2) flatten1 = Flatten()(pool1) drop1 = Dropout(0.5)(flatten1) dense = Dense(units=num_outputs, activation="softmax", kernel_initializer="glorot_normal")(drop1) model = Model(inputs=input, outputs=dense) return model
Example #26
Source File: embeddings.py From fancy-cnn with MIT License | 4 votes |
def sentence_embedding(sentence_len, wv_params, wv_size, input_name='sentence_embedding', output_name='vector_embedding'): ''' Creates an embedding of word vectors into a sentence image. Args: ----- sentence_len: length of sentences to be passed wv_params: a dict of the following format wv_params = { 'fixed_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : True }, 'floating_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : False } } the keys of the dictionary are the names in the keras graph model, and you can have any number of word vector layers encoded. input_name: the name of the input node for the graph output_name: the name of the output node for the graph Returns: -------- a keras container that takes as input an integer array with shape (n_samples, n_words), and returns shape (n_samples, wv_channels, len_sentence, wv_dim)! ''' # -- output is (n_samples, n_channels, n_words, wv_dim) g = SubGraph() if KERAS_BACKEND: g.add_input(input_name, (sentence_len, ), dtype='int') else: g.add_input(input_name, (-1, ), dtype='int') for name, params in wv_params.iteritems(): # g.add_input(params['input_name'], (-1, ), dtype='int') g.add_node(make_embedding(wv_size=wv_size, **params), name=name, input=input_name) g.add_node(Reshape((sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') g.add_node(Permute(dims=(2, 1, 3)), name='permute', input='reshape') # -- output is of shape (nb_samples, nb_wv_channels, len_sentence, wv_dim) g.add_output(name=output_name, input='permute') return g
Example #27
Source File: models_GAN.py From DeepLearningImplementations with MIT License | 4 votes |
def generator_upsampling(noise_dim, img_dim, bn_mode, model_name="generator_upsampling", dset="mnist"): """ Generator model of the DCGAN args : img_dim (tuple of int) num_chan, height, width pretr_weights_file (str) file holding pre trained weights returns : model (keras NN) the Neural Net model """ s = img_dim[1] f = 512 if dset == "mnist": start_dim = int(s / 4) nb_upconv = 2 else: start_dim = int(s / 16) nb_upconv = 4 if K.image_data_format() == "channels_first": bn_axis = 1 reshape_shape = (f, start_dim, start_dim) output_channels = img_dim[0] else: reshape_shape = (start_dim, start_dim, f) bn_axis = -1 output_channels = img_dim[-1] gen_input = Input(shape=noise_dim, name="generator_input") x = Dense(f * start_dim * start_dim, input_dim=noise_dim)(gen_input) x = Reshape(reshape_shape)(x) x = BatchNormalization(axis=bn_axis)(x) x = Activation("relu")(x) # Upscaling blocks for i in range(nb_upconv): x = UpSampling2D(size=(2, 2))(x) nb_filters = int(f / (2 ** (i + 1))) x = Conv2D(nb_filters, (3, 3), padding="same")(x) x = BatchNormalization(axis=1)(x) x = Activation("relu")(x) x = Conv2D(nb_filters, (3, 3), padding="same")(x) x = Activation("relu")(x) x = Conv2D(output_channels, (3, 3), name="gen_Conv2D_final", padding="same", activation='tanh')(x) generator_model = Model(inputs=[gen_input], outputs=[x], name=model_name) return generator_model
Example #28
Source File: models_GAN.py From DeepLearningImplementations with MIT License | 4 votes |
def generator_deconv(noise_dim, img_dim, bn_mode, batch_size, model_name="generator_deconv", dset="mnist"): """ Generator model of the DCGAN args : nb_classes (int) number of classes img_dim (tuple of int) num_chan, height, width pretr_weights_file (str) file holding pre trained weights returns : model (keras NN) the Neural Net model """ assert K.backend() == "tensorflow", "Deconv not implemented with theano" s = img_dim[1] f = 512 if dset == "mnist": start_dim = int(s / 4) nb_upconv = 2 else: start_dim = int(s / 16) nb_upconv = 4 reshape_shape = (start_dim, start_dim, f) bn_axis = -1 output_channels = img_dim[-1] gen_input = Input(shape=noise_dim, name="generator_input") x = Dense(f * start_dim * start_dim, input_dim=noise_dim)(gen_input) x = Reshape(reshape_shape)(x) x = BatchNormalization(axis=bn_axis)(x) x = Activation("relu")(x) # Transposed conv blocks for i in range(nb_upconv - 1): nb_filters = int(f / (2 ** (i + 1))) s = start_dim * (2 ** (i + 1)) o_shape = (batch_size, s, s, nb_filters) x = Deconv2D(nb_filters, (3, 3), output_shape=o_shape, strides=(2, 2), padding="same")(x) x = BatchNormalization(axis=-1)(x) x = Activation("relu")(x) # Last block s = start_dim * (2 ** (nb_upconv)) o_shape = (batch_size, s, s, output_channels) x = Deconv2D(output_channels, (3, 3), output_shape=o_shape, strides=(2, 2), padding="same")(x) x = Activation("tanh")(x) generator_model = Model(inputs=[gen_input], outputs=[x], name=model_name) return generator_model
Example #29
Source File: models_WGAN.py From DeepLearningImplementations with MIT License | 4 votes |
def generator_upsampling(noise_dim, img_dim, bn_mode, model_name="generator_upsampling", dset="mnist"): """DCGAN generator based on Upsampling and Conv2D Args: noise_dim: Dimension of the noise input img_dim: dimension of the image output bn_mode: keras batchnorm mode model_name: model name (default: {"generator_upsampling"}) dset: dataset (default: {"mnist"}) Returns: keras model """ s = img_dim[1] f = 512 if dset == "mnist": start_dim = int(s / 4) nb_upconv = 2 else: start_dim = int(s / 16) nb_upconv = 4 if K.image_dim_ordering() == "th": bn_axis = 1 reshape_shape = (f, start_dim, start_dim) output_channels = img_dim[0] else: reshape_shape = (start_dim, start_dim, f) bn_axis = -1 output_channels = img_dim[-1] gen_input = Input(shape=noise_dim, name="generator_input") # Noise input and reshaping x = Dense(f * start_dim * start_dim, input_dim=noise_dim)(gen_input) x = Reshape(reshape_shape)(x) x = BatchNormalization(axis=bn_axis)(x) x = Activation("relu")(x) # Upscaling blocks: Upsampling2D->Conv2D->ReLU->BN->Conv2D->ReLU for i in range(nb_upconv): x = UpSampling2D(size=(2, 2))(x) nb_filters = int(f / (2 ** (i + 1))) x = Conv2D(nb_filters, (3, 3), padding="same", kernel_initializer=RandomNormal(stddev=0.02))(x) x = BatchNormalization(axis=1)(x) x = Activation("relu")(x) x = Conv2D(nb_filters, (3, 3), padding="same", kernel_initializer=RandomNormal(stddev=0.02))(x) x = Activation("relu")(x) # Last Conv to get the output image x = Conv2D(output_channels, (3, 3), name="gen_conv2d_final", padding="same", activation='tanh', kernel_initializer=RandomNormal(stddev=0.02))(x) generator_model = Model(inputs=[gen_input], outputs=[x], name=model_name) visualize_model(generator_model) return generator_model
Example #30
Source File: models.py From DeepLearningImplementations with MIT License | 4 votes |
def generator_upsampling(cat_dim, cont_dim, noise_dim, img_dim, bn_mode, model_name="generator_upsampling", dset="mnist"): """ Generator model of the DCGAN args : img_dim (tuple of int) num_chan, height, width pretr_weights_file (str) file holding pre trained weights returns : model (keras NN) the Neural Net model """ s = img_dim[1] f = 128 if dset == "mnist": start_dim = int(s / 4) nb_upconv = 2 else: start_dim = int(s / 16) nb_upconv = 4 if K.image_dim_ordering() == "th": bn_axis = 1 reshape_shape = (f, start_dim, start_dim) output_channels = img_dim[0] else: reshape_shape = (start_dim, start_dim, f) bn_axis = -1 output_channels = img_dim[-1] cat_input = Input(shape=cat_dim, name="cat_input") cont_input = Input(shape=cont_dim, name="cont_input") noise_input = Input(shape=noise_dim, name="noise_input") gen_input = merge([cat_input, cont_input, noise_input], mode="concat") x = Dense(1024)(gen_input) x = BatchNormalization()(x) x = Activation("relu")(x) x = Dense(f * start_dim * start_dim)(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = Reshape(reshape_shape)(x) # Upscaling blocks for i in range(nb_upconv): x = UpSampling2D(size=(2, 2))(x) nb_filters = int(f / (2 ** (i + 1))) x = Conv2D(nb_filters, (3, 3), padding="same")(x) x = BatchNormalization(axis=bn_axis)(x) x = Activation("relu")(x) # x = Conv2D(nb_filters, (3, 3), padding="same")(x) # x = BatchNormalization(axis=bn_axis)(x) # x = Activation("relu")(x) x = Conv2D(output_channels, (3, 3), name="gen_Conv2D_final", padding="same", activation='tanh')(x) generator_model = Model(inputs=[cat_input, cont_input, noise_input], outputs=[x], name=model_name) return generator_model