Convolutional Neural Network (CNN) A convolutional neural network Let's experiment to identify the name from the image of the flower!
This time, we will put the program on the docker environment.
Macbook pro 13 2016
You can easily install docker as a binary from the following site.
https://docs.docker.com/docker-for-mac/
You can install compiled binaries packed with machine learning packages Build an environment with conda (Miniconda).
$ docker pull continuumio/miniconda3
$ 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 -t conda:init .
$ docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
conda                    init                daf923e29da8        2 hours ago         1.55 GB
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
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.

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)
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])
Tested with cucumber flowers.

$ 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.
I will also give you how to make a chatbot with learning when you have time.
Recommended Posts