Pokemon Classification using Keras in Python
In this blog, We will learn about how to do Image classification using Keras in Python by classifying Pokemon. We will make a deep learning model that can classify different types of Pokemon. The dataset can be taken from this GitHub repository
This dataset contains 10 different pokemon such as:
Now, we will build a deep learning model.
We will use the Sequential model with layers as Convolutional, Maxpooling, Flatten, and Dropouts to reduce overfitting.
The shape of the image passed is (64,64,3) as we have changed the size of the image to 64,64 and 3 is the number of channels as it is the colored image and has 3 channels RGB.
We will use Relu activations for the hidden layers and Softmax for the output layer.
np.random.seed(1000) #Instantiate an empty model model = Sequential() # 1st Convolutional Layer model.add(Conv2D(filters=32, input_shape=(64,64,3), kernel_size=(3,3) , padding='valid')) model.add(Activation('relu')) # Max Pooling model.add(MaxPooling2D(pool_size=(2,2), padding='valid')) # 2nd Convolutional Layer model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid')) model.add(Activation('relu')) # Max Pooling model.add(MaxPooling2D(pool_size=(2,2), padding='valid')) # 3rd Convolutional Layer model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid')) model.add(Activation('relu')) # Max Pooling model.add(MaxPooling2D(pool_size=(2,2), padding='valid')) # 4th Convolutional Layer model.add(Conv2D(filters=128, kernel_size=(3,3), padding='valid')) model.add(Activation('relu')) # Max Pooling model.add(MaxPooling2D(pool_size=(2,2), padding='valid')) # Passing it to a Fully Connected layer model.add(Flatten()) # 1st Fully Connected Layer model.add(Dense(128)) model.add(Activation('relu')) # Add Dropout to prevent overfitting model.add(Dropout(0.2)) # 2nd Fully Connected Layer model.add(Dense(256)) model.add(Activation('relu')) # Add Dropout model.add(Dropout(0.2)) # 3rd Fully Connected Layer model.add(Dense(128)) model.add(Activation('relu')) # Add Dropout model.add(Dropout(0.2)) # Output Layer model.add(Dense(10)) model.add(Activation('softmax')) model.summary()
Output: Model: "sequential_3" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_10 (Conv2D) (None, 62, 62, 32) 896 _________________________________________________________________ activation_67 (Activation) (None, 62, 62, 32) 0 _________________________________________________________________ max_pooling2d_10 (MaxPooling (None, 31, 31, 32) 0 _________________________________________________________________ conv2d_11 (Conv2D) (None, 29, 29, 64) 18496 _________________________________________________________________ activation_68 (Activation) (None, 29, 29, 64) 0 _________________________________________________________________ max_pooling2d_11 (MaxPooling (None, 14, 14, 64) 0 _________________________________________________________________ conv2d_12 (Conv2D) (None, 12, 12, 64) 36928 _________________________________________________________________ activation_69 (Activation) (None, 12, 12, 64) 0 _________________________________________________________________ max_pooling2d_12 (MaxPooling (None, 6, 6, 64) 0 _________________________________________________________________ conv2d_13 (Conv2D) (None, 4, 4, 128) 73856 _________________________________________________________________ activation_70 (Activation) (None, 4, 4, 128) 0 _________________________________________________________________ max_pooling2d_13 (MaxPooling (None, 2, 2, 128) 0 _________________________________________________________________ flatten_4 (Flatten) (None, 512) 0 _________________________________________________________________ dense_12 (Dense) (None, 128) 65664 _________________________________________________________________ activation_71 (Activation) (None, 128) 0 _________________________________________________________________ dropout_7 (Dropout) (None, 128) 0 _________________________________________________________________ dense_13 (Dense) (None, 256) 33024 _________________________________________________________________ activation_72 (Activation) (None, 256) 0 _________________________________________________________________ dropout_8 (Dropout) (None, 256) 0 _________________________________________________________________ dense_14 (Dense) (None, 128) 32896 _________________________________________________________________ activation_73 (Activation) (None, 128) 0 _________________________________________________________________ dropout_9 (Dropout) (None, 128) 0 _________________________________________________________________ dense_15 (Dense) (None, 10) 1290 _________________________________________________________________ activation_74 (Activation) (None, 10) 0 ================================================================= Total params: 263,050 Trainable params: 263,050 Non-trainable params: 0
We have made the model with 263K parameters.
Now we will compile our model by using Adam as the Optimizer and Categorical loss entropy as the loss.
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
Finally, we will train the model for 10 epochs using fit_generator.
his=model.fit_generator(train,epochs=10,shuffle=True)
Output: Epoch 1/10 37/52 [====================>.........] - ETA: 10s - loss: 0.6556 - accuracy: 0.777152/52 [==============================] - 35s 678ms/step - loss: 0.6685 - accuracy: 0.7783 Epoch 2/10 52/52 [==============================] - 35s 665ms/step - loss: 0.6527 - accuracy: 0.7881 Epoch 3/10 52/52 [==============================] - 35s 665ms/step - loss: 0.6350 - accuracy: 0.7862 Epoch 4/10 52/52 [==============================] - 35s 673ms/step - loss: 0.6144 - accuracy: 0.7942 Epoch 5/10 52/52 [==============================] - 36s 687ms/step - loss: 0.5790 - accuracy: 0.8161 Epoch 6/10 52/52 [==============================] - 35s 669ms/step - loss: 0.6082 - accuracy: 0.7978 Epoch 7/10 52/52 [==============================] - 35s 668ms/step - loss: 0.5465 - accuracy: 0.8094 Epoch 8/10 52/52 [==============================] - 35s 669ms/step - loss: 0.5960 - accuracy: 0.7996 Epoch 9/10 52/52 [==============================] - 35s 680ms/step - loss: 0.5819 - accuracy: 0.8076 Epoch 10/10 52/52 [==============================] - 36s 687ms/step - loss: 0.5272 - accuracy: 0.8270
Evaluating the Training Accuracy
print(model.evaluate(train))
Output: 52/52 [==============================] - 33s 640ms/step [0.1573140025138855, 0.8532277941703796]
We got 85% training accuracy which is good.
Lastly, we will predict the images on the trained model.
model.predict_classes(test,batch_size=None)
Output: array([7, 4, 8, 5, 0, 7, 7, 3, 6, 9, 6, 6, 2, 8, 9, 6, 4, 1, 1, 9, 6, 6,9, 1, 5, 3, 4, 9, 2, 9, 4, 7, 9, 9, 4, 4, 0, 3, 9, 4, 6, 0, 5, 4, 4, 3, 1, 9, 9, 6, 2, 8, 2, 1, 2, 9, 7, 8, 3, 2, 1, 1, 9, 7, 6, 2, 4, 2, 2, 4, 2, 6, 6, 3, 9, 4, 7, 7, 1, 1, 8, 9, 2, 5, 1, 6, 2, 2, 6, 0, 8, 7, 2, 6, 9, 1, 9, 4, 9, 5, 9, 6, 0, 6, 2, 0, 9, 2, 3, 8, 8, 1, 9, 5, 0, 1, 2, 5, 8, 1, 9, 6, 6, 5, 6, 2, 1, 7, 9, 9, 2, 6, 1, 0, 1, 4, 2, 2, 6, 6, 9, 9, 9, 9, 7, 6, 7, 1, 9, 7, 2, 0, 4, 1, 7, 4, 2, 6, 3, 1, 1, 2, 2, 2, 5, 6, 1, 6, 6, 1, 1, 9, 1, 6, 3, 2, 9, 9, 7, 6, 6, 1, 1, 1, 6, 6, 6])
Conclusion
In this blog, we have learned about how to make an image classification model using Keras.
The comment box is yours. Feel free to comment on any doubts or suggestions below.
Leave a Reply