def baseline_model(): #CNN参数 embedding_dims = 50 filters = 250 kernel_size = 3 hidden_dims = 250 model = Sequential() model.add(Embedding(max_features, embedding_dims)) model.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1)) #池化 model.add(GlobalMaxPooling1D()) model.add(Dense(2, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) #可视化 plot_model(model, to_file='yelp-cnn-model.png',show_shapes=True) model.summary() return model
def 製造模型(): n=18 inputs = Input(shape=[256,256,3]) c128 = down(1 *n,inputs) c64 = down(2 *n,c128) c32 = down(4 *n,c64) c16 = down(8 *n,c32) c8 = down(16*n,c16) c4 = down(32*n,c8) avg = GlobalAveragePooling2D()(c4) prediction = Reshape([3,2])(Dense(6,activation='sigmoid')(avg)) model = Model(inputs=inputs, outputs=prediction) adam = Adam(lr=1e-3,amsgrad=True) model.summary() model.compile(loss='mse',optimizer=adam) plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=False) return model
def defineNetwork(self): print("Setting up network...") inputs = Input(shape=(self.nx, self.ny, self.n_diversity)) x = GaussianNoise(self.noise)(inputs) conv = Convolution2D(self.n_filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(x) x = self.residual(conv) for i in range(self.n_conv_layers): x = self.residual(x) x = Convolution2D(self.n_filters, (3, 3), padding='same', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = add([x, conv]) final = Convolution2D(1, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal')(x) self.model = Model(inputs=inputs, outputs=final) json_string = self.model.to_json() f = open('{0}_model.json'.format(self.root), 'w') f.write(json_string) f.close() with open('{0}_summary.txt'.format(self.root), 'w') as f: with redirect_stdout(f): self.model.summary() plot_model(self.model, to_file='{0}_model.png'.format(self.root), show_shapes=True)
def unet_other(weights=None, input_size=(512, 512, 4)): inputs = Input(input_size) conv1 = Conv2D(32, 3, activation='elu', padding='same', kernel_initializer='he_normal')(inputs) conv1 = BatchNormalization()(conv1) conv1 = Conv2D(32, 3, activation='elu', padding='same', kernel_initializer='he_normal')(conv1) conv1 = BatchNormalization()(conv1) pool2 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(64, 3, activation='elu', padding='same', kernel_initializer='he_normal')(pool2) conv2 = BatchNormalization()(conv2) conv2 = Conv2D(64, 3, activation='elu', padding='same', kernel_initializer='he_normal')(conv2) conv2 = BatchNormalization()(conv2) pool3 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D(128, 3, activation='elu', padding='same', kernel_initializer='he_normal')(pool3) conv3 = BatchNormalization()(conv3) conv3 = Conv2D(128, 3, activation='elu', padding='same', kernel_initializer='he_normal')(conv3) conv3 = BatchNormalization()(conv3) pool4 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(256, 3, activation='elu', padding='same', kernel_initializer='he_normal')(pool4) conv4 = BatchNormalization()(conv4) conv4 = Conv2D(256, 3, activation='elu', padding='same', kernel_initializer='he_normal')(conv4) conv4 = BatchNormalization()(conv4) up4 = UpSampling2D(size=(2,2))(conv4) merge5 =concatenate([up4, conv3], axis=-1) drop5 = Dropout(0.5)(merge5) conv5 = Conv2D(128, 3, activation='elu', padding='same', kernel_initializer='he_normal')(drop5) conv5 = Conv2D(128, 3, activation='elu', padding='same', kernel_initializer='he_normal')(conv5) up5 = UpSampling2D(size=(2,2))(conv5) merge6 = concatenate([up5, conv2], axis=-1) drop6 = Dropout(0.5)(merge6) conv6 = Conv2D(64, 3,activation='elu', padding='same', kernel_initializer='he_normal')(drop6) conv6 = Conv2D(64, 3, activation='elu', padding='same', kernel_initializer='he_normal')(conv6) up6 = UpSampling2D(size=(2,2))(conv6) merge7 = concatenate([up6, conv1], axis=-1) drop7 = Dropout(0.5)(merge7) conv7 = Conv2D(32, 3, activation='elu',padding='same', kernel_initializer='he_normal')(drop7) conv7 = Conv2D(32, 3, activation='elu', padding='same', kernel_initializer='he_normal')(conv7) conv7 = Conv2D(1, 1, activation='sigmoid', padding='same', kernel_initializer='he_normal')(conv7) model = Model(input=inputs, output=conv7) model.compile(optimizer=Adam(lr=1e-4), loss=jaccard_coef_loss, metrics=['binary_crossentropy', jaccard_coef_int]) with open('model-summary-report.txt', 'w') as fh: # Pass the file handle in as a lambda function to make it callable model.summary(print_fn=lambda x: fh.write(x + '\n')) if (weights): model.load_weights(weights) plot_model(model, to_file='unet-other-model.png',show_shapes=True) #vis model return model
def multi_input_model(model,max_dict): ''' multi_input_model ''' print "multi_input_model" model_displayid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_displayid',input_shape_dim1 = 1,name='displayid') model_adid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_adid',input_shape_dim1 = 1,name='adid') model_platform = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_platform',input_shape_dim1 = 1,name = 'platform') model_hour = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_hour',input_shape_dim1 = 1,name = 'hour') model_weekday = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_weekday',input_shape_dim1 = 1 ,name = 'weekday') model_uid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_uid',input_shape_dim1 = 1, name = 'uid') model_documentid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_documentid',input_shape_dim1 = 1,name = 'documentid') model_campaignid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_campaignid',input_shape_dim1 = 1,name = 'campaignid') model_advertiserid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_advertiserid',input_shape_dim1 = 1, name = 'advertiserid') model_sourceidx = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_sourceidx',input_shape_dim1 = 1,name = 'sourceid') model_categoryid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_categoryid',input_shape_dim1 = 2,name = 'categoryid') model_entityid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_entityid',input_shape_dim1 = 10,name = 'entityid') model_topicid = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_topicid',input_shape_dim1 = 39,name = 'topicid') model_uidview_doc = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_doctrfids',input_shape_dim1 = 306,name = 'uidview_doc') model_uidview_source = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_sourceidy',input_shape_dim1 = 160,name = 'uidview_source') model_uidview_onehour_doc = sub_input_model(max_dict,embedding_size = 50,max_name = 'max_docids',input_shape_dim1 = 123,name = 'uidview_onehour_doc') model.add(Concatenate([model_displayid, model_adid, model_platform,model_hour,model_weekday,model_uid,model_documentid,model_campaignid,\ model_advertiserid,model_sourceidx,model_categoryid,model_entityid,model_topicid,model_uidview_doc,\ model_uidview_source,model_uidview_onehour_doc], mode='concat', concat_axis=1)) print('the model\'s input shape ', model.input_shape) print ('the mode\'s output shape ', model.output_shape) model.add(Dense(30,activation = 'relu',name = 'Dense_1')) print model.output_shape model.add(Dense(1, activation='sigmoid',name = 'Dense_2')) print('the final model\'s shape', model.output_shape) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy', 'mae']) plot_model(model,to_file='model.png', show_shapes=True)
def model_plot_save_mult(models_dict): '''Saves multiple models plot # eg: plot_model_mult({'model_name': model}) ''' # graphviz and pydot needed for plot_model from keras.utils import plot_model # single plot_model use # plot_model(model, to_file="model.png") for name, model in models_dict.items(): plot_model(model, to_file='%s_plot.png' % name)
def __init__(self, n, X_train, Y_train, X_test, Y_test, learning_rate, rho, epsilon, epochs, loss_function, log_dir, batch_size, metrics, tensorBoard, early): embedding_vecor_length = 60 top_words = 157 max_review_length = 500 self.model = Sequential() self.model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length,mask_zero=True)) self.model.add(LSTM(384, return_sequences=True)) self.model.add(LSTM(384)) self.model.add(Dense(2, activation='softmax')) # truncate and pad input sequences plot_model(self.model, to_file='modelSMILE2Vect.png') X_train = sequence.pad_sequences(X_train, maxlen=max_review_length) X_test = sequence.pad_sequences(X_test, maxlen=max_review_length) self.X_train = X_train self.Y_train = Y_train self.X_test = X_test self.Y_test = Y_test self.learning_rate = learning_rate self.rho = rho self.epsilon = epsilon self.epochs = epochs self.loss_function = loss_function self.log_dir = log_dir self.batch_size = batch_size self.metrics = metrics self.tensorBoard = tensorBoard self.early = early print(self.model.summary())
def define_network(self, l2_reg): print("Setting up network...") self.model = nn_model.zdi(150, self.noise, activation='selu', n_filters=64, l2_reg=1e-7) json_string = self.model.to_json() f = open('{0}_model.json'.format(self.root), 'w') f.write(json_string) f.close() with open('{0}_summary.txt'.format(self.root), 'w') as f: with redirect_stdout(f): self.model.summary() plot_model(self.model, to_file='{0}_model.png'.format(self.root), show_shapes=True)
def define_network(self): print("Setting up network...") self.model = nn_model.define_network(self.nx, self.ny, self.noise, self.depth) json_string = self.model.to_json() f = open('{0}_{1}_model.json'.format(self.root, self.depth), 'w') f.write(json_string) f.close() with open('{0}_{1}_summary.txt'.format(self.root, self.depth), 'w') as f: with redirect_stdout(f): self.model.summary() plot_model(self.model, to_file='{0}_{1}_model.png'.format(self.root, self.depth), show_shapes=True)
def define_network(self, l2_reg): print("Setting up network...") # self.model = nn_model.keepsize(self.nx, self.ny, self.noise, self.depth, activation=self.activation, n_filters=self.n_filters, l2_reg=l2_reg) self.model = nn_model.encdec(self.nx, self.ny, self.noise, self.depth, activation=self.activation, n_filters=self.n_filters, l2_reg=l2_reg) json_string = self.model.to_json() f = open('{0}_{1}_model.json'.format(self.root, self.depth), 'w') f.write(json_string) f.close() with open('{0}_{1}_summary.txt'.format(self.root, self.depth), 'w') as f: with redirect_stdout(f): self.model.summary() plot_model(self.model, to_file='{0}_{1}_model.png'.format(self.root, self.depth), show_shapes=True)
def __init__(self, vocab_size, max_length, X_train, Y_train, X_test, Y_test, learning_rate, rho, epsilon, epochs, loss_function, log_dir, batch_size, metrics, tensorBoard, early): self.model = Sequential() self.model.add(Embedding(vocab_size+1, 100, input_length=max_length, trainable = True, mask_zero=False)) self.model.add(Bidirectional(GRU(100, return_sequences=True))) self.model.add(AttLayer(100)) self.model.add(Dense(2, activation='softmax')) plot_model(self.model, to_file='modelHATT.png') self.X_train = X_train self.Y_train = Y_train self.X_test = X_test self.Y_test = Y_test self.learning_rate = learning_rate self.rho = rho self.epsilon = epsilon self.epochs = epochs self.loss_function = loss_function self.log_dir = log_dir self.batch_size = batch_size self.metrics = metrics self.tensorBoard = tensorBoard self.early = early print(self.model.summary())
def baseline_model(): #CNN参数 #filters个数通常与文本长度相当 便于提取特征 filters = max_document_length # Inputs input = Input(shape=[max_document_length]) # 词向量层,本文使用了预训练word2vec词向量,把trainable设为False x = Embedding(max_features + 1, embedding_dims, weights=[embedding_matrix], trainable=trainable)(input) # conv layers convs = [] for filter_size in [3,4,5]: l_conv = Conv1D(filters=filters, kernel_size=filter_size, activation='relu')(x) l_pool = MaxPooling1D()(l_conv) l_pool = Flatten()(l_pool) convs.append(l_pool) merge = concatenate(convs, axis=1) out = Dropout(0.2)(merge) output = Dense(32, activation='relu')(out) output = Dense(units=2, activation='softmax')(output) #输出层 model = Model([input], output) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) #可视化 plot_model(model, to_file='yelp-cnn-model-textcnn.png',show_shapes=True) model.summary() return model
def baseline_model(): model = Sequential() model.add(Embedding(max_features, 128)) model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2)) model.add(Dense(2, activation='softmax')) # try using different optimizers and different optimizer configs model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) #可视化 plot_model(model, to_file='yelp-lstm-model.png',show_shapes=True) model.summary() return model
def init_model(): # Define the input tensor input_tensor = Input((FRAME_HEIGHT, FRAME_WIDTH, ACCUMULATED_FRAME_NUM)) # Get the output tensor output_tensor = input_tensor for block_index in np.arange(4) + 1: output_tensor = Conv2D(filters=64 * block_index, kernel_size=5, strides=2, padding="same")(output_tensor) output_tensor = Activation("relu")(output_tensor) output_tensor = GlobalAveragePooling2D()(output_tensor) output_tensor = Dense(ACTION_NUM)(output_tensor) # Define the model model = Model(input_tensor, output_tensor) model.compile(loss="mean_squared_error", optimizer=Adam(lr=LEARNING_RATE)) model.summary() plot_model(model, to_file=MODEL_STRUCTURE_FILE_PATH, show_shapes=True, show_layer_names=True) return model
def baseline_model(): #CNN参数 embedding_dims = 50 filters = 100 # Inputs input = Input(shape=[max_document_length]) # Embeddings layers x = Embedding(max_features, embedding_dims)(input) # conv layers convs = [] for filter_size in [3,4,5]: l_conv = Conv1D(filters=filters, kernel_size=filter_size, activation='relu')(x) l_pool = MaxPooling1D()(l_conv) l_pool = Flatten()(l_pool) convs.append(l_pool) merge = concatenate(convs, axis=1) out = Dropout(0.2)(merge) output = Dense(32, activation='relu')(out) output = Dense(units=2, activation='softmax')(output) #输出层 model = Model([input], output) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) #可视化 plot_model(model, to_file='yelp-cnn-model-textcnn.png',show_shapes=True) model.summary() return model
def define_network(self): if (self.method == 'mdn'): self.model = nn_model.network(self.n_lambda, self.depth, noise=self.noise, activation=self.activation, n_filters=self.n_kernels, l2_reg=self.l2_reg) self.model.compile(loss=self.mean_log_Gaussian_like, optimizer=Adam(lr=self.lr)) if (self.method == 'dropout'): self.model = nn_model.network_dropout(self.n_lambda, self.n_classes, self.depth, noise=self.noise, activation=self.activation, n_filters=self.n_kernels, l2_reg=self.l2_reg, wd=self.wd, dd=self.dd) self.model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=self.lr), metrics=['accuracy']) if (self.method == 'nodropout'): self.model = nn_model.network_nodropout(self.n_lambda, self.n_classes, self.depth, noise=self.noise, activation=self.activation, n_filters=self.n_kernels, l2_reg=self.l2_reg) self.model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=self.lr), metrics=['accuracy']) json_string = self.model.to_json() f = open('{0}_model.json'.format(self.root), 'w') f.write(json_string) f.close() plot_model(self.model, to_file='{0}_model.png'.format(self.root), show_shapes=True)
def train(self, initial_epoch=0): orig_stdout = sys.stdout f = open(os.path.join(self.config.model_dir, 'model_summary.txt'), 'w') sys.stdout = f print(self.model.summary()) for layer in self.model.layers: print(layer.get_config()) sys.stdout = orig_stdout f.close() plot_model(self.model, show_shapes=True, to_file=os.path.join(self.config.model_dir, 'model_structure.png')) t0 = time.time() samples_per_epoch = int(math.floor(len(self.training_set) / self.config.batch_size)) val_step = int(math.floor(len(self.validation_set) / self.config.batch_size)) print("Samples per epoch: {}".format(samples_per_epoch)) pb = PrintBatch() tb_x, tb_y = self.tensorboard_data_generator(self.config.max_image_summary) tb = TensorBoardCustom(self.config, tb_x, tb_y, self.config.tensorboard_dir) model_checkpoint = ModelCheckpoint( os.path.join(self.config.model_dir, 'weights-{epoch:02d}-{loss:.2f}.hdf5'), monitor='loss', verbose=2, save_best_only=False, save_weights_only=False, mode='auto', period=self.config.log_step) self.history = self.model.fit_generator(generator=self.train_data_generator(), steps_per_epoch=samples_per_epoch, callbacks=[pb, model_checkpoint, tb], validation_data=self.validation_data_generator(), validation_steps=val_step, epochs=self.config.num_epochs, verbose=2, initial_epoch=initial_epoch) t1 = time.time() self.plot_graphs() print("Training completed in " + str(t1 - t0) + " seconds")
def train(x, y, **kwargs): neurons = kwargs.get("neurons", 128) dense_neurons = kwargs.get("dense_neurons", 0) learning_rate = kwargs.get("learning_rate", 0.001) dropout = kwargs.get("dropout", 0) recurrent_dropout = kwargs.get("recurrent_dropout", 0) optimizer = kwargs.get("optimizer", "rmsprop") regularization = kwargs.get("regularization", 0) batch_size = kwargs.get("batch_size", 128) epochs = kwargs.get("epochs", 50) layer_type = kwargs.get("type", "LSTM") validation = kwargs.get("validation", 0) model = models.Sequential() embedding = numpy.zeros((64, 64), dtype=numpy.float32) numpy.fill_diagonal(embedding, 1) model.add(layers.embeddings.Embedding( 64, 64, input_length=x[0].shape[-1], weights=[embedding], trainable=False)) model.add(getattr(layers, layer_type)( neurons, dropout=dropout, recurrent_dropout=recurrent_dropout, kernel_regularizer=regularizers.l2(regularization), return_sequences=True)) model.add(getattr(layers, layer_type)( neurons // 2, dropout=dropout, recurrent_dropout=recurrent_dropout, kernel_regularizer=regularizers.l2(regularization))) if dense_neurons > 0: model.add(layers.Dense(dense_neurons)) model.add(layers.normalization.BatchNormalization()) model.add(layers.advanced_activations.PReLU()) model.add(layers.Dense(y[0].shape[-1], activation="softmax")) optimizer = getattr(optimizers, optimizer)(lr=learning_rate, clipnorm=1.) model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy", "top_k_categorical_accuracy"]) if kwargs.get("visualize"): plot_model(model, to_file=kwargs["visualize"], show_shapes=True) model.fit(x, y, batch_size=batch_size, epochs=epochs, validation_split=validation) return model
# 模型适配生成 model.fit_generator( train_generator, # 训练集 samples_per_epoch=2400, # 训练集总样本数,如果提供样本数量不够,则调整图片(翻转、平移等)补足数量(本例,该函数补充2400-240个样本) nb_epoch=epochs, validation_data=validation_generator, # 验证集 nb_val_samples=800, # 验证集总样本数,如果提供样本数量不够,则调整图片(翻转、平移等)补足数量(本例,该函数补充800-60个样本) callbacks=[history]) # 回调函数,绘制批次(epoch)和精确度(acc)关系图表函数 # Save model and weights if not os.path.isdir(save_dir): # 没有save_dir对应目录则建立 os.makedirs(save_dir) model_path = os.path.join(save_dir, model_name) model.save(model_path) print('Saved trained model at %s ' % model_path) # 显示批次(epoch)和精确度(acc)关系图表 history.loss_plot('epoch') # 模型结构图 from keras.utils import plot_model plot_model(model, to_file='model.png', show_shapes=True) # Score trained model. # scores = model.evaluate(x_test, y_test, verbose=1) # print('Test loss:', scores[0]) # print('Test accuracy:', scores[1]) ''' '''
def _build_net(self): # evaluation网络 eval_inputs = [ Input(shape=( 50, 75, )), Input(shape=[ 30, 75, ]), Input(shape=[ 50, ]) ] x = LSTM(units=50, return_sequences=0)(eval_inputs[0]) x1 = Dense(units=50, activation='relu')(x) # x1 = Dense(units=50,activation='relu')(x1) x = LSTM(units=30, return_sequences=0)(eval_inputs[1]) x2 = Dense(units=50, activation='relu')(x) # x2 = Dense(units=50,activation='relu')(x2) x_whole = Concatenate()([x1, x2, eval_inputs[2]]) # x_repeat = RepeatVector(n=30)(x_whole) x_whole = Dense(units=60, activation="relu")(x_whole) x_whole = Dense(units=60, activation="relu")(x_whole) self.q_eval = Dense(units=60, activation='tanh')(x_whole) # target网络---注意这个target层输出是q_next而不是,算法中的q_target target_inputs = [ Input(shape=( 50, 75, )), Input(shape=[ 30, 75, ]), Input(shape=[ 50, ]) ] x = LSTM(units=50, return_sequences=0)(target_inputs[0]) x1 = Dense(units=50, activation='relu')(x) # x1 = Dense(units=50, activation='relu')(x1) x = LSTM(units=30, return_sequences=0)(target_inputs[1]) x2 = Dense(units=50, activation='relu')(x) # x2 = Dense(units=50, activation='relu')(x2) x_whole = Concatenate()([x1, x2, target_inputs[2]]) # x_repeat = RepeatVector(30)(x_whole) x_whole = Dense(units=60, activation="relu")(x_whole) x_whole = Dense(units=60, activation="relu")(x_whole) self.q_next = Dense(units=60, activation='tanh')(x_whole) self.model1 = Model(eval_inputs, self.q_eval) self.model2 = Model(target_inputs, self.q_next) rmsprop = RMSprop(lr=self.lr) self.model1.compile(loss='mean_squared_error', optimizer=rmsprop, metrics=['accuracy']) self.model2.compile(loss='mean_squared_error', optimizer=rmsprop, metrics=['accuracy']) plot_model(self.model1, to_file="model1_eval.png", show_layer_names=True, show_shapes=True) plot_model(self.model2, to_file="model1_target.png", show_layer_names=True, show_shapes=True)
def getModel(self): optim = self.optimizer input1 = Input(shape=(32, 32, 1), name='1stInput') conv1 = Conv2D(24, (3, 3), activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(self.l2Lambda), data_format='channels_last', name='1st')(input1) batchNorm1 = BatchNormalization(name='batchNorm1')(conv1) maxpool3 = MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same', name='pool3')(batchNorm1) drop3 = Dropout(0.25, name='dropout3')(maxpool3) conv2 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(self.l2Lambda), data_format='channels_last', name='2nd_conv')(drop3) batchNorm2 = BatchNormalization(name='batchNorm2')(conv2) maxpool4 = MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same', name='pool4')(batchNorm2) drop4 = Dropout(0.25, name='dropout4')(maxpool4) flatForLSTM = TimeDistributed( Flatten(name='flatLayer_for_LSTM'))(drop4) lstm0 = LSTM(25, activation='tanh', return_sequences=True, dropout=0.25, recurrent_dropout=0.25, recurrent_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(self.l2Lambda), recurrent_regularizer=regularizers.l2(self.l2Lambda), name='lstm_0')(flatForLSTM) # ============================================================================= # d1 = Dense(512, activation = 'sigmoid',kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), name='denseInput_1')(lstm0) # # # drop1 = Dropout(0.25,name='drpout_11')(d1) # d2 = Dense(512, activation = 'sigmoid',kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), name='denseInput_2')(drop1) # # # drop2 = Dropout(0.25,name='drpout2')(d2) # ============================================================================= # ============================================================================= # self.model=Sequential() # self.model.add(TimeDistributed(Conv2D(24, (3,3),activation = 'relu', kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), # data_format='channels_last',name='1st', input_shape = (32,32,1)))) # # self.model.add(TimeDistributed(BatchNormalization(name='batchNorm1'))) # self.model.add(TimeDistributed(Conv2D(16, (3,3),activation = 'relu',kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), # data_format='channels_last',name='convlayer2'))) # self.model.add(TimeDistributed(BatchNormalization(name='batchNorm2'))) # # self.model.add(TimeDistributed(Conv2D(10, (3,3), activation = 'relu',kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), # data_format='channels_last',name='convlayer3'))) # # self.model.add(TimeDistributed(BatchNormalization(name='batchNorm3'))) # # self.model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),strides=(1,1),padding='same',name = 'pool3'))) # self.model.add(TimeDistributed(Dropout(0.25,name='dropout3'))) # self.model.add(TimeDistributed(Flatten(name='flat0'))) # self.model.add(LSTM(10,activation='tanh',return_sequences=True, dropout=0.25, name='lstm_1')) # # self.model.add(Dense(1024,activation='relu',kernel_initializer='glorot_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), name='FUll_1') ) # # self.model.add(Dropout(0.25,name='dropout4')) # # self.model.add(Dense(1024,activation='relu',kernel_initializer='glorot_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), name='FUll_2')) # self.model.add(Dropout(0.25,name='dropout5')) # # self.model.add( Dense(self.numCategories, activation='softmax', name='FUll_3') ) # # self.model.compile(optimizer=optim,loss='categorical_crossentropy',metrics=['accuracy']) # ============================================================================= #input1 = Input(shape=(32,32,1),name='1stInput' ) #1st convolution Layer # ============================================================================= # conv1 = Conv2D(24, (3,3),activation = 'relu', kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), # data_format='channels_last',name='1st')(lstm0) # # # batchNorm1 = BatchNormalization(name='batchNorm1')(conv1) # # #2nd convolution Layer # conv2 = Conv2D(16, (3,3),activation = 'relu',kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), # data_format='channels_last',name='convlayer2')(batchNorm1) # # # batchNorm2 = BatchNormalization(name='batchNorm2')(conv2) # #3rd convolution Layer # conv3 = Conv2D(10, (3,3), activation = 'relu',kernel_initializer='he_uniform', # kernel_regularizer=regularizers.l2(self.l2Lambda), # data_format='channels_last',name='convlayer3')(batchNorm2) # # # batchNorm3 = BatchNormalization(name='batchNorm3')(conv3) # maxpool3 = MaxPooling2D(pool_size=(2, 2),strides=(1,1),padding='same',name = 'pool3')(batchNorm3) # drop3 = Dropout(0.25,name='dropout3')(maxpool3) # # #adding LSTM layer # #reshape = Reshape(target_shape=) # flatForLSTM = TimeDistributed(Flatten(name='flatLayer_for_LSTM'))(drop3) # lstm0 = LSTM(25,activation='tanh',return_sequences=True,dropout=0.25, # recurrent_dropout=0.25,recurrent_initializer='glorot_uniform',kernel_regularizer = regularizers.l2(self.l2Lambda), # recurrent_regularizer = regularizers.l2(self.l2Lambda),name='lstm_0')(flatForLSTM) # ============================================================================= #lstm1 = LSTM(10,activation='tanh',return_sequences=True,dropout=0.25, name='lstm_1')(lstm0) # ============================================================================= #Flatten layer flat1 = Flatten(name='flat1')(lstm0) # ============================================================================= # sending the same image to Deep Neural netword parellely # ============================================================================= flatIp = Flatten(name='FlattenInput')(input1) denseIP1 = Dense(1024, activation='sigmoid', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(self.l2Lambda), name='denseInput1')(flatIp) dropDense1 = Dropout(0.25, name='DenseDrop1')(denseIP1) denseIP2 = Dense(1024, activation='sigmoid', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(self.l2Lambda), name='input2_dense1')(dropDense1) dropDense2 = Dropout(0.25, name='DenseDrop2')(denseIP2) # ============================================================================= # Concatinate both Branches # ============================================================================= concat = concatenate([flat1, dropDense2]) # ============================================================================= # Fully connected layers # ============================================================================= hidden1 = Dense(1024, activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(self.l2Lambda), name='FUll_1')(concat) drop6 = Dropout(0.25, name='dropout6')(hidden1) hidden2 = Dense(1024, activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(self.l2Lambda), name='FUll_2')(drop6) drop7 = Dropout(0.25, name='dropout7')(hidden2) output = Dense(self.numCategories, activation='softmax', name='FUll_3')(drop7) #drop5 = Dropout(0.2,name='dropout4')(hidden2) self.model = Model(inputs=input1, outputs=output) self.baseModel = self.model #self.model.compile(optimizer=optim,loss='categorical_crossentropy',metrics=['accuracy']) #self.model = multi_gpu_model(self.model,gpus=1) self.model.compile(optimizer=optim, loss='categorical_crossentropy', metrics=['accuracy']) print(self.model.summary()) plot_model(self.model, to_file=self.path + 'Models/Seperate_Models/' + 'modelStructure_CNN+LSTM.png') print('Model configured..!')
def build_combined_categorical(FLAGS, NUM_FILTERS, FILTER_LENGTH1, FILTER_LENGTH2): XDinput = Input(shape=(FLAGS.max_smi_len, ), dtype='int32') ### Buralar flagdan gelmeliii XTinput = Input(shape=(FLAGS.max_seq_len, ), dtype='int32') ### SMI_EMB_DINMS FLAGS GELMELII encode_smiles = Embedding(input_dim=FLAGS.charsmiset_size + 1, output_dim=128, input_length=FLAGS.max_smi_len)(XDinput) encode_smiles = Conv1D(filters=NUM_FILTERS, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(encode_smiles) encode_smiles = Conv1D(filters=NUM_FILTERS * 2, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(encode_smiles) encode_smiles = Conv1D(filters=NUM_FILTERS * 3, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(encode_smiles) encode_smiles = GlobalMaxPooling1D()(encode_smiles) encode_protein = Embedding(input_dim=FLAGS.charseqset_size + 1, output_dim=128, input_length=FLAGS.max_seq_len)(XTinput) encode_protein = Conv1D(filters=NUM_FILTERS, kernel_size=FILTER_LENGTH2, activation='relu', padding='valid', strides=1)(encode_protein) encode_protein = Conv1D(filters=NUM_FILTERS * 2, kernel_size=FILTER_LENGTH2, activation='relu', padding='valid', strides=1)(encode_protein) encode_protein = Conv1D(filters=NUM_FILTERS * 3, kernel_size=FILTER_LENGTH2, activation='relu', padding='valid', strides=1)(encode_protein) encode_protein = GlobalMaxPooling1D()(encode_protein) encode_interaction = keras.layers.concatenate( [encode_smiles, encode_protein], axis=-1) #merge.Add()([encode_smiles, encode_protein]) # Fully connected FC1 = Dense(1024, activation='relu')(encode_interaction) FC2 = Dropout(0.1)(FC1) FC2 = Dense(1024, activation='relu')(FC2) FC2 = Dropout(0.1)(FC2) FC2 = Dense(512, activation='relu')(FC2) # And add a logistic regression on top predictions = Dense(1, kernel_initializer='normal')( FC2 ) #OR no activation, rght now it's between 0-1, do I want this??? activation='sigmoid' interactionModel = Model(inputs=[XDinput, XTinput], outputs=[predictions]) interactionModel.compile(optimizer='adam', loss='mean_squared_error', metrics=[cindex_score ]) #, metrics=['cindex_score'] print(interactionModel.summary()) plot_model(interactionModel, to_file='figures/build_combined_categorical.png') return interactionModel
def build_combined_onehot(FLAGS, NUM_FILTERS, FILTER_LENGTH1, FILTER_LENGTH2): XDinput = Input(shape=(FLAGS.max_smi_len, FLAGS.charsmiset_size)) XTinput = Input(shape=(FLAGS.max_seq_len, FLAGS.charseqset_size)) encode_smiles = Conv1D(filters=NUM_FILTERS, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(XDinput) encode_smiles = Conv1D(filters=NUM_FILTERS * 2, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(encode_smiles) encode_smiles = Conv1D(filters=NUM_FILTERS * 3, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(encode_smiles) encode_smiles = GlobalMaxPooling1D()( encode_smiles) #pool_size=pool_length[i] encode_protein = Conv1D(filters=NUM_FILTERS, kernel_size=FILTER_LENGTH2, activation='relu', padding='valid', strides=1)(XTinput) encode_protein = Conv1D(filters=NUM_FILTERS * 2, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(encode_protein) encode_protein = Conv1D(filters=NUM_FILTERS * 3, kernel_size=FILTER_LENGTH1, activation='relu', padding='valid', strides=1)(encode_protein) encode_protein = GlobalMaxPooling1D()(encode_protein) encode_interaction = keras.layers.concatenate( [encode_smiles, encode_protein]) #encode_interaction = keras.layers.concatenate([encode_smiles, encode_protein], axis=-1) #merge.Add()([encode_smiles, encode_protein]) # Fully connected FC1 = Dense(1024, activation='relu')(encode_interaction) FC2 = Dropout(0.1)(FC1) FC2 = Dense(1024, activation='relu')(FC2) FC2 = Dropout(0.1)(FC2) FC2 = Dense(512, activation='relu')(FC2) predictions = Dense(1, kernel_initializer='normal')(FC2) interactionModel = Model(inputs=[XDinput, XTinput], outputs=[predictions]) interactionModel.compile(optimizer='adam', loss='mean_squared_error', metrics=[cindex_score ]) #, metrics=['cindex_score'] print(interactionModel.summary()) plot_model(interactionModel, to_file='figures/build_combined_onehot.png') return interactionModel
def Q3conv2d_fuctional(): window_size = 9 time = datetime.datetime.now().strftime('%m%d_%H:%M') x_train, y_train = data_Processing.get_cb513_3train_data(window_size) x_test, y_test = data_Processing.get_cb513_3test_data(window_size) x_train = x_train.reshape(-1, np.size(x_train, axis=1), np.size(x_train, axis=2), 1) x_test = x_test.reshape(-1, np.size(x_test, axis=1), np.size(x_test, axis=2), 1) print(np.shape(x_train), np.shape(y_train)) inputs = Input(shape=(np.size(x_train, axis=1), np.size(x_train, axis=2), 1)) inputs_1 = Conv2D(64, (2, 2), padding='same', activation='relu')(inputs) inputs_1 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(inputs_1) inputs_1 = Conv2D(32, (2, 2), padding='same', activation='relu')(inputs_1) inputs_1 = Dropout((0.1))(inputs_1) inputs_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(inputs) inputs_2 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(inputs_2) inputs_2 = Conv2D(32, (2, 2), padding='same', activation='relu')(inputs_2) inputs_2 = Dropout((0.1))(inputs_2) inputs_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(inputs) inputs_3 = Conv2D(32, (1, 1), padding='same', activation='relu')(inputs_3) inputs_3 = Dropout((0.2))(inputs_3) outputs = keras.layers.concatenate([inputs_1, inputs_2, inputs_3], axis=1) outputs = Flatten()(outputs) outputs = Dense(512, activation='relu')(outputs) outputs = Dropout((0.2))(outputs) outputs = Dense(128, activation='relu')(outputs) outputs = Dropout((0.2))(outputs) outputs = Dense(4, activation='softmax')(outputs) model = Model(inputs=inputs, outputs=outputs) # model = load_model("C:\\bishe\model\cnn_2d_model_9_44_funtional_.h5") sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['acc']) #当验证集的loss不在下降时,中断训练 from keras.callbacks import EarlyStopping earlyStopping = EarlyStopping(monitor='val_loss', patience=2) history = model.fit( x_train, y_train, batch_size=128, epochs=1, verbose=1, validation_split=0.1, callbacks=[earlyStopping], shuffle=True, ) # call the function # training_vis(history) score = model.evaluate(x_test, y_test, batch_size=128, verbose=1) print(score) # model.summary() #评估自己的模型,即删除Noseq后的预测准确度 prediction = model.predict(x_test, batch_size=128, verbose=1) # result = acc_Q8(y_test, prediction) # print("acc_Q8:",result) print(prediction) #保存模型 save_model = "C:\\bishe\model\cnn_2d_model_9_44_3new_funtional_.h5" # model.save_weights('c:\\bishe\model\cnn_2d_weights_funtional.h5') model.save(save_model) #画出模型结构图,保存到图片 from keras.utils import plot_model png_f = 'C:\\bishe\model\cnn_2d_model_3_funtional.png' plot_model(model, to_file=png_f, show_shapes=True)
def create_models(backbone_retinanet, num_classes, weights, multi_gpu=0, freeze_backbone=False, distance=False, distance_alpha=1.0, lr=1e-5, cfg=None, inputs=(None,None,3)): """ Creates three models (model, training_model, prediction_model). :param backbone_retinanet: <func> A function to call to create a retinanet model with a given backbone :param num_classes: <int> The number of classes to train :param weights: <keras.Weights> The weights to load into the model :param multi_gpu: <int> The number of GPUs to use for training :param freeze_backbone: <bool> If True, disables learning for the backbone :param distance: <bool> If True, distance detection is enabled :param distance_alpha: <float> Weighted loss factor for distance loss :param lr: <float> Learning rate for network training :param cfg: <Configuration> Config class with config parameters :param inputs: <tuple> Input shape for neural network :return model: <keras.Model> The base model. This is also the model that is saved in snapshots. :return training_model: <keras.Model> The training model. If multi_gpu=0, this is identical to model. :return prediction_model: <keras.Model> The model wrapped with utility functions to perform object detection (applies regression values and performs NMS). """ modifier = freeze_model if freeze_backbone else None # load anchor parameters, or pass None (so that defaults will be used) if 'small' in cfg.anchor_params: anchor_params = AnchorParameters.small num_anchors = AnchorParameters.small.num_anchors() else: anchor_params = None num_anchors = None # Keras recommends initialising a multi-gpu model on the CPU to ease weight sharing, and to prevent OOM errors. # optionally wrap in a parallel model if multi_gpu > 1: from keras.utils import multi_gpu_model with tf.device('/cpu:0'): model = model_with_weights(backbone_retinanet(num_classes, num_anchors=num_anchors, modifier=modifier, inputs=inputs, distance=distance), weights=weights, skip_mismatch=True, config=copy.deepcopy(cfg), num_classes=num_classes) training_model = multi_gpu_model(model, gpus=multi_gpu) else: model = model_with_weights(backbone_retinanet(num_classes, num_anchors=num_anchors, modifier=modifier, inputs=inputs, distance=distance, cfg=cfg), weights=weights, skip_mismatch=True, config=copy.deepcopy(cfg), num_classes=num_classes) training_model = model try: from keras.utils import plot_model # Write the keras model plot into a file plot_path = os.path.join(cfg.tb_logdir, cfg.model_name) makedirs(plot_path) plot_model(training_model, to_file=(os.path.join(plot_path, cfg.network) + '.png'), show_shapes=True) except Exception: # TODO: Catch the particular exceptions print(traceback.format_exc()) print(sys.exc_info()[2]) # make prediction model prediction_model = retinanet_bbox(model=model, anchor_params=anchor_params, score_thresh_train=cfg.score_thresh_train, class_specific_filter=cfg.class_specific_nms) # compile model if distance: training_model.compile( loss={ 'regression' : losses.smooth_l1(), 'classification': losses.focal(), 'distance' : losses.smooth_l1(alpha=distance_alpha) }, optimizer=keras.optimizers.adam(lr=lr, clipnorm=0.001) ) else: training_model.compile( loss={ 'regression' : losses.smooth_l1(), 'classification': losses.focal(), }, optimizer=keras.optimizers.adam(lr=lr, clipnorm=0.001) ) return model, training_model, prediction_model
def build_model(self): logger = logging.getLogger(__name__) logger.info("building model...") logger.info("Embedding of shape {}, {}, {}".format( self.__context_vocab_size, self.config.model.embedding_dim, self.__windows_size)) e = Embedding(self.__context_vocab_size, self.config.model.embedding_dim) encoder_inputs = Input(shape=(None, ), name="encoder_input") en_x = e(encoder_inputs) encoder = LSTM(self.config.model.lstm_encoder_dim, return_state=True, dropout=self.config.model.dropout_1, recurrent_dropout=self.config.model.recurrent_dropout_1) encoder_outputs, state_h, state_c = encoder(en_x) # We discard `encoder_outputs` and only keep the states. encoder_states = [state_h, state_c] # Set up the decoder, using `encoder_states` as initial state. decoder_inputs = Input(shape=(None, ), name='decoder_input') dex = e final_dex = dex(decoder_inputs) decoder_lstm = LSTM( self.config.model.lstm_decoder_dim, return_sequences=True, return_state=True, dropout=self.config.model.dropout_2, recurrent_dropout=self.config.model.recurrent_dropout_2) decoder_outputs, _, _ = decoder_lstm(final_dex, initial_state=encoder_states) decoder_dense = Dense(self.__context_vocab_size, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs) self.model = Model([encoder_inputs, decoder_inputs], decoder_outputs) self.model.compile(optimizer='rmsprop', loss=self.config.model.loss, metrics=self.config.model.metrics) print(self.model.summary()) # Encode the input sequence to get the "thought vectors" self.encoder_model = Model(encoder_inputs, encoder_states) # Decoder setup # Below tensors will hold the states of the previous time step decoder_state_input_h = Input( shape=(self.config.model.lstm_decoder_dim, )) decoder_state_input_c = Input( shape=(self.config.model.lstm_decoder_dim, )) decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c] dec_emb2 = dex( decoder_inputs) # Get the embeddings of the decoder sequence # To predict the next word in the sequence, set the initial states to the states from the previous time step decoder_outputs2, state_h2, state_c2 = decoder_lstm( dec_emb2, initial_state=decoder_states_inputs) decoder_states2 = [state_h2, state_c2] decoder_outputs2 = decoder_dense( decoder_outputs2 ) # A dense softmax layer to generate prob dist. over the target vocabulary # Final decoder model self.decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs2] + decoder_states2) # summarize model plot_model(self.encoder_model, to_file=os.path.join(self.report_folder, 'encoder_model.png')) plot_model(self.decoder_model, to_file=os.path.join(self.report_folder, 'decoder_model.png')) plot_model(self.model, to_file=os.path.join(self.report_folder, 'model.png'))
def print_model_summary(): model = unet_depth_segm(5) print(model.summary()) plot_model(model, "unet_depth_segm_model.png")
x = Activation(sigmoid_neg)(x) x = Dropout(0.5)(x) # 1x1 x = Convolution2D(512, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation(sigmoid_neg)(x) x = Dropout(0.5)(x) x = Convolution2D(nb_classes, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = GlobalAveragePooling2D()(x) # Adjust for FCN x = BatchNormalization()(x) x = Activation('softmax')(x) model = Model(inputs=input_, outputs=x) return model if __name__ == '__main__': model = create_model(100, (32, 32, 3)) model.summary() from keras.utils import plot_model plot_model(model, to_file='baseline.png', show_layer_names=False, show_shapes=True)
print('-' * 40) print('KERAS & GPU INFO:') import keras try: print(' Environment: {}'.format(os.environ.get('CONDA_DEFAULT_ENV'))) except: None print(' Version: {} {}'.format(keras.__name__, keras.__version__)) print(' Backend: {}'.format(keras.backend._BACKEND)) print(' Device: {}'.format(keras.backend.T.config.device)) print(' Image Format: {}'.format(keras.backend.image_data_format())) NNmodel = Model.get_NNmodel() if log: from keras.utils import plot_model plot_model(NNmodel, to_file='{}{}_{}_model.png'.format( str(PATH_SETTINGS['path_log']), STAGE, MODEL_ID), show_shapes=True) print(' Input shape: {}'.format(NNmodel.layers[0].input_shape)) print(' Layers: {}'.format(len(NNmodel.layers))) print(' Output shape: {}'.format(NNmodel.layers[-1].output_shape)) print(' Optimizer: {}'.format(NNmodel.optimizer)) print(' Loss: {}'.format(NNmodel.loss)) print(' Metrics: {}'.format(NNmodel.metrics_names)) print('GPU INFO:') print(subprocess.check_output("nvidia-smi", shell=True)) # READ & PREPARE DATASET print('-' * 40) print('READING DATASET') Data = ld.FishDATA() dsetID = ld.read_dsetID()
# generate latent vector Q(z|X) x = Flatten()(x) x = Dense(16, activation='relu')(x) z_mean = Dense(latent_dim, name='z_mean')(x) z_log_var = Dense(latent_dim, name='z_log_var')(x) # use reparameterization trick to push the sampling out as input # note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(latent_dim, ), name='z')([z_mean, z_log_var]) # instantiate encoder model encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') encoder.summary() plot_model(encoder, to_file='vae_cnn_encoder.png', show_shapes=True) # build decoder model latent_inputs = Input(shape=(latent_dim, ), name='z_sampling') x = Dense(shape[1] * shape[2] * shape[3], activation='relu')(latent_inputs) x = Reshape((shape[1], shape[2], shape[3]))(x) x = Conv2DTranspose(filters=64, kernel_size=kernel_size, activation='relu', strides=1, padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2DTranspose(filters=32, kernel_size=kernel_size,
kernel_size=4, strides=1, padding='same', activation='sigmoid')(u3) last_16 = Conv2D(1, kernel_size=4, strides=1, padding='same', activation='sigmoid')(u2) last_8 = Conv2D(1, kernel_size=4, strides=1, padding='same', activation='sigmoid')(u1) #u_model = Model(d0, last_512) mcnn_model = Model(d0, outputs=[ last_512, last_256, last_128, last_64, last_32, last_16, last_8 ]) unet_model = Model(d0, last_512) return unet_model, mcnn_model if __name__ == '__main__': u, m = build_denoising_model(input_shape=(512, 512, 4)) #plot_model(u, 'denoising_u_model.png', show_shapes=True, rankdir='TB') plot_model(m, 'denoising_model.png', show_shapes=True, rankdir='TB') m.summary()
def main(model_name): #'dM06AMFLsrc.mp4' TEST_VIDEO = sys.argv[1] show_images = False diagnose_plots = False #model_dir = './models' model_dir = os.path.join(PROJ_DIR,'log_models') global backend # override backend if provided as an input arg #if len(sys.argv) > 1: #if 'tf' in sys.argv[1].lower(): backend = 'tf' #else: #backend = 'th' print "[Info] Using backend={}".format(backend) if backend == 'tf': model_weight_filename = os.path.join(model_dir, model_name) model_json_filename = os.path.join(model_dir, 'sports1M_model_custom_2l.json') else: print 'please change to backend=tf' print("[Info] Reading model architecture...") model = model_from_json(open(model_json_filename, 'r').read()) #model = c3d_model.get_model(backend=backend) # visualize model model_img_filename = os.path.join(model_dir, 'c3d_model.png') if not os.path.exists(model_img_filename): from keras.utils import plot_model plot_model(model, to_file=model_img_filename) print("[Info] Loading model weights...") model.load_weights(model_weight_filename) print("[Info] Loading model weights -- DONE!") model.compile(loss='mean_squared_error', optimizer='sgd') print("[Info] Loading labels...") with open('labels_aggr.txt', 'r') as f: labels = [line.strip() for line in f.readlines()] print('Total labels: {}'.format(len(labels))) print("[Info] Loading a sample video...") cap = cv2.VideoCapture(TEST_VIDEO) vid = [] while True: ret, img = cap.read() if not ret: break vid.append(cv2.resize(img, (171, 128))) vid = np.array(vid, dtype=np.float32) #plt.imshow(vid[2000]/256) #plt.show() # sample 16-frame clip #start_frame = 100 start_frame = 2 X = vid[start_frame:(start_frame + 16), :, :, :] #diagnose(X, verbose=True, label='X (16-frame clip)', plots=show_images) # subtract mean subtract_mean = False if subtract_mean: mean_cube = np.load('models/train01_16_128_171_mean.npy') mean_cube = np.transpose(mean_cube, (1, 2, 3, 0)) #diagnose(mean_cube, verbose=True, label='Mean cube', plots=show_images) X -= mean_cube #diagnose(X, verbose=True, label='Mean-subtracted X', plots=show_images) # center crop X = X[:, 8:120, 30:142, :] # (l, h, w, c) #diagnose(X, verbose=True, label='Center-cropped X', plots=show_images) if backend == 'th': X = np.transpose(X, (3, 0, 1, 2)) # input_shape = (3,16,112,112) else: pass # input_shape = (16,112,112,3) # get activations for intermediate layers if needed inspect_layers = [ # 'fc6', # 'fc7', ] for layer in inspect_layers: int_model = c3d_model.get_int_model(model=model, layer=layer, backend=backend) int_output = int_model.predict_on_batch(np.array([X])) int_output = int_output[0, ...] print "[Debug] at layer={}: output.shape={}".format(layer, int_output.shape) diagnose(int_output, verbose=True, label='{} activation'.format(layer), plots=diagnose_plots, backend=backend) # inference time_start = time.time() output = model.predict_on_batch(np.array([X])) time_end = time.time() print 'time elapsed for model.predict:', time_end-time_start,'s' # show results print('Saving class probabilitities in probabilities.png') plt.plot(output[0]) plt.title('Probability') plt.savefig("probabilities.png") print('Position of maximum probability: {}'.format(output[0].argmax())) print('Maximum probability: {:.5f}'.format(max(output[0]))) print('Corresponding label: {}'.format(labels[output[0].argmax()])) # sort top five predictions from softmax output top_inds = output[0].argsort()[::-1][:5] # reverse sort and take five largest items print('\nTop 5 probabilities and labels:') for i in top_inds: print('{1}: {0:.5f}'.format(output[0][i], labels[i]))
def main(model_name): show_images = False diagnose_plots = False count_correct = 0 model_dir = os.path.join(PROJ_DIR,'log_models') global backend # override backend if provided as an input arg if len(sys.argv) > 1: if 'tf' in sys.argv[1].lower(): backend = 'tf' else: backend = 'th' print "[Info] Using backend={}".format(backend) if backend == 'th': model_weight_filename = os.path.join(model_dir, model_name) model_json_filename = os.path.join(model_dir, 'sports1M_model_custom_3l.json') #model_json_filename = os.path.join(model_dir, 'sports1M_model_custom_2l.json') else: model_weight_filename = os.path.join(model_dir, model_name) model_json_filename = os.path.join(model_dir, 'sports1M_model_custom_3l.json') #model_json_filename = os.path.join(model_dir, 'sports1M_model_custom_2l.json') print("[Info] Reading model architecture...") #model = model_from_json(open(model_json_filename, 'r').read()) from keras.models import load_model model = load_model(model_weight_filename) # model = c3d_model.get_model(backend=backend) # visualize model model_img_filename = os.path.join(model_dir, 'c3d_model_custom.png') if not os.path.exists(model_img_filename): from keras.utils import plot_model plot_model(model, to_file=model_img_filename) model.load_weights(model_weight_filename) #model.compile(loss='mean_squared_error', optimizer='sgd') #hyhy #print("[Info] Loading labels...") with open('labels_aggr.txt', 'r') as f: labels_txt = [line.strip() for line in f.readlines()] print('Total num of classes: {}'.format(len(labels_txt))) for TEST_VIDEO in TEST_VIDEOS: print 'Test video path name:', TEST_VIDEO if '_F.' in TEST_VIDEO: gt_label = 0 else: gt_label = 1 gt_frame_labels = collect_gt_labels(TEST_VIDEO,'../aggr_vids/gt_labels/gt_anno.txt') cap = cv2.VideoCapture(TEST_VIDEO) fps = cap.get(cv2.cv.CV_CAP_PROP_FPS) print 'frame per second:', fps vid, vid_view, pred_labels, view_interval, frame_i = [], [],[], 25, 0 STOP,clip = False,[] while True: ret, img = cap.read() vid_len = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) #print 'vid len',vid_len #exit(0) k = cv2.waitKey(1) & 0xFF if k == ord('q'): print 'key interrupted' break exit(0) if ret: frame_i += 1 if frame_i % 1 == 0: vid_view.append(img) #vid.append(cv2.resize(img, (171, 128))) vid.append(cv2.resize(img, (INPUT_SIZE, INPUT_SIZE),interpolation=cv2.INTER_AREA)) if frame_i % (view_interval) == 0 and frame_i> (CLIP_LENGTH): print 'frame:',frame_i vid_np = np.array(vid, dtype=np.float32) X = vid_np[0:CLIP_LENGTH, :, :, :] start_frame = frame_i - CLIP_LENGTH offset_time = start_frame / fps print '\nstart frame:{}, offset:{}'.format(start_frame,offset_time) # plt.show() count_correct,pred_txt,pred_label,confidence = eva_one_clip(X, vid_view,start_frame, model, EVA_SAVE_PATH_NO_AGGR, EVA_SAVE_PATH,count_correct, labels_txt,gt_label,TEST_VIDEO) for pred_label_i in xrange(view_interval): pred_labels.append(pred_label) #total_video_frames = frame_i #total_test_clips = int(total_video_frames / CLIP_LENGTH) #print 'vid len', total_video_frames #print 'TP:{:.3f}'.format(count_correct/float(total_test_clips)) demo(vid_view[frame_i%(view_interval)], pred_txt, pred_label, confidence) vid, vid_view = vid[view_interval:max(CLIP_LENGTH,view_interval)], vid_view[view_interval:max(CLIP_LENGTH,view_interval)] else: cap.release() break print 'last frame_i',frame_i,'vid len:',vid_len print 'gt_labels:(len:',len(gt_frame_labels),') ',gt_frame_labels print 'pred_labels:(len:',len(pred_labels),') ',pred_labels def calc_dice(gt_frame_labels,pred_labels): min_len = min(len(gt_frame_labels),len(pred_labels)) score = 0 for i in xrange(min_len): if gt_frame_labels[i] == pred_labels[i]: score +=1 print '2*score:',2.0*score score = 2.0*score/(len(gt_frame_labels) + len(pred_labels)) return score score = calc_dice(gt_frame_labels,pred_labels) print 'dice:',score print 'TEST end.'
def build_model(): word_embed_layer = Embedding( input_dim=embedding_matrix.shape[0], # 索引字典大小 output_dim=embedding_matrix.shape[1], # 词向量的维度 input_length=context_window_size, weights=[embedding_matrix], trainable=True, name='word_embed_layer') candidate_embed_layer = Embedding( input_dim=conceptEmbeddings.shape[0], output_dim=conceptEmbeddings.shape[1], input_length=1, weights=[conceptEmbeddings], # AutoExtend 训练获得 trainable=True) pos_embed_layer = Embedding( input_dim=60, # 索引字典大小 output_dim=20, # pos向量的维度 input_length=context_window_size, trainable=True) x_input = [] to_join = [] attn = [] # addCandidateInput candidate_input = Input(shape=(1, ), dtype='int32', name='candidate_input') candidate_embed = candidate_embed_layer(candidate_input) controller = Flatten()(candidate_embed) x_input.append(candidate_input) # addContextInput left_context_input = Input(shape=(context_window_size, ), dtype='int32', name='left_context_input') right_context_input = Input(shape=(context_window_size, ), dtype='int32', name='right_context_input') left_context_embed = word_embed_layer(left_context_input) left_context_embed = Dropout(0.5)(left_context_embed) right_context_embed = word_embed_layer(right_context_input) right_context_embed = Dropout(0.5)(right_context_embed) x_input += [left_context_input, right_context_input] # # addContextPOSInput # # left_pos_input = Input(shape=(context_window_size,), dtype='int32', name='left_pos_input') # right_pos_input = Input(shape=(context_window_size,), dtype='int32', name='right_pos_input') # x_input += [left_pos_input, right_pos_input] # left_pos_embed = pos_embed_layer(left_pos_input) # right_pos_embed = pos_embed_layer(right_pos_input) # # # 拼接词向量和POS向量 # # left_context_embed = [left_context_embed, left_pos_embed] # left_context_embed = concatenate(left_context_embed, axis=-1) # left_context_embed = Dropout(0.5)(left_context_embed) # right_context_embed = [right_context_embed, right_pos_embed] # right_context_embed = concatenate(right_context_embed, axis=-1) # right_context_embed = Dropout(0.5)(right_context_embed) if context_network == 'cnn': left_context = CNN(left_context_embed) right_context = CNN(right_context_embed) elif context_network == 'mean': left_context = Lambda( mask_aware_mean, output_shape=(context_window_size, ))(left_context_embed) right_context = Lambda( mask_aware_mean, output_shape=(context_window_size, ))(right_context_embed) elif context_network == 'gru': left_context = CuDNNGRU(200)(left_context_embed) right_context = CuDNNGRU(200)( right_context_embed) # , go_backwards=True elif context_network == 'attention': left_rnn = CuDNNGRU(200, return_sequences=True)(left_context_embed) right_rnn = CuDNNGRU(200, return_sequences=True)(right_context_embed) left_context, attn_values_left = buildAttention(left_rnn, controller) right_context, attn_values_right = buildAttention( right_rnn, controller) attn += [attn_values_left, attn_values_right] # attention 的输出 elif context_network == 'composition': # 组合上下文和ID所有相关的信息 left_rnn = CuDNNGRU(200, return_sequences=True)(left_context_embed) right_rnn = CuDNNGRU(200, return_sequences=True)(right_context_embed) left_context, attn_values_left = buildAttention(left_rnn, controller) right_context, attn_values_right = buildAttention( right_rnn, controller) a = multiply([left_context, controller]) b = multiply([right_context, controller]) c = subtract([left_context, controller]) d = subtract([right_context, controller]) e = multiply([c, c]) f = multiply([d, d]) else: raise ("unknown") if mode == 'gating': # 门控机制 a = Dense(200, use_bias=False)(left_context) b = Dense(200, use_bias=False)(right_context) c = add([a, b]) # c = Merge()([a, b]) c = Activation('tanh')(c) c = Dense(1, activation='sigmoid', use_bias=False)(c) f = Lambda(lambda x: 1 - x) left = multiply([c, left_context]) right = multiply([f(c), right_context]) context = add([left, right]) # context = merge([left, right]) to_join += [context] to_join.append(controller) elif mode == 'composition': to_join += [a, b, c, d, e, f] else: to_join += [left_context, right_context] to_join.append(controller) # add mention # mention_input = Input(shape=(max_mention_words,), dtype='int32', name='mention_input') # mention_embed = word_embed_layer(mention_input) # mention_mean = Lambda(mask_aware_mean, output_shape=(200,))(mention_embed) # x_input.append(mention_input) # to_join.append(mention_mean) # join all inputs x = concatenate(to_join) if len(to_join) > 1 else to_join[0] x = Dropout(0.5)(x) # build classifier model x = Dense(200, activation='relu')(x) x = Dense(50, activation='relu')(x) x = Dropout(0.5)(x) output = Dense(2, activation='softmax', name='main_output')(x) model = Model(inputs=x_input, outputs=[output]) # binary_crossentropy categorical_crossentropy adagrad = Adagrad(lr=0.01, decay=1e-6, clipvalue=1.) sgd = SGD(lr=0.015, decay=0.05) model.compile(loss='binary_crossentropy', optimizer='adagrad', metrics=["accuracy"]) attn_model = Model(inputs=x_input, outputs=attn) print("model compiled!") model.summary() plot_model( model, to_file= '/home/administrator/PycharmProjects/keras_bc6_track1/sample/ned/data/model.png', show_shapes=True) return model
squeezenet = x #mobilenet = MobileNet(input_tensor=tf.placeholder('float32',shape=(1,input_size,input_size,3)),include_top=False,input_shape=(input_size,input_size,3),weights=None)(input_image) ##mobilenet = MobileNet(include_top=False,input_shape=(input_size,input_size,3),weights=None)(input_image) print("squeezenet") print(squeezenet) detect = Conv2D(30, (1, 1), strides=(1, 1), padding='same', name='DetectoinLayer0')(squeezenet) print(detect) detect = Reshape((13, 13, 5, 6))(detect) detect = Lambda(lambda args: args[0])([detect, true_boxes]) yolo = Model([input_image, true_boxes], detect) plot_model(yolo, to_file='yolo_squezenet.png') print(detect) yolo.summary() #Profiling ... #opts = tf.profiler.ProfileOptionBuilder.float_operation() #flops = tf.profiler.profile(g, run_meta=run_meta, cmd='scope', options=opts) opts = tf.profiler.ProfileOptionBuilder.trainable_variables_parameter() params = tf.profiler.profile(g, run_meta=run_meta, cmd='scope', options=opts) opts = tf.profiler.ProfileOptionBuilder.float_operation() flops = tf.profiler.profile(g, run_meta=run_meta, cmd='op', options=opts)
# # Inital Training Of Generator Network # In[9]: gen = Model(inputs, decoder(encoder(inputs)), name='Generator') gan = Model(inputs, disc(gen(inputs)), name='GAN') sgd = SGD(lr=1e-4, clipnorm=1, clipvalue=0.5) disc.compile(loss='mean_squared_logarithmic_error', optimizer=sgd, metrics=['acc']) gen = Model(inputs, decoder(encoder(inputs)), name='Generator') gan = Model(inputs, disc(gen(inputs)), name='GAN') import pydot from keras.utils import plot_model plot_model(encoder, to_file='encoder.png') plot_model(decoder, to_file='decoder.png') plot_model(disc, to_file='disc.png') plot_model(gan, to_file='gan.png') # In[10]: gen = Model(inputs, decoder(encoder(inputs)), name='Generator') gan = Model(inputs, disc(gen(inputs)), name='GAN') sgd = SGD(lr=1e-4, clipnorm=1, clipvalue=0.5) disc.compile(loss='mean_squared_logarithmic_error', optimizer=sgd, metrics=['acc']) gen.compile(loss='mean_squared_logarithmic_error', optimizer=sgd, metrics=['acc'])
conv_9_1 = Convolution3D(a / 8 + b, (5, 5, 5), padding='same', data_format="channels_last")(merged_9) add_9 = add([conv_9_1, merged_9]) conv_9_2 = Convolution3D(output_classes, (1, 1, 1), padding='same', data_format="channels_last")(add_9) softmax_layer = Activation(softmax)(conv_9_2) model = Model(input_layer, softmax_layer) model.summary(line_length=113) from keras.utils import plot_model plot_model(model, 'V-Net_tiny2.png', show_shapes=True) #%% def downward_layer_relu(input_layer, n_convolutions, n_output_channels): inl = input_layer for _ in range(n_convolutions - 1): inl = Activation('relu')(Convolution3D( n_output_channels // 2, (5, 5, 5), padding='same', data_format="channels_last")(inl)) conv = Convolution3D(n_output_channels // 2, (5, 5, 5), padding='same', data_format="channels_last")(inl) add_node = add([conv, input_layer])
# model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2),padding='same',name='pool')) # model.add(GlobalMaxPooling2D()) model.add(GlobalAveragePooling2D()) #model.add(Flatten()) model.add(Dense(units=8, activation='softmax')) learning_rate = 1e-4 optimizer_init = Adam(learning_rate=learning_rate) model.compile(loss='categorical_crossentropy', optimizer=optimizer_init, metrics=['accuracy']) print(model.summary()) plot_model(model, to_file=results_dir + 'model_baseline.png', show_shapes=True, show_layer_names=True) print('Done!\n') print(model.summary()) #model.compile(loss='categorical_crossentropy', optimizer=SGD(learning_rate=0.01, momentum=0.9), metrics=['accuracy']) # preprocessing_function=preprocess_input, datagen = ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, preprocessing_function=preprocess_input, rotation_range=0.,
val_acc = extra_hist.epoch_data['val_acc'] print("MODEL: " + str(model_version) + " | RUN: " + str(run_version) + " | #EPOCHS: " + str(inputs.num_epochs)) print("----- ACCURACY -----") print("avg: " + str(np.mean(val_acc))) print("max: " + str(np.max(val_acc))) print("min: " + str(np.min(val_acc))) print("") # -+-+-+-+-+-+-+- SAVING RESULTS -+-+-+-+-+-+-+- print("SAVING MODEL AND RESULTS") # -> data print("Saving data to " + data_name) with open(data_name, "a") as f: writer = csv.writer(f) writer.writerow([model_version, run_version, inputs.num_epochs, 'loss'] + extra_hist.epoch_data['loss']) writer.writerow([model_version, run_version, inputs.num_epochs, 'acc'] + extra_hist.epoch_data['acc']) writer.writerow([model_version, run_version, inputs.num_epochs, 'val_loss'] + extra_hist.epoch_data['val_loss']) writer.writerow([model_version, run_version, inputs.num_epochs, 'val_acc'] + extra_hist.epoch_data['val_acc']) # -> diagram print("Saving model diagram to " + diagram_name) plot_model(model, to_file=diagram_name) # -> graph print("Saving model results graph to " + graph_name) plot_epochs(hist.history, batch_history, graph_name) print(" >< = >< = >< = >< = >< = >< = >< = >< = >< = >< = >< = >< = >< = >< = >< = >< ") print("")
def save_model_graph(self): plot_model(self.gan_model, to_file='/out/GAN_Model.png')
# In[11]: ### START CODE HERE ### img_path = 'images/my_image.jpg' ### END CODE HERE ### img = image.load_img(img_path, target_size=(64, 64)) imshow(img) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) print(happyModel.predict(x)) # ## 5 - Other useful functions in Keras (Optional) # # Two other basic features of Keras that you'll find useful are: # - `model.summary()`: prints the details of your layers in a table with the sizes of its inputs/outputs # - `plot_model()`: plots your graph in a nice layout. You can even save it as ".png" using SVG() if you'd like to share it on social media ;). It is saved in "File" then "Open..." in the upper bar of the notebook. # # Run the following code. # In[12]: happyModel.summary() # In[ ]: plot_model(happyModel, to_file='HappyModel.png') SVG(model_to_dot(happyModel).create(prog='dot', format='svg'))
x = Dense(4096, activation="elu", W_regularizer=l2(0.01))(x) x = Dropout(0.5)(x) x = Dense(2048, activation="elu", W_regularizer=l2(0.01))(x) x = Dense(2048, activation="elu", W_regularizer=l2(0.01))(x) x = Dense(1, activation="linear")(x) return Model(input=input_image, output=x) #Training the nvidia model model = getNvidiaModel() model.compile(loss='mse',optimizer='adam') #Splitting the training and validation data model.fit(X,y,validation_split=Steering_Angle,shuffle=True,epochs=2) model.save('model.h5') plot_model(model, to_file='nvidia.png', show_shapes=True) #########Code For Generator Below. This Was Left Unused Because The GPU Instance Could Handle The Data In-Memory Which Was Much Faster########## '''def generator(df, batch_size=32): num_samples = df.shape[0] while 1: # Loop forever so the generator never terminates for offset in range(0, num_samples, int(batch_size/6)): batch_samples = df[offset:offset+batch_size].reset_index(drop=True) images = [] angles = [] y=[] X=np.empty(shape=[batch_samples.shape[0]*6,160,320,3]) X=X.astype(np.float32)
def printModel(self): plot_model(self.model, to_file='modelChemception.png')
def unet_bn(weights=None, input_size=(512,512,4)): inputs = Input(input_size) conv1 = Convolution2D(32, 3, 3, border_mode='same', init='he_uniform')(inputs) conv1 = BatchNormalization()(conv1) conv1 = keras.layers.advanced_activations.ELU()(conv1) conv1 = Convolution2D(32, 3, 3, border_mode='same', init='he_uniform')(conv1) conv1 = BatchNormalization()(conv1) conv1 = keras.layers.advanced_activations.ELU()(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Convolution2D(64, 3, 3, border_mode='same', init='he_uniform')(pool1) conv2 = BatchNormalization()(conv2) conv2 = keras.layers.advanced_activations.ELU()(conv2) conv2 = Convolution2D(64, 3, 3, border_mode='same', init='he_uniform')(conv2) conv2 = BatchNormalization()(conv2) conv2 = keras.layers.advanced_activations.ELU()(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Convolution2D(128, 3, 3, border_mode='same', init='he_uniform')(pool2) conv3 = BatchNormalization()(conv3) conv3 = keras.layers.advanced_activations.ELU()(conv3) conv3 = Convolution2D(128, 3, 3, border_mode='same', init='he_uniform')(conv3) conv3 = BatchNormalization()(conv3) conv3 = keras.layers.advanced_activations.ELU()(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Convolution2D(256, 3, 3, border_mode='same', init='he_uniform')(pool3) conv4 = BatchNormalization()(conv4) conv4 = keras.layers.advanced_activations.ELU()(conv4) conv4 = Convolution2D(256, 3, 3, border_mode='same', init='he_uniform')(conv4) conv4 = BatchNormalization()(conv4) conv4 = keras.layers.advanced_activations.ELU()(conv4) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) conv5 = Convolution2D(512, 3, 3, border_mode='same', init='he_uniform')(pool4) conv5 = BatchNormalization()(conv5) conv5 = keras.layers.advanced_activations.ELU()(conv5) conv5 = Convolution2D(512, 3, 3, border_mode='same', init='he_uniform')(conv5) conv5 = BatchNormalization()(conv5) conv5 = keras.layers.advanced_activations.ELU()(conv5) up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4],axis=-1) conv6 = Convolution2D(256, 3, 3, border_mode='same', init='he_uniform')(up6) conv6 = BatchNormalization()(conv6) conv6 = keras.layers.advanced_activations.ELU()(conv6) conv6 = Convolution2D(256, 3, 3, border_mode='same', init='he_uniform')(conv6) conv6 = BatchNormalization()(conv6) conv6 = keras.layers.advanced_activations.ELU()(conv6) up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=-1) conv7 = Convolution2D(128, 3, 3, border_mode='same', init='he_uniform')(up7) conv7 = BatchNormalization()(conv7) conv7 = keras.layers.advanced_activations.ELU()(conv7) conv7 = Convolution2D(128, 3, 3, border_mode='same', init='he_uniform')(conv7) conv7 = BatchNormalization()(conv7) conv7 = keras.layers.advanced_activations.ELU()(conv7) up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=-1) conv8 = Convolution2D(64, 3, 3, border_mode='same', init='he_uniform')(up8) conv8 = BatchNormalization()(conv8) conv8 = keras.layers.advanced_activations.ELU()(conv8) conv8 = Convolution2D(64, 3, 3, border_mode='same', init='he_uniform')(conv8) conv8 = BatchNormalization()(conv8) conv8 = keras.layers.advanced_activations.ELU()(conv8) up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=-1) conv9 = Convolution2D(32, 3, 3, border_mode='same', init='he_uniform')(up9) conv9 = BatchNormalization()(conv9) conv9 = keras.layers.advanced_activations.ELU()(conv9) conv9 = Convolution2D(32, 3, 3, border_mode='same', init='he_uniform')(conv9) crop9 = Cropping2D(cropping=((16, 16), (16, 16)))(conv9) conv9 = BatchNormalization()(crop9) conv9 = keras.layers.advanced_activations.ELU()(conv9) conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9) model = Model(input=inputs, output=conv10) model.compile(optimizer=Adam(lr=1e-4), loss=jaccard_coef_loss, metrics=['binary_crossentropy', jaccard_coef_int]) with open('unet-bn-model-summary-report.txt', 'w') as fh: # Pass the file handle in as a lambda function to make it callable model.summary(print_fn=lambda x: fh.write(x + '\n')) if (weights): model.load_weights(weights) plot_model(model, to_file='unet-bn-model.png',show_shapes=True) #vis model return model
for i in range(N-1): x = conv_block(x, filters=filters*8, factor=1, learnall = learnall) nb_conv += 2 nb_conv+= 6 #x = BatchNormalization(axis=channel_axis, momentum=0.1, epsilon=0.0005, gamma_initializer='uniform')(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) x = AveragePooling2D((8,8))(x) x = Flatten()(x) x = Dense(nb_classes, activation='softmax', name = name)(x) model = Model(ip, x) if verbose: print("ResNet-%d-%d with RAM created." % (nb_conv, factor)) return model if __name__ == "__main__": from keras.utils import plot_model from keras.layers import Input from keras.models import Model init = (32, 32, 3) wrn_28_10 = create_wide_residual_network(init, nb_classes=10, N=2, k=2, dropout=0.0) wrn_28_10.summary() plot_model(wrn_28_10, "WRN-16-2.png", show_shapes=True, show_layer_names=True)
def train_and_predict(self): hidden_units = self.params["hidden_units"] dropout = self.params["dropout"] batch_size = self.params["batch_size"] model_path = self.params["model_path"] predict_file = self.params["prediction_path"] # Load data: [[token11, token12, ...],[token21,token22,...]] # and label: [[label11, label12, ...],[label21,label22,...]] X_train_data, y_train_data = load_dataset("train_en.txt") X_dev_data, y_dev_data = load_dataset("dev_en.txt") X_test_data = load_dataset_test("test_en_unlabeled.txt") #get embedding matrix and word_to_index embedding_file_en = 'embedding/glove.6B.300d.txt' embedding_matrix = np.zeros((400003, 300)) word_to_index = dict() i = 1 with open(embedding_file_en, 'r') as f: lines = f.readlines() for line in lines: values = line.split() word = values[0] word_to_index[word] = i coefs = np.array(values[1:], dtype='float32') embedding_matrix[i] = coefs i += 1 word_to_index['__PADDING__'.lower()] = 400001 embedding_matrix[400001] = [0.0] * 300 word_to_index['__OOV__'.lower()] = 400002 embedding_matrix[400002] = [ random.uniform(-1, 1) for j in range(300) ] print('Loaded en_embedding %s word vectors.' % len(embedding_matrix)) print('\n') index_to_word = dict((v, k) for k, v in word_to_index.items()) # Get index -> label and label -> index dictionaries index_to_label = get_index_dict(y_train_data) label_to_index = dict((v, k) for k, v in index_to_label.items()) # Get indexed data and labels - 400002 is the OOV index X_train_index = [[ word_to_index.get(word.lower(), 400002) for word in sentence ] for sentence in X_train_data] X_dev_index = [[ word_to_index.get(word.lower(), 400002) for word in sentence ] for sentence in X_dev_data] X_test_index = [[ word_to_index.get(word.lower(), 400002) for word in sentence ] for sentence in X_test_data] y_train_index = [[label_to_index[label] for label in sentence] for sentence in y_train_data] y_dev_index = [[label_to_index[label] for label in sentence] for sentence in y_dev_data] # For batch training: # Pad additional x=[400001]/y=[0] elements at the end for the last batch: X_train_padded = X_train_index + [[400001] for _ in range( math.ceil(len(X_train_index) / batch_size) * batch_size - len(X_train_index))] X_dev_padded = X_dev_index + [[400001] for _ in range( math.ceil(len(X_dev_index) / batch_size) * batch_size - len(X_dev_index))] X_test_padded = X_test_index + [[400001] for _ in range( math.ceil(len(X_test_index) / batch_size) * batch_size - len(X_test_index))] y_train_padded = y_train_index + [[0] for _ in range( math.ceil(len(y_train_index) / batch_size) * batch_size - len(y_train_index))] y_dev_padded = y_dev_index + [[0] for _ in range( math.ceil(len(y_dev_index) / batch_size) * batch_size - len(y_dev_index))] # Get maximum sentence length to pad instances max_sentence_length = max( map(lambda x: len(x), X_train_data + X_dev_data + X_test_data)) # Get the number of classes: number_of_classes = len(index_to_label.items()) # Pad for all inputs X_train = sequence.pad_sequences(X_train_padded, maxlen=max_sentence_length) X_dev = sequence.pad_sequences(X_dev_padded, maxlen=max_sentence_length, padding='post') X_test = sequence.pad_sequences(X_test_padded, maxlen=max_sentence_length, padding='post') # For categorical cross_entropy we need matrices representing the classes: # Note that we pad after doing the transformation into the matrix! y_train = sequence.pad_sequences(np.asarray([ to_categorical(y_label, number_of_classes + 1) for y_label in y_train_padded ]), maxlen=max_sentence_length) y_dev = sequence.pad_sequences(np.asarray([ to_categorical(y_label, number_of_classes + 1) for y_label in y_dev_padded ]), maxlen=max_sentence_length, padding='post') def build_model(embedding_matrix, max_sentence_length, number_of_classes): model = Sequential() model.add( Embedding(len(embedding_matrix), len(embedding_matrix[0]), input_length=max_sentence_length, weights=[embedding_matrix], mask_zero=True, trainable=False, batch_input_shape=(batch_size, max_sentence_length))) model.add(Dropout(dropout)) model.add( Bidirectional( LSTM(hidden_units, input_shape=(batch_size, max_sentence_length), return_sequences=True))) model.add( Bidirectional( LSTM(hidden_units, input_shape=(batch_size, max_sentence_length), return_sequences=True))) model.add(Dropout(dropout)) model.add(TimeDistributed(Dense(number_of_classes + 1))) model.add(Activation('softmax')) model.compile('adam', 'categorical_crossentropy', metrics=[metrics.categorical_accuracy]) return model class F1ScoreModelCheckpointer(Callback): def __init__(self, model): super(F1ScoreModelCheckpointer).__init__() self.model = model def on_train_begin(self, logs={}): self.best_f1 = 0.0 self.f1s = 0.0 def on_epoch_end(self, batch, logs={}): #1. Get predictions predictions = self.model.predict(self.validation_data[0], batch_size=batch_size) # Compute F1 score for test set: test_pred = [] test_truth = [] #2. Flatten all outputs for sent_pred, sent_truth in zip(predictions, self.validation_data[1]): for lab, word_pred in zip(np.argmax(sent_truth, axis=1), np.argmax(sent_pred, axis=1)): if lab != 0: #remove padding test_truth.append(index_to_label[lab]) test_pred.append( index_to_label.get(word_pred, max(test_truth))) #3. Compute f1 score f1_test = f1_score(test_truth, test_pred, list(index_to_label.values()), average='macro') #4. Store best model self.f1s = f1_test if self.f1s > self.best_f1: self.best_f1 = self.f1s self.model.save_weights(model_path) print(" F1 score %f" % (self.f1s)) return def evaluate(model, raw_data, lab_data): best_f1 = 0.0 f1s = 0.0 #1. Get predictions predictions = model.predict(raw_data, batch_size=batch_size) # Compute F1 score for test set: test_pred = [] test_truth = [] #2. Flatten all outputs for sent_pred, sent_truth in zip(predictions, lab_data): for lab, word_pred in zip(np.argmax(sent_truth, axis=1), np.argmax(sent_pred, axis=1)): if lab != 0: #remove padding test_truth.append(index_to_label[lab]) test_pred.append( index_to_label.get(word_pred, max(test_truth))) #3. Compute f1 score f1_test = f1_score(test_truth, test_pred, list(index_to_label.values()), average='macro') #4. Store best model f1s = f1_test if f1s > best_f1: best_f1 = f1s model.save_weights(model_path) return f1s def train(model, raw_data, lab_data, np_epoch): checkpointer = F1ScoreModelCheckpointer(model) test_f1s = list() epoch = list() for i in range(np_epoch): print(i) model.fit( X_train, y_train, batch_size=batch_size, epochs=1, #1 callbacks=[checkpointer], validation_data=(raw_data, lab_data), shuffle=False) model.reset_states() test_f1s.append(evaluate(model, raw_data, lab_data)) epoch.append(i) model.reset_states() #checkpointer.loss_plot('epoch') history = DataFrame() history['epoch'] = epoch history['test'] = test_f1s return history def predict(model): model.load_weights(model_path) # Get class probabilities for the test set: predictions = model.predict(X_test, batch_size=batch_size) print('writting.......') # Compute F1 score for test set: test_pred = [] #test_truth = [] pre_data = [] for sent_pred in predictions: for word_pred in sent_pred: test_pred.append(index_to_label[word_pred.tolist().index( max(word_pred))]) if word_pred.tolist().index(max(word_pred)) == 0: print("Warning, PADDING label got predicted!") #All sentences ending with ‘.’ are marked as ‘O’ k = 0 for i in range(len(X_test_data)): pre_data.append([]) if X_test_data[i][-1] != '.': for j in range(len(X_test_data[i])): pre_data[i].append([X_test_data[i][j], 'O']) k += 1 else: for j in range(len( X_test_data[i])): #All '.' are marked as 'O'. if X_test_data[i][j] == '.': pre_data[i].append([X_test_data[i][j], 'O']) else: pre_data[i].append( [X_test_data[i][j], test_pred[k]]) k += 1 pre_label = [] for i in pre_data: for j in i: pre_label.append(j[1]) l = 0 with open(predict_file, 'w') as f: for i in range(len(X_test_data)): for j in range(len(X_test_data[i])): f.write(str(X_test_data[i][j])) f.write('_en') f.write('\t') f.write(pre_label[l]) f.write('\n') l += 1 if i != len(X_test_data) - 1: f.write('\n') print('Write finished.\n') history = DataFrame() repeats = 1 #5 for i in range(repeats): np_epoch = self.params['epochs'] model = build_model(embedding_matrix, max_sentence_length, number_of_classes) history = train(model, X_dev, y_dev, np_epoch) plt.plot(history['epoch'], history['test'], color='orange', label='f1 score') plt.savefig('epochs_diagnostic.png') plt.show() plot_model(model, to_file='model.png', show_shapes=True) predict(model)
log_dir = '../log/' weights_dir = '../weights/basic/' train_data_dir = '/home/mcv/datasets/MIT_split/train' val_data_dir = '/home/mcv/datasets/MIT_split/test' test_data_dir = '/home/mcv/datasets/MIT_split/test' img_width = 299 # original: 224 -> we have to change it to 299 to fit the inception_v3 model input shape img_height = 299 batch_size = 32 number_of_epoch = 20 validation_samples = 807 # create the base pre-trained model base_model = InceptionV3(weights='imagenet') plot_model(base_model, to_file=results_dir + 'modelInceptionV3original.png', show_shapes=True, show_layer_names=True) x = base_model.layers[-2].output x = Dense(8, activation='softmax', name='predictions')(x) model = Model(input=base_model.input, output=x) plot_model(model, to_file=results_dir + 'modelInceptionV38classes.png', show_shapes=True, show_layer_names=True) for layer in base_model.layers: layer.trainable = False print(model.summary())
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc']) history = model.fit(x_train, y_train, batch_size=128, epochs=100, validation_data=(x_val, y_val)) end = time.clock() print('Running time: %s Seconds' % (end - start)) print(model.summary()) plot_model(model, show_shapes=True, to_file='model.png') acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(acc) + 1) plt.plot(epochs, acc, 'bo', label='Traning acc') plt.plot(epochs, val_acc, 'b', label='Validation acc') plt.title('Training and validation accuracy') plt.legend() plt.figure
def model_to_png(self, out_file, shapes=True): plot_model( self.model, to_file=out_file, show_shapes=shapes)
#The multi-classifical problem,so we use softmax with 46 NN union to achieve the purpose. x = model.add(layers.Dense(99, activation='softmax')) #Compile the model model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc']) X_train_label = np_utils.to_categorical(X_train_label, 99) X_val_label = np_utils.to_categorical(X_val_label, 99) from keras.utils import plot_model plot_model(model, show_shapes=True, to_file='leaf_classification.png') rmsprop = SGD(lr=0.01, nesterov=True, decay=1e-6, momentum=0.9) model.compile(loss='categorical_crossentropy', optimizer=rmsprop, metrics=['acc']) history = model.fit(input_train, X_train_label, epochs=15, validation_data=(input_val, X_val_label), batch_size=16) from keras.utils import plot_model
######################################################################################################################## #endregion #region VISUALIZE MODEL #region Model overview # https://graphviz.gitlab.io/download/ needs this # and installing pydot-ng and graphviz # NOTE: if you use linux, the path needs to be changed to correct path try: from keras.utils import plot_model import os os.environ[ "PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/' plot_model(model, to_file='results/model.png') plt.figure() img = plt.imread('results/model.png') imgplot = plt.imshow(img) plt.tight_layout() plt.axis('off') if plot_performance: plt.show(block=False) else: plt.clf() except Exception as e: print('Error, cannot visualize model') print(e) #endregion #region Filters
def plotModelPicture(model, model_name): plot_model(model, to_file = outputPath + model_name + '-model.png', show_shapes=False)
# stop() assert len(env.action_space.shape) == 1 nb_actions = env.action_space.shape[0] # Next, we build a very simple model. actor = Sequential() actor.add(Flatten(input_shape=(1,) + env.observation_space.shape)) actor.add(Dense(16)) actor.add(Activation('relu')) actor.add(Dense(nb_actions)) actor.add(Activation('linear')) print(actor.summary()) plot_model(actor, to_file='actor.png', show_shapes=True) action_input = Input(shape=(nb_actions,), name='action_input') observation_input = Input(shape=(1,) + env.observation_space.shape, name='observation_input') flattened_observation = Flatten()(observation_input) x = concatenate([action_input, flattened_observation]) x = Dense(32)(x) x = Activation('relu')(x) x = Dense(1)(x) x = Activation('linear')(x) critic = Model(inputs=[action_input, observation_input], outputs=x) print(critic.summary()) plot_model(critic, to_file='critic.png', show_shapes=True) # # Finally, we configure and compile our agent. You can use every built-in Keras optimizer and
def vaegan_actual_model(original_dim=(64, 64, 3), batch_size=64, latent_dim=128, epochs=50, mse_flag=True): '''VAE model.''' # VAE model = encoder + decoder # build encoder model input_shape = original_dim inputs = Input(shape=input_shape, name='encoder_input') x = Conv2D(64, (5, 5), strides=(2, 2), padding='same')(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(128, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(256, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Flatten()(x) x = Dense(2048)(x) x = BatchNormalization()(x) x = Activation('relu', name='z_mean')(x) z_mean = Dense(latent_dim, name='x_mean')(x) z_log_var = Dense(latent_dim, name='x_log_var')(x) # use reparameterization trick to push the sampling out as input # note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(latent_dim, ), name='z')([z_mean, z_log_var]) #encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') encoder.summary() plot_model(encoder, to_file='vaegan_encoder.png', show_shapes=True) # build decoder model latent_inputs = Input(shape=(latent_dim, ), name='z_sampling') x = Dense(8 * 8 * 256)(latent_inputs) x = BatchNormalization()(x) x = Activation('relu')(x) x = Reshape((8, 8, 256))(x) x = Conv2DTranspose(256, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(128, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(32, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(3, (5, 5), strides=(1, 1), padding='same')(x) outputs = Activation('tanh')(x) # instantiate decoder model decoder = Model(latent_inputs, outputs, name='decoder') decoder.summary() plot_model(decoder, to_file='vaegan_decoder.png', show_shapes=True) # instantiate VAE model outputs = decoder(encoder(inputs)[2]) vae = Model(inputs, outputs, name='vae_mlp') #outputs = Dense(original_dim, activation='sigmoid')(x) if mse_flag: reconstruction_loss = mse(inputs, outputs) else: reconstruction_loss = binary_crossentropy(inputs, outputs) reconstruction_loss *= original_dim[0] * original_dim[1] * original_dim[2] kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5 vae_loss = K.mean(reconstruction_loss + kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer=RMSprop(lr=0.0003)) vae.summary() plot_model(vae, to_file='vae.png', show_shapes=True) return encoder, decoder, vae
# 4x4 x = MaxPooling2D(pool_size=(2, 2))(x) x = Convolution2D(512, (4, 4), padding='valid', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = Activation('elu')(x) x = Dropout(0.5)(x) # 1x1 x = Convolution2D(512, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = Activation('elu')(x) x = Dropout(0.5)(x) x = Convolution2D(nb_classes, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = GlobalAveragePooling2D()(x) # Adjust for FCN x = Activation('softmax')(x) model = Model(inputs=input_, outputs=x) return model if __name__ == '__main__': model = create_model(100, (32, 32, 3)) model.summary() from keras.utils import plot_model plot_model(model, to_file='baseline.png', show_layer_names=False, show_shapes=True)
# Decoder x = Conv2D(512, kernel, padding='same')(x) x = BatchNormalization()(x) x = UpSampling2D(size=pool_size)(x) x = Conv2D(256, kernel, padding='same')(x) x = BatchNormalization()(x) x = UpSampling2D(size=pool_size)(x) x = Conv2D(128, kernel, padding='same')(x) x = BatchNormalization()(x) x = UpSampling2D(size=pool_size)(x) x = Conv2D(64, kernel, padding='same')(x) x = BatchNormalization()(x) x = Conv2D(nClasses, (1, 1), padding='valid')(x) outputs = Activation('softmax')(x) model = Model(inputs=inputs, outputs=outputs, name='segnet') return model if __name__ == '__main__': model = segnet((496, 496, 6), 4) model.summary() from keras.utils import plot_model plot_model(model, show_shapes=True, to_file='SegNet_tf.png')
from keras.utils import plot_model from keras_vggface import VGGFace model = VGGFace(model='vgg16') plot_model(model, to_file='vgg16.png', show_shapes=True) model = VGGFace(model='resnet50') plot_model(model, to_file='resnet50.png', show_shapes=True) model = VGGFace(model='senet50') plot_model(model, to_file='senet50.png' ,show_shapes=True)
for i in range(0,4396): for j in range(0,38): if predictions[i][j] == max(predictions[i]): # Finding Max. To Find Predicted Class if j == true_labels[i]: predicted_labels[i] = j count+=1 print(count) print(count/4396) # No Batch-Normalization and No Dropout + Augmentation "CustomModel.h5" print(f1_score(true_labels,predicted_labels, average="macro")) # Finding F1-Score print(precision_score(true_labels,predicted_labels, average="macro")) # Finding Precision print(recall_score(true_labels,predicted_labels, average="macro")) # Finding Recall plt.plot(history.history['acc']) plt.title('Model Accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train'],loc='upper left') plt.show() plt.plot(history.history['loss']) plt.title('Model Loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train'],loc='upper left') plt.show() from keras.utils import plot_model plot_model(model,to_file='model.png')
def vaegan_complete_model(original_dim=(64, 64, 3), batch_size=64, latent_dim=128, epochs=50, mse_flag=True, lr=0.0003): '''VAEGAN complete model.''' # VAE model = encoder + decoder # build encoder model input_shape = original_dim inputs = Input(shape=input_shape, name='encoder_input') x = Conv2D(64, (5, 5), strides=(2, 2), padding='same', name='enc_conv1')(inputs) x = BatchNormalization(name='enc_bn1')(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2, name='enc_LReLU1')(x) x = Conv2D(128, (5, 5), strides=(2, 2), padding='same', name='enc_conv2')(x) x = BatchNormalization(name='enc_bn2')(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2, name='enc_LReLU2')(x) x = Conv2D(256, (5, 5), strides=(2, 2), padding='same', name='enc_conv3')(x) x = BatchNormalization(name='enc_bn3')(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2, name='enc_LReLU3')(x) x = Flatten()(x) #x = Dense(2048, name = 'enc_dense1')(x) #x = BatchNormalization(name = 'enc_bn4')(x) #x = Activation('relu', name='z_mean')(x) #x = LeakyReLU(alpha = 0.2, name = 'enc_dense2')(x) x_mean = Dense(latent_dim, name='x_mean')(x) x_mean = BatchNormalization()(x_mean) z_mean = LeakyReLU(alpha=0.2, name='z_mean')(x_mean) x_log_var = Dense(latent_dim, name='x_log_var')(x) x_log_var = BatchNormalization()(x_log_var) z_log_var = LeakyReLU(alpha=0.2, name='z_log_var')(x_log_var) # use reparameterization trick to push the sampling out as input # note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(latent_dim, ), name='z')([z_mean, z_log_var]) #encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') print('encoder') encoder.summary() #plot_model(encoder, to_file='vaegan_encoder_complete.png', show_shapes=True) # build decoder model latent_inputs = Input(shape=(latent_dim, ), name='z_sampling') x = Dense(8 * 8 * 256)(latent_inputs) x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Reshape((8, 8, 256))(x) x = Conv2DTranspose(256, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2DTranspose(128, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2DTranspose(32, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2DTranspose(3, (5, 5), strides=(1, 1), padding='same')(x) outputs = Activation('tanh')(x) # instantiate decoder model decoder = Model(latent_inputs, outputs, name='decoder') print('decoder') decoder.summary() #plot_model(decoder, to_file='vaegan_decoder_complete.png', show_shapes=True) #instantiate discriminator x_recon = Input(shape=input_shape) #x = Conv2D(32,(5,5), strides =(2,2),padding='same')(x_recon) x = Conv2D(32, (5, 5), strides=(1, 1), padding='same')(x_recon) #x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(128, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(256, (5, 5), strides=(2, 2), padding='same')(x) x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) l_layer = Conv2D(256, (5, 5), strides=(2, 2), padding='same')(x) l_layer_shape = (8, 8, 256) input_disc2 = Input(shape=l_layer_shape) x = BatchNormalization()(input_disc2) #x = BatchNormalization()(l_layer) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Flatten()(x) x = Dense(512)(x) x = BatchNormalization()(x) #x = Activation('relu')(x) x = LeakyReLU(alpha=0.2)(x) x = Dense(1)(x) output_dis = Activation('sigmoid')(x) #discriminator_2 = Model(input_disc2, output_dis, name='discriminator_1') '''construct discriminator with l_layer output''' discriminator_l = Model(x_recon, l_layer, name='discriminator_l') print('discriminator_l') discriminator_l.summary() ''' construct discriminator second part''' discriminator_2 = Model(input_disc2, output_dis, name='discriminator_2') print('discriminator_2') discriminator_2.summary() ''' construct discriminator (discriminator trainable) ''' discriminator = Model(x_recon, discriminator_2(discriminator_l(x_recon)), name='discriminator') print('discriminator') #optimizer = RMSprop(lr=lr) discriminator.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=lr), metrics=['accuracy']) print('discriminator') discriminator.summary() '''construct model 1 (encoder trainable) ''' encoder.trainable = True decoder.trainable = False discriminator_l.trainable = False discriminator_2.trainable = False print('encoder_model_try') disc_xtilde = discriminator_l(decoder(encoder(inputs)[2])) disc_x = discriminator_l(inputs) out_recon = decoder(encoder(inputs)[2]) model1_enc = Model(inputs, [discriminator_2(disc_x), discriminator_2(disc_xtilde)], name='model_encoder1') model1_enc.summary() plot_model(model1_enc, to_file='model1_enc.png', show_shapes=True) ''' model1_enc = Model(inputs, discriminator_l(decoder(encoder(inputs)[2])), name='model1_encoder') print('model1 encoder trainable') plot_model(model1_enc, to_file='model1_enc.png', show_shapes=True) ''' '''Define losses for encoder parameter update''' reconstruction_loss = nll_loss(disc_x, disc_xtilde) #reconstruction_loss *= original_dim[0]*original_dim[1]*original_dim[2] #recon_mse = mse(inputs,out_recon) #recon_mse *= original_dim[0]*original_dim[1]*original_dim[2] kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5 #vae_loss = K.mean(reconstruction_loss + kl_loss+recon_mse) vae_loss = K.mean(reconstruction_loss + kl_loss) model1_enc.add_loss(vae_loss) model1_enc.compile(optimizer=RMSprop(lr=lr * (0.5))) #model1_enc.compile(optimizer=RMSprop(lr=0.003*0.001)) #model1_enc.summary() ''' construct model 2 (decoder trainable) ''' encoder.trainable = False decoder.trainable = True discriminator_l.trainable = False discriminator_2.trainable = False zp = Input(shape=(latent_dim, ), name='zp') out_zp = discriminator_2(discriminator_l(decoder(zp))) model2_dec = Model( [inputs, zp], [discriminator_2(disc_x), discriminator_2(disc_xtilde), out_zp], name='model2_encoder') print('model2 decoder trainable') model2_dec.summary() plot_model(model2_dec, to_file='model2_dec.png', show_shapes=True) #reconstruction_loss = nll_loss(disc_x,disc_xtilde) #reconstruction_loss *= original_dim[0]*original_dim[1]*original_dim[2] gamma = 1e-6 #vae_loss = K.mean(reconstruction_loss + kl_loss) #gan_real_loss = binary_crossentropy(K.ones_like(discriminator_2(disc_x)),discriminator_2(disc_x)) gan_fake_loss1 = binary_crossentropy( K.ones_like(discriminator_2(disc_xtilde)), discriminator_2(disc_xtilde)) gan_fake_loss2 = binary_crossentropy(K.ones_like(out_zp), out_zp) #gan_fake_loss1 = binary_crossentropy(K.zeros_like(discriminator_2(disc_xtilde)),discriminator_2(disc_xtilde)) #gan_fake_loss2 = binary_crossentropy(K.zeros_like(out_zp),out_zp) gan_fake_loss = K.mean(gan_fake_loss1 + gan_fake_loss2) #dec_loss = K.mean(gamma*reconstruction_loss - gan_fake_loss) #dec_loss = gamma*reconstruction_loss - gan_fake_loss dec_loss = gamma * reconstruction_loss + gan_fake_loss model2_dec.add_loss(dec_loss) model2_dec.compile(optimizer=RMSprop(lr=lr)) #optimizer = RMSprop(lr=lr) #discriminator.compile(loss='binary_crossentropy', # optimizer=optimizer, # metrics=['accuracy']) #print('discriminator') return encoder, decoder, discriminator, model1_enc, model2_dec
Cl2, epochs=epochs, batch_size=batch_size, callbacks=tbLogs, verbose=2, validation_split=cv_split) accuracy = np.asarray(log.history['acc']) loss = np.asarray(log.history['loss']) val_loss = np.asarray(log.history['val_loss']) val_acc = np.asarray(log.history['val_acc']) #score = model.evaluate(A_test, Cl2_test, batch_size=A.shape[1]) model.save(model_name) plot_model(model, to_file=model_directory + '/keras_MLP_model.png', show_shapes=True) print('\n =============================================') print(' \033[1mKeras MLP\033[0m - Model Configuration') print(' =============================================') print("\n Training set file:", learnFile) print("\n Data size:", A.shape, "\n") for conf in model.get_config(): print(conf, "\n") print('\n ==========================================') print(' \033[1mKeras MLP\033[0m - Training Summary') print(' ==========================================') print("\n Accuracy - Average: {0:.2f}%; Max: {1:.2f}%".format( 100 * np.average(accuracy), 100 * np.amax(accuracy))) print(" Loss - Average: {0:.4f}; Min: {1:.4f}".format(np.average(loss),