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.
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))
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))
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.
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