[Practical exercise] machine learning to build a series of 03-keras neural network to identify cats and dogs

Previous describes the installation and set up keras linear regression neural network, we try to build this neural network recognition of cats and dogs.

Download images of cats and dogs in advance kaggle data collection, download links: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data

Inside with test1.zip and train.zip archive, which is used to train inside the picture of the model train, and test inside the picture is used to test the model.

Which train the picture, the picture has played a label, such as a cat is cat.0.jpg, the dog is dog.0.jgp, in order to make the neural network have supervised learning.

Train.zip first extract to c: \ data \ train \ directory, the note windows \ is not enough, paths / or \\ FIG.


1, image type and quantity statistics

import os
train='C:\\data\\train\\'
dogs=[train+i for i in os.listdir(train) if 'dog' in i]
cats=[train+i for i in os.listdir(train) if 'cat' in i]
print(len(dogs),len(cats))

12500 12500

# Statistical c: \ data \ train \ below the number of pictures, complete statistics found that dogs have 12,500, cats have 12500. Save the code as a first step py file named data_process for easy reference in the second step. (If jupyter notebook using anaconda installation, then put the current user directory to c: \ users \ xxx \)

2, sorting pictures

Automatically create a file called mini_trainningdata folder automatically from the data \ train \ inside selected 1000 and 1000 dog cat pictures, respectively, into c: \ mini_trainningdata \ train \ dogs and cats inside, and then select 500 cats, 500 dog into the c: \ mini_trainningdata \ test \ dogs and cat inside.

import os
def createDir(path):
    if not os.path.exists(path):
        try:
            os.makedirs(path)
        except:
            print("创建文件夹失败")
            exit(1)
path="C:/min_trainingdata/"
createDir(path+"train/dogs")
createDir(path+"train/cats")
createDir(path+"test/dogs")
createDir(path+"test/cats")
import data_process as dp
import shutil
for dog,cat in list(zip(dp.dogs,dp.cats))[:1000]:
    shutil.copyfile(dog,path+"train/dogs/"+os.path.basename(dog))
    print(os.path.basename(dog)+"操作成功")
    shutil.copyfile(cat, path + "train/cats/" + os.path.basename(cat))
    print(os.path.basename(cat) + "操作成功")
for dog, cat in list(zip(dp.dogs, dp.cats))[1000:1500]:
    shutil.copyfile(dog, path + "test/dogs/" + os.path.basename(dog))
    print(os.path.basename(dog) + "操作成功")
    shutil.copyfile(cat, path + "test/cats/" + os.path.basename(cat))
    print(os.path.basename(cat) + "操作成功")

3, data enhancement

Save the following code to c: /users/xxx/catvsdogs/morph.py

from keras.preprocessing.image import ImageDataGenerator
train_dir="c:/mini_trainningdata/train/"
test_dir="c:/mini_trainningdata/test/"
train_pic_gen=ImageDataGenerator(rescale=1./255,rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,
                                 shear_range=0.2,zoom_range=0.5,horizontal_flip=True,fill_mode='nearest')
test_pic_gen=ImageDataGenerator(rescale=1./255)
train_flow=train_pic_gen.flow_from_directory(train_dir,(128,128),batch_size=32,class_mode='binary')
test_flow=test_pic_gen.flow_from_directory(test_dir,(128,128),batch_size=32,class_mode='binary')
# print(train_flow.class_indices)

4, construction and training model

from keras.models import Sequential
from keras.layers import Convolution2D,MaxPool2D,Flatten,Dense,Dropout
from keras.callbacks import TensorBoard
model=Sequential([
    Convolution2D(32,3,3,input_shape=(128,128,3),activation='relu'),
    MaxPool2D(pool_size=(2,2)),
    Convolution2D(64,3,3,input_shape=(128,128,3),activation='relu'),
    MaxPool2D(pool_size=(2,2)),
    Flatten(),
    Dense(64,activation='relu'),
    Dropout(0.5),
    Dense(1,activation='sigmoid')
])
model.summary()
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
import catvsdogs.morph as morph # 1 increases above reference data codes 
model.fit_generator (
    morph.train_flow,steps_per_epoch=100,epochs=50,verbose=1,validation_data=morph.test_flow,validation_steps=100,
    callbacks=[TensorBoard(log_dir='./logs/1')]
)
model.save('c:/mini_trainningdata/catdogs_model.h5')

After you do, you will find the model has been launched and training.

5, reload and continue training

from keras.callbacks import TensorBoard
from keras.models import load_model
model=load_model('c:/mini_trainningdata/catdogs_model.h5')
model.summary()
import catvsdogs.morph as morph
model.fit_generator(
    morph.train_flow,steps_per_epoch=100,epochs=50,verbose=1,validation_data=morph.test_flow,validation_steps=100,
    callbacks=[TensorBoard(log_dir='./logs/2')]
)
model.save('c:/mini_trainningdata/catdogs_model.h5')

6, test images using custom

Images can be modified to be tested under your own path and the path pre_x = get_inputs inside.

We pic0 ~ 2 adopt the cat, pic3 ~ 5 using dogs, pic6 crocodile, pic7 monkey, pic8 Iron Man, were entered for testing.

004.png

Under the Code need to install opencv-python

pip install opencv-python
from keras import models
import numpy as np
import cv2
def get_inputs(src=[]):
    pre_x = []
    for s in src:
        input = cv2.imread(s)
        input = cv2.resize(input, (128, 128))
        input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB)
        pre_x.append(input)  # input一张图片
    pre_x = np.array(pre_x) / 255.0
    return pre_x
def put_prey(pre_y,label):
    output=[]
    for y in pre_y:
        if y[0]<0.5:#二分类,此处只用一个神经元输出
            output.append([label[0],1-y[0]])
        else:
            output.append([label[1], y[0]])
    return output
model=models.load_model('c:/mini_trainningdata/catdogs_model.h5')
pre_x=get_inputs(['C:\\testdata\\pic0.jpg','C:\\testdata\\pic1.jpg','\\testdata\\pic2.jpg',
                  'C:\\testdata\\pic3.jpg','C:\\testdata\\pic4.jpg','C:\\testdata\\pic5.jpg',
                  'C:\\testdata\\pic6.jpg','C:\\testdata\\pic7.jpg','C:\\testdata\\pic8.jpg',
                 ])
pre_y=model.predict(pre_x)
import catvsdogs.morph as mp
output=put_prey(pre_y,list(mp.train_flow.class_indices.keys()))
print(output)

003.png

Looks like accuracy rate is not very high, just before three in the cat 2, 3 dogs and then only in the one, the last three are not sure there is no way in.

Guess you like

Origin blog.51cto.com/14423403/2419998