def do_training(self, model: Model, steps_per_epoch: int, epochs: int, validation_data): model.fit_generator(self.generator, validation_data=validation_data, steps_per_epoch=steps_per_epoch, epochs=epochs, callbacks=[self.tensorboard_callback])
def train(self): encoder_inputs = Input(shape=(320, 1024)) encoder_BidirectionalLSTM = Bidirectional( LSTM(128, return_sequences=True)) encoder_out = encoder_BidirectionalLSTM(encoder_inputs) decoder_LSTM = LSTM(256, return_sequences=True) decoder_out = decoder_LSTM(encoder_out) attn_layer = Attention(use_scale=True) attn_out = attn_layer([encoder_out, decoder_out]) dense = TimeDistributed(Dense(1, activation='softmax')) decoder_pred = dense(attn_out) model = Model(inputs=encoder_inputs, outputs=decoder_pred) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() self.model = model t = trange(self.config.n_epochs, desc='Epoch', ncols=90) for epoch_i in t: model.fit_generator(generator=self.train_loader) ckpt_path = self.config.save_dir + '/epoch-{}.ckpt'.format(epoch_i) tqdm.write("Save parameters at {}".format(ckpt_path)) model.save_weights('ckpt_path') self.evaluate(epoch_i)
def _main(): outputs, inputs = model_tiny_YOLOv1() input_shape = tuple(inputs.shape[1:]) model = Model(inputs=inputs, outputs=outputs) model.compile(loss=yolo_loss, optimizer='adam') tf.keras.utils.plot_model(model, to_file='../model.png', show_shapes=True, show_layer_names=False, rankdir='TB', expand_nested=False, dpi=96) save_dir = 'checkpoints' weights_path = os.path.join(save_dir, 'faces-weights.hdf5') checkpoint = ModelCheckpoint(weights_path, monitor='val_loss', verbose=1, save_weights_only=False, save_best_only=True) if not os.path.isdir(save_dir): os.makedirs(save_dir) if os.path.exists(weights_path): model = tf.keras.models.load_model(weights_path, custom_objects={ "ReshapeYOLO": ReshapeYOLO, "yolo_loss": yolo_loss }) else: # model.load_weights('tiny-yolov1.hdf5', by_name=True) print('no train history') opt = Adam(lr=0.0001) model.compile(loss=yolo_loss, optimizer=opt) early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto') tb_callback = tf.keras.callbacks.TensorBoard(log_dir="TensorBoard", histogram_freq=1, write_graph=True, write_images=True) train_generator = SequenceForCelebA('train', annotation_path, img_path, input_shape, batch_size) validation_generator = SequenceForCelebA('val', annotation_path, img_path, input_shape, int(batch_size / 2)) with tf.device('/gpu:0'): model.fit_generator( train_generator, steps_per_epoch=len(train_generator), epochs=epochs, validation_data=validation_generator, validation_steps=len(validation_generator), # use_multiprocessing=True, workers=4, callbacks=[early_stopping, checkpoint, tb_callback], verbose=1) model.save_weights('faces-tiny-yolov1.hdf5')
def trainMobilenetFullyTrainable(): # Transfer learning Mobilenet model with no layers trainable from tensorflow.keras.optimizers import Adam from tensorflow.keras import layers from tensorflow.keras import Model from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 # Base model is a MobileNet without pretrained weights base_model = MobileNetV2(input_shape=(32, 32, 3), weights=None, classes=num_classes, include_top=True) # Define model using functional method, input -> resize -> MobileNet input_layer = keras.layers.Input(shape=(28, 28, 3)) x = keras.layers.Layer()(input_layer) x = layers.experimental.preprocessing.Resizing(32, 32, interpolation="bilinear", name="the_resize_layer")(x) output = base_model(x) model = Model(inputs=input_layer, outputs=output) # Compile model model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) # Train model with samples flowing from ImageDataGenerator model.fit_generator(datagen.flow(x_train, y_train_onehot, batch_size=10), steps_per_epoch=len(x_train) // 10, epochs=epochs, verbose=1) return model
def train_model(self): """ Training the model """ print("Training the model") LR = 1e-3 epochs = 200 callbacks = [ EarlyStopping(monitor='val_loss', min_delta=0, patience=30, verbose=0, mode='auto'), ModelCheckpoint('model.h5', monitor='val_loss', mode='min', save_best_only=True), ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0) ] # Pre trained model Xception without fully connected layers base_model = Xception(input_shape=(self.img_size[0], self.img_size[1], 3), include_top=False, weights='imagenet') # Unfreeze the layers base_model.trainable = True x = GlobalMaxPooling2D()(base_model.output) x = Dense(512, activation='relu')(x) x = Dense(10, activation='relu')(x) output = Dense(1, activation='linear')(x) model = Model(inputs=base_model.input, outputs=output) model.compile(loss='mse', optimizer=Adam(learning_rate=LR), metrics=[self.mae_in_months]) print(base_model.summary()) print(model.summary()) history = model.fit_generator( self.train_datagen.flow(self.x_train, self.y_train, batch_size=self.batch_size), steps_per_epoch=len(self.x_train) / self.batch_size, validation_data=self.val_datagen.flow(self.x_val, self.y_val, batch_size=self.batch_size), validation_steps=len(self.x_val) / self.batch_size, callbacks=callbacks, epochs=epochs, verbose=1) self.plot_it(history) model.load_weights('model.h5') pred = self.mean_bone_age + self.std_bone_age * (model.predict( self.x_val, batch_size=self.batch_size, verbose=True)) actual = self.mean_bone_age + self.std_bone_age * (self.y_val)
def train_clf(self, graph, L): ''' Train SGC model with updated labeled pool L Return new trained model ''' train_targets = self.target_encoding.transform( self.df_targets.loc[L].to_dict("records")) train_gen = self.generator.flow(L, train_targets) sgc = GCN( layer_sizes=[train_targets.shape[1]], generator=self.generator, bias=True, dropout=0.5, activations=["softmax"], kernel_regularizer=regularizers.l2(5e-4), ) x_inp, predictions = sgc.build() class_support = dict(Counter(self.df_targets.loc[L]["label"])) classes = sorted(self.data.class_labels) counts = [ class_support[c] if c in class_support else 0 for c in classes ] weights = np.sum(counts) / np.array(counts) weighted_loss = self.weighted_categorical_crossentropy(weights) model = Model(inputs=x_inp, outputs=predictions) model.compile( optimizer=optimizers.Adam(lr=0.2), # loss=losses.categorical_crossentropy, loss=weighted_loss, metrics=["acc"], ) # if not os.path.isdir("model_logs"): # os.makedirs("model_logs") # es_callback = EarlyStopping( # monitor="acc", patience=50 # ) # patience is the number of epochs to wait before early stopping in case of no further improvement # mc_callback = ModelCheckpoint( # "model_logs/best_model.h5", monitor="acc", save_best_only=True, save_weights_only=True # ) history = model.fit_generator( train_gen, epochs=50, verbose=0, shuffle= False, # this should be False, since shuffling data means shuffling the whole graph # callbacks=[es_callback, mc_callback], ) # model.load_weights("model_logs/best_model.h5") return model
def transferNoTrainable(): # Transfer learning Mobilenet model with no layers trainable from tensorflow.keras.optimizers import Adam from tensorflow.keras import layers from tensorflow.keras import Model from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 # Base model is a MobileNet with ImageNet pretrained weights and the top removed base_model = MobileNetV2(input_shape=(32, 32, 3), weights='imagenet', classes=num_classes, include_top=False) # Set all of the layers to be not trainable for layer in base_model.layers: layer.trainable = False # Define model using functional method, input -> resize -> MobileNet input_layer = keras.layers.Input(shape=(28, 28, 3)) x = keras.layers.Layer()(input_layer) x = layers.experimental.preprocessing.Resizing(32, 32, interpolation="bilinear", name="the_resize_layer")(x) x = base_model(x) # Flatten the output layer x = layers.GlobalAveragePooling2D()(x) # Add a fully connected layer with 1,024 hidden units and ReLU activation x = layers.Dense(1024, activation='relu')(x) # Add a final sigmoid layer for classification output = layers.Dense(10, activation='sigmoid')(x) model = Model(inputs=input_layer, outputs=output) # Compile model model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) # Train model with samples flowing from ImageDataGenerator model.fit_generator(datagen.flow(x_train, y_train_onehot, batch_size=10), steps_per_epoch=len(x_train) // 10, epochs=epochs, verbose=1) return model
class BaseModel: def __init__(self, num_outputs=4): self.model = None self.num_outputs = num_outputs @abstractmethod def get_io_layers(self): pass def build_model(self): inputs, outputs = self.get_io_layers() self.model = Model(inputs=inputs, outputs=outputs) self.model.compile(optimizer=Adam(1e-4), loss='binary_crossentropy', metrics=['acc']) #self.model.compile(optimizer=Adam(1e-4), loss=dice_loss, metrics=[dice_coef]) def fit(self, **args): return self.model.fit(**args) def fit_generator(self, **args): return self.model.fit_generator(**args) def predict(self, **args): return self.model.predict(**args) def predict_generator(self, **args): return self.model.predict_generator(**args) def summary(self): return self.model.summary() def plot_history(self, keys=['loss', 'val_loss']): if len(keys) == 0: return history = self.model.history.history n = 3 primary = np.array(history[keys[0]]) ymin = primary.mean() - primary.std() * n ymax = primary.mean() + primary.std() * n plt.figure() plt.ylim(ymin, ymax) for key in keys: if key in history.keys(): #plt.plot(np.arange(len(history[key])), history[key], label=key) plt.plot(history[key], label=key) else: print(f'Unable to plot {key}') plt.legend() plt.show()
def _train_model( self, x_train: np.array, y_train: np.array, x_val: np.array, y_val: np.array, model: kr.Model, params: Dict, ) -> kr.callbacks.History: if "seed" in params: tf.random.set_seed(params["seed"]) callbacks = ([ kr.callbacks.EarlyStopping(monitor="val_loss", patience=params["early_stopping"]) ] if "early_stopping" in params else []) # logging params to wandb - not syncing, active syncing causes # slurm to not terminate the job os.environ["WANDB_MODE"] = "dryrun" wandb.init(project="deepscribe", config=params) callbacks.append(WandbCallback()) if "reweight" in params: class_weights_arr = compute_class_weight("balanced", np.unique(y_train), y_train) class_weight_dict = dict(enumerate(class_weights_arr)) else: class_weight_dict = None # use image data generator to perform random translations shear_range = params.get("shear", 0.0) zoom_range = params.get("zoom", 0.0) data_gen = kr.preprocessing.image.ImageDataGenerator( shear_range=shear_range, zoom_range=zoom_range) history = model.fit_generator( data_gen.flow(x_train, y=y_train), steps_per_epoch=x_train.shape[0] / params["batch_size"], epochs=params["epochs"], validation_data=(x_val, y_val), callbacks=callbacks, class_weight=class_weight_dict, ) return history
def building_model(local_path1, last_output, pre_trained_model): callbacks = myCallback() x = layers.Flatten()(last_output) x = layers.Dense(1024, activation = 'relu')(x) #Dropouts step x = layers.Dropout(0.2)(x) #dropout 20% x = layers.Dense(1,activation = 'sigmoid')(x) model = Model(pre_trained_model.input,x) model.summary() model.compile(optimizer = RMSprop(lr=1e-04), loss = 'binary_crossentropy', metrics = ['acc']) train_generator, validation_generator=generating_data(local_path1) history = model.fit_generator( train_generator, steps_per_epoch = 100, epochs = 10, verbose = 2, validation_data = validation_generator, validation_steps = 50, callbacks = [callbacks] ) #verbose=2: Note the values per epoch (loss, accuracy, #validation loss, validation accuracy) ## retrieve values acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] #evaluate the model model_evaluation(acc, val_acc, loss, val_loss) ## save model model.save('TF_Dogs_Cats_TransferLearning0.h5') #after training, saving the model into the .h5 file return model
text_input = Input(shape=(MAX_LEN, )) embedding = Embedding(vocab_size, EMBEDDING_DIM, input_length=MAX_LEN)(text_input) x = Concatenate()([images, embedding]) y = Bidirectional(LSTM(256, return_sequences=False))(x) pred = Dense(vocab_size, activation='softmax')(y) model = Model(inputs=[img_input, text_input], outputs=pred) model.compile(loss='categorical_crossentropy', optimizer="RMSProp", metrics=['accuracy']) model.summary() callbacks = [ EarlyStopping(patience=10, verbose=1), ReduceLROnPlateau(factor=0.1, patience=3, min_lr=0.00001, verbose=1), ModelCheckpoint(os.path.join( os.path.join(model_workspace_dir, 'weights_best.hdf5')), verbose=1, save_best_only=False, save_weights_only=True), CSVLogger(os.path.join(model_workspace_dir, 'training.csv')), PerformanceMetrics(os.path.join(model_workspace_dir, 'performance.csv')), ] model.fit_generator(generator=training_generator, validation_data=validation_generator, epochs=100, callbacks=callbacks)
val_humans_fnames = os.listdir(val_humans_dir) print(len(train_horses_fnames), len(train_humans_fnames)) print(len(val_humans_fnames), len(val_humans_fnames)) train_datagen = ImageDataGenerator(rescale=1. / 255., rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=0.2, fill_mode='nearest') test_datagen = ImageDataGenerator(rescale=1. / 255.) train_generator = train_datagen.flow_from_directory(train_dir, batch_size=20, class_mode='binary', target_size=(150, 150)) validation_generator = test_datagen.flow_from_directory(validation_dir, batch_size=20, class_mode='binary', target_size=(150, 150)) callbacks = myCallback() history = model.fit_generator(train_generator, validation_data=validation_generator, steps_per_epoch=50, epochs=3, validation_steps=50, verbose=2, callbacks=[callbacks])
train_dir, # This is the source directory for training images target_size=(150, 150), # All images will be resized to 150x150 batch_size=20, # Since we use binary_crossentropy loss, we need binary labels class_mode='binary') # Flow validation images in batches of 20 using val_datagen generator validation_generator = val_datagen.flow_from_directory(validation_dir, target_size=(150, 150), batch_size=20, class_mode='binary') #training uper model history = model.fit_generator( train_generator, steps_per_epoch=100, # 2000 images = batch_size * steps epochs=15, validation_data=validation_generator, validation_steps=50, # 1000 images = batch_size * steps verbose=2) #uploading from computer import numpy as np from google.colab import files from keras.preprocessing import image uploaded = files.upload() for fn in uploaded.keys(): # predicting images
SOURCE_DIR = r'C:\Users\sachi\Desktop\keras-covid-19\dataset' print(len(os.listdir(COVID_SOURCE_DIR))) print(len(os.listdir(NORMAL_SOURCE_DIR))) EPOCHS = int(input("Enter no. of epochs: ")) train_datagen = ImageDataGenerator( rescale=1. / 255, validation_split=0.2) # set validation split train_generator = train_datagen.flow_from_directory( SOURCE_DIR, class_mode='binary', subset='training') # set as training data validation_generator = train_datagen.flow_from_directory( SOURCE_DIR, # same directory as training data class_mode='binary', subset='validation') # set as validation data H = model.fit_generator(train_generator, validation_data=validation_generator, epochs=EPOCHS) N = EPOCHS plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, N), H.history["loss"], label="train_loss") plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc") plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc") plt.title("Training Loss and Accuracy on COVID-19 Dataset") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend(loc="lower left")
class YOLO(object): """YOLO network""" def __init__(self, backend, input_size, labels, anchors): self.input_size = input_size self.labels = labels self.anchors = anchors self.nb_class = len(self.labels) self.nb_box = len(self.anchors) // 2 # Construct the model # =================== input_image = Input(shape=(self.input_size, self.input_size, 3)) # Feature extraction layer if backend == 'Full Yolo': self.feature_extractor = FullYoloFeature(self.input_size) elif backend == 'Tiny Yolo': self.feature_extractor = TinyYoloFeature(self.input_size) else: raise Exception('YOLO does not support {} backend'.format(backend)) self.grid_h, self.grid_w = self.feature_extractor.get_output_shape() features = self.feature_extractor.extract(input_image) # Object detection layer output = Conv2D(self.nb_box * (4 + 1 + self.nb_class), (1, 1), strides=(1, 1), padding='same', name='DetectionLayer')(features) output = Reshape((self.grid_h, self.grid_w, self.nb_box, 4 + 1 + self.nb_class))(output) # Plug input and output into the model self.model = Model(input_image, output) # Initialize the weights of the detection layer layer = self.model.layers[-2] weights = layer.get_weights() new_kernel = np.random.random(size=weights[0].shape) new_bias = np.random.random(size=weights[1].shape) layer.set_weights([new_kernel, new_bias]) # MODEL SUMMARY self.model.summary() def _custom_loss(self, y_true, y_pred): cell_x = tf.cast( tf.reshape(tf.tile(tf.range(self.grid_w), [self.grid_h]), (1, self.grid_h, self.grid_w, 1, 1)), tf.float32) cell_y = tf.transpose(cell_x, (0, 2, 1, 3, 4)) cell_grid = tf.tile(tf.concat([cell_x, cell_y], -1), [self.batch_size, 1, 1, self.nb_box, 1]) # Adjust prediction # ================= # adjust x & y (batch, grid_h, grid_w, nb_box, [x, y]) pred_box_xy = tf.cast( tf.sigmoid(y_pred[..., :2]) + cell_grid, tf.float32) # adjust w & h (batch, grid_h, grid_w, nb_box, [w,h]) pred_box_wh = tf.cast( tf.exp(tf.maximum(y_pred[..., 2:4], 2.)) * np.reshape(self.anchors, [1, 1, 1, self.nb_box, 2]), tf.float32) # adjust confidence (batch, grid_h, grid_w, nb_box) pred_box_conf = tf.cast(tf.sigmoid(y_pred[..., 4]), tf.float32) # adjust class probabilities (batch, grid_h, gird_w, nb_box, [classes]) pred_box_class = tf.cast(y_pred[..., 5:], tf.float32) # Adjust ground truth # =================== # adjust x & y (batch, grid_h, grid_w, nb_box, [x, y]) true_box_xy = tf.cast(y_true[..., :2], tf.float32) # adjust w & h (batch, grid_h, grid_w, nb_box, [w, h]) true_box_wh = tf.cast(y_true[..., 2:4], tf.float32) # adjust confidence (batch, grid_h, grid_w, nb_box) true_wh_half = true_box_wh / 2.0 true_mins = true_box_xy - true_wh_half true_maxes = true_box_xy + true_wh_half pred_wh_half = pred_box_wh / 2.0 pred_mins = pred_box_xy - pred_wh_half pred_maxes = pred_box_xy + pred_wh_half intersect_mins = tf.maximum(pred_mins, true_mins) intersect_maxes = tf.minimum(pred_maxes, true_maxes) intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] true_areas = true_box_wh[..., 0] * true_box_wh[..., 1] pred_areas = pred_box_wh[..., 0] * pred_box_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = tf.truediv(intersect_areas, union_areas) true_box_conf = tf.cast(iou_scores * y_true[..., 4], tf.float32) # adjust class probability (batch, grid_h, grid_w, nb_box, [classes]) true_box_class = tf.cast(tf.argmax(y_true[..., 5:], axis=-1), tf.float32) # Determine the mask # ================== # coordinate mask coord_mask = tf.cast( tf.expand_dims(y_true[..., 4], axis=-1) * self.coord_scale, tf.float32) # confidence mask conf_mask = tf.cast(y_true[..., 4], tf.float32) + tf.cast( 1 - y_true[..., 4], tf.float32) * self.no_object_scale # class mask class_mask = tf.cast(y_true[..., 4] * self.class_scale, tf.float32) # Finalize the loss # ================= nb_coord_box = tf.reduce_sum(tf.cast(coord_mask > 0., tf.float32)) nb_class_box = tf.reduce_sum(tf.cast(class_mask > 0., tf.float32)) nb_conf_box = tf.reduce_sum(tf.cast(conf_mask > 0., tf.float32)) loss_xy = tf.reduce_sum( tf.square(true_box_xy - pred_box_xy) * coord_mask) / (nb_coord_box + 1e-6) / 2. loss_wh = tf.reduce_sum( tf.square(tf.sqrt(true_box_wh) - tf.sqrt(pred_box_wh)) * coord_mask) / (nb_coord_box + 1e-6) / 2. loss_conf = tf.reduce_sum( tf.square(true_box_conf - pred_box_conf) * conf_mask) / (nb_conf_box + 1e-6) / 2. loss_class = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=true_box_class, logits=pred_box_class) loss_class = tf.reduce_sum( loss_class * class_mask) / (nb_class_box + 1e-6) / 2. return loss_xy + loss_wh + loss_conf + loss_class def train( self, train_images, # the list of images to train the mode valid_images, # the list of images to validate the model nb_epochs, # the number of times to repeat the validation set learning_rate, # the learning rate # loss function related batch_size, object_scale, no_object_scale, coord_scale, class_scale, # path to save the model saved_weights_name, debug=False): self.batch_size = batch_size self.debug = debug # Coefficient used in loss function self.object_scale = object_scale self.no_object_scale = no_object_scale self.coord_scale = coord_scale self.class_scale = class_scale # Construct train and validate data generator # ============================================ generator_config = { 'IMAGE_H': self.input_size, 'IMAGE_W': self.input_size, 'GRID_H': self.grid_h, 'GRID_W': self.grid_w, 'BOX': self.nb_box, 'LABELS': self.labels, 'ANCHORS': self.anchors, 'BATCH_SIZE': self.batch_size } train_generator = YoloDataGenerator( train_images, generator_config, norm=self.feature_extractor.normalize) valid_generator = YoloDataGenerator( valid_images, generator_config, norm=self.feature_extractor.normalize) # Compile the model # ================= optimizer = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) self.model.compile(loss=self._custom_loss, optimizer=optimizer) # Make a few callbacks # ==================== early_stop = EarlyStopping(monitor='val_loss', min_delta=0.001, patience=5, mode='min', verbose=1) checkpoint = ModelCheckpoint(saved_weights_name, monitor='val_loss', verbose=1, save_best_only=True, mode='min', period=1) tensorboard = TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True, write_images=False) # Start the training process # ========================== self.model.fit_generator( generator=train_generator, steps_per_epoch=len(train_generator), validation_data=valid_generator, validation_steps=len(valid_generator), epochs=nb_epochs, verbose=1, callbacks=[early_stop, checkpoint, tensorboard])
class myCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): if (logs.get('accuracy') > 0.97): print("\nReached 97.0% accuracy so cancelling training!") self.model.stop_training = True # In[26]: callback = myCallback() history = custom_model.fit_generator( train_generator, validation_data=validation_generator, steps_per_epoch=1, epochs=100, validation_steps=1, verbose=2, # callbacks = [callback] ) # In[27]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc))
"./models/20200102-val_loss.{val_loss:.2f}.h5", monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1) # checkpoint = callbacks.ModelCheckpoint("./models/loss.{loss:.2f}-acc.{acc:.2f}-val_loss.{val_loss:.2f}-val_acc.{val_acc:.2f}.h5", monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1) # early = EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto') # reduce_lr = callbacks.ReduceLROnPlateau(monitor='acc', factor=0.1, patience=2, min_lr=0.000001) learningRateScheduler = callbacks.LearningRateScheduler(lr_decay) tensorboard = callbacks.TensorBoard(log_dir='./logs') # In[ ]: history = model.fit_generator( generator=train_generator, steps_per_epoch=ceil(len(train_labels) / batch_size), validation_data=test_generator, validation_steps=ceil(len(test_labels) / batch_size), epochs=nb_epochs, callbacks=[checkpoint, tensorboard, learningRateScheduler], use_multiprocessing=True, workers=6, verbose=1) # In[ ]: model.save('./models/20200102-last_model.h5')
lrr = ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=1, factor=0.4, min_lr=0.0001) callbacks = [lrr] STEP_SIZE_TRAIN = train_gen.n // train_gen.batch_size STEP_SIZE_VALID = valid_gen.n // valid_gen.batch_size transfer_learning_history = model.fit_generator( generator=train_gen, steps_per_epoch=STEP_SIZE_TRAIN, validation_data=valid_gen, validation_steps=STEP_SIZE_VALID, epochs=1, callbacks=callbacks, class_weight='auto', ) model.save('M1') model.evaluate(test_gen, steps=STEP_SIZE_VALID, verbose=1) STEP_SIZE_TEST = valid_gen.n // valid_gen.batch_size test_gen.reset() pred = model.predict(valid_gen, steps=STEP_SIZE_TEST, verbose=1) predicted_class_indices = np.argmax(pred, axis=1)
zca_whitening=False, # apply ZCA whitening zca_epsilon=1e-06, # epsilon for ZCA whitening rotation_range=20, # randomly rotate images in the range (degrees, 0 to 180) # randomly shift images horizontally (fraction of total width) width_shift_range=0.1, # randomly shift images vertically (fraction of total height) height_shift_range=0.1, shear_range=0.1, # set range for random shear zoom_range=0.2, # set range for random zoom channel_shift_range=0., # set range for random channel shifts # set mode for filling points outside the input boundaries fill_mode='nearest', cval=0., # value used for fill_mode = "constant" horizontal_flip=True, # randomly flip images vertical_flip=False, # randomly flip images # set rescaling factor (applied before any other transformation) rescale=None, # set function that will be applied on each input preprocessing_function=None, # image data format, either "channels_first" or "channels_last" data_format=None) datagen.fit(x_train) callbacks = [ModelCheckpoint(filepath="./teacher_models/teacher_model_epoch_{epoch:02d}-val_acc_{val_acc}.hdf5")] model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_test, y_test), workers=4, callbacks=callbacks)
target_size=(256, 256), # All images will be resized to 256x256 batch_size=128, class_mode='categorical') validation_generator = test_datagen.flow_from_directory( validation_dir, target_size=(256, 256), batch_size=128, class_mode='categorical') callbacks = myCallback() history = model.fit_generator( train_generator, steps_per_epoch=32, # 16512 images = batch_size * steps epochs=200, validation_data=validation_generator, validation_steps=7, # 4126 images = batch_size * steps verbose=1, callbacks = [callbacks]) #Simple Deep Learning Architecture from keras import Sequential from tensorflow.keras.optimizers import RMSprop from tensorflow.keras.optimizers import Adam class myCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): if(logs.get('val_acc')>0.97): print("\nReached 97% validation accuracy so cancelling training!") self.model.stop_training = True
last = initial_model.output x = Flatten()(last) x = Dense(4096, activation='relu')(x) x = Dropout(0.4)(x) x = Dense(4096, activation='relu')(x) x = Dropout(0.4)(x) preds = Dense(1, activation='sigmoid')(x) model = Model(initial_model.input, preds) # %% [markdown] ###3. (compile and fit) Now we train the model model.compile(Adam(1e-5), loss='binary_crossentropy', metrics=['accuracy']) model.fit_generator(generator=training_generator, use_multiprocessing=True, epochs=1, workers=8) # %% [markdown] ###4. (evaluate and predict) Read validation images and evaluate the model X_val = np.empty((len(f_valid), PATCH_DIM, PATCH_DIM, 3)) y_val = np.empty((len(f_valid)), dtype=int) for i, f in enumerate(f_valid): # Store sample img_path = os.path.join(TRAIN_DIR, f) img = image.load_img(img_path, target_size=(PATCH_DIM, PATCH_DIM)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x)
class SixthPage(BoxLayout): def __init__(self, **kwargs): super().__init__(**kwargs) self.orientation = "vertical" self.add_widget(Label()) self.add_widget(Label()) self.add_widget(Label()) self.add_widget(Label()) self.updating_btn = Button(text="Start Training", on_press=self.updating, pos_hint={ "center_x": 0.5, "center_y": 0.5 }, size_hint=(0.5, 0.5), background_color=(0, 0, 0, 1), font_size="20sp") self.add_widget(self.updating_btn) self.add_widget(Label()) self.add_widget(Label()) self.add_widget(Label()) self.add_widget(Label()) def updating(self, obj): # print(main_app.fourth_page.dataset_folder_name) # print(int(main_app.fifth_page.epoch_inp.text)) # print(int(main_app.fifth_page.batch_size_inp.text)) # print(float(main_app.fifth_page.learning_rate_inp.text)) # print(float(int(main_app.fifth_page.drop_out_rate_inp.text)/100)) self.clear_widgets() self.orientation = "vertical" local_weights_file = "model_weights/inception_v3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5" pre_trained_model = InceptionV3(input_shape=(150, 150, 3), include_top=False, weights=None) pre_trained_model.load_weights(local_weights_file) for layer in pre_trained_model.layers: layer.trainable = False last_layer = pre_trained_model.get_layer("mixed7") last_output = last_layer.output x = layers.Flatten()(last_output) x = layers.Dense(1024, activation="relu")(x) x = layers.Dropout( float(int(main_app.fifth_page.drop_out_rate_inp.text) / 100))(x) x = layers.Dense(1, activation="sigmoid")(x) self.model = Model(pre_trained_model.input, x) self.model.compile(optimizer=RMSprop( lr=float(main_app.fifth_page.learning_rate_inp.text)), loss="categorical_crossentropy", metrics=["acc"]) from tensorflow.keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_generator = train_datagen.flow_from_directory( main_app.fourth_page.dataset_folder_name + "train/", target_size=(150, 150), class_mode="categorical") validation_datagen = ImageDataGenerator(rescale=1 / 255) self.validation_generator = validation_datagen.flow_from_directory( main_app.fourth_page.dataset_folder_name + "test/", target_size=(150, 150), class_mode="categorical") history = self.model.fit_generator( train_generator, validation_data=self.validation_generator, epochs=int(main_app.fifth_page.epoch_inp.text), verbose=2) scores = self.model.evaluate_generator(self.validation_generator) print("%s: %.2f%%" % (self.model.metrics_names[1], scores[1] * 100)) acc = history.history["acc"] val_acc = history.history["val_acc"] loss = history.history["loss"] val_loss = history.history["val_loss"] epochs = range(len(acc)) plt.subplot(211) plt.title("Training History") plt.plot(epochs, acc, "-b", label="training_acc") plt.plot(epochs, val_acc, "-r", label="validation_acc") plt.legend(loc="upper left") plt.subplot(212) # self.add_widget(FigureCanvasKivyAgg(plt.gcf())) plt.plot(epochs, loss, "-b", label="training_loss") plt.plot(epochs, val_loss, "-r", label="validation_loss") plt.legend(loc="upper left") self.add_widget(FigureCanvasKivyAgg(plt.gcf())) self.model_save_btn = Button(text="Save model", background_color=(0, 0, 0, 1), font_size="20sp", on_press=self.save_model, size_hint=(0.2, 0.2), pos_hint={ "center_x": 0.5, "center_y": 0.8 }) self.add_widget(self.model_save_btn) self.model_load_btn = Button(text="Load model", background_color=(0, 0, 0, 1), font_size="20sp", on_press=self.load_model, size_hint=(0.2, 0.2), pos_hint={ "center_x": 0.5, "center_y": 0.8 }) self.add_widget(self.model_load_btn) self.add_widget( Button(text="Quit", background_color=(0, 0, 0, 1), font_size="20sp", on_press=self.quit_app, size_hint=(0.2, 0.2), pos_hint={ "center_x": 0.9, "center_y": 1.0 })) def path_check(self, path): if os.path.exists(path): pass else: os.makedirs(path) def quit_app(self, obj): App.get_running_app().stop() def save_model(self, obj): self.model_json = self.model.to_json() self.path_check("saved_models/") with open("saved_models/model.json", "w") as json_file: json_file.write(self.model_json) self.model.save_weights("saved_models/model.h5") print("Saved model to disk") def load_model(self, obj): json_file = open('saved_models/model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("saved_models/model.h5") print("Loaded model from disk") # evaluate loaded model on test data loaded_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) score = loaded_model.evaluate_generator(self.validation_generator) print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1] * 100))
model = Model(inputs=[y_in, y_err], outputs=h_dec) model.compile(optimizer='adam', loss=chi2(y_err)) #model.load_weights("model_weights/lstm_autoencoder_2020-01-09_18-20-21.h5") training_time_stamp = datetime.datetime.now( tz=pytz.timezone('Europe/London')).strftime("%Y-%m-%d_%H-%M-%S") CB = EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=100, verbose=1, mode='auto') MC = ModelCheckpoint('model_weights/model_{}.h5'.format(training_time_stamp), monitor='val_loss', mode="auto", save_best_only=True, verbose=1) history = model.fit_generator(training_generator, epochs=8000, verbose=2, callbacks=[MC, CB], validation_data=validation_generator) np.savetxt("training_history/loss_history-{}.txt".format(training_time_stamp), [ np.asarray(history.history["loss"]), np.asarray(history.history["val_loss"]) ], delimiter=",")
return cutout datagen = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, preprocessing_function=get_cutout()) datagen.fit(x_train) # Training history_red = red_model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, steps_per_epoch=x_train.shape[0] // batch_size, validation_data=(x_test, y_test), callbacks=[cp_callback, warm_up_lr]) # Plot plt.figure() plt.plot(history_red.history['accuracy'], label='train') plt.plot(history_red.history['val_accuracy'], label='test') max_idx = np.argmax(history_red.history['val_accuracy']) plt.plot(max_idx, history_red.history['val_accuracy'][max_idx], 'ks') plt.annotate(' epoch:' + str(max_idx) + '\n' + ' acc:' + str(history_red.history['val_accuracy'][max_idx]), xytext=(max_idx, history_red.history['val_accuracy'][max_idx]), xy=(max_idx, history_red.history['val_accuracy'][max_idx]))
def train(): epochs = 300 n_classes = 5 batch_train = 64 batch_test = 20 image_w = 224 image_h = 224 # 导入数据 path = '../../../../datasets/traindatas/flower_data/train' # 读入数据的路径 path_t = '../../../../datasets/traindatas/flower_data/val' ################################自写数据生成方法############################### ''' datagen = dataGenerator() # 实例化数据生成器 # 执行一次获取数据 datagen.readData(path) samples_num = datagen.num_samples train_datas = datagen.augment(True, path, batch_train, True, PreDataForVGG, image_w, image_h) # 导入数据并进行增强 datagen_t = dataGenerator() # 执行一次获取数据 datagen_t.readData(path_t) samples_num_t = datagen_t.num_samples val_datas = datagen_t.augment(False, path_t, batch_test, False, PreDataForVGG, image_w, image_h) ''' ############################################################################ params = dict( # rescale=1./255, rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, vertical_flip=True, preprocessing_function=PreDataForCaffe) # 配置参数 train_datagen = dataGenerator(**params) test_datagen = dataGenerator(preprocessing_function=PreDataForCaffe) train_generator = train_datagen.flow_from_directory(path, batch_size=batch_train, target_size=(image_w, image_h), shuffle=True) test_generator = test_datagen.flow_from_directory(path_t, batch_size=batch_test, target_size=(image_w, image_h), shuffle=False) train_num = train_generator.samples test_num = test_generator.samples ############################################################################ inputs = Input(shape=[image_w, image_h, 3]) base_model = resnet50( include_top=False, weights= '../../../../preweights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', input_tensor=inputs) # fla = Flatten()(base_model.output) fla = GlobalAveragePooling2D()(base_model.output) # x = Dense(1024, activation='relu')(fla) # x = Dropout(rate=0.5)(x) # x = Dense(128, activation='relu')(x) # x = Dropout(rate=0.5)(x) # x = Dense(128, activation='relu')(fla) # x = Dropout(rate=0.5)(x) output = Dense(n_classes, activation='softmax')(fla) model = Model(inputs=inputs, outputs=output) # 完成Model构建 base_model.trainable = False model.summary() # 编译模型 optimizer = optimizers.Adam(lr=0.0002) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc']) #设置callback callbacks = [ # EarlyStopping(patience=5, verbose=1), ModelCheckpoint('saveWeights/resnet18TrsModel.h5', monitor='val_acc', verbose=1, save_best_only=True, mode='max') ] # 训练 ''' model.fit_generator(train_datas, steps_per_epoch=math.ceil(samples_num/batch_train), epochs=epochs, validation_data=val_datas, validation_steps=math.ceil(samples_num_t/batch_test), verbose=2, callbacks=callbacks)''' model.fit_generator(train_generator, steps_per_epoch=math.ceil(train_num / batch_train), epochs=epochs, validation_data=test_generator, validation_steps=math.ceil(test_num / batch_test), verbose=2, callbacks=callbacks)
tru_02 = y_true[:, n_cls:-1] accu_01 = K.cast(K.equal(K.argmax(tru_01), K.argmax(out_01)), K.floatx()) accu_02 = K.cast(K.equal(K.argmax(tru_02), K.argmax(out_02)), K.floatx()) return (accu_01 + accu_02) / 2 model.compile(loss=dual_loss(n_cls), optimizer='adadelta', metrics=['mse']) traingen = MyDualV1(X_train, y_train, n_cls) model.fit_generator(traingen, epochs=10, verbose=1, workers=8, use_multiprocessing=True) def top_k_accuracy(y_score, y_true, k=5): argsrt = np.argsort(y_score)[:, -k:] top_k_bool = [] for i in range(len(y_true)): if y_true[i] in argsrt[i]: top_k_bool.append(1) else: top_k_bool.append(0) return np.mean(top_k_bool) # %% Model for Analysis out_01_model = inner_model
train_set, n_way=n_way, preprocessing=preprocessing, batch_size=16, target_size=(28, 28, 3), ) val_sequence = ProtoNetsSequence(val_set, batch_size=16, target_size=(28, 28, 3)) proto_nets.compile(optimizer='Adam', loss='categorical_crossentropy') Model.fit_generator( # to use patched fit_generator, see first cell proto_nets, train_sequence, validation_data=val_sequence, callbacks=callbacks, epochs=100, steps_per_epoch=1000, validation_steps=200, use_multiprocessing=True, ) #%% Prediction proto_nets = load_model('logs/proto_nets/best_weights.h5') encoder = proto_nets.get_layer('branch_model') head_model = proto_nets.get_layer('head_model') test_sequence = DeterministicSequence(test_set, batch_size=16, target_size=(28, 28, 3)) embeddings = encoder.predict_generator(test_sequence, verbose=1) k_shot = 5
"""#### Train Model The model is trained by feeding the data from the image generators """ import tensorflow as tf config = tf.ConfigProto(device_count = {'GPU': 0}) sess = tf.Session(config=config) from PIL import Image, ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True simple_model_history = simple_model.fit_generator( train_generator, steps_per_epoch=total_train_imgs // batch_size, # total_train_images = batch_size * steps epochs=28, validation_data=validation_generator, validation_steps=total_val_imgs // batch_size, # total_val_images = batch_size * steps verbose=2) """### 2. Using a Pre-trained Model (a.k.a Transfer Learning) **Feature Extraction** _General Idea_ 1. Take a model trained on a very large dataset, run it on smaller dataset, and extract the intermediate representations (features) that the model generates. These representations are frequently informative for the computer vision task at hand, even though the task may be quite different from the problem that the original model was trained on. 2. Use the Inception V3 model developed at Google, and pre-trained on ImageNet, a large dataset of web images (1.4M images and 1000 classes). 3. Use the output of the very last layer before the Flatten operation, the so-called "bottleneck layer." for Feature Extraction. The fully connected layers will be too specialized for the task the network was trained on, and thus the features learned by these layers won't be very useful for a new task. The bottleneck features, retain much generality.
# As an optimizer, here we will use SGD # with a very low learning rate (0.00001) model.compile(loss='binary_crossentropy', optimizer=SGD( lr=0.00001, momentum=0.9), metrics=['acc']) callbacks = [] callbacks.append(tf.keras.callbacks.ModelCheckpoint('./checkpoint.h5', save_best_only=True, save_weights_only=False)) history = model.fit_generator( train_generator, steps_per_epoch=100, epochs=50, validation_data=validation_generator, validation_steps=50, verbose=2) # Plot training history import matplotlib.pyplot as plt acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) plt.plot(epochs, acc, 'bo', label='Training acc')
train_generator = train_datagen.flow_from_directory(Training_dir, batch_size=20, class_mode='categorical', target_size=(300, 300)) Validation_dir = 'C:/Users/91797/Desktop/project_mask/withmask-withoutmask_2/testing' validation_datagen = ImageDataGenerator(rescale=1.0 / 255) validation_generator = validation_datagen.flow_from_directory( Validation_dir, batch_size=20, class_mode='categorical', target_size=(300, 300)) history = model.fit_generator(train_generator, epochs=1, verbose=1, validation_data=validation_generator) acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) plt.plot(epochs, acc, 'r', "Training Accuracy") plt.plot(epochs, val_acc, 'b', "Validation Accuracy") plt.title('Training and validation accuracy') plt.figure() plt.plot(epochs, loss, 'r', "Training Loss")