Image Classification using Keras: Intel Scene Classification Challenge

Steve Rogers
Apr 20, 2019 · 3 min read

Identifying natural scenes from all around the world is an interesting computer vision problem. In this challenge, we are going to classify six different category Imges(buildings,forest,glacier,mountain,sea, street). This article is designed to be a tutorial for those who are just getting started with Convolutional Neural Networks for Image Classification and want to see how to experiment with network architecture, hyperparameters, data augmentations, and how to deal with loading custom data for test and train. Contest Link

Problem Statement

Dataset Description

'buildings' -> 0
'forest' -> 1
'glacier' -> 2
'mountain' -> 3
'sea' -> 4
'street' -> 5

Setting up the System

  • pandas
  • matplotlib
  • tensorflow
  • keras
  • numpy

Importing Libraries

import pandas as pd
import numpy as np
import glob
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
import os,shutil
from keras import models,layers,optimizers
from keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D

Reading train data

df= pd.read_csv("../input/trainimage/train.csv")
df = df.replace(0,'buildings').replace(1,'forest').replace(2,'glacier').replace(3,'mountain').replace(4,'sea').replace(5,'street')

Image Augmentation using keras ImageDataGenerator

datagen = ImageDataGenerator(rescale=1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip=True,
preprocessing_function= preprocess_input,
validation_split=0.25)
train_generator = datagen.flow_from_dataframe(dataframe=df,
directory="../input/trainimage/train",
x_col="image_name",
y_col="label",
subset="training",
batch_size=100,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(150,150))
valid_generator = datagen.flow_from_dataframe(dataframe=df,
directory="../input/trainimage/train",
x_col="image_name",
y_col="label",
subset="validation", batch_size=10,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(150,150))

Buliding a CNN model using ResNet 50 pretrained on ImageNet

from keras.applications.resnet50 import ResNet50res_conv = ResNet50(include_top=False,
weights='imagenet',
input_tensor=None,
input_shape=(150,150,3),
pooling=None,classes=1000)
model = models.Sequential()
model.add(res_conv)

model.add(Conv2D(32, 3, 3, border_mode='same', input_shape=(150,150,3), activation='relu'))
model.add(Conv2D(32, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 1)))

model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 1)))

model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(6, activation='softmax'))

# Show a summary of the model. Check the number of trainable parameters
model.summary()
model.compile(loss='categorical_crossentropy',optimizer = optimizers.RMSprop(lr=1e-4),metrics=['acc'])

Training our Network

STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_sizeSTEP_SIZE_VALID=valid_generator.n//valid_generator.batch_sizehistory =model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=20,verbose=1)

Saving a model

model.save("inception.h5")

Conclusion

beovolytics

Beovolytics integrates software assembly lines into large organizations by deploying solutions offered by the Cloud ecosystem.

Steve Rogers

Written by

beovolytics

Beovolytics integrates software assembly lines into large organizations by deploying solutions offered by the Cloud ecosystem.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade