[1 copy per day] Classify_images_Using_Python & Machine Learning [Daily_Coding_003]

at first

――This article is a memorandum article for elementary school students who are self-taught in python, machine learning, etc. ――It will be extremely simple, "study while copying the code that you are interested in". ――We would appreciate your constructive comments (LGTM & stock if you like it).

Theme: Classify_images_Using_Python & Machine Learning

Today's topic is a video on Youtube called Classify_images_Using_Python & Machine Learning. It is like learning images of dogs and cats and judging them.

Classify Images Using Python & Machine Learning

The analysis used Google Colaboratry as shown in the youtube video.

Then I would like to do it.

Step1: Import library ~ Process data

import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from tensorflow.keras import layers
from keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

next

  1. Read data
  2. Data type confirmation
  3. Data shape confirmation
  4. Check some data
  5. Output one image

I will do up to.

#1
from keras.datasets import cifar10
(x_train, x_test), (y_train, y_test) = cifar10.load_data()

#2
print(type(x_train))
print(type(y_train))
print(type(x_test))
print(type(y_test))

#3
print('x_train shape:', x_train.shape)
print('y_train shape:', y_train.shape)
print('x_test shape:', x_test.shape)
print('y_test shape:', y_test.shape)

#4
index = 10
x_train[index]

#5
img = plt.imshow(x_train[index])

deer.png

I can see the image. Next, let's look at the label associated with this image.

print('The image label is:', x_train[index])

If you look at this, you can see that it is labeled 4. Next up, this dataset contains 10 different images.

# Get the image classification
classification = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
# Print the image class
print('The image class is:', classification[y_train[index][0]])

So, it seems that this is an image of a "deer" (even if you often get close to the screen, it's tough ...).

Next, set the explained variable y to ʻone_hot_encoding`, and assign a number of 0 or 1 (1 if correct, 0 otherwise) so that the image label can be put in the Neural Network.

y_train_one_hot = to_categorical(y_train)
y_test_one_hot = to_categorical(y_test)
print(y_train_one_hot)
print('The one hot label is:', y_train_one_hot[index])

Then standardize the dataset (0-1).

x_train = x_train / 255
x_test = x_test / 255
x_train[index]

This is about the end of processing. Next, let's build a CNN model!

Step2: Build a model

model = Sequential()

model.add( Conv2D(32, (5,5), activation='relu', input_shape=(32,32,3)))

model.add(MaxPool2D(pool_size=(2,2)))

model.add( Conv2D(32, (5,5), activation='relu'))

model.add(MaxPool2D(pool_size=(2,2)))

model.add(Flatten())

model.add(Dense(1000, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(500, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(250, activation='relu'))

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

Next, compile the model and train it.


model.compile(loss= 'categorical_crossentropy',
              optimizer= 'adam',
              metrics= ['accuracy'])


hist = model.fit(x_train, y_train_one_hot,
                 batch_size = 256,
                 epochs = 10,
                 validation_split = 0.2)

Test the completed model with test data.

model.evaluate(x_test, y_test_one_hot)[1]
>> 0.6811000108718872

Well, it was the same in the video, but the result is not so good. .. ..

For the time being, draw the accuracy and loss error with matplotlib.

# Visualize the model accuracy
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.show()
#Visualize the models loss
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper right')
plt.show()

Finally, let's make a prediction with a model using an appropriate image (this time the image of a cat).

# Test the model with an example
from google.colab import files
uploaded = files.upload()

# show the image
new_image = plt.imread('cat-xxxxx.jpeg')
img = plt.imshow(new_image)

# Resize the image
from skimage.transform import resize
resized_image = resize(new_image, (32,32,3))
img = plt.imshow(resized_image)

# Get the models predictions
predictions = model.predict(np.array([resized_image]))
# Show the predictions
predictions

# Sort the predictions from least to greatest
list_index = [0,1,2,3,4,5,6,7,8,9]
x = predictions

for i in range(10):
  for j in range(10):
    if x[0][list_index[i]] > x[0][list_index[j]]:
      temp = list_index[i]
      list_index[i] = list_index[j]
      list_index[j] = temp

# Show the sorted labels in order
print(list_index)

# Print the first 5 predictions
for i in range(5):
  print(classification[list_index[i]], ':', round(predictions[0][list_index[i]] * 100, 2), '%')

Result is,

cat : 51.09 % dog : 48.73 % deer : 0.06 % bird : 0.04 % frog : 0.04 %

The accuracy of the model was not good and the image used was not good because it was almost impossible to judge whether it was a cat or a dog (laughs).

Finally

This time, I studied image judgment using tensorflow and keras. Of course, I'm aware that it's not accurate enough to be used, but I think it was a good stepping stone.

Thank you for your continued support.

(Learning so far)

  1. [1 copy per day] Predict employee attrition [Daily_Coding_001]
  2. [1 copy per day] Build a Stock Prediction Program [Daily_Coding_002]

Recommended Posts

[1 copy per day] Classify_images_Using_Python & Machine Learning [Daily_Coding_003]
[1 copy per day] Classify_images_Using_Python & Machine Learning [Daily_Coding_003]
[1 copy per day] Build a Stock Prediction Program [Daily_Coding_002]
[1 copy per day] Build a Stock Prediction Program [Daily_Coding_002]
Machine learning
Python learning day 4
[Memo] Machine learning
Machine learning classification
Machine Learning sample
Machine learning tutorial summary
About machine learning overfitting
Machine learning ⑤ AdaBoost Summary
Machine Learning: Supervised --AdaBoost
Machine learning logistic regression
Learning record 4 (8th day)
Learning record 3 (7th day)
Machine learning support vector machine
Learning record 5 (9th day)
Learning record 6 (10th day)
Programming learning record day 2
Learning record 8 (12th day)
Learning record 1 (4th day)
Learning record 7 (11th day)
Studying Machine Learning ~ matplotlib ~
Machine learning linear regression
Machine learning course memo
Machine learning library dlib
Machine learning (TensorFlow) + Lotto 6
Somehow learn machine learning
Learning record 2 (6th day)
Learning record 16 (20th day)
Learning record 22 (26th day)
Machine learning library Shogun
Machine learning rabbit challenge
Introduction to machine learning
Machine Learning: k-Nearest Neighbors
What is machine learning?