Keras 猫狗分类
In [1]:
#由于Keras已经与TensorFlow合并,tensorflow下面导入keras
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
import numpy as np
import shutil
import os
2024-12-15 01:26:36.691294: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2024-12-15 01:26:36.707253: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:479] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-12-15 01:26:36.727409: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:10575] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-12-15 01:26:36.727434: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1442] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2024-12-15 01:26:36.741008: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2024-12-15 01:26:37.421106: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
In [2]:
base_dir='/dataset/cat_dog/'
In [3]:
train_dir = os.path.join(base_dir, 'train')
train_dir_dog = os.path.join(train_dir, 'dog')
train_dir_cat = os.path.join(train_dir, 'cat')
test_dir = os.path.join(base_dir, 'test')
test_dir_dog = os.path.join(test_dir, 'dog')
test_dir_cat = os.path.join(test_dir, 'cat')
for d in [train_dir, train_dir_dog, train_dir_dog, test_dir, test_dir_dog, test_dir_cat]:
os.makedirs(d, exist_ok=True)
In [4]:
fnames = ['cat.{}.jpg'.format(i) for i in range(3000)]
for n in fnames:
s = os.path.join(train_dir, n)
d = os.path.join(train_dir_cat, n)
shutil.copyfile(s, d)
In [5]:
fnames = ['dog.{}.jpg'.format(i) for i in range(3000)]
for n in fnames:
s = os.path.join(train_dir, n)
d = os.path.join(train_dir_dog, n)
shutil.copyfile(s, d)
In [6]:
fnames = ['cat.{}.jpg'.format(i) for i in range(3000,4001)]
for n in fnames:
s = os.path.join(train_dir, n)
d = os.path.join(test_dir_cat, n)
shutil.copyfile(s, d)
fnames = ['dog.{}.jpg'.format(i) for i in range(3000,4001)]
for n in fnames:
s = os.path.join(train_dir, n)
d = os.path.join(test_dir_dog, n)
shutil.copyfile(s, d)
In [7]:
#图片生成器
from tensorflow.keras.preprocessing.image import ImageDataGenerator
In [8]:
#归一化
train_g = ImageDataGenerator(rescale=1/255)
test_g = ImageDataGenerator(rescale=1/255)
In [9]:
#target_size 统一处理图片尺寸
#class_mode 二分类
train_gen = train_g.flow_from_directory(train_dir,
target_size=(200,200),
batch_size=300,
class_mode='binary')
Found 6000 images belonging to 2 classes.
In [10]:
test_gen = train_g.flow_from_directory(test_dir,
target_size=(200,200),
batch_size=300,
class_mode='binary')
Found 3003 images belonging to 2 classes.
创建模型¶
In [16]:
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
In [21]:
model = Sequential()
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(200, 200, 3)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPool2D())
model.add(layers.Dropout(0.25))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPool2D())
model.add(layers.Dropout(0.25))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPool2D())
model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.25))
#模型输出2分类0或1
model.add(layers.Dense(1, activation='sigmoid'))
In [22]:
model.summary()
Model: "sequential_2"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ conv2d_12 (Conv2D) │ (None, 198, 198, 64) │ 1,792 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_13 (Conv2D) │ (None, 196, 196, 64) │ 36,928 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ max_pooling2d_6 (MaxPooling2D) │ (None, 98, 98, 64) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dropout_6 (Dropout) │ (None, 98, 98, 64) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_14 (Conv2D) │ (None, 96, 96, 64) │ 36,928 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_15 (Conv2D) │ (None, 94, 94, 64) │ 36,928 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ max_pooling2d_7 (MaxPooling2D) │ (None, 47, 47, 64) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dropout_7 (Dropout) │ (None, 47, 47, 64) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_16 (Conv2D) │ (None, 45, 45, 64) │ 36,928 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv2d_17 (Conv2D) │ (None, 43, 43, 64) │ 36,928 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ max_pooling2d_8 (MaxPooling2D) │ (None, 21, 21, 64) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dropout_8 (Dropout) │ (None, 21, 21, 64) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ flatten_2 (Flatten) │ (None, 28224) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_4 (Dense) │ (None, 128) │ 3,612,800 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dropout_9 (Dropout) │ (None, 128) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_5 (Dense) │ (None, 1) │ 129 │ └─────────────────────────────────┴────────────────────────┴───────────────┘
Total params: 3,799,361 (14.49 MB)
Trainable params: 3,799,361 (14.49 MB)
Non-trainable params: 0 (0.00 B)
编译¶
In [23]:
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
loss='binary_crossentropy',
metrics=['acc'])
训练¶
In [24]:
#steps_per_epoch * batch_size 等于1个epoch
his = model.fit(train_gen,
epochs=10,
steps_per_epoch=20,
validation_data=test_gen,
validation_steps=10)
Epoch 1/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 18s 640ms/step - acc: 0.4933 - loss: 0.6948 - val_acc: 0.4110 - val_loss: 0.6960 Epoch 2/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 20s 1s/step - acc: 0.5426 - loss: 0.6887 - val_acc: 0.6410 - val_loss: 0.6736 Epoch 3/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 18s 893ms/step - acc: 0.5766 - loss: 0.6747 - val_acc: 0.6357 - val_loss: 0.6628 Epoch 4/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 60s 3s/step - acc: 0.5935 - loss: 0.6589 - val_acc: 0.6187 - val_loss: 0.6785 Epoch 5/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 60s 3s/step - acc: 0.5987 - loss: 0.6468 - val_acc: 0.6277 - val_loss: 0.6786 Epoch 6/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 60s 3s/step - acc: 0.6241 - loss: 0.6413 - val_acc: 0.6443 - val_loss: 0.6663 Epoch 7/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 120s 3s/step - acc: 0.6446 - loss: 0.6213 - val_acc: 0.6427 - val_loss: 0.6748 Epoch 8/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 60s 3s/step - acc: 0.6538 - loss: 0.6050 - val_acc: 0.6123 - val_loss: 0.7059 Epoch 9/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 60s 3s/step - acc: 0.6792 - loss: 0.5959 - val_acc: 0.6157 - val_loss: 0.7117 Epoch 10/10 20/20 ━━━━━━━━━━━━━━━━━━━━ 60s 3s/step - acc: 0.6826 - loss: 0.5829 - val_acc: 0.6373 - val_loss: 0.6976
In [25]:
import matplotlib.pylab as plt
%matplotlib inline
In [26]:
plt.plot(his.epoch, his.history.get('loss'), label='loss')
plt.plot(his.epoch, his.history.get('val_loss'), label='val_loss')
plt.legend()
Out[26]:
<matplotlib.legend.Legend at 0x7fa2741b7d90>

In [ ]: