100% found this document useful (2 votes)
103 views29 pages

Labpractice 2

This document loads and preprocesses the CIFAR-10 dataset. It defines a convolutional neural network model with two convolutional layers and max pooling, followed by flatten, dense, and dropout layers. The model is trained on the CIFAR-10 training data for 3 epochs and evaluates the loss and accuracy on both the training and test sets. Graphs are generated to visualize the loss and accuracy over the epochs.

Uploaded by

Rajashree Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
103 views29 pages

Labpractice 2

This document loads and preprocesses the CIFAR-10 dataset. It defines a convolutional neural network model with two convolutional layers and max pooling, followed by flatten, dense, and dropout layers. The model is trained on the CIFAR-10 training data for 3 epochs and evaluates the loss and accuracy on both the training and test sets. Graphs are generated to visualize the loss and accuracy over the epochs.

Uploaded by

Rajashree Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

labpractice2

August 24, 2023

[1]: import tensorflow as tf


import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np
import pandas as pd
import math
import datetime
import platform

print('Python version:', platform.python_version())


print('Tensorflow version:', tf.__version__)
print('Keras version:', tf.keras.__version__)

Python version: 3.10.12


Tensorflow version: 2.12.0
Keras version: 2.12.0

[2]: (x_train, y_train),(x_test, y_test) = tf.keras.datasets.cifar10.load_data()

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz


170498071/170498071 [==============================] - 4s 0us/step

[3]: print('x_train',x_train.shape)
print('y_train',y_train.shape)
print('x_test',x_test.shape)
print('y_test',y_test.shape)

x_train (50000, 32, 32, 3)


y_train (50000, 1)
x_test (10000, 32, 32, 3)
y_test (10000, 1)

[8]: #save image parameters to the constants that we will use later for data␣
↪re-shaping and for model training

(_, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS) = x_train.shape


IMAGE_CHANNELS = 3

print('IMAGE_WIDTH:', IMAGE_WIDTH)

1
print('IMAGE_HEIGHT:', IMAGE_HEIGHT)
print('IMAGE_CHANNELS:', IMAGE_CHANNELS)

IMAGE_WIDTH: 32
IMAGE_HEIGHT: 32
IMAGE_CHANNELS: 3

[10]: #visualize
plt.imshow(x_train[0],cmap=plt.cm.binary)
plt.show()

[11]: numbers_to_display = 25
num_cells = math.ceil(math.sqrt(numbers_to_display))
plt.figure(figsize=(10,10))
for i in range(numbers_to_display):
plt.subplot(num_cells, num_cells, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i], cmap=plt.cm.binary)
plt.xlabel(y_train[i])
plt.show()

2
/usr/local/lib/python3.10/dist-packages/matplotlib/text.py:1279: FutureWarning:
elementwise comparison failed; returning scalar instead, but in the future will
perform elementwise comparison
if s != self._text:

[12]: x_train_with_chanels = x_train.reshape(


x_train.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)

x_test_with_chanels= x_test.reshape(

3
x_test.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)

[13]: print('x_train_with_chanels:',x_train_with_chanels.shape)
print('x_test_with_chanels:',x_test_with_chanels.shape)

x_train_with_chanels: (50000, 32, 32, 3)


x_test_with_chanels: (10000, 32, 32, 3)

[14]: #mask or scaling


x_train_normalized = x_train_with_chanels / 255
x_test_normalized = x_test_with_chanels / 255

[15]: x_train_normalized[0][18] #picking 18 no picture from 0 row

[15]: array([[0.59607843, 0.44705882, 0.31372549],


[0.45882353, 0.29411765, 0.14509804],
[0.44705882, 0.31372549, 0.18823529],
[0.48235294, 0.35294118, 0.22352941],
[0.49411765, 0.35686275, 0.21960784],
[0.47843137, 0.3254902 , 0.18823529],
[0.36470588, 0.22745098, 0.1254902 ],
[0.70196078, 0.60392157, 0.54117647],
[0.93333333, 0.88627451, 0.83137255],
[0.97254902, 0.95294118, 0.89803922],
[0.66666667, 0.5254902 , 0.40784314],
[0.7254902 , 0.51764706, 0.34509804],
[0.94509804, 0.83921569, 0.69411765],
[0.90196078, 0.85490196, 0.76470588],
[0.73333333, 0.6627451 , 0.55686275],
[0.70588235, 0.62745098, 0.51372549],
[0.65098039, 0.57254902, 0.45098039],
[0.57254902, 0.46666667, 0.33333333],
[0.58431373, 0.45490196, 0.30980392],
[0.61568627, 0.48627451, 0.3254902 ],
[0.72156863, 0.61568627, 0.43137255],
[0.84705882, 0.76470588, 0.55294118],
[0.83137255, 0.77647059, 0.59607843],
[0.9254902 , 0.86666667, 0.77254902],
[0.9254902 , 0.83137255, 0.69019608],
[0.65098039, 0.49019608, 0.24705882],
[0.53333333, 0.33333333, 0.0627451 ],
[0.5254902 , 0.31764706, 0.05098039],
[0.50980392, 0.3254902 , 0.05098039],

4
[0.49803922, 0.3372549 , 0.0627451 ],
[0.5372549 , 0.41176471, 0.10588235],
[0.59215686, 0.50196078, 0.21176471]])

[16]: model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Convolution2D(
input_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS),
kernel_size = 5,
filters = 8,
strides = 1,
activation= tf.keras.activations.relu,
kernel_initializer = tf.keras.initializers.VarianceScaling()
))

model.add(tf.keras.layers.MaxPooling2D(
pool_size = (2,2),
strides=(2,2)
))

model.add(tf.keras.layers.Convolution2D(
input_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS),
kernel_size = 5,
filters = 16,
strides = 1,
activation= tf.keras.activations.relu,
kernel_initializer = tf.keras.initializers.VarianceScaling()
))

model.add(tf.keras.layers.MaxPooling2D(
pool_size = (2,2),
strides=(2,2)
))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(
units = 128, activation = tf.keras.activations.relu
))

model.add(tf.keras.layers.Dropout(0.2))

model.add(tf.keras.layers.Dense(
units = 10,
activation = tf.keras.activations.softmax,
kernel_initializer = tf.keras.initializers.VarianceScaling()

5
))

[17]: model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 28, 28, 8) 608

max_pooling2d (MaxPooling2D (None, 14, 14, 8) 0


)

conv2d_1 (Conv2D) (None, 10, 10, 16) 3216

max_pooling2d_1 (MaxPooling (None, 5, 5, 16) 0


2D)

flatten (Flatten) (None, 400) 0

dense (Dense) (None, 128) 51328

dropout (Dropout) (None, 128) 0

dense_1 (Dense) (None, 10) 1290

=================================================================
Total params: 56,442
Trainable params: 56,442
Non-trainable params: 0
_________________________________________________________________

[18]: adam_optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)

model.compile(
optimizer=adam_optimizer,
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy']
)

