[DOCKER] Identify the name from the flower image with keras (tensorflow)

Convolutional Neural Network (CNN) A convolutional neural network Let's experiment to identify the name from the image of the flower!

Introduction

This time, we will put the program on the docker environment.

    1. Environmental preparation
  1. program
    1. Experiment   Place the program in Git.

Environmental preparation

Development environment

Macbook pro 13 2016

install docker

You can easily install docker as a binary from the following site.

https://docs.docker.com/docker-for-mac/

Build a machine learning environment from a Miniconda image

You can install compiled binaries packed with machine learning packages Build an environment with conda (Miniconda).

① Download template

$ docker pull continuumio/miniconda3

② Create Dockerfile

$ vi Dockerfile
  :
$ cat Dockerfile
FROM continuumio/miniconda3
  RUN apt-get update
  RUN pip install --upgrade pip
  RUN pip install tensorflow \
                  pandas \
                  scipy \
                  Pillow \
                  keras
  RUN pip install -U scikit-learn
  RUN mkdir /home/src/

③ docker build (image creation)

$ docker build -t conda:init .

④ Confirmation

$ docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
conda                    init                daf923e29da8        2 hours ago         1.55 GB

⑤ Start docker

This time, mount the folder to be developed on docker.

# [mount dir]Please rewrite and use.
$ docker run -i -t -v [mount dir]:/home/src conda:init /bin/bash

You can exit by pressing "Ctrl + p" or "Ctrl + q".

When accessing the container again

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
745860b72c67        conda:init         "/usr/bin/tini -- ..."   11 hours ago        Up 11 hours                             friendly_galileo

#Specify CONTAINER ID
$ docker attach 745860b72c67

This completes the environment preparation

Flower image classification program from here

① Preparation of training data

Collect flower images from the internet! Give the name of the flower to the directory name as shown in the capture below, and place the image of the flower in it.

キャプチャ

② Creation of learning model

Prepare a program to save an array of image data and categories.

data_create.py


from PIL import Image
import sys
from sklearn import cross_validation
import os, glob
import numpy as np
import random, math

#
#Generate training data
#
class DataCreate : 
  def __init__(self, script_name):
    Image.LOAD_TRUNCATED_IMAGES = True
  
  def create(self) :
    input_dir = "images"
    categorys = []
    
    dir_list = os.listdir(input_dir)
    for index, dir_name in enumerate(dir_list):
      if dir_name == '.DS_Store' :
        continue
      categorys.append(dir_name)
    image_size = 50
    train_data = [] #image data,Label data
    for idx, category in enumerate(categorys): 
      try :
        print("---", category)
        image_dir = input_dir + "/" + category
        files = glob.glob(image_dir + "/*.jpg ")
        for i, f in enumerate(files):
          img = Image.open(f)
          img = img.convert("RGB")
          img = img.resize((image_size, image_size))
          data = np.asarray(img)
          train_data.append([data, idx])

      except:
        print("SKIP : " + category)

    #Shuffle data
    random.shuffle(train_data)
    X, Y = [],[]
    for data in train_data: 
      X.append(data[0])
      Y.append(data[1])

    test_idx = math.floor(len(X) * 0.8)
    xy = (np.array(X[0:test_idx]), np.array(X[test_idx:]), 
          np.array(Y[0:test_idx]), np.array(Y[test_idx:]))
    np.save("./npy/flower", xy)
  

if __name__ == "__main__":
  args = sys.argv
  datacreate = DataCreate(args[0])
  datacreate.create()


Next, prepare a program to create a learning model.

train.py


import sys
import os
import numpy as np
import pandas as pd
import gc
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.utils import np_utils

#
#Generate model
#
class TrainModel : 
  def __init__(self):
    input_dir = 'images'
    self.nb_classes = len([name for name in os.listdir(input_dir) if name != ".DS_Store"])
    x_train, x_test, y_train, y_test = np.load("./npy/flower.npy")
    #Normalize the data
    self.x_train = x_train.astype("float") / 256
    self.x_test = x_test.astype("float") / 256
    self.y_train = np_utils.to_categorical(y_train, self.nb_classes)
    self.y_test = np_utils.to_categorical(y_test, self.nb_classes)

  def train(self, input=None) :
    model = Sequential()
    # K=32, M=3, H=3
    if input == None :
      model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=self.x_train.shape[1:]))
    else :
      model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=input))

    # K=64, M=3, H=3 (adjustment)
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Convolution2D(64, 3, 3, border_mode='same'))
    model.add(Activation('relu'))
    # K=64, M=3, H=3 (adjustment)
    model.add(Convolution2D(64, 3, 3))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten()) 
    model.add(Dense(512))
    # biases  nb_classes
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(self.nb_classes))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    if input == None :
      #Learn and save the model
      model.fit(self.x_train, self.y_train, batch_size=32, nb_epoch=10)
      hdf5_file = "./model/flower-model.hdf5"
      model.save_weights(hdf5_file)

      #model test
      score = model.evaluate(self.x_test, self.y_test)
      print('loss=', score[0])
      print('accuracy=', score[1])
    return model

