图像卷积运算
对图像矩阵与滤波器矩阵进行对应相乘再求和运算,转化得到新的矩阵。
作用:快速定位图像中某些边缘特征
英文:convolition
将图片与轮廓滤波器进行卷积运算,可快速定位固定轮廓特征的位置
卷积神经网络的核心
计算机根据样本图片,自动寻找合适的轮廓过滤器,对新图片进行轮廓匹配
自动求解W,寻找合适的过滤器
一个过滤器不够,需要寻找很多过滤器
池化层实现维度缩减
池化:按照一个固定规则对图像矩阵进行处理,将其转换为更低维度的矩阵
保留核心信息的情况下,实现维度缩减
卷积神经网络
把卷积、池化、mlp先后连接在一起,组成卷积神经网络
卷积神经网络两大特点
卷积运算导致的两个问题:
图像填充(padding)
通过在图像各边增加像素,使其在进行卷积运算后维持原图大小
通过padding增加像素的数量,由过滤器尺寸与stride决定
经典的CNN模型用于新场景
使用经典的CNN模型结构对图像预处理,再建立MLP模型
参考经典的CNN结构搭建新模型
加载经典的CNN模型,剥除其FC层,对图像进行预处理
把预处理完成的数据作为输入,分类结果为输出,建立一个MLP模型
模型训练
任务:基于dataset\training_set数据,根据提供的结构,建立CNN模型
图片加载
from keras.preprocessing.image import ImageDataGenerator
图像增强/预处理配置
train_datagen = ImageDataGenerator(rescale=1./255)
加载图像
training_set = train_datagen.flow_from_directory('./dataset/training_set',
target_size=(50,50),
batch_size=32,
class_mode='binary')
建立CNN模型
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense
model=Sequential()
卷积层
model.add(Conv2D(32,(3,3),input_shape=(50,50,3),activation='relu'))
池化层
model.add(MaxPooling2D(pool_size=(2,2)))
第二个卷积、池化层
model.add(Conv2D(32,(3,3),activation = 'relu'))
model.add(MaxPooling2D(pool_size =(2,2)))
Flattening层
model.add(Flatten())
全连接层
model.add(Dense(units =128, activation = 'relu'))
model.add(Dense(units=1,activation = 'sigmoid'))
训练与预测
配置模型
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy’])
查看模型结构
model.summary()
训练模型
model.fit_generator(training_set,epochs = 25)
计算预测准确率
model.evaluate_generator(training_set)
任务:使用VGG16的结构提取图像特征,再根据特征建立mlp模型,实现猫狗图像识别。训练/测试数据:dataset\data_vgg
备注:mlp模型只有一个隐藏层(10个神经元)
图片加载
from keras.preprocessing.image import img_to_array, load_img
img_path ='1.jpg'
img = load_img(img_path, target_size=(224,224))
img =img_to_array(img)
图片预处理
from keras.applications.vgg16 import preprocess_input
x=np.expand_dims(img,axis=0)
把图片矩阵转化为可用于VGG16输入的矩阵
x=preprocess_input(x)
载入VGG16结构(去除全连接层)
from keras.applications.vgg16 import VGG16
model_vgg =VGG16(weights='imagenet', include_top=False)
特征提取
features = model_vgg.predict(x)
建立MLP模型
建立一个Sequential顺序模型并添加各层
from keras.models import Sequential
model= Sequential()
from keras.layers import Dense
model.add(Dense(units=10,activation='relu',input_dim=25088))
model.add(Dense(units=1,activation='sigmoid'))
model.summary()
通过.compile()配置模型求解过程参数
model.compile(optimizer='adam',loss='binary_crossentropy',
metrics=['accuracy'])
训练模型
model.fit(X_train,y_train,epochs=50)
任务:基于dataset\training_set数据,根据提供的结构,建立CNN模型
加载数据
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('./dataset/training_set',
target_size=(50,50),
batch_size=32,
class_mode='binary')
建模
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense
model=Sequential()
model.add(Conv2D(32,(3,3),input_shape=(50,50,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(32,(3,3),activation = 'relu'))
model.add(MaxPooling2D(pool_size =(2,2)))
model.add(Flatten())
model.add(Dense(units=128, activation = 'relu'))
model.add(Dense(units=1,activation = 'sigmoid'))
配置模型
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
查看模型结构
model.summary()
训练
model.fit_generator(training_set,epochs=25,steps_per_epoch=250)
准确率
accuracy_train=model.evaluate_generator(training_set,steps=1)
print(accuracy_train)
测试数据准确率
test_set=train_datagen.flow_from_directory('./dataset/test_set',
target_size=(50,50),
batch_size=32,
class_mode='binary')
accuracy_test=model.evaluate_generator(test_set,steps=1)
print(accuracy_test)
测试
from keras.preprocessing.image import load_img,img_to_array
from PIL import Image
import os
pic_cat='1.jpg'
pic_cat= load_img(pic_cat,target_size=(50,50))
pic_cat=img_to_array(pic_cat)
pic_cat=pic_cat/255
pic_cat=pic_cat.reshape(1,50,50,3)
result=model.predict_classes(pic_cat)
print(result)
img = Image.open(os.path.join('1.jpg'))
img
from keras.preprocessing.image import load_img,img_to_array
pic_dog='7.jpg'
pic_dog= load_img(pic_dog,target_size=(50,50))
pic_dog=img_to_array(pic_dog)
pic_dog=pic_dog/255
pic_dog=pic_dog.reshape(1,50,50,3)
result=model.predict_classes(pic_dog)
print(result)
img = Image.open(os.path.join('7.jpg'))
img
测试
import matplotlib as mlp
font2 = {'family':'SimHei','weight':'normal',
'size':20,
}
mlp.rcParams['font.family']='SimHei'
mlp.rcParams['axes.unicode_minus']= False
from matplotlib import pyplot as plt
from matplotlib.image import imread
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
a = [i for i in range(1,10)]
fig= plt.figure(figsize=(10,10))
for i in a:
img_name = str(i)+'.jpg'
img_ori = load_img(img_name,target_size=(50,50))
img = img_to_array(img_ori)
img= img.astype('float32')/255
img = img.reshape(1,50,50,3)
result = model.predict_classes(img)
img_ori = load_img(img_name,target_size=(250,250))
plt.subplot(3,3,i)
plt.imshow(img_ori)
plt.title('预测为:狗狗'if result[0][0]== 1 else '预测为:猫咪')
plt.show()
任务:使用VGG16的结构提取图像特征,再根据特征建立mlp模型,实现猫狗图像识别。训练/测试数据:dataset\data_vgg
备注:mlp模型只有一个隐藏层(10个神经元)
加载数据
from keras.preprocessing.image import img_to_array, load_img
img_path ='1.jpg'
img = load_img(img_path, target_size=(224,224))
img =img_to_array(img)
import numpy as np
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import VGG16
model_vgg =VGG16(weights='imagenet', include_top=False)
x=np.expand_dims(img,axis=0)
x=preprocess_input(x)
print(x.shape)
特征提取
features = model_vgg.predict(x)
摊开
features =features.reshape(1,7*7*512)
VGG批量图片预处理
#load image and preprocess it with vgg16 structure
#--by flare
from keras.preprocessing.image import img_to_array,load_img
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
model_vgg = VGG16(weights='imagenet', include_top=False)
#define a method to load and preprocess the image
def modelProcess(img_path,model):
img = load_img(img_path, target_size=(224, 224))
img = img_to_array(img)
x = np.expand_dims(img,axis=0)
x = preprocess_input(x)
x_vgg = model.predict(x)
x_vgg = x_vgg.reshape(1,25088)
return x_vgg
#list file names of the training datasets
import os
folder = "dataset/data_vgg/cats"
dirs = os.listdir(folder)
#generate path for the images
img_path = []
for i in dirs:
if os.path.splitext(i)[1] == ".jpg":
img_path.append(i)
img_path = [folder+"//"+i for i in img_path]
#preprocess multiple images
features1 = np.zeros([len(img_path),25088])
for i in range(len(img_path)):
feature_i = modelProcess(img_path[i],model_vgg)
print('preprocessed:',img_path[i])
features1[i] = feature_i
folder = "dataset/data_vgg/dogs"
dirs = os.listdir(folder)
img_path = []
for i in dirs:
if os.path.splitext(i)[1] == ".jpg":
img_path.append(i)
img_path = [folder+"//"+i for i in img_path]
features2 = np.zeros([len(img_path),25088])
for i in range(len(img_path)):
feature_i = modelProcess(img_path[i],model_vgg)
print('preprocessed:',img_path[i])
features2[i] = feature_i
#label the results
print(features1.shape,features2.shape)
y1 = np.zeros(300)
y2 = np.ones(300)
#generate the training data
X = np.concatenate((features1,features2),axis=0)
y = np.concatenate((y1,y2),axis=0)
y = y.reshape(-1,1)
print(X.shape,y.shape)
数据分离
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=50)
print(X_train.shape,X_test.shape,X.shape)
设置模型
from keras.models import Sequential
from keras.layers import Dense
model= Sequential()
model.add(Dense(units=10,activation='relu',input_dim=25088))
model.add(Dense(units=1,activation='sigmoid'))
model.summary()
配置模型求解过程参数 训练模型
model.compile(optimizer='adam',loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(X_train,y_train,epochs=50)
准确率
from sklearn.metrics import accuracy_score
y_train_predict = model.predict_classes(X_train)
accuracy_train=accuracy_score(y_train,y_train_predict)
print(accuracy_train)
y_test_predict = model.predict_classes(X_test)
accuracy_test=accuracy_score(y_test,y_test_predict)
print(accuracy_test)
测试
import matplotlib as mlp
font2 = {'family':'SimHei','weight':'normal',
'size':20,
}
mlp.rcParams['font.family']='SimHei'
mlp.rcParams['axes.unicode_minus']= False
from matplotlib import pyplot as plt
from matplotlib.image import imread
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
a = [i for i in range(1,10)]
fig= plt.figure(figsize=(10,10))
for i in a:
img_name = str(i)+'.jpg'
img_path = img_name
img = load_img(img_path,target_size=(224,224))
img = img_to_array(img)
x = np.expand_dims(img,axis=0)
x = preprocess_input(x)
x_vgg = model_vgg.predict(x)
x_vgg = x_vgg.reshape(1,25088)
result = model.predict_classes(x_vgg)
img_ori = load_img(img_name,target_size=(250,250))
plt.subplot(3,3,i)
plt.imshow(img_ori)
plt.title('预测为:狗狗'if result[0][0]== 1 else '预测为:猫咪')
plt.show()