[19]: training_history = model.fit(


x_train_normalized,
y_train,
epochs = 3,
validation_data=(x_test_normalized, y_test) #validation accuracy = test␣
↪accuracy

6
Epoch 1/3
1563/1563 [==============================] - 21s 7ms/step - loss: 1.6220 -
accuracy: 0.4124 - val_loss: 1.4148 - val_accuracy: 0.4983
Epoch 2/3
1563/1563 [==============================] - 7s 5ms/step - loss: 1.3461 -
accuracy: 0.5196 - val_loss: 1.2512 - val_accuracy: 0.5519
Epoch 3/3
1563/1563 [==============================] - 6s 4ms/step - loss: 1.2458 -
accuracy: 0.5573 - val_loss: 1.2224 - val_accuracy: 0.5548

[20]: plt.xlabel('Epoch Number')


plt.ylabel('Loss')
plt.plot(training_history.history['loss'],label='training set')
plt.plot(training_history.history['val_loss'], label='test set')
plt.legend()

[20]: <matplotlib.legend.Legend at 0x797755553250>

[21]: plt.xlabel('Epoch Number')


plt.ylabel('Accuracy')
plt.plot(training_history.history['accuracy'],label='training set')

7
plt.plot(training_history.history['val_accuracy'], label='test set')
plt.legend()

[21]: <matplotlib.legend.Legend at 0x7977558b2470>

[22]: train_loss, train_accuracy = model.evaluate(x_train_normalized, y_train)


print('Training loss: ', train_loss)
print('Training accuracy: ', train_accuracy)

1563/1563 [==============================] - 4s 2ms/step - loss: 1.1580 -


accuracy: 0.5835
Training loss: 1.1580241918563843
Training accuracy: 0.5835000276565552

[23]: validation_loss, validation_accuracy = model.evaluate(x_test_normalized, y_test)


print('Validation loss: ', validation_loss)
print('Validation accuracy: ', validation_accuracy)

313/313 [==============================] - 1s 3ms/step - loss: 1.2224 -


accuracy: 0.5548
Validation loss: 1.2223572731018066
Validation accuracy: 0.5547999739646912

8
#Resnet 18
[24]: import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np
import pandas as pd
import math
import datetime
import platform

print('Python version:', platform.python_version())


print('Tensorflow version:', tf.__version__)
print('Keras version:', tf.keras.__version__)

Python version: 3.10.12


Tensorflow version: 2.12.0
Keras version: 2.12.0

[25]: (x_train, y_train),(x_test, y_test) = tf.keras.datasets.cifar10.load_data()

[26]: print('x_train',x_train.shape)
print('y_train',y_train.shape)
print('x_test',x_test.shape)
print('y_test',y_test.shape)

x_train (50000, 32, 32, 3)


y_train (50000, 1)
x_test (10000, 32, 32, 3)
y_test (10000, 1)

[27]: #save image parameters to the constants that we will use later for data␣
↪re-shaping and for model training

(_, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS) = x_train.shape

print('IMAGE_WIDTH:', IMAGE_WIDTH)
print('IMAGE_HEIGHT:', IMAGE_HEIGHT)
print('IMAGE_CHANNELS:', IMAGE_CHANNELS)

IMAGE_WIDTH: 32
IMAGE_HEIGHT: 32
IMAGE_CHANNELS: 3

[28]: #visualize
plt.imshow(x_train[0],cmap=plt.cm.binary)
plt.show()

9
[29]: x_train_with_chanels = x_train.reshape(
x_train.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)

x_test_with_chanels= x_test.reshape(
x_test.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)

[30]: print('x_train_with_chanels:',x_train_with_chanels.shape)
print('x_test_with_chanels:',x_test_with_chanels.shape)

x_train_with_chanels: (50000, 32, 32, 3)


x_test_with_chanels: (10000, 32, 32, 3)

[31]: #mask or scaling


x_train_normalized = x_train_with_chanels / 255

10
x_test_normalized = x_test_with_chanels / 255

[32]: x_train_normalized[0][18] #picking 18 no picture from 0 row

[32]: array([[0.59607843, 0.44705882, 0.31372549],


[0.45882353, 0.29411765, 0.14509804],
[0.44705882, 0.31372549, 0.18823529],
[0.48235294, 0.35294118, 0.22352941],
[0.49411765, 0.35686275, 0.21960784],
[0.47843137, 0.3254902 , 0.18823529],
[0.36470588, 0.22745098, 0.1254902 ],
[0.70196078, 0.60392157, 0.54117647],
[0.93333333, 0.88627451, 0.83137255],
[0.97254902, 0.95294118, 0.89803922],
[0.66666667, 0.5254902 , 0.40784314],
[0.7254902 , 0.51764706, 0.34509804],
[0.94509804, 0.83921569, 0.69411765],
[0.90196078, 0.85490196, 0.76470588],
[0.73333333, 0.6627451 , 0.55686275],
[0.70588235, 0.62745098, 0.51372549],
[0.65098039, 0.57254902, 0.45098039],
[0.57254902, 0.46666667, 0.33333333],
[0.58431373, 0.45490196, 0.30980392],
[0.61568627, 0.48627451, 0.3254902 ],
[0.72156863, 0.61568627, 0.43137255],
[0.84705882, 0.76470588, 0.55294118],
[0.83137255, 0.77647059, 0.59607843],
[0.9254902 , 0.86666667, 0.77254902],
[0.9254902 , 0.83137255, 0.69019608],
[0.65098039, 0.49019608, 0.24705882],
[0.53333333, 0.33333333, 0.0627451 ],
[0.5254902 , 0.31764706, 0.05098039],
[0.50980392, 0.3254902 , 0.05098039],
[0.49803922, 0.3372549 , 0.0627451 ],
[0.5372549 , 0.41176471, 0.10588235],
[0.59215686, 0.50196078, 0.21176471]])

[40]: from tensorflow.keras.utils import to_categorical

y_train_encoded = to_categorical(y_train, num_classes=10)


y_test_encoded = to_categorical(y_test, num_classes=10)

[41]: import tensorflow as tf


from tensorflow.keras.layers import Input, Conv2D, BatchNormalization,␣
↪Activation, MaxPooling2D, GlobalAveragePooling2D, Dense

from tensorflow.keras.models import Model

11
def resnet_block(x, filters, kernel_size=3, stride=1):

# First convolution layer


x = Conv2D(filters, kernel_size=kernel_size, strides=stride,␣
↪padding='same')(x)

x = BatchNormalization()(x)
x = Activation('relu')(x)

# Second convolution layer


x = Conv2D(filters, kernel_size=kernel_size, strides=1, padding='same')(x)
x = BatchNormalization()(x)

x = Activation('relu')(x)

return x

input_shape = (32, 32, 3)


num_classes = 10

input_layer = Input(shape=input_shape)
x = Conv2D(64, kernel_size=7, strides=2, padding='same')(input_layer)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')(x)

x = resnet_block(x, filters=64)
x = resnet_block(x, filters=64)
x = resnet_block(x, filters=128, stride=2)
x = resnet_block(x, filters=128)
x = resnet_block(x, filters=256, stride=2)
x = resnet_block(x, filters=256)
x = resnet_block(x, filters=512, stride=2)
x = resnet_block(x, filters=512)

x = GlobalAveragePooling2D()(x)
output_layer = Dense(num_classes, activation='softmax')(x)

resnet18_model = Model(inputs=input_layer, outputs=output_layer)

resnet18_model.summary()

Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 32, 32, 3)] 0

12
conv2d_36 (Conv2D) (None, 16, 16, 64) 9472

batch_normalization_34 (Bat (None, 16, 16, 64) 256


chNormalization)

activation_34 (Activation) (None, 16, 16, 64) 0

max_pooling2d_4 (MaxPooling (None, 8, 8, 64) 0


2D)

conv2d_37 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_35 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_35 (Activation) (None, 8, 8, 64) 0

conv2d_38 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_36 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_36 (Activation) (None, 8, 8, 64) 0

conv2d_39 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_37 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_37 (Activation) (None, 8, 8, 64) 0

conv2d_40 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_38 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_38 (Activation) (None, 8, 8, 64) 0

conv2d_41 (Conv2D) (None, 4, 4, 128) 73856

batch_normalization_39 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_39 (Activation) (None, 4, 4, 128) 0

conv2d_42 (Conv2D) (None, 4, 4, 128) 147584

13
batch_normalization_40 (Bat (None, 4, 4, 128) 512
chNormalization)

activation_40 (Activation) (None, 4, 4, 128) 0

conv2d_43 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_41 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_41 (Activation) (None, 4, 4, 128) 0

conv2d_44 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_42 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_42 (Activation) (None, 4, 4, 128) 0

conv2d_45 (Conv2D) (None, 2, 2, 256) 295168

batch_normalization_43 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_43 (Activation) (None, 2, 2, 256) 0

conv2d_46 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_44 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_44 (Activation) (None, 2, 2, 256) 0

conv2d_47 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_45 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_45 (Activation) (None, 2, 2, 256) 0

conv2d_48 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_46 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_46 (Activation) (None, 2, 2, 256) 0

conv2d_49 (Conv2D) (None, 1, 1, 512) 1180160

14
batch_normalization_47 (Bat (None, 1, 1, 512) 2048
chNormalization)

activation_47 (Activation) (None, 1, 1, 512) 0

conv2d_50 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_48 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_48 (Activation) (None, 1, 1, 512) 0

conv2d_51 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_49 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_49 (Activation) (None, 1, 1, 512) 0

conv2d_52 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_50 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_50 (Activation) (None, 1, 1, 512) 0

global_average_pooling2d_2 (None, 512) 0


(GlobalAveragePooling2D)

dense_4 (Dense) (None, 10) 5130

=================================================================
Total params: 11,019,530
Trainable params: 11,011,722
Non-trainable params: 7,808
_________________________________________________________________

[42]: # Compile the model


resnet18_model.compile(optimizer='adam', loss='categorical_crossentropy',␣
↪metrics=['accuracy'])

[44]: # Train the model

training_history = resnet18_model.fit(
x_train_normalized,
y_train_encoded,

15
epochs= 3,
validation_data=(x_test_normalized, y_test_encoded)
)

Epoch 1/3
1563/1563 [==============================] - 32s 20ms/step - loss: 1.1081 -
accuracy: 0.6251 - val_loss: 1.2142 - val_accuracy: 0.5884
Epoch 2/3
1563/1563 [==============================] - 32s 21ms/step - loss: 0.9596 -
accuracy: 0.6756 - val_loss: 1.2812 - val_accuracy: 0.5409
Epoch 3/3
1563/1563 [==============================] - 32s 20ms/step - loss: 0.8619 -
accuracy: 0.7112 - val_loss: 1.0369 - val_accuracy: 0.6572

[45]: plt.xlabel('Epoch Number')


plt.ylabel('Loss')
plt.plot(training_history.history['loss'],label='training set')
plt.plot(training_history.history['val_loss'], label='test set')
plt.legend()

[45]: <matplotlib.legend.Legend at 0x79775d4afee0>

16
[46]: plt.xlabel('Epoch Number')
plt.ylabel('Accuracy')
plt.plot(training_history.history['accuracy'],label='training set')
plt.plot(training_history.history['val_accuracy'], label='test set')
plt.legend()

[46]: <matplotlib.legend.Legend at 0x79775d4f3e50>

[48]: from tensorflow.keras.models import Model


train_loss, train_accuracy = resnet18_model.evaluate(x_train_normalized,␣
↪y_train_encoded)

print('Training loss: ', train_loss)


print('Training accuracy: ', train_accuracy)

1563/1563 [==============================] - 10s 6ms/step - loss: 0.8833 -


accuracy: 0.6980
Training loss: 0.8832731246948242
Training accuracy: 0.6980400085449219

[49]: validation_loss, validation_accuracy = resnet18_model.


↪evaluate(x_test_normalized, y_test_encoded)

17
print('Validation loss: ', validation_loss)
print('Validation accuracy: ', validation_accuracy)

313/313 [==============================] - 2s 7ms/step - loss: 1.0369 -


accuracy: 0.6572
Validation loss: 1.036912441253662
Validation accuracy: 0.6571999788284302
#RESNET 34
[50]: import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np
import pandas as pd
import math
import datetime
import platform

print('Python version:', platform.python_version())


print('Tensorflow version:', tf.__version__)
print('Keras version:', tf.keras.__version__)

Python version: 3.10.12


Tensorflow version: 2.12.0
Keras version: 2.12.0

[52]: cifar_dataset = tf.keras.datasets.cifar10


(x_train, y_train),(x_test, y_test) = cifar_dataset.load_data()

