MLops : Session 21

MLops : Session 21

No alt text provided for this image

Till my last article, we learn to create the architecture of our model for CNN. Here is no definite architecture for creating our model. We have to change value of hyperparameters so that we get more accuracy for prediction.

Today we are going to see how to give pictures as a dataset and train our model...

We also see some more networks like AlexNet, LeNet...

Creating our architecture for the model :

This is the same code as I describe in the earlier session... I just share the screenshot...

No alt text provided for this image
No alt text provided for this image

This part we already discussed. Now let's see further...

Image Preprocessing :

Preprocessing refers to all the transformations on the raw data before it is fed to the machine learning or deep learning algorithm. The preprocessing is also important to speed up training (for instance, centering and scaling techniques).

I take a dataset of Cat and Dogs. You can go to kaggle and download the dataset from there...

So let's take a look towards preprocessing code which you are also getting from Keras official site...

No alt text provided for this image
  • Flow from directory method: This method will identify classes automatically from the folder name. For this method, arguments to be used are directory value: The path to parent directory containing sub-directories(class/label) with images. The above method is flow_from_directory method.
  • First we import ImageDataGenerator function provided by the sklearn_preprocessing module. The Keras deep learning neural network library provides the capability to fit models using image data augmentation via the ImageDataGenerator class. Image data augmentation is used to expand the training dataset in order to improve the performance and ability of the model to generalize
  • Rescale: rescaling factor. Defaults to None. If None or 0, no rescaling is applied, otherwise we multiply the data by the value provided (after applying all other transformations).
  • shear_range: Float. Shear Intensity (Shear angle in counter-clockwise direction in degrees)
  • horizontal_flip: Boolean value. Randomly flip inputs horizontally.
  • validation_data: In CNN we also give our testing dataset to the model at fit time using this argument. By this way, you can also check the accuracy of your training set at the model fitting time.
  • I took 25 epochs in model.fit function with 8000 steps_per_epoch. These all things are hyperparameter so you can give what you want.

Now you are ready to train your model...

Now it is also good to save your model after fitting so that you can directly load the dataset where you want... for this...

model.save("model_name.h5")

For importing this Keras have also one more function...

from keras.models import load_model
model = load_model('model_name.h5')

Now if we have some images and want to detect that...

  • Keras has a function image by which you can directly load the image. This function uses pillow module behind the scene that's why we also need to install pillow module.
No alt text provided for this image
  • Now for giving this image to predict, we have to change this in numpy_array first...
No alt text provided for this image
  • Now let's try to predict...
No alt text provided for this image

You can see here that it gives error... This is because Keras take a 4D image and we have a three-dimension image. So for handle this error we have to expand the dimension of the image...

No alt text provided for this image
  • Now finally our result is shown...We have one more function which we used to check that which digit belongs to which class.
No alt text provided for this image

You can match from here that the output is accurate or not.

By changing the number of convolutional layers and pooling layers and other different parameters, try to find the best accuracy.

Some predefined CNN networks:

We have many predefined CNN architecture like LeNet, AlexNet, VGG, GoogLeNet, ResNet, and many more… You can go and check that these models use the same way to build their architecture.

You can make your own by just try again and again by doing some modification in architecture like adding some convolve layer or pooling layer or many other hyperparameters...

Hope we meet soon...

Happy Learning :)


To view or add a comment, sign in

More articles by Gaurav Gupta

Others also viewed

Explore content categories