Meme Classification Using CNN in Python

In this tutorial, we will work on Image Classification using TensorFlow to classify an image if it is a MEME or NOT. I will also let you know how to create your own dataset rather than using already available datasets on the internet for this task.

To perform this machine learning task, we will use the TensorFlow Python module and use it’s Keras API.

Our goal here is to build a binary classifier using CNN to categorize the images correctly as MEME or NOT MEME with the help of Python programming. So the task here is meme classification using CNN in Python language.

I hope you already know about CNN.

DATASET CREATION

As we all know for making a model we need 2 different sets:-

  • Training set
  • Test set (Here, I’ll use the test dataset as a validation dataset )

Create two folders as shown in the above image. Then inside both of these folders create two more folders with the name MEME and NOT MEME respectively.

Talking about the Training set folder add around 500+ images in MEME and NOT MEME folders. Then do the same thing with the Test set and add 1/3rd different images you added in the training set folder. Remember images in the meme folder should be nearly equal to not meme folder to avoid biasness. So, Here’s Your Dataset is ready now.

Before starting you must install all the required libraries mentioned below.

IMPORTING THE PYTHON LIBRARIES

Let’s first import all the necessary Python libraries that we are going to use:

import tensorflow as tf
import os
from tensorflow import keras
from tensorflow.keras import layers, optimizers, Sequential
from tensorflow.keras.layers import Activation , Dropout , Conv2D, MaxPooling2D, Dense, Flatten
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np

BUILDING THE MODEL

model = Sequential()
model.add(Conv2D(32, (3,3), input_shape = (64,64,3), activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.summary()

Let’s see what layer’s I have used and their working

  • Conv2D: This is the first layer of your model which is the input layer where feature extraction takes place.
  • MaxPooling: It is a pooling operation that selects the maximum element from the region of the feature map that is covered by the filter.
  • Flatten: Flatten is used to transform the input into a vector and feed it into the fully connected layer.
  • Dense Layer: It is just a regular layer of neurons in a neural network. Here each neuron actually receives input from all the other neurons in the previous layer, thus densely connected.
  • Dropout: This is a technique that is used to prevent a model from overfitting.
  • Sigmoid and RELU Activation Functions

OUTPUT:

IMAGE AUGMENTATION AND IMAGEDATAGENERATOR

Image Augmentation is the technique which increases the number of images available for training models, without actually collecting new images. For Image augmentation, you can directly use ImageDataGenerator class available in Keras. See how:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
        rescale=1. / 255, 
        rotation_range=30,  
        zoom_range = 0.15,  
        width_shift_range=0.10,  
        height_shift_range=0.10,  
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
    'ENTER LOCATION OF THE TRAINING SET FOLDER',
    target_size=(64,64),
    batch_size=15,
    class_mode='binary')

test_set = test_datagen.flow_from_directory(
    'ENTER LOCATION OF THE TEST SET FOLDER',
    target_size=(64,64),
    batch_size=15,
    class_mode='binary')

OUTPUT:

 

COMPILING AND TRAINING THE MODEL

Training my model on GPU:

with tf.device('/GPU:0'):
    history = model.compile( optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
    model.fit(
        training_set,
        steps_per_epoch=991//15,
        epochs=30,
        validation_data=test_set,
        validation_steps=296//15
    )

OUTPUT:

We get a validation accuracy of around 80% which is the model’s ability to deal with new images.

After training just save your model.

model.save('model.h5')

For instance, let’s see how the model responds to an image we feed.

We will use this image:

Below is our Python code:

classifier = load_model('model.h5')

path = 'E:/Deep_Learning/Meme_Classification/test_image.jpg'
img_original = load_img(path)
img = load_img(path, target_size = (64,64))
img_tensor = img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis = 0)
img_tensor/=255.0

pred = classifier.predict(img_tensor)
print(pred)

if pred<.5: str = '--------------Meme--------------'
else: str = '--------------Non meme--------------'

plt.imshow(img_original)
plt.axis('off')
plt.title(str)
plt.show()

OUTPUT:

And our model predicts the image as a meme which is actually a meme. So we did the Meme Classification task successfully using CNN in Python language.

Thanks for reading!

3 responses to “Meme Classification Using CNN in Python”

  1. Aman Bahuguna says:

    Awesome tutorial buddy.
    Looking forward for more.

  2. Dr indrajeet kumar says:

    Good work dear.
    You can improve the accuracy.

  3. Vaibhav Nabiyal says:

    Superb..bro….

    Thank you for this valuable code

Leave a Reply

Your email address will not be published. Required fields are marked *