我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.layers.core.Reshape()。
def simple_cnn(agent, env, dropout=0, learning_rate=1e-3, **args): with tf.device("/cpu:0"): state = tf.placeholder('float', [None, agent.input_dim]) S = Input(shape=[agent.input_dim]) h = Reshape( agent.input_dim_orig )(S) h = TimeDistributed( Convolution2D(16, 8, 8, subsample=(4, 4), border_mode='same', activation='relu', dim_ordering='tf'))(h) # h = Dropout(dropout)(h) h = TimeDistributed( Convolution2D(32, 4, 4, subsample=(2, 2), border_mode='same', activation='relu', dim_ordering='tf'))(h) h = Flatten()(h) # h = Dropout(dropout)(h) h = Dense(256, activation='relu')(h) # h = Dropout(dropout)(h) h = Dense(128, activation='relu')(h) V = Dense(env.action_space.n, activation='linear',init='zero')(h) model = Model(S, V) model.compile(loss='mse', optimizer=RMSprop(lr=learning_rate) ) return state, model
def conv_net(): model = Sequential() model.add(Conv1D(nb_filter=20, filter_length=1, input_shape=(10, 55))) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Reshape((200,))) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(128)) model.add(Activation('relu')) model.add(Dropout(0.5)) #model.add(Dense(128)) #model.add(Activation('relu')) #model.add(Dropout(0.5)) model.add(Dense(5, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) return nn(lambda: model)
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
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
def __init__(self, data_root): # Dimensionality of image features img_dim = 4096 self._model = Sequential() self._model.add(Reshape(input_shape=(img_dim,), target_shape=(img_dim,))) # Load the precomputed VGG features print("Loading VGG features...") pretrained_vgg_model_fpath = pjoin(data_root, 'coco', 'vgg_feats.mat') features_struct = scipy.io.loadmat(pretrained_vgg_model_fpath) self._vgg_features = features_struct['feats'] image_ids = lines(pjoin(data_root, 'coco_vgg_IDMap.txt')) print ("Done.") self._id_map = {} for ids in image_ids: id_split = ids.split() self._id_map[id_split[0]] = int(id_split[1])
def make_dcgan_generator(Xk_g, n_lat, n_chan=1): n_g_hid1 = 1024 # size of hidden layer in generator layer 1 n_g_hid2 = 128 # size of hidden layer in generator layer 2 x = Dense(n_g_hid1)(Xk_g) x = BatchNormalization(mode=2)(x) x = Activation('relu')(x) x = Dense(n_g_hid2*7*7)(x) x = BatchNormalization(mode=2)(x) x = Activation('relu')(x) x = Reshape((n_g_hid2, 7, 7))(x) x = Deconvolution2D(64, 5, 5, output_shape=(128, 64, 14, 14), border_mode='same', activation=None, subsample=(2,2), init='orthogonal', dim_ordering='th')(x) x = BatchNormalization(mode=2, axis=1)(x) x = Activation('relu')(x) g = Deconvolution2D(n_chan, 5, 5, output_shape=(128, n_chan, 28, 28), border_mode='same', activation='sigmoid', subsample=(2,2), init='orthogonal', dim_ordering='th')(x) return g
def make_dcgan_generator(Xk_g, n_lat, n_chan=1): n_g_hid1 = 1024 # size of hidden layer in generator layer 1 n_g_hid2 = 128 # size of hidden layer in generator layer 2 x = Dense(n_g_hid1, init=conv2D_init)(Xk_g) x = BatchNormalization(mode=2, )(x) x = Activation('relu')(x) x = Dense(n_g_hid2*7*7, init=conv2D_init)(x) x = Reshape((n_g_hid2, 7, 7))(x) x = BatchNormalization(mode=2, axis=1)(x) x = Activation('relu')(x) x = Deconvolution2D(64, 5, 5, output_shape=(128, 64, 14, 14), border_mode='same', activation=None, subsample=(2,2), init=conv2D_init, dim_ordering='th')(x) x = BatchNormalization(mode=2, axis=1)(x) x = Activation('relu')(x) g = Deconvolution2D(n_chan, 5, 5, output_shape=(128, n_chan, 28, 28), border_mode='same', activation='sigmoid', subsample=(2,2), init=conv2D_init, dim_ordering='th')(x) return g
def create_resnet50(input_img): net = ResNet50(weights='imagenet', include_top=False, input_tensor=input_img) for layer in net.layers[1:]: layer.trainable = False net = Reshape((-1,))(net.outputs[0]) return net
def get_decoder(C_1, C_2, C_3, c, c2, d, mp, mp2, nb_pool): decoder = models.Sequential() decoder.add(DependentDense(d.input_shape[1], d, input_shape=(d.output_shape[1],))) decoder.add(Activation('tanh')) decoder.add(Reshape((C_2, 8, 8))) # ==================================================== # decoder.add(DePool2D(mp3, size=(nb_pool, nb_pool))) # decoder.add(Deconvolution2D(c3, nb_out_channels=C_2, border_mode='same')) # decoder.add(Activation('tanh')) # ==================================================== decoder.add(DePool2D(mp2, size=(nb_pool, nb_pool))) decoder.add(Deconvolution2D(c2, nb_out_channels=C_1, border_mode='same')) decoder.add(Activation('tanh')) # ==================================================== decoder.add(DePool2D(mp, size=(nb_pool, nb_pool))) decoder.add(Deconvolution2D(c, nb_out_channels=3, border_mode='same')) decoder.add(Activation('tanh')) # ==================================================== return decoder
def build_model(nb_filters=32, nb_pool=2, nb_conv=3): model = models.Sequential() d = Dense(30) c = Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='same', input_shape=(1, 28, 28)) mp =MaxPooling2D(pool_size=(nb_pool, nb_pool)) # ========= ENCODER ======================== model.add(c) model.add(Activation('tanh')) model.add(mp) model.add(Dropout(0.25)) # ========= BOTTLENECK ====================== model.add(Flatten()) model.add(d) model.add(Activation('tanh')) # ========= BOTTLENECK^-1 ===================== model.add(DependentDense(nb_filters * 14 * 14, d)) model.add(Activation('tanh')) model.add(Reshape((nb_filters, 14, 14))) # ========= DECODER ========================= model.add(DePool2D(mp, size=(nb_pool, nb_pool))) model.add(Deconvolution2D(c, border_mode='same')) model.add(Activation('tanh')) return model
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
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
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
def my_model(dropout): ############ model params ################ line_length = 248 # seq size train_char = 58 hidden_neurons = 512 # hidden neurons batch = 64 # batch_size no_epochs = 5 ################### Model ################ model = Sequential() # layer 1 model.add(LSTM(hidden_neurons, return_sequences=True, input_shape=(line_length, train_char))) model.add(Dropout(dropout)) # layer 2 model.add(LSTM(hidden_neurons, return_sequences=True)) model.add(Dropout(dropout)) # layer 3 model.add(LSTM(hidden_neurons, return_sequences=True)) model.add(Dropout(dropout)) model.add(Reshape((248, 512))) # fc layer model.add(TimeDistributed(Dense(58, activation='softmax'))) # model.load_weights("weights/model_maha1_noep50_batch64_seq_248.hdf5") # model.layers.pop() # model.layers.pop() # model.add(Dropout(dropout)) #model.add(TimeDistributed(Dense(train_char, activation='softmax'))) initlr = 0.00114 adagrad = Adagrad(lr=initlr, epsilon=1e-08) model.compile(optimizer=adagrad, loss='categorical_crossentropy', metrics=['accuracy']) ###load weights#### return model
def test_reshape(): layer_test(core.Reshape, kwargs={'target_shape': (8, 1)}, input_shape=(3, 2, 4))
def __init__(self): print('Loading GloVe data... ', end='', flush=True) self._nlp = English() # TODO(Bernhard): try word2vec instead of glove.. print('Done.') # embedding_dims of glove embedding_dims = 300 self._model = Sequential() self._model.add(Reshape(input_shape=(embedding_dims,), target_shape=(embedding_dims,)))
def build_models(self, input_shape): middle_neurons = 10 self.encoder = Sequential() self.encoder.add(Conv2D(64, (5, 5), strides=(2, 2), padding = 'same', input_shape=input_shape)) self.encoder.add(Activation(selu)) self.encoder.add(Conv2D(128, (5, 5), strides=(2, 2), padding = 'same')) self.encoder.add(Activation(selu)) self.encoder.add(Flatten()) self.encoder.add(Dense(middle_neurons)) self.encoder.add(Activation('sigmoid')) self.encoder.summary() self.decoder = Sequential() self.decoder.add(Dense(7*7*128, input_shape=(middle_neurons,))) self.decoder.add(Activation(selu)) if keras.backend.image_data_format() == 'channels_first': self.decoder.add(Reshape([128, 7, 7])) else: self.decoder.add(Reshape([7, 7, 128])) self.decoder.add(UpSampling2D(size=(2, 2))) self.decoder.add(Conv2D(64, (5, 5), padding='same')) self.decoder.add(Activation(selu)) self.decoder.add(UpSampling2D(size=(2, 2))) self.decoder.add(Conv2D(1, (5, 5), padding='same')) self.decoder.add(Activation('sigmoid')) self.decoder.summary() self.autoencoder = Sequential() self.autoencoder.add(self.encoder) self.autoencoder.add(self.decoder) self.autoencoder.compile(loss='mean_squared_error', optimizer=Adam(lr=1e-4), metrics=['accuracy'])
def __call__(self): model = Sequential() model.add(Reshape((28, 28, 1), input_shape=(784,))) # Convolution Layer 1 model.add(Conv2D(64, kernel_size=(4, 4), strides=(2, 2), \ kernel_initializer=self.initializer)) model.add(LeakyReLU()) # Convolution Layer 2 model.add(Conv2D(128, kernel_size=(4, 4), strides=(2, 2), \ kernel_initializer=self.initializer)) model.add(LeakyReLU()) # Batch Normalization model.add(BatchNormalization()) # Flatten the input model.add(Flatten()) # Dense Layer model.add(Dense(1024, kernel_initializer=self.initializer)) model.add(LeakyReLU()) # Batch Normalization model.add(BatchNormalization()) # To the output that has two classes model.add(Dense(2, activation='softmax')) return model
def __call__(self): model = Sequential() model.add(Dense(1024, kernel_initializer=self.initializer, \ kernel_regularizer=self.regularizer, input_shape=(self.z_dim,))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dense(7 * 7 * 128, kernel_initializer=self.initializer, \ kernel_regularizer=self.regularizer)) model.add(Reshape((7, 7, 128))) model.add(BatchNormalization()) model.add(Activation('relu')) # Convolution transpose layer model.add(Conv2DTranspose(64, kernel_size=(4, 4), strides=(2, 2), padding='same',\ kernel_initializer=self.initializer, kernel_regularizer=self.regularizer)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Conv2DTranspose(1, kernel_size=(4, 4), strides=(2, 2), padding='same',\ kernel_initializer=self.initializer, kernel_regularizer=self.regularizer)) model.add(Activation('sigmoid')) model.add(Reshape((784,))) return model
def append_segmentation_branch(self, graph): graph.add_node(Convolution2D(512, 1, 1, activation='relu'), name='seg_conv1', input=self.final_common_layer) graph.add_node(Flatten(), name='seg_flat', input='seg_conv1') graph.add_node(Dense(512), name='seg_dense1', input='seg_flat') # no activation here! graph.add_node(Dense(56*56), name='seg_dense2', input='seg_dense1') graph.add_node(Reshape(dims=(56, 56)), name='seg_reshape', input='seg_dense2') graph.add_output(input='seg_reshape', name='seg_output') # usage- # fng = FullNetGenerator('Resources/vgg16_graph_weights.h5') # fn = fng.create_full_net()
def create_network(): # PixelCNN architecture, no pooling layer x = Input(batch_shape=(batch_size,n_channel,mnist_dim,mnist_dim)) # First layer using mask A x_ = Convolution2DNoFlip(*first_layer, input_shape=(1, 28, 28), border_mode='same', mask='A')(x) # Second type of layers using mask B for i in range(n_layer // 2): x_1 = Convolution2DNoFlip(*second_layer, activation='relu', border_mode='same', mask='B')(x_) x_2 = Convolution2DNoFlip(*second_layer, activation='relu', border_mode='same', mask='B')(x_1) if res_connections: x_ = merge([x_, x_2], mode='sum') else: x_ = x_2 # 2 layers of Relu followed by 1x1 conv x_ = Convolution2DNoFlip(64, 1, 1, activation='relu', border_mode='same', mask='B')(x_) x_ = Convolution2DNoFlip(128, 1, 1, activation='relu', border_mode='same', mask='B')(x_) # Depending on the output x_ = Convolution2DNoFlip(*third_layer,border_mode='same', mask='B')(x_) if MODE == '256ary': x_ = Reshape((256, mnist_dim**2))(x_) x_ = Permute((2,1))(x_) y = Activation(activation)(x_) model = Model(x, y) model.compile(optimizer='adagrad', loss=cost) print "Model compiled" return model
def Unet (nClasses , optimizer=None , input_width=360 , input_height=480 , nChannels=1 ): inputs = Input((nChannels, input_height, input_width)) conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(inputs) conv1 = Dropout(0.2)(conv1) conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(pool1) conv2 = Dropout(0.2)(conv2) conv2 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(pool2) conv3 = Dropout(0.2)(conv3) conv3 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(conv3) up1 = merge([UpSampling2D(size=(2, 2))(conv3), conv2], mode='concat', concat_axis=1) conv4 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(up1) conv4 = Dropout(0.2)(conv4) conv4 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv4) up2 = merge([UpSampling2D(size=(2, 2))(conv4), conv1], mode='concat', concat_axis=1) conv5 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(up2) conv5 = Dropout(0.2)(conv5) conv5 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv5) conv6 = Convolution2D(nClasses, 1, 1, activation='relu',border_mode='same')(conv5) conv6 = core.Reshape((nClasses,input_height*input_width))(conv6) conv6 = core.Permute((2,1))(conv6) conv7 = core.Activation('softmax')(conv6) model = Model(input=inputs, output=conv7) if not optimizer is None: model.compile(loss="categorical_crossentropy", optimizer= optimizer , metrics=['accuracy'] ) return model
def model(args): # Image model model_image = Sequential() model_image.add(Reshape((args.img_vec_dim,), input_shape=(args.img_vec_dim,))) model_image.add(Dense(args.num_hidden_units_mlp)) model_image.add(Activation(args.activation_1)) model_image.add(Dropout(args.dropout)) # Language Model model_language = Sequential() model_language.add(Embedding(args.vocabulary_size, args.word_emb_dim, input_length=args.max_ques_length)) model_language.add(LSTM(args.num_hidden_units_lstm, return_sequences=True, input_shape=(args.max_ques_length, args.word_emb_dim))) model_language.add(LSTM(args.num_hidden_units_lstm, return_sequences=True)) model_language.add(LSTM(args.num_hidden_units_lstm, return_sequences=False)) model_language.add(Dense(args.num_hidden_units_mlp)) model_language.add(Activation(args.activation_1)) model_language.add(Dropout(args.dropout)) # combined model model = Sequential() model.add(Merge([model_language, model_image], mode='mul')) # for _ in xrange(number_of_dense_layers): for i in xrange(args.num_hidden_layers_mlp): model.add(Dense(args.num_hidden_units_mlp)) model.add(Activation(args.activation_1)) model.add(Dropout(args.dropout)) model.add(Dense(args.nb_classes)) model.add(Activation(args.class_activation)) return model
def model(args): # Image model model_image = Sequential() model_image.add(Reshape((args.img_vec_dim,), input_shape=(args.img_vec_dim,))) # Language Model model_language = Sequential() model_language.add(Embedding(args.vocabulary_size, args.word_emb_dim, input_length=args.max_ques_length)) model_language.add(LSTM(args.num_hidden_units_lstm, return_sequences=True, input_shape=(args.max_ques_length, args.word_emb_dim))) model_language.add(LSTM(args.num_hidden_units_lstm, return_sequences=True)) model_language.add(LSTM(args.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(args.num_hidden_layers_mlp): model.add(Dense(args.num_hidden_units_mlp)) model.add(Dropout(args.dropout)) model.add(Dense(args.nb_classes)) model.add(Activation(args.class_activation)) return model
def test_embeddings(df, y_train): df = df[['Agencia_ID','Canal_ID']] X = np.array(df.values.copy()) y = y_train print "Before", X.shape, y.shape print np.max(X[:,0]), np.max(X[:,1]) #X = np.array([[0, 0], [1, 1], [2, 2], [3, 3], [3, 4], [5,6]]) X_list = [X[:, 0].reshape(-1, 1), X[:, 1].reshape(-1, 1)] #y = np.array([0, 0, 0, 1, 1, 6]) print "After", X.shape, y.shape, X_list[0].shape, X_list[1].shape embed1 = Sequential() embed1.add(Embedding(30000, 5, input_length=1)) embed1.add(Reshape(dims=(5,))) embed2 = Sequential() embed2.add(Embedding(12, 3, input_length=1)) embed2.add(Reshape(dims=(3,))) model = Sequential() model.add(Merge([embed1, embed2], mode='concat')) model.add(Dense(32, init='uniform')) model.add(Activation('relu')) model.add(Dense(1, init='uniform')) model.add(Activation('sigmoid')) model.compile(loss='mean_absolute_error', optimizer='adam') model.fit(X_list, y, nb_epoch=10, batch_size=4) #print_category_stats() #test_embeddings()
def __call__(self, xW, layer_idx): '''calculate gated activation maps given input maps ''' if self.stack_name == 'vertical': stack_tag = 'v' elif self.stack_name == 'horizontal': stack_tag = 'h' if self.crop_right: xW = Lambda(self._crop_right, name='h_crop_right_'+str(layer_idx))(xW) if self.v_map is not None: xW = merge([xW, self.v_map], mode='sum', name='h_merge_v_'+str(layer_idx)) if self.h is not None: hV = Dense(output_dim=2*self.nb_filters, name=stack_tag+'_dense_latent_'+str(layer_idx))(self.h) hV = Reshape((1, 1, 2*self.nb_filters), name=stack_tag+'_reshape_latent_'+str(layer_idx))(hV) #xW = merge([xW, hV], mode=lambda x: x[0]+x[1]) xW = Lambda(lambda x: x[0]+x[1], name=stack_tag+'_merge_latent_'+str(layer_idx))([xW,hV]) xW_f = Lambda(lambda x: x[:,:,:,:self.nb_filters], name=stack_tag+'_Wf_'+str(layer_idx))(xW) xW_g = Lambda(lambda x: x[:,:,:,self.nb_filters:], name=stack_tag+'_Wg_'+str(layer_idx))(xW) xW_f = Lambda(lambda x: K.tanh(x), name=stack_tag+'_tanh_'+str(layer_idx))(xW_f) xW_g = Lambda(lambda x: K.sigmoid(x), name=stack_tag+'_sigmoid_'+str(layer_idx))(xW_g) res = merge([xW_f, xW_g], mode='mul', name=stack_tag+'_merge_gate_'+str(layer_idx)) #print(type(res), K.int_shape(res), hasattr(res, '_keras_history')) return res
def build_model(): model = Sequential() # model.add(Convolution1D(16, 2, border_mode='valid', input_shape=(20, 1))) # model.add(Activation('relu')) # model.add(Convolution1D(32, 3, border_mode='valid')) # model.add(Activation('relu')) # model.add(Convolution1D(32, 2, border_mode='valid')) # model.add(Activation('relu')) # model.add(MaxPooling1D(pool_length=2)) # model.add(Flatten()) # model.add(Dense(32)) # model.add(Activation('relu')) # model.add(Reshape((32, 1))) model.add(LSTM(input_dim=1, output_dim=16, activation='relu', return_sequences=True)) model.add(Dropout(0.2)) # Dropout overfitting model.add(LSTM(32, activation='relu', return_sequences=False)) model.add(Dropout(0.2)) # Dropout overfitting # model.add(Dense(64)) # model.add(Activation("relu")) # model.add(Dropout(0.2)) # Dropout overfitting model.add(Dense(64)) model.add(Activation("softmax")) start = time.time() # sgd = SGD(lr=0.5, decay=1e-6, momentum=0.9, nesterov=True) # model.compile(loss="mse", optimizer=sgd) model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy']) # Nadam RMSprop() print "Compilation Time : ", time.time() - start return model
def seqCNN_CPTM(c_conf=(4, 3, 32, 32), p_conf=(4, 3, 32, 32), t_conf=(4, 3, 32, 32), metadata_dim=None): ''' C - Temporal Closeness P - Period T - Trend conf = (nb_flow, seq_len, map_height, map_width) metadata_dim ''' model = Sequential() components = [] for conf in [c_conf, p_conf, t_conf]: if conf is not None: components.append(seqCNNBaseLayer1_2(conf)) # nb_flow = conf[0] nb_flow, _, map_height, map_width = conf # model.add(Merge(components, mode='concat', concat_axis=1)) # concat if len(components) > 1: model.add(Merge(components, mode='sum')) else: model = components[0] model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(nb_flow, 3, 3, border_mode='same')) metadata_processor = Sequential() # metadata_processor.add(Dense(output_dim=nb_flow * map_height * map_width, input_dim=metadata_dim)) metadata_processor.add(Dense(output_dim=10, input_dim=metadata_dim)) metadata_processor.add(Activation('relu')) metadata_processor.add(Dense(output_dim=nb_flow * map_height * map_width)) metadata_processor.add(Activation('relu')) metadata_processor.add(Reshape((nb_flow, map_height, map_width))) model_final=Sequential() model_final.add(Merge([model, metadata_processor], mode='sum')) model_final.add(Activation('tanh')) return model_final
def lateFusion(metadata_dim, n_flow=2, seq_len=3, map_height=32, map_width=32): model=Sequential() mat_model=seqCNNBase(n_flow, seq_len, map_height, map_width) metadata_processor=Sequential() metadata_processor.add(Dense(output_dim=n_flow * map_height * map_width, input_dim=metadata_dim)) metadata_processor.add(Reshape((n_flow, map_height, map_width))) # metadata_processor.add(Activation('relu')) model=Sequential() model.add(Merge([mat_model, metadata_processor], mode='sum')) model.add(Activation('tanh')) return model
def create_miml_model(base_model, L, K, name="miml"): """ Arguments: base_model (Sequential): A Neural Network in keras form (e.g. VGG, GoogLeNet) L (int): number of labels K (int): number of sub categories """ model = Sequential(layers=base_model.layers, name=name) # input: feature_map.shape = (n_bags, C, H, W) _, C, H, W = model.layers[-1].output_shape print("Creating miml... input feature_map.shape={},{},{}".format(C, H, W)) n_instances = H * W # shape -> (n_bags, (L * K), n_instances, 1) model.add(Convolution2D(L * K, 1, 1, name=MIML_FIRST_LAYER_NAME)) # shape -> (n_bags, L, K, n_instances) model.add(Reshape((L, K, n_instances), name=MIML_CUBE_LAYER_NAME)) # shape -> (n_bags, L, 1, n_instances) model.add(MaxPooling2D((K, 1), strides=(1, 1))) # softmax model.add(Reshape((L, n_instances))) model.add(Permute((2, 1))) model.add(Activation("softmax")) model.add(Permute((2, 1))) model.add(Reshape((L, 1, n_instances), name=MIML_TABLE_LAYER_NAME)) # shape -> (n_bags, L, 1, 1) model.add(MaxPooling2D((1, n_instances), strides=(1, 1))) # shape -> (n_bags, L) model.add(Reshape((L,), name=MIML_OUTPUT_LAYER_NAME)) return model
def _build_network(self, vocab_size, maxlen, emb_weights=[], hidden_units=256, trainable=False): print('Build model...') model = Sequential() model.add(Embedding(vocab_size, emb_weights.shape[1], input_length=maxlen, weights=[emb_weights], trainable=trainable)) # model.add(Reshape((maxlen, emb_weights.shape[1], 1))) model.add(Convolution1D(emb_weights.shape[1], 3, kernel_initializer='he_normal', padding='valid', activation='sigmoid', input_shape=(1, maxlen))) # model.add(MaxPooling1D(pool_size=3)) model.add(Convolution1D(emb_weights.shape[1], 3, kernel_initializer='he_normal', padding='valid', activation='sigmoid', input_shape=(1, maxlen - 2))) # model.add(MaxPooling1D(pool_size=3)) model.add(Dropout(0.25)) model.add(LSTM(hidden_units, kernel_initializer='he_normal', activation='sigmoid', dropout=0.5, return_sequences=True)) model.add(LSTM(hidden_units, kernel_initializer='he_normal', activation='sigmoid', dropout=0.5)) model.add(Dense(hidden_units, kernel_initializer='he_normal', activation='sigmoid')) model.add(Dense(2, activation='softmax')) adam = Adam(lr=0.0001) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy']) print('No of parameter:', model.count_params()) print(model.summary()) return model
def _build_network(self, vocab_size, maxlen, emb_weights=[], hidden_units=256, trainable=False): print('Build model...') model = Sequential() model.add(Embedding(vocab_size, emb_weights.shape[1], input_length=maxlen, weights=[emb_weights], trainable=trainable)) model.add(Reshape((maxlen,emb_weights.shape[1],1))) model.add(BatchNormalization(momentum=0.9)) # model.add(Convolution2D(int(hidden_units/8), (5,5), kernel_initializer='he_normal', padding='valid', activation='sigmoid')) # model.add(MaxPooling2D((2,2))) # model.add(Dropout(0.5)) # # model.add(Convolution2D(int(hidden_units/4), (5,5), kernel_initializer='he_normal', padding='valid', activation='sigmoid')) # model.add(MaxPooling2D((2,2))) # model.add(Dropout(0.5)) model.add(TimeDistributed(LSTM(hidden_units, kernel_initializer='he_normal', activation='sigmoid', dropout=0.5, return_sequences=True))) model.add(TimeDistributed(LSTM(hidden_units, kernel_initializer='he_normal', activation='sigmoid', dropout=0.5))) model.add(Flatten()) # model.add(Dense(int(hidden_units/2), kernel_initializer='he_normal', activation='sigmoid')) # model.add(Dropout(0.5)) model.add(Dense(2,activation='softmax')) adam = Adam(lr=0.0001) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy']) print('No of parameter:', model.count_params()) print(model.summary()) return model
def get_cnn(self): """ Build a keras' convolutional neural network model. :returns: A tuple of 2 models, for encoding and encoding+decoding model. :rtype: tuple(Model) """ n_vocab = self.abstracts_preprocessor.get_num_vocab() n1 = 64 input_layer = Input(shape=(n_vocab,)) model = Reshape((1, n_vocab,))(input_layer) model = Convolution1D(n1, 3, border_mode='same', activation='sigmoid', W_regularizer=l2(.01))(model) model = Reshape((n1,))(model) model = Dense(n1, activation='sigmoid', W_regularizer=l2(.01))(model) model = Reshape((1, n1))(model) model = Convolution1D(self.n_factors, 3, border_mode='same', activation='softmax', W_regularizer=l2(.01))(model) encoding = Reshape((self.n_factors,), name='encoding')(model) model = Reshape((1, self.n_factors))(encoding) model = Convolution1D(n1, 3, border_mode='same', activation='sigmoid', W_regularizer=l2(.01))(model) model = Reshape((n1,))(model) model = Dense(n1, activation='relu', W_regularizer=l2(.01))(model) model = Reshape((1, n1))(model) model = Convolution1D(n_vocab, 3, border_mode='same', W_regularizer=l2(.01))(model) decoding = Reshape((n_vocab,))(model) model = concatenate([encoding, decoding]) self.model = Model(inputs=input_layer, outputs=model) self.model.compile(loss='mean_squared_error', optimizer='sgd')
def set_weights(self, weight_file): # Output Parameters print('Loading weights from %s...' % weight_file) np_data = np.fromfile(weight_file, np.float32) np_data = np_data[4:] # Just in case # Iterate through the layers and assign each convolutional layer the weights and biases i = 0 for layer in self.net.layers: shape_weights = [ w.shape for w in layer.get_weights() ] # Either an FC Layer or a Conv2D Layer if shape_weights != []: weight_shape, bias_shape = shape_weights # Reshape the weights into the biases # Flatten the shapes with np prod to index the weights bias = np_data[i:i+np.prod(bias_shape)].reshape(bias_shape) i += np.prod(bias_shape) weights = np_data[i:i+np.prod(weight_shape)].reshape(weight_shape) i += np.prod(weight_shape) layer.set_weights([weights, bias]) return # Process an image # Output: [[box_info_0], ..., [box_info_n]] # box_info_i = [x,y,w,h,c,[(label, probability)]]
def test_reshape(): layer_test(core.Reshape, kwargs={'target_shape': (8, 1)}, input_shape=(3, 2, 4)) layer_test(core.Reshape, kwargs={'target_shape': (-1, 1)}, input_shape=(3, 2, 4)) layer_test(core.Reshape, kwargs={'target_shape': (1, -1)}, input_shape=(3, 2, 4))
def __build_keras_model(self): models = [] model_artist_id = Sequential() model_artist_id.add(Embedding(100, 10, input_length=1)) model_artist_id.add(Reshape(target_shape=(10,))) models.append(model_artist_id) model_week = Sequential() model_week.add(Embedding(7, 2, input_length=1)) model_week.add(Reshape(target_shape=(6,))) models.append(model_week) # model_gender = Sequential() # model_gender.add(Embedding(1, 3, input_length=1)) # model_gender.add(Reshape(target_shape=(3,))) # models.append(model_gender) model_day = Sequential() model_day.add(Embedding(1, 10, input_length=1)) model_day.add(Reshape(target_shape=(10,))) models.append(model_day) # model_language = Sequential() # model_language.add(Embedding(1, 3, input_length=1)) # model_language.add(Reshape(target_shape=(3,))) # models.append(model_language) model_others = Sequential() model_others.add(Reshape((self.others_dim,), input_shape=(self.others_dim,))) models.append(model_others) self.model = Sequential() self.model.add(Merge(models, mode='concat')) self.model.add(Dense(100, init='uniform')) self.model.add(Activation('relu')) self.model.add(Dense(200, init='uniform')) self.model.add(Activation('relu')) self.model.add(Dense(1)) self.model.compile(loss='mean_absolute_error', optimizer='adam')
def build_model(vocab_size, embed_dim=50, level='word', token_len=15, token_char_vector_dict={}, nb_recurrent_layers=3): if level == 'word': pivot_inp = Input(shape=(1, ), dtype='int32', name='pivot') pivot_embed = Embedding(input_dim=vocab_size, output_dim=embed_dim)(pivot_inp) elif level == 'char': pivot_inp = Input(shape=(token_len, len(token_char_vector_dict)), name='pivot') for i in range(nb_recurrent_layers): if i == 0: curr_input = pivot_inp else: curr_input = curr_out l2r = LSTM(output_dim=embed_dim, return_sequences=True, activation='tanh')(curr_input) r2l = LSTM(output_dim=embed_dim, return_sequences=True, activation='tanh', go_backwards=True)(curr_input) curr_out = merge([l2r, r2l], name='encoder_'+str(i+1), mode='sum') flattened = Flatten()(curr_out) pivot_embed = Dense(embed_dim)(flattened) pivot_embed = Reshape((1, embed_dim))(pivot_embed) context_inp = Input(shape=(1, ), dtype='int32', name='context') context_embed = Embedding(input_dim=vocab_size, output_dim=embed_dim)(context_inp) prod = merge([pivot_embed, context_embed], mode='dot', dot_axes=2) res = Reshape((1, ), input_shape=(1, 1))(prod) activ = Activation('sigmoid', name='label')(res) model = Model(input=[pivot_inp, context_inp], output=activ) optim = RMSprop() model.compile(loss='mse', optimizer=optim) return model
def simple_cnn(agent, env, dropout=0, learning_rate=1e-3, **args): with tf.device("/cpu:0"): state = tf.placeholder('float', [None, agent.input_dim]) # Policy Model S = Input(shape=[agent.input_dim]) h = Reshape( agent.input_dim_orig )(S) h = TimeDistributed( Convolution2D(16, 8, 8, subsample=(4, 4), border_mode='same', activation='relu', dim_ordering='tf'))(h) h = TimeDistributed( Convolution2D(32, 4, 4, subsample=(2, 2), border_mode='same', activation='relu', dim_ordering='tf'))(h) h = Flatten()(h) h = Dense(256, activation='relu')(h) h = Dense(128, activation='relu')(h) V = Dense(env.action_space.n, activation='sigmoid',init='zero')(h) policy_model = Model(S, V) policy_model.compile(loss='mse', optimizer=RMSprop(lr=learning_rate) ) # Value Model S = Input(shape=[agent.input_dim]) h = Reshape( agent.input_dim_orig )(S) h = TimeDistributed( Convolution2D(16, 8, 8, subsample=(4, 4), border_mode='same', activation='relu', dim_ordering='tf'))(h) h = TimeDistributed( Convolution2D(32, 4, 4, subsample=(2, 2), border_mode='same', activation='relu', dim_ordering='tf'))(h) h = Flatten()(h) h = Dense(256, activation='relu')(h) h = Dense(128, activation='relu')(h) V = Dense(1, activation='linear',init='zero')(h) value_model = Model(S, V) value_model.compile(loss='mse', optimizer=RMSprop(lr=learning_rate) ) return state, policy_model, value_model
def simple_rnn(agent, env, dropout=0, h0_width=8, h1_width=8, **args): with tf.device("/cpu:0"): state = tf.placeholder('float', [None, agent.input_dim]) S = Input(shape=[agent.input_dim]) h = Reshape([agent.nframes, agent.input_dim/agent.nframes])(S) h = TimeDistributed(Dense(h0_width, activation='relu', init='he_normal'))(h) h = Dropout(dropout)(h) h = LSTM(h1_width, return_sequences=True)(h) h = Dropout(dropout)(h) h = LSTM(h1_width)(h) h = Dropout(dropout)(h) V = Dense(env.action_space.n, activation='linear',init='zero')(h) model = Model(S,V) return model
def simple_rnn(agent, env, dropout=0, h0_width=8, h1_width=8, **args): S = Input(shape=[agent.input_dim]) h = Reshape([agent.nframes, agent.input_dim/agent.nframes])(S) h = TimeDistributed(Dense(h0_width, activation='relu', init='he_normal'))(h) h = Dropout(dropout)(h) h = LSTM(h1_width, return_sequences=True)(h) h = Dropout(dropout)(h) h = LSTM(h1_width)(h) h = Dropout(dropout)(h) V = Dense(env.action_space.n, activation='linear',init='zero')(h) model = Model(S,V) model.compile(loss='mse', optimizer=RMSprop(lr=args["learning_rate"]) ) return model
def simple_cnn(agent, env, dropout=0, learning_rate=1e-3, **args): S = Input(shape=[agent.input_dim]) h = Reshape( agent.input_dim_orig )(S) h = TimeDistributed( Convolution2D(16, 8, 8, subsample=(4, 4), border_mode='same', activation='relu'))(h) h = TimeDistributed( Convolution2D(32, 4, 4, subsample=(2, 2), border_mode='same', activation='relu'))(h) h = Flatten()(h) h = Dense(256, activation='relu')(h) V = Dense(env.action_space.n, activation='linear',init='zero')(h) model = Model(S, V) model.compile(loss='mse', optimizer=RMSprop(lr=learning_rate) ) return model
def test_reshape(self): layer = core.Reshape(dims=(10, 10)) self._runner(layer)
def VQA_MODEL(): image_feature_size = 4096 word_feature_size = 300 number_of_LSTM = 3 number_of_hidden_units_LSTM = 512 max_length_questions = 30 number_of_dense_layers = 3 number_of_hidden_units = 1024 activation_function = 'tanh' dropout_pct = 0.5 # Image model model_image = Sequential() model_image.add(Reshape((image_feature_size,), input_shape=(image_feature_size,))) # Language Model model_language = Sequential() model_language.add(LSTM(number_of_hidden_units_LSTM, return_sequences=True, input_shape=(max_length_questions, word_feature_size))) model_language.add(LSTM(number_of_hidden_units_LSTM, return_sequences=True)) model_language.add(LSTM(number_of_hidden_units_LSTM, return_sequences=False)) # combined model model = Sequential() model.add(Merge([model_language, model_image], mode='concat', concat_axis=1)) for _ in xrange(number_of_dense_layers): model.add(Dense(number_of_hidden_units, init='uniform')) model.add(Activation(activation_function)) model.add(Dropout(dropout_pct)) model.add(Dense(1000)) model.add(Activation('softmax')) return model
def __init__(self): self.model = Sequential() self.model.add(convolutional.Convolution2D(input_shape=(48, 19, 19), nb_filter=K, nb_row=5, nb_col=5, init='uniform', activation='relu', border_mode='same')) for i in range(2,13): self.model.add(convolutional.Convolution2D(nb_filter=K, nb_row=3, nb_col=3, init='uniform', activation='relu', border_mode='same')) self.model.add(convolutional.Convolution2D(nb_filter=1, nb_row=1, nb_col=1, init='uniform', border_mode='same')) self.model.add(Reshape((19,19))) self.model.add(Activation('softmax')) sgd = SGD(lr=LEARNING_RATE, decay=DECAY) self.model.compile(loss='binary_crossentropy', optimizer=sgd)
def predict_image(self, test_img, show=False): ''' predicts classes of input image INPUT (1) str 'test_image': filepath to image to predict on (2) bool 'show': True to show the results of prediction, False to return prediction OUTPUT (1) if show == False: array of predicted pixel classes for the center 208 x 208 pixels (2) if show == True: displays segmentation results ''' imgs = io.imread(test_img).astype('float').reshape(4,240,240) plist = []; # create patches from an entire slice for img in imgs: if np.max(img) != 0: img /= np.max(img) p = extract_patches_2d(img, (33,33)) #Reshape a 2D image into a collection of patches plist.append(p) patches = np.array(zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3]))) # predict classes of each pixel based on model # Generate class predictions for the input samples batch by batch, full_pred.shape=(43264,) full_pred = self.model_comp.predict(patches, batch_size=200, verbose=0) # Record the category of the highest possiblility as feature # Create full slice feature map feature_map = np.zeros(full_pred.shape[0]) for i in xrange(full_pred.shape[0]): feature_map[i] = np.argmax(full_pred[i]) fp1 = feature_map.reshape(208,208) label0 = len(np.argwhere(feature_map==0)) label1 = len(np.argwhere(feature_map==1)) label2 = len(np.argwhere(feature_map==2)) label3 = len(np.argwhere(feature_map==3)) label4 = len(np.argwhere(feature_map==4)) # print label0, label1, label2, label3, label4 return fp1
def qualitative_CNN(self, vocab_size, emb_dim, max_len, nb_filters): self.max_len = max_len max_features = vocab_size filter_lengths = [3, 4, 5] print("Build model...") self.qual_conv_set = {} '''Embedding Layer''' qual_model_input=Input(input_shape=(max_len,), dtype='int32',name='qual_model_input') qual_model_embedding=Embedding(max_features, emb_dim, input_length=max_len, weights=model_embedding.get_weights())(qual_model_input) '''Convolution Layer & Max Pooling Layer''' for i in filter_lengths: model_internal = Sequential() model_internal.add(Reshape((max_len, emb_dim,1), input_shape=(max_len, emb_dim))) self.qual_conv_set[i] = Conv2D(nb_filters,(i,emb_dim),strides=(1,1),activation="relu",weights='model_convolutional_'+str(i).layers[1].get_weights()) model_internal.add(self.qual_conv_set[i]) model_internal.add(MaxPooling2D(pool_size=(max_len - i + 1, 1))) model_internal.add(Flatten()) if i==3: qual_model_convolutional_3=model_internal(qual_model_embedding) if i==4: qual_model_convolutional_4=model_internal(qual_model_embedding) if i==5: qual_model_convolutional_5=model_internal(qual_model_embedding) qual_model_output=Model(inputs=qual_model_input,outputs=['qual_model_convolutional_'+str(i) for i in filer_lengths]) qual_model_output.compile(optimizer='rmsprop', loss={'qual_model_convolutional_3': 'mse', 'qual_model_convolutional_4': 'mse', 'qual_model_convolutional_5': 'mse'}) self.qual_model=qual_model_output