机器学习猫狗分类

Task02:基于决策树的分类预测

猫狗分类

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.optimizers import RMSprop

from keras.preprocessing.image import ImageDataGenerator

# 下面的一部分是进行数据预处理
import os
import shutil

dataset_dir = './do_test/test/'
train_cats_dir = './do_test/train/cats/'
train_dogs_dir = './do_test/train/dogs/'
validation_cats_dir = './do_test/validation/cats/'
validation_dogs_dir = './do_test/validation/dogs/'

### 数据搬移,构建训练集验证集
if not os.path.exists(dataset_dir):
    os.makedirs(dataset_dir)
if not os.path.exists(train_cats_dir):
    os.makedirs(train_cats_dir)
if not os.path.exists(train_dogs_dir):
    os.makedirs(train_dogs_dir)
if not os.path.exists(validation_cats_dir):
    os.makedirs(validation_cats_dir)
if not os.path.exists(validation_dogs_dir):
    os.makedirs(validation_dogs_dir)

cat_count = 0
dog_count = 0

cats_dir = os.path.join(dataset_dir,'cat')
dog_dir = os.path.join(dataset_dir,'dog')

#### 将照片文件名都放置一个列表中,后续再遍历
image_list = os.listdir(dog_dir) +os.listdir(cats_dir)
for image in image_list:
    print(image)
    animal = image.split('.')[0]
    if animal == 'cat':
        cat_count += 1
        if cat_count % 5 == 0:  ### 训练集和验证集:80%:20%
            image_path = os.path.join(cats_dir, image)
            shutil.move(image_path, validation_cats_dir) ### 猫的图片迁移到验证集
        else:
            image_path = os.path.join(cats_dir, image)
            shutil.move(image_path, train_cats_dir)  ### 猫的图片迁移到训练集
    if animal == 'dog':
        dog_count += 1
        if dog_count % 5 == 0:
            image_path = os.path.join(dog_dir, image)
            shutil.move(image_path, validation_dogs_dir)
        else:
            image_path = os.path.join(dog_dir, image)
            shutil.move(image_path, train_dogs_dir)
### 装载训练集和验证集
train_dir = './do_test/train/'
validation_dir = './do_test/validation/'

train_datagen = ImageDataGenerator(rescale=1. / 255)
validation_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_dir,  # target directory
    target_size=(150, 150),  # resize图片将图片压缩到150*150大小,加快训练速度
    batch_size=20, ### 每二十张为一组
    class_mode='binary'
)

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

for data_batch, labels_batch in train_generator:
    print('data batch shape:', data_batch.shape)
    print('labels batch shape:', labels_batch.shape)
    break


### 构建神经网络
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu',
                 input_shape=(150, 150, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

### 展示神经网络架构
print(model.summary())

### 设置神经网络损失函数,优化函数,验证指标
model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(lr=1e-4),
              metrics=['acc'])

### 训练模型
hist = model.fit_generator(
    train_generator,
    steps_per_epoch=10,
    epochs=10,  ### 训练训练十次
    validation_data=validation_generator,
    validation_steps=20)

### 存储模型
model.save('cat_and_dog.h5')

你可能感兴趣的:(python,机器学习)