def train(): model = load_model() model.summary() plot_model(model, to_file='model.png') json_string = model.to_json() open('model_architecture.json', 'w').write(json_string) x_train = np.load('final_AAI_Dataset.npz')['trainSet'] y_train = np.load('final_AAI_Dataset.npz')['trainLabel'] # y_train = np.reshape(y_train, (y_train.shape[0], y_train.shape[1])) x_test = np.load('final_AAI_Dataset.npz')['testSet'] y_test = np.load('final_AAI_Dataset.npz')['testLabel'] # y_test = np.reshape(y_test, (y_test.shape[0], y_test.shape[1])) # x_train = (x_train - np.mean(x_train)) / np.std(x_train) # y_train = (y_train - np.mean(y_train)) / np.std(y_train) # x_test = (x_test - np.mean(x_test)) / np.std(x_test) # y_test = (y_test - np.mean(y_test)) / np.std(y_test) print('Training....................') model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy']) history = LossHistory() model.fit(x_train, y_train, batch_size=64, epochs=200, validation_data=(x_test, y_test), callbacks=[history]) model.save_weights('model_weights.h5') history.loss_plot('epoch')
if __name__ == '__main__': # Load generator function batch_size = 256 train_generator = generator(train_samples, batch_size=batch_size) validation_generator = generator(validation_samples, batch_size=batch_size) # Load model retrain = False if not retrain: model = behavioral_cloning_model() else: model = load_model('model.h5') history = LossHistory() # Start training model.fit_generator(train_generator, steps_per_epoch=len(train_samples) / batch_size, validation_data=validation_generator, validation_steps=len(validation_samples) / batch_size, epochs=15, verbose=2, callbacks=[history]) print('training finished') # Plot training history history.loss_plot('epoch') # Save Keras model model.save('model.h5')
mode='auto', epsilon=0.0001, cooldown=0, min_lr=0) # 创建一个 LossHistory 实例 history = LossHistory() # compile model.compile(optimizer=adam(lr=LEARNING_RATE), loss='binary_crossentropy', metrics=['accuracy']) # fit model.fit_generator(train_generator, steps_per_epoch=train_generator.samples // BATCH_SIZE, epochs=EPOCHS, validation_data=valid_generator, validation_steps=valid_generator.samples // BATCH_SIZE, callbacks=[check_point, early_stopping, history]) # 绘制 loss 曲线和 batch 曲线 history.loss_plot('batch', os.path.join(RESULT_PATH, 'inception_v1_loss_batch.png')) history.acc_plot('batch', os.path.join(RESULT_PATH, 'inception_v1_acc_batch.png')) history.loss_plot('epoch', os.path.join(RESULT_PATH, 'inception_v1_loss_epoch.png')) history.acc_plot('epoch', os.path.join(RESULT_PATH, 'inception_v1_acc_epoch.png'))
def create_model(): train_gen = ImageDataGenerator() valid_gen = ImageDataGenerator() train_generator = train_gen.flow_from_directory(TRAIN_DATA_PATH, IMAGE_SIZE, shuffle=True, batch_size=BATCH_SIZE, color_mode='grayscale') valid_generator = valid_gen.flow_from_directory(VALID_DATA_PATH, IMAGE_SIZE, batch_size=BATCH_SIZE, color_mode='grayscale') inputs = Input((*IMAGE_SIZE, 1)) x_input = Lambda(my_preprocess)(inputs) # block1 x = Conv2D(64, (3, 3), input_shape=(*IMAGE_SIZE, 1), strides=(1, 1), padding='same', activation='relu', name='block1_conv1')(x_input) x = Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block1_pool')(x) # block2 x = Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block2_conv1')(x) x = Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block2_pool')(x) # block3 x = Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block3_conv1')(x) x = Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block3_conv2')(x) x = Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='block3_pool')(x) # side1 x_side1 = SeparableConv2D(512, (3, 3), padding='same', use_bias=False, name='side1_sepconv1')(x) x_side1 = BatchNormalization(name='side1_bn1')(x_side1) x_side1 = Activation('relu', name='side1_act1')(x_side1) x_side1 = SeparableConv2D(512, (3, 3), padding='same', use_bias=False, name='side1_sepconv2')(x_side1) x_side1 = BatchNormalization(name='side1_bn2')(x_side1) x_side1 = Activation('relu', name='side1_act2')(x_side1) x_side1 = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='side1_pool')(x_side1) x_side1 = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='side1_sepconv3')(x_side1) x_side1 = BatchNormalization(name='side1_bn3')(x_side1) x_side1 = Activation('relu', name='side1_act3')(x_side1) x_side1 = SeparableConv2D(728, (3, 3), padding='same', activation='relu', name='side1_sepconv4')(x_side1) x_side1 = GlobalAveragePooling2D(name='side1_gap')(x_side1) # side2 x_side2_1_1 = Conv2D(256, (1, 1), strides=(1, 1), padding='same', activation='relu', name='side2_1_conv1')(x) x_side2_1_2 = Conv2D(256, (1, 1), strides=(1, 1), padding='same', activation='relu', name='side2_2_conv1')(x) x_side2_1_2 = Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='side2_2_conv2')(x_side2_1_2) x_side2_1_3 = Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='side2_3_conv1')(x) x_side2_1_3 = Conv2D(256, (1, 1), strides=(1, 1), padding='same', activation='relu', name='side2_3_conv2')(x_side2_1_3) x_side2_1 = keras.layers.concatenate( [x_side2_1_1, x_side2_1_2, x_side2_1_3]) x_side2_2_1 = Conv2D(256, (1, 1), strides=(1, 1), padding='same', activation='relu', name='side3_1_conv1')(x_side2_1) x_side2_2_2 = Conv2D(256, (1, 1), strides=(1, 1), padding='same', activation='relu', name='side3_2_conv1')(x_side2_1) x_side2_2_2 = Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='side3_2_conv2')(x_side2_2_2) x_side2_2_3 = Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='side3_3_conv1')(x_side2_1) x_side2_2_3 = Conv2D(256, (1, 1), strides=(1, 1), padding='same', activation='relu', name='side3_3_conv2')(x_side2_2_3) x_side2_2 = keras.layers.concatenate( [x_side2_2_1, x_side2_2_2, x_side2_2_3]) x_side2 = GlobalAveragePooling2D(name='side2_gap')(x_side2_2) # block4 x = Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block4_conv1')(x) x = Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block4_conv2')(x) x = Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu', name='block4_conv3')(x) x = GlobalAveragePooling2D(name='gap')(x) x = keras.layers.concatenate([x, x_side1, x_side2]) x = Dropout(DROP_RATE, name='dropout1')(x) predictions = Dense(CLASS_NUM, activation='softmax', name='dense1')(x) model = Model(inputs=inputs, outputs=predictions) model.summary() plot_model(model, to_file=os.path.join(RESULT_PATH, 'my_model.png'), show_shapes=True) check_point = ModelCheckpoint(monitor='val_loss', filepath=os.path.join( MODEL_PATH, MODEL_NAME), verbose=1, save_best_only=True, save_weights_only=False, mode='auto') # early stopping early_stopping = EarlyStopping(monitor='val_loss', patience=EARLY_STOPPING_PATIENCE, verbose=0, mode='auto') # reduce lr reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=REDUCE_LR_PATIENCE, verbose=0, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0) # 创建一个 LossHistory 实例 history = LossHistory() # compile model.compile(optimizer=adam(lr=LEARNING_RATE), loss='binary_crossentropy', metrics=['accuracy']) # fit model.fit_generator(train_generator, steps_per_epoch=train_generator.samples // BATCH_SIZE, epochs=EPOCHS, validation_data=valid_generator, validation_steps=valid_generator.samples // BATCH_SIZE, callbacks=[check_point, early_stopping, history]) # 绘制 loss 曲线和 batch 曲线 history.loss_plot('batch', os.path.join(RESULT_PATH, 'my_loss_batch.png')) history.acc_plot('batch', os.path.join(RESULT_PATH, 'my_batch.png')) history.loss_plot('epoch', os.path.join(RESULT_PATH, 'my_loss_epoch.png')) history.acc_plot('epoch', os.path.join(RESULT_PATH, 'my_acc_epoch.png')) K.clear_session()
def mergeFinetuneModel(): X_train = [] X_valid = [] filenames = [ os.path.join(OUTPUT_PATH, 'inceptionv3-finetune-output.hdf5'), os.path.join(OUTPUT_PATH, 'resnet50-finetune-output.hdf5'), os.path.join(OUTPUT_PATH, 'xception-finetune-output.hdf5'), os.path.join(OUTPUT_PATH, 'vgg16-finetune-output.hdf5') ] for filename in filenames: with h5py.File(filename, 'r') as h: X_train.append(np.array(h['X_train'])) X_valid.append(np.array(h['X_val'])) y_train = np.array(h['y_train']) y_valid = np.array(h['y_val']) for x in X_train: print(x.shape) for x in X_valid: print(x.shape) X_train = np.concatenate(X_train, axis=1) X_valid = np.concatenate(X_valid, axis=1) # check print('X_train shape:', X_train.shape) print('X_valid shape:', X_valid.shape) print('y_train shape:', y_train.shape) print('y_valid shape:', y_valid.shape) X_train, y_train = shuffle(X_train, y_train) y_train = to_categorical(y_train) X_valid, y_valid = shuffle(X_valid, y_valid) y_valid = to_categorical(y_valid) print('X_train shape:', X_train.shape) print('X_valid shape:', X_valid.shape) print('y_train shape:', y_train.shape) print('y_valid shape:', y_valid.shape) inputs = Input(X_train.shape[1:]) x = Dense(2048, activation='relu')(inputs) x = Dropout(DROP_RATE)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(CLASS_NUM, activation='softmax')(inputs) model = Model(inputs, predictions) check_point = ModelCheckpoint(filepath=os.path.join( MODEL_PATH, 'merge-model-01.hdf5'), verbose=1, save_best_only=True) early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=EARLY_STOPPING_PATIENCE, verbose=1, mode='auto') # 创建一个 LossHistory 实例 history = LossHistory() model.compile(loss='binary_crossentropy', optimizer=Adam(lr=LEARNING_RATE), metrics=['accuracy']) model.fit(X_train, y_train, epochs=EPOCHS, batch_size=BATCH_SIZE, validation_data=(X_valid, y_valid), callbacks=[early_stopping, check_point, history]) # 绘制 loss 曲线和 batch 曲线 history.loss_plot('batch', os.path.join(RESULT_PATH, 'merge_all_loss_batch.png')) history.acc_plot('batch', os.path.join(RESULT_PATH, 'merge_all_acc_batch.png')) history.loss_plot('epoch', os.path.join(RESULT_PATH, 'merge_all_loss_epoch.png')) history.acc_plot('epoch', os.path.join(RESULT_PATH, 'merge_all_acc_epoch.png'))
class Vae: def __init__(self, pic_train_nb=5000, pic_test_nb=100): self.train_data = load_data_anime_face(pic_train_nb) / 255 self.test_data = load_data_anime_face(pic_test_nb) / 255 self.input_shape = (96, 96, 3) self.batch_size = 512 self.latent_dim = 50 self.epochs = 200 self.learning_rate = 0.01 def build_model(self): # Encoder self.inputs = Input(shape=self.input_shape, name="Encoder_input") self.x = Conv2D(filters=16, kernel_size=(3, 3), activation='relu', strides=2, padding='same', data_format="channels_last")(self.inputs) self.x = Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=2, padding='same', data_format="channels_last")(self.x) self.shape = K.int_shape(self.x) self.x = Flatten()(self.x) self.x = Dense(16, activation='relu')(self.x) self.z_mean = Dense(self.latent_dim, name='z_mean')(self.x) self.z_log_var = Dense(self.latent_dim, name='z_log_var')(self.x) self.z = Lambda(sample, output_shape=(self.latent_dim, ), name='z')([self.z_mean, self.z_log_var]) self.encoder = Model(self.inputs, [self.z_mean, self.z_log_var, self.z], name='encoder') print(self.encoder.summary()) # Decoder self.latent_inputs = Input(shape=(self.latent_dim, ), name='z_sampling') self.x = Dense(self.shape[1] * self.shape[2] * self.shape[3], activation='relu')(self.latent_inputs) self.x = Reshape((self.shape[1], self.shape[2], self.shape[3]))(self.x) self.x = Conv2DTranspose(filters=32, kernel_size=(3, 3), activation='relu', strides=2, padding='same', data_format="channels_last")(self.x) self.x = Conv2DTranspose(filters=16, kernel_size=(3, 3), activation='relu', strides=2, padding='same', data_format="channels_last")(self.x) self.outputs = Conv2DTranspose(filters=3, kernel_size=(3, 3), activation='sigmoid', padding='same', name='decoder_output')(self.x) self.decoder = Model(self.latent_inputs, self.outputs, name='decoder') print(self.decoder.summary()) # Instantiate VAE model self.outputs = self.decoder(self.encoder(self.inputs)[2]) self.vae_model = Model(self.inputs, self.outputs, name='vae') def train_model(self): self.output_loss = mse(K.flatten(self.inputs), K.flatten(self.outputs)) self.output_loss *= 96 * 96 * 3 self.kl_loss = 1 + self.z_log_var - K.square(self.z_mean) - K.exp( self.z_log_var) self.kl_loss = K.sum(self.kl_loss, axis=-1) self.kl_loss *= -0.5 self.total_loss = K.mean(self.output_loss + self.kl_loss) self.vae_model.add_loss(self.total_loss) self.adam = Adam(lr=self.learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08) self.vae_model.compile(optimizer=self.adam) print(self.vae_model.summary()) self.history = LossHistory() self.vae_model.fit(self.train_data, epochs=self.epochs, batch_size=self.batch_size, validation_data=(self.test_data, None), callbacks=[self.history]) def plot_given_z(self, z): pict = self.decoder.predict(z) plot(pict) def plot_random_result(self): z = np.array([np.random.normal(0, 1, self.latent_dim)]) self.plot_given_z(z) def plot_random_train(self): train_size = np.shape(self.train_data)[0] train_pic_id = np.random.randint(0, train_size) train_pic = np.array([self.train_data[train_pic_id]]) plot(train_pic) predict_pic = self.vae_model.predict(train_pic) plot(predict_pic) def plot_random_test(self): test_size = np.shape(self.test_data)[0] test_pic_id = np.random.randint(0, test_size) test_pic = np.array([self.test_data[test_pic_id]]) plot(test_pic) predict_pic = self.vae_model.predict(test_pic) plot(predict_pic) def plot_loss(self): self.history.loss_plot('epoch') def save_model(self): return 0 def load_model(self): return 0
def finetuneModel(): train_gen = ImageDataGenerator() valid_gen = ImageDataGenerator() train_generator = train_gen.flow_from_directory(TRAIN_DATA_PATH, IMAGE_SIZE, shuffle=True, batch_size=BATCH_SIZE, color_mode='grayscale') valid_generator = valid_gen.flow_from_directory(VALID_DATA_PATH, IMAGE_SIZE, batch_size=BATCH_SIZE, color_mode='grayscale') inputs = Input((*IMAGE_SIZE, 1)) x = Lambda(my_preprocess)(inputs) base_model = InceptionV3(input_tensor=x, weights=None, include_top=False) x = GlobalAveragePooling2D(name='my_global_average_pooling_layer_1')( base_model.output) x = Dropout(DROP_RATE, name='my_dropout_layer_1')(x) predictions = Dense(CLASS_NUM, activation='softmax', name='my_dense_layer_1')(x) model = Model(base_model.input, predictions) plot_model(model, to_file=os.path.join(RESULT_PATH, 'inceptionv3.png'), show_shapes=True) # set trainable layer for layer in model.layers[:INCEPTIONV3_NO_TRAINABLE_LAYERS]: layer.trainable = False for layer in model.layers[INCEPTIONV3_NO_TRAINABLE_LAYERS:]: layer.trainable = True model.summary() # check for i, layer in enumerate(model.layers): print('{}: {}, {}'.format(i, layer.name, layer.trainable)) print('=' * 100) layers = zip(range(len(model.layers)), [x.name for x in model.layers]) for layer_num, layer_name in layers: print('{}: {}'.format(layer_num + 1, layer_name)) # check point check_point = ModelCheckpoint(monitor='val_loss', filepath=os.path.join( MODEL_PATH, MODEL_NAME), verbose=1, save_best_only=True, save_weights_only=False, mode='auto') # early stoppiing early_stopping = EarlyStopping(monitor='val_loss', patience=EARLY_STOPPING_PATIENCE, verbose=0, mode='auto') # 创建一个 LossHistory 实例 history = LossHistory() # compile model.compile(optimizer=adam(lr=LEARNING_RATE), loss='binary_crossentropy', metrics=['accuracy']) # fit model.fit_generator(train_generator, steps_per_epoch=train_generator.samples // BATCH_SIZE, epochs=EPOCHS, validation_data=valid_generator, validation_steps=valid_generator.samples // BATCH_SIZE, callbacks=[check_point, early_stopping, history]) # 绘制 loss 曲线和 batch 曲线 history.loss_plot('batch', os.path.join(RESULT_PATH, 'inceptionv3_loss_batch.png')) history.acc_plot('batch', os.path.join(RESULT_PATH, 'inceptionv3_acc_batch.png')) history.loss_plot('epoch', os.path.join(RESULT_PATH, 'inceptionv3_loss_epoch.png')) history.acc_plot('epoch', os.path.join(RESULT_PATH, 'inceptionv3_acc_epoch.png'))