IMAGE CLASSIFICATION

Introduction cat & dog image classification

Vann Ponlork
Mar 5, 2019 · 7 min read

In this topic, I want to show you the neural network of the classifier in TensorFlow run on CPU. And after that, I will show an introduction on how to save the model, restore, and save the whole model. In addition, I will show you about how to create the dataset for training model, what type and image shape should impose to the dataset. As well as I will introduce about TensorFlow serving what is TensorFlow serving and how to deploy the model to the server as well as send data to predict on the server.

All of this introduction use Python and Pycharm for IDE. For those who underbook of python, you should ready document about python first.

We will use TensorFlow in this introduction. For more detail of TensorFlow you can check on:

https://www.tensorflow.org/tutorials/

Image:

The image shape is thought to in format: [width, height, channel].

Channel mean: color type ex: [28,28,3] the 3 mean RGB.In the training process, you can train every type of image and channel, but I recommend using a grey scale to reduce image size and in training process, it will use less memory it means channel equal 1.

Neural Network:

Image for post
Image for post

In neural network there: are input => hidden layer(neural network) => output

For image the image the famous usage is convolutional neural network(CNN).

Image for post
Image for post

In this case, we slide our window by 1 pixel at a time. In some cases, people slide the windows by more than 1 pixel. This number is called stride.

Pooling Layer:

Pooling layer is mostly used immediately after the convolutional layer to reduce the spatial size(only width and height, not depth). This reduces the number of parameters, hence computation is reduced. Also, less number of parameters avoid overfitting. The most common form of pooling is Max pooling where we take a filter of size F*F and apply the maximum operation over the F*F sized part of the image.

Fully Connected Layer:

If each neuron in a layer receives input from all the neurons in the previous layer, then this layer is called a fully connected layer. The output of this layer is computed by matrix multiplication followed by bias offset.

When designing the architecture of a neural network you have to decide on: How do you arrange layers? which layers to use? how many neurons to use in each layer etc.?

Let’s start:

For training we need to have dataset:

Image: To create a dataset for train model we have to prepare image first. Whatever the size of the image you want, but if you train the image to model 28 x 28 px so when you let the model make a prediction it need 28x28.

In addition, the image has 3 channel it means RGB, so we can reduce its size by changing the number of the channel to 1(grey scale).

The question is can I impose the number of image channel to 3 for training?

The answers: Yes you can, but use much memory of your machine.

Now, I use 28 x 28 and 3 of the channel of image shape.

Dataset:

To train the model you need to have a batch of data, so you need you to have a dataset. For a beginner, you can sue MNIST dataset or you can create dataset by yourself. For MNIST dataset you can try it yourself, but now I want to introduce of creating the dataset.

In the training process there is supervise and unsupervised, so this dataset introduction I will show about supervised.

Supervise it means it has a label to identify image ex. [255,255,….233] ,[0,1]

the first array is image and the second [0,1] is a label.

The question is: if I have the multi-type of images what the form of label is it?

The answer is : label will change to number of label items ex, [0,1,0,0,0] or [1,0,0,0,0]

I have dog and cat images I resize these images to 28x28 in RGB (3 channel). The data format is [image array] [label], after I make random shuffle to mix shuffle of cat and dog image together and save it by saving it to array dataset training_data.npy.

Next step we have to prepare code to train data.

Training data to machine it means train data to the model. One more thing to train image to the model we use a convolutional neural network (CNN) because it catches pixel by pixel to train to model. In this introduction, I use Tensoflow both neural network and CNN. I will create 2 layers of the neural network.

I have created two layers of the neural network and have one output. Let me how to show briefly about the code above:

We use TensorFlow, batch_size is the size of data that will train to model, we have 2 of tf.placeholder(tf.float32) one is image and one is a label.

Note: if we train large data to model it will more accuracy, less of data will have less of accuracy.

But it will use a large of memory too.

So, now I will train the model with 500 images each time (batch_size = 500).

Note: To train data to machine we will use sigmoid function : z = w.x +b. reference : http://neuralnetworksanddeeplearning.com/

Now we start to optimize by reducing cost.

COST is normal have algebra: cost = 0.5(label — prediction)²

optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)

Rate default is 0.01

In this code I use 100 epoch and 2 iteration (my dataset have 1000 images and train 500 each time).

NOTE: Model should train more images and train more epoch to get more accuracy.

_,c,accuracy,y_predict,label_prediction = sess.run([optimizer,cost,accuracy,y_predict,label_prediction],feed_dict={x:batch_x,y:batch_y})

After we have already trained our model to have to get training result to save to model

saver = tf.train.Saver()

saver.save(sess,model_path)

The question is: how to restore the training process from where the epoch stop?

The answer is: we have to restore the process of saving.

if os.path.isfile(model_path + '.index'):
saver.restore(sess,model_path)
print('Model restore')

So, it will restore from where it stops.This saving process just saves the variable is not save the whole model. To deploy the model to the web to predict we have to save the whole model.The format that we will save the whole model is JSON format.

So, Here is it:

This saves the whole model after saving the variable.This thinks this introduction is enough to create a neural network, use CNN for image training, train model, save the model, and restore model.
Now, I want to show you the next step of how to deploy the model to the web to predict.
In this introduction I will use Tensorflow_serving the I will deploy the model.
I recommend using docker for TensorFlow cause it is easy and has already tensorflow_serving.

Reference:
https://www.docker.com/
https://www.tutorialspoint.com/docker/
https://www.vagrantup.com/downloads.html
https://www.virtualbox.org/wiki/Downloads

Prerequisite study:
-vitualbox
-vagrant
-ubuntu16(some comment line)
-git
-docker(image and container)
-NGINX or APACH

Now, I start my vagrant hostname host3 and I run docker container.
First, we need to check the docker repository by

# docker search tensorflow_serving

Image for post
Image for post

Note: In the ML program we need to know our program run on CPU or GPU. In my program, it runs on the CPU. So show check TensorFlow program that uses in CPU.

So, you need to pull it from the repository:
#docker pull docker_container_name:
#docker ps -a
After you push your model to ubuntu host we need to bind it to tensorflow_serving contain.

#docker run -p 8501:8501 — mount type=bind,source=/ImageCmpl/model_store/,target=/models/my_model -e MODEL_NAME=my_model -t tensorflow/serving

After binding the model you need to run docker container that has just bind the model.
#docker start your_docker_container.
#docker ps
So, after you have started your container you can access from a web browser.

http://192.168.33.15:8501/v1/models/classifier

After it will reply back by JSON data.

{
"model_version_status": [
{
"version": "1",
"state": "AVAILABLE",
"status": {
"error_code": "OK",
"error_message": ""
}
}
]
}
Image for post
Image for post

The message like that it means it works. Now you have success deploy model to the tensorflow_serving server.
The next step is to send some image to server let model predict.
To send data to the server you need format data to JSON format.
Prerequisite study:
-HTML
-JAVASCRIPT
-JSON
_AJAX , JQUERY(understand how to transfer data to server by these script)
https://www.w3schools.com/
https://www.tutorialspoint.com/
https://www.google.com/

LET’S START:

Image for post
Image for post

The images that should choose for predict is 28x28 pixels cause data in the dataset is 28x28 pixels.

Reference:

https://www.tensorflow.org/tutorials/
https://www.w3schools.com/
https://www.tutorialspoint.com/
https://www.docker.com/
https://www.tutorialspoint.com/docker/
https://www.vagrantup.com/downloads.html
https://www.virtualbox.org/wiki/Downloads

LASTMILE WORKS / DYNAMO TECH - R&D Project

Developing next-generation technology in Combodia

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store