if __name__ == "__main__":
  args = sys.argv
  train = TrainModel()
  train.train()
  gc.collect()

Learning is now complete.

Then try running it.

#Arrange images
$ python data_create.py
---Ai (Tadeai)
---Aogiri
---Aomoria thistle (Okino thistle)
---Agapanthus (Murasaki Clivia)
---Akaza
---Akashouma
---Akabana buckwheat
---Akabana Mitsumata
---Rehmannia glutinosa
---Acanthus(Bear's breeches)
---Aginashi
---Akizaki snowflake
---Akino Unagitsukami
---Salvia japonica (Salvia japonica)
---Akinonogeshi
---Akinowasu Regusa
   :
   :

#Learning
$ python train.py 
  :
loss= 0.0410282239616
accuracy= 0.992773946126

The hit rate of 99% is buggy (laughs)

③ Model test for the time being

Prepare a simple program for model testing.

check.py


import train as train
import sys, os
from PIL import Image
import numpy as np
import pandas as pd

if len(sys.argv) <= 1:
  quit()

image_size = 50
input_dir = 'images'
categories = [name for name in os.listdir(input_dir) if name != ".DS_Store"]

X = []
for file_name in sys.argv[1:]:
  img = Image.open(file_name)
  img = img.convert("RGB")
  img = img.resize((image_size, image_size))
  in_data = np.asarray(img)
  X.append(in_data)

X = np.array(X)

model = train.TrainModel().train(X.shape[1:])
model.load_weights("./model/flower-model.hdf5")

predict = model.predict(X)

for pre in predict:
  y = pre.argmax()
  print("Flower name: ", categories[y])

Test run

Tested with cucumber flowers.

flower.jpg

$ python check.py test/flower.jpg
Using TensorFlow backend.
Flower name:cucumber

Cucumber is successful! But there were times when other photos didn't work. I will study a little more and give it again.

Finally

I will also give you how to make a chatbot with learning when you have time.

Recommended Posts

Identify the name from the flower image with keras (tensorflow)
Challenge image classification with TensorFlow2 + Keras CNN 1 ~ Move for the time being ~
Image recognition with keras
Challenge image classification with TensorFlow2 + Keras 3 ~ Visualize MNIST data ~
Remove the frame from the image
Easy image classification with TensorFlow
Image recognition with Keras + OpenCV
2020/02 Python 3.7 + TensorFlow 2.1 + Keras 2.3.1 + YOLOv3 Object detection with the latest version
MNIST (DCNN) with Keras (TensorFlow backend)
Challenge image classification by TensorFlow2 + Keras 4 ~ Let's predict with trained model ~
Challenge image classification by TensorFlow2 + Keras 1-Move for the time being-
[TensorFlow] [Keras] Neural network construction with Keras
Challenge image classification with TensorFlow2 + Keras 9-Learning, saving and loading models-
Gender is determined from the name.
Let's cut the face from the image
Try blurring the image with opencv2
[TensorFlow / Keras] The road to assembling an RNN with your favorite structure
Image display taken with the built-in ISIGHT
Compare raw TensorFlow with tf.contrib.learn and Keras
Run Keras with CNTK backend from CentOS
Image acquisition from camera with Python + OpenCV
Right-click the image → realize "Compress with TinyPNG"
I tried playing with the image with Pillow
When changing the table name with flask_migrate
Understand the images of various matrix operations used in Keras (Tensorflow) with examples
Region extraction method using cellular automaton Try region extraction from the image with growcut (Python)
[Python] I tried the same calculation as LSTM predict with from scratch [Keras]
Try to solve the fizzbuzz problem with Keras
I tried "smoothing" the image with Python + OpenCV
Crop the image to rounded corners with pythonista
I tried "differentiating" the image with Python + OpenCV
(Note) Importing Excel with the same column name
Image processing from scratch with python (5) Fourier transform
Download the image from the text file containing the URL
Exposing the DCGAN model for Cifar 10 with keras
Determine the numbers in the image taken with the webcam
Image processing from scratch with python (4) Contour extraction
Detect folders with the same image in ImageHash
Rewrite the name of the namespaced tag with lxml
I tried "binarizing" the image with Python + OpenCV
Load the TensorFlow model file .pb with readNetFromTensorflow ().
Calculate the angle between n-dimensional vectors with TensorFlow
Display the image after Data Augmentation with Pytorch
Challenge image classification with TensorFlow2 + Keras 6-Try preprocessing and classifying images prepared by yourself-