Tensorflow 2.0实践课】2.1 Augmentation"/>
【吴恩达Tensorflow 2.0实践课】2.1 Augmentation
目录
2.1.1 Training with th Cats & Dogs dataset
2.1.2 walking through the notebook
2.1.1 Training with th Cats & Dogs dataset
把图片放入不同文件夹中,TensorFlow能够自动添加标签
2.1.2 walking through the notebook
下载数据(来源Kaggle)
!wget --no-check-certificate \.zip \-O /tmp/cats_and_dogs_filtered.zip
-O 接重新命名的文件
解压文件
import os
import zipfilelocal_zip = '/tmp/cats_and_dogs_filtered.zip'zip_ref = zipfile.ZipFile(local_zip, 'r')zip_ref.extractall('/tmp')
zip_ref.close()
zipfile.ZipFile: 用于读写 ZIP 文件的类
解压到 /tmp/cats_and_dogs_filtered 文件夹中
定义目录
base_dir = '/tmp/cats_and_dogs_filtered'train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')# Directory with our training cat/dog pictures
train_cats_dir = os.path.join(train_dir, 'cats')
train_dogs_dir = os.path.join(train_dir, 'dogs')# Directory with our validation cat/dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
打印文件名
train_cat_fnames = os.listdir( train_cats_dir )
train_dog_fnames = os.listdir( train_dogs_dir )print(train_cat_fnames[:10])
print(train_dog_fnames[:10])
打印训练/验证图片的个数
print('total training cat images :', len(os.listdir( train_cats_dir ) ))
print('total training dog images :', len(os.listdir( train_dogs_dir ) ))print('total validation cat images :', len(os.listdir( validation_cats_dir ) ))
print('total validation dog images :', len(os.listdir( validation_dogs_dir ) ))
显示结果:
total training cat images : 1000 total training dog images : 1000 total validation cat images : 500 total validation dog images : 500
显示部分图片
%matplotlib inline
# 用 jupyter/Colab 需要加这一行
# 作用是当你调用matplotlib.pyplot的绘图函数plot()进行绘图的时候,或者生成一个figure画布的时候,可以直接在你的python console里面生成图像。import matplotlib.image as mpimg
import matplotlib.pyplot as plt# Parameters for our graph; we'll output images in a 4x4 configuration
nrows = 4
ncols = 4pic_index = 0 # Index for iterating over images# Set up matplotlib fig, and size it to fit 4x4 pics
fig = plt.gcf()
fig.set_size_inches(ncols*4, nrows*4)# 猫和狗,各自显示8张,所以索引+8
pic_index+=8# pic_index-8为0; pic_index为8
# next_cat_pix为一个列表,./train/cat中前8个图片的完整地址
next_cat_pix = [os.path.join(train_cats_dir, fname) for fname in train_cat_fnames[ pic_index-8:pic_index] ]# next_dog_pix为一个列表,./train/dog中前8个图片的完整地址
next_dog_pix = [os.path.join(train_dogs_dir, fname) for fname in train_dog_fnames[ pic_index-8:pic_index]]# 一共16张图片,索引存入i中,路径存入img_path中
for i, img_path in enumerate(next_cat_pix + next_dog_pix):# Set up subplot; subplot indices start at 1# 第一幅图位置为(4, 4, 1)sp = plt.subplot(nrows, ncols, i + 1)sp.axis('Off') # Don't show axes (or gridlines)# 读取图片img = mpimg.imread(img_path)# 显示图片plt.imshow(img)plt.show()
显示结果:
创建模型
import tensorflow as tfmodel = tf.keras.models.Sequential([# Note the input shape is the desired size of the image 150x150 with 3 bytes colortf.keras.layers.Conv2D(16, (3 ,3), activation='relu', input_shape=(150, 150, 3)),tf.keras.layers.MaxPooling2D(2 ,2),tf.keras.layers.Conv2D(32, (3 ,3), activation='relu'),tf.keras.layers.MaxPooling2D(2 ,2),tf.keras.layers.Conv2D(64, (3 ,3), activation='relu'),tf.keras.layers.MaxPooling2D(2 ,2),# Flatten the results to feed into a DNNtf.keras.layers.Flatten(),# 512 neuron hidden layertf.keras.layers.Dense(512, activation='relu'),# Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('cats') and 1 for the other ('dogs')tf.keras.layers.Dense(1, activation='sigmoid')
])model.summary()
显示结果:
设置优化器、损失函数
from tensorflow.keras.optimizers import RMSpropmodelpile(optimizer=RMSprop(lr=0.001),loss='binary_crossentropy',metrics = ['acc'])
数据预处理
from tensorflow.keras.preprocessing.image import ImageDataGenerator# All images will be rescaled by 1./255.
train_datagen = ImageDataGenerator( rescale = 1.0/255. )
test_datagen = ImageDataGenerator( rescale = 1.0/255. )# --------------------
# Flow training images in batches of 20 using train_datagen generator
# --------------------
train_generator = train_datagen.flow_from_directory(train_dir,batch_size=20,class_mode='binary',target_size=(150, 150))
# --------------------
# Flow validation images in batches of 20 using test_datagen generator
# --------------------
validation_generator = test_datagen.flow_from_directory(validation_dir,batch_size=20,class_mode = 'binary',target_size = (150, 150))
训练
history = model.fit_generator(train_generator,validation_data=validation_generator,steps_per_epoch=100,epochs=15,validation_steps=50,verbose=2)
可视化卷积过程
import numpy as np
import random
from tensorflow.keras.preprocessing.image import img_to_array, load_img# Let's define a new Model that will take an image as input, and will output
# intermediate representations for all layers in the previous model after
# the first.
successive_outputs = [layer.output for layer in model.layers[1:]]#visualization_model = Model(img_input, successive_outputs)
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)# Let's prepare a random input image of a cat or dog from the training set.
cat_img_files = [os.path.join(train_cats_dir, f) for f in train_cat_fnames]
dog_img_files = [os.path.join(train_dogs_dir, f) for f in train_dog_fnames]img_path = random.choice(cat_img_files + dog_img_files)
img = load_img(img_path, target_size=(150, 150)) # this is a PIL imagex = img_to_array(img) # Numpy array with shape (150, 150, 3)
x = x.reshape((1,) + x.shape) # Numpy array with shape (1, 150, 150, 3)# Rescale by 1/255
x /= 255.0# Let's run our image through our network, thus obtaining all
# intermediate representations for this image.
successive_feature_maps = visualization_model.predict(x)# These are the names of the layers, so can have them as part of our plot
layer_names = [layer.name for layer in model.layers]# -----------------------------------------------------------------------
# Now let's display our representations
# -----------------------------------------------------------------------
for layer_name, feature_map in zip(layer_names, successive_feature_maps):if len(feature_map.shape) == 4:#-------------------------------------------# Just do this for the conv / maxpool layers, not the fully-connected layers#-------------------------------------------n_features = feature_map.shape[-1] # number of features in the feature mapsize = feature_map.shape[ 1] # feature map shape (1, size, size, n_features)# We will tile our images in this matrixdisplay_grid = np.zeros((size, size * n_features))#-------------------------------------------------# Postprocess the feature to be visually palatable#-------------------------------------------------for i in range(n_features):x = feature_map[0, :, :, i]x -= x.mean()x /= x.std ()x *= 64x += 128x = np.clip(x, 0, 255).astype('uint8')display_grid[:, i * size : (i + 1) * size] = x # Tile each filter into a horizontal grid#-----------------# Display the grid#-----------------scale = 20. / n_featuresplt.figure( figsize=(scale * n_features, scale) )plt.title ( layer_name )plt.grid ( False )plt.imshow( display_grid, aspect='auto', cmap='viridis' )
测试准确率和损失
#-----------------------------------------------------------
# Retrieve a list of list results on training and test data
# sets for each training epoch
#-----------------------------------------------------------
acc = history.history[ 'acc' ]
val_acc = history.history[ 'val_acc' ]
loss = history.history[ 'loss' ]
val_loss = history.history['val_loss' ]epochs = range(len(acc)) # Get number of epochs#------------------------------------------------
# Plot training and validation accuracy per epoch
#------------------------------------------------
plt.plot ( epochs, acc )
plt.plot ( epochs, val_acc )
plt.title ('Training and validation accuracy')
plt.figure()#------------------------------------------------
# Plot training and validation loss per epoch
#------------------------------------------------
plt.plot ( epochs, loss )
plt.plot ( epochs, val_loss )
plt.title ('Training and validation loss' )
清理
import os, signalos.kill( os.getpid() , signal.SIGKILL)
2.1.3 Coding Augmentation with ImageDataGenerator
图片旋转有助于提高识别
# update to do image augmentation
train_datagen = ImageDataGenerator(rescale=1.0/255, # 归一化rotation_range=40, # 0-40° 随机旋转图像width_shift_range=0.2, # 移位,将图片在水平方向上,随机移位20%height_shift_range=0.2, # 移位,将图片在垂直方向上,随机移位20%shear_range=0.2, # 剪力,随机剪去图像的20%zoom_range=0.2, # 随机缩放比例20%horizontal_flip=True # 随机水平镜像fill_mode='nearest' # 填充周围像素
)
图片剪力:
2.1.4 Demonstrating overfitting in cats vs. dogs
train_datagen = ImageDataGenerator(rescale=1./255)
过拟合
2.1.5 Adding augmentation to cats vs. dogs
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')
验证数据集的损失波动较大
俺算的结果:
2.1.6 Exploring augmentation horses vs. humans
更多推荐
【吴恩达Tensorflow 2.0实践课】2.1 Augmentation
发布评论