# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import sys
sys.path.append("/home/ecbm4040/Final_Project/e4040-2021Fall-Project-SCNN-as6430-as6456-vsk2123/src/")

# + id="e2be64ad"
import tensorflow as tf
import numpy as np
import random
from tensorflow import keras
from tensorflow.keras import utils as np_utils
#from keras import utils as np_utils
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, Dropout, BatchNormalization, Softmax
from tensorflow.keras import Model
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.losses import categorical_crossentropy
import datetime
from time import time
from matplotlib import pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling2D

# + colab={"base_uri": "https://localhost:8080/"} id="238e6748" outputId="2faa18bd-ca54-44e0-9ae1-0dc01e0f8d82"
# Load and prepare data

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
# Scale images to the [0, 1] range
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255

print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")

input_shape = (x_train.shape[1], x_train.shape[2], x_train.shape[3])
output_size=10

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, output_size)
y_test = keras.utils.to_categorical(y_test, output_size)
print(y_train.shape)
print(y_test.shape)



# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="G_G1nSk_lS0Y" outputId="5f995f6d-bb10-4e06-c979-30b047c48618"
# Plot sample images before augmentation
for i in range(0,9):
    plt.subplot(330 + 1 + i)
    plt.imshow(x_train[i])
plt.show()


# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="JghQdsPzlDMV" outputId="131e3091-a1bf-4678-d66f-7a99f1b91348"
# Data augmentation code

def HSV_perturbations(image):
    """
    Takes an input image and returns it with either the randomly adjusted hue or saturation
    (or may be makes no HSV change at all) with a probability of 1/3
    """
    choice=random.randint(1,3)
    print(choice)
    image = np.array(image)
    if choice ==1:
        return tf.image.random_hue(image, 1/random.randint(1,10))
    elif choice ==2:
        return tf.image.random_saturation(image, 5, 10)
    else:
        return image # so as to avoid not change hue for every image

datagen = ImageDataGenerator(
    horizontal_flip=True,
    width_shift_range=0.1,
    height_shift_range=0.1,
    brightness_range=[0.5,1.5]
    )
datagen.fit(x_train)

# Plot sample augmented images
for X_batch, y_batch in datagen.flow(x_train, y_train, batch_size=9):
    for i in range(0, 9):
        plt.subplot(330 + 1 + i)
        plt.imshow(X_batch[i].astype(np.uint8))
    plt.show()
    break


# + id="356e89ec"
# Define CNN model architecture
def spatialCNN():

    model = Sequential()

    model.add(Conv2D(96, kernel_size=(5,5),padding="same", input_shape=input_shape))
    model.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))

    model.add(Conv2D(192, kernel_size=(5,5),padding="same"))
    model.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))

    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Dense(512))
    model.add(Dense(output_size, activation='softmax')) 
    

    return model
    


# + [markdown] id="Uy5dI4zMgc8D"
#

# + colab={"base_uri": "https://localhost:8080/"} id="08e3a1dc" outputId="4238f6b6-f706-405d-d544-62459dbfe076"
# Compiling and training the model
batch_size=128
nb_epochs=50

train_generator = datagen.flow(x_train, y_train, batch_size=batch_size)
valid_generator = datagen.flow(x_test, y_test, batch_size=batch_size)

standard_cnn_model = spatialCNN()
print(standard_cnn_model.summary())
standard_cnn_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
#history=standard_cnn_model.fit(x=x_train, y=y_train, batch_size=batch_size,epochs=nb_epochs, validation_data=(x_test, y_test))
history=standard_cnn_model.fit_generator(train_generator,epochs=nb_epochs,steps_per_epoch=len(x_train)//batch_size, validation_data=valid_generator)

StarcoderdataJupyterScriptsDedupFiltered