def __init__(self, attrData, config): super(MainVggAction, self).__init__() src = config.get('IMAGE') # Define hyperparameters INPUT_SIZE = 256 #Change this to 48 if the code is taking too long to run BATCH_SIZE = 16 STEPS_PER_EPOCH = 200 EPOCHS = 3 vgg16 = VGG16(include_top=False, weights='imagenet', input_shape=(INPUT_SIZE, INPUT_SIZE, 3)) # Freeze the pre-trained layers for layer in vgg16.layers: layer.trainable = False # Add a fully connected layer with 1 node at the end input_ = vgg16.input output_ = vgg16(input_) last_layer = Flatten(name='flatten')(output_) last_layer = Dense(1, activation='sigmoid')(last_layer) model = Model(input=input_, output=last_layer) model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) training_data_generator = ImageDataGenerator(rescale = 1./255) testing_data_generator = ImageDataGenerator(rescale = 1./255) training_set = training_data_generator.flow_from_directory(src+'Train/', target_size = (INPUT_SIZE, INPUT_SIZE), batch_size = BATCH_SIZE, class_mode = 'binary') test_set = testing_data_generator.flow_from_directory(src+'Test/', target_size = (INPUT_SIZE, INPUT_SIZE), batch_size = BATCH_SIZE, class_mode = 'binary') print(""" Caution: VGG16 model training can take up to an hour if you are not running Keras on a GPU. If the code takes too long to run on your computer, you may reduce the INPUT_SIZE paramater in the code to speed up model training. """) model.fit_generator(training_set, steps_per_epoch = STEPS_PER_EPOCH, epochs = EPOCHS, verbose=1) score = model.evaluate_generator(test_set, steps=100) for idx, metric in enumerate(model.metrics_names): print("{}: {}".format(metric, score[idx])) model_json = model.to_json() with open("model_food_vgg.json", "w") as json_file: json_file.write(model_json) model.save_weights("model_food_vgg.h5") print("Saved model to disk")
def main(): model_path = "./model.h5" checkpoint = ModelCheckpoint(model_path, monitor='val_loss', mode='min', save_best_only='True', verbose=1) model = VGG16(include_top=False, input_shape=(224, 224, 3)) for layer in model.layers: layer.trainable = False flat1 = Flatten()(model.layers[-1].output) class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1) output = Dense(1, activation='sigmoid')(class1) model = Model(inputs=model.inputs, outputs=output) opt = SGD(lr=0.001, momentum=0.9) model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy']) datagen = ImageDataGenerator(featurewise_center=True) datagen.mean = [123.68, 116.779, 103.939] train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/', class_mode='binary', batch_size=16, target_size=(224, 224)) test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/', class_mode='binary', batch_size=16, target_size=(224, 224)) model.fit_generator(train_it, steps_per_epoch=len(train_it), validation_data=test_it, validation_steps=len(test_it), epochs=10, callbacks=[checkpoint], verbose=1) user_input = input("Do you want to evaluate the model on the test set?\ (y/n)") if user_input == 'y': score = model.evaluate_generator(test_it, steps=len(test_it), verbose=1) print("loss: ", score[0]) print('accuracy: ', score[1])
def model(train_gen, valid_gen): n_classes = 196 steps_per_epoch = 204 validation_steps = 51 channels = 3 output = "label_bbox" # Tensorboard root_logdir = os.path.join(os.curdir, 'logs') run_id = time.strftime(f"run_%Y_%m_%d-%H_%M_%S") run_logdir = os.path.join(root_logdir, run_id) tensorboard = TensorBoard(run_logdir) # Early Stopping early_stopping = EarlyStopping(patience=15, restore_best_weights=True) # Model Checkpoints checkpoint = ModelCheckpoint( filepath=f'checkpoints/' + \ 'epoch.{epoch:02d}_val_loss.{val_loss:.6f}.h5', verbose=1, save_best_only=True) #Reduce LR on Plateau reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=5, min_lr=1e-4, verbose=1) callbacks = [tensorboard, early_stopping, checkpoint, reduce_lr] DefaultConv2D = partial( Conv2D, kernel_size=3, kernel_initializer="he_normal", kernel_regularizer=l2({{uniform(1e-5, 1)}}), # activation="relu", padding="same") input = Input(shape=(224, 224, channels)) # ============================================================================= # conv_1a = DefaultConv2D(filters=64, # kernel_size=5, # strides=2)(input) # norm_1a = BatchNormalization()(conv_1a) # relu_1a = Activation(activation="relu")(norm_1a) # # max_pool_1 = MaxPooling2D(pool_size=2)(relu_1a) # # conv_1b = DefaultConv2D(filters=64)(max_pool_1) # norm_1b = BatchNormalization()(conv_1b) # relu_1b = Activation(activation="relu")(norm_1b) # conv_1c = DefaultConv2D(filters=64)(relu_1b) # norm_1c = BatchNormalization()(conv_1c) # relu_1c = Activation(activation="relu")(norm_1c) # # max_pool_2 = MaxPooling2D(pool_size=2)(relu_1c) # # conv_2a = DefaultConv2D(filters=128)(max_pool_2) # norm_2a = BatchNormalization()(conv_2a) # relu_2a = Activation(activation="relu")(norm_2a) # conv_2b = DefaultConv2D(filters=128)(relu_2a) # norm_2b = BatchNormalization()(conv_2b) # relu_2b = Activation(activation="relu")(norm_2b) # # max_pool_3 = MaxPooling2D(pool_size=2)(relu_2b) # # flatten = Flatten()(max_pool_3) # ============================================================================= choice_activation = {{choice(['relu', 'leaky_relu'])}} conv = DefaultConv2D(filters=64, strides=2)(input) norm = BatchNormalization()(conv) if choice_activation == 'relu': act = Activation(activation="relu")(norm) else: act = LeakyReLU(0.2)(norm) conv = DefaultConv2D(filters=64)(act) norm = BatchNormalization()(conv) if choice_activation == 'relu': act = Activation(activation="relu")(norm) else: act = LeakyReLU(0.2)(norm) choice_pool = {{choice(['max', 'avg'])}} if choice_pool == 'max': x = MaxPooling2D(pool_size=2)(act) else: x = AveragePooling2D(pool_size=2)(act) for filters in [64] * 3: x = Residual(filters, activation=choice_activation)(x) res_block_depth_1 = {{choice([2, 3])}} x = Residual(128, strides=2, activation=choice_activation)(x) for filters in [128] * res_block_depth_1: x = Residual(filters, activation=choice_activation)(x) res_block_depth_2 = {{choice([4, 5])}} x = Residual(256, strides=2, activation=choice_activation)(x) for filters in [256] * res_block_depth_2: x = Residual(filters, activation=choice_activation)(x) x = Residual(512, strides=2, activation=choice_activation)(x) for filters in [512] * 2: x = Residual(filters, activation=choice_activation)(x) x = GlobalAvgPool2D()(x) drop = Dropout({{uniform(0, .5)}})(x) dense = Dense(512)(drop) norm = BatchNormalization()(dense) if choice_activation == 'relu': act = Activation(activation="relu")(norm) else: act = LeakyReLU(0.2)(norm) # If we choose 'one', add an additional dense layer choice_dense = {{choice(['yes', 'no'])}} if choice_dense == 'yes': drop = Dropout({{uniform(0, .5)}})(act) dense = Dense(256)(drop) norm = BatchNormalization()(dense) if choice_activation == 'relu': act = Activation(activation="relu")(norm) else: act = LeakyReLU(0.2)(norm) drop = Dropout({{uniform(0, .5)}})(act) class_output = Dense(n_classes, activation="softmax", name="labels")(drop) bbox_output = Dense(units=4, name="bbox")(drop) # Optimizer sgd = SGD(lr={{uniform(.0001, .3)}}) adam = Adam(lr={{uniform(.0001, .3)}}) choice_opt = {{choice(['adam', 'sgd'])}} if choice_opt == 'adam': optimizer = adam else: optimizer = sgd model = None if output == "bbox": model = Model(inputs=input, outputs=bbox_output) model.compile(loss="msle", optimizer=optimizer, metrics=["accuracy"]) elif output == "label": model = Model(inputs=input, outputs=class_output) model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"]) else: model = Model(inputs=input, outputs=[class_output, bbox_output]) model.compile(loss=["categorical_crossentropy", "msle"], loss_weights=[.8, .2], optimizer=optimizer, metrics=["accuracy"]) model.fit(train_gen, epochs=400, steps_per_epoch=steps_per_epoch, validation_data=valid_gen, validation_steps=validation_steps, callbacks=callbacks, verbose=1) # ============================================================================= # #print(model.metrics_names) # # ['loss', 'labels_loss', 'bbox_loss', 'labels_acc', 'bbox_acc'] # validation_loss = np.amin(result.history['loss']) # print('Best validation loss of epoch:', validation_loss) # ============================================================================= results = model.evaluate_generator(valid_gen, steps=validation_steps, verbose=0) validation_loss = results[0] print('val_labels_acc', results[3]) print('val_bbox_acc', results[4]) print('Validation Loss:', validation_loss) return {'loss': validation_loss, 'status': STATUS_OK, 'model': model}
) # fit the model r = model.fit_generator( train_generator, validation_data=valid_generator, epochs=10, steps_per_epoch=int(np.ceil(len(image_files) / batch_size)), validation_steps=int(np.ceil(len(valid_image_files) / batch_size)), ) # create a 2nd train generator which does not use data augmentation # to get the true train accuracy train_generator2 = gen_test.flow_from_directory( train_path, target_size=IMAGE_SIZE, batch_size=batch_size, ) model.evaluate_generator( train_generator2, steps=int(np.ceil(len(image_files) / batch_size))) plt.plot(r.history['loss'], label='train loss') plt.plot(r.history['val_loss'], label='val loss') plt.legend() plt.show() plt.plot(r.history['accuracy'], label='train acc') plt.plot(r.history['val_accuracy'], label='val acc') plt.legend() plt.show()
custom_Xception_model.compile(loss= pseudo_huber ,optimizer='adadelta',metrics=['accuracy']) hist = custom_Xception_model.fit_generator(generator=train_generator, steps_per_epoch=STEP_SIZE_TRAIN, validation_data=valid_generator, validation_steps=STEP_SIZE_VALID, epochs=2) print ("[STATUS] start time - {}".format(datetime.datetime.now().strftime("%H:%M"))) print("Noiseless") print("0") test_loss, test_acc =custom_Xception_model.evaluate_generator(generator=valid_generator,steps=STEP_SIZE_VALID, verbose=1) print("Gaussian") print("5") test_loss1, test_acc1 =custom_Xception_model.evaluate_generator(generator=valid_generator1,steps=STEP_SIZE_VALID, verbose=1) print("10") test_loss2, test_acc2 =custom_Xception_model.evaluate_generator(generator=valid_generator2,steps=STEP_SIZE_VALID, verbose=1) print("15") test_loss3, test_acc3 =custom_Xception_model.evaluate_generator(generator=valid_generator3,steps=STEP_SIZE_VALID, verbose=1) print("20") test_loss4, test_acc4 =custom_Xception_model.evaluate_generator(generator=valid_generator4,steps=STEP_SIZE_VALID, verbose=1) print("25") test_loss5, test_acc5 =custom_Xception_model.evaluate_generator(generator=valid_generator5,steps=STEP_SIZE_VALID, verbose=1)
mc = ModelCheckpoint('C:/data/MC/best_LT_vision2_LT.hdf5', save_best_only=True, mode='auto') es = EarlyStopping(monitor='val_loss', patience=5, verbose=1, mode='auto') rl = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, verbose=1, mode='auto') model.fit_generator(train_generator, epochs=100, steps_per_epoch=len(x_train) / 32, verbose=1, validation_data=valid_generator, callbacks=[es, rl, mc]) model.save('C:/data/h5/LT_vision_6.h5') model.save_weights('C:/data/h5/LT_vision_model2_6.h5') # model = load_model('C:/data/h5/LT_vision_model2_5_mobileNet.h5') # model.load_weights('C:/data/h5/LT_vision_5_mobileNet.h5') #EVAL loss, acc = model.evaluate_generator(valid_generator) print("loss : ", loss) print("acc : ", acc) result = model.predict(test_generator, verbose=True) sub = pd.read_csv('C:/data/LPD_competition/sample.csv') sub['prediction'] = np.argmax(result, axis=1) sub.to_csv('C:/data/LPD_competition/pred3.csv', index=False)
axes[0].set_title('Training and Validation Accuracy') axes[0].legend(loc='best') axes[1].plot(epochs, loss, 'r-', label='Training Loss') axes[1].plot(epochs, val_loss, 'b--', label='Validation Loss') axes[1].set_title('Training and Validation Loss') axes[1].legend(loc='best') plt.show() plot_training(history) # %% # Prediction accuracy on train data score = model.evaluate_generator(train_generator, verbose=1) print("Prediction accuracy on train data =", score[1]) # %% # Prediction accuracy on testing data score = model.evaluate_generator(valid_generator, verbose=1) print("Prediction accuracy on CV data =", score[1]) # %% # Classification report and confusion matrix tick_labels = artists_top_name.tolist() def showClassficationReport_Generator(model, valid_generator, STEP_SIZE_VALID): # Loop on each generator batch and predict y_pred, y_true = [], []
callbacks_list = [checkpoint, reduce_lr] history = model.fit_generator(train_batches, steps_per_epoch=train_steps, class_weight=class_weights, validation_data=valid_batches, validation_steps=val_steps, epochs=30, verbose=1, callbacks=callbacks_list) val_loss, val_cat_acc, val_top_2_acc, val_top_3_acc = \ model.evaluate_generator(test_batches, steps=len(df_val)) print('val_loss:', val_loss) print('val_cat_acc:', val_cat_acc) print('val_top_2_acc:', val_top_2_acc) print('val_top_3_acc:', val_top_3_acc) model.load_weights('model.h5') val_loss, val_cat_acc, val_top_2_acc, val_top_3_acc = \ model.evaluate_generator(test_batches, steps=len(df_val)) print('val_loss:', val_loss) print('val_cat_acc:', val_cat_acc) print('val_top_2_acc:', val_top_2_acc)
def main(): CLASSES = 24 TRAIN_DIR = 'training_images' VALIDATION_DIR = 'validation_images' TEST_DIR = 'testing_images' NUM_TRAIN = 20592 NUM_VAL = 6863 NUM_TEST = 7172 # setup InceptionV3 model and weights while leaving out top layer base_model = InceptionV3(weights='imagenet', include_top=False) # Add our custom classification layer, preserving the original Inception-v3 architecture # but adapting the output to our number of classes. # We use a GlobalAveragePooling2D preceding the fully-connected Dense layer of 2 outputs. x = base_model.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(0.4)(x) predictions = Dense(CLASSES, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) # freeze all our base_model layers and train the last ones for layer in base_model.layers: layer.trainable = False # Compile model using an RMSProp optimizer # with the default learning rate of 0.001, # and a categorical_crossentropy — used in multiclass classification tasks — as loss function. model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # Rescale data to 84x84, as InceptionV3 requires image sizes at least 75x75 SCALED_WIDTH = 84 SCALED_HEIGHT = 84 BATCH_SIZE = 64 train_datagen = ImageDataGenerator(rescale=1. / 255) validation_datagen = ImageDataGenerator(rescale=1. / 255) test_datagen = ImageDataGenerator(rescale=1. / 255) # Use image generators to rescale images into batches train_generator = train_datagen.flow_from_directory( TRAIN_DIR, target_size=(SCALED_HEIGHT, SCALED_WIDTH), batch_size=BATCH_SIZE, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory( VALIDATION_DIR, target_size=(SCALED_HEIGHT, SCALED_WIDTH), batch_size=BATCH_SIZE, class_mode='categorical') test_generator = test_datagen.flow_from_directory( TEST_DIR, target_size=(SCALED_HEIGHT, SCALED_WIDTH), batch_size=BATCH_SIZE, class_mode='categorical') # perform transfer learning on model EPOCHS = 8 STEPS_PER_EPOCH = math.ceil(NUM_TRAIN / BATCH_SIZE) VALIDATION_STEPS = math.floor(NUM_VAL / BATCH_SIZE) history = model.fit_generator(generator=train_generator, epochs=EPOCHS, steps_per_epoch=STEPS_PER_EPOCH, validation_data=validation_generator, validation_steps=VALIDATION_STEPS) # Evaluate the model on the test data using `evaluate` print('\n# Evaluate on test data') results = model.evaluate_generator(test_generator) print('test loss, test acc:', results) # save model MODEL_FILE = 'baseline_keras_inception_v3.h5' model.save(MODEL_FILE)
Learning_rate = rate2[i] trainingFeatures = (np.reshape( trainingFeatures, [7 * 7 * 512, trainingFeatures.shape[0]])).conj().T (InputWeight11, InputWeight22, YYM, BB) = TELM33_new.telm33_new(trainingFeatures, train_label.conj().T, C, InputWeight1[0].T, InputWeight2[0].T, InputWeight3[0].T, Learning_rate, Learning_rate) InputWeight1[0] = InputWeight11 InputWeight2[0] = InputWeight22 InputWeight3[0] = YYM net.layers[20].set_weights(InputWeight1) net.layers[22].set_weights(InputWeight2) net.layers[24].set_weights(InputWeight3) #net.fit(trainData, train_label, epochs = 1, batch_size = 16) net.fit_generator(train_generator, steps_per_epoch=3125, epochs=1) #, #validation_data=validation_generator, #validation_steps=124) net.save(os.path.join('outputs', 'checkpoint_vgg16_telm33_' + str(i))) score = net.evaluate_generator(test_generator, steps=16, verbose=1) print('Test loss:', score[0]) print('Test accuracy:', score[1]) end = time.time() print(end - start)
history = models.fit_generator(train_gen, steps_per_epoch=train_steps, class_weight=class_weights, validation_data=val_gen, validation_steps=val_steps, epochs=100, verbose=1, callbacks=callbacks_list) # Get the best epoch from the training log df = pd.read_csv('training_log.csv') best_acc = df['val_categorical_accuracy'].max() # display the row with the best accuracy print() display(df[df['val_categorical_accuracy'] == best_acc]) # Evaluate the model models.load_weights('model.h5') _, accuracy = models.evaluate_generator(test_gen, steps=len(df_val)) print('Accuracy :', accuracy) # Plot the Training Curves acc = history.history['categorical_accuracy'] val_acc = history.history['val_categorical_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(acc) + 1) plt.plot(epochs, loss, 'g', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and validation loss') plt.xlabel("Epochs") plt.ylabel("Loss")
axes[0].plot(epochs, val_acc, 'b--', label='Validation Accuracy') axes[0].set_title('Training and Validation Accuracy') axes[0].legend(loc='best') axes[1].plot(epochs, loss, 'r-', label='Training Loss') axes[1].plot(epochs, val_loss, 'b--', label='Validation Loss') axes[1].set_title('Training and Validation Loss') axes[1].legend(loc='best') plt.savefig("training_plot.png") plot_training(history) # Prediction accuracy on train data score = model.evaluate_generator(train_generator) print("Prediction accuracy on train data =", score[1]) tick_labels = artists_top_name.tolist() def showClassficationReport_Generator(model, valid_generator, STEP_SIZE_VALID): # Loop on each generator batch and predict y_pred, y_true = [], [] for i in range(STEP_SIZE_VALID): (X, y) = next(valid_generator) y_pred.append(model.predict(X)) y_true.append(y) # Create a flat list for y_true and y_pred y_pred = [subresult for result in y_pred for subresult in result]
) # train the model # define callback function early_stopping = EarlyStopping( monitor='val_loss', patience=10, ) reduce_lr = ReduceLROnPlateau( monitor='val_loss', factor=0.1, patience=5, ) model.fit_generator(train_generator, steps_per_epoch=train_image_numbers // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=validation_steps, callbacks=[early_stopping, reduce_lr]) # test the model test_metrics = model.evaluate_generator( test_generator, steps=1, ) for i in zip(model.metrics_names, test_metrics): print(i) # save the model model.save(model_filepath)
plt.legend(['train_loss', 'validation_loss'], loc='best') plt.show() plot_accuracy(history,'FOOD') plot_loss(history,'FOOD') for layer in model.layers: print(layer.name) validation_generator = valid_datagen.flow_from_directory( validation_data_dir, target_size=(img_height, img_width), batch_size=1, class_mode='categorical') model.evaluate_generator(generator=validation_generator, steps= nb_validation_samples //batch_size) test_generator.reset() pred=model.predict_generator(test_generator, steps=test_generator.n//test_generator.batch_size, verbose=1) predicted_class_indices=np.argmax(pred,axis=1) print(predicted_class_indices) labels = (train_generator.class_indices) labels = dict((v,k) for k,v in labels.items()) predictions = [labels[k] for k in predicted_class_indices] import pandas as pd filenames=test_generator.filenames
y_pred_memory = Dense(N, activation='softmax')(y_pred_memory) y_pred = y_pred_memory[:, -1, :N] adam = tf.keras.optimizers.Adam(learning_rate=0.001) model = Model(inputs=inp, outputs=y_pred) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) history = model.fit_generator(data_generator.sample_batch('train', B), epochs=150, steps_per_epoch=50, validation_data=data_generator.sample_batch( 'val', B), validation_steps=B) print( model.evaluate_generator(data_generator.sample_batch('test', B), steps=50)) fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(6, 4)) axs[0].plot(history.history['accuracy']) axs[0].plot(history.history['val_accuracy']) axs[0].set_title('Model accuracy') axs[0].set_ylabel('Accuracy') axs[0].set_xlabel('Epoch') axs[0].legend(['Train', 'Val'], loc='upper left') axs[1].plot(history.history['loss']) axs[1].plot(history.history['val_loss']) axs[1].set_title('Model loss') axs[1].set_ylabel('Loss') axs[1].set_xlabel('Epoch') axs[1].legend(['Train', 'Val'], loc='upper left')
from matplotlib import pyplot as plt plt.style.use('dark_background') from sklearn.datasets import load_breast_cancer from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder from sklearn.metrics import roc_curve from sklearn.metrics import auc from sklearn.metrics import precision_recall_curve from sklearn.metrics import precision_score from sklearn.metrics import recall_score from sklearn.metrics import f1_score from sklearn.metrics import average_precision_score from inspect import signature acc = model.evaluate_generator(traingen, steps=2, verbose=1) print(acc) pred_train= model.predict(traingen) scores = model.evaluate(traingen,verbose=0) print('Accuracy on training data: {}% \n Error on training data: {}'.format(scores[1], 1 - scores[1])) pred_test= model.predict(validationgen) scores2 = model.evaluate(validationgen,verbose=0) print('Accuracy on test data: {}% \n Error on test data: {}'.format(scores2[1], 1 - scores2[1])) from tensorflow.keras import backend as K import tensorflow as tf import os # This line must be executed before loading Keras model. K.set_learning_phase(0)
plt.plot(history.history['val_accuracy']) plt.title('Model Accuracy Progress During Cross-Validation') plt.xlabel('Epoch') plt.ylabel('Validation Accuracy') plt.legend(['Validation Accuracy']) ## Test Data test_directory = '/content/drive/My Drive/Data Science for Business/Test' os.listdir(test_directory) test_gen = ImageDataGenerator(rescale = 1./255) test_generator = test_gen.flow_from_directory(batch_size = 40, directory= test_directory, shuffle= True, target_size=(256,256), class_mode= 'categorical') evaluate = model.evaluate_generator(test_generator, steps = test_generator.n // 4, verbose =1) print('Accuracy Test : {}'.format(evaluate[1])) from sklearn.metrics import confusion_matrix, classification_report, accuracy_score prediction = [] original = [] image = [] for i in range(len(os.listdir(test_directory))-1): for item in os.listdir(os.path.join(test_directory,str(i))): img= cv2.imread(os.path.join(test_directory,str(i),item)) img = cv2.resize(img,(256,256)) image.append(img) img = img / 255
max_queue_size=15, workers=8, callbacks=callbacks_list) plot_history(history, save_fig=True, save_path=os.path.join('data', 'models-new', 'bottleneck.png')) print("\n") bn_end_time = datetime.now() print("[Info] Model Bottlenecking completed at: {}".format(bn_end_time)) bn_duration = bn_end_time - bn_start_time print("[Info] Total time for Bottlenecking: {}".format(bn_duration)) (eval_loss, eval_accuracy) = model.evaluate_generator( validation_generator, steps=validation_steps) print("\n") print("[INFO] accuracy: {:.2f}%".format(eval_accuracy * 100)) print("[INFO] Loss: {}".format(eval_loss)) print("[Info] Saving initial model to disk: {}".format(initial_model_path)) model.save(initial_model_path) else: model = load_model(initial_model_path) if run_finetune: # reset our data generators
class Faves_model(): def __init__(self, lr=0.00005): self.lr = lr self.class_list = ["Original", "Tampered"] self.fc_layers = [1024, 1024] self.build_finetune_model(0.5, self.fc_layers, len(self.class_list)) self.model_ready = None def build_finetune_model(self, dropout, fc_layers, num_classes): base_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) for layer in base_model.layers: layer.trainable = False x = base_model.output x = Flatten()(x) for fc in fc_layers: x = Dense(fc, activation='relu')(x) x = Dropout(dropout)(x) predictions = Dense(num_classes, activation='softmax')(x) self.finetune_model = Model(inputs=base_model.input, outputs=predictions) adam = Adam(lr=self.lr) self.finetune_model.compile(adam, loss='binary_crossentropy', metrics=['accuracy']) def train_model(self, epochs=100, data_dir='./drive/My Drive/flickr_dataset'): filepath = "./checkpoints/" + "faves" + "_model_weights2.h5" if not os.path.exists("./checkpoints"): os.makedirs("./checkpoints") checkpoint = ModelCheckpoint(filepath, monitor="val_acc", verbose=1, save_best_only=True) early_stopping = EarlyStopping(monitor='val_acc', min_delta=0.05, patience=0, verbose=0, mode='auto', baseline=None, restore_best_weights=False) callbacks_list = [checkpoint, early_stopping] train_generator, validation_generator, test_generator = getGenerators( data_dir, 25, 2, 2) history = self.finetune_model.fit_generator( train_generator, epochs=epochs, workers=8, shuffle=True, callbacks=callbacks_list, validation_data=validation_generator, validation_freq=5, use_multiprocessing=True) print("testing") print(self.finetune_model.evaluate_generator(test_generator)) self.finetune_model.save_weights("./model_weights.h5") plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig('./accuracy.jpg') # summarize history for loss plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig('./loss.jpg') self.model_ready = True def load_finetune_model(self, model_path): self.finetune_model.load_weights(model_path) self.model_ready = True def predict(self, images): if self.model_ready: self.finetune_model.predict(images) else: raise Exception("train or load the model first") def faves_scorer(self, images): score = 0.0 if self.model_ready: predictions = self.finetune_model.predict(img, steps=1)[1] for p in predictions: score += p[1] return score / float(len(images)) else: raise Exception("train or load the model first")
filepath="transferlearning_weightsVGG16For2New.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max') history = model.fit_generator(training_set, steps_per_epoch=5208/batch_size, #no. of images on training set/batch size epochs=10, use_multiprocessing = False, validation_data = test_set, validation_steps = 624/batch_size, workers = 8, max_queue_size = 1000, callbacks=[lr_reduce,checkpoint]) model.load_weights("transferlearning_weightsVGG16For2.hdf5") model.evaluate_generator(test_set, workers = 8, max_queue_size = 1000) y_predict = model.predict_generator(test_set, workers = 8, max_queue_size = 1000) y_pred = [1 if i > 0.5 else 0 for i in y_predict] y_true = [0 if i<234 else 1 for i in range(624)] listData2 = listData() cm = confusion_matrix(y_true, y_pred) cm5 = [] cm5 = confusion_matrix(y_pred, y_true) cmScore = (cm5[0,0]+cm5[1,1])/(cm5[0,0] + cm5[0,1] + cm5[1,0] + cm5[1,1]) #log = log_loss(y_pred, y_true) ps = precision_score(y_pred, y_true) rs = recall_score(y_pred, y_true)
class ClassificationModel: """ [Tensorflow Imagenet Model] # Class created using model provided in keras.applications """ def __init__(self, class_list, img_width, img_height, batch_size) -> None: backend.clear_session() self.class_list = class_list self.img_width, self.img_height = img_width, img_height # batch_size can be up to 16 based on GPU 4GB (not available for 32) self.batch_size = batch_size self.model = None self.train_data = None self.validation_data = None self.num_train_data = None self.day_now = time.strftime("%Y%m%d%H", time.localtime(time.time())) self.checkpointer = None self.csv_logger = None self.history = None self.model_name = "inception_v3" def generate_train_val_data(self, data_dir="dataset/train/"): """ # Create an ImageDataGenerator by dividing the train and validation set # by 0.8/0.2 based on the train dataset folder. # train : 60600 imgs / validation : 15150 imgs """ num_data = 0 for root, dirs, files in os.walk(data_dir): if files: num_data += len(files) self.num_train_data = num_data _datagen = image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, validation_split=0.2, ) self.train_data = _datagen.flow_from_directory( data_dir, target_size=(self.img_height, self.img_width), batch_size=self.batch_size, class_mode="categorical", subset="training", ) self.validation_data = _datagen.flow_from_directory( data_dir, target_size=(self.img_height, self.img_width), batch_size=self.batch_size, class_mode="categorical", subset="validation", ) def set_model(self, model_name="inception_v3"): """ # This is a function that composes a model, and proceeds to compile. # [Reference] - https://www.tensorflow.org/api_docs/python/tf/keras/applications/inception_v3 """ if model_name == "inception_v3": self.model = InceptionV3(weights="imagenet", include_top=False) elif model_name == "mobilenet_v2": self.model = MobileNetV2(weights="imagenet", include_top=False) self.model_name = model_name x = self.model.output x = GlobalAveragePooling2D()(x) x = Dense(128, activation="relu")(x) x = Dropout(0.2)(x) pred = Dense( len(self.class_list), kernel_regularizer=regularizers.l2(0.005), activation="softmax", )(x) self.model = Model(inputs=self.model.input, outputs=pred) self.model.compile( optimizer=SGD(lr=0.0001, momentum=0.9), loss="categorical_crossentropy", metrics=["accuracy"], ) return 1 def train(self, epochs=10): """ # Training-related environment settings (log, checkpoint) and training """ train_samples = self.num_train_data * 0.8 val_samples = self.num_train_data * 0.2 os.makedirs(os.path.join('models', 'checkpoint'), exist_ok=True) self.checkpointer = ModelCheckpoint( filepath= f"models/checkpoint/{self.model_name}_checkpoint_{self.day_not}.hdf5", verbose=1, save_best_only=True, ) os.makedirs(os.path.join('logs', 'training'), exist_ok=True) self.csv_logger = CSVLogger( f"logs/training/{self.model_name}_history_model_{self.day_now}.log" ) self.history = self.model.fit_generator( self.train_data, steps_per_epoch=train_samples // self.batch_size, validation_data=self.validation_data, validation_steps=val_samples // self.batch_size, epochs=epochs, verbose=1, callbacks=[self.csv_logger, self.checkpointer], ) self.model.save(f"models/{self.model_name}_model_{self.day_now}.hdf5") return self.history def evaluation(self, batch_size=16, data_dir="test/", steps=5): """ # Evaluate the model using the data in data_dir as a test set. """ if self.model is not None: test_datagen = image.ImageDataGenerator(rescale=1.0 / 255) test_generator = test_datagen.flow_from_directory( data_dir, target_size=(self.img_height, self.img_width), batch_size=batch_size, class_mode="categorical", ) scores = self.model.evaluate_generator(test_generator, steps=steps) print("Evaluation data: {}".format(data_dir)) print("%s: %.2f%%" % (self.model.metrics_names[1], scores[1] * 100)) else: print("Model not found... : load_model or train plz") def prediction(self, img_path, show=True, save=False): """ # Given a path for an image, the image is predicted and displayed through plt. """ target_name = img_path.split(".")[0] target_name = target_name.split("/")[-1] save_time = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) img = image.load_img(img_path, target_size=(self.img_height, self.img_width)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img /= 255.0 if self.model is not None: pred = self.model.predict(img) index = np.argmax(pred) self.class_list.sort() pred_value = self.class_list[index] if show: plt.imshow(img[0]) plt.axis("off") plt.title("prediction: {}".format(pred_value)) print("[Model Prediction] {}: {}".format( target_name, pred_value)) plt.show() if save: os.makedirs('results', exist_ok=True) plt.savefig( f"results/{self.model_name}_example_{target_name}_{save_time}.png" ) return 1 else: print("Model not found... : load_model or train plz") return 0 def load(self): """ # If an already trained model exists, load it. """ model_path = "models/checkpoint/" os.makedirs(model_path, exist_ok=True) model_list = os.listdir(model_path) if model_list: h5_list = [file for file in model_list if file.endswith(".hdf5")] h5_list.sort() backend.clear_session() self.model = load_model(model_path + h5_list[-1], compile=False) print("Model loaded...: ", h5_list[-1]) self.model.compile( optimizer=SGD(lr=0.0001, momentum=0.9), loss="categorical_crossentropy", metrics=["accuracy"], ) return 1 else: print("Model not found... : train plz") return 0 def show_accuracy(self): """ # Shows the accuracy graph of the training history. # TO DO: In the case of a loaded model, a function to find and display the graph is added """ if self.history is not None: title = f"model_accuracy_{self.day_now}" plt.title(title) plt.plot(self.history.history["accuracy"]) plt.plot(self.history.history["val_accuracy"]) plt.ylabel("accuracy") plt.xlabel("epoch") plt.legend(["train_acc", "val_acc"], loc="best") plt.show() os.makedirs('results', exist_ok=True) plt.savefig( f"results/accuracy_{self.model_name}_model_{self.day_now}.png") else: print("Model not found... : load_model or train plz") def show_loss(self): """ # Shows the loss graph of the training history. # TO DO: In the case of a loaded model, a function to find and display the graph is added """ if self.history is not None: title = f"model_loss_{self.day_now}" plt.title(title) plt.plot(self.history.history["loss"]) plt.plot(self.history.history["val_loss"]) plt.ylabel("loss") plt.xlabel("epoch") plt.legend(["train_loss", "val_loss"], loc="best") plt.show() os.makedirs('results', exist_ok=True) plt.savefig( f"results/loss_{self.model_name}_model_{self.day_now}.png". format()) else: print("Model not found... : load_model or train plz")
class kerasUNet(object): """kerasUNet Skeleton to hold network parameters, network archictecture, and functions. Contains: build_network batch_generator train_network rand_batch_generator det_batch_generator evaluate_network save_model load_model """ def __init__(self, index_df, object_df, param_file='basic.param'): self.index = index_df self.param = NetworkParam(param_file) self.train_dict, self.val_dict = img_util.get_training_dicts( index_df, self.param.size_buckets, self.param.val_fold) self.class_lookup = img_util.get_common_class_index( object_df, Nclasses=self.param.Nclasses) Ntrain = 0 for key, file_list in self.train_dict.items(): Ntrain += len(file_list) Nval = 0 for key, file_list in self.val_dict.items(): Nval += len(file_list) self.param.train_steps = np.ceil(Ntrain / self.param.batch_size) self.param.val_steps = np.ceil(Nval / self.param.batch_size) np.random.seed(self.param.seed) keras.backend.clear_session() self.build_network() def load_param(self, param_file): param_dict = util.load_param(param_file) #Now overwrite any values with the for key, value in kwargs.items(): setattr(self, key, value) def build_network(self): #Input_shape = (W,H) #down1_shape = (W/2, H/2) inputs = Input(shape=(None, None, 3)) down1 = DownBlock(inputs, filters=4, kernel_size=3, dropout=self.param.dropout, scope='Down1') #down2_shape = (W/4, H/4) down2 = DownBlock(down1, filters=8, kernel_size=3, dropout=self.param.dropout, scope='Down2') #down3_shapes: (W/8, H/8) down3 = DownBlock(down2, filters=16, kernel_size=3, dropout=self.param.dropout, scope='Down3') #middle layers: mid_shape = (W/8, H/8) mid = MidBlock(down3, filters=16, kernel_size=3, dropout=self.param.dropout, scope='Mid') #1st step up. up2_shape = (W/4, H/4) up3 = UpBlock(mid, filters=32, kernel_size=3, dropout=self.param.dropout, scope='Up3') up3b = SkipConnection(down2, up3, 32, scope='Skip3') #1st step up. up2_shape = (W/2, H/2) up2 = UpBlock(up3b, filters=32, kernel_size=3, dropout=self.param.dropout, scope='Up2') # residual connection for results from upsampling with stuff from step before up2b = SkipConnection(down1, up2, 32, scope='Skip2') #2nd step up up1_shape = (W,H) up1 = UpBlock(up2b, filters=32, kernel_size=3, dropout=self.param.dropout, scope='Up1') up1b = SkipConnection(inputs, up1, 32, scope='Skip1') #could have more 2D convolutions here. #final 1D convolution (pixel-wise Dense layer) #softmax to make a probability distribution across classes. final = Conv2D(kernel_size=1, filters=self.param.Nclasses, activation='softmax', name='FinalConv')(up1b) self.model = Model(inputs=inputs, outputs=final) self.model.compile(loss=self.pixelwise_crossentropy, optimizer="adam", metrics=[self.IOU]) def IOU(self, Ytrue, Ypred): """ compute pixelwise cross-entropy across most popular classes example by example. Input: Ytrue Tensor (Nbatch, W, H, 3) Ypred Tensor (Nbatch, W, H, Nclass) """ #define a custom loss function using the pixel-wise cross entropy. #Nb,W,H,Nc = Ypred.shape R = Ytrue[:, :, :, 0] G = Ytrue[:, :, :, 1] Ytrue_class = (R // 10) * 256 + G #get classes_present #classes_present = set(K.reshape(ytrue_class,-1)) #classes_present = tf.unique(K.reshape(ytrue_class,-1)) cost = 0 for i, class_label in enumerate(self.class_lookup): #get numerical value associated with class cl pred_msk = Ypred[:, :, :, i] > 0.5 #if class_label in classes_present: #make logical mask label_msk = K.equal(Ytrue_class, class_label) label_AND_pred = tf.cast(tf.math.logical_and(label_msk, pred_msk), tf.float32) label_OR_pred = tf.cast(tf.math.logical_or(label_msk, pred_msk), tf.float32) iou = K.sum(label_AND_pred) / (K.sum(label_OR_pred) + 0.01) cost = cost + iou cost = cost / self.param.Nclasses return cost def pixelwise_crossentropy(self, Ytrue, Ypred): """ compute pixelwise cross-entropy across most popular classes example by example. Input: Ytrue Tensor (Nbatch, W, H, 3) Ypred Tensor (Nbatch, W, H, Nclass) """ #define a custom loss function using the pixel-wise cross entropy. #W,H,Nc = tf.shape(Ypred) R = Ytrue[:, :, :, 0] G = Ytrue[:, :, :, 1] ytrue_class = (R // 10) * 256 + G #get classes_present shapes = tf.cast(tf.shape(ytrue_class), tf.float32) #classes_present = set(K.reshape(ytrue_class,-1)) classes_present = tf.unique(K.reshape(ytrue_class, [K.prod(shapes)])) cost = 0 for i, class_label in enumerate(self.class_lookup): #get numerical value associated with class cl Ypred_c = K.clip(Ypred[:, :, :, i], self.param.eps, 1 - self.param.eps) #if class_label in classes_present: #make logical mask msk = K.equal(ytrue_class, class_label) cost = cost - K.sum( tf.boolean_mask(K.log(1 - Ypred_c), tf.math.logical_not(msk))) cost = cost - K.sum(tf.boolean_mask(K.log(Ypred_c), msk)) #else: # #find all erroneous classes # cost += K.mean(K.log(1-ypred_c))/(Nclasses) shapes = tf.cast(tf.shape(Ypred), tf.float32) cost = cost / (self.param.Nclasses * shapes[1] * shapes[2]) return cost def _get_X_y_batch(self, ind, size_bucket=0): """given list of indices and a corresponding bucket, returns a batch for training. """ Wtarget = img_util.Wlist[size_bucket + 1] Htarget = img_util.Hlist[size_bucket + 1] Nb = len(ind) X = np.zeros([Nb, Wtarget, Htarget, 3]) y = np.zeros([Nb, Wtarget, Htarget, 3]) for j, i in enumerate(ind): row = self.index.iloc[i] X_name = '/'.join([row['folder'], row['filename']]) y_name = X_name[:-4] + '_seg.png' X[j] = image.load_img(X_name, target_size=(Wtarget, Htarget)) y[j] = image.load_img(y_name, target_size=(Wtarget, Htarget)) return X, y def get_random_train_bucket(self, file_dict): """generator to pick a random allowed_bucket using relative sizes """ #list of tuples with key and number in each bucket. N_in_bucket = [(key, len(val)) for key, val in file_dict.items()] vals = [x[1] for x in N_in_bucket] #maps those sizes to [0,1) interval to randomly select one rand_boundary = np.cumsum(vals) / np.sum(vals) r = np.random.random() for i, v in enumerate(rand_boundary): if (r < v): return N_in_bucket[i][0] def rand_batch_generator(self, file_dict): """ Generator to make random batches of data. Intended for use in training to endlessly generate samples of data. dict - dict of list of files to lookup """ while True: size_bucket = self.get_random_train_bucket(file_dict) ind_sub = np.random.choice(file_dict[size_bucket], size=self.param.batch_size) yield self._get_X_y_batch(ind_sub, size_bucket) def det_batch_generator(self, file_dict): """det_batch_generator Load deterministic batch generator. Intended for use with inference/prediction to deterministically loop over data once. """ Nb = self.param.batch_size for bucket, ind_list in file_dict.items(): N_in_bucket = len(ind_list) i0 = 0 i1 = Nb while i1 < len(ind_list): ind_sub = np.arange(i0, i1) yield self._get_X_y_batch(ind_sub, bucket) i0 = i1 i1 += Nb #Last iter ind_sub = np.arange(i0, len(y)) yield self._get_X_y_batch(ind_sub, bucket) def train_network(self): """train_network() Actually train the keras model. Uses random batches from rand_batch_generator and train_dict Input: None Output: None Side-effect: Trains self.model. """ self.model.fit_generator(self.rand_batch_generator(self.train_dict), epochs=self.param.Nepoch, steps_per_epoch=self.param.train_steps) def predict_all(self, file_dict): """predict(X,y,vec) Loop over whole file_dict and predict for all samples (HUGE TASK! DONT DO IT) Input: X - np.array with rows of indices y - np.array with value of true class. vec - np.array with wordvectors Output pred - tuple with losses/metrics over dataset """ Ntot = 0 for key, val in file_dict.items(): Ntot += len(val) Nsteps = np.ceil(Ntot / self.param.batch_size) pred = self.model.predict_generator( self.det_batch_generator(file_dict), steps=Nsteps) return pred def predict_afew(self, ind_sub=np.arange(3), size_bucket=0): """ Loop over a few instances and data and predict for all samples. Input: file_dict - dict with list of entries to sample from index_df ind_sub - np.array with locations to use size_bucket - bucket from file_dict to try using Output pred - Output images for that batch (pixelwise probabilities for that class batch - initial raw images y - ground truth """ batch, y = self._get_X_y_batch(ind_sub, size_bucket) pred = self.model.predict(batch) return pred, batch, y def evaluate(self): """ Evaluates metrics by iterating over all files in file_dict using loss and metrics specified in model. Input: file_dict - np.array with rows of indices Output eval_scores - tuple with losses/metrics over dataset """ Nsteps = np.ceil(len(y) / self.param.batch_size) eval_scores = self.model.evaluate_generator(self.det_batch_generator(), steps=Nsteps) return eval_scores def save_model(self): """saves whole model in hdf5 format.""" save_name = '/'.join([self.param.model_dir, self.param.model_name]) self.model.save(save_name + '.model') self.param.save_param(self.param, save_name + '.param') def load_model(self, path_base): """loads saved model from hdf5""" self.param = util.load_param(param_path + '.param') self.model = keras.models.load_model(path + '.model') def get_most_likely(self, pred): """convert a (width, height, Nclass) array to (width,height,3) array R/G colors. """ return img_util.get_color_from_pred(pred, self.class_lookup) def compare_pred_images(self, pred, y, num=0): plt.figure() plt.subplot(121) plt.imshow(pred2[num].astype(int)) plt.subplot(122) tmp = np.zeros(y[num].shape) for i in range(2): tmp[:, :, i] = y[num, :, :, i] plt.imshow(tmp.astype(int))
plt.legend() plt.pause(0.01) checkpoint = ModelCheckpoint('contest_best.h5', verbose=1, monitor='val_mean_absolute_error', save_best_only=True, mode='min') plot_losses = PlotLosses() #Train Model model.fit_generator(train_generator, steps_per_epoch=len(train_generator), epochs=60, validation_data=validation_generator, validation_steps=len(validation_generator), callbacks=[checkpoint, plot_losses]) #Test Model model = load_model('contest_best.h5') score = model.evaluate_generator(test_generator, steps=len(test_generator)) print('score (mse, mae):\n', score) test_generator.reset() predict = model.predict_generator(test_generator, steps=len(test_generator), workers=1, use_multiprocessing=False) print('prediction:\n', predict)
class VAE: def __init__(self): self.model_name = 'facetest' self.version = "1" self.save_dir = self.model_name + "v" + self.version + "/" if 'anime' in self.model_name: self.data_dir = r"X:\Projects\2DO\anime-faces" else: self.data_dir = r"W:\Projects\Done\FDGAN\kiryatgat-1502-fdgan-master\CelebA\img_align_celeba" self.log_dir = self.save_dir + "/logs/" self.sample_dir = self.save_dir + '/samples/' self.test_dir = self.save_dir + '/test/' self.sample_size = 5000 self.shape = None self.sd_layer = None self.mean_layer = None self.shape_before_flattening = None self.decoder_output = None self.stride = 2 # # These are mean and standard deviation values obtained from the celebA dataset used for training # self.mean = 0.43810788 # self.std = 0.29190385 self.encoder = None self.decoder = None self.autoencoder = None self.batch_size = 256 self.epochs = 50 self.input_size = 64 self.encoder_output_dim = 256 self.decoder_input = Input(shape=(self.encoder_output_dim, ), name='decoder_input') self.input_shape = (self.input_size, self.input_size, 3) self.encoder_input = Input(shape=self.input_shape, name='encoder_input') # self.data_set = CelebA(output_size=self.input_size, channel=self.input_shape[-1], sample_size=self.sample_size, # batch_size=self.batch_size, crop=True, filter=False, data_dir=self.data_dir, # ignore_image_description=True) self.use_batch_norm = True self.use_dropout = False # self.LOSS_FACTOR = 10000 self.learning_rate = 0.0005 self.adam_optimizer = Adam(lr=self.learning_rate) # callbacks # checkpoint_vae = ModelCheckpoint(os.path.join(WEIGHTS_FOLDER, 'VAE/weights.h5'), save_weights_only=True, # verbose=1) # self.early_stop_callback = EarlyStopping(monitor='loss', min_delta=0.001, patience=3, mode='min', verbose=1) self.checkpoint_callback = ModelCheckpoint( self.save_dir + self.model_name + '_best.h5', monitor='loss', verbose=1, save_best_only=True, mode='min', period=1) # self.tensorboard_callback = keras.callbacks.TensorBoard(log_dir=log_dir) # def vae_loss(self, input_img, output): # compute the average MSE error, then scale it up, ie. simply sum on all # axes reconstruction_loss = K.sum(K.square(output - input_img)) # compute the KL loss kl_loss = - 0.5 * K.sum(1 # + self.sd_layer - K.square(self.mean_layer) - K.square(K.exp(self.sd_layer)), axis=-1) # return the average # loss over all images in batch total_loss = K.mean(reconstruction_loss + kl_loss) return total_loss def kl_loss(self, y_true, y_pred): kl_loss = -0.5 * K.sum(1 + self.sd_layer - K.square(self.mean_layer) - K.exp(self.sd_layer), axis=1) return kl_loss # def r_loss(self, y_true, y_pred): # return K.mean(K.square(y_true - y_pred), axis=[1, 2, 3]) # # MSE loosssss def r_loss(self, y_true, y_pred): return K.mean(K.square(y_true - y_pred), axis=[1, 2, 3]) def total_loss(self, y_true, y_pred): # return self.LOSS_FACTOR * self.r_loss(y_true, y_pred) + self.kl_loss(y_true, y_pred) return K.mean( self.r_loss(y_true, y_pred) + self.kl_loss(y_true, y_pred) - 0.001) def sampler(self, layers): std_norm = K.random_normal(shape=(K.shape(layers[0])[0], 128), mean=0, stddev=1) return layers[0] + layers[1] * std_norm # Building the Encoder # def build_encoder(self): # x = self.inp # x = Conv2D(32, (2, 2), strides=self.stride, activation="relu", padding="same")(x) # x = BatchNormalization()(x) # # x = Conv2D(64, (2, 2), strides=self.stride, activation="relu", padding="same")(x) # x = BatchNormalization()(x) # # x = Conv2D(128, (2, 2), strides=self.stride, activation="relu", padding="same")(x) # x = BatchNormalization()(x) # # self.shape = K.int_shape(x) # x = Flatten()(x) # # x = Dense(256, activation="relu")(x) # # self.mean_layer = Dense(128, activation="relu")(x) # should sigmoid # self.mean_layer = BatchNormalization()(self.mean_layer) # # self.sd_layer = Dense(128, activation="relu")(x) # should sigmoid # self.sd_layer = BatchNormalization()(self.sd_layer) # # # latent_vector = Lambda(self.sampler)([self.mean_layer, self.sd_layer]) # return Model(self.inp, [self.mean_layer, self.sd_layer], name="VAE_Encoder") # # # Building the decoder # def build_decoder(self): # decoder_inp = Input(shape=(128,)) # x = decoder_inp # x = Dense(self.shape[1] * self.shape[2] * self.shape[3], activation="relu")(x) # # x = Reshape((self.shape[1], self.shape[2], self.shape[3]))(x) # # x = (Conv2DTranspose(32, (3, 3), strides=self.stride, activation="relu", padding="same"))(x) # x = BatchNormalization()(x) # # x = (Conv2DTranspose(16, (3, 3), strides=self.stride, activation="relu", padding="same"))(x) # x = BatchNormalization()(x) # # x = (Conv2DTranspose(8, (3, 3), strides=self.stride, activation="relu", padding="same"))(x) # x = BatchNormalization()(x) # # outputs = Conv2DTranspose(3, (3, 3), activation='sigmoid', padding='same', name='decoder_output')(x) # # should RELU # # return Model(decoder_inp, outputs, name="VAE_Decoder") def build_encoder(self): conv_filters = [32, 64, 64, 64] conv_kernel_size = [3, 3, 3, 3] conv_strides = [2, 2, 2, 2] # Number of Conv layers n_layers = len(conv_filters) # Define model input x = self.encoder_input # Add convolutional layers for i in range(n_layers): x = Conv2D(filters=conv_filters[i], kernel_size=conv_kernel_size[i], strides=conv_strides[i], padding='same', name='encoder_conv_' + str(i))(x) if self.use_batch_norm: x = BatchNormalization()(x) x = LeakyReLU()(x) if self.use_dropout: x = Dropout(rate=0.25)(x) # Required for reshaping latent vector while building Decoder self.shape_before_flattening = K.int_shape(x)[1:] x = Flatten()(x) self.mean_layer = Dense(self.encoder_output_dim, name='mu')(x) self.sd_layer = Dense(self.encoder_output_dim, name='log_var')(x) # Defining a function for sampling def sampling(args): mean_mu, log_var = args epsilon = K.random_normal(shape=K.shape(mean_mu), mean=0., stddev=1.) return mean_mu + K.exp(log_var / 2) * epsilon # Using a Keras Lambda Layer to include the sampling function as a layer # in the model encoder_output = Lambda( sampling, name='encoder_output')([self.mean_layer, self.sd_layer]) return Model(self.encoder_input, encoder_output, name="VAE_Encoder") # Building the decoder def build_decoder(self): conv_filters = [64, 64, 32, 3] conv_kernel_size = [3, 3, 3, 3] conv_strides = [2, 2, 2, 2] n_layers = len(conv_filters) # Define model input decoder_input = self.decoder_input # To get an exact mirror image of the encoder x = Dense(np.prod(self.shape_before_flattening))(decoder_input) x = Reshape(self.shape_before_flattening)(x) # Add convolutional layers for i in range(n_layers): x = Conv2DTranspose(filters=conv_filters[i], kernel_size=conv_kernel_size[i], strides=conv_strides[i], padding='same', name='decoder_conv_' + str(i))(x) # Adding a sigmoid layer at the end to restrict the outputs # between 0 and 1 if i < n_layers - 1: x = LeakyReLU()(x) else: x = Activation('sigmoid')(x) # Define model output self.decoder_output = x return Model(decoder_input, self.decoder_output, name="VAE_Decoder") def build_autoencoder(self): self.encoder = self.build_encoder() self.decoder = self.build_decoder() # Input to the combined model will be the input to the encoder. # Output of the combined model will be the output of the decoder. self.autoencoder = Model(self.encoder_input, self.decoder(self.encoder( self.encoder_input)), name="Variational_Auto_Encoder") self.autoencoder.compile( optimizer=self.adam_optimizer, loss=self.total_loss, # metrics=[self.total_loss], metrics=[self.r_loss, self.kl_loss], experimental_run_tf_function=False) self.autoencoder.summary() if os.path.exists(self.save_dir): if os.path.exists(self.save_dir + self.model_name + ".h5"): self.autoencoder.load_weights( self.save_dir + self.model_name + ".h5") # Loading pre-trained weights print("===Loaded model weights===") if os.path.exists(self.save_dir + self.model_name + '_best.h5'): self.autoencoder.load_weights(self.save_dir + self.model_name + '_best.h5') print("===Loaded best model===") else: os.makedirs(self.save_dir) plot_model(self.autoencoder, to_file="model.png", show_shapes=True) return self.autoencoder def train(self): if "anime" in self.data_dir: filenames = np.array( glob.glob(os.path.join(self.data_dir, '*/*.png'))) else: filenames = np.array( glob.glob(os.path.join(self.data_dir, '*/*.jpg'))) NUM_IMAGES = len(filenames) print("Total number of images : " + str(NUM_IMAGES)) data_flow = ImageDataGenerator(rescale=1. / 255).flow_from_directory( self.data_dir, target_size=self.input_shape[:2], batch_size=self.batch_size, shuffle=True, class_mode='input', subset='training') self.autoencoder.fit_generator(data_flow, shuffle=True, epochs=self.epochs, initial_epoch=0, steps_per_epoch=NUM_IMAGES // (self.batch_size * 2), callbacks=[self.checkpoint_callback]) self.autoencoder.save_weights(self.save_dir + self.model_name + ".h5") def eval(self): data_flow = ImageDataGenerator(rescale=1. / 255).flow_from_directory( self.data_dir, target_size=self.input_shape[:2], batch_size=self.batch_size, shuffle=True, class_mode='input', subset='training') loss = self.autoencoder.evaluate_generator(data_flow, 100) print('total loss: {:.2f} - r_loss: {:.2f} - kl_loss: {:.2f}'.format( loss[0], loss[1], loss[2])) def generate(self, image=None): if not os.path.exists(self.sample_dir): os.makedirs(self.sample_dir) if image is None: img = np.random.normal(size=(9, self.input_size, self.input_size, 3)) cv2.imshow("generated", img[0] * 255) prediction = self.autoencoder.predict(img) op = np.vstack( (np.hstack((prediction[0], prediction[1], prediction[2])), np.hstack((prediction[3], prediction[4], prediction[5])), np.hstack((prediction[6], prediction[7], prediction[8])))) print(op.shape) op = cv2.resize(op, (self.input_size * 9, self.input_size * 9), interpolation=cv2.INTER_AREA) op = cv2.cvtColor(op, cv2.COLOR_BGR2RGB) # cv2.imshow("generated", op) # cv2.imwrite(self.sample_dir + "generated" + str(r(0, 9999)) + ".jpg", (op * 255).astype("uint8")) else: img = cv2.imread(image, cv2.IMREAD_UNCHANGED) img = cv2.resize(img, (self.input_size, self.input_size), interpolation=cv2.INTER_AREA) img = img.astype("float32") img = img / 255 prediction = self.autoencoder.predict( img.reshape(1, self.input_size, self.input_size, 3)) img = cv2.resize(prediction[0][:, :, ::-1], (960, 960), interpolation=cv2.INTER_AREA) # op = cv2.cvtColor(op, cv2.COLOR_BGR2RGB) # img = (img - self.mean) / self.std # cv2.imshow("prediction", cv2.resize(img/255, (960, 960), interpolation=cv2.INTER_AREA)) cv2.imshow("prediction", img) cv2.imwrite( self.sample_dir + "generated" + str(r(0, 9999)) + ".jpg", (img * 255).astype("uint8")) while cv2.waitKey(0) != 27: pass cv2.destroyAllWindows()
#verbose : 얼마나 자세히 정보를 출력할지 0: quiet, 1: update messages. lr_reducer = ReduceLROnPlateau(monitor='val_loss', patience=12, factor=0.5, verbose=1) #시각화 tensorboard = TensorBoard(log_dir='./logs') #monitored 되는 값의 증가가 멈추면 종료 early_stopper = EarlyStopping(monitor='val_loss', patience=30, verbose=1) #가중치 저장. 매 epoch마다 저장됨. checkpoint = ModelCheckpoint('./models/model.h5') #training final_model.fit_generator( train_iterator, steps_per_epoch=1000, epochs=250, validation_data=test_iterator, validation_steps=200, verbose=1, shuffle=True, callbacks=[lr_reducer, checkpoint, early_stopper, tensorboard], #use_multiprocessing=True, workers=1) #test output scores = final_model.evaluate_generator(test_iterator, steps=2000) print(scores)
class ModelTrain: def __init__(self, train_dir, test_dir, val_dir, base_dir, f=None): self.extensions = ['jpg', 'jpeg', 'JPG', 'JPEG'] self.train_dir = train_dir self.test_dir = test_dir self.val_dir = val_dir self.num_train_samples = 0 self.num_val_samples = 0 self.num_test_samples = 0 self.num_classes = 0 if not f is None: self.f = open(f, 'w+') self.base_dir = base_dir self.model_list = { 'mobilenet': tf.keras.applications.mobilenet.MobileNet(include_top=False, input_shape=(224, 224, 3), pooling='avg'), 'mobilenet_v2': tf.keras.applications.mobilenet_v2.MobileNetV2(include_top=False, input_shape=(224, 224, 3), pooling='avg'), 'inception_v3': tf.keras.applications.inception_v3.InceptionV3(include_top=False, input_shape=(224, 224, 3), pooling='avg') } self.data_generators = { 'mobilenet': ImageDataGenerator(preprocessing_function=tf.keras.applications. mobilenet.preprocess_input), 'mobilenet_v2': ImageDataGenerator(preprocessing_function=tf.keras.applications. mobilenet_v2.preprocess_input), 'inception_v3': ImageDataGenerator(preprocessing_function=tf.keras.applications. inception_v3.preprocess_input) } def create_image_dir(self, image_dir: str, testing_percetange=25, validation_percetage=25): # This code is based on: https://github.com/googlecodelabs/tensorflow-for-poets-2/blob/6be494e0300555fd48c095abd6b2764ba4324592/scripts/retrain.py#L125 moves = 'Moves {} to {}' if not os.path.exists(image_dir): print('Root path directory ' + image_dir + ' not found') tf.logging.error("Root path directory '" + image_dir + "' not found.") return None result = collections.defaultdict() sub_dirs = [ os.path.join(image_dir, item) for item in os.listdir(image_dir) ] sub_dirs = sorted(item for item in sub_dirs if os.path.isdir(item)) for sub_dir in sub_dirs: file_list = [] dir_name = os.path.basename(sub_dir) if dir_name == image_dir: continue tf.logging.info("Looking for images in '" + dir_name + "'") for ext in self.extensions: file_glob = os.path.join(image_dir, dir_name, '*.' + ext) file_list.extend(gfile.Glob(file_glob)) if not file_list: print('No files found') tf.logging.warning('No files found') continue label_name = re.sub(r'[^a-z0-9]+', ' ', dir_name.lower()) for file_name in file_list: val_sub_dir = os.path.join(self.val_dir, dir_name) if not os.path.exists(val_sub_dir): os.mkdir(val_sub_dir) train_sub_dir = os.path.join(self.train_dir, dir_name) if not os.path.exists(train_sub_dir): os.mkdir(train_sub_dir) os.mkdir(os.path.join(train_sub_dir, 'n')) test_sub_dir = os.path.join(self.test_dir, dir_name) if not os.path.exists(test_sub_dir): os.mkdir(test_sub_dir) # print(sub_dir) # print(os.path.join(dir_name, self.val_dir)) # print(os.path.join(self.val_dir, dir_name)) base_name = os.path.basename(file_name) # print(klklk) # print(base_name) hash_name = re.sub(r'_nohash_.*$', '', file_name) hash_name_hashed = hashlib.sha1( compat.as_bytes(hash_name)).hexdigest() percetage_hash = ((int(hash_name_hashed, 16) % (MAX_NUM_IMAGES_PER_CLASS + 1)) * (100.0 / MAX_NUM_IMAGES_PER_CLASS)) if percetage_hash < validation_percetage: if os.path.exists(os.path.join(val_sub_dir, base_name)): continue shutil.copy(file_name, val_sub_dir) print(moves.format(base_name, val_sub_dir)) # self.num_val_samples += 1 elif percetage_hash < (testing_percetange + validation_percetage): if os.path.exists(os.path.join(test_sub_dir, base_name)): continue shutil.copy(file_name, test_sub_dir) print(moves.format(base_name, test_sub_dir)) # self.num_test_samples += 1 else: if os.path.exists(os.path.join(train_sub_dir, base_name)): continue shutil.copy(file_name, train_sub_dir + '\\n') print(moves.format(base_name, train_sub_dir + '\\n')) # self.num_train_samples += 1 print('Done') # This code is based on https: // www.kaggle.com / vbookshelf / skin - lesion - analyzer - tensorflow - js - web - app def top_2_accuracy(self, y_true, y_pred): return top_k_categorical_accuracy(y_true, y_pred, k=2) def top_3_accuracy(self, y_true, y_pred): return top_k_categorical_accuracy(y_true, y_pred, k=3) def top_5_accuracy(self, y_true, y_pred): return top_k_categorical_accuracy(y_true, y_pred, k=5) def data_augmentation(self, batch_size=1, image_size=224, num_img_aug=500): aug_dir = self.train_dir + '_aug' if not os.path.exists(aug_dir): os.mkdir(aug_dir) for folder in os.listdir(self.train_dir): print(os.path.join(self.train_dir, folder)) folder_path = os.path.join(self.train_dir, folder) folder_path_aug = folder_path.replace(self.train_dir, aug_dir) if not os.path.exists(folder_path_aug + '_aug'): os.mkdir(folder_path_aug + '_aug') for sub_folder in os.listdir(os.path.join(self.train_dir, folder)): path = os.path.join(self.train_dir, folder).replace(self.train_dir, aug_dir) save_path = path + '_aug' save_path = os.path.join(save_path, sub_folder) sub_folder_path = os.path.join(folder_path, sub_folder) print('sub folder path', sub_folder_path) # print('folder path', save_path) if not os.path.exists(save_path): os.mkdir(save_path) print('save_path', save_path) data_aug_gen = ImageDataGenerator(rotation_range=180, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.1, horizontal_flip=True, vertical_flip=True, fill_mode='nearest') path_dir = os.path.join(self.train_dir, folder) print('direktori: ', os.path.join(path_dir, sub_folder)) ini_dir = os.path.join(path_dir, sub_folder) aug_datagen = data_aug_gen.flow_from_directory( directory=ini_dir, save_to_dir=save_path, save_format='jpg', target_size=(image_size, image_size), batch_size=batch_size) num_files = len(os.listdir(ini_dir)) # print(num_files) num_batches = int( np.ceil((num_img_aug - num_files) / batch_size)) for i in range(0, num_batches): imgs, labels = next(aug_datagen) for folder in os.listdir(aug_dir): path = os.path.join(aug_dir, folder) for subfolder in os.listdir(path): sub_path = os.path.join(path, subfolder) print('There are {} images in {}'.format( len(os.listdir(sub_path)), subfolder)) if 'train' in sub_path: self.num_train_samples += len(os.listdir(sub_path)) elif 'val' in sub_path: self.num_val_samples += len(os.listdir(sub_path)) print('num train', self.num_train_samples) print('num val', self.num_val_samples) def data_augmentation2(self, batch_size=16, image_size=224, num_img_aug=500): self.f.write('Data Augmentation\n') self.aug_dir = self.train_dir + '_aug' if os.path.exists(self.aug_dir): shutil.rmtree(self.aug_dir) os.mkdir(self.aug_dir) else: os.mkdir(self.aug_dir) for folder in os.listdir(self.train_dir): # Kelas self.num_classes += 1 print(os.path.join(self.train_dir, folder)) self.f.write(os.path.join(self.train_dir, folder) + '\n') folder_path = os.path.join(self.train_dir, folder) folder_path_aug = folder_path.replace(self.train_dir, self.aug_dir) if not os.path.exists(folder_path_aug): os.mkdir(folder_path_aug) data_aug_gen = ImageDataGenerator(rotation_range=180, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.1, horizontal_flip=True, vertical_flip=True, fill_mode='nearest') path_dir = os.path.join(self.train_dir, folder) # print('direktori: ', os.path.join(path_dir, sub_folder)) print('direktori: ', os.path.join(self.train_dir, folder)) self.f.write('direktori: ' + os.path.join(self.train_dir, folder) + '\n') # ini_dir = os.path.join(path_dir, sub_folder) ini_dir = os.path.join(self.train_dir, folder) aug_datagen = data_aug_gen.flow_from_directory( directory=ini_dir, save_to_dir=folder_path_aug, save_format='jpg', target_size=(image_size, image_size), batch_size=batch_size) num_files = len(os.listdir(ini_dir)) # print(num_files) num_batches = int(np.ceil((num_img_aug - num_files) / batch_size)) for i in range(0, num_batches): imgs, labels = next(aug_datagen) # self.plots(imgs, titles=None, fname=ini_dir + '\\fig'+str(i)+'.jpg') for folder in os.listdir(self.aug_dir): path = os.path.join(self.aug_dir, folder) print('There are {} images in {}'.format(len(os.listdir(path)), folder)) self.f.write('There are {} images in {}'.format( len(os.listdir(path)), folder) + '\n') self.num_train_samples += len(os.listdir(path)) for folder in os.listdir(self.val_dir): path = os.path.join(self.val_dir, folder) print('There are {} images in {}'.format(len(os.listdir(path)), folder)) self.f.write('There are {} images in {}'.format( len(os.listdir(path)), folder) + '\n') self.num_val_samples += len(os.listdir(path)) print('num train', self.num_train_samples) self.f.write('num train' + str(self.num_train_samples) + '\n') print('num val', self.num_val_samples) self.f.write('num val' + str(self.num_val_samples) + '\n') def setup_generators(self, train_batch_size=10, val_batch_size=10, image_size=224): # train_path = '' # valid_path = '' num_train_samples = self.num_train_samples num_val_samples = self.num_val_samples self.train_steps = np.ceil(num_train_samples / train_batch_size) self.val_steps = np.ceil(num_val_samples / val_batch_size) datagen = ImageDataGenerator(preprocessing_function=tf.keras. applications.mobilenet.preprocess_input) self.train_batches = datagen.flow_from_directory( directory=self.aug_dir, target_size=(image_size, image_size), batch_size=train_batch_size) self.valid_batches = datagen.flow_from_directory( directory=self.val_dir, target_size=(image_size, image_size), batch_size=val_batch_size) self.test_batches = datagen.flow_from_directory( directory=self.val_dir, target_size=(image_size, image_size), batch_size=1, shuffle=False) def define_mobile_net(self, class_weights=None, model='mobilenet', dropout=0.25, epochs=30, name='V1'): self.name = model self.f.write('MODEL: ' + self.name + '\n') x = self.model_list[model].output x = Dropout(dropout, name='do_akhir')(x) # x = Conv2D(7, (1, 1), # padding='same', # name='conv_preds')(x) # x = Activation('softmax', name='act_softmax')(x) predictions = Dense(self.num_classes, activation='softmax')(x) self.new_model = Model(inputs=self.model_list[model].input, outputs=predictions) print(self.new_model.summary()) # self.f.write(self.new_model.summary() + '\n') # self.new_model = model_list[model] for layer in self.new_model.layers[:-23]: layer.trainable = False self.new_model.compile(Adam(lr=0.01), loss='categorical_crossentropy', metrics=[ categorical_accuracy, self.top_2_accuracy, self.top_3_accuracy ]) print('Validation Batches: ', self.valid_batches.class_indices) self.f.write('Validation Batches: ' + str(self.valid_batches.class_indices) + '\n') # if not class_weights: # class_weights = { # 0: 0.8, # akiec # 1: 0.8, # bcc # 2: 0.6, # bkl # 3: 1.0, # mel # 4: 1.0, # nv # 5: 0.5, # vasc # } if not class_weights: np.random.seed(0) class_weights = { i: b for i, b in enumerate(np.random.rand(self.num_classes)) } filepath = os.path.join(self.base_dir, 'best_model' + self.name + '.h5') checkpoint = ModelCheckpoint(filepath, monitor='val_top_3_accuracy', verbose=1, save_best_only=True, mode='max') reduce_lr = ReduceLROnPlateau(monitor='val_top_3_accuracy', factor=0.5, patience=2, verbose=1, mode='max', min_lr=0.00001) callbacks_list = [checkpoint, reduce_lr] self.history = self.new_model.fit_generator( self.train_batches, steps_per_epoch=self.train_steps, class_weight=class_weights, validation_data=self.valid_batches, validation_steps=self.val_steps, epochs=epochs, verbose=1, callbacks=callbacks_list) with open(os.path.join(self.base_dir, 'trainHistoryDict' + self.name), 'wb') as file_pi: pickle.dump(self.history.history, file_pi) # with open('historyfile.json', 'w') as f: # json.dump(self.history.history, f) # serialize model to JSON model_json = self.new_model.to_json() with open( os.path.join(self.base_dir, 'last_step_model' + self.name + '.json'), 'w') as json_file: json_file.write(model_json) # serialize weights to HDF5 self.new_model.save_weights( os.path.join(self.base_dir, "last_step_weight_" + self.name + ".h5")) self.new_model.save( os.path.join(self.base_dir, "last_step_model_" + self.name + ".h5")) output_path = tf.contrib.saved_model.save_keras_model( self.new_model, os.path.join(self.base_dir, 'model_' + self.name)) print(type(output_path)) print(output_path) self.f.write('Saved model to disk: {} \n'.format(output_path)) print("Saved model to disk") print(self.new_model.metrics_names) # Last Step val_loss, val_cat_acc, val_top_2_acc, val_top_3_acc = \ self.new_model.evaluate_generator(self.test_batches, steps=self.num_val_samples) print('Last Step') self.f.write('Last Step \n') print('val_loss:', val_loss) self.f.write('val_loss:' + str(val_loss) + '\n') print('val_cat_acc:', val_cat_acc) self.f.write('val_cat_acc:' + str(val_cat_acc) + '\n') print('val_top_2_acc:', val_top_2_acc) self.f.write('val_top_2_acc:' + str(val_top_2_acc) + '\n') print('val_top_3_acc:', val_top_3_acc) self.f.write('val_top_3_acc:' + str(val_top_3_acc) + '\n') # Best Step self.new_model.load_weights(filepath) val_loss, val_cat_acc, val_top_2_acc, val_top_3_acc = \ self.new_model.evaluate_generator(self.test_batches, steps=self.num_val_samples) print('Best Step') self.f.write('Best Step \n') print('val_loss:', val_loss) self.f.write('val_loss:' + str(val_loss) + '\n') print('val_cat_acc:', val_cat_acc) self.f.write('val_cat_acc:' + str(val_cat_acc) + '\n') print('val_top_2_acc:', val_top_2_acc) self.f.write('val_top_2_acc:' + str(val_top_2_acc) + '\n') print('val_top_3_acc:', val_top_3_acc) self.f.write('val_top_3_acc:' + str(val_top_3_acc) + '\n') def predicts(self, model_path: str, model='mobilenet', image_size=224): datagen = self.data_generators[model] filename = os.path.join(self.base_dir, 'predict_' + model + '.txt') self.name = model f = open(filename, 'w+') cm_plot_labels = [] num_val_samples = 0 for folder in os.listdir(self.base_dir): path = os.path.join(self.base_dir, folder) if os.path.isdir(path): for sub_folder in os.listdir(path): sub_path = os.path.join(path, sub_folder) if 'val' in sub_path: # temp = sub_folder.split('_') cm_plot_labels.append(sub_folder) print('There are {} images in {}'.format( len(os.listdir(sub_path)), sub_folder)) f.write('There are {} images in {} \n'.format( len(os.listdir(sub_path)), sub_folder)) num_val_samples += len(os.listdir(sub_path)) test_batches = datagen.flow_from_directory( directory=os.path.join(self.base_dir, 'val_dir'), target_size=(image_size, image_size), batch_size=1, shuffle=False) loaded_model = tf.contrib.saved_model.load_keras_model(model_path) predictions = loaded_model.predict_generator(test_batches, steps=num_val_samples, verbose=1) test_labels = test_batches.classes cm = confusion_matrix(test_labels, predictions.argmax(axis=1)) self.plot_confusion_matrix(cm, cm_plot_labels, title='Confusion Matrix') y_pred = np.argmax(predictions, axis=1) y_true = test_batches.classes report = classification_report(y_true, y_pred, target_names=cm_plot_labels) print(report) f.write(report) f.close() ''' Recall = Given a class, will the classifier be able to detect it? Precision = Given a class prediction from a classifier, how likely is it to be correct? F1 Score = The harmonic mean of the recall and precision. Essentially, it punishes extreme values. ''' def save_learning_curves(self, name='V1'): acc = self.history.history['categorical_accuracy'] val_acc = self.history.history['val_categorical_accuracy'] loss = self.history.history['loss'] val_loss = self.history.history['val_loss'] train_top2_acc = self.history.history['top_2_accuracy'] val_top2_acc = self.history.history['val_top_2_accuracy'] train_top3_acc = self.history.history['top_3_accuracy'] val_top3_acc = self.history.history['val_top_3_accuracy'] epochs = range(1, len(acc) + 1) plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and validation loss') plt.legend() plt.savefig(fname=os.path.join( self.base_dir, 'Training and validation loss ' + self.name + '.jpg')) plt.clf() # plt.figure() plt.plot(epochs, acc, 'bo', label='Training cat acc') plt.plot(epochs, val_acc, 'b', label='Validation cat acc') plt.title('Training and validation cat accuracy') plt.legend() # plt.figure() plt.savefig(fname=os.path.join( self.base_dir, 'Training and validation cat accuracy ' + self.name + '.jpg')) plt.clf() plt.plot(epochs, train_top2_acc, 'bo', label='Training top2 acc') plt.plot(epochs, val_top2_acc, 'b', label='Validation top2 acc') plt.title('Training and validation top2 accuracy') plt.legend() # plt.figure() plt.savefig(fname=os.path.join( self.base_dir, 'Training and validation top2 accuracy ' + self.name + '.jpg')) plt.clf() plt.plot(epochs, train_top3_acc, 'bo', label='Training top3 acc') plt.plot(epochs, val_top3_acc, 'b', label='Validation top3 acc') plt.title('Training and validation top3 accuracy') plt.legend() plt.savefig(fname=os.path.join( self.base_dir, 'Training and validation top3 accuracy ' + self.name + '.jpg')) plt.clf() # plt.show() # plt.savefig(fname='training_curves.jpg') def plots( sellf, ims, fname, figsize=(12, 6), rows=5, interp=False, titles=None, ): # 12,6 if type(ims[0]) is np.ndarray: ims = np.array(ims).astype(np.uint8) if (ims.shape[-1] != 3): ims = ims.transpose((0, 2, 3, 1)) f = plt.figure(figsize=figsize) cols = len(ims) // rows if len(ims) % 2 == 0 else len(ims) // rows + 1 for i in range(len(ims)): sp = f.add_subplot(rows, cols, i + 1) sp.axis('Off') if titles is not None: sp.set_title(titles[i], fontsize=16) # plt.imshow(ims[i], interpolation=None if interp else 'none') plt.savefig(fname=fname, dpi=f.dpi) def plot_confusion_matrix(self, cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues, name='V1'): if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.ylabel('True label') plt.xlabel('Predicted label') plt.tight_layout() plt.savefig( os.path.join(self.base_dir, 'confusion_matrix ' + self.name + '.jpg')) plt.clf()
verbose=1) log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1) training_generator = JoinedGenerator(train_it, train_lidar, train_radar) validation_generator = JoinedGenerator(val_it, val_lidar, val_radar) test_generator = JoinedGenerator(test_it, test_lidar, test_radar) history = fusion.fit_generator(training_generator, steps_per_epoch=60, epochs=10, validation_data=validation_generator, validation_steps=10, callbacks=[tensorboard_callback]) loss = fusion.evaluate_generator(test_generator, steps=4) fusion.save_weights('./checkpoints/my_checkpoint') #######################################TEST################################################# image_mod = cv2.imread("1.jpg") image_mod = cv2.resize(image_mod, (150, 150)) image_mod_lidar = cv2.imread("3.jpg") image_mod_lidar = cv2.resize(image_mod_lidar, (150, 150)) image_mod_radar = cv2.imread("6.jpg") image_mod_radar = cv2.resize(image_mod_lidar, (150, 150)) val = fusion.predict([ image_mod.reshape(-1, 150, 150, 3),
validation_steps = 5, epochs = 5, shuffle=True,verbose=1) ''' model.fit_generator(generator=train_generator, steps_per_epoch=STEP_SIZE_TRAIN, validation_data=valid_generator, validation_steps=STEP_SIZE_VALID, epochs=5, shuffle=True, verbose=1) model.evaluate_generator(generator=valid_generator, steps=STEP_SIZE_TEST) test_generator.reset() pred = model.predict_generator(test_generator, steps=STEP_SIZE_TEST, verbose=1) predicted_class_indices = np.argmax(pred, axis=1) labels = (train_generator.class_indices) labels = dict((v, k) for k, v in labels.items()) predictions = [labels[k] for k in predicted_class_indices] filenames = test_generator.filenames results = pd.DataFrame({"Filename": filenames, "Predictions": predictions}) results.to_csv( "D:/My files/courses/108-1/Machine learning & DL/Final project/Task2/rsv3.csv", index=False)
class OptionsDetector(ImgGenerator): def __init__(self, options={}): # input self.DEPTH = 1 self.HEIGHT = 64 self.WEIGHT = 295 self.COLOR_CHANNELS = 3 # outputs 1 self.CLASS_REGION = options.get("class_region", [ "xx-unknown", "eu-ua-2015", "eu-ua-2004", "eu-ua-1995", "eu", "xx-transit", "ru", "kz", "eu-ua-ordlo-dpr", "eu-ua-ordlo-lpr", "ge", "by", "su", "kg" ]) # outputs 2 self.CLASS_STATE = options.get( "class_state", ["garbage", "filled", "not filled", "empty"]) # outputs 3 self.CLASS_COUNT_LINE = options.get("class_count_line", ["0", "1", "2", "3"]) # model self.MODEL = None # model hyperparameters self.OUT_DENSE_INIT = 'uniform' self.OUT_DENSE_ACTIVATION = 'softmax' self.DENSE_ACTIVATION = 'softmax' self.DROPOUT_1 = 0.2 self.DROPOUT_2 = 0.5 self.DENSE_LAYERS = 512 self.ENSEMBLES = 1 self.BATCH_NORMALIZATION_AXIS = -1 self.L2_LAMBDA = 0.001 self.W_REGULARIZER = l2(self.L2_LAMBDA) # callbacks hyperparameters self.REDUCE_LRO_N_PLATEAU_PATIENCE = 10 self.REDUCE_LRO_N_PLATEAU_FACTOR = 0.1 # train hyperparameters self.BATCH_SIZE = 32 self.STEPS_PER_EPOCH = 0 # defain auto self.VALIDATION_STEPS = 0 # defain auto self.EPOCHS = 150 # compile model hyperparameters self.LOSSES = { "REGION": "categorical_crossentropy", # tf.losses.softmax_cross_entropy "STATE": "categorical_crossentropy", # tf.losses.softmax_cross_entropy "COUNT_LINE": "categorical_crossentropy", # tf.losses.softmax_cross_entropy } self.LOSS_WEIGHTS = {"REGION": 1.0, "STATE": 1.0, "COUNT_LINE": 1.0} self.OPT = "adamax" # tf.keras.optimizers.Adamax self.METRICS = ["accuracy"] # for tf self.INPUT_NODE = "input_2:0" self.OUTPUT_NODES = ("REGION/Softmax:0", "STATE/Softmax:0", "COUNT_LINE/Softmax:0") def change_dimension(self, w, h): if w != self.WEIGHT and h != self.HEIGHT: self.HEIGHT = h self.WEIGHT = w if self.MODEL != None: self.MODEL.layers.pop(0) newInput = Input(shape=(self.HEIGHT, self.WEIGHT, self.COLOR_CHANNELS)) newOutputs = self.MODEL(newInput) self.MODEL = Model(newInput, newOutputs) def create_model(self, input_model, conv_base, dropout_1, dropout_2, dense_layers, output_labels1, \ output_labels2, output_labels3, out_dense_init, W_regularizer, \ out_dense_activation, dense_activation, BatchNormalization_axis): # cnn x = conv_base # classificator 1 x1 = layers.Flatten()(x) x1 = layers.Dropout(dropout_2)(x1) x1 = layers.Dense(dense_layers, activation=dense_activation)(x1) x1 = layers.BatchNormalization(axis=BatchNormalization_axis)(x1) x1 = layers.Dense(output_labels1, kernel_initializer=out_dense_init, kernel_regularizer=W_regularizer)(x1) x1 = layers.Activation(out_dense_activation, name="REGION")(x1) # classificator 2 x2 = layers.Flatten()(x) x2 = layers.Dropout(dropout_2)(x2) x2 = layers.Dense(dense_layers, activation=dense_activation)(x2) x2 = layers.BatchNormalization(axis=BatchNormalization_axis)(x2) x2 = layers.Dense(output_labels2, kernel_initializer=out_dense_init, kernel_regularizer=W_regularizer)(x2) x2 = layers.Activation(out_dense_activation, name="STATE")(x2) # classificator 3 x3 = layers.Flatten()(x) x3 = layers.Dropout(dropout_2)(x3) x3 = layers.Dense(dense_layers, activation=dense_activation)(x3) x3 = layers.BatchNormalization(axis=BatchNormalization_axis)(x3) x3 = layers.Dense(output_labels3, kernel_initializer=out_dense_init, kernel_regularizer=W_regularizer)(x3) x3 = layers.Activation(out_dense_activation, name="COUNT_LINE")(x3) #x = keras.layers.concatenate([x1, x2], axis=1) model = Model(inputs=[input_model], outputs=[x1, x2, x3]) # compile model model.compile(optimizer=self.OPT, loss=self.LOSSES, loss_weights=self.LOSS_WEIGHTS, metrics=self.METRICS) return model def ensemble(self, models, model_input): # collect outputs of models in a list outputs = [model(model_input) for model in models] # averaging outputs y = layers.Average()(outputs) # build model from same input and avg output model = Model(inputs=model_input, outputs=y, name='ensemble') return model def prepare(self, base_dir, verbose=1): if verbose: print("START PREPARING") # you mast split your data on 3 directory train_dir = os.path.join(base_dir, 'train') validation_dir = os.path.join(base_dir, 'val') test_dir = os.path.join(base_dir, 'test') # compile generators self.train_generator = self.compile_train_generator( train_dir, (self.HEIGHT, self.WEIGHT), self.BATCH_SIZE) self.validation_generator = self.compile_test_generator( validation_dir, (self.HEIGHT, self.WEIGHT), self.BATCH_SIZE) self.test_generator = self.compile_test_generator( test_dir, (self.HEIGHT, self.WEIGHT), self.BATCH_SIZE) if verbose: print("DATA PREPARED") def create_conv(self, inp): # trainable cnn model conv_base = VGG16(weights='imagenet', include_top=False) # block trainable cnn parameters conv_base.trainable = False return conv_base(inp) def create_simple_conv(self, inp): conv_base = layers.Conv2D(32, (3, 3), activation='relu')(inp) conv_base = layers.MaxPooling2D((2, 2))(conv_base) conv_base = layers.Conv2D(64, (3, 3), activation='relu')(conv_base) conv_base = layers.MaxPooling2D((2, 2))(conv_base) conv_base = layers.Conv2D(128, (3, 3), activation='relu')(conv_base) conv_base = layers.MaxPooling2D((2, 2))(conv_base) conv_base = layers.Conv2D(128, (3, 3), activation='relu')(conv_base) conv_base = layers.MaxPooling2D((2, 2))(conv_base) return conv_base def train(self, log_dir="./", verbose=1, cnn="simple"): # init count outputs self.OTPUT_LABELS_1 = len(self.CLASS_REGION) self.OTPUT_LABELS_2 = len(self.CLASS_STATE) self.OTPUT_LABELS_3 = len(self.CLASS_COUNT_LINE) # create input input_model = Input(shape=(self.HEIGHT, self.WEIGHT, self.COLOR_CHANNELS)) if (cnn == "simple"): conv_base = self.create_simple_conv(input_model) else: conv_base = self.create_conv(input_model) # traine callbacks self.CALLBACKS_LIST = [ callbacks.ModelCheckpoint( filepath=os.path.join(log_dir, 'buff_weights.h5'), monitor='val_loss', save_best_only=True, ), callbacks.ReduceLROnPlateau( monitor='val_loss', factor=self.REDUCE_LRO_N_PLATEAU_FACTOR, patience=self.REDUCE_LRO_N_PLATEAU_PATIENCE, ) ] # train all modelsArr = [] for i in np.arange(self.ENSEMBLES): # create model model = self.create_model(input_model = input_model, conv_base = conv_base, \ dropout_1 = self.DROPOUT_1 , dropout_2 = self.DROPOUT_2, dense_layers = self.DENSE_LAYERS, \ output_labels1 = self.OTPUT_LABELS_1, output_labels2 = self.OTPUT_LABELS_2, \ output_labels3 = self.OTPUT_LABELS_3, out_dense_init = self.OUT_DENSE_INIT, W_regularizer = self.W_REGULARIZER, \ out_dense_activation = self.OUT_DENSE_ACTIVATION , dense_activation = self.DENSE_ACTIVATION, \ BatchNormalization_axis = self.BATCH_NORMALIZATION_AXIS) # train history = model.fit_generator( self.train_generator, steps_per_epoch=self.STEPS_PER_EPOCH, epochs=self.EPOCHS, callbacks=self.CALLBACKS_LIST, validation_data=self.validation_generator, validation_steps=self.VALIDATION_STEPS, verbose=verbose) # load best model model.load_weights(os.path.join(log_dir, 'buff_weights.h5')) # append to models modelsArr.append(model) # merge ensembles if len(modelsArr) > 1: self.MODEL = self.ensemble(modelsArr, input_model) elif len(modelsArr) == 1: self.MODEL = modelsArr[0] return self.MODEL def test(self): test_loss, test_loss1, test_loss2, test_loss3, test_acc1, test_acc2, test_acc3 = self.MODEL.evaluate_generator( self.test_generator, steps=self.VALIDATION_STEPS) print("test loss: {}".format(test_loss)) print("test loss: {} test loss: {} test loss: {}".format( test_loss1, test_loss2, test_loss3)) print("test acc: {} test acc {} test acc {}".format( test_acc1, test_acc2, test_acc3)) return test_loss, test_loss1, test_loss2, test_loss3, test_acc1, test_acc2, test_acc3 def save(self, path, verbose=1): if self.MODEL != None: if bool(verbose): print("model save to {}".format(path)) self.MODEL.save(path) def isLoaded(self): if self.MODEL == None: return False return True @classmethod def get_classname(cls): return cls.__name__ def load(self, path_to_model, options={}, verbose=0): if path_to_model == "latest": model_info = download_latest_model(self.get_classname(), "simple") path_to_model = model_info["path"] options["class_region"] = model_info["class_region"] self.CLASS_REGION = options.get("class_region", [ "xx-unknown", "eu-ua-2015", "eu-ua-2004", "eu-ua-1995", "eu", "xx-transit", "ru", "kz", "eu-ua-ordlo-dpr", "eu-ua-ordlo-lpr", "ge", "by", "su", "kg" ]) self.MODEL = load_model(path_to_model) if verbose: self.MODEL.summary() def getRegionLabel(self, index): return self.CLASS_REGION[index] def getStateLabel(self, index): return self.CLASS_STATE[index] def predict(self, imgs, return_acc=False): Xs = [] for img in imgs: Xs.append(self.normalize(img)) predicted = [[], [], []] if bool(Xs): if len(Xs) == 1: predicted = self.MODEL.predict_on_batch(np.array(Xs)) else: predicted = self.MODEL(np.array(Xs), training=False) regionIds = [] for region in predicted[0]: regionIds.append(int(np.argmax(region))) stateIds = [] for state in predicted[1]: stateIds.append(int(np.argmax(state))) countLines = [] for countL in predicted[2]: countLines.append(int(np.argmax(countL))) if return_acc: return regionIds, stateIds, countLines, predicted return regionIds, stateIds, countLines def predict_pb(self, imgs, return_acc=False): Xs = [] for img in imgs: Xs.append(self.normalize(img)) predicted = [[], [], []] if bool(Xs): tensorX = tf.convert_to_tensor(Xs) predicted = self.pb_model(tensorX) regionIds = [] for region in predicted[0]: regionIds.append(int(np.argmax(region))) stateIds = [] for state in predicted[1]: stateIds.append(int(np.argmax(state))) countLines = [] for countL in predicted[2]: countLines.append(int(np.argmax(countL))) if return_acc: return regionIds, stateIds, countLines, predicted return regionIds, stateIds, countLines def load_pb(self, model_dir): self.pb_model = tf.saved_model.load(model_dir) def getRegionLabels(self, indexes): return [self.CLASS_REGION[index] for index in indexes] def compile_train_generator(self, train_dir, target_size, batch_size=32): # with data augumentation imageGenerator = ImgGenerator(train_dir, self.WEIGHT, self.HEIGHT, self.BATCH_SIZE, [ len(self.CLASS_STATE), len(self.CLASS_REGION), len(self.CLASS_COUNT_LINE) ]) print("start train build") imageGenerator.build_data() self.STEPS_PER_EPOCH = self.STEPS_PER_EPOCH or imageGenerator.n / imageGenerator.batch_size or imageGenerator.n / imageGenerator.batch_size + 1 print("end train build") return imageGenerator.generator() def compile_test_generator(self, test_dir, target_size, batch_size=32): imageGenerator = ImgGenerator(test_dir, self.WEIGHT, self.HEIGHT, self.BATCH_SIZE, [ len(self.CLASS_STATE), len(self.CLASS_REGION), len(self.CLASS_COUNT_LINE) ]) print("start test build") imageGenerator.build_data() self.VALIDATION_STEPS = self.VALIDATION_STEPS or imageGenerator.n / imageGenerator.batch_size or imageGenerator.n / imageGenerator.batch_size + 1 print("end test build") return imageGenerator.generator()
# ca. 188 sek pro Epoche auf Ryzen # Epoch 1/1 93/93 - 188s 2s/step - loss: 0.5926 - acc: 0.7618 - val_loss: 0.6895 - val_acc: 0.7160 # model1: ca. 60 sek pro Epoche auf 2060 Su # Epoch 7/10 93/93 - 59s 634ms/step - loss: 0.2736 - acc: 0.8918 - val_loss: 0.6113 - val_acc: 0.7853 # Epoch 10/10 93/93 - 59s 634ms/step - loss: 0.1869 - acc: 0.9292 - val_loss: 0.8205 - val_acc: 0.7487 # model2: ca. 60 sek pro Epoche auf 2060 Su # Epoch 3/10 93/93 - 61s 651ms/step - loss: 0.3742 - acc: 0.8518 - val_loss: 0.5345 - val_acc: 0.7747 # Epoch 10/10 93/93 - 58s 629ms/step - loss: 0.1587 - acc: 0.9336 - val_loss: 0.9599 - val_acc: 0.7447 # We can now finally evaluate this model on the test data: test_generator = test_datagen.flow_from_directory( test_dir, target_size=(224,224), batch_size=32, class_mode='categorical') test_loss, test_acc = model.evaluate_generator(test_generator, steps=30) test_loss, test_acc