Created
December 8, 2021 16:44
-
-
Save hsleonis/ad672eadbc1827095d4f1aefa49936b9 to your computer and use it in GitHub Desktop.
ALexNet - Deep Neural Network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
AlexNet | |
""" | |
import tensorflow as tf | |
import numpy as np | |
from tensorflow.keras.layers import Activation, Conv2D, AveragePooling2D, Dense, Flatten, BatchNormalization, MaxPool2D, Dropout | |
from tensorflow.keras.models import Sequential | |
from tensorflow.keras.datasets import mnist | |
from tensorflow.keras.utils import to_categorical | |
import matplotlib.pyplot as plt | |
# Load the cifar10 dataset using the Keras library: | |
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data() | |
# In order to reference the class names of the images during the visualization stage, | |
# a python list containing the classes is initialized with the variable name CLASS_NAMES. | |
CLASS_NAMES= ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] | |
# The CIFAR dataset is partitioned into 50,000 training data and 10,000 test data by default. | |
# The validation data is obtained by taking the last 5000 images within the training data. | |
validation_images, validation_labels = train_images[:5000], train_labels[:5000] | |
train_images, train_labels = train_images[5000:], train_labels[5000:] | |
# `tf.data.Dataset.from_tensor_slices` method takes the train, test, and validation dataset partitions | |
# and returns a corresponding TensorFlow Dataset representation. | |
train_ds = tf.data.Dataset.from_tensor_slices((train_images, train_labels)) | |
test_ds = tf.data.Dataset.from_tensor_slices((test_images, test_labels)) | |
validation_ds = tf.data.Dataset.from_tensor_slices((validation_images, validation_labels)) | |
# Explore Dataset: | |
plt.figure(figsize=(10,10)) | |
for i, (image, label) in enumerate(train_ds.take(5)): | |
ax = plt.subplot(5,5,i+1) | |
plt.imshow(image) | |
plt.title(CLASS_NAMES[label.numpy()[0]]) | |
plt.axis('off') | |
# Preprocess: | |
def process_images(image, label): | |
# Normalize images to have a mean of 0 and standard deviation of 1 | |
image = tf.image.per_image_standardization(image) | |
# Resize images because AlexNet expects images of size 224x224x3 instead of 32x32x3: | |
image = tf.image.resize(image, (224,224)) | |
return image, label | |
# Let’s get the size of each of the dataset partition we created, | |
# the sizes of the dataset partitions are required to ensure that the dataset is thoroughly shuffled | |
# before passed through the network. | |
train_ds_size = tf.data.experimental.cardinality(train_ds).numpy() | |
test_ds_size = tf.data.experimental.cardinality(test_ds).numpy() | |
validation_ds_size = tf.data.experimental.cardinality(validation_ds).numpy() | |
# For our basic input/data pipeline, we will conduct three primary operations: | |
# Preprocessing the data within the dataset | |
# Shuffle the dataset | |
# Batch data within the dataset | |
train_ds = (train_ds.map(process_images) | |
.shuffle(buffer_size=train_ds_size) | |
.batch(batch_size=32, drop_remainder=True)) | |
test_ds = (test_ds.map(process_images) | |
.shuffle(buffer_size=train_ds_size) | |
.batch(batch_size=32, drop_remainder=True)) | |
validation_ds = (validation_ds.map(process_images) | |
.shuffle(buffer_size=train_ds_size) | |
.batch(batch_size=32, drop_remainder=True)) | |
# AlexNet Model | |
alexnet = Sequential([ | |
Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu', input_shape=(224,224,3)), | |
BatchNormalization(), | |
MaxPool2D(pool_size=(3,3), strides=(2,2)), | |
Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), activation='relu', padding="same"), | |
BatchNormalization(), | |
MaxPool2D(pool_size=(3,3), strides=(2,2)), | |
Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"), | |
BatchNormalization(), | |
Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"), | |
BatchNormalization(), | |
Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"), | |
BatchNormalization(), | |
MaxPool2D(pool_size=(3,3), strides=(2,2)), | |
Flatten(), | |
Dense(4096, activation='relu'), | |
Dropout(0.5), | |
Dense(4096, activation='relu'), | |
Dropout(0.5), | |
Dense(10, activation='softmax') | |
]) | |
# Compile model | |
alexnet.compile(loss='sparse_categorical_crossentropy', optimizer=tf.optimizers.SGD(learning_rate=0.001), metrics=['accuracy']) | |
# Model preview: | |
alexnet.summary() | |
# Tensorboard: | |
import os, time | |
root_logdir = os.path.join(os.curdir, "logs\\fit\\") | |
def get_run_logdir(): | |
run_id = time.strftime("run_%Y_%m_%d-%H_%M_%S") | |
return os.path.join(root_logdir, run_id) | |
run_logdir = get_run_logdir() | |
tensorboard_cb = tf.keras.callbacks.TensorBoard(run_logdir) | |
# Train model: | |
alexnet.fit(train_ds, | |
epochs=10, | |
validation_data=validation_ds, | |
validation_freq=1, | |
callbacks=[tensorboard_cb]) | |
# Let’s evaluate model's performance by passing the test dataset: | |
alexnet.evaluate(test_ds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment