开始记录学习深度学习的点点滴滴
随着人工智能的不断发展,这门技术也越来越重要,很多人都开启了学习人工智能,本人开始记录学习人工智能的点点滴滴。
深度学习的概念在网上已经能够搜到很多内容了,这里主要介绍和记录以下相关可以实际运行的代码,对深度学习的实际操作进行深入理解,对出现的问题记录以下。我先从图像分类开始学习深度学习。
这里记录了第一个深度学习示例。
图片的搜索可以从深度学习的网站下载,这里我用scrapy爬虫爬取的。图片目录按照以下方式设置:
这里我的图片包括战斗机、直升飞机和舰船,其中战斗机文件名都为Fighter_X.jpg,直升飞机的文件名标识为Helicopter_X.jpg,舰船的飞机名标识为Ship_X.jpg。
1. 读取文件的代码如下:
import cv2
import numpy as np
import pandas as pd
import matplotlib.image as npimg
import matplotlib.pyplot as plt
import os
import random
import gc
train_dir = 'input/train' # 训练集数据
val_dir = 'input/test' # 验证集数据
test_imgs = ['input/test/{}'.format(i) for i in os.listdir(val_dir)]
train_imgs = ['input/train/{}'.format(i) for i in os.listdir(train_dir)]
random.shuffle(train_imgs) ###图片乱序,这对学习很重要。
for ima in train_imgs[0:10]:
img = npimg.imread(ima)
imgplot = plt.imshow(img)
plt.show()
nrows = 224
ncoloumns = 224
channel = 3
def read_and_process_image(list_imgs):
X = []
y = []
for image in list_imgs:
X.append(cv2.resize(cv2.imread(image,cv2.IMREAD_COLOR),(nrows,ncoloumns),interpolation=cv2.INTER_CUBIC))
if 'Fighter' in image:
y.append(0)
elif 'Helicopter' in image:
y.append(1)
return X,y
X,y = read_and_process_image(train_imgs)
plt.figure(figsize=(20,10))
columns = 5
for i in range(columns):
plt.subplot(5/columns+1,columns,i+1)
plt.imshow(X[i])```
from sklearn.model_selection import train_test_split
del train_imgs
gc.collect()
X = np.array(X)
y = np.array(y)
print("Shape of train set:", X.shape)
print("Shape of label set:", y.shape)
X_train, X_val, y_train, y_val = train_test_split(X,y, test_size=0.2, random_state=2)
print("Shape of train images is:", X_train.shape)
print("Sahpe of validation images is:", X_val.shape)
print("Shape of train label is:", y_train.shape)
print("Shape of validation label is:", y_val.shape)
del X
del y
gc.collect()
ntrain = len(X_train)
nval = len(X_val)
2. 构建深度学习网络的代码如下:
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array, load_img
import tensorflow as tf
batch_size = 32
model = models.Sequential()
model.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(64,(3,3),activation='relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(128,(3,3),activation='relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(128,(3,3),activation='relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc']) ####构建反向梯度
3. 进行训练和模型数据保存:
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,)
val_datagen = ImageDataGenerator(rescale=1./255)
#y_train = keras.utils.to_categorical(y_train,3) ####由于这里是多目标分类,因此需要进行one-hot编码。
#y_val = keras.utils.to_categorical(y_val,3)
train_datagen = train_datagen.flow(X_train,y_train,batch_size=batch_size)
val_datagen = val_datagen.flow(X_val,y_val,batch_size=batch_size)
history = model.fit_generator(train_datagen,
steps_per_epoch=ntrain,
epochs=1,
validation_data=val_datagen,
validation_steps=nval)
#Save the model
model.save_weights('model_wieghts.h5')
model.save('model_keras.h5')
4. 看图验证:
from keras.models import load_model
modelTest = load_model('model_keras.h5')
random.shuffle(test_imgs)
X_test, y_test = read_and_process_image(test_imgs[0:10])
x = np.array(X_test)
test_datagen = ImageDataGenerator(rescale=1./255)
i = 0
test_label = []
plt.figure(figsize=(30,20))
for batch in test_datagen.flow(x, batch_size=1):
pred = modelTest.predict(batch)
if pred > 0.5:
test_label.append('Fighter')
else:
test_label.append('Helicopter')
plt.subplot(5/columns + 1, columns, i + 1)
plt.title ('This is a ' + test_label[i])
imgplot = plt.imshow(batch[0])
i += 1
if i % 10 ==0:
break
plt.show()
随着学习的深入,我们也学习了学习迁移,理论知识这里不多介绍。
图片的目录设置如下图:
1. 读取文件和数据处理里的代码如下:
import matplotlib.pyplot as plt
import os
import random
import gc
train_dir = 'input/train' # 训练集数据
val_dir = 'input/test' # 验证集数据
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array, load_img
from datetime import datetime
TIMESTAMP = "{0:%Y-%m-%dT%H-%M-%S/}".format(datetime.now())
from keras.callbacks import TensorBoard, ModelCheckpoint
batch_size = 32
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array, load_img
from datetime import datetime
TIMESTAMP = "{0:%Y-%m-%dT%H-%M-%S/}".format(datetime.now())
from keras.callbacks import TensorBoard, ModelCheckpoint
batch_size = 32
nrows = 224
ncoloumns = 224
channel = 3
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)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical',
shuffle=True,
seed = 12)
validation_generator = val_datagen.flow_from_directory(
val_dir ,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical',
shuffle=False,
seed=13)
2. 构建迁移网络代码如下:
import keras
from keras import Model
from keras_squeezenet import SqueezeNet
from keras.callbacks import TensorBoard, ModelCheckpoint
from keras.layers import Flatten, Dense, Dropout, GlobalAveragePooling2D
from keras.models import load_model
from keras.preprocessing import image
from PIL import ImageFile
import numpy as np
import tensorflow as tf
from keras.optimizers import SGD
from keras import optimizers
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
classes = sorted([o for o in os.listdir(train_dir)]) # 根据文件名分类# x = base_model.output
x = base_model.output # 自定义网络
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(3, activation='softmax')(x)
model = Model(base_model.input, predictions) # 新网络=预训练网络+自定义网络
model.summary()
model.compile(optimizer=optimizers.RMSprop(lr=0.0001),
loss='categorical_crossentropy', metrics=['accuracy'])
3. 运行代码如下:
for layer in base_model.layers:
layer.trainable = False
model.fit_generator(train_generator,
steps_per_epoch=85,
epochs=10,
validation_data=validation_generator,
validation_steps=10)
validation_data=(X_val, y_val))
model.save_weights('model_wieghts.h5')
model.save('model_keras.h5')
4. 查看训练结果代码如下:
img = image.load_img('./input/train/Ship_85.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
res = model.predict(x)
plt.imshow(img)
print(np.argmax(res, axis=1)[0])
这里对文章进行总结: