Convolutional Neural Netowrks
36. Convolutional Neural Netowrks#
import numpy as np # advanced math library
import matplotlib.pyplot as plt
import random # for generating random numbers
from keras.datasets import cifar10
from keras.models import Sequential # Model type to be used
from keras.layers.core import Dense, Dropout, Activation # Types of layers to be used in our model
from keras.utils import np_utils # NumPy related tools
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D, GlobalAveragePooling2D, Flatten
from keras.layers import BatchNormalization
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Input In [1], in <cell line: 5>()
2 import matplotlib.pyplot as plt
3 import random # for generating random numbers
----> 5 from keras.datasets import cifar10
6 from keras.models import Sequential # Model type to be used
8 from keras.layers.core import Dense, Dropout, Activation # Types of layers to be used in our model
File /opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/keras/__init__.py:21, in <module>
15 """Implementation of the Keras API, the high-level API of TensorFlow.
16
17 Detailed documentation and user guides are available at
18 [keras.io](https://keras.io).
19 """
20 # pylint: disable=unused-import
---> 21 from tensorflow.python import tf2
22 from keras import distribute
24 from keras import models
ModuleNotFoundError: No module named 'tensorflow'
(c_X_train, c_y_train), (c_X_test, c_y_test) = cifar10.load_data()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 (c_X_train, c_y_train), (c_X_test, c_y_test) = cifar10.load_data()
NameError: name 'cifar10' is not defined
c_X_test.shape
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 c_X_test.shape
NameError: name 'c_X_test' is not defined
nb_classes = 10
c_X_train_flat = c_X_train.reshape(50000, 3072) #add an additional dimension to represent the single-channel
c_X_test_flat = c_X_test.reshape(10000, 3072)
c_X_train_flat = c_X_train_flat.astype('float32') # change integers to 32-bit floating point numbers
c_X_test = c_X_test.astype('float32')
c_X_train_flat /= 255 # normalize each value for each pixel for the entire vector for each input
c_X_test /= 255
c_Y_train = np_utils.to_categorical(c_y_train, nb_classes)
c_Y_test = np_utils.to_categorical(c_y_test, nb_classes)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [4], in <cell line: 3>()
1 nb_classes = 10
----> 3 c_X_train_flat = c_X_train.reshape(50000, 3072) #add an additional dimension to represent the single-channel
4 c_X_test_flat = c_X_test.reshape(10000, 3072)
7 c_X_train_flat = c_X_train_flat.astype('float32') # change integers to 32-bit floating point numbers
NameError: name 'c_X_train' is not defined
model_dropout = Sequential()
model_dropout.add(Dense(512, input_shape=(3072,)))
model_dropout.add(Activation('sigmoid'))
model_dropout.add(Dropout(0.2))
model_dropout.add(Dense(512))
model_dropout.add(Activation('relu'))
model_dropout.add(Dropout(0.2))
model_dropout.add(Dense(10))
model_dropout.add(Activation('softmax'))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 model_dropout = Sequential()
3 model_dropout.add(Dense(512, input_shape=(3072,)))
4 model_dropout.add(Activation('sigmoid'))
NameError: name 'Sequential' is not defined
model_dropout.summary()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 model_dropout.summary()
NameError: name 'model_dropout' is not defined
model_dropout.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model_dropout.fit(c_X_train_flat, c_Y_train,
batch_size=128, epochs=5,
verbose=1)
score = model_dropout.evaluate(c_X_test_flat, c_Y_test)
score
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 model_dropout.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
2 model_dropout.fit(c_X_train_flat, c_Y_train,
3 batch_size=128, epochs=5,
4 verbose=1)
6 score = model_dropout.evaluate(c_X_test_flat, c_Y_test)
NameError: name 'model_dropout' is not defined
c_X_train = c_X_train.astype('float32') # change integers to 32-bit floating point numbers
c_X_train = c_X_train.astype('float32')
c_X_train /= 255 # normalize each value for each pixel for the entire vector for each input
c_X_train /= 255
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 c_X_train = c_X_train.astype('float32') # change integers to 32-bit floating point numbers
2 c_X_train = c_X_train.astype('float32')
4 c_X_train /= 255 # normalize each value for each pixel for the entire vector for each input
NameError: name 'c_X_train' is not defined
model_cnn = Sequential() # Linear stacking of layers
# Convolution Layer 1
model_cnn.add(Conv2D(32, (3, 3), input_shape=(32,32,3))) # 32 different 3x3 kernels -- so 32 feature maps
model_cnn.add(BatchNormalization(axis=-1)) # normalize each feature map before activation
convLayer01 = Activation('relu') # activation
model_cnn.add(convLayer01)
# Convolution Layer 2
model_cnn.add(Conv2D(32, (3, 3))) # 32 different 3x3 kernels -- so 32 feature maps
model_cnn.add(BatchNormalization(axis=-1)) # normalize each feature map before activation
model_cnn.add(Activation('relu')) # activation
convLayer02 = MaxPooling2D(pool_size=(2,2)) # Pool the max values over a 2x2 kernel
model_cnn.add(convLayer02)
# Convolution Layer 3
model_cnn.add(Conv2D(64,(3, 3))) # 64 different 3x3 kernels -- so 64 feature maps
model_cnn.add(BatchNormalization(axis=-1)) # normalize each feature map before activation
convLayer03 = Activation('relu') # activation
model_cnn.add(convLayer03)
# Convolution Layer 4
model_cnn.add(Conv2D(64, (3, 3))) # 64 different 3x3 kernels -- so 64 feature maps
model_cnn.add(BatchNormalization(axis=-1)) # normalize each feature map before activation
model_cnn.add(Activation('relu')) # activation
convLayer04 = MaxPooling2D(pool_size=(2,2)) # Pool the max values over a 2x2 kernel
model_cnn.add(convLayer04)
model_cnn.add(Flatten()) # Flatten final 4x4x64 output matrix into a 1024-length vector
# Fully Connected Layer 5
model_cnn.add(Dense(512)) # 512 FCN nodes
model_cnn.add(BatchNormalization()) # normalization
model_cnn.add(Activation('relu')) # activation
# Fully Connected Layer 6
model_cnn.add(Dropout(0.2)) # 20% dropout of randomly selected nodes
model_cnn.add(Dense(10)) # final 10 FCN nodes
model_cnn.add(Activation('softmax')) # softmax activation
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 model_cnn = Sequential() # Linear stacking of layers
3 # Convolution Layer 1
4 model_cnn.add(Conv2D(32, (3, 3), input_shape=(32,32,3))) # 32 different 3x3 kernels -- so 32 feature maps
NameError: name 'Sequential' is not defined
model_cnn.summary()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 model_cnn.summary()
NameError: name 'model_cnn' is not defined
model_cnn.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model_cnn.fit(c_X_train, c_Y_train,
batch_size=128, epochs=5,
verbose=1)
score = model_cnn.evaluate(c_X_test, c_Y_test)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [11], in <cell line: 1>()
----> 1 model_cnn.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
2 model_cnn.fit(c_X_train, c_Y_train,
3 batch_size=128, epochs=5,
4 verbose=1)
6 score = model_cnn.evaluate(c_X_test, c_Y_test)
NameError: name 'model_cnn' is not defined
score_cnn = model_cnn.evaluate(c_X_test, c_Y_test)
score_cnn
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 score_cnn = model_cnn.evaluate(c_X_test, c_Y_test)
2 score_cnn
NameError: name 'model_cnn' is not defined