# ---
# 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 keras
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Dense
from keras.layers import Dropout, Activation, Flatten
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf 

# %matplotlib inline
# -

np.random.seed(32)
NUM_CLASSES = 10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

TYPE_MAP = {
    0: 'Plain',
    1: 'Auto',
    2: 'Bird',
    3: 'Cat',
    4: 'Deer',
    5: 'Dog',
    6: 'Frog',
    7: 'Horse',
    8: 'Ship',
    9: 'Truck'
}

# +
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0

y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)
y_test = keras.utils.to_categorical(y_test, NUM_CLASSES)

# +
import random

dataset_size = X_train.shape[0]

idx = random.randint(0, dataset_size)
#sample = random.choice(X_train)

sample_img = X_train[idx]
sample_label = np.argmax(y_train[idx])

print("Image: %s" % TYPE_MAP[sample_label])
#print(sample_img)
plt.imshow(sample_img)

# +
input_shape = X_train.shape[1:]

model = Sequential()

# less and small filters would be better for this model

model.add(Conv2D(32, (5, 5), padding='same', input_shape=input_shape))
model.add(Activation('relu'))

model.add(Conv2D(32, (3, 3)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Activation('relu'))
model.add(Dropout(0.25))

model.add(Conv2D(64, (5, 5), padding='same'))
model.add(Activation('relu'))

model.add(Conv2D(64, (3, 3)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Conv2D(64, (4, 4), padding='same'))
model.add(Activation('relu'))

model.add(Flatten())
model.add(Dense(2048, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))

model.add(Dense(128, activation='relu'))
model.add(Dense(NUM_CLASSES))
# model.add(Dense(NUM_CLASSES, activation='softmax'))
# normally it is better to use the code line above but because of 
# dependency problems in the current environment, I used the code line below
model.add(Activation(tf.nn.softmax))

print(model.count_params())
print(model.summary())

"""
model.compile(loss=keras.losses.categorical_crossentropy,
             optimizer=keras.optimizers.Adadelta(),
             metrics=['accuracy'])
"""

model.compile(loss=keras.losses.categorical_crossentropy,
             optimizer=keras.optimizers.Adam(),
             metrics=['accuracy'])
# -

model.fit(X_train, y_train, batch_size=128, epochs=12, verbose=1, 
          validation_data=(X_test, y_test), shuffle=True)

model.evaluate(X_test, y_test, verbose=1)

# +
idx = random.randint(0, X_test.shape[0])

sample_img = X_test[idx]
sample_label = np.argmax(y_test[idx])

print("Image: %s" % TYPE_MAP[sample_label])
plt.imshow(sample_img)

sample_img = sample_img.reshape(1, 32, 32, 3)

for i, v in enumerate(model.predict(sample_img)[0]):
  print("Probability of being %s: %.2f%%" % (TYPE_MAP[i], v * 100))

# +
"""
for layer in model.layers:
  print("Layer : \n"), layer.get_weights()
"""
weights = model.layers[0].get_weights()[0]
biases = model.layers[0].get_weights()[1]

#print(biases)

# to save weights and layers to use later
model.save_weights('cifar10-cnn.h5')

# to use the saved training, create the same layes and model.compile, then model load
model.load_weights('cifar10-cnn.h5')
# -



StarcoderdataJupyterScriptsDedupFiltered