Original Source Here

# Image Classification on CIFAR-10 with **15 **Lines OF Code

A quick approach for training a CIFAR-10 image classifier within 15 lines of Code

# Introduction

In this blog , we will build and train an image classifier CNN on the popular CIFAR-10 dataset using transfer learning and with the help of the popular deep learning framework TensorFlow. Our main aim is to showcase that with a few lines of code it is possible to achieve a good accuracy on a large dataset. With more fine tuning of various hyperparameters we can achieve even better results.

# Importing Libraries

We will only import the TensorFlow library since the library is sufficient for all our tasks from data collection to model selection and implementation.

`import tensorflow as tf `

**Dataset**

The CIFAR-10 dataset consists of 60000 32×32 color images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images. We can access this dataset directly through the tensorflow library.

`dataset = tf.keras.datasets.cifar10`

(x_train,y_train), (x_test , y_test) = dataset.load_data()

The data come already separated in training and test subsets . We will use the test data as our validation data and score the performance of the model with the help of validation accuracy.

# Pre-Processing

We will now pre-process our data before fitting the data in our model to reduce the problem of overfitting . This process is known as **Data Augmentation. **We will use the **ImageDataGenerator **function from** **tensorflow library for this purpose.

Firstly we will begin by normalizing our images .

`x_train = x_train /255 ; x_test = x_test/255`

Then we will call the **ImageDataGenerator **function and set various parameters for horizontal flipping , zooming and shift of width and height of images.

`data_generator = tf.keras.preprocessing.image.ImageDataGenerator(`

horizontal_flip=True,

zoom_range=0.05,

width_shift_range=0.05 ,

height_shift_range=0.05)

Now , we will use the** .flow **method to create the training dataset. We will be using a batch size of 32 for this task. Batch size is the number of images that must have passed through the network so that the weights are updated.

`train_generator = data_generator.flow(x_train, y_train, batch_size=32)`

So far we have loaded the necessary library and the dataset and preprocessed our data to fit into our model . All this has been accomplished within 5 lines of code.

# CNN Model Design

We will build our model with the help of transfer learning . We will use the ResNet50V2 model for this purpose. We will also use the already trained ImageNet weights as initialized weights for our model.

We will first design a base model in which we will use transfer learning to import ResNet50V2 and then add this base model to our Sequential API. The ResNet50V2 can be imported from **tensorflow.applications**. In the arguments, we will keep the **include_top = False, **since we will be creating our own fully connected layers in the next part . We will set the **weights=”imagenet”** and in the **input_shape** argument we will use the shape of the image.

`base_model = tf.keras.applications.ResNet50V2(`

include_top=False,

weights="imagenet",

input_shape=x_train.shape[1:])

Next we will create a sequential API for our model using the TensorFlow library. We will first add our **base model** to our main model followed by a a **Flatten** layer .The Flatten layers passes the outputs from the base model to next fully connected layers by converting the output to a 1-D array Net we will add a **Dense** layer with 1024 units followed by a **Dropout** layer with a dropout rate of 0.2 . The dropout layer help in reducing overfitting of training data and thus helps in reduction of high variance. The last dense layer will have 10 output units with activation set to softmax, since this is a multi-class classification.

`model= tf.keras.Sequential()`

model.add(base_model)

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(1024,activation=('relu')))

model.add(tf.keras.layers.Dropout(.2))

model.add(tf.keras.layers.Dense(10,activation=('softmax')))

# Compiling and fitting the model

Since our data is pre-processed and our model is also ready , it’s time to compile the model .

`model.compile(`

optimizer=tf.keras.optimizers.Adam(learning_rate = 0.0005),

loss = "sparse_categorical_crossentropy",

metrics = ["accuracy"])

We will use the Adam Optimizer and set **learning_rate = 0.0005 **. We will be using **loss = “sparse_categorical_crossentropy” **and accuracy as our metric for scoring our results.

After successfully compiling our model we will now call fit function and save the progress metrics in history. Next we use the **train_generator** for our training data and set validation data = **(x_test , y_test)** , since we are scoring our model on validation accuracy. We will set the number of epochs to be **15** and the verbosity to be equal to 1.

`history = model.fit(train_generator , `

validation_data=(x_test , y_test),

steps_per_epoch = x_train.shape[0]/32,

epochs=10 ,

verbose = 1 )

After training the model for 15 epochs we get an accuracy of about on the training set is **83.3% **and about on the test set is** 77.85%**.

At last , we will save our model using the **model.save** function.

`model.save("CIFAR-10.h5")`

# Conclusion

We have successfully trained the CIFAR-10 dataset and have achieved an accuracy of and all this in only 15 lines of code .

import tensorflow as tf #1(x_train , y_train) , (x_test , y_test) = tf.keras.datasets.cifar10.load_data() #2x_train = x_train /255 ; x_test = x_test/255 #3data_generator = tf.keras.preprocessing.image.ImageDataGenerator(

horizontal_flip=True,

zoom_range=0.05, width_shift_range=0.05 , height_shift_range=0.05) #4train_generator = data_generator.flow(x_train, y_train,32) #5base_model = tf.keras.applications.ResNet50V2(

include_top=False,

weights="imagenet",

input_shape=x_train.shape[1:] ,

classes = 10) #6model= tf.keras.Sequential() #7

model.add(base_model) #8

model.add(tf.keras.layers.Flatten()) #9

model.add(tf.keras.layers.Dense(1024,activation=('relu'))) #10

model.add(tf.keras.layers.Dropout(.2)) #11

model.add(tf.keras.layers.Dense(10,activation=('softmax'))) #12

model.compile(

optimizer=tf.keras.optimizers.Adam(learning_rate = 0.0005),

loss = "sparse_categorical_crossentropy",

metrics = ["accuracy"]) #13history = model.fit(train_generator ,

validation_data=(x_test , y_test),

steps_per_epoch = x_train.shape[0]/32,

epochs=10 ,

verbose = 1 ) #14model.save("CIFAR-10.h5") #15

I hope you enjoyed it.

The code and the trained model for this blog can be accessed here — https://github.com/sanskar-hasija/CIFAR10-15LOC

Also, if you want a full-fledged version of this classification with better choice of hyperparameters settings , you can check this — https://github.com/sanskar-hasija/CIFAR_10_ResNet

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot