深度学习-推荐系统-CV-NLPPython机器学习机器学习与数据挖掘

基于keras2.0 + tensorflow1.2 + GP

2018-02-16  本文已影响407人  梅花鹿数据

一: 前言

这次是利用的keras 2.08 版本配合tensorflow 1.2 + GPU 进行的CNN 猫狗二分类识别训练(根据相关demo学习修改),keras的2.x和之前的1.x版本有很多不同,查看更新 所以之前的很多代码需要修改才可以在keras2.x上使用。

二:运行环境

建议使用anaconda 科学环境包,安装tensorflow和tensorflow-gpu的时候会给安装相关模块,十分方便。另外我用的是 Nvidia 显卡,所以可以使用gpu加速训练,是使用cpu训练速度10倍以上。

三:代码分析

数据结构

data/
    train/
        dogs/
            dog001.jpg
            ...
        cats/
            cat001/jpg
            ...
    validation/
        dogs/
            dog001.jpg
            ...
        cats/
            cat001/jpg
            ...

训练模块

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64,(3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# the model so far outputs 3D feature maps (height, width, features)

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
test_datagen = ImageDataGenerator(rescale=1. / 255)

# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
    'data/train',  # this is the target directory
    target_size=(150, 150),  # all images will be resized to 150x150
    batch_size=32,
    class_mode='binary')  # since we use binary_crossentropy loss, we need binary labels

# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
    'data/validation',
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary')

model.fit_generator(
        train_generator,
        steps_per_epoch=2000,
        epochs=30,
        validation_data=validation_generator,
        validation_steps=800)
json_string = model.to_json()
model.save('cat_dog_model_30*2000.h5')
model.save_weights('cat_dog_weights_30*2000.h5')
# loss是训练集损失值. acc是训练集准确率。val_loss是测试集上的损失值,val_acc是测试集上的准确率。
上一篇 下一篇

猜你喜欢

热点阅读