def create_model(self): recurrent_units = 60 main_input = Input(shape=(self.maxLen,), name='news') embedding = Embedding(self.max_features, self.embed_size, weights=[self.embedding_matrix], trainable=False, name='embedding') embedding_layer = embedding(main_input) embedding_layer = SpatialDropout1D(0.25)(embedding_layer) rnn_1 = Bidirectional(GRU(recurrent_units, return_sequences=True, dropout=0.1, recurrent_dropout=0.1))( embedding_layer) x = Bidirectional(GRU(recurrent_units, return_sequences=True, dropout=0.1, recurrent_dropout=0.1))(rnn_1) # x = concatenate([rnn_1, rnn_2], axis=2) last = Lambda(lambda t: t[:, -1], name='last')(x) maxpool = GlobalMaxPooling1D()(x) attn = AttentionWeightedAverage()(x) average = GlobalAveragePooling1D()(x) ocr_input = Input(shape=(self.ocrLen,), name='ocr') ocr_embedding_layer = embedding(ocr_input) ocr_embedding_layer = SpatialDropout1D(0.25)(ocr_embedding_layer) ocr_rnn_1 = Bidirectional(GRU(recurrent_units // 2, return_sequences=True, dropout=0.1, recurrent_dropout=0.1))( ocr_embedding_layer) ocr_rnn_2 = Bidirectional(GRU(recurrent_units // 2, return_sequences=True, dropout=0.1, recurrent_dropout=0.1))(ocr_rnn_1) ocr_maxpool = GlobalMaxPooling1D()(ocr_rnn_2) ocr_attn = AttentionWeightedAverage()(ocr_rnn_2) all_views = concatenate([last, maxpool, attn, average, ocr_maxpool, ocr_attn], axis=1) x = Dropout(0.5)(all_views) dense2 = Dense(3, activation="softmax")(x) res_model = Model(inputs=[main_input, ocr_input], outputs=dense2) plot_model(model, to_file="model.png", show_shapes=True) # res_model = Model(inputs=[main_input], outputs=main_output) return res_model
def define_model(length, vocab_size): # channel 1: 三个 channel 只是对应的 kernel_size 不同, 表示使用不同的窗口大小来控制 n-gram; 4-grams, 6-grams, and 8-grams inputs1 = Input(shape=(length,)) embedding1 = Embedding(vocab_size, 100)(inputs1) conv1 = Conv1D(filters=32, kernel_size=4, activation='relu')(embedding1) drop1 = Dropout(0.5)(conv1) pool1 = MaxPooling1D(pool_size=2)(drop1) flat1 = Flatten()(pool1) # channel 2 inputs2 = Input(shape=(length,)) embedding2 = Embedding(vocab_size, 100)(inputs2) conv2 = Conv1D(filters=32, kernel_size=6, activation='relu')(embedding2) drop2 = Dropout(0.5)(conv2) pool2 = MaxPooling1D(pool_size=2)(drop2) flat2 = Flatten()(pool2) # channel 3 inputs3 = Input(shape=(length,)) embedding3 = Embedding(vocab_size, 100)(inputs3) conv3 = Conv1D(filters=32, kernel_size=8, activation='relu')(embedding3) drop3 = Dropout(0.5)(conv3) pool3 = MaxPooling1D(pool_size=2)(drop3) flat3 = Flatten()(pool3) # merge merged = concatenate([flat1, flat2, flat3]) # interpretation dense1 = Dense(10, activation='relu')(merged) outputs = Dense(1, activation='sigmoid')(dense1) model = Model(inputs=[inputs1, inputs2, inputs3], outputs=outputs) # compile model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print(model.summary()) plot_model(model, show_shapes=True, to_file='tmp_multichannel.png') return model
def test_plot_sequential_embedding(): """Fixes #11376""" model = Sequential() model.add(Embedding(10000, 256, input_length=400, name='embed')) vis_utils.plot_model(model, to_file='model1.png', show_shapes=True, show_layer_names=True) os.remove('model1.png')
def main(): input1 = Input(shape=(64,), dtype='float32') input2 = Input(shape=(64,), dtype='float32') btp = NeuralTensorLayer(output_dim=32, input_dim=64)([input1, input2]) p = Dense(units=1)(btp) model = Model(inputs=[input1, input2], outputs=[p]) sgd = SGD(lr=0.0000000001, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='mean_squared_error', optimizer=sgd) from keras.utils.vis_utils import plot_model plot_model(model,'model.png',show_shapes=True)
def test_plot_model(): model = Sequential() model.add(Conv2D(filters=2, kernel_size=(2, 3), input_shape=(3, 5, 5), name='conv')) model.add(Flatten(name='flat')) model.add(Dense(5, name='dense1')) vis_utils.plot_model(model, to_file='model1.png', show_layer_names=False) os.remove('model1.png') model = Sequential() model.add(LSTM(16, return_sequences=True, input_shape=(2, 3), name='lstm')) model.add(TimeDistributed(Dense(5, name='dense2'))) vis_utils.plot_model(model, to_file='model2.png', show_shapes=True) os.remove('model2.png')
def compile(self,load=False): model = Sequential() model.add(Dense(n_hidden,input_shape=(4,))) model.add(bn()) model.add(Activation('relu')) model.add(Dense(n_hidden)) model.add(bn()) model.add(Activation('relu')) model.add(Dense(3)) model.add(Activation('softmax')) adam = optimizers.Adam(lr=self.lr) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy']) self.model = model plot_model(model,show_shapes=True,to_file='model.png',rankdir='TB') model.summary() if load == True: model.load_weights(self.save_path) print("Loaded weights from {0}.".format(self.save_path))
def train(epochs=200,batch_size=64,mode=1): import numpy as np import os from keras import callbacks from keras.utils.vis_utils import plot_model if mode==1: num_classes = 10 (x_train,y_train),(x_test,y_test) = load_cifar_10() else: num_classes = 100 (x_train,y_train),(x_test,y_test) = load_cifar_100() model = CapsNetv1(input_shape=[32, 32, 3], n_class=num_classes, n_route=3) print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') model.summary() log = callbacks.CSVLogger('results/capsule-cifar-'+str(num_classes)+'-log.csv') tb = callbacks.TensorBoard(log_dir='results/tensorboard-capsule-cifar-'+str(num_classes)+'-logs', batch_size=batch_size, histogram_freq=True) checkpoint = callbacks.ModelCheckpoint('weights/capsule-cifar-'+str(num_classes)+'weights-{epoch:02d}.h5', save_best_only=True, save_weights_only=True, verbose=1) lr_decay = callbacks.LearningRateScheduler(schedule=lambda epoch: 0.001 * np.exp(-epoch / 10.)) plot_model(model, to_file='models/capsule-cifar-'+str(num_classes)+'.png', show_shapes=True) model.compile(optimizer=optimizers.Adam(lr=0.001), loss=[margin_loss, 'mse'], loss_weights=[1., 0.1], metrics={'output_recon':'accuracy','output':'accuracy'}) from utils.helper_function import data_generator generator = data_generator(x_train,y_train,batch_size) model.fit_generator(generator, steps_per_epoch=x_train.shape[0] // batch_size, validation_data=([x_test, y_test], [y_test, x_test]), epochs=epochs, verbose=1, max_q_size=100, callbacks=[log,tb,checkpoint,lr_decay])
def test_plot_model(): model = Sequential() model.add(Conv2D(2, kernel_size=(2, 3), input_shape=(3, 5, 5), name='conv')) model.add(Flatten(name='flat')) model.add(Dense(5, name='dense1')) vis_utils.plot_model(model, to_file='model1.png', show_layer_names=False) os.remove('model1.png') model = Sequential() model.add(LSTM(16, return_sequences=True, input_shape=(2, 3), name='lstm')) model.add(TimeDistributed(Dense(5, name='dense2'))) vis_utils.plot_model(model, to_file='model2.png', show_shapes=True) os.remove('model2.png') inner_input = Input(shape=(2, 3), dtype='float32', name='inner_input') inner_lstm = Bidirectional(LSTM(16, name='inner_lstm'), name='bd')(inner_input) encoder = Model(inner_input, inner_lstm, name='Encoder_Model') outer_input = Input(shape=(5, 2, 3), dtype='float32', name='input') inner_encoder = TimeDistributed(encoder, name='td_encoder')(outer_input) lstm = LSTM(16, name='outer_lstm')(inner_encoder) preds = Dense(5, activation='softmax', name='predictions')(lstm) model = Model(outer_input, preds) vis_utils.plot_model(model, to_file='model3.png', show_shapes=True, expand_nested=True, dpi=300) os.remove('model3.png')
def fit(inp_layer, inp_embed, X, y): #inp_layer, inp_embed = feature_generate(X, cate_columns, cont_columns) input = merge(inp_embed, mode = 'concat') # deep layer for i in range(6): if i == 0: deep = Dense(272, activation='relu')(Flatten()(input)) else: deep = Dense(272, activation='relu')(deep) # cross layer cross = CrossLayer(output_dim = input.shape[2].value, num_layer = 8, name = "cross_layer")(input) #concat both layers output = merge([deep, cross], mode = 'concat') output = Dense(y.shape[1], activation = 'softmax')(output) model = Model(inp_layer, output) print(model.summary()) plot_model(model, to_file = 'model.png', show_shapes = True) model.compile(Adam(0.01), loss = 'categorical_crossentropy', metrics = ["accuracy"]) model.fit([X[c] for c in X.columns], y, batch_size = 256, epochs = 10) return model
x_test =np.linspace(-5,5,200) x_test=np.array(x_test).reshape((len(x_test),1)) y_test =np.sin(x_test) x_test =scaler.transform(x_test) #prediction data x_prd =np.linspace(-3,3,101) x_prd =np.array(x_prd).reshape((len(x_prd),1)) x_prd =scaler.transform(x_prd) y_prd =np.sin(x_prd) #plot testing data fig,ax =plt.subplots() ax.plot(x_prd,y_prd,'r') model =Sequential() model.add(Dense(100,input_dim=1)) model.add(Activation('relu')) model.add(Dense(50,activation='relu')) model.add(Dense(1,activation='tanh')) model.summary() model.compile(loss='mean_squared_error',optimizer=SGD() ,metrics=['accuracy']) hist =model.fit(x_test,y_test,batch_size=10,nb_epoch=20) out =model.predict(x_prd,batch_size=1) ax.plot(x_prd,out,'k--',lw=4) ax.set_xlabel('Measured') ax.set_ylabel('Predicted') plt.show() plot_model(model,'model.png')
from keras import models from keras import layers network = models.Sequential() network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28, ))) network.add(layers.Dense(10, activation='softmax')) network.compile( optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # json = network.to_json() # print(json) print(network.summary()) from keras.utils.vis_utils import plot_model #from keras.utils import plot_model plot_model(network, to_file='model.png') train_images = train_images.reshape((60000, 28 * 28)) print(train_images.shape) print(train_images.dtype) train_images = train_images.astype('float32') / 255 print(train_images.dtype) test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255 from keras.utils import to_categorical train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels)
outputs = Conv2D(2, (1, 1), activation='softmax', name='output')(c9) #loss = Lambda(weighted_categorical_crossentropy)([outputs, weight_masks, true_masks]) #model = Model(inputs=[inputs,weight_masks,true_masks], outputs=loss) model = Model(inputs=inputs, outputs=outputs) #model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[mean_iou]) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[mean_iou]) #model.compile(optimizer='adam', loss=lambda y_true, y_pred:y_pred) #model.compile(optimizer='adam', loss=identity_loss) #model.compile(optimizer='adam', loss=pixel_loss, metrics=[mean_iou]) model.summary() #%% plot_model(model, show_shapes=True, to_file='U-net1.png') #%% #y = np.zeros([600,128,128,2],dtype = np.float32) #y_test = np.zeros([70,128,128,2],dtype = np.float32) #%% #x_train = X_train[0:600] #y_train = Y_train[0:600:] #x_test = X_train[600:] #y_test = Y_train[600:] ##%% #x_train = np.array(x_train) #y_train = np.array(y_train) #weight_mask = np.array(weight_mask) #mask = weight_mask[0:600] #mask_test = weight_mask[600:] ##%%
outputs = Concatenate(axis=-1)([GlobalAveragePooling2D()(xception), # Takes a list of the two models used of the same shape and returns a single GlobalAveragePooling2D()(nas_net)]) # concatenated model while averaging the pooling operation for spatial data. outputs = Dropout(0.5)(outputs) # Regularize the data to reduce overfitting in our model. outputs = Dense(1, activation='sigmoid')(outputs) model = Model(inputs, outputs) # Keras creates a model with all the necessary layers required in the computation of outputs given inputs # model.compile is the configuration for the model to be trained with. # In this case, we chose the Adam optimizer, which is an extension of stochastic gradient descent that is used for computer vision and natural language #lr=0.0001, decay=0.00001 model.compile(optimizer=Adam(lr=0.01, decay=0.01), loss='binary_crossentropy', metrics=['accuracy']) model.summary() plot_model(model, to_file=MODEL_PLOT_FILE, show_shapes=True, show_layer_names=True) # Run this block to load a pretrained model for this project # Edit the path file for your own trained models !ls -la /content/drive/My\ Drive/8epochs model.load_weights('/content/drive/My Drive/8epochs/elbueno.h5') # Data Setup for the predictions, it takes 6 random images of both positive and negative folders # It creates the folder or resets them and adds 6 .tif images to be further used in the next cell. PREDICTION = DIR_NAME + 'predictions/' ZERO_SET = '/content/competitions/training/0' ZPREDICTION_SET = DIR_NAME + 'predictions/0/'
best_params = grid_search.best_params_ # ------- print('\n> Fitting MLP with best parameters from grid search') X_train, X_val, y_train, y_val = train_test_split(X, y, shuffle=False) print(' - X train shape: {}\n - Y train shape: {}'.format( X_train.shape, y_train.shape)) print(' - X val shape: {}\n - Y val shape: {}'.format(X_val.shape, y_val.shape)) print() best_model = build_model(regularizer_mode=None, **best_params) best_model.summary() plot_model(best_model, to_file=os.path.join(output_folder, 'model', 'model.pdf'), show_shapes=True) batch_size = best_params['batch_size'] epochs = best_params['epochs'] history = best_model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_val, y_val), verbose=1, callbacks=[ tf.keras.callbacks.EarlyStopping( monitor='val_loss', patience=5, min_delta=0.01,
decoder_input_seq = Input(shape=(None, num_decoder_tokens)) decoder_LSTM = LSTM(LSTM_decoder_units, return_state=True, return_sequences=True) #return states of decoder are not required during training decoder_LSTM_outputs, _, _ = decoder_LSTM(decoder_input_seq, initial_state=encoder_states) decoder_output_layer = Dense(num_decoder_tokens, activation="softmax") decoder_LSTM_outputs = decoder_output_layer(decoder_LSTM_outputs) #build the model model = Model([encoder_input_seq, decoder_input_seq], decoder_LSTM_outputs) #plot the model #plot the built model plot_model(model, to_file='model_train.png', show_shapes=True) #training the model model.compile(optimizer="rmsprop", loss="categorical_crossentropy") model.fit([encoder_inputs, decoder_inputs], decoder_outputs, batch_size=train_batch_size, epochs=epochs, validation_split=0.2) #define a new model for testing #define the state inputs to the decoder #define the encoder model to get the states of the encoder model to feed into decoder encoder_model = Model(encoder_input_seq, encoder_states) decoder_input_s1 = Input(shape=(LSTM_encoder_units, ))
def weights_model(timesteps=101, dim=512, unit=128, emotion_embedding_dim=64, n_class=5): inputs = Input((timesteps, dim)) x = BatchNormalization()(inputs) x = LSTM(unit, return_sequences=True)(x) x = Reshape((timesteps, unit))(x) # FC (W^h) for mapping the dim from unit to emotion_embedding_dim x = Lambda(lambda x: tf.unstack(x, axis=1),name='unstack1')(x) # len(x)=timestaps xs = [] # unit->emotion_embedding_dim dense2=Dense(emotion_embedding_dim,name='Wh') for x_i in x: x_temp=dense2(x_i) xs.append(x_temp) x = xs # FC (e={e_1,e_2,...}) for embedding mapping from emotion_embedding_dim to n_class xs = [] # emotion_embedding_dim->n_class emotion_vectors=Dense(n_class,activation='softmax', name='emotion_vectors') for x_i in x: x_temp=emotion_vectors(x_i) xs.append(x_temp) f_weights = Lambda(lambda x: tf.stack(x, axis=1), name='f_weights')(xs) # len(x)=timestaps x = Lambda(lambda x: tf.reduce_mean(x, axis=1),name='reduce_mean1')(f_weights) return Model(inputs=inputs, outputs=x) model=weights_model() plot_model(model,to_file='f_weights.png',show_shapes=False)
model = Model(img_input, output) # model.summary() model.compile( optimizer=SGD(lr=lr_init, decay=lr_decay, momentum=0.9), # model.compile(optimizer=Adam(lr=lr_init, decay=lr_decay), # loss='categorical_crossentropy', loss='binary_crossentropy', metrics=['acc', mean_iou]) return model '''binary_accuracy: 对二分类问题,计算在所有预测值上的平均正确率 categorical_accuracy:对多分类问题,计算再所有预测值上的平均正确率 sparse_categorical_accuracy:与categorical_accuracy相同,在对稀疏的目标值预测时有用 top_k_categorical_accracy: 计算top-k正确率,当预测值的前k个值中存在目标类别即认为预测正确 sparse_top_k_categorical_accuracy:与top_k_categorical_accracy作用相同,但适用于稀疏情况''' if __name__ == "__main__": model = unet29_nobn( input_shape=(512, 512, 3), num_classes=2, lr_init=0.001, lr_decay=0.001, vgg_weight_path= 'E:/Model weights/VGG16_VGG19_and ResNet50/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' ) # vgg_weight_path = '/media/ding/Study/Model weights/VGG16_VGG19_and ResNet50/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5') # model.eval() model.summary() plot_model(model, to_file='unet29_model.png', show_shapes=True)
def modelRGBD(): inp_RGB = Input(shape =(IMG_SIZE , IMG_SIZE, 3)) conv_layer1 = Conv2D(8, (7,7), strides=(1, 1), padding='valid', activation='relu')(inp_RGB) conv_layer2 = Conv2D(8, (7,7), strides=(1, 1), padding='valid', activation='relu')(conv_layer1) #BN_1 = BatchNormalization()(conv_layer2) pool_layer1 = MaxPooling2D(pool_size=(2, 2), strides=(1,1), padding='valid')(conv_layer2) dropout1 = Dropout(0.5)(pool_layer1) conv_layer3 =Conv2D(8, (3,3), strides=(1, 1), padding='valid', activation='relu')(dropout1) conv_layer4 = Conv2D(16, (3,3), strides=(1, 1), padding='valid', activation='relu')(conv_layer3) #BN_2 = BatchNormalization()(conv_layer4) pool_layer2 = MaxPooling2D(pool_size=(2, 2), strides=(1,1), padding='valid')(conv_layer4) dropout2 = Dropout(0.2)(pool_layer2) conv_layer5 = Conv2D(32, (3,3), strides=(1, 1), padding='valid', activation='relu')(dropout2) #BN_3 = BatchNormalization()(conv_layer5) pool_layer3 = MaxPooling2D(pool_size=(2, 2), strides=(1,1), padding='valid')(conv_layer5) dropout3 = Dropout(0.2)(pool_layer3) flatten_layer = Flatten()(dropout3) hidden1 = Dense(512, activation = 'relu')(flatten_layer) #BN_4 = BatchNormalization()(hidden1) dropout4 = Dropout(0.2)( hidden1) inp_D = Input(shape =(IMG_SIZE , IMG_SIZE, 1)) conv_layer1_D = Conv2D(8, (7,7), strides=(1, 1), padding='valid', activation='relu')(inp_D) conv_layer2_D = Conv2D(8, (7,7), strides=(1, 1), padding='valid', activation='relu')(conv_layer1_D) #BN_1 = BatchNormalization()(conv_layer2) pool_layer1_D = MaxPooling2D(pool_size=(2, 2), strides=(1,1), padding='valid')(conv_layer2_D) dropout1_D = Dropout(0.5)(pool_layer1_D) conv_layer3_D =Conv2D(16, (3,3), strides=(1, 1), padding='valid', activation='relu')(dropout1_D) conv_layer4_D = Conv2D(16, (3,3), strides=(1, 1), padding='valid', activation='relu')(conv_layer3_D) #BN_2 = BatchNormalization()(conv_layer4) pool_layer2_D = MaxPooling2D(pool_size=(2, 2), strides=(1,1), padding='valid')(conv_layer4_D) dropout2_D = Dropout(0.2)(pool_layer2_D) conv_layer5_D = Conv2D(32, (3,3), strides=(1, 1), padding='valid', activation='relu')(dropout2_D) #BN_3 = BatchNormalization()(conv_layer5) pool_layer3_D = MaxPooling2D(pool_size=(2, 2), strides=(1,1), padding='valid')(conv_layer5_D) dropout3_D = Dropout(0.2)(pool_layer3_D) flatten_layer_D = Flatten()(dropout3_D) hidden1_D = Dense(512, activation = 'relu')(flatten_layer_D) #BN_4 = BatchNormalization()(hidden1) dropout4_D = Dropout(0.2)( hidden1_D) hidden_merge = Concatenate(axis = -1)([dropout4 , dropout4_D]) # Combining both streams hidden = Dense(1024,activation = 'relu')(hidden_merge) #BN = BatchNormalization()(hidden) dropout = Dropout(0.5)(hidden) #hidden = Dense(1024,activation = 'relu')(dropout) out = Dense(4,activation='softmax')(dropout) model1 = Model([inp_RGB, inp_D],out) model1.summary() plot_model(model1, to_file='model_plot.png', show_shapes=True, show_layer_names=True) sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9) model1.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['acc']) return model1
def retain(ARGS): """Create the model""" #Define the constant for model saving reshape_size = ARGS.emb_size + ARGS.numeric_size if ARGS.allow_negative: embeddings_constraint = FreezePadding() beta_activation = 'tanh' output_constraint = None else: embeddings_constraint = FreezePadding_Non_Negative() beta_activation = 'sigmoid' output_constraint = non_neg() #Get available gpus , returns empty list if none glist = get_available_gpus() def reshape(data): """Reshape the context vectors to 3D vector""" return K.reshape(x=data, shape=(K.shape(data)[0], 1, reshape_size)) #Code Input codes = L.Input((None, None), name='codes_input') inputs_list = [codes] #Calculate embedding for each code and sum them to a visit level codes_embs_total = L.Embedding( ARGS.num_codes + 1, ARGS.emb_size, name='embedding', embeddings_constraint=embeddings_constraint)(codes) codes_embs = L.Lambda(lambda x: K.sum(x, axis=2))(codes_embs_total) #Numeric input if needed if ARGS.numeric_size: numerics = L.Input((None, ARGS.numeric_size), name='numeric_input') inputs_list.append(numerics) full_embs = L.concatenate([codes_embs, numerics], name='catInp') else: full_embs = codes_embs #Apply dropout on inputs full_embs = L.Dropout(ARGS.dropout_input)(full_embs) #Time input if needed if ARGS.use_time: time = L.Input((None, 1), name='time_input') inputs_list.append(time) time_embs = L.concatenate([full_embs, time], name='catInp2') else: time_embs = full_embs #Setup Layers #This implementation uses Bidirectional LSTM instead of reverse order # (see https://github.com/mp2893/retain/issues/3 for more details) #If training on GPU and Tensorflow use CuDNNLSTM for much faster training if glist: alpha = L.Bidirectional(L.CuDNNLSTM(ARGS.recurrent_size, return_sequences=True), name='alpha') beta = L.Bidirectional(L.CuDNNLSTM(ARGS.recurrent_size, return_sequences=True), name='beta') else: alpha = L.Bidirectional(L.LSTM(ARGS.recurrent_size, return_sequences=True, implementation=2), name='alpha') beta = L.Bidirectional(L.LSTM(ARGS.recurrent_size, return_sequences=True, implementation=2), name='beta') alpha_dense = L.Dense(1, kernel_regularizer=l2(ARGS.l2)) beta_dense = L.Dense(ARGS.emb_size + ARGS.numeric_size, activation=beta_activation, kernel_regularizer=l2(ARGS.l2)) #Compute alpha, visit attention alpha_out = alpha(time_embs) alpha_out = L.TimeDistributed(alpha_dense, name='alpha_dense_0')(alpha_out) alpha_out = L.Softmax(axis=1)(alpha_out) #Compute beta, codes attention beta_out = beta(time_embs) beta_out = L.TimeDistributed(beta_dense, name='beta_dense_0')(beta_out) #Compute context vector based on attentions and embeddings c_t = L.Multiply()([alpha_out, beta_out, full_embs]) c_t = L.Lambda(lambda x: K.sum(x, axis=1))(c_t) #Reshape to 3d vector for consistency between Many to Many and Many to One implementations contexts = L.Lambda(reshape)(c_t) #Make a prediction contexts = L.Dropout(ARGS.dropout_context)(contexts) output_layer = L.Dense(1, activation='sigmoid', name='dOut', kernel_regularizer=l2(ARGS.l2), kernel_constraint=output_constraint) #TimeDistributed is used for consistency # between Many to Many and Many to One implementations output = L.TimeDistributed(output_layer, name='time_distributed_out')(contexts) #Define the model with appropriate inputs model = Model(inputs=inputs_list, outputs=[output]) plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) return model
def on_train_begin(self, logs=None): plot_model(self.model, to_file=self.filename, show_shapes=True, show_layer_names=True)
from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(5, input_dim=4, activation='relu')) model.add(Dense(5, activation='sigmoid')) model.add(Dense(5, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) print(model.summary()) from keras.utils.vis_utils import plot_model plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True) """ ) ( ( ( /( )\ ) )\ ( ) ) ( )\()) ((_) ( /( ((_) )\ ) ( /( ( ( /( )( ((_)\ _ )(_)) _ (()/( )(_)) )\ )(_)) (()\ | |(_) | | ((_)_ | | )(_)) ((_)_ ((_) ((_)_ ((_) | '_ \ | | / _` | | | | || | / _` | (_-< / _` | | '_| |_.__/ |_| \__,_| |_| \_, | \__,_| /__/ \__,_| |_| |__/ """ import networkx as nx import matplotlib.pyplot as pltg = nx.Graph()# giris katmanı g = nx.Graph()# giris katmanı
fe = Dropout(0.5)(fe) # normal fe = Conv2D(256, (3, 3), padding='same', kernel_initializer=init)(fe) fe = BatchNormalization()(fe) fe = LeakyReLU(alpha=0.2)(fe) fe = Dropout(0.5)(fe) # flatten feature maps fe = Flatten()(fe) # real/fake output out1 = Dense(1, activation='sigmoid')(fe) # class label output out2 = Dense(n_classes, activation='softmax')(fe) # define model model = Model(in_image, [out1, out2]) # compile model opt = Adam(lr=0.0002, beta_1=0.5) model.compile( loss=['binary_crossentropy', 'sparse_categorical_crossentropy'], optimizer=opt) return model # define the discriminator model model = define_discriminator() # summarize the model model.summary() # plot the model plot_model(model, to_file='discriminator_plot.png', show_shapes=True, show_layer_names=True)
n_train = int(len(df)) df_train = df[:n_train] df_val = df[n_train:] # Load training and validation data im_shape = (256, 256) X_train, y_train = loadDataJSRT(df_train, path, im_shape) X_val, y_val = loadDataJSRT(df_val, path, im_shape) # Build model inp_shape = X_train[0].shape UNet = build_UNet2D_4L(inp_shape) UNet.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # Visualize model plot_model(UNet, 'model.png', show_shapes=True) ########################################################################################## model_file_format = 'model.{epoch:03d}.hdf5' print model_file_format checkpointer = ModelCheckpoint(model_file_format, period=10) train_gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, rescale=1., zoom_range=0.2, fill_mode='nearest', cval=0) test_gen = ImageDataGenerator(rescale=1.)
# The decoder RNN could be multiple layers stacked or a single layer model.add(LSTM(32, return_sequences=True)) # For each of step of the output sequence, decide which character should # be chosen model.add(TimeDistributed(Dense(data_dim))) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(dataX, dataY, epochs=1000) # Store model graph in png # (Error occurs on in python interactive shell) plot_model(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True) # Create test data set for fun testX = [] testY = [] for i in range(10): rand_pick = np.random.choice(10, 7) x = [char_dic[digit[c]] for c in rand_pick] y = [alpha[c] for c in rand_pick] testX.append(x) testY.append(y) # One-hot encoding testX = np_utils.to_categorical(testX, num_classes=num_classes)
nb_epoch = 70 else: nb_epoch = 40 model.fit(x_train_data, y_train_data, batch_size=batch_size, epochs=nb_epoch, verbose=1, shuffle=True, validation_split=validation, callbacks=[history_data]) dump_history(store_path, history_data) model.save(os.path.join(store_path, 'model.h5')) plot_model(model, to_file=os.path.join(store_path, 'model.png')) test_y = model.predict_classes(x_test_data, batch_size=batch_size) test_output = np.zeros((len(test_y) + 1, 2), dtype='|S5') test_output[0, 0] = "id" test_output[0, 1] = "label" for i in range(test_output.shape[0] - 1): test_output[i + 1, 0] = str(i) test_output[i + 1, 1] = str(test_y[i]) np.savetxt(os.path.join(store_path, 'predict.csv'), test_output, delimiter=",", fmt="%s")
ClassRoot='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/Data/' if __name__ == '__main__': #model48=ClassNet_48() #model36=ClassNet_36() #model24=ClassNet_24() # #plot(model,to_file='/home/customer/document/lung-nodule-detection/huangyj/ClassModel.png',show_shapes=True) # #plot_model(model48,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/ClassModel48.png',show_shapes=True) #plot_model(model36,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/ClassModel36.png',show_shapes=True) #plot_model(model24,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/ClassModel24.png',show_shapes=True) aug=20 use_existing=False model=ClassNet_48()#ClassNet_MultiScale() WeightName_load='classnet_Flex_7_10.hdf5'#_7_6_2.hdf5' WeightName_save='classnet_Flex_7_10.hdf5' plot_model(model,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/'+WeightName_save+'.png',show_shapes=True) if use_existing: model.load_weights(weights_path + WeightName_load) #plot_model(model24,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/ClassModel_conc6_19.png',show_shapes=True) #patch1=np.load("/media/customer/新加卷/LUNG/pos.npy") patch1=np.load(ClassRoot+'train/'+"Resized_Train_Patch_pos.npy")#"Class_Train_Pos_64.npy")#[:,:,8:56,8:56,8:56]#[:,:,:,:,::-1]#.transpose([0,1,2,4,3])#.astype(np.float32)/255.0 #patch1=patch1[0:patch1.shape[0]]#-aug] # patch1=np.zeros([patch1_temp.shape[0]/2,1,48,48,48],dtype=np.uint8) # for i in range(patch1.shape[0]/20): # patch1[i*10:i*10+10]=patch1_temp[i*20:i*20+10] #patch1=patch1[0:20000]2 patch2=np.load(ClassRoot+'train/'+"Class_train_neg_resized.npy")#[:,:,:,:,::-1]#.transpose([0,1,2,4,3]) #patch3=np.load(ClassRoot+'train/'+"Class_Train_BakNeg.npy") #patch2=np.concatenate((patch2,patch3),axis=0) sample_weight1=np.ones([patch1.shape[0]],dtype=np.float32) sample_weight1=sample_weight1*1.0
def conv(): # Model constants input_shape = (height, width, 1) optimizer = optimizers.Adadelta() batch_normalization = True dropout_rate = 0.4 epochs = 5 # Set up the data source = 'data/dataset_norm_lbrsdftlog.csv' paths, labels = CSVParser(source)() paths_labels = list(zip(paths, labels)) random.shuffle(paths_labels) paths, labels = zip(*paths_labels) X_train = np.asarray(paths[0:int(0.5 * len(paths))]) y_train = np.asarray(labels[0:int(0.5 * len(paths))]) X_test = np.asarray(paths[int(0.5 * len(paths)):int(0.75 * len(paths))]) y_test = np.asarray(labels[int(0.5 * len(paths)):int(0.75 * len(paths))]) X_val = np.asarray(paths[int(0.75 * len(paths)):]) y_val = np.asarray(labels[int(0.75 * len(paths)):]) num_classes = np.unique(labels).shape[0] # Set up the labels le = preprocessing.LabelEncoder() le = preprocessing.LabelEncoder() le.fit(np.unique(labels)) y_train = le.transform(y_train) y_test = le.transform(y_test) y_val = le.transform(y_val) y_train = to_categorical(y_train, num_classes=num_classes) y_test = to_categorical(y_test, num_classes=num_classes) y_val = to_categorical(y_val, num_classes=num_classes) # Set up the generator training_gen = Generator(X_train, y_train, batch_size=16, loader_fn=img_load, balance_samples=True) validation_gen = Generator(X_val, y_val, batch_size=16, loader_fn=img_load, balance_samples=True) test_gen = Generator(X_test, y_test, batch_size=16, loader_fn=img_load, balance_samples=True) # Build model model = Sequential() # Convolutional layers: # Convolutional layer 1 model.add( Conv2D(filters=16, kernel_size=(7, 7), strides=1, activation='relu', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='same')) if batch_normalization: model.add(BatchNormalization()) # Convolutional layer 2 model.add( Conv2D(filters=32, kernel_size=(5, 5), strides=1, activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')) if batch_normalization: model.add(BatchNormalization()) # Convolutional layer 3 model.add( Conv2D(filters=64, kernel_size=(3, 3), strides=1, activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')) if batch_normalization: model.add(BatchNormalization()) # Convolutional layer 4 model.add(Conv2D(128, (3, 3), strides=1, activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')) if batch_normalization: model.add(BatchNormalization()) # Convolutional layer 5 model.add(Conv2D(128, (3, 3), strides=1, activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')) if batch_normalization: model.add(BatchNormalization()) # Convolutional layer 6 model.add(Conv2D(256, (3, 3), strides=1, activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')) if batch_normalization: model.add(BatchNormalization()) # Dense layers: model.add(Flatten()) model.add(Dense(1024, activation='relu')) model.add(Dense(512, activation='relu')) if batch_normalization: model.add(BatchNormalization()) if dropout_rate > 0: model.add(Dropout(dropout_rate)) # Classification layer: model.add(Dense(num_classes, activation='softmax')) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) plot_model(model, to_file='models/scratch_model.png', show_shapes=True, show_layer_names=True) print(model.summary()) # Network training: # es = EarlyStoppingRange(monitor='acc', min_delta=0.01, patience=25, # verbose=2, mode='auto', min_val_monitor=0.8) es = keras.callbacks.EarlyStopping(monitor='loss', patience=2, verbose=2, mode='auto') mchkpt = keras.callbacks.ModelCheckpoint('models/scratch_model/' 'model_checkpoint.h5') H = model.fit_generator(generator=training_gen, validation_data=validation_gen, epochs=epochs, callbacks=[es, mchkpt], verbose=1) score = model.evaluate_generator(generator=test_gen, verbose=1) print('Test loss:', score[0]) print('Test accuracy:', score[1]) # Plot the training loss and accuracy plt.style.use("ggplot") plt.figure(figsize=(10, 5)) N = len(H.epoch) plt.plot(np.arange(0, N), H.history["loss"], label="train_loss") plt.plot(np.arange(0, N), H.history["loss"], label="val_loss") plt.plot(np.arange(0, N), H.history["acc"], label="train_acc") plt.plot(np.arange(0, N), H.history["acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch") plt.ylabel("Loss/Accuracy") plt.legend(loc="lower left") plt.savefig('scratch_train.png') print('[INFO] serializing network...') model.save('models/scratch_model/conv.h5') print('DONE')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sun Dec 22 23:08:54 2019 @author: david """ from keras.utils.vis_utils import plot_model import matplotlib.pyplot as plt import matplotlib.image as mpimg from keras.models import load_model model = load_model("models/mnist_simple_cnn_10epochs.h5") ruta = "models/model_plot_mnist2.png" # Genarar la gráfica plot_model(model, to_file=ruta, show_shapes=True, show_layer_names=True) # Mostrar la grafica aquí img = mpimg.imread(ruta) plt.figure(figsize=(30, 15)) imgplot = plt.imshow(img)
def print_summary(self): self.model.summary() with open(path.join(self.config.model_dir, "model_summary.txt"),'w') as msf: self.model.summary(print_fn=lambda x: msf.write(x + "\n")) from keras.utils.vis_utils import plot_model plot_model(self.model, to_file=self.model_dir+"/model_plot.png", show_shapes=True, show_layer_names=True) plot_model(self.ae._encoder, to_file=self.model_dir+"/enc_plot.png", show_shapes=True, show_layer_names=True) plot_model(self.ae._decoder, to_file=self.model_dir+"/dec_plot.png", show_shapes=True, show_layer_names=True) plot_model(self.ae._p_pred, to_file=self.model_dir+"/p_pred_plot.png", show_shapes=True, show_layer_names=True) plot_model(self.pred.model, to_file=self.model_dir+"/pred_plot.png", show_shapes=True, show_layer_names=True) plot_model(self.latent_compression, to_file=self.model_dir+"/lc_plot.png", show_shapes=True, show_layer_names=True) if self.in_out_states: plot_model(self.state_init_model, to_file=self.model_dir+"/state_init_plot.png", show_shapes=True, show_layer_names=True)
#fc = Dropout(0.5)(fc) #add dropout prediction = Dense(num_classes, activation='softmax', name='prediction')(fc) model = Model(inputs=base_model.input, outputs=prediction) opt = SGD(lr=0.001, momentum=0.9, decay=0.0005) #opt = RMSprop(lr=0.001) #model.load_weights("/home/n-kamiya/models/model_without_MAMC/model_osme_inceptv3_beta.best_loss.hdf5") model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) #%% plot_model(model, to_file="model.png", show_shapes=True) #%% implement checkpointer and reduce_lr (to prevent overfitting) import datetime now = datetime.datetime.now() checkpointer = ModelCheckpoint( filepath= '/home/n-kamiya/models/model_without_MAMC/model_osme_se_inceptv3_alpha_p.best_loss_.hdf5', verbose=1, save_best_only=True) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, min_lr=0.0000001)
model.add(Dense(10, activation='relu', input_shape=( 1090, 1090, 1, ))) # set of FC => RELU layers model.add(Dense(500)) model.add(Activation("relu")) model.add(Convolution2D(50, 5, 5, border_mode="same")) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Flatten()) # softmax classifier model.add(Dense(class_numb)) model.add(Activation("softmax")) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) plot_model(model, show_shapes=True, to_file=('model.png')) history = model.fit(train_x, train_y, validation_data=(val_x, val_y), batch_size=5, epochs=15, verbose=1, shuffle=True) print(model.evaluate(test_x, test_y, verbose=0))
def SCVAE(expr, patience=30, metric='Silhouette', outliers=False, prefix=None, k=None, label=None, id_map=None, log=True, scale=True, rep=0): expr[expr < 0] = 0.0 if log: expr = np.log2(expr + 1) if scale: for i in range(expr.shape[0]): expr[i, :] = expr[i, :] / np.max(expr[i, :]) if outliers: o = outliers_detection(expr) expr = expr[o == 1, :] if label is not None: label = label[o == 1] if rep > 0: expr_train = np.matlib.repmat(expr, rep, 1) else: expr_train = np.copy(expr) vae_ = VAE(in_dim=expr.shape[1], loss=config['loss']) vae_.vaeBuild() print_summary(vae_.vae) if prefix is not None: plot_model(vae_.vae, to_file=prefix + '_model.eps', show_shapes=True) wait = 0 best_metric = -np.inf epoch = config['epoch'] batch_size = config['batch_size'] res_cur = [] aux_cur = [] for e in range(epoch): print("Epoch %d/%d" % (e + 1, epoch)) loss = vae_.vae.fit(expr_train, expr_train, epochs=1, batch_size=batch_size, shuffle=True) train_loss = -loss.history['loss'][0] #val_loss = -loss.history['val_loss'][0] print("Loss:" + str(train_loss)) #print(h1) if k is None and label is not None: k = len(np.unique(label)) # for r in res: # print("======"+str(r.shape[1])+"========") # #if r.shape[1] == 2: # # r = cart2polar(r) # pred,si = clustering( r,k=k ) # if label is not None: # metrics_ = measure( pred,label ) # metrics_['Silhouette'] = si # cur_metric = train_loss #metrics[metric] if best_metric < cur_metric: best_metric = cur_metric wait = 0 res = vae_.ae.predict(expr) res_cur = res aux_cur = vae_.aux.predict(expr) #if prefix is not None: # model_file = prefix+'_model_weights_'+str(e)+'.h5' # vae_.vae.save_weights(model_file) else: wait += 1 if e > 100 and wait > patience: break ## use best_e for subsequent analysis ## visualization if prefix is not None: for r in res_cur: _, dim = r.shape pic_name = prefix + '_dim' + str(dim) + '.eps' if dim == 2: #r = cart2polar(r) if outliers: o = outliers_detection(r) r_p = r[o == 1] label_p = label[o == 1] else: r_p = np.copy(r) label_p = np.copy(label) fig = print_2D(r_p, label_p, id_map=id_map) fig.savefig(pic_name) else: fig32 = print_heatmap(r, label=label, id_map=id_map) fig32.savefig(pic_name) ## analysis results aux_res = h5py.File(prefix + '_res.h5', mode='w') aux_res.create_dataset(name='AUX', data=aux_cur) aux_res.create_dataset(name='EXPR', data=expr) count = 1 for r in res_cur: aux_res.create_dataset(name='RES' + str(count), data=r) count += 1 aux_res.close() return res_cur
def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer): # Create the *Keras* model if cf.model_name == 'fcn8': model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, load_pretrained=cf.load_imageNet) elif cf.model_name == 'unet': model = build_unet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'segnet_basic': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=True) elif cf.model_name == 'segnet_vgg': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=False) elif cf.model_name == 'resnetFCN': model = build_resnetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'densenetFCN': model = build_densenetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'lenet': model = build_lenet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'alexNet': model = build_alexNet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'vgg16': model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'vgg19': model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'resnet50': model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'InceptionV3': model = build_inceptionV3(in_shape, cf.dataset.n_classes, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=False) elif cf.model_name == 'tiny-yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=True) else: raise ValueError('Unknown model') # Load pretrained weights if cf.load_pretrained: print(' loading model weights from: ' + cf.weights_file + '...') model.load_weights(cf.weights_file, by_name=True) # Compile model model.compile(loss=loss, metrics=metrics, optimizer=optimizer) # Show model structure if cf.show_model: model.summary() plot_model(model, to_file=os.path.join(cf.savepath, 'model.png')) # Output the model print(' Model: ' + cf.model_name) # model is a keras model, Model is a class wrapper so that we can have # other models (like GANs) made of a pair of keras models, with their # own ways to train, test and predict return One_Net_Model(model, cf, optimizer)
encoded_imgs = encoder.predict(x_test) decoded_imgs = decoder.predict(encoded_imgs) n = 10 # 要可视化的数字图片个数 plt.figure(figsize=(20, 4)) for i in range(n): # 原图: ax = plt.subplot( 2, n, i + 1) #!这里必须用i+1,而不是i,因为ValueError: num must be 1 <= num <= 20, not 0 plt.imshow(x_test[i].reshape(28, 28)) plt.gray() #显示灰度图片 ax.get_xaxis().set_visible(False) #不显示x轴坐标 ax.get_yaxis().set_visible(False) #不显示y轴坐标 # 重构图: ax = plt.subplot(2, n, i + 1 + n) plt.imshow(decoded_imgs[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() # 模型可视化 # ============================================================================== #from keras.utils.visualize_util import plot #版本1的模块 from keras.utils.vis_utils import plot_model #版本2 更新了接口 plot_model(autoencoder, to_file='simple_autoencoder.png')
#determine training and validation steps train_steps_per_epoch = np.floor(x_train_private.shape[0]/settings['batch_size']).astype(np.int32) val_steps_per_epoch = np.floor(x_val_private.shape[0]/settings['batch_size']).astype(np.int32) #determine loss weights per loss obf_loss_weight = K.variable(settings['loss_weight_obf']) att_loss_weight = K.variable(1 - settings['loss_weight_obf']) folder = 'results/propdAdd_semiblind_approach1_dataset3/weight_{}_number_{}'.format(settings['loss_weight_obf'], number) if not os.path.exists(folder): os.makedirs(folder) obfuscator, obfuscator_part, comb = get_model.load_model(settings) plot_model(comb, to_file=folder + '\combination.png', show_shapes=True) plot_model(obfuscator, to_file=folder + '\obfuscator.png', show_shapes=True) obfuscator.compile(optimizer = Adam(lr=settings['learning_rate_obf']), loss = 'mean_absolute_error') comb.compile(optimizer=Adam(lr=settings['learning_rate_att']), loss = ['mean_absolute_error', 'mean_squared_error', 'binary_crossentropy', 'binary_crossentropy'], loss_weights=[obf_loss_weight, obf_loss_weight, att_loss_weight, att_loss_weight], metrics = ['mae']) #create metrics dictionary metrics = dict() metrics['train_public_loss'] = np.zeros(settings['epochs']) metrics['train_private_loss'] = np.zeros(settings['epochs']) metrics['val_public_loss'] = np.zeros(settings['epochs']) metrics['val_private_loss'] = np.zeros(settings['epochs']) metrics['att_train_public_loss'] = np.zeros(settings['epochs']) metrics['att_train_private_loss'] = np.zeros(settings['epochs']) metrics['att_val_public_loss'] = np.zeros(settings['epochs']) metrics['att_val_private_loss'] = np.zeros(settings['epochs'])
parser.add_argument('--save_dir', default='result') parser.add_argument('--is_training', default=0, type=int) parser.add_argument('--weights', default='pretrained_model_siu2018_capsulenet.h5') parser.add_argument('--lr', default=0.001, type=float) args = parser.parse_args() print(args) if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) (x_train, y_train), (x_test, y_test) = veri_yukle() model, eval_model = CapsNet(input_shape=x_train.shape[1:], n_class=len(np.unique(np.argmax(y_train, 1))), num_routing=args.num_routing) model.summary() plot_model(model, to_file=args.save_dir + '/model.png', show_shapes=True) if args.weights is not None: # Model ağırlıkları verilir model.load_weights(args.weights) if args.is_training: train(model=model, data=((x_train, y_train), (x_test, y_test)), args=args) else: # Ağırlıkları verildiği sürece test işlemi yapılır. if args.weights is None: print( 'No weights are provided. Will test using random initialized weights.' ) test(model=eval_model, data=(x_test, y_test))
r5 = np.mean([reward_rec[i:i + 10] for i in range(0, len(reward_rec), 10)], axis=1) plt.plot(range(len(r5)), r5, c='b') plt.xlabel('iters') plt.ylabel('mean score') copy_critic_to_actor() model_loaded = keras.models.load_model('crtic_2000.HDF5') pred = predict(actor_q_model, X_test) accuracy_score(y_test, pred) plot_model(model_loaded, 'model.png', True) Image('model.png') X_train_og = X_train.reshape((-1, 28, 28)) X_test_og = X_test.reshape((-1, 28, 28)) rnn_model = Sequential() rnn_model.add(GRU(64, input_shape=(28, 28))) rnn_model.add(Dense(32, activation='relu')) rnn_model.add(Dense(num_actions, activation='softmax')) rnn_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) rnn_model.fit(X_train_og,
def plot(model, file_path): plot_model(model, to_file=file_path, show_shapes=True)
model.save('Outputs/' + str(modelName) + '/' + str(modelName) + "_ModelDetails.h5") # DELETING EXISTING MODEL # del model # # LOADING MODEL FROM THE SAVED H5 FILE # model = load_model('Outputs/' + str(modelName) + '/' + str(modelName) + "_ModelDetails.h5") ################################################################################################################## # SAVING MODEL ARCHITECTURE AND TRAINING AND VALIDATION DATA FROM HISTORY FUNCTION AS IMAGES ################################################################################################################## plot_model(model, to_file='Outputs/' + str(modelName) + '/' + str(modelName) + "_modelPlot.png", show_shapes=True, show_layer_names=True) # PLOTS FOR ACCURACY AND LOSS FROM HISTORY FUNCTION # FOR ACCURACY plt.figure(figsize=(10, 10), dpi=150) plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('Model Accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.savefig('Outputs/' + str(modelName) + '/' + str(modelName) + '_Accuracy_Graph.png', dpi=150)
generated += sent_str print('----- Generating with seed: "' + sent_str + '"') sys.stdout.write(generated) for i in range(1): f_x = np.zeros((1, maxlen_words, len(words))) r_x = np.zeros((1, maxlen_words, len(words))) for t, word in enumerate(f_sent_list): f_x[0, t, word_indices[word]] = 1. for t, word in enumerate(r_sent_list): r_x[0, t, word_indices[word]] = 1. preds = model.predict([f_x,r_x], verbose=0)[0] print('\n\nbefore_sampling\n') print_top5(preds) next_index = sample(preds, diversity) next_word = indices_word[next_index] generated = generated + ' ' + next_word tmp =[] tmp.append(next_word) f_sent_list = f_sent_list[1:] + tmp print('\nafter_sampling\n↓') sys.stdout.write(next_word) sys.stdout.flush() print('\n↑') print() plot_model(model, to_file='./model_image/model_word_merge_new.png')
print(args) import os if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # load dataset from datasets import load_mnist, load_usps if args.dataset == 'mnist': x, y = load_mnist() elif args.dataset == 'usps': x, y = load_usps('data/usps') elif args.dataset == 'mnist-test': x, y = load_mnist() x, y = x[60000:], y[60000:] # prepare the DCEC model dcec = DCEC(input_shape=x.shape[1:], filters=[32, 64, 128, 10], n_clusters=args.n_clusters) plot_model(dcec.model, to_file=args.save_dir + '/dcec_model.png', show_shapes=True) dcec.model.summary() # begin clustering. optimizer = 'adam' dcec.compile(loss=['kld', 'mse'], loss_weights=[args.gamma, 1], optimizer=optimizer) dcec.fit(x, y=y, tol=args.tol, maxiter=args.maxiter, update_interval=args.update_interval, save_dir=args.save_dir, cae_weights=args.cae_weights) y_pred = dcec.y_pred print('acc = %.4f, nmi = %.4f, ari = %.4f' % (metrics.acc(y, y_pred), metrics.nmi(y, y_pred), metrics.ari(y, y_pred)))
from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.utils.vis_utils import plot_model model = Sequential() model.add(Dense(2, input_dim=1, activation='relu')) model.add(Dense(1, activation='sigmoid')) plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) from keras.layers.convolutional import Conv2D from keras.layers.pooling import MaxPooling2D from keras.optimizers import SGD from keras import backend as K K.set_image_data_format('channels_first') # what parameters mean https://keras.io/layers/convolutional/ # for different layers https://keras.io/layers/core/ def cnn_model(): model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=(3, IMG_SIZE, IMG_SIZE), activation='relu')) model.add(Conv2D(32, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.2)) model.add(Conv2D(64, (3, 3), padding='same', activation='relu')) model.add(Conv2D(64, (3, 3), activation='relu'))
if load_val: val_han_word_seq = pickle.load(open(Config.cache_dir + "/g_val_han_word_seq_%s.pkl"%(Config.sentence_num*Config.sentence_word_length), "rb")) else: val_han_word_seq = word_han_preprocess(val.content.values) gc.collect() pickle.dump(val_han_word_seq, open(Config.cache_dir + '/g_val_han_word_seq_%s.pkl'%(Config.sentence_num*Config.sentence_word_length), "wb")) print(np.shape(val_han_word_seq)) print("Load Word") word_embed_weight = pickle.load(open(Config.word_embed_path, "rb")) print("Load ok") model = get_han(Config.sentence_num, Config.sentence_word_length, word_embed_weight) from keras.utils.vis_utils import plot_model plot_model(model, to_file= model_name+ '.png',show_shapes=True) for i in range(12): if i == 5: K.set_value(model.optimizer.lr, 0.0001) if i == 6: for l in trainable_layer: model.get_layer(l).trainable = True model.fit_generator( train_batch_generator(train.content.values, train.label.values, batch_size=batch_size), epochs=1, steps_per_epoch= int(train.shape[0]/batch_size), validation_data=(val_han_word_seq, val_label) ) pred = np.squeeze(model.predict(val_han_word_seq))
model = Sequential() model.add(LSTM(6,input_shape=(1,window_size))) # model.add(LSTM(6,input_shape=(1,window_size))) model.add(Dense(1)) # print_weights = LambdaCallback(on_epoch_end=lambda batch, logs: print(model.layers[0].get_weights())) model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mape', 'accuracy']) # history = model.fit(train_X,train_Y,validation_data=(test_X, test_Y),epochs=250,batch_size=1,callbacks = [print_weights],verbose=2) history = model.fit(train_X,train_Y,validation_data=(test_X, test_Y),epochs=250,batch_size=1,verbose=2) return model,history # on_epoch_end=lambda batch, logs: print (model.layers[1].get_weights()) #fit the model model1, history = fit_model(train_X, train_Y, window_size) plot_model(model1, to_file='LSTM_Latur_plot_1.png', show_shapes=True, show_layer_names=True) model1.summary() train_Y.shape test_Y.shape def predict_and_score(model,X,Y): #Make predictions on the original scale of data pred = scaler.inverse_transform(model.predict(X)) #Prepare Y also to be in original data scale orig_data = scaler.inverse_transform(Y) # print(orig_data) # print("-----") # print(pred[:,0]) #Calculate RMSE score = math.sqrt(mean_squared_error(orig_data, pred[:, 0])) return (score,pred)
import img_utils import os os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/' if __name__ == "__main__": path = r"headline_carspeed.jpg" val_path = "val_images/" scale = 2 """ Plot the models """ model = models.ResNetSR(scale).create_model() plot_model(model, to_file="architectures/ResNet.png", show_layer_names=True, show_shapes=True) """ Train Res Net SR """ rnsr = models.ResNetSR(scale) rnsr.create_model(None, None, 3, load_weights=True) rnsr.evaluate(val_path) """ Evaluate ResNetSR on Set5/14 """ rnsr = models.ResNetSR(scale) rnsr.create_model(None, None, 3, load_weights=True)
import numpy as np from keras.models import Sequential from keras.layers import Dense, Activation # from keras.utils.visualize_util import plot from keras.utils.vis_utils import plot_model model = Sequential() model.add(Dense(32, input_dim=500)) model.add(Activation(activation='sigmoid')) model.add(Dense(1)) model.add(Activation(activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) data = np.random.random((1000, 500)) labels = np.random.randint(2, size=(1000, 1)) score = model.evaluate(data,labels, verbose=0) print("Before Training:", list(zip(model.metrics_names, score)) ) model.fit(data, labels, nb_epoch=10, batch_size=32, verbose=0) score = model.evaluate(data,labels, verbose=0) print("After Training:", list(zip(model.metrics_names, score)) ) plot_model(model, to_file='s2.png', show_shapes=True)
return [best_w_loss, best_w_jaccard, last_w, logger] ############################################ Compile ################################################# if last_activation != 'sigmoid' and last_activation != 'softmax': raise ValueError("Incorrect last activation :" + last_activation) model = get_model(None, classes_num, last_activation, layers_in_block=layers_in_block) plot_model(model=model, to_file=callbacks_full_dir + model_name + ".png", show_shapes=True, show_layer_names=False, dpi=200) model.summary() with open(callbacks_full_dir + "param_count.txt", "w") as f: f.write(str(model.count_params())) if is_load: if not weight_path: raise ValueError("Don't load weight_path") model.load_weights(weight_path) if loss_function == 'categorical_crossentropy': pass elif loss_function == 'dice_loss':
parser.add_argument('--num_routing', default=3, type=int) # num_routing should > 0 parser.add_argument('--shift_fraction', default=0.1, type=float) parser.add_argument('--debug', default=0, type=int) # debug>0 will save weights by TensorBoard parser.add_argument('--save_dir', default='./result') parser.add_argument('--is_training', default=1, type=int) parser.add_argument('--weights', default=None) args = parser.parse_args() print(args) if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # load data (x_train, y_train), (x_test, y_test) = load_mnist() # define model model = CapsNet(input_shape=[28, 28, 1], n_class=len(np.unique(np.argmax(y_train, 1))), num_routing=args.num_routing) model.summary() plot_model(model, to_file=args.save_dir+'/model.png', show_shapes=True) # train or test if args.weights is not None: # init the model weights with provided one model.load_weights(args.weights) if args.is_training: train(model=model, data=((x_train, y_train), (x_test, y_test)), args=args) else: # as long as weights are given, will run testing if args.weights is None: print('No weights are provided. Will test using random initialized weights.') test(model=model, data=(x_test, y_test))
""" Plot the normalized average confusion matrix """ cm = np.array([[np.mean(f_TPs), np.mean(f_FNs)], [np.mean(f_FPs), np.mean(f_TNs)]]) cmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.subplots(figsize=(10, 10)) target_names = ["Viitesuhtes", "Viitesuhteta"] sns.set(font_scale=1.8) sns.heatmap(cmn, annot=True, fmt='.2f', xticklabels=target_names, yticklabels=target_names) plt.ylabel('Tegelik', fontsize=20) plt.xlabel('Ennustatud', fontsize=20) plt.savefig(fName) plotConfusionMatrix(f_TPs, f_FNs, f_FPs, f_TNs, TEST_NAME + '_confused') #MODEL SUMMARY IN A SEPERATE PICTURE plot_model(seq, to_file=TEST_NAME + '.png', show_shapes=True, show_layer_names=True)
if __name__ == '__main__': # Path to csv-file. File should contain X-ray filenames as first column, # mask filenames as second column. csv_path = '/path/to/dataset/idx-train.csv' # Path to the folder with images. Images will be read from path + path_from_csv path = csv_path[:csv_path.rfind('/')] + '/' df = pd.read_csv(csv_path) # Shuffle rows in dataframe. Random state is set for reproducibility. df = df.sample(frac=1, random_state=23) # Load training data append_coords = True X, y = loadDataGeneral(df, path, append_coords) # Build model inp_shape = X[0].shape model = build_model(inp_shape) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # Visualize model plot_model(model, 'model.png', show_shapes=True) model.summary() ########################################################################################## checkpointer = ModelCheckpoint('model.{epoch:03d}.hdf5', period=5) model.fit(X, y, batch_size=1, epochs=50, callbacks=[checkpointer], validation_split=0.2)
model.add(Dense(128, activation='relu')) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) if num_classes == 2: model.add(Dense(1, activation='sigmoid')) else: model.add(Dense(num_classes, activation='sigmoid')) # model.add(Activation('tanh')) # # Print model summary # # ================================================== print(model.summary()) # # #Visualize the model in a graph plot_model(model, to_file='E:\\model_plot.png', show_shapes=True, show_layer_names=True) # model compilation # ================================================== if num_classes == 2: model.compile(loss='binary_crossentropy', optimizer='Adagrad', metrics=['accuracy']) else: model.compile(loss='categorical_crossentropy', optimizer='Adagrad', metrics=['accuracy']) # model.compile(loss='mean_absolute_percentage_error', optimizer='adam', metrics=['accuracy']) # model early_stopping and checkpointer
choi_acc=1.0*choiOK/sent_i sampNG=sent_i - sampOK rankNG=sent_i - rankOK choiNG=sent_i - choiOK samp_result='samp: '+str(samp_acc)+' ( OK: '+str(sampOK)+' NG: '+str(sampNG)+' )\n' rank_result='rank: '+str(rank_acc)+' ( OK: '+str(rankOK)+' NG: '+str(rankNG)+' )\n' choi_result='choi: '+str(choi_acc)+' ( OK: '+str(choiOK)+' NG: '+str(choiNG)+' )\n' result=samp_result+rank_result+choi_result end_time=datetime.datetime.today() diff_time=end_time-start_time with open(today_str+'result.txt', 'a') as rslt: rslt.write(result+'\ntotal time ='+str(diff_time)) print(result) print('all_end = ',end_time) plot_loss(list_loss, list_val_loss) plot_model(my_model, to_file=today_str+'model.png', show_shapes=True) print('total =',diff_time)
from keras.layers import * from keras.optimizers import * from keras.utils.vis_utils import plot_model def attention_model(timesteps=101, dim=512, unit=128, n_class=5): inputs = Input((timesteps, dim),name='input_data') # the weights f of timesteps (timesteps * n_class) f_weights = Input((timesteps, n_class),name='f_weights') x = BatchNormalization()(inputs) x = LSTM(unit, return_sequences=True)(x) x = Reshape((timesteps, unit))(x) # multiply weights f_weights to the result from LSTM x = Lambda(lambda x: tf.matmul(x[0], x[1], transpose_a=True),name='fweights_mul')([f_weights, x]) # final multi-softmax x = Lambda(lambda x: tf.unstack(x, axis=1),name='unstack1')(x) # len(x)=timestaps xs = [] # each class with different weights w^n for x_i in x: xs.append(Dense(1)(x_i)) x = Lambda(lambda x: tf.stack(x, axis=1),name='stack1')(xs) x = Reshape((n_class,))(x) x = Activation('softmax')(x) return Model(inputs=[inputs, f_weights], outputs=x) model=attention_model() plot_model(model,to_file='attention_model.png',show_shapes=False)