Image Classification using Keras: Intel Scene Classification Challenge

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
How do we, humans, recognize a forest as a forest or a mountain as a mountain? We are very good at categorizing scenes based on the semantic representation and object affinity, but we know very little about the processing and encoding of natural scene categories in the human brain. In this problem, you are provided with a dataset of ~25k images from a wide range of natural scenes from all around the world. Your task is to identify which kind of scene can the image be categorized into.
Dataset Description
The categories of scenes and their corresponding labels in the dataset are as follows(Dataset link) -
'buildings' -> 0
'forest' -> 1
'glacier' -> 2
'mountain' -> 3
'sea' -> 4
'street' -> 5
Setting up the System
Before we actually get into the model building phase, we need to ensure that the right libraries and frameworks have been installed. The below libraries are required to run this project:
- 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
Hopefully, this article helps you load data and get familiar with formatting image data, as well as learn more about image classification and convolutional neural networks.