Tensorflow and Keras

Machine Learning Using Mobile Camera Pictures – Simplest Tutorial

Custom Machine Learning model from pictures taken from mobile camera has been never so easy without tensorflow and keras.

In this tutorial, I am making a Dense Neural Network using Tensorflow and Keras with custom pictures dataset.

I focused on learning machine learning concept and implementations with available tools, in this tutorial instead of achieving high efficiency. For an ease of understanding lets divide this tutorial in 4 steps:

Gathering Data

This is first step of machine learning process. In this project I am going to gather data from my kitchen by taking photographs with mobile phone. I have decided to take pictures of tumblers and dish-plates. So, I have two categories of data here:

    • 1. Tumblers
    • 2. Dish-plates

Lets take 5 photographs of each with a plain background. Data Scientists always recommend more samples than we are taking. (Each picture in this case has been considered a sample of data).
Now transfer this data to your computer where you are going to write your machine learning program.

Preparing Data

Before using this data in our machine learning program we need to work on it little bit. In this work we will resize the images and make all the images of same size, so that it will be easy to feed them into our machine learning models. Its also good for reducing the size of images, as an image taken with mobile camera has size more than 1028×800 and we are going to use only 340×340.

I am using Image Mac Image Preview for this task. Selecting all the images at same time and resizing them from tool option in menu.

Batch Image resize mac- khalsa labs

Once our images are resized to same size, 340×340. We are good to go forward for writing our machine learning program

Programming Neural Network (Designing Model)

For start programming in tensorflow, Lets import the libraries that are essential for a basic machine learning program. I will explain them one by one in another brief tutorial.

from __future__ import absolute_import, division, print_function, unicode_literals
# for MacOS multithreading
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
from keras.preprocessing.image import load_img, img_to_array

now tell the program, where we kept our images.

DATA_DIR = "ml-data/"

all_pictures = os.listdir(DATA_DIR)
if all_pictures.__contains__('DS_Store'):
    all_pictures.remove('.DS_Store')

Machine learning models treat image like arrays. For this reason, we need to convert our pictures data to arrays. Python has great library to do so, it is called numpy  that we already imported at the beginning of our tutorial.
Steps to convert Images to Numpy Array :

1. Initialize the numpy dataset

i = 0
for pic_name in all_pictures: 
   image = load_img(DATA_DIR + pic_name,color_mode="grayscale") #color_mode="grayscale"
   image = img_to_array(image)
   image = image/255
   dataset[i] = image
   i += 1

Lets define a label for each image, in this case I have arranged 5 pictures of tumblers and then 4 pictures of dish-plates in my image folder by naming then number wise.

Defined labels; 1 for each tumbler and 0 for dish-plates

labels = np.array((1,1,1,1,1,0,0,0,0),dtype='uint8')

Lets define our model now

# step 1 - initiate mode
model = keras.Sequential([
keras.layers.Flatten(input_shape=(340,340,1)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(2, activation='softmax')
])


# step 2 - compile model, set loss functionn 
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# step 3 - train model
model.fit(dataset, labels, epochs=10)

In above code, we used a model with with 3 layers. First is input layer (Flatten), Second is our hidden layer with relu and third is output layer with softmax activation.
[Remember this easy way; use softmax activation for output layers]

In step2, we compile model with adam optimizer, used it as common optimizer for image models.

In step3, we feed our data to the model we constructed by model.fit method. Image dataset and labels. Epoch in this function can be described as the number of times we want to feed this data to our model.

Step 4 [Final Step]: Predicting Results of Learning Model

 

x = model.predict(dataset, batch_size=9)

def check_photo(x, i):
   plt.imshow(dataset[i].reshape(340,340))
   print(x[i][0])
   if x[i][0]>=1:
       print("It is a Dishplate")
   else:
       print("It is a glass")

check_photo(x,4)   #it will tell us the output predicted by computer

Hurray !! We finished a machine learning program with neural network with our house hold items data. You can download complete source code here with image samples.

https://github.com/singh47/MulticlassClassification-Kitchen

Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe

Subscribe to Khalsa Labs