[PYTHON] I tried using the trained model VGG16 of the deep learning library Keras

I tried to make my own model for studying deep learning, but unfortunately it didn't work. I managed to do it just by using the trained model, so I will make a note of the knowledge I learned at that time.

program

The program created this time is described below. This program identifies what the given image is from among 1000 types.

vgg16.py


import sys
import numpy as np
from keras.preprocessing.image import load_img,img_to_array 
from keras.applications.vgg16 import VGG16,preprocess_input,decode_predictions

image_data = load_img(sys.argv[1],target_size=(224,224))
array_data = img_to_array(image_data)
array_data = np.array([array_data])
array_data = preprocess_input(array_data)
model = VGG16()
results = model.predict(array_data)
print(decode_predictions(results))

Program description

Keras has a function to download the trained model, so this time we will use it to download and use the trained model VGG16.

The image is loaded in the lower part. Since the image size that can be imported into VGG16 is only 224 x 224, it is resized when it is read.

python


image_data = load_img(sys.argv[1],target_size=(224,224))

Since the data format supported by VGG16 is RGB, jpeg images cannot be imported as they are. So I use img_to_array to convert the image to an RGB array (224,224,3). In addition, Keras's identification program predict does not identify one by one, but identifies multiple sheets at once, so the input data format is an array of four layers. This time, since it is a specification that identifies only one sheet, it is converted to 4 layers using np.array.

python


array_data = img_to_array(image_data)
array_data = np.array([array_data])      #(224,224,3) -> (1,224,224,3)

The image is identified in the lower part. preprocess_input is preprocessing to put it in VGG16. The trained model is read by VGG16 () and identified by predict.

python


array_data = preprocess_input(array_data)
model = VGG16()
results = model.predict(array_data)

The result identified by predict is just a list of numbers, so even if you look at this, you cannot tell what it was identified by. Therefore, the screen is output after converting to characters or sorting in order of high probability.

python


print(decode_predictions(results))

Usage and execution results

You can execute it with the command below. (It takes a considerable amount of time to load the trained model.)

python


$ python3 vgg16.py dog.jpg     #dog.Identify the image jpg
...
...
[[('n02113023', 'Pembroke', 0.41220817), ('n02115641', 'dingo', 0.29989296), ('n02113186', 'Cardigan', 0.06258105), ('n02085620', 'Chihuahua', 0.048699833), ('n02094258', 'Norwich_terrier', 0.03338512)]]

The result is output in list format. In the above example, there is a 41% chance that it will be identified as pembroke. Up to 5 candidates are available in descending order of probability.

at the end

I wanted to do real-time image identification on the Raspberry Pi, but I haven't succeeded because Keras hasn't been installed on the Raspberry Pi. I will do my best without giving up. In addition, I will continue to study so that I can prepare training data and train the model by myself.

Recommended Posts

I tried using the trained model VGG16 of the deep learning library Keras
I tried the common story of using Deep Learning to predict the Nikkei 225
I tried the common story of predicting the Nikkei 225 using deep learning (backtest)
I tried deep learning using Theano
I tried refactoring the CNN model of TensorFlow using TF-Slim
I tried face recognition of the laughter problem using Keras.
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
I tried calling the prediction API of the machine learning model from WordPress
I tried deep learning
I tried hosting Pytorch's deep learning model using TorchServe on Amazon SageMaker
I tried to visualize the model with the low-code machine learning library "PyCaret"
I tried using the image filter of OpenCV
I tried using the functional programming library toolz
I implemented the VGG16 model in Keras and tried to identify CIFAR10
[Python] I tried to judge the member image of the idol group using Keras
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I tried running an object detection tutorial using the latest deep learning algorithm
Count the number of parameters in the deep learning model
I tried using the API of the salmon data project
[MNIST] I tried Fine Tuning using the ImageNet model.
PyTorch Learning Note 2 (I tried using a pre-trained model)
I tried to compress the image using machine learning
I tried the changefinder library!
I tried to understand the learning function of neural networks carefully without using a machine learning library (first half).
I tried using the Python library from Ruby with PyCall
Sine wave prediction using RNN in deep learning library Keras
I made a VGG16 model using TensorFlow (on the way)
[TF] I tried to visualize the learning result using Tensorboard
[Machine learning] I tried to summarize the theory of Adaboost
[Python] I tried collecting data using the API of wikipedia
I tried handwriting recognition of runes with CNN using Keras
I tried to divide with a deep learning language model
Deep Learning Model Lightening Library Distiller
I tried using GrabCut of OpenCV
Diversion of layers of trained keras model
I tried reinforcement learning using PyBrain
I tried to predict the infection of new pneumonia using the SIR model: ☓ Wuhan edition ○ Hubei edition
I tried using the checkio API
[Pokemon Sword Shield] I tried to visualize the judgment basis of deep learning using the three family classification as an example.
I tried to get the index of the list using the enumerate function
I tried to make deep learning scalable with Spark × Keras × Docker
I looked at the meta information of BigQuery & tried using it
[Anomaly detection] Try using the latest method of deep distance learning
I tried the asynchronous server of Django 3.0
Image recognition model using deep learning in 2016
[Kaggle] I tried ensemble learning using LightGBM
Visualize the effects of deep learning / regularization
Othello-From the tic-tac-toe of "Implementation Deep Learning" (2)
I tried using the BigQuery Storage API
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to get the batting results of Hachinai using image processing
I tried to extract and illustrate the stage of the story using COTOHA
I made a GAN with Keras, so I made a video of the learning process.
Using COTOHA, I tried to follow the emotional course of Run, Melos!
Deep Learning beginners tried weather forecasting from meteorological satellite images using Keras
I tried to predict the behavior of the new coronavirus with the SEIR model.
I tried to train the RWA (Recurrent Weighted Average) model in Keras
I tried to predict the deterioration of the lithium ion battery using the Qore SDK
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
I tried using scrapy for the first time
I tried the pivot table function of pandas