[53]: print('x_train',x_train.shape)
print('y_train',y_train.shape)
print('x_test',x_test.shape)
print('y_test',y_test.shape)

x_train (50000, 32, 32, 3)


y_train (50000, 1)
x_test (10000, 32, 32, 3)
y_test (10000, 1)

[55]: #save image parameters to the constants that we will use later for data␣
↪re-shaping and for model training

(_, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS) = x_train.shape

print('IMAGE_WIDTH:', IMAGE_WIDTH)
print('IMAGE_HEIGHT:', IMAGE_HEIGHT)
print('IMAGE_CHANNELS:', IMAGE_CHANNELS)

18
IMAGE_WIDTH: 32
IMAGE_HEIGHT: 32
IMAGE_CHANNELS: 3

[56]: x_train_with_chanels = x_train.reshape(


x_train.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)

x_test_with_chanels= x_test.reshape(
x_test.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)

[57]: #mask or scaling


x_train_normalized = x_train_with_chanels / 255
x_test_normalized = x_test_with_chanels / 255

[58]: x_train_normalized[0][18] #picking 18 no picture from 0 row

[58]: array([[0.59607843, 0.44705882, 0.31372549],


[0.45882353, 0.29411765, 0.14509804],
[0.44705882, 0.31372549, 0.18823529],
[0.48235294, 0.35294118, 0.22352941],
[0.49411765, 0.35686275, 0.21960784],
[0.47843137, 0.3254902 , 0.18823529],
[0.36470588, 0.22745098, 0.1254902 ],
[0.70196078, 0.60392157, 0.54117647],
[0.93333333, 0.88627451, 0.83137255],
[0.97254902, 0.95294118, 0.89803922],
[0.66666667, 0.5254902 , 0.40784314],
[0.7254902 , 0.51764706, 0.34509804],
[0.94509804, 0.83921569, 0.69411765],
[0.90196078, 0.85490196, 0.76470588],
[0.73333333, 0.6627451 , 0.55686275],
[0.70588235, 0.62745098, 0.51372549],
[0.65098039, 0.57254902, 0.45098039],
[0.57254902, 0.46666667, 0.33333333],
[0.58431373, 0.45490196, 0.30980392],
[0.61568627, 0.48627451, 0.3254902 ],
[0.72156863, 0.61568627, 0.43137255],
[0.84705882, 0.76470588, 0.55294118],
[0.83137255, 0.77647059, 0.59607843],

19
[0.9254902 , 0.86666667, 0.77254902],
[0.9254902 , 0.83137255, 0.69019608],
[0.65098039, 0.49019608, 0.24705882],
[0.53333333, 0.33333333, 0.0627451 ],
[0.5254902 , 0.31764706, 0.05098039],
[0.50980392, 0.3254902 , 0.05098039],
[0.49803922, 0.3372549 , 0.0627451 ],
[0.5372549 , 0.41176471, 0.10588235],
[0.59215686, 0.50196078, 0.21176471]])

[59]: from tensorflow.keras.utils import to_categorical

y_train_encoded = to_categorical(y_train, num_classes=10)


y_test_encoded = to_categorical(y_test, num_classes=10)

[60]: import tensorflow as tf


from tensorflow.keras.layers import Input, Conv2D, BatchNormalization,␣
↪Activation, MaxPooling2D, GlobalAveragePooling2D, Dense

from tensorflow.keras.models import Model

def resnet_block(x, filters, kernel_size=3, stride=1):

# First convolution layer


x = Conv2D(filters, kernel_size=kernel_size, strides=stride,␣
↪padding='same')(x)

x = BatchNormalization()(x)
x = Activation('relu')(x)

# Second convolution layer


x = Conv2D(filters, kernel_size=kernel_size, strides=1, padding='same')(x)
x = BatchNormalization()(x)

x = Activation('relu')(x)

return x

input_shape = (32, 32, 3)


num_classes = 10

input_layer = Input(shape=input_shape)
x = Conv2D(64, kernel_size=7, strides=2, padding='same')(input_layer)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')(x)

x = resnet_block(x, filters=64)
x = resnet_block(x, filters=64)

20
x = resnet_block(x, filters=64) # Additional block compared to ResNet-18
x = resnet_block(x, filters=128, stride=2)
x = resnet_block(x, filters=128)
x = resnet_block(x, filters=128)
x = resnet_block(x, filters=128) # Additional block compared to ResNet-18
x = resnet_block(x, filters=256, stride=2)
x = resnet_block(x, filters=256)
x = resnet_block(x, filters=256)
x = resnet_block(x, filters=256)
x = resnet_block(x, filters=256)
x = resnet_block(x, filters=256) # Additional block compared to ResNet-18
x = resnet_block(x, filters=512, stride=2)
x = resnet_block(x, filters=512)
x = resnet_block(x, filters=512)

x = GlobalAveragePooling2D()(x)
output_layer = Dense(num_classes, activation='softmax')(x)

resnet34_model = Model(inputs=input_layer, outputs=output_layer)

resnet34_model.summary()

Model: "model_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 32, 32, 3)] 0

conv2d_53 (Conv2D) (None, 16, 16, 64) 9472

batch_normalization_51 (Bat (None, 16, 16, 64) 256


chNormalization)

activation_51 (Activation) (None, 16, 16, 64) 0

max_pooling2d_5 (MaxPooling (None, 8, 8, 64) 0


2D)

conv2d_54 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_52 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_52 (Activation) (None, 8, 8, 64) 0

conv2d_55 (Conv2D) (None, 8, 8, 64) 36928

21
batch_normalization_53 (Bat (None, 8, 8, 64) 256
chNormalization)

activation_53 (Activation) (None, 8, 8, 64) 0

conv2d_56 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_54 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_54 (Activation) (None, 8, 8, 64) 0

conv2d_57 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_55 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_55 (Activation) (None, 8, 8, 64) 0

conv2d_58 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_56 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_56 (Activation) (None, 8, 8, 64) 0

conv2d_59 (Conv2D) (None, 8, 8, 64) 36928

batch_normalization_57 (Bat (None, 8, 8, 64) 256


chNormalization)

activation_57 (Activation) (None, 8, 8, 64) 0

conv2d_60 (Conv2D) (None, 4, 4, 128) 73856

batch_normalization_58 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_58 (Activation) (None, 4, 4, 128) 0

conv2d_61 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_59 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_59 (Activation) (None, 4, 4, 128) 0

22
conv2d_62 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_60 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_60 (Activation) (None, 4, 4, 128) 0

conv2d_63 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_61 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_61 (Activation) (None, 4, 4, 128) 0

conv2d_64 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_62 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_62 (Activation) (None, 4, 4, 128) 0

conv2d_65 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_63 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_63 (Activation) (None, 4, 4, 128) 0

conv2d_66 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_64 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_64 (Activation) (None, 4, 4, 128) 0

conv2d_67 (Conv2D) (None, 4, 4, 128) 147584

batch_normalization_65 (Bat (None, 4, 4, 128) 512


chNormalization)

activation_65 (Activation) (None, 4, 4, 128) 0

conv2d_68 (Conv2D) (None, 2, 2, 256) 295168

batch_normalization_66 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_66 (Activation) (None, 2, 2, 256) 0

23
conv2d_69 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_67 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_67 (Activation) (None, 2, 2, 256) 0

conv2d_70 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_68 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_68 (Activation) (None, 2, 2, 256) 0

conv2d_71 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_69 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_69 (Activation) (None, 2, 2, 256) 0

conv2d_72 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_70 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_70 (Activation) (None, 2, 2, 256) 0

conv2d_73 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_71 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_71 (Activation) (None, 2, 2, 256) 0

conv2d_74 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_72 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_72 (Activation) (None, 2, 2, 256) 0

conv2d_75 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_73 (Bat (None, 2, 2, 256) 1024


chNormalization)

24
activation_73 (Activation) (None, 2, 2, 256) 0

