使用VGG-16做kaggle cats vs dogs 图像分类

直接上代码

from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
import keras
import os
from keras import models
from keras import layers
from tensorflow.keras.callbacks import TensorBoard
from keras.applications import VGG16

original_dataset_dir = '/home/ysy/tf_keras_learning/kagglecatsanddogs_3367a (1)'

# Directories for our training,
# validation and test splits
train_dir = os.path.join(original_dataset_dir, 'train')
#os.mkdir(train_dir)
validation_dir = os.path.join(original_dataset_dir, 'validation')
#os.mkdir(validation_dir)
test_dir = os.path.join(original_dataset_dir, 'test')
#os.mkdir(test_dir)

# Directory with our training cat pictures
train_cats_dir = os.path.join(train_dir, 'cats')
#os.mkdir(train_cats_dir)

# Directory with our training dog pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')
#os.mkdir(train_dogs_dir)

# Directory with our validation cat pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')
#os.mkdir(validation_cats_dir)

# Directory with our validation dog pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
#os.mkdir(validation_dogs_dir)

# Directory with our validation cat pictures
test_cats_dir = os.path.join(test_dir, 'cats')
#os.mkdir(test_cats_dir)

# Directory with our validation dog pictures
test_dogs_dir = os.path.join(test_dir, 'dogs')
#os.mkdir(test_dogs_dir)


conv_base = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(150, 150, 3))

NAME = "VGG-16_pretrain_1"
print(NAME)


model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

conv_base.trainable = True

set_trainable = False

print('trainable weights is :',len(model.trainable_weights))

train_datagen = ImageDataGenerator(
      rescale=1./255,
      rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
      train_dir,
      target_size=(150, 150),
      batch_size=20,
      class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        validation_dir,
        target_size=(150, 150),
        batch_size=20,
        class_mode='binary')

tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))

model.compile(loss='binary_crossentropy',
              optimizer=optimizers.RMSprop(lr=2e-5),
              metrics=['acc'])

history = model.fit_generator(
      train_generator,
      steps_per_epoch=100,
      epochs=30,
      validation_data=validation_generator,
      validation_steps=50,
      verbose=2)

model.save('VGG16_pretrain_all.model')

训练结果

Epoch 28/30
 - 29s - loss: 0.0979 - acc: 0.9635 - val_loss: 0.1180 - val_acc: 0.9630
Epoch 29/30
 - 29s - loss: 0.1071 - acc: 0.9585 - val_loss: 0.0908 - val_acc: 0.9675
Epoch 30/30
 - 29s - loss: 0.1029 - acc: 0.9555 - val_loss: 0.1363 - val_acc: 0.9605

测试代码

import sys
import os
from keras.preprocessing.image import ImageDataGenerator

sys.path.remove("/opt/ros/kinetic/lib/python2.7/dist-packages")

import cv2
import tensorflow as tf

original_dataset_dir = '/home/ysy/tf_keras_learning/kagglecatsanddogs_3367a (1)'
test_dir = os.path.join(original_dataset_dir, 'test')

CATEGORIES = ["Dog", "Cat"]


def prepare(filepath):
    IMG_SIZE = 150
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

model = tf.keras.models.load_model("VGG16_pretrain_all.model")

test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=(150, 150),
        batch_size=20,
        class_mode='binary')

test_loss, test_acc = model.evaluate_generator(test_generator, steps=50)

#prediction = model.predict([prepare('/home/ysy/tf_keras_learning/cat2.png')])

print('test acc:', test_acc)

#print(prediction)
#print(CATEGORIES[int(prediction[0][0])])

 

测试结果

Found 2000 images belonging to 2 classes.
test acc: 0.9649999928474426

 

你可能感兴趣的:(图像识别练习)