Breaking News

Create a Tensorflow keras image classification model

Keras is widely used for creating different type of machine learning and deep learning algorithms on top of other machine learning libraries such as Tensorflow and Theano. In this blog post, we will write python code using keras on tensorflow backend to train a image classification model.

Image classification using Convolutional neural networks provides best result. We will try to preprocess our data and then feed that data to our keras model.
We split this part in four Phases

  1. Data Collection
  2. Data Preprocessing
  3. Create a Keras Model
  4. Training and Testing

Keras Image Classification model

Data Collection
Most time taking and hardworking procedure is to collect data. Data collection is the first step in every deep learning algorithm. Deep learning requires a good amount of data for training a model, so data is most important thing in deep learning.
There are different sources from which you can get data such as Kaggle, which has a large collection of data from different companies. If you want to train model on own dataset, you are required to collect data for this. In this example, we will try to create a fruit image classification model. Complete dataset is available on Kaggle.
Fruits 360 dataset | Kaggle
You can use your own dataset too. We are using 14 fruit images to train a model. So create a new folder/directory for your project and create a new folder named dataset and paste your data in that folder such as shows in given example.

projectFolder
  • dataset
    • Apple
      • Image 1
      • Image 2
      • Image 3
    • Mango
      • Image 1
      • Image 2
      • Image 3
    • Banana
      • Image 1
      • Image 2
      • Image 3

Data Preprocessing
Data is required to be in equal dimensions so we can feed that data to our network. For this we need different types of libraries such as OpenCV and Numpy. So first of all we install all required libraries required for training our model. Run this command to install required libraries

pip install numpy opencv-python tensorflow keras
After this create a new python file in parent directory on project and start coding.
# import all reauired libs
import numpy as np
import cv2
import os


# Create variables required in code
DATA_DIR = "G:/Kaggle/fruits/fruits-360/Training"
IMG_HEIGHT = 150
IMG_WIDTH = 150
NUM_CLASSES = 0
ALLOWED_TYPES = ["jpg", "jpeg", "png"]
TRAIN_DATA = []
TRAIN_LABELS = []

# Preprocess images
# count no of classes
for folder in os.listdir(DATA_DIR):
    NUM_CLASSES += 1

# Create labels
LABELS_MASTER = np.identity(NUM_CLASSES)

print("[info] Preprocessing Images...")

counter = 0
# Loop through every folder in dataset
for folder in os.listdir(DATA_DIR):
    print("[info] Processing:", folder)
    for imgName in os.listdir(os.path.join(DATA_DIR, folder)):
        if imgName.split(".")[-1].lower() in ALLOWED_TYPES:
            image = cv2.imread(os.path.join(DATA_DIR, folder, imgName))
            image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH))
            TRAIN_DATA.append(image)
            label = LABELS_MASTER[counter]
            TRAIN_LABELS.append(label)
        else:
            print("[info] Error: image format incorrect")
    counter += 1

TRAIN_DATA = np.array(TRAIN_DATA)
TRAIN_LABELS = np.array(TRAIN_LABELS)
print("[info] Preprocessing Done...")      

Next step is to split data in three parts to train model. Training, Validation and Testing.
Training: 70%
Validation: 15%
Testing: 15%

from sklearn.model_selection import train_test_split
# Validation data 15% and Train data 85%
(trainX, val_x, trainY, val_y) = train_test_split(TRAIN_DATA,
 TRAIN_LABELS, test_size=0.15, random_state=42)

# From 85% train data, split data into 70% for train and 15% for test
(trainX, testX, trainY, testY) = train_test_split(trainX,
 trainY, test_size=0.15, random_state=42)

Now we will create a keras model to train our image classification model.

from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
from sklearn.metrics import confusion_matrix
from keras.models import Sequential
from keras.layers import Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint

EPOCHS = 20

model = Sequential()

# Add a 32 convolutional layer of 3x3 size
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=(IMG_HEIGHT , IMG_WIDTH, 3)))
model.add(Conv2D(64, (3, 3), activation='relu'))

# Add a 2x2 size polling layer
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(128, (3,3), activation='relu'))
model.add(Conv2D(128, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(256, activation='relu'))

# Softmax activation function
model.add(Dense(NUM_CLASSES, activation='softmax'))


# Save best weights according to best validation accuracy
filepath = "weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

# Compile and train
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

# Train Model
H = model.fit(trainX, trainY, validation_data = (val_x, val_y), epochs=EPOCHS, callbacks = callbacks_list, verbose=1)

model.save("my_model.model")

No comments