conv2d_76 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_74 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_74 (Activation) (None, 2, 2, 256) 0

conv2d_77 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_75 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_75 (Activation) (None, 2, 2, 256) 0

conv2d_78 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_76 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_76 (Activation) (None, 2, 2, 256) 0

conv2d_79 (Conv2D) (None, 2, 2, 256) 590080

batch_normalization_77 (Bat (None, 2, 2, 256) 1024


chNormalization)

activation_77 (Activation) (None, 2, 2, 256) 0

conv2d_80 (Conv2D) (None, 1, 1, 512) 1180160

batch_normalization_78 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_78 (Activation) (None, 1, 1, 512) 0

conv2d_81 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_79 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_79 (Activation) (None, 1, 1, 512) 0

conv2d_82 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_80 (Bat (None, 1, 1, 512) 2048


chNormalization)

25
activation_80 (Activation) (None, 1, 1, 512) 0

conv2d_83 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_81 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_81 (Activation) (None, 1, 1, 512) 0

conv2d_84 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_82 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_82 (Activation) (None, 1, 1, 512) 0

conv2d_85 (Conv2D) (None, 1, 1, 512) 2359808

batch_normalization_83 (Bat (None, 1, 1, 512) 2048


chNormalization)

activation_83 (Activation) (None, 1, 1, 512) 0

global_average_pooling2d_3 (None, 512) 0


(GlobalAveragePooling2D)

dense_5 (Dense) (None, 10) 5130

=================================================================
Total params: 21,138,826
Trainable params: 21,123,594
Non-trainable params: 15,232
_________________________________________________________________

[61]: resnet34_model.compile(optimizer='adam', loss='categorical_crossentropy',␣


↪metrics=['accuracy'])

[62]: # Train the model

training_history = resnet34_model.fit(
x_train_normalized,
y_train_encoded,
epochs= 3,
validation_data=(x_test_normalized, y_test_encoded)
)

Epoch 1/3

26
1563/1563 [==============================] - 118s 35ms/step - loss: 2.0182 -
accuracy: 0.2217 - val_loss: 2.2848 - val_accuracy: 0.1103
Epoch 2/3
1563/1563 [==============================] - 55s 35ms/step - loss: 1.8123 -
accuracy: 0.3067 - val_loss: 2.9065 - val_accuracy: 0.2077
Epoch 3/3
1563/1563 [==============================] - 56s 36ms/step - loss: 1.6814 -
accuracy: 0.3672 - val_loss: 1.9971 - val_accuracy: 0.3290

[63]: plt.xlabel('Epoch Number')


plt.ylabel('Loss')
plt.plot(training_history.history['loss'],label='training set')
plt.plot(training_history.history['val_loss'], label='test set')
plt.legend()

[63]: <matplotlib.legend.Legend at 0x7977062f3070>

[64]: plt.xlabel('Epoch Number')


plt.ylabel('Accuracy')
plt.plot(training_history.history['accuracy'],label='training set')
plt.plot(training_history.history['val_accuracy'], label='test set')

27
plt.legend()

[64]: <matplotlib.legend.Legend at 0x79770619ea70>

[66]: from tensorflow.keras.models import Model


train_loss, train_accuracy = resnet34_model.evaluate(x_train_normalized,␣
↪y_train_encoded)

print('Training loss: ', train_loss)


print('Training accuracy: ', train_accuracy)

1563/1563 [==============================] - 17s 11ms/step - loss: 1.9870 -


accuracy: 0.3311
Training loss: 1.9869939088821411
Training accuracy: 0.33114001154899597

[67]: validation_loss, validation_accuracy = resnet34_model.


↪evaluate(x_test_normalized, y_test_encoded)

print('Validation loss: ', validation_loss)


print('Validation accuracy: ', validation_accuracy)

313/313 [==============================] - 3s 10ms/step - loss: 1.9971 -

28
accuracy: 0.3290
Validation loss: 1.997099757194519
Validation accuracy: 0.32899999618530273

[ ]:

29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy