<jupyter_start><jupyter_text><jupyter_code>! nvidia-smi -L
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, models

from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop, SGD
from keras.utils import np_utils
from keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras import regularizers
from keras.callbacks import LearningRateScheduler
from keras.preprocessing.image import ImageDataGenerator

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

%matplotlib inline
%config InlineBackend.figure_format = 'retina'
plt.style.use('ggplot')

#Define learning rate
'''
def lr_schedule(epoch):
    lrate = 0.001
    if epoch > 50:
        lrate = 0.0005
    elif epoch > 75:
        lrate = 0.0003        
    return lrate
'''
def lr_schedule(epoch):
    lrate = 0.001
    if epoch > 50:
        lrate = 0.0005
    if epoch > 75:
        lrate = 0.0003
    elif epoch > 100:
        lrate = 0.0001        
    return lrate
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Parse numbers as floats
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')


# Normalize pixel values to be between 0 and 1
x_train /= 255.0
x_test /= 255.0
num_classes = 10
#convert array of labeled data to one-hot vector.
y_train = np_utils.to_categorical(y_train,num_classes)
y_test = np_utils.to_categorical(y_test,num_classes)

#Create the model
weight_decay = 1e-4

#Layer1: BatchNormalization
model = Sequential()
model.add(Conv2D(64, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay), input_shape=x_train.shape[1:]))
model.add(Activation('elu'))
model.add(BatchNormalization())

#Layer2: BatchNormalization+Dropout(0.2)
model.add(Conv2D(64, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))

#Layer3: BatchNormalization
model.add(Conv2D(128, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())

#Layer4: BatchNormalization+MaxPooling+Dropout(0.3)
model.add(Conv2D(128, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.3))

#Layer5: BatchNormalization
model.add(Conv2D(128, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())

#Layer6: BatchNormalization+MaxPooling+Dropout(0.4)
model.add(Conv2D(128, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.4))

#Layer7: BatchNormalization
model.add(Conv2D(256, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())

#Layer8: BatchNormalization+MaxPooling+Dropout(0.5)
model.add(Conv2D(256, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Data preprocessing<jupyter_code>model.summary()
#data augmentation
datagen = ImageDataGenerator(
    rotation_range=15,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True,
    zoom_range=0.4,
    fill_mode="nearest"
    )
datagen.fit(x_train)

#training
batch_size = 64 # number of samples to be used for each gradient update
epoch_size = 125 # number of iterations over the entire x_train data

#Compile model
opt= keras.optimizers.RMSprop(lr=0.001,decay=1e-6)
model.compile(loss='categorical_crossentropy',optimizer=opt, metrics=['accuracy'])
history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),\
                    steps_per_epoch=x_train.shape[0] // batch_size,epochs=epoch_size,\
                    verbose=1,validation_data=(x_test,y_test),callbacks=[LearningRateScheduler(lr_schedule)])

model.summary()
#testing
scores = model.evaluate(x_test, y_test, batch_size=64, verbose=1)
print('\nTest result: %.3f loss: %.3f' % (scores[1]*100,scores[0]))


f, (plt1, plt2) = plt.subplots(1, 2,figsize=(15,5))

plt1.plot(history.history['accuracy'])
plt1.plot(history.history['val_accuracy'])
plt1.set_title('Accuracy (Higher Better)')
plt1.set(xlabel='Epoch', ylabel='Accuracy')
plt1.legend(['train', 'validation'], loc='lower right')
plt1.xaxis.set_major_locator(MaxNLocator(integer=True))

plt2.plot(history.history['loss'])
plt2.plot(history.history['val_loss'])
plt2.set_title('Loss (Lower Better)')
plt2.set(xlabel='Epoch', ylabel='Loss')
plt2.legend(['train', 'validation'], loc='upper right')
plt2.xaxis.set_major_locator(MaxNLocator(integer=True))

saved_model_dir = 'saved_model'
tf.saved_model.save(model, saved_model_dir)
 
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
 
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)
output = model.predict_classes(x_test)
print (output)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = y_train.reshape(y_train.shape[0])
y_test = y_test.reshape(y_test.shape[0])
ind = np.where(np.equal(output, y_test)==0)
err_x = x_test[ind[0]]
err_y = output[ind[0]]
print (err_x.shape)
examples_per_class = 6
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

plt.figure(figsize=(18,18))
for cls, cls_name in enumerate(class_names):
  idxs = np.where(cls == err_y)
  idxs = np.random.choice(idxs[0], examples_per_class, replace=False)
  for i, idx in enumerate(idxs):
    plt.subplot(examples_per_class, len(class_names), i * len(class_names) + cls + 1)
    plt.imshow(err_x[idx].astype('uint8'), cmap = 'gray')
    plt.axis('off')
    if i == 0:
      plt.title(cls_name)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1453551912568306, 'fraction_numerical': 0.0351288056206089, 'mean_word_length': 6.405780346820809, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '134393', 'license': 'CC0-1.0', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2363, 'n_words': 417, 'path': 'Final_Dawnet_Cifar10_Assignment.ipynb', 'repo_name': 'TheAI-Lady/30-seconds-of-code', 'size': '6405'}