Keras 利用vgg16进行猫狗识别

导入模块

import os
import numpy as np
import tensorflow as tf
import random
import seaborn as sns
import matplotlib.pyplot as plt

from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten, Input
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import RMSprop, Adam, SGD
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16, preprocess_input

from sklearn.model_selection import train_test_split

从目标文件夹读取图片函数,利用keras封装好的读图操作,会进行自动插值

def read_and_process_image(data_dir,width=64, height=64, channels=3, preprocess=False):
    train_images= [data_dir + i for i in os.listdir(data_dir)]
    
    random.shuffle(train_images)
    
    def read_image(file_path, preprocess):
        img = image.load_img(file_path, target_size=(height, width))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        # if preprocess:
            # x = preprocess_input(x)
        return x
    
    def prep_data(images, proprocess):
        count = len(images)
        data = np.ndarray((count, height, width, channels), dtype = np.float32)
        
        for i, image_file in enumerate(images):
            image = read_image(image_file, preprocess)
            data[i] = image
        
        return data
    
    def read_labels(file_path):
        labels = []
        for i in file_path:
            label = 1 if 'dog' in i else 0
            labels.append(label)
        
        return labels
    
    X = prep_data(train_images, preprocess)
    labels = read_labels(train_images)
    
    assert X.shape[0] == len(labels)
    
    print("Train shape: {}".format(X.shape))
    
    return X, labels

            
        

读取图片

# 读取图片
WIDTH = 224
HEIGHT = 224
CHANNELS = 3
X, y = read_and_process_image('D:\\Python_Project\\train\\',width=WIDTH, height=HEIGHT, channels=CHANNELS)

查看数据y的信息

# 统计y
sns.countplot(y)

显示图片

# 显示图片
def show_cats_and_dogs(X, idx):
    plt.figure(figsize=(10,5), frameon=True)
    img = X[idx,:,:,::-1]
    img = img/255
    plt.imshow(img)
    plt.show()

for idx in range(0,3):
    show_cats_and_dogs(X, idx)

分割训练集和测试集

train_X = X[0:17500,:,:,:]
train_y = y[0:17500]
test_X = X[17500:25000,:,:,:]
test_y = y[17500:25000]
train_X.shape
test_X.shape

在vgg16模型中加入两层神经网络输出

def vgg16_model(input_shape= (HEIGHT,WIDTH,CHANNELS)):
    vgg16 = VGG16(include_top=False, weights='imagenet',input_shape=input_shape)
    
    for layer in vgg16.layers:
        layer.trainable = False
    last = vgg16.output
    # 后面加入自己的模型
    x = Flatten()(last)
    x = Dense(256, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(256, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(1, activation='sigmoid')(x)
    
    model = Model(inputs=vgg16.input, outputs=x)
    
    return model

创建模型并编译

model_vgg16 = vgg16_model()
model_vgg16.summary()
model_vgg16.compile(loss='binary_crossentropy',optimizer = Adam(0.0001), metrics = ['accuracy'])

开始训练模型

# 训练模型
history = model_vgg16.fit(train_X,train_y, validation_data=(test_X, test_y),epochs=5,batch_size=128,verbose=True)
score = model_vgg16.evaluate(test_X, test_y, verbose=0)
print("Large CNN Error: %.2f%%" %(100-score[1]*100))

 

你可能感兴趣的:(算法)