def join_models(GE_dataset, CNV_dataset, MUT_dataset): shape_GE = GE_dataset.shape[1] shape_CNV = CNV_dataset.shape[1] shape_MUT = MUT_dataset.shape[1] Inputs_1 = Input(shape=[shape_GE], name='Inputs_1') x = Dense(384, activation='sigmoid', name='Dense_1_0')(Inputs_1) x = Dropout(0.2, name='Dropout_1_0')(x) x = Dense(512, activation='relu', name='Dense_1_1')(x) Outputs_1 = Dense(101, activation='linear', name='Outputs_1')(x) Inputs_2 = Input(shape=[shape_CNV], name='Inputs_2') y = Dense(256, activation='sigmoid', name='Dense_2_0')(Inputs_2) y = Dropout(0.5, name='Dropout_2_0')(y) y = Dense(256, activation='relu', name='Dense_2_1')(y) Outputs_2 = Dense(101, activation='sigmoid', name='Outputs_2')(y) Inputs_3 = Input(shape=[shape_MUT], name='Inputs_3') z = Dense(384, activation='relu', name='Dense_3_0')(Inputs_3) z = Dropout(0.4, name='Dropout_3_0')(z) z = Dense(512, activation='relu', name='Dense_3_1')(z) Outputs_3 = Dense(101, activation='linear', name='Outputs_3')(z) Concatenated = concatenate([Outputs_1, Outputs_2, Outputs_3], name='Concatenated') a = Dense(64, activation='relu', name='Dense_4_0')(Concatenated) a = Dense(64, activation='relu', name='Dense_4_1')(a) Main_output = Dense(101, activation='linear', name='Main_output')(a) model = Model(inputs=[Inputs_1, Inputs_2, Inputs_3], outputs=Main_output) model.compile(optimizer='RMSprop', loss='mean_squared_error',metrics=['mean_squared_error']) plot_model(model, show_shapes=True, to_file='join_models.png') return(model)
def build_model(self): inputs = Input((self.patch_height, self.patch_width, 1)) conv1 = self.encoding_block(32, strides=(3, 3), padding='same')(inputs) conv1 = self.se_block(ratio=2)(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = self.encoding_block(64, strides=(3, 3), padding='same')(pool1) conv2 = self.se_block(ratio=2)(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = self.encoding_block(128, strides=(3, 3), padding='same')(pool2) conv3 = self.se_block(ratio=2)(conv3) up = UpSampling2D(size=(2, 2))(conv3) conv4 = decoding_block(filters, strides=(3, 3), padding='same')(up, conv2) conv4 = self.se_block(ratio=2)(conv4) up1 = UpSampling2D(size=(2, 2))(conv4) conv5 = decoding_block(filters, strides=(3, 3), padding='same')(up1, conv1) conv5 = self.se_block(ratio=2)(conv5) conv6 = Conv2D(self.num_seg_class + 1, (1, 1), padding='same')(conv5) conv6 = LeakyReLU(alpha=0.3)(conv6) conv6 = core.Reshape((self.patch_height * self.patch_width, self.num_seg_class + 1))(conv6) act = Activation('softmax')(conv6) model = Model(inputs=inputs, outputs=act) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy']) plot_model(model, to_file=os.path.join(self.config.checkpoint, "model.png"), show_shapes=True) self.model = model
def build_model(): """ build CNN-RNN model :return: """ input_shape = (config.resize[0], config.resize[1], config.channel) inputs = Input(shape=input_shape) # CNN layers x = CNN5(inputs, config.l2) if config.cnn_type == 'CNN5' else ResNet50(inputs) conv_shape = x.get_shape() x = Reshape(target_shape=(int(conv_shape[1]), int(conv_shape[2] * conv_shape[3])))(x) # concat Bi-RNN layers to encode and decode sequence x = BiLSTM(x, units=config.rnn_units, use_gpu=config.use_gpu) if config.rnn_type == 'BiLSTM' \ else BiGRU(x, units=config.rnn_units, use_gpu=config.use_gpu) predictions = TimeDistributed(Dense(config.n_class, kernel_initializer='he_normal', activation='softmax'))(x) base_model = Model(inputs=inputs, outputs=predictions) # CTC_loss labels = Input(name='the_labels', shape=[config.max_seq_len, ], dtype='float32') input_length = Input(name='input_length', shape=[1, ], dtype='int64') label_length = Input(name='label_length', shape=[1, ], dtype='int64') loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')( [predictions, labels, input_length, label_length]) model = Model(inputs=[inputs, labels, input_length, label_length], outputs=[loss_out]) model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=Adam(lr=config.lr)) if not os.path.exists('./plotModel'): os.makedirs('./plotModel') plot_model(model, './plotModel/{}-{}_model.png'.format(config.cnn_type, config.rnn_type), show_shapes=True) plot_model(base_model, './plotModel/{}-{}_base_model.png'.format(config.cnn_type, config.rnn_type), show_shapes=True) return model, base_model, int(conv_shape[1])
def plot_model(self): if self._model is not None: plot_model(self._model, to_file='{}.png'.format(self._model_name), show_shapes=True) model_plot = misc.imread('{}.png'.format(self._model_name)) plt.figure(figsize=(15, 15)) plt.imshow(model_plot) plt.show()
def print_model(): """ Build model and print its plot to file :return:void """ model = get_model() plot_model(model, to_file='results/model_plot.png', show_shapes=True, show_layer_names=False)
def build(self): # Embedding-layer to transform input into 3D-space. input_embedding = Embedding(self.data_sequence.vocab_size(), self.embedding_dim) # Inputs encoder_inputs = Input(shape=(None,)) encoder_inputs_emb = input_embedding(encoder_inputs) # Encoder LSTM encoder = LSTM(self.lstm_hidden_dim, return_state=True) encoder_outputs, state_h, state_c = encoder(encoder_inputs_emb) state = [state_h, state_c] # state will be used to initialize the decoder # Start vars (emulates a constant input) def constant(input_batch, size): batch_size = K.shape(input_batch)[0] return K.tile(K.ones((1, size)), (batch_size, 1)) decoder_in = Lambda(constant, arguments={'size': self.embedding_dim})(encoder_inputs_emb) # "start word" # Definition of further layers to be used in the model (decoder and mapping to vocab-sized vector) decoder_lstm = LSTM(self.lstm_hidden_dim, return_sequences=False, return_state=True) decoder_dense = Dense(self.data_sequence.vocab_size(), activation='softmax') chars = [] # Container for single results during the loop for i in range(self.max_decoder_length): # Reshape necessary to match LSTMs interface, cell state will be reintroduced in the next iteration decoder_in = Reshape((1, self.embedding_dim))(decoder_in) decoder_in, hidden_state, cell_state = decoder_lstm(decoder_in, initial_state=state) state = [hidden_state, cell_state] # Mapping decoder_out = decoder_dense(decoder_in) # Reshaping and storing for later concatenation char = Reshape((1, self.data_sequence.vocab_size()))(decoder_out) chars.append(char) # Teacher forcing. During training the original input will be used as input to the decoder decoder_in_train = Lambda(lambda x, ii: x[:, -ii], arguments={'ii': i+1})(encoder_inputs_emb) decoder_in = Lambda(lambda x, y: K.in_train_phase(y, x), arguments={'y': decoder_in_train})(decoder_in) # Single results are joined together (axis 1 vanishes) decoded_seq = Concatenate(axis=1)(chars) self.model = Model(encoder_inputs, decoded_seq, name="enc_dec") self.model.compile(optimizer='adam', loss='categorical_crossentropy') self.model.summary() try: file_name = 'enc_dec_model' plot_model(self.model, to_file=f'{file_name}.png', show_shapes=True) print(f"Model built. Saved {file_name}.png\n") except (ImportError, FileNotFoundError): print(f"Skipping plotting of model due to missing dependencies.")
def build_model(self): inputs = Input((self.patch_height, self.patch_width, 1)) conv1 = Conv2D(32, (1, 1), activation=None, padding='same')(inputs) conv1 = normalization.BatchNormalization(epsilon=2e-05, axis=3, momentum=0.9, weights=None, beta_initializer='zero', gamma_initializer='one')(conv1) conv1 = Activation('relu')(conv1) conv1 = self.DenseBlock(conv1, 32) # 48 conv1 = self.se_block(ratio=2)(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = self.DenseBlock(pool1, 64) # 24 conv2 = self.se_block(ratio=2)(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = self.DenseBlock(pool2, 64) # 12 conv3 = self.se_block(ratio=2)(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = self.DenseBlock(pool3, 64) # 12 conv4 = self.se_block(ratio=2)(conv4) up1 = Conv2DTranspose(64, (3, 3), strides=2, activation='relu', padding='same', kernel_regularizer=regularizers.l2(0.0001))(conv4) up1 = concatenate([up1, conv3], axis=3) conv5 = self.DenseBlock(up1, 64) conv5 = self.se_block(ratio=2)(conv5) up2 = Conv2DTranspose(64, (3, 3), strides=2, activation='relu', padding='same', kernel_regularizer=regularizers.l2(0.0001))(conv5) up2 = concatenate([up2, conv2], axis=3) conv6 = self.DenseBlock(up2, 64) conv6 = self.se_block(ratio=2)(conv6) up3 = Conv2DTranspose(32, (3, 3), strides=2, activation='relu', padding='same', kernel_regularizer=regularizers.l2(0.0001))(conv6) up3 = concatenate([up3, conv1], axis=3) conv7 = self.DenseBlock(up3, 32) conv7 = self.se_block(ratio=2)(conv7) conv8 = Conv2D(self.num_seg_class + 1, (1, 1), activation='relu', padding='same', kernel_regularizer=regularizers.l2(0.0001))(conv7) # conv6 = normalization.BatchNormalization(epsilon=1e-06, mode=1, axis=-1, momentum=0.9, weights=None, beta_init='zero', gamma_init='one')(conv6) # for tensorflow conv8 = core.Reshape((self.patch_height * self.patch_width, self.num_seg_class + 1))(conv8) # for theano # conv8 = core.Reshape(((self.num_seg_class + 1), self.patch_height * self.patch_width))(conv8) # conv8 = core.Permute((2, 1))(conv8) ############ act = Activation('softmax')(conv8) model = Model(inputs=inputs, outputs=act) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy']) plot_model(model, to_file=os.path.join(self.config.checkpoint, "model.png"), show_shapes=True) self.model = model
def plt_keras_model(self, model, **kwargs): desc = get_varargin(kwargs, 'description', '') img_size = get_varargin(kwargs, 'figsize', (500,800)) kerasUtils.plot_model(model, to_file = 'model.png', show_shapes = True, dpi = 120, expand_nested = True) encoded = base64.b64encode(open('model.png', 'rb').read()).decode('utf-8') img_tag = desc + "<br/><br/>" + "<img src=\'data:image/png;base64,{}\' style = 'width:{}px;height:{}px'>"\ .format(encoded,img_size[0],img_size[1]) os.remove('model.png') self.filelogger.info(img_tag)
def fitness(self): print() print("Hyper-parameters:") """ Hyper-parameters: learning_rate: Learning-rate for the optimizer. num_dense_layers: Number of dense layers. num_dense_nodes: Number of nodes in each dense layer. activation: Activation function for all layers. """ # Print the hyper-parameters. print() print('learning rate: {0:.1e}'.format(self.learning_rate)) print('num_dense_layers:', self.num_dense_layers) print('num_dense_nodes:', self.num_dense_nodes) print('activation:', self.activation) print() # Create the neural network with these hyper-parameters. self.model = self.create_model() # Save an image containing info about the model plot_model( self.model, to_file= 'C:/Users/mathe/Desktop/CNN-GAN/CNN/MNIST_KERAS/assets/model.png') # Instantiate TensorBoard class, which # will give us access for all what is # happening with our model self.callback_log = TensorBoard( log_dir=self.tensorboard.log_dir, histogram_freq=1, batch_size=32, write_graph=True, write_images=True, embeddings_metadata=self.tensorboard.metadata) # Use Keras to train the model. print("\n\n") print('Training...') print("\n\n") self.history = self.model.fit( x=self.data.x, y=self.data.y, epochs=self.params.training_hparams.epochs, batch_size=self.params.training_hparams.batch_size, validation_data=self.data.validation_data, callbacks=[self.callback_log]) return self.history, self.model
def gimage(self): if not self.IS_GIMAGE: return if os.path.exists(f'{self.H5_NAME}_model.png'): return from tensorflow.python.keras.utils import plot_model plot_model(self.MODEL.model, to_file=f'{self.H5_NAME}_model.png', show_shapes=True) self._Log(f'{self.H5_NAME}_model.png', _T='Successfully save model image:')
def check_model(model, model_name, x, y): # # 设定格式化模型名称,以时间戳作为标记 # model_name = "test" # tensorboard = TensorBoard(log_dir='logs/{}'.format(model_name)) model.compile('adam', 'binary_crossentropy',metrics=['binary_crossentropy']) # model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5, callbacks=[tensorboard]) model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5) print(model_name + " test train valid pass!") print(model_name + " test pass!") start = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') plot_model(model, to_file=model_name + '_' + start + '.png')
def __init__(self, filename=None) -> None: super().__init__() self._train_acc_threshold = 0.9 self._validate_acc_threshold = 0.9 if filename and os.path.exists(filename): logging.info("Loading model from: %s", filename) self._model = models.load_model(filename) else: logging.info("Starting with clean model") self._model = self._get_nn() self._model.summary(print_fn=logging.info) utils.plot_model(self._model, to_file=os.path.join(os.path.dirname(__file__), '..', 'model.png'), show_shapes=True)
def __init__(self, use_cudnn_lstm=True, plot_model_architecture=False): n_hidden = 50 input_dim = 300 # unit_forget_bias: Boolean. If True, add 1 to the bias of the forget gate at initialization. Setting it to true will also force bias_initializer="zeros". This is recommended in Jozefowicz et al. # he_normal: Gaussian initialization scaled by fan_in (He et al., 2014) if use_cudnn_lstm: # Use CuDNNLSTM instead of LSTM, because it is faster lstm = layers.CuDNNLSTM(n_hidden, unit_forget_bias=True, kernel_initializer='he_normal', kernel_regularizer='l2', name='lstm_layer') else: lstm = layers.LSTM(n_hidden, unit_forget_bias=True, kernel_initializer='he_normal', kernel_regularizer='l2', name='lstm_layer') # Building the left branch of the model: inputs are variable-length sequences of vectors of size 128. left_input = Input(shape=(None, input_dim), name='input_1') # left_masked_input = layers.Masking(mask_value=0)(left_input) left_output = lstm(left_input) # Building the right branch of the model: when you call an existing layer instance, you reuse its weights. right_input = Input(shape=(None, input_dim), name='input_2') # right_masked_input = layers.Masking(mask_value=0)(right_input) right_output = lstm(right_input) # Builds the classifier on top l1_norm = lambda x: 1 - K.abs(x[0] - x[1]) merged = layers.Lambda(function=l1_norm, output_shape=lambda x: x[0], name='L1_distance')([left_output, right_output]) predictions = layers.Dense(1, activation='tanh', name='Similarity_layer')(merged) #sigmoid # Instantiating and training the model: when you train such a model, the weights of the LSTM layer are updated based on both inputs. self.model = Model([left_input, right_input], predictions) self.__compile() print(self.model.summary()) if plot_model_architecture: from tensorflow.python.keras.utils import plot_model plot_model(self.model, to_file='siamese_architecture.png')
def mlp(): # Load images data X_train, X_test, y_train, y_test = utils.prepare_data() model = get_model(num_classes=10) # Get model architecture summary model.summary() plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) model.fit(X_train, y_train, batch_size=100, epochs=3) model.evaluate(X_test, y_test)
def plot_(model_path, file_path): """Visualize a model Parameters ---------- model_path : str Path to the model.h5 file_path : str Destination file to save i.e. model.png """ model = load_model(model_path) plot_model(model, file_path, show_shapes=True, show_layer_names=False)
def get_dense_ae(self): encoded_input = Input(shape=(self.img_height, self.img_height, self.num_channels)) encoded_flat = Flatten()(encoded_input) encoded_output = Dense(units=self.code_size)(encoded_flat) decoded_input = Input(shape=(self.code_size,)) decoded_flat = Dense(units=np.prod((self.img_height, self.img_height, self.num_channels)))(decoded_input) decoded_output = Reshape(target_shape=(self.img_height, self.img_height, self.num_channels))(decoded_flat) encoder = Model(encoded_input, encoded_output, name="encoder") plot_model(encoder, to_file=self.model_dir + '/encoder.png', show_shapes=True) decoder = Model(decoded_input, decoded_output, name="decoder") plot_model(decoder, to_file=self.model_dir + '/decoder.png', show_shapes=True) model = Model(encoded_input, decoder(encoder(encoded_input)), name="autoencoder") return model
def build_DNN(optimizer='RMSprop', activation='relu', dropout_rate=0.0, neurons1=128, neurons2=128): #Create layers Inputs = Input(shape=[637], name='Inputs') x = Dense(neurons1, activation=activation)(Inputs) x = Dropout(dropout_rate)(x) x = Dense(neurons2, activation='relu')(x) Outputs = Dense(101, activation='linear', name='outputs')(x) #Compile model model = Model(inputs=Inputs, outputs=Outputs) model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['mean_squared_error', 'mean_absolute_error']) plot_model(model, show_shapes=True, to_file='model.png') return model
def build(self): self.model = Sequential() self.model.add( Embedding(self.data_sequence.vocab_size(), self.embedding_size)) self.model.add(LSTM(self.hidden_state_size)) self.model.add(Dense(1, activation='sigmoid')) self.model.compile('adam', loss='binary_crossentropy', metrics=['acc']) self.model.summary() try: file_name = 'model' plot_model(self.model, to_file=f'{file_name}.png', show_shapes=True) print(f"Model built. Saved {file_name}.png\n") except (ImportError, FileNotFoundError, OSError): print(f"Skipping plotting of model due to missing dependencies.")
def get_custom_model(classes=2): def preprocess_input(img): img = img / 255. return img.astype(np.float32) def decode_img(img): img = img * 255. return img.astype(np.uint8) model = Sequential() model.add(Conv2D(32, (3, 3), input_shape=(224, 224, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Flatten()) # 用全局平均池化代替flatten减少参数,避免过拟合,提高正确率 model.add(GlobalAveragePooling2D()) model.add(Dense(64, activation='relu')) # model.add(Dropout(0.5)) model.add(Dense(classes, activation='softmax')) model.summary() ckpt = './ckpt/custom.h5' checkpoint = ModelCheckpoint(filepath=ckpt) tensorboard = './log/custom' tensorboard = TensorBoard(log_dir=tensorboard) if os.path.exists(ckpt): model.load_weights(ckpt, by_name=True) print("load done") else: plot_model(model, to_file='custom.png') sgd = SGD(lr=0.000001, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy']) return model, checkpoint, tensorboard, preprocess_input, decode_img
def create_model(categories, categories_index, word2vec): # First dimension of inputs is variable (None) because length of # sentences can vary. ctags_vec = Input(shape=(None, len(categories_index)), dtype='float32', name='ctags_vec') word_vec = Input(shape=(None, word2vec.vector_size), dtype='float32', name='word_vec') all_inputs = concatenate([ctags_vec, word_vec]) masked_inputs = Masking(mask_value=0., )(all_inputs) biLSTM = Bidirectional( LSTM(HIDDEN_LAYER_DIMENSION, return_sequences=True, dropout=0.5))(masked_inputs) for _ in range(HIDDEN_LAYERS - 1): biLSTM = Bidirectional( LSTM(HIDDEN_LAYER_DIMENSION, return_sequences=True, dropout=0.5))(biLSTM) predicted_tag_categories = [ Dense(len(category.values), activation='softmax', name=f'{category.name}')(biLSTM) for category in categories ] model = Model(inputs=[ctags_vec, word_vec], outputs=predicted_tag_categories) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # TODO Should we remove it? Or enable with some debug flag? plot_model(model, to_file='model.png') return model
def get_densenet121_model(classes=2): def preprocess_input(img): img[:, :, 0] = (img[:, :, 0] - 103.94) * 0.017 img[:, :, 1] = (img[:, :, 1] - 116.78) * 0.017 img[:, :, 2] = (img[:, :, 2] - 123.68) * 0.017 return img.astype(np.float32) def decode_img(img): img[:, :, 0] = (img[:, :, 0] / 0.017) + 103.94 img[:, :, 1] = (img[:, :, 1] / 0.017) + 116.78 img[:, :, 2] = (img[:, :, 2] / 0.017) + 123.68 return img.astype(np.uint8) base_model = tf.keras.applications.DenseNet121(include_top=False, classes=2) x = base_model.output x = GlobalAveragePooling2D()(x) pre = Dense(classes, activation='softmax', name='fc1000')(x) model = Model(inputs=base_model.input, outputs=pre) model.summary() for layer in base_model.layers: layer.trainable = False ckpt = './ckpt/densenet121.h5' checkpoint = ModelCheckpoint(filepath=ckpt) tensorboard = './log/densenet121' tensorboard = TensorBoard(log_dir=tensorboard) if os.path.exists(ckpt): model.load_weights(ckpt, by_name=True) print("load done") else: plot_model(model, to_file='densenet121.png') model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='binary_crossentropy', metrics=['accuracy']) return model, checkpoint, tensorboard, preprocess_input, decode_img
def build_model(max_seq_length): input_ids = Input(shape=(max_seq_length, ), name="input_ids") # token embedding input_mask = Input(shape=(max_seq_length, ), name="input_mask") # masking segment_ids = Input(shape=(max_seq_length, ), name="segment_ids") # segment embedding in_bert = [input_ids, input_mask, segment_ids] bert_output = BertLayer(n_fine_tune_layers=3, bert_path=bert_path, output_type='mean_pooling', trainable=True)(in_bert) dense = Dense(256, activation="relu")(bert_output) pred = Dense(1, activation="sigmoid")(dense) model = Model(inputs=in_bert, outputs=pred) model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]) model.summary() plot_model(model, "./bert_structure.png", show_shapes=True) return model
def get_mobilev2_model(classes=2): def preprocess_input(img): img = img / 128. img = img - 1. return img.astype(np.float32) def decode_img(img): img = img + 1. img = img * 128. return img.astype(np.uint8) base_model = MobileNetV2(include_top=False, input_shape=(224, 224, 3)) x = base_model.output x = GlobalAveragePooling2D()(x) pre = Dense(classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=pre) model.summary() # 冻结这些层就无法训练 # 迁移学习,用训练好的权重,重写全连接层再进行训练 for layer in base_model.layers: layer.trainable = False ckpt = './ckpt/mobilev2.h5' checkpoint = ModelCheckpoint(filepath=ckpt) tensorboard = './log/mobilev2' tensorboard = TensorBoard(log_dir=tensorboard) if os.path.exists(ckpt): model.load_weights(ckpt) print('load done') else: plot_model(model, to_file='mobilev2.png') sgd = SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy']) return model, checkpoint, tensorboard, preprocess_input, decode_img
model = FCN32(11, 320, 320) model.load_weights("model.h5") skip1 = Conv2DTranspose(512, kernel_size=(3, 3), strides=(2, 2), padding='same', kernel_initializer="he_normal", name="upsampling6")(model.get_layer("fc7").output) summed = add(inputs=[skip1, model.get_layer("block4_pool").output]) up7 = UpSampling2D(size=(16, 16), interpolation='bilinear', name='upsamping_7')(summed) o = Conv2D(nClasses, kernel_size=(3, 3), activation='relu', padding='same', name='conv_7')(up7) o = Reshape((-1, nClasses))(o) o = Activation("softmax")(o) fcn16 = Model(model.input, o) return fcn16 if __name__ == "__main__": model = FCN16(15, 320, 320) model.summary() plot_model(model, show_shapes=True, to_file='fcn16.png') # plot_model(model,show_shapes=True,to_file='fcn32.png')
def display_model(model): plot_model(model, to_file='model.png', show_shapes=True) return Image('model.png')
def conv_network(data, labels, output_labels, dataset_name, model_name, epochs, batch_size, label_norm='sum', export_datatype=None, export_acquisitions=None, dataset_normalisation=None, save_directory=SAVE_ROOT + 'models/'): global SAVE_ROOT _MODEL_NAME = datetime.datetime.now().strftime('%d%m%y_%H:%M:%S') + '_' _MODEL_NAME += model_name + '_' _MODEL_NAME += 'e_' + str(epochs) + '_' _MODEL_NAME += 'b_' + str(batch_size) + '_' _MODEL_NAME += 'n_' + str(len(data)) + '_' data, input_shape = reshape_data(data) _MODEL_NAME += 'shp_' + str(input_shape).replace('(', '').replace( ')', '').replace(', ', '_') models = [] n_classes = len( labels[0]) # don't remove me - used in the following eval statements # Been disabled as XLA_GPUs can appear multiple times in the num_gpus() method. # if num_gpus() > 1: # # multi gpu support: https://keras.io/getting-started/faq/#how-can-i-run-a-keras-model-on-multiple-gpus # import tensorflow as tf # with tf.device('/cpu:0'): # models.append(eval(model_name + '(input_shape, n_classes)')) # models.append(keras.utils.multi_gpu_model(models[0], gpus=num_gpus())) # print('Model split over ' + str(num_gpus()) + ' GPUs') # elif num_gpus() == 1: # with tf.device('/gpu:0'): # models.append(eval(model_name + '(input_shape, n_classes)')) # else: # with tf.device('/cpu:0'): # models.append(eval(model_name + '(input_shape, n_classes)')) models.append(eval(model_name + '(input_shape, n_classes)')) if label_norm == 'sum': if models[0].layers[-1].activation.func_name != 'softmax': raise Exception( 'When using "sum" label norm, please use softmax activation for final layer.' ) elif label_norm == 'max': if models[0].layers[-1].activation.func_name != 'sigmoid': raise Exception( 'When using "max" label norm, please use sigmoid activation for final layer.' ) for sub_directory in ['complete', 'incomplete']: if not os.path.exists(os.path.join(save_directory, sub_directory)): os.makedirs(os.path.join(save_directory, sub_directory)) save_path = save_directory + 'incomplete/' + _MODEL_NAME + '/' if os.path.isdir(save_path): raise Exception('There is already a model with this name') else: os.makedirs(save_path) plot_model(models[0], to_file=save_path + '/network_structure.png', show_shapes=True, show_layer_names=True) plot_label_distribution(labels, save_path, output_labels) optimiser = keras.optimizers.Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, amsgrad=False) for model in models: model.compile(loss='mse', optimizer=optimiser, metrics=['acc', 'mse', 'mae']) model._MODEL_NAME = _MODEL_NAME model.output_labels = output_labels model.output_normalisation = dataset_normalisation model_metadata = { '_MODEL_NAME': _MODEL_NAME, 'output_labels': output_labels, 'output_normalisation': label_norm, 'dataset_name': dataset_name, 'export_datatype': export_datatype, 'export_acquisitions': export_acquisitions } models[-1].summary() callbacks = [ keras.callbacks.EarlyStopping(monitor='loss', min_delta=1e-12, patience=15, verbose=1, restore_best_weights=True) ] history = models[-1].fit(x=data, y=labels, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=True, callbacks=callbacks) save_network(save_path, models[0], model_metadata) score = models[-1].evaluate(data, labels, verbose=1) print('Test loss:', score[0]) print('Test accuracy:', score[1]) history = save_history(dataset_name, save_path, history.history, prefix='training_history', last_iteration=True) plot_history(history, save_path, prefix='train_') # alter the save path to be more descriptive old_save_path = save_path save_path = save_path.rstrip('/') # include the final later activaiton for reference save_path += '_actvn_' + models[-1].layers[-1].activation.func_name # and the dataset label norm technique if dataset_normalisation: save_path += '_' + dataset_normalisation # and the final error save_path += '_ac_' + str(np.round(score[1], 2)) # and the final error save_path += '_l_' + str(np.round(score[0], 4)) # mark the model complete, and then move it! save_path = save_path.replace('/incomplete/', '/complete/') os.rename(old_save_path, save_path) return models[-1], save_path
"Pclass": np.array(xtest["Pclass"]), "Sex": np.array(xtest["Sex"]), "Cabin": np.array(xtest["Cabin"]), "Embarked": np.array(xtest["Embarked"]), "Age": np.array(xtest["Age"]), "SibSp": np.array(xtest["SibSp"]), "Parch": np.array(xtest["Parch"]), "Fare": np.array(xtest["Fare"]) } deepfm_model = deepfm(sparse_feature_list, sparse_feature_reindex_dict, dense_feature_list) print(deepfm_model.summary()) plot_model(deepfm_model, to_file='deepfm_model.png') deepfm_model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy']) history = deepfm_model.fit(xtrain_data, ytrain, epochs=1000, batch_size=32, validation_data=(xtest_data, ytest)) import matplotlib.pyplot as plt loss = history.history['loss'] val_loss = history.history['val_loss']
model.add(Flatten()) model.add(Dense(units=512)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dense(units=64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(units=10, activation='softmax')) startTime = time.time() model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) tsb = TensorBoard( log_dir='.\logs\hw2_i', write_graph=True, write_images=True, ) # start study history_model1 = model.fit(x_train, y_train, batch_size=128, epochs=5, validation_split=0.2, callbacks=[tsb]) endTime = time.time() - startTime print(endTime) plot_model(model, to_file='model2_I.png')
batch_size=BATCH_SIZE) full_model = RNA.full_model encoder_model = RNA.encoder_model decoder_model = RNA.decoder_model adam = Adam(lr=LEARNING_RATE, clipnorm=1.0) for model in [full_model, encoder_model, decoder_model]: model.compile(loss='categorical_crossentropy', sample_weight_mode='temporal', optimizer=adam, metrics=[categorical_accuracy]) model.summary() plot_model(full_model, to_file='models/full_arch.png', show_shapes=True) plot_model(encoder_model, to_file='models/encoder_arch.png', show_shapes=True) plot_model(decoder_model, to_file='models/decoder_arch.png', show_shapes=True) ############################################################### """ Train the model """ callbacks = get_callbacks(WEIGHTS_FPATH, LOG_FPATH, "val_categorical_accuracy") try: full_model.fit_generator(
trainlosses = np.array(history1.losses) np.save("./performanceevaluation/trainlosses_" + storename + ".npy", trainlosses) np.save("./performanceevaluation/valiloss_" + storename + ".npy", valiloss) plotlosses(history1.losses, storename) plotvalilosses(valiloss, storename) timeused = timerecord.times timeused = np.array(timeused) np.save("./performanceevaluation/timeused_" + storename + ".npy", timeused) plottimehistory(timeused, storename) plot_model(model, show_shapes=True, to_file='./images/lstm_model_' + storename + '.png') model.save("./performanceevaluation/lstm_model_" + storename + ".h5") #----------------------------------------------------------- plotstart = 8000 plotend = 10000 normalplotline = test_X[plotstart:plotend, :, :] realplotline = test_y[plotstart:plotend] yhat = model.predict(normalplotline) print("yhat: ", yhat.shape) np.save("./plotdata/realplotline.npy", realplotline)