#compile model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) #directory base_dir = "C:/Users/admin/Desktop/python/machine learning/cats_and_dogs_small" train_dir = os.path.join(base_dir, "train") validation_dir = os.path.join(base_dir, 'validation') # scaling 1/255 with all images 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) # validation data should not do data augmentation test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_dir, #target directory target_size=(150, 150), # All image size convert 150x150 batch_size=32, # batch size class_mode='binary' # require label of two value to use binary_crossentropy ) validation_generator = test_datagen.flow_from_directory(validation_dir, target_size=(150, 150),
Convolution2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(64, 64, 3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(units=128, activation='relu')) model.add(Dense(units=1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) from keras_preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) training_set = train_datagen.flow_from_directory('cnn_dataset/training_set/', target_size=(64, 64), batch_size=32, class_mode='binary') test_set = test_datagen.flow_from_directory('cnn_dataset/test_set/', target_size=(64, 64), batch_size=32, class_mode='binary') model.fit(training_set, steps_per_epoch=8000, epochs=25, validation_data=test_set, validation_steps=800)
def main(): image_size = 256 no_classes = 8 if K.image_data_format() == 'channels_first': inputs = Input(shape = (3, image_size, image_size)) else: inputs = Input(shape = (image_size, image_size, 3)) x = Convolution2D(32, (3, 3), strides=(2,2), padding='valid')(inputs) x = IRCNN_base(x) x = Dense(units=no_classes, activation='softmax')(x) model = Model(inputs, x, name='IRCNN') model.summary() from keras import optimizers from keras import callbacks import math # adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999) sgd = optimizers.SGD(lr=0.01) filepath="./weights-{epoch:02d}-{val_accuracy:.2f}.hdf5" mcp = callbacks.ModelCheckpoint(filepath=filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max') callbacks_list = [mcp] model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) from keras_preprocessing.image import ImageDataGenerator aug = ImageDataGenerator(rotation_range=45, fill_mode='wrap', samplewise_center=True, samplewise_std_normalization=True, horizontal_flip=True, vertical_flip=True, validation_split=0.15) bs = 16 train_path = './train' test_path = './test/' train_generator = aug.flow_from_directory( train_path, target_size=(256, 256), class_mode="categorical", batch_size=bs, subset='training') # set as training data validation_generator = aug.flow_from_directory( train_path, # same directory as training data target_size=(256, 256), class_mode="categorical", batch_size=bs, subset='validation') # set as validation data train_step=train_generator.n//train_generator.batch_size valid_step=validation_generator.n//validation_generator.batch_size #model.fit_generator( history = model.fit_generator( train_generator, steps_per_epoch=train_step, validation_data=validation_generator, validation_steps=valid_step, epochs=4, callbacks=callbacks_list, verbose=1) print('\nhistory dict:', history.history) test_gen = ImageDataGenerator() test_data = test_gen.flow_from_directory(test_path, target_size = (256, 256), class_mode=None, batch_size = bs,subset='test') # Evaluate the model on the test data using `evaluate` print('\n# Evaluate on test data') #results = model.evaluate(x_test, y_test, batch_size=128) results = model.evaluate(test_data, batch_size=16) print('test loss, test acc:', results)
import tensorflow as tf import keras_preprocessing from keras_preprocessing.image import ImageDataGenerator TRAINING_DIR = "../dataset" training_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=50, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest', validation_split=0.15) train_generator = training_datagen.flow_from_directory( TRAINING_DIR, target_size=(120, 120), class_mode='categorical', batch_size=16, subset='training') validation_generator = training_datagen.flow_from_directory( TRAINING_DIR, target_size=(120, 120), class_mode='categorical', batch_size=16, subset='validation') model = tf.keras.models.Sequential([ # This is the first convolution tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(120, 120, 3)),
def train(self): train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) valid_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_generator = train_datagen.flow_from_directory( directory=os.path.join(self.data_path, 'train/'), target_size=(200, 200), color_mode="rgb", batch_size=32, class_mode='categorical', shuffle=True, seed=42) valid_generator = valid_datagen.flow_from_directory( directory=os.path.join(self.data_path, 'valid/'), target_size=(200, 200), color_mode="rgb", batch_size=32, class_mode="categorical", shuffle=True, seed=42) test_generator = test_datagen.flow_from_directory( directory=os.path.join(self.data_path, 'test/'), target_size=(200, 200), color_mode="rgb", batch_size=32, class_mode='categorical') STEP_SIZE_TRAIN = train_generator.n // train_generator.batch_size STEP_SIZE_VALID = valid_generator.n // valid_generator.batch_size self.model.fit_generator(generator=train_generator, steps_per_epoch=STEP_SIZE_TRAIN, validation_data=valid_generator, validation_steps=STEP_SIZE_VALID, epochs=10) self.model.evaluate_generator(generator=valid_generator, steps=STEP_SIZE_VALID) STEP_SIZE_TEST = test_generator.n // test_generator.batch_size test_generator.reset() pred = self.model.predict_generator(test_generator, steps=STEP_SIZE_TEST, verbose=1) predicted_class_indices = np.argmax(pred, axis=1) labels = train_generator.class_indices self.labels = dict((v, k) for k, v in labels.items()) predictions = [self.labels[k] for k in predicted_class_indices] print(predictions)
LR, N_NEURONS, N_EPOCHS, BATCH_SIZE, DROPOUT, IMAGE_SIZE, \ resize_image #%% ---------------------------------------- Set-Up -------------------------------------------------------------------- SEED = 42 os.environ['PYTHONHASHSEED'] = str(SEED) random.seed(SEED) np.random.seed(SEED) tf.random.set_seed(SEED) weight_init = glorot_uniform(seed=SEED) #%% train_df = pd.read_csv(training_images_list_filename) test_df = pd.read_csv(validation_images_list_filename) datagen = ImageDataGenerator(rescale=1. / 255., validation_split=0.25) train_generator = datagen.flow_from_dataframe(dataframe=train_df, directory=None, x_col="name", y_col="class", subset="training", batch_size=32, seed=42, shuffle=True, class_mode="categorical", target_size=(32, 25)) valid_generator = datagen.flow_from_dataframe(dataframe=train_df, directory=None, x_col="name",
from keras.models import load_model import cv2 import numpy as np import matplotlib.pyplot as plt from keras.preprocessing import image from keras_preprocessing.image import ImageDataGenerator import os dir = os.getcwd() validation_datagen = ImageDataGenerator(rescale=1. / 255) validation_dir = dir + '/validation' val_batchsize = 1 image_size = 224 model = load_model('small_last5.h5') # Create a generator for prediction validation_generator = validation_datagen.flow_from_directory( validation_dir, target_size=(image_size, image_size), batch_size=val_batchsize, class_mode='categorical', shuffle=False) # Get the filenames from the generator fnames = validation_generator.filenames # Get the ground truth from generator ground_truth = validation_generator.classes # Get the label to class mapping from the generator label2index = validation_generator.class_indices
from keras.models import load_model from keras_preprocessing.image import ImageDataGenerator from ImageClassification_Training import preprocess_image if __name__ == '__main__': test_dir = "data/fruits-360_dataset/fruits-360/Test" checkpoint_file = 'data/models/pani_adam_200_cnn.hdf5' test_datagen = ImageDataGenerator(rescale=1 / 255) test_generator = test_datagen.flow_from_directory(test_dir, target_size=(100, 100)) label_map = test_generator.class_indices model = load_model(checkpoint_file) img_tensor = preprocess_image("data/upload/apples1.jpg") classes = model.predict_classes(img_tensor, batch_size=1) for label, num in label_map.items(): if num == classes: print("I think this is a:", label) score = model.evaluate_generator(test_generator, verbose=1) print('Test Accuracy: ', score[1])
def build_and_train_models(): # load MNIST dataset #(x_train, _), (_, _) = mnist.load_data() x_train = np.load('/home/ld07063u/data/data_crop_128_grayscale_inv.npy') # reshape data for CNN as (28, 28, 1) and normalize datagen = ImageDataGenerator() datagen.fit(x_train) i = 0 print('running data augmentation******************************') for X_batch in datagen.flow(x_train, batch_size=200): i = i + 1 print(i) if (i > 1000): break x_train = np.append(x_train, X_batch, axis=0) # reshape data for CNN as (28, 28, 1) and normalize print('The shape of the training data is :', x_train.shape) image_size = 128 x_train = np.reshape(x_train, [-1, image_size, image_size, 1]) x_train = x_train.astype('float32') / 255 model_name = "dcgan_128_gs" # network parameters # the latent or z vector is 100-dim latent_size = 100 batch_size = 64 train_steps = 60000 lr = 2e-4 decay = 6e-8 input_shape = (image_size, image_size, 1) # build discriminator model inputs = Input(shape=input_shape, name='discriminator_input') discriminator = build_discriminator(inputs) # [1] or original paper uses Adam, # but discriminator converges easily with RMSprop optimizer = RMSprop(lr=lr, decay=decay) discriminator.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) discriminator.summary() # build generator model input_shape = (latent_size, ) inputs = Input(shape=input_shape, name='z_input') generator = build_generator(inputs, image_size) generator.summary() # build adversarial model optimizer = RMSprop(lr=lr * 0.5, decay=decay * 0.5) # freeze the weights of discriminator during adversarial training discriminator.trainable = False # adversarial = generator + discriminator adversarial = Model(inputs, discriminator(generator(inputs)), name=model_name) adversarial.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) adversarial.summary() # train discriminator and adversarial networks models = (generator, discriminator, adversarial) params = (batch_size, latent_size, train_steps, model_name) train(models, x_train, params)
epochs = 100 validate_rate = 0.2 bt_size = 256 param = {} param['Epochs'] = epochs param['Validate_Rate'] = validate_rate param['Batch_size'] = bt_size param['Learning_Rate'] = learn_rate param['Weight_initializer'] = 'he_normal' param['Bias_initializer'] = 'Constant 0.01' datagen = ImageDataGenerator( rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, zoom_range=0.2, horizontal_flip=True, validation_split=validate_rate ) train_generator = datagen.flow( x_train, y_train, batch_size=bt_size, subset='training' ) validate_generator = datagen.flow( x_train, y_train, batch_size=bt_size, subset='validation' ) K.clear_session()
update_y = False # Define our custom metric def PSNR(y_true, y_pred): max_pixel = 1.0 return 10.0 * (1.0 / math.log(10)) * K.log((max_pixel ** 2) / (K.mean(K.square(y_pred - y_true)))) # parameters epochs = int(sys.argv[1]) batch_size = int(sys.argv[2]) # Create generator datagen = ImageDataGenerator(validation_split=0.1, rescale=1./255) # Prepare training and validation datasets train_it = datagen.flow_from_directory(directory='data/big/', target_size=(200,80), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='training') val_it = datagen.flow_from_directory(directory='data/big/', target_size=(200,80), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='validation') train_small_it = datagen.flow_from_directory(directory='data/small/', target_size=(100,40), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='training', interpolation = 'bilinear') val_small_it = datagen.flow_from_directory(directory='data/small/', target_size=(100,40), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='validation', interpolation = 'bilinear') # Build model input_img = Input(shape=(100, 40, 1)) # adapt this if using `channels_first` image data format x = UpSampling2D((2, 2), interpolation='bilinear')(input_img) x = Conv2D(64, (9, 9), activation='relu', padding='same')(x) x = Conv2D(32, (3, 3), activation='relu', padding='same')(x) x = Conv2D(1, (3, 3), activation='relu', padding='same')(x) upsample = Model(input_img, x)
callbacks = EarlyStopping(monitor='val_loss', patience=1, verbose=1, mode='auto') # autosave best Model best_model_file = "data_augmented_weights.h5" best_model = ModelCheckpoint(best_model_file, monitor='val_acc', verbose=2, save_best_only=True) # In[16]: train_datagen = ImageDataGenerator(shear_range=0.1, zoom_range=0.1, rotation_range=10., width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True) val_datagen = ImageDataGenerator() history = vgg16_model.fit_generator(train_datagen.flow(X_train, Y_train, batch_size=batch_size), nb_epoch=epochs, samples_per_epoch=len(X_train), validation_data=val_datagen.flow( X_test, Y_test, batch_size=64, shuffle=False),
import tensorflow as tf from keras_preprocessing.image import ImageDataGenerator cst_batch = 16 size = 250 train_datagen = ImageDataGenerator(rescale=1. / 255, zoom_range=0.2) train_set = train_datagen.flow_from_directory('train', batch_size=cst_batch, shuffle=True, target_size=(size, size), interpolation='bicubic', class_mode='binary', color_mode='grayscale') val_datagen = ImageDataGenerator(rescale=1. / 255, zoom_range=0.2) val_set = val_datagen.flow_from_directory('val', batch_size=cst_batch, shuffle=True, target_size=(size, size), interpolation='bicubic', class_mode='binary', color_mode='grayscale') cnn = tf.keras.models.Sequential() cnn.add( tf.keras.layers.Conv2D(50, (3, 3), activation='relu', input_shape=(size, size, 1))) cnn.add(tf.keras.layers.AveragePooling2D((2, 2)))
def Train_whole_Test(Tiles_Dir, network, Results_Dirs, Project_Dir=Current_Dir, experiment='Hist', cvtype='_FinalTest_', DataType='TestData'): # Set parameters seed = 42 batchsize = 150 # Load the data - USE OF THE CREATED CLASS 'DataGen' ---------------- # DataGen will generate one data fold for the Training and one data fold for the Testing Train_Data = DataGen(Tiles_Dir, 'None', 'train', Project_Dir).Generate() Test_Data = DataGen(Tiles_Dir, 'None', 'test', Project_Dir).Generate() #----------------------------------------------------------------------------------------------------------------------- # ********** Training on the whole training data of the selected dataset and Predicting on the primarily kept-out test data ********** # This is a main loop that will run as many times as the number of the selected folds when calling the Train_Val_Pred() #----------------------------------------------------------------------------------------------------------------------- # Clear the previous session to control for OOM Errors in every next run K.clear_session() # Set the target size needed for the ImageDataGenerators if network == 'Xception' or network == 'InceptionV3': targetsize = (299, 299) else: targetsize = (224, 224) # ------------------------------------------------------------------------------------------------------------------ # Re-sampling and shuffling data # ------------------------------------------------------------------------------------------------------------------ # NOTE: The internal validation data can be used for final evaluation, as they are not used in the training to update the gradient descent # Set the validation and test data (THE SAME TABLE IS USED FROM BOTH) PredDF = shuffle(shuffle(Test_Data)) # ------------------------------------------------------------------------------------------------------------------ # Re-sampling the Train Data # Same number of examples per Class # ------------------------------------------------------------------------------------------------------------------ # Dictionary of class keys and their number of examples -imbalanced Imbalanced_Classes_TrainData = Counter(Train_Data['Class']) print('\n The Training tiles per Class before re-sampling:{}'.format( Imbalanced_Classes_TrainData)) # Find the number of examples in the minority class Minority_class_Train = np.array(list( Imbalanced_Classes_TrainData.values())).min() # Instantiate an new Train data table Train_resampled = pd.DataFrame() # Select as many examples per Class as those in the minority class # Essentially rows are selected from the primary Train data, but it cannot be controled that also each patient will contribute the same number of images for theClass in list(Imbalanced_Classes_TrainData.keys()): Train_resampled = Train_resampled.append( Train_Data[Train_Data['Class'] == theClass].sample( n=Minority_class_Train, replace=False)) # Dictionary of class keys and their number of examples-balanced Balanced_Classes_Train = Counter(Train_resampled['Class']) print('\n The Training tiles per Class after Under-sampling:{}'.format( Balanced_Classes_Train)) # Finally, very important! ---> SHUFFLE THE TRAIN DATA TrainDF = shuffle(shuffle(Train_resampled, random_state=42)) # ------------------------------------------------------------------------------------------------------------------ # Prepare the Image data generators # The data will flow from dataframes !!! # Dataframe Structure: (column A: Images Full Paths, column B: Class as String) #------------------------------------------------------------------------------------------------------------------- # T-R-A-I-N-I-N-G print(" \n The training samples are: ") train_gen = ImageDataGenerator(rescale=1. / 255, vertical_flip=True) train_generator = train_gen.flow_from_dataframe( dataframe=pd.DataFrame(data={ 'filename': TrainDF['FULL_PATH'], 'class': TrainDF['Class'] }), directory=None, image_data_generator=train_gen, x_col='filename', y_col='class', color_mode='rgb', save_prefix='', target_size=targetsize, batch_size=batchsize, shuffle=True, class_mode='sparse', save_format='jpg', interpolation='nearest', validate_filenames=True, seed=seed) # V-A-L-I-D-A-T-I-O-N (using the test data) print(" \n The samples for validation after each epoch are: ") test_gen = ImageDataGenerator(rescale=1. / 255) valid_generator = test_gen.flow_from_dataframe( dataframe=pd.DataFrame(data={ 'filename': PredDF['FULL_PATH'], 'class': PredDF['Class'] }), directory=None, image_data_generator=train_gen, x_col='filename', y_col='class', color_mode='rgb', save_prefix='', target_size=targetsize, batch_size=batchsize, shuffle=True, class_mode='sparse', save_format='jpg', interpolation='nearest', validate_filenames=True, seed=seed) # FOR P-R-E-D-I-C-T-I-O-N (using the test data, that was also used for validation) - THE NETWORK DOES NOT USE THIS DATA FOR TRAINING !! # This is the same generator as the previous, with the only differences that, when testing, batch_size = 1 and Shuffle= False print(" \n The testing samples are: ") PredDF_generator = test_gen.flow_from_dataframe( dataframe=pd.DataFrame(data={ 'filename': PredDF['FULL_PATH'], 'class': PredDF['Class'] }), directory=None, image_data_generator=test_gen, x_col='filename', y_col='class', color_mode='rgb', save_prefix='', target_size=targetsize, batch_size=1, shuffle=False, class_mode='sparse', save_format='jpg', interpolation='nearest', validate_filenames=True, seed=seed) # ------------------------------------------------------------------- # LOAD THE MODEL #-------------------------------------------------------------------- # Arguments: Set_Network(network_name, number_classes) model = Set_Network(network, len(train_generator.class_indices.keys())) # Epochs first epochs_first, epochs_total = 6, 10 lr_1 = 3e-4 lr_2 = 3e-5 #_____________________COMPILE THE MODEL _____________________________ # 1st compilation (to train only the added dense layers) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(lr=lr_1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=lr_1 / epochs_first), metrics=['sparse_categorical_accuracy']) # Define the steps per epoch train_steps, val_steps = len(train_generator), len(valid_generator) # ___________________ T-R-A-I-N the model___________________________ # (initialization of the weights in the added layers) print( " \n *** Train the {} with the basemodel layers untrained. Only the weights of the added Dense Layers are unfreezed." .format(network)) Fit_history = model.fit_generator(generator=train_generator, validation_data=valid_generator, shuffle=True, steps_per_epoch=train_steps, validation_steps=val_steps, epochs=epochs_first, verbose=1, workers=4, max_queue_size=20, use_multiprocessing=False) # ____________________Fine-tuning the Base_Model ____________________ # Unfreeze convolutional layers from the current baseline network if network == 'ResNet50': net = 'resnet50' for layer in model.get_layer(net).layers[:165]: layer.trainable = False # Fine-tuning the last 10 layers for layer in model.get_layer(net).layers[165:]: layer.trainable = True elif network == 'InceptionV3': net = 'inception_v3' for layer in model.get_layer(net).layers[:249]: layer.trainable = False #Fine-tuning the top 2 Inception blocks for layer in model.get_layer(net).layers[249:]: layer.trainable = True elif network == 'Xception': net = 'xception' for layer in model.get_layer(net).layers[:-16]: layer.trainable = False # Fine-tuning the last 16 layers for layer in model.get_layer(net).layers[-16:]: layer.trainable = True #__________________________ RE - COMPILE THE MODEL __________________________________ # 2nd compilation (to train both the unfreezed Conv and the added dense layers) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(lr=lr_2, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.00005), metrics=['sparse_categorical_accuracy']) # Prepare a Callback to track the training and validation accuracy, and the training and validation loss checkpoint = ModelCheckpoint(Results_Dirs + '\\' + os.path.basename(Tiles_Dir) + '_' + network + '_' + experiment + '_Final' + 'Acc.h5', monitor='sparse_categorical_accuracy', verbose=0, save_best_only=True) # Save the training resulted accuracies and losses, only for the epochs after the 2nd model compilation # The metrics from training after the 1st model compilation are only printed in the console during the first part of the training csv_logger = CSVLogger( os.path.basename(Tiles_Dir) + '_' + network + '_' + experiment + '_Final' + ".log") # Reset data generators train_generator.reset() valid_generator.reset() #___________________ RE - TRAIN the model___________________________ # (Weights of the unfreezed layers will also be updated) print( " \n *** Train the chosen last Conv Layers of the {} and the added Dense Layers." .format(network)) # Continue the re-training from the last epoch of the previous training FineTune_history = model.fit_generator(generator=train_generator, validation_data=valid_generator, shuffle=True, epochs=epochs_total, initial_epoch=Fit_history.epoch[-1], steps_per_epoch=train_steps, validation_steps=val_steps, callbacks=[checkpoint, csv_logger], verbose=1, workers=4, max_queue_size=20, use_multiprocessing=False) # Save the final model, where the weights from the last Conv chosen layers and the added ones were updated model.save( os.path.basename(Tiles_Dir) + '_' + network + '_' + experiment + '_Final' + '.h5') model.save_weights( os.path.basename(Tiles_Dir) + '_' + 'Weights_' + network + '_' + experiment + '_Final' + '.h5') # -------------------------- Plot the training and validation metrics from the last training --------------------- # Note: The following lines 258-272 are slightly adjusted and come from a plotting paradigm on 'tensorflow.org', # https://www.tensorflow.org/tutorials/images/classification?authuser=0&hl=zh-cn plt.style.use('seaborn-colorblind') fig, axs = plt.subplots(2) axs[0].plot(np.arange(0, epochs_total - epochs_first + 1), FineTune_history.history["loss"], "r", np.arange(0, epochs_total - epochs_first + 1), FineTune_history.history["val_loss"], "-bo") axs[0].set_ylabel("Loss") axs[0].set_xlabel("Epochs") axs[0].set_title('Training and validation accuracy and loss', fontsize=12, y=1.109) plt.legend(["train", "val"], loc="best") axs[1].plot(np.arange(0, epochs_total - epochs_first + 1), FineTune_history.history["sparse_categorical_accuracy"], "r", np.arange(0, epochs_total - epochs_first + 1), FineTune_history.history["val_sparse_categorical_accuracy"], "-bo") axs[1].set_ylabel("Accuracy") axs[1].set_xlabel("Epochs") plt.legend(["train", "val"], loc='best') fig.tight_layout() fig = plt.gcf() plt.show() plt.draw() fig.savefig(Results_Dirs + '\\' + network + '_' + experiment + '_' + '_Final' + ".png", dpi=1200, quality=95) plt.close() # --------------------------------------------------------------------------------------------------------------------------- # PREDICTIONS FOR THE KEPT-OUT TEST DATA #---------------------------------------------------------------------------------------------------------------------------- # Use the function 'Predict.py' to return the soft predictions PredDF_generator.reset() print(" \n Predicting on the last fold of data:") # ------------------------------------------------------------------- # P-R-E-D-I-C-T-I-N-G #-------------------------------------------------------------------- # Leave the Idx empty Idx = '' # Predict Predictions_FoldData, Predicted_Filtered = Predict( PredDF, model, network, 'TestData', PredDF_generator, Idx, Results_Dirs, cvtype, experiment, Project_Dir=Current_Dir).predictions() # Gather the correct predictions per patient and classes # Those where the highest predicted probabilities indeed belong to the True Label Correct_Predictions = Predicted_Filtered.drop(['Predicted', 'Position'], 1) Correct_Predictions = Correct_Predictions.rename( columns={'True_Positives': 'Probability'}) Correct_Predictions = Correct_Predictions.sort_index(level=0) Patients = Correct_Predictions.index.unique() # This is a list where each entry is a Dataframe. Each dataframe has the results from one patient: # (patient image, Predicted_Label, True_Label, Predicted_Accuracy) ListResultsPerPatient = [] for els in list(Patients): ListResultsPerPatient.append( Correct_Predictions[Correct_Predictions.index == els]) """ Creat a dictionary with the class, the patient ids for this class, and the mean Probability from all of the images of each patient: patient_1 : Mean Prob Class1 patient_2 : Mean Prob ... : ... patient_1 : Mean Prob Class2 patient_2 : Mean Prob """ # Instantiate an empty dictionary MeanPntProb = {} # Iterate over each patient's results table to retrieve the Average Acc for order, patient in enumerate(ListResultsPerPatient): Index = patient.index.unique()[0] Subtype = patient['True_Labels'].unique()[0] MeanPntProb[Subtype, Index] = patient['Probability'].mean() # Sort the MeanPntAcc based on the class name Sorted_MeanPntProb = OrderedDict( sorted(MeanPntProb.items(), key=lambda val: val[0])) # This is the final Average Acc dataframe with two levels of indices (Class, patient id) Final_DF_AVERAGE_Prob_Pnts = pd.DataFrame( Sorted_MeanPntProb.values(), pd.MultiIndex.from_frame(pd.DataFrame(Sorted_MeanPntProb.keys()), names=['Subtype', 'Patient'])) Final_DF_AVERAGE_Prob_Pnts = Final_DF_AVERAGE_Prob_Pnts.rename( columns={0: 'Average_Probability'}) # Save the dataframe to an *.xlsx file in the results folder Final_DF_AVERAGE_Prob_Pnts.to_excel(Results_Dirs + '\\' + os.path.basename(Tiles_Dir) + '_' + experiment + '_' + network + '_' + '_Average_Prob_ClassPnt' + cvtype + '.xlsx') # -------------------------------------------------------------------------------------------------------------------------- # The following plots are based on the image tiles, with the results not on the patient level but only on the class level #--------------------------------------------------------------------------------------------------------------------------- # Plot the roc curve for each class for the current last fold used for predictions plot_roc( np.array(PredDF_generator.classes), Predictions_FoldData, PredDF, Idx, title='ROC Curve per class for predicting on the final Testing Data', plot_micro=False, plot_macro=False, classes_to_plot=None, ax=None, figsize=(14, 7), cmap='tab20c', title_fontsize='x-large', text_fontsize='x-large') plt.savefig(Results_Dirs + '\\' + experiment + '_' + network + 'Roc_Curves_' + DataType + '_Final' + '.png', dpi=1200, quality=95) plt.close() # Plot the precision-recall curves for the current used fold plot_precision_recall_curve( np.array(PredDF_generator.classes), Predictions_FoldData, PredDF, Idx, title= 'Precision_Recall Curve per class for predicting on the final Testing Data', ax=None, figsize=(14, 7), cmap='tab20c', title_fontsize='x-large', text_fontsize='large') plt.savefig(Results_Dirs + '\\' + experiment + '_' + network + 'Precision_Recall_Curves_' + DataType + '_Final' + '.png', dpi=1200, quality=95) plt.close() # E V A L U A T I O N # Export the evaluation loss and accuracy, after evaluating the model on the Test Data Scores = model.evaluate_generator(PredDF_generator, steps=len(PredDF_generator)) for the, metric in enumerate(model.metrics_names): print('{}: {}'.format(metric, Scores[the])) # Save the evaluation accuracy and loss Scores_df = pd.DataFrame(data={ 'loss': Scores[0], 'Accuracy': Scores[1] }, index=['metrics']) Scores_df.to_excel(Results_Dirs + '\\' + os.path.basename(Tiles_Dir) + '_' + experiment + '_' + network + '_' + '_Eval_Scores' + cvtype + '.xlsx')
BATCH_SIZE = 32 NUM_TRAINING_EXAMPLES = 36808 NUM_VALIDATION_EXAMPLES = 3197 CLASS_WEIGHTS = [1, 1.6] INIT_LEARNING_RATE = 1e-4 ALPHA_REG = 1e-5 init_epoch = 0 MODEL_NAME = f'Adam-DECAY-{INIT_LEARNING_RATE}-PREACTIVATION-ImageDIM{IMAGE_DIM}-l1_l2-REG-{ALPHA_REG}-{DATA_TYPE}-weights-c64xr{NUM_64_BLOCK}-c128xr{NUM_128_BLOCK}-' \ f'c256xr{NUM_256_BLOCK}-c512xr{NUM_512_BLOCK}-MAXPOOL' aug = ImageDataGenerator( rotation_range=45, horizontal_flip=True, vertical_flip=True, dtype='float16' ) callbacks = [ ModelCheckpoint(f'Models/{MODEL_NAME}/' + 'weights.{epoch:02d}-{val_loss:.2f}-new_best.hdf5', monitor='val_loss', save_best_only=True, verbose=0), ModelCheckpoint(f'Models/{MODEL_NAME}/' + 'weights.{epoch:02d}-{val_loss:.2f}.hdf5', period=5, verbose=1), ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=50, verbose=1), TensorBoard(log_dir=f'logs/{MODEL_NAME}') ] def mkdir(file_path): """
if __name__ == '__main__': os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = "0" config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.95 session = tf.Session(config=config) K.set_session(session) train_datagen = ImageDataGenerator(rotation_range=5, width_shift_range=0.0, height_shift_range=0.0, brightness_range=(0.85, 1.15), shear_range=0.0, zoom_range=0.1, channel_shift_range=0.3, fill_mode='nearest', horizontal_flip=True, vertical_flip=False) valid_datagen = ImageDataGenerator() train = train_datagen.flow_from_directory(train_dir, target_size=img_size, color_mode='rgb', batch_size=32, interpolation='bicubic') valid = valid_datagen.flow_from_directory(val_dir, target_size=img_size, color_mode='rgb', batch_size=32,
model_cat_auto_color.compile(optimizer='RMSprop', loss='mse') import os # 我能不能预先载入模型,继续训练? 10月14日 17点57分 if os.path.exists('.\\cats_auto_color.h5') == True: print('模型文件已存在。') model_cat_auto_color.load_weights('.\\cats_auto_color.h5') print('模型参数载入成功') # train_X_Data这个文件夹下面必须只有一个子文件夹,里面全都是训练图片 # train_Y_Data同理 train_X_dir = 'D:\\testPicture\\autoColor\\x1\\' train_Y_dir = 'D:\\testPicture\\autoColor\\y1\\' from keras_preprocessing.image import ImageDataGenerator train_X_dataGen = ImageDataGenerator(rescale=1. / 255) train_Y_dataGen = ImageDataGenerator(rescale=1. / 255) batch_size = int(input('batch的大小是多少?输入数字按回车结束:')) """ 现在主要是解决generator生成黑白图像和彩色不匹配的问题 不匹配是没办法训练的 此训练版本为单图训练版,文件夹里只有一张图片10.24 """ train_X_generator = train_X_dataGen.flow_from_directory( train_X_dir, target_size=(256, 256), batch_size=batch_size, class_mode='binary', color_mode="grayscale" # 提取为灰度图片 )
model_name = 'regression_kali_0-60_train_lr0001_model_%s_train_bs%d_%s_lr%s_%s_pos' % (backbone, batch_size, learning_rate, optimizer, loss) model_save_path = '%s.hdf5' % model_name log_save_path = '%s-train-%s.log' % (model_name, get_git_revision_hash()) elif backbone == 'inceptionv3': BACKBONE = InceptionV3 elif backbone == 'resnet50': BACKBONE = ResNet50 elif backbone == 'efficientnetb2': # noinspection PyUnresolvedReferences from efficientnet import EfficientNetB2 BACKBONE = EfficientNetB2 if optimizer == 'adam': OPTIMIZER = Adam noAugGen = ImageDataGenerator(rescale=1. / 255,samplewise_center=True,samplewise_std_normalization=True,) # Load data. if data_augmentation: genImg = ImageDataGenerator( rescale=1. / 255, samplewise_center=True, samplewise_std_normalization=True, width_shift_range=0.01, height_shift_range=0.01, brightness_range=[0.9, 1.1] ) else: genImg = ImageDataGenerator(rescale=1. / 255) # genImg.flow_from_dataframe()
num_valid_steps = math.floor(num_valid_samples / BATCH_SIZE) model = cnn() last = model.layers[-1].output #输出 x = Dense(1, activation="sigmoid")(last) model = Model(model.input, x) model.summary() model.compile( loss='binary_crossentropy', #损失函数:对数损失 optimizer='rmsprop', #优化器 metrics=['accuracy']) train_datagen = ImageDataGenerator( rescale=1. / 255, #归一化 shear_range=0.2, #随机错位切换的角度 zoom_range=0.2, #图片随机缩放的范围 horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( 'G:/python/untitled1/demo/data/train', #图片地址 target_size=(224, 224), # 调整图片大小为 150x150 batch_size=16, #设置批量数据的大小为32 class_mode='binary') #设置返回标签的类型 ,应对应损失函数 validation_generator = test_datagen.flow_from_directory( 'G:/python/untitled1/demo/data/validation', target_size=(224, 224), batch_size=16, class_mode='binary')
# Parameters training_data_codename = "accumulative_data_4-9_actual" dir_train = "E:/Artificial Intelligence/naruto/combined_training_data/" dir_val = "E:/Artificial Intelligence/naruto/validation_data/" WIDTH = 165 HEIGHT = 235 EPOCHS = 50 batch_size = 32 MODEL_NAME = f'VGG16_EPOCHS{EPOCHS}_CODENAME_{training_data_codename}_{time.time()}' # PREV_MODEL = 'VGG16_EPOCHS50_CODENAME_accumulative_data_4-9_1586483732.9708767' # model = models.load_model(PREV_MODEL) # datagen = ImageDataGenerator(width_shift_range=[-.10,.10], height_shift_range=[-.10,.10], # brightness_range=[.8,1.1], zoom_range=[.7,1.3]) datagen = ImageDataGenerator() train_it = datagen.flow_from_directory(dir_train, batch_size=batch_size, target_size=(HEIGHT, WIDTH)) val_it = datagen.flow_from_directory(dir_val, batch_size=batch_size, target_size=(HEIGHT, WIDTH)) test_it = None # ------------------------ # Import and Build Model # ------------------------ vgg_base = VGG16(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) optimizer = optimizers.Nadam()
def train(self, save_name: str = None): train_path = 'D:\\univ\\3 grade\\kursach\\main\\data\\train' validate_path = 'D:\\univ\\3 grade\\kursach\\main\\data\\validate' test_path = 'D:\\univ\\3 grade\\kursach\\main\\data\\test' x_train, y_train = self.__load_data(train_path) x_validate, y_validate = self.__load_data(validate_path) x_test, y_test = self.__load_data(test_path) batch_size = 128 self.num_classes = 2 epochs = 30 img_rows, img_cols = 64, 64 if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_validate = x_validate.reshape(x_validate.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) self.input_shape = (1, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_validate = x_validate.reshape(x_validate.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) self.input_shape = (img_rows, img_cols, 1) x_train = x_train.astype('float32') x_validate = x_validate.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_validate /= 255 x_test /= 255 print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_validate.shape[0], 'test samples') y_train = keras.utils.to_categorical(y_train, self.num_classes) y_validate = keras.utils.to_categorical(y_validate, self.num_classes) y_test = keras.utils.to_categorical(y_test, self.num_classes) model = self.__create_model() print_summary(model) aug = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, fill_mode="nearest") h = model.fit_generator(aug.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, steps_per_epoch=len(x_train) // batch_size, validation_data=(x_validate, y_validate)) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) if save_name is not None: model.save(Config.BASE_DIR + '/cnn/models/' + save_name + '.h5') print_summary(model) plt.plot(h.history['acc']) plt.plot(h.history['val_acc']) plt.title('Model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.show() # Plot training & validation loss values plt.plot(h.history['loss']) plt.plot(h.history['val_loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.show()
def solution_model(): # url = 'https://storage.googleapis.com/download.tensorflow.org/data/rps.zip' # urllib.request.urlretrieve(url, 'rps.zip') # local_zip = 'rps.zip' # zip_ref = zipfile.ZipFile(local_zip, 'r') # zip_ref.extractall('C:/data/image/') # zip_ref.close() TRAINING_DIR = "C:/data/image/rps/" train_datagen = ImageDataGenerator( rescale = 1.0/255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, validation_split=0.2 ) train_generator = train_datagen.flow_from_directory( TRAINING_DIR, batch_size = 32, class_mode='categorical', target_size = (150, 150), subset = 'training' ) validation_datagen = ImageDataGenerator( rescale=1.0/255, validation_split=0.2 ) validation_generator = validation_datagen.flow_from_directory( TRAINING_DIR, batch_size = 32, class_mode='categorical', target_size = (150, 150), subset = 'validation' ) model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(128, (2,2), padding='same', activation = 'relu', input_shape=(150, 150, 3)), tf.keras.layers.BatchNormalization(), tf.keras.layers.Conv2D(64, 2, padding='same', activation='relu'), tf.keras.layers.Conv2D(32, 2, padding='same', activation='relu'), tf.keras.layers.MaxPool2D(3,3), tf.keras.layers.Flatten(), tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(3, activation='softmax') # 다중분류 ]) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) # history= model.fit_generator( # train_generator, steps_per_epoch=32, epochs=20, # validation_data=validation_generator # ) history = model.fit(train_generator, steps_per_epoch=8, epochs=40, verbose=1, validation_data=validation_generator, validation_steps=8 ) loss, acc = model.evaluate(validation_generator) print("loss : ", loss) print("acc : ", acc) return model
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['categorical_accuracy']) return model model = build_model() train_gen = ImageDataGenerator( rescale=1.0 / 255.0, featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=30, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range= 0.1, # randomly shift images horizontally (fraction of total width) height_shift_range= 0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) test_gen = ImageDataGenerator(rescale=1.0 / 255.0) training = train_gen.flow(X_train, y_train, batch_size=32) testing = test_gen.flow(X_test, y_test, batch_size=32) earlystopper = EarlyStopping(monitor='val_loss', patience=30, mode='max')
# This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization= False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening zca_epsilon=1e-06, # epsilon for ZCA whitening rotation_range= 0, # 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., # set range for random shear zoom_range=0., # 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='channels_last', # fraction of images reserved for validation (strictly between 0 and 1) validation_split=0.0)
'/home/jovyan/work/CNV/img/val', batch_size=batch_size, image_size=(201, 200), class_names=['nor', 'del','dup']) ''' import pandas as pd import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras.layers.experimental.preprocessing import CenterCrop from tensorflow.keras.layers.experimental.preprocessing import Rescaling import kerastuner from keras_preprocessing.image import ImageDataGenerator df = pd.read_csv("/home/jovyan/work/CNV/1Dimg/train/train.csv") df["labels"]=df["labels"].apply(lambda x:x.split(",")) datagen=ImageDataGenerator(rescale=1./255.) train_generator=datagen.flow_from_dataframe( dataframe=df, directory="/home/jovyan/work/CNV/1Dimg/train", x_col="filename", y_col="labels", batch_size=32, seed=42, shuffle=True, class_mode="categorical", classes=["del","dup","nor"], target_size=(1,512)) test_datagen=ImageDataGenerator(rescale=1./255.) df = pd.read_csv("/home/jovyan/work/CNV/1Dimg/val/val.csv") df["labels"]=df["labels"].apply(lambda x:x.split(","))
"../Previous_Files/my_model_butterfly_VGG16_fitGen_fine-tuned") # Step 1 - Set up fine tuning on pre-trained ImageNet vgg19 model - train all layers for VGG16 and VGG19 models but only the Layers from # 94 and above for the Inception V3 and Xception models for layer in model.layers: layer.trainable = True # Step 2 - Compile the revised model using SGD optimizer with a learing rate of 0.0001 and a momentum of 0.9 model.compile(optimizer=SGD(lr=0.00001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) train_datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, preprocessing_function=preprocess_input, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) validation_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) BATCH_SIZE = 16 IMAGE_SIZE = (200, 200) print("Fine-Tuning current model") train_generator = train_datagen.flow_from_directory( 'E:/360MoveData/Users/11047/Desktop/Aritificial Intelligence/Coursework/data_split_new/train', target_size=IMAGE_SIZE, batch_size=BATCH_SIZE,
img = mpimg.imread(img_path) plt.imshow(img) plt.axis('Off') plt.show() import tensorflow as tf import keras_preprocessing from keras_preprocessing import image from keras_preprocessing.image import ImageDataGenerator TRAINING_DIR = "/tmp/rps/" training_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, fill_mode='nearest') VALIDATION_DIR = "/tmp/rps-test-set/" validation_datagen = ImageDataGenerator(rescale = 1./255) train_generator = training_datagen.flow_from_directory( TRAINING_DIR, target_size=(150,150), class_mode='categorical' ) validation_generator = validation_datagen.flow_from_directory(
def build_model(): model = Sequential() model.add( Conv2D(filters=32, kernel_size=(3, 3), activation="relu", padding="same", input_shape=(256, 256, 1))) model.add( Conv2D(filters=32, kernel_size=(3, 3), activation="relu", padding="same")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(rate=0.25)) model.add( Conv2D(filters=64, kernel_size=(3, 3), activation="relu", padding="same")) model.add( Conv2D(filters=64, kernel_size=(3, 3), activation="relu", padding="same")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(rate=0.25)) model.add(Flatten()) model.add(Dense(1024, activation="relu")) model.add(BatchNormalization()) model.add(Dropout(rate=0.4)) model.add(Dense(6, activation="softmax")) gen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_batches = gen.flow_from_directory("runes/mutated", model.input_shape[1:3], color_mode="grayscale", shuffle=True, seed=1, batch_size=16) valid_batches = gen.flow_from_directory("runes/validation", model.input_shape[1:3], color_mode="grayscale", shuffle=True, seed=1, batch_size=16) test_batches = gen.flow_from_directory("runes/testing", model.input_shape[1:3], shuffle=False, color_mode="grayscale", batch_size=8) model.compile(Adam(lr=0.001), loss="categorical_crossentropy", metrics=["accuracy"]) history1 = model.fit_generator(train_batches, steps_per_epoch=163, epochs=5, validation_data=valid_batches, validation_steps=624) p = model.predict_generator(test_batches, verbose=True) # recall_score(pre["label"], pre["pre"]) #roc_auc_score(pre["label"], pre[1]) #true_positive_rate, false_positive_rate, threshold = roc_curve(pre["label"], pre[1]) # roc = DataFrame([true_positive_rate, false_positive_rate]).T # roc.plot(x=0,y=1) plt.plot(history1.history['accuracy']) plt.plot(history1.history['val_accuracy']) plt.axhline(0, color="black") plt.axvline(0, color="black") plt.title('Model Accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Training set', 'Validation set'], loc='upper left') plt.show() plt.plot(history1.history['val_loss']) plt.plot(history1.history['loss']) plt.axhline(0, color="black") plt.axvline(0, color="black") plt.title('Model Loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Training set', 'Test set'], loc='upper left') plt.show() model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights_only = False model.save_weights("keras_model.h5")
def generate_backdoor( self, x_val: np.ndarray, y_val: np.ndarray, y_target: np.ndarray ) -> Tuple[np.ndarray, np.ndarray]: """ Generates a possible backdoor for the model. Returns the pattern and the mask :return: A tuple of the pattern and mask for the model. """ import keras.backend as K from keras_preprocessing.image import ImageDataGenerator self.reset() datagen = ImageDataGenerator() gen = datagen.flow(x_val, y_val, batch_size=self.batch_size) mask_best = None pattern_best = None reg_best = float("inf") cost_set_counter = 0 cost_up_counter = 0 cost_down_counter = 0 cost_up_flag = False cost_down_flag = False early_stop_counter = 0 early_stop_reg_best = reg_best mini_batch_size = len(x_val) // self.batch_size for _ in tqdm(range(self.steps), desc="Generating backdoor for class {}".format(np.argmax(y_target))): loss_reg_list = [] loss_acc_list = [] for _ in range(mini_batch_size): x_batch, _ = gen.next() y_batch = [y_target] * x_batch.shape[0] _, batch_loss_reg, _, batch_loss_acc = self.train([x_batch, y_batch]) loss_reg_list.extend(list(batch_loss_reg.flatten())) loss_acc_list.extend(list(batch_loss_acc.flatten())) avg_loss_reg = np.mean(loss_reg_list) avg_loss_acc = np.mean(loss_acc_list) # save best mask/pattern so far if avg_loss_acc >= self.attack_success_threshold and avg_loss_reg < reg_best: mask_best = K.eval(self.mask_tensor) pattern_best = K.eval(self.pattern_tensor) reg_best = avg_loss_reg # check early stop if self.early_stop: # pragma: no cover if reg_best < float("inf"): if reg_best >= self.early_stop_threshold * early_stop_reg_best: early_stop_counter += 1 else: early_stop_counter = 0 early_stop_reg_best = min(reg_best, early_stop_reg_best) if cost_down_flag and cost_up_flag and early_stop_counter >= self.early_stop_patience: logger.info("Early stop") break # cost modification if avg_loss_acc >= self.attack_success_threshold: cost_set_counter += 1 if cost_set_counter >= self.patience: self.cost = self.init_cost K.set_value(self.cost_tensor, self.cost) cost_up_counter = 0 cost_down_counter = 0 cost_up_flag = False cost_down_flag = False else: cost_set_counter = 0 if avg_loss_acc >= self.attack_success_threshold: cost_up_counter += 1 cost_down_counter = 0 else: cost_up_counter = 0 cost_down_counter += 1 if cost_up_counter >= self.patience: cost_up_counter = 0 self.cost *= self.cost_multiplier_up K.set_value(self.cost_tensor, self.cost) cost_up_flag = True elif cost_down_counter >= self.patience: cost_down_counter = 0 self.cost /= self.cost_multiplier_down K.set_value(self.cost_tensor, self.cost) cost_down_flag = True if mask_best is None: mask_best = K.eval(self.mask_tensor) pattern_best = K.eval(self.pattern_tensor) return mask_best, pattern_best
imageForms = image #for x in range(4): # for y in range(4): # imageForms.append(cv2.filter2D(src=image,ddepth=-1,kernel=np.multiply(baseKernels[x],np.reshape(baseKernels[y],[5,1])))) #imageForms = sum(imageForms) return imageForms #get directory of input images and create array of images and store images in the directory to the array train_dir = "/pi/Training_Data/Train_Resized" #get labels pickle and convert to dataframe then sort by the filename to go along with the images train_labels_file = "/pi/Training_Data/Training_Input_Resized.pkl" train_labels = pd.read_pickle(train_labels_file) train_datagen = ImageDataGenerator(rescale=1. / 255, preprocessing_function=image_transform) train_generator = train_datagen.flow_from_dataframe( dataframe=train_labels, directory=train_dir, target_size=(108, 192), x_col='Filename', y_col=[ 'Right Ankle x', 'Right Knee x', 'Right Hip x', 'Left Hip x', 'Left Knee x', 'Left Ankle x', 'Pelvis x', 'Thorax x', 'Upper Neck x', 'Head Top x', 'Right Wrist x', 'Right Elbow x', 'Right Shoulder x', 'Left Shoulder x', 'Left Elbow x', 'Left Wrist x', 'Right Ankle y', 'Right Knee y', 'Right Hip y', 'Left Hip y', 'Left Knee y', 'Left Ankle y', 'Pelvis y', 'Thorax y', 'Upper Neck y', 'Head Top y', 'Right Wrist y', 'Right Elbow y', 'Right Shoulder y', 'Left Shoulder y', 'Left Elbow y', 'Left Wrist y' ],