A Simple Image Classification Problem using Keras (dog_vs_cat)


In [ ]:
# import the necessary packages
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Activation
from keras.optimizers import SGD
from keras.layers import Dense
from keras.utils import np_utils
from imutils import paths
import numpy as np
import argparse
import os
import cv2
import pandas as pd
import numpy as np

In [2]:
def image_to_feature_vector(image, size=(32, 32)):
	# resize the image to a fixed size, then flatten the image into
	# a list of raw pixel intensities
	return cv2.resize(image, size).flatten()

In [3]:
import glob
print("[INFO] describing images...")
train_image_path = "data/train/"
image_paths = glob.glob(os.path.join(train_image_path, '*.jpg'))

# initialize the data matrix and labels list
data = []
labels = []


[INFO] describing images...

In [4]:
# loop over the input images
for (i, imagePath) in enumerate(image_paths):
    # load the image and extract the class label (assuming that our
    # path as the format: /path/to/dataset/{class}.{image_num}.jpg
    image = cv2.imread(imagePath)
    label = imagePath.split(os.path.sep)[-1].split(".")[0]
 
    # construct a feature vector raw pixel intensities, then update
    # the data matrix and labels list
    features = image_to_feature_vector(image)
    data.append(features)
    labels.append(label)
 
    # show an update every 1,000 images
    if i > 0 and i % 1000 == 0:
        print("[INFO] processed {}/{}".format(i, len(image_paths)))


[INFO] processed 1000/25000
[INFO] processed 2000/25000
[INFO] processed 3000/25000
[INFO] processed 4000/25000
[INFO] processed 5000/25000
[INFO] processed 6000/25000
[INFO] processed 7000/25000
[INFO] processed 8000/25000
[INFO] processed 9000/25000
[INFO] processed 10000/25000
[INFO] processed 11000/25000
[INFO] processed 12000/25000
[INFO] processed 13000/25000
[INFO] processed 14000/25000
[INFO] processed 15000/25000
[INFO] processed 16000/25000
[INFO] processed 17000/25000
[INFO] processed 18000/25000
[INFO] processed 19000/25000
[INFO] processed 20000/25000
[INFO] processed 21000/25000
[INFO] processed 22000/25000
[INFO] processed 23000/25000
[INFO] processed 24000/25000

Data Preprocessing


In [5]:
# encode the labels, converting them from strings to integers
le = LabelEncoder()
encoded_labels = le.fit_transform(labels)
pd.DataFrame(encoded_labels).head(5)
print(pd.DataFrame(labels).describe())

normalized_data = np.array(data) / 255.0
categorical_labels = np_utils.to_categorical(encoded_labels, 2)


            0
count   25000
unique      2
top       cat
freq    12500

In [6]:
# partition the data into training and testing splits, using 75%
# of the data for training and the remaining 25% for testing
print("[INFO] constructing training/testing split...")
labels = categorical_labels.tolist
(trainData, testData, trainLabels, testLabels) = train_test_split(data, categorical_labels, test_size=0.25, random_state=42)


[INFO] constructing training/testing split...

Define an architecture - > Feed Forward Network of dimension "3072-768-384-2"


In [9]:
model = Sequential()
model.add(Dense(768, input_dim=3072, kernel_initializer="uniform", activation="relu"))
model.add(Dense(384, kernel_initializer="uniform", activation="relu"))
model.add(Dense(2))
model.add(Activation("softmax"))

In [16]:
# train the model using SGD
print("[INFO] compiling model...")
sgd = SGD(lr=0.001)
model.compile(loss="binary_crossentropy", optimizer=sgd, metrics=["accuracy"])
model.fit(np.array(trainData), np.array(trainLabels), epochs=50, batch_size=128)


[INFO] compiling model...
Epoch 1/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 2/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 3/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 4/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 5/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 6/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 7/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 8/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 9/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 10/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 11/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 12/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 13/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 14/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 15/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 16/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 17/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 18/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 19/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 20/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 21/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 22/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 23/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 24/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 25/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 26/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 27/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 28/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 29/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 30/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 31/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 32/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 33/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 34/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 35/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 36/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 37/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 38/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 39/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 40/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 41/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 42/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 43/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 44/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 45/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 46/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 47/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 48/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 49/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Epoch 50/50
18750/18750 [==============================] - 4s - loss: 8.0160 - acc: 0.4999     
Out[16]:
<keras.callbacks.History at 0x7f6bc514af10>

In [17]:
# show the accuracy on the testing set
print("[INFO] evaluating on testing set...")
(loss, accuracy) = model.evaluate(np.array(testData), np.array(testLabels), batch_size=150, verbose=1)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))


[INFO] evaluating on testing set...
5850/6250 [===========================>..] - ETA: 0s[INFO] loss=8.0100, accuracy: 50.0320%

In [ ]: