DEV Community

Cover image for Machine Learning - Going Furthur with CNN Part 1
Sandeep Balachandran
Sandeep Balachandran

Posted on

Machine Learning - Going Furthur with CNN Part 1

Hey there,

Another weekend right?
Lets motivate those who reading this a little bit

A man found a cocoon of butterfly. One day he noticed there is a butterfly struggled to force its body through the small opening of the cocoon. So the man decides to help the butterfly by snipping off the remaining bit of cocoon since the butterfly looked like it was stuck.
So the fly then emerged easily also it had a swollen body and small , shriveled wings. The man was waiting for the butterfly to enlarge its wings and fly.
But the fly was unable to fly and crawling around with tiny wings and swollen body.
Despite the kind heart of the man, he didn’t understand that the restricting cocoon and the struggle needed by the butterfly to get itself through the small opening; were God’s way of forcing fluid from the body of the butterfly into its wings. To prepare itself for flying once it was out of the cocoon.

Moral of the story:

Our struggles in life develop our strengths. Without struggles, we never grow and never get stronger, so it’s important for us to tackle challenges on our own, and not be relying on help from others
Never ever try to crack open a coccoon. Mind your own business

Main Content From Here

So far, we've only looked at small grayscale images such as a 28 by 28 grayscale images from the Fashion-Mnist dataset.

details

In real applications however, we usually have to deal with high resolution color images of different sizes like the ones we see here.

details

As we mentioned earlier in this lesson, we're going to create a CNN that can classify colored images of cats and dogs. To do this, we're going to use cat and dog images from Microsoft's image database. Each image in this dataset is labeled with either a one or a zero. Where a 1 indicates the image corresponds to a dog, and a 0 indicates that the image corresponds to a cat.

details

Even though Microsoft's dataset contains over 3 million labeled images of cats and dogs, only 25,000 of them are publicly available. Training our CNN on this 25,000 images will take a very long time. Therefore, in order to decrease the training time, we will only use a small subset of images to train our CNNs. Our subset contains 2,000 training images and 1,000 validation images.

details

Out of the 2,000 training images, 1,000 of them are dog images and 1,000 of them are cat images. When working with this dataset, we will face two main challenges.

  • Working with images of different sizes
  • Working with color images

So let's start by learning how to work with images of different sizes.



Working with images of different sizes

first challenge will be dealing with images of different sizes. This is because a neural network needs a fixed size input. As an example,

details

We had to flatten the grayscale images from the Fashion MNIST dataset before feeding them into our neural network. We used a flattened layer to transform the 28 by 28 grayscale images from a 2D array to a 1D array of 784 pixels. Since all of the images from the Fashion MNIST dataset have the same size, they all get flattened to 1D arrays of the same size. However, when working with images of different sizes, flattening the images will give rise to one-dimensional arrays of different sizes. Since neural networks need a fixed size input, just flattening the images won't work. When doing image classification, we always solve this problem by resizing all the images to the same size.

details

In this lesson, we'll choose to resize all the images in our dogs and cats dataset to be 150 pixels in height, and 150 pixels in width. By resizing all images to the same size, this will guarantee that when we flatten the images, they'll result in 1D arrays of the same size.

details

Our second challenge will be dealing with color images which we can see in the next one.

Adios . Muchas Gracias

Top comments (0)