<jupyter_start><jupyter_text>Package version- tensorflow==2.2.0rc3- numpy==1.18.2- matplotlib==3.2.1- image==1.5.30- google==2.0.3  Import Tensorflow<jupyter_code>import tensorflow
tensorflow.__version__<jupyter_output><empty_output><jupyter_text>**Load any image**<jupyter_code># skimage.data has a lot of sample images to play around with
from skimage import data
import matplotlib.pyplot as plt

image = data.coins()
plt.imshow(image, cmap='gray')<jupyter_output><empty_output><jupyter_text>**Import utilities needed for convolution**<jupyter_code>from scipy.signal import convolve2d<jupyter_output><empty_output><jupyter_text>**Find the 5X5 Guassian Blur kernel with sigma = 1.0 and convolve the above image with that kernel***Hint: You can create various Guassian kernel at http://dev.theomader.com/gaussian-kernel-calculator/*<jupyter_code>import numpy as np
kernel = np.array([[0.003765,0.015019,0.023792,0.015019,0.003765],
                   [0.015019,0.059912,0.094907,0.059912,0.015019],
                   [0.023792,0.094907,0.150342,0.094907,0.023792],
                   [0.015019,0.059912,0.094907,0.059912,0.015019],
                   [0.003765,0.015019,0.023792,0.015019,0.003765]])<jupyter_output><empty_output><jupyter_text>**Convole the guassian kernel with the image and use 'valid' convolution and show the result side by side**<jupyter_code>from scipy.signal import convolve2d
blurred_image = convolve2d(image, kernel, mode = 'valid')
plt.imshow(blurred_image,cmap='gray')
plt.show()
plt.imshow(image,cmap='gray')<jupyter_output><empty_output><jupyter_text>Build a CNN to classify 10 monkey species **Mounting Google Drive on to the Google Colab instance**<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).<jupyter_text>**Set the appropriate path for the datsaet zip provided**<jupyter_code>images_path = "/content/drive/My Drive/monkeys_dataset.zip"<jupyter_output><empty_output><jupyter_text>**Extracting the dataset.zip to the present working directory**<jupyter_code>from zipfile import ZipFile
with ZipFile(images_path, 'r') as zip:
  zip.extractall()<jupyter_output><empty_output><jupyter_text>*Check the list of files in the pwd(present working directory) by running command 'ls' and ensure 'dataset' folder has been generated*<jupyter_code>!ls<jupyter_output>classifier.h5  classifier_weights.h5  dataset  drive  sample_data<jupyter_text>**Build a Sequential CNN classifier with input shape as 64X64 and using three sets of Convoltutional + Pooling layers. You can additionally use Dropout in the fully connected layers. Make sure the final layer shape matches with the number of classes**<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

# Initialising the CNN classifier
classifier = Sequential()

# Add a Convolution layer with 32 kernels of 3X3 shape with activation function ReLU
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu', padding = 'same'))

# Add a Max Pooling layer of size 2X2
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Add another Convolution layer with 32 kernels of 3X3 shape with activation function ReLU
classifier.add(Conv2D(32, (3, 3), activation = 'relu', padding = 'same'))

# Adding another pooling layer
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Add another Convolution layer with 32 kernels of 3X3 shape with activation function ReLU
classifier.add(Conv2D(32, (3, 3), activation = 'relu', padding = 'valid'))

# Adding another pooling layer
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Flattening the layer before fully connected layers
classifier.add(Flatten())

# Adding a fully connected layer with 512 neurons
classifier.add(Dense(units = 512, activation = 'relu'))

# Adding dropout with probability 0.5
classifier.add(Dropout(0.5))


# Adding a fully connected layer with 128 neurons
classifier.add(Dense(units = 128, activation = 'relu'))


# The final output layer with 10 neurons to predict the categorical classifcation
classifier.add(Dense(units = 10, activation = 'softmax'))<jupyter_output><empty_output><jupyter_text>**Compile the CNN classifier with Adam optimizer (default Learning rate and other parameters)and Categorical Crossentropy as loss function and Accuracy as the metric to monitor** *Optionally you can use an optimizer with custom learning rate and passing it to the optimizer parameter of compile**Eg: tensorflow.keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)*<jupyter_code>classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>**Use ImageDataGenerator to create a test and training set data generators and use fit_generator() function to train the model** *ImageDataGenerator is a powerful preprocessing utility to generate training and testing data with common data augmentation techniques. It can also be used to  generate training data from Images stored in hierarchical directory structuresFor more options of ImageDataGenerator go to https://keras.io/preprocessing/image/*<jupyter_code>from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Create data generator for training data with data augmentation and normalizing all
# values by 255
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)


test_datagen = ImageDataGenerator(rescale = 1./255)

# Setting training data generator's source directory
# Setting the target size to resize all the images to (64,64) as the model input layer expects 64X64 images

training_set = train_datagen.flow_from_directory('./dataset/train',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

# Setting testing data generator's source directory
test_set = test_datagen.flow_from_directory('./dataset/test',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'categorical')


# There are 1098 training images and 272 test images in total
classifier.fit_generator(training_set,
                         steps_per_epoch = int(1098/32),
                         epochs = 1,
                         validation_data = test_set,
                         validation_steps = int(272/32))<jupyter_output>Found 1098 images belonging to 10 classes.
Found 272 images belonging to 10 classes.
34/34 [==============================] - 38s 1s/step - loss: 2.2958 - accuracy: 0.1471 - val_loss: 2.2120 - val_accuracy: 0.2070<jupyter_text>**save the model and its weights**<jupyter_code>classifier.save('./classifier.h5')
classifier.save_weights('./classifier_weights.h5')<jupyter_output><empty_output><jupyter_text>*Check the current directory if the weights have been saved*<jupyter_code>!ls<jupyter_output>classifier.h5  classifier_weights.h5  dataset  drive  sample_data<jupyter_text>Testing the model **Load the pre-trained saved model and load the weights**<jupyter_code>from tensorflow.keras.models import load_model

# Load the pre trained model from the HDF5 file saved previously
pretrained_model = load_model('./classifier.h5')
pretrained_model.load_weights('./classifier_weights.h5')<jupyter_output><empty_output><jupyter_text>**Test the model on one single image from the test folders**<jupyter_code>import cv2
test_image = cv2.imread('/content/dataset/test/n0/n000.jpg')


# Check if the size of the Image array is compatible with Keras model
print(test_image.shape)

# Reshape the image to 64x64x3
test_image = cv2.resize(test_image, (64, 64))
print(test_image.shape)

# If not compatible expand the dimensions to match with the Keras Input
test_image = np.expand_dims(test_image, axis = 0)
test_image =test_image*1/255.0

#Check the size of the Image array again
print('After expand_dims: '+ str(test_image.shape))


#Predict the result of the test image
result = classifier.predict(test_image)

# Check the indices Image Data Generator has allotted to each folder
classes_dict = training_set.class_indices
print(classes_dict)

# Creating a list of classes in test set for showing the result as the folder name
prediction_class = []
for class_name,index in classes_dict.items():
  prediction_class.append(class_name)
  
print(result[0])

# Index of the class with maximum probability
predicted_index = np.argmax(result[0])

# Print the name of the class
print(prediction_class[predicted_index])<jupyter_output>(331, 500, 3)
(64, 64, 3)
After expand_dims: (1, 64, 64, 3)
{'n0': 0, 'n1': 1, 'n2': 2, 'n3': 3, 'n4': 4, 'n5': 5, 'n6': 6, 'n7': 7, 'n8': 8, 'n9': 9}
[0.11890665 0.0843783  0.08120754 0.18248226 0.08211511 0.10593714
 0.07660571 0.08192824 0.07960276 0.1068363 ]
n3
{'fraction_non_alphanumeric': 0.0987347247756029, 'fraction_numerical': 0.0580728885043798, 'mean_word_length': 4.317998849913743, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '538079', 'license': 'MIT', 'n_tokens_mistral': 3200, 'n_tokens_neox': 2815, 'n_words': 840, 'path': '09_Computer_Vision/coin_prediction.ipynb', 'repo_name': 'shekarroy15/AIML-Projects', 'size': '9247'}