Exemple #1
0
#Download History as CSV to local drive
hist_df = pd.DataFrame(history.history) 
hist_df.to_csv('trainHistoryDict.csv')
files.download('trainHistoryDict.csv')

"""## Test the model

### Measure test accuracy of model
"""

#Load the model for required Epoch
model1 = load_model('/content/drive/My Drive/model_47-0.80-0.88.h5')

#Reset the test generator
test_generator.reset()
#Evaluate the model on test generator
history1=model1.evaluate_generator(test_generator)

#Print the test accuracy
print(f'The test accuracy of the model is: {round(history1[1]*100,2)}%')

#Get predictions on test data
Y_pred = model1.predict_generator(test_generator)
#Convert prediction scores to categorical values
y_pred = np.argmax(Y_pred, axis=1)

"""### Plot confusion matrix"""

#Print plot title
print('Confusion Matrix')
Exemple #2
0
lr = 0.00001
train_batch = 64
epochs = 50
data_raw = ImageDataGenerator().flow_from_directory(PATH_DIR, class_mode='categorical', target_size=(IMG_SIZE, IMG_SIZE), shuffle=True, seed=42)
X, y = generator2array(data_raw)

################################ Fix Problem ###############################################
img_name = []
batches_per_epoch = data_raw.samples // data_raw.batch_size + (data_raw.samples % data_raw.batch_size > 0)
current_index = 0
for i in range(batches_per_epoch):
    index_array = data_raw.index_array[current_index:current_index+data_raw.batch_size]
    img_name = img_name + [data_raw.filenames[idx] for idx in index_array]
    current_index = current_index + 32

data_raw.reset()
#####################################################################################################

print(data_raw.class_indices)
kf = KFold(n_splits=10, shuffle=False)
cvscores, count = [], 1
for train, test in kf.split(X, y):
    (X_train, y_train), (X_test, y_test) = (X[train], y[train]), (X[test], y[test])
    X_train = X_train.astype('float32') / 255.
    X_test = X_test.astype('float32') / 255.

    def train_generator(x, y, batch_size, shift_fraction=0.):
        train_datagen = ImageDataGenerator(width_shift_range=shift_fraction,
                                           height_shift_range=shift_fraction)
        generator = train_datagen.flow(x, y, batch_size=batch_size)
        while 1: