<jupyter_start><jupyter_text>Exercise 8.03Import the required Libraries<jupyter_code>import numpy as np
import keras
from keras.layers import Dense
from keras.models import Sequential
from tensorflow import random<jupyter_output>Using TensorFlow backend.<jupyter_text>Initiate the Model<jupyter_code>vgg_model = keras.applications.vgg16.VGG16()<jupyter_output><empty_output><jupyter_text>Check the model summary<jupyter_code>vgg_model.summary()<jupyter_output>Model: "vgg16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_____________________________________________________________[...]<jupyter_text>Remove the last layer<jupyter_code>last_layer = str(vgg_model.layers[-1])

np.random.seed(42)
random.set_seed(42)
classifier= Sequential()
for layer in vgg_model.layers:
    if str(layer) != last_layer:
        classifier.add(layer)<jupyter_output><empty_output><jupyter_text>Recheck the summary & Last layer<jupyter_code>classifier.summary()<jupyter_output>Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
______________________________________________________[...]<jupyter_text>Freeze layers<jupyter_code>for layer in classifier.layers:
    layer.trainable=False<jupyter_output><empty_output><jupyter_text>Add a new layer & Check summary & last layer<jupyter_code>classifier.add(Dense(1, activation='sigmoid'))
classifier.summary()<jupyter_output>Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
______________________________________________________[...]<jupyter_text>Complie the network<jupyter_code>classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Process the image and fit itCreate training and test data generators<jupyter_code>from keras.preprocessing.image import ImageDataGenerator

generate_train_data = ImageDataGenerator(rescale = 1./255,
                                         shear_range = 0.2,
                                         zoom_range = 0.2,
                                         horizontal_flip = True)

generate_test_data = ImageDataGenerator(rescale =1./255)<jupyter_output><empty_output><jupyter_text>Create training and test datasets<jupyter_code>training_dataset = generate_train_data.flow_from_directory('../Data/Dataset/training_set',
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_datasetset = generate_test_data.flow_from_directory('../Data/Dataset/test_set',
                                            target_size = (224, 224),
                                            batch_size = 32,
                                            class_mode = 'binary')<jupyter_output>Found 10764 images belonging to 2 classes.
Found 2674 images belonging to 2 classes.<jupyter_text>Fit the model to the training data<jupyter_code>classifier.fit_generator(training_dataset,
                         steps_per_epoch = 100,
                         epochs = 10,
                         validation_data = test_datasetset,
                         validation_steps = 30,
                         shuffle=False)<jupyter_output>Epoch 1/10
100/100 [==============================] - 1306s 13s/step - loss: 0.4165 - accuracy: 0.8019 - val_loss: 0.4084 - val_accuracy: 0.8750
Epoch 2/10
100/100 [==============================] - 1265s 13s/step - loss: 0.3124 - accuracy: 0.8706 - val_loss: 0.2068 - val_accuracy: 0.8969
Epoch 3/10
100/100 [==============================] - 1280s 13s/step - loss: 0.2556 - accuracy: 0.8984 - val_loss: 0.1578 - val_accuracy: 0.8879
Epoch 4/10
100/100 [==============================] - 1246s 12s/step - loss: 0.2453 - accuracy: 0.8903 - val_loss: 0.1456 - val_accuracy: 0.9083
Epoch 5/10
100/100 [==============================] - 817s 8s/step - loss: 0.2383 - accuracy: 0.8997 - val_loss: 0.1684 - val_accuracy: 0.9000
Epoch 6/10
100/100 [==============================] - 661s 7s/step - loss: 0.2359 - accuracy: 0.9006 - val_loss: 0.1790 - val_accuracy: 0.9017
Epoch 7/10
100/100 [==============================] - 645s 6s/step - loss: 0.2169 - accuracy: 0.9106 - val_loss: 0.1694 - val_accuracy[...]<jupyter_text>Predict a new imageLoad the image snad show the image<jupyter_code>import numpy as np
from keras.preprocessing import image
new_image = image.load_img('../Data/Prediction/test_image_2.jpg', target_size = (224, 224))
new_image<jupyter_output><empty_output><jupyter_text>Show the class labels<jupyter_code>training_dataset.class_indices<jupyter_output><empty_output><jupyter_text>Make the prediction<jupyter_code>new_image = image.img_to_array(new_image)
new_image = np.expand_dims(new_image, axis = 0)
result = classifier.predict(new_image)

if result[0][0] == 1:
    prediction = 'It is a flower'
else:
    prediction = 'It is a car'

print(prediction)<jupyter_output>It is a car