<jupyter_start><jupyter_text>Convolutional Neural Net: Training, Validation, and TestingBy <NAME>April 2021<jupyter_code>version = 22<jupyter_output><empty_output><jupyter_text>Step 1Import ```keras``` and any other libraries you might need for data manipulation.<jupyter_code>import matplotlib.pyplot as plt
from tifffile import imread, imwrite
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import activations, datasets, layers, models
from keras.utils import to_categorical

from skimage.transform import resize
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Step 2Verify that you can load and display your TIFs<jupyter_code>data = imread(f'./img_data/move_up/ses_01_run_01_7095.tif')
plt.imshow(data[:,:,5], aspect = 'auto')
plt.gca().invert_yaxis()
type(data)<jupyter_output><empty_output><jupyter_text>Step 3Reshape your data to match the input of your CNN, and plot it to make sure that it looks the same as in the previous step<jupyter_code>print(data.shape)

res_im = resize(data, (64,64,8))
print (res_im.shape)
plt.imshow(data[:,:,5], aspect='auto')
plt.gca().invert_yaxis()
def get_labels(image_data_path):
    labels = []
    dir_list = os.listdir(image_data_path)
    for i in range(len(dir_list)):
        labels.append(dir_list[i])
    return labels
print(get_labels('./img_data'))
def load_images(labels, image_data_path, img_h, img_w):
    train_labels = np.zeros((0,1))
    
    for i in range(len(labels)):
        dir_path = image_data_path + '/' + labels[i]
        data_list = os.listdir(dir_path)
        
        for j in range(len(data_list)):
            img_in = imread(dir_path + '/' + data_list[j])
            res_img = resize(img_in, (64, 64, 8))
            res_img = np.reshape(res_img, (1, img_h, img_w, 8))
            if i == 0 and j == 0:
                train_imgs = res_img
            else:
                train_imgs = np.append(train_imgs, res_img, 0)
            train_labels = np.append(train_labels, np.array([[i]]), axis =0)
        print("Loaded all {} images".format(labels[i]))
    print("Done")
    return train_imgs, train_labels
            
class_labels = get_labels('./img_data')
imgs, labels = load_images(class_labels, './img_data', 64, 64)
print(imgs.shape)
print(labels.shape)
assert imgs.shape[0] == labels.shape[0]<jupyter_output>(760, 64, 64, 8)
(760, 1)<jupyter_text>Label code as loaded by load_data():- 0: move_down- 1: move_idle- 2: move_left- 3: move_right- 4: move_up<jupyter_code>print(class_labels)
imgs_train, imgs_test, labels_train, labels_test = train_test_split(imgs, labels, test_size = 0.10)
print(imgs_train.shape)
print(labels_train.shape)
print(imgs_test.shape)
print(labels_test.shape)
print(labels_train[0])
train_dataset = tf.data.Dataset.from_tensor_slices((imgs_train, labels_train))
model = models.Sequential()

model.add(layers.Conv2D(128, (3,3), input_shape = (64,64,8), data_format = "channels_last"))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Activation(activations.elu))
model.add(layers.Dropout(rate = 0.5))

model.add(layers.Conv2D(128, (3,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Activation(activations.elu))
model.add(layers.Dropout(rate = 0.5))
model.add(layers.BatchNormalization(momentum = 0.6))

model.add(layers.Conv2D(64, (3,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Activation(activations.elu))
model.add(layers.Dropout(rate = 0.5))
model.add(layers.BatchNormalization(momentum = 0.6))

model.add(layers.Conv2D(32, (3,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Activation(activations.elu))
model.add(layers.Dropout(rate = 0.25))

model.add(layers.Flatten())

model.add(layers.Dense(100))
model.add(layers.Activation(activations.elu))
model.add(layers.Dropout(rate = 0.25))

model.add(layers.Dense(len(class_labels)))
model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 0.00005),
             loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True),
             metrics=['accuracy'])
checkpoint_filepath = '.\tmp\checkpoint'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True,
    monitor='val_accuracy',
    mode='max',
    save_best_only=True)
history = model.fit(imgs_train, 
                    labels_train, 
                    batch_size = 5, 
                    epochs = 350, 
                    validation_data = (imgs_test, labels_test), 
                    callbacks=[model_checkpoint_callback]
                   )
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.axhline(y=np.mean(history.history['val_accuracy']), color='r')
plt.title('Model Accuracy 13')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.ylim([0, 1])
plt.grid()

fig = plt.gcf()
plt.show()

fig.savefig("model_accuracy_{:02d}.jpeg".format(version), dpi = 200)
model.load_weights(checkpoint_filepath)
model.save("classifier_{:02d}_v3".format(version))
results = model.evaluate(imgs_test, labels_test)
print("test loss, test acc: ", results)
res_data = resize(data, (64, 64, 8))
res_data = np.reshape(res_data, (1, 64, 64, 8))
model.predict(res_data)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14240090174713507, 'fraction_numerical': 0.029118917903437912, 'mean_word_length': 4.68196371398079, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '588687', 'license': 'MIT', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1904, 'n_words': 344, 'path': 'jupyter-notebooks/cnn-model-trainer.ipynb', 'repo_name': 'Fredware/open-bci-robot-manipulator', 'size': '5330'}