【机器学习案例七】:图像类别识别keras图像识别
案例背景
数据集 caltech101 中给出了经过转化的 102 种物体图像数据
( 128*128 像 素 ), 共 9144 个样例,相应的类标签在
caltech101_labels给出。在此基础上将原始数据划分为训练集(80%)
science direct和测试集(20%)。(如果计算机性能有限,可以从 102 种物体中任意
抽取 10~20 种作为数据集)
数据预处理
导⼊库
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import keras
dels import Sequential
from keras.layers import Den, Dropout
from keras.optimizers import SGD
del_lection import train_test_split
读取数据
ad_csv('caltech101_labels.csv')
ad_csv('caltech101.csv')
删除⽆⽤列
data.lumns[0],axis=1,inplace=True)lmnt
划分训练集测试集
x_train,x_test,y_train,y_test = train_test_split(data,labels,test_size = 0.2,random_state = 1)标签处理
num_class=102
y_train = _categorical(y_train, num_class)井底之蛙英文版
y_test = _categorical(y_test, num_class)
普通神经⽹络
要求
建⽴具有两个隐藏层以及 Dropout 层的神经⽹络模型对图像数据进⾏分类,并对模型性能进⾏评价。
建⽴模型
model.add(Den(4092, activation='relu', input_shape=(16384,)))
model.add(Den(2048, activation='relu'))
model.add(Dropout(0.5))
model.add(Den(2048, activation='relu'))
model.add(Dropout(0.1))
model.add(Den(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Den(102, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-4, momentum=0.9, nesterov=True)
model.summary()
history = model.fit(x_train, y_train, batch_size=128, epochs=30,
verbo=1, validation_data=(x_test, y_test))
模型评估
score = model.evaluate(x_train, y_train, verbo=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
训练集:0.59
测试集:0.41
CNN
要求
分别建⽴具有 1 个和 2 个卷积层的 CNN 模型对图像数据进⾏分类,并对模型性能进⾏评价,重点考察卷积核数量对模型性能的影响。模型建⽴(两个卷积层)
导⼊库
from sklearn.datats import fetch_mldata
dels import Sequential
from keras.layers import Den, Dropout
from keras.layers import Conv2D, MaxPooling2D,Flatten
from keras.optimizers import SGD
挑选⼀⼩部分数据,共36类
data1=data.iloc[:5000,:]
label1=labels.iloc[:5000,:]
训练数据预处理
img_rows, img_cols = 128, 128
X_train,X_test,y_train,y_test = train_test_split(data1,label1,test_size = 0.2,random_state = 1)
X_train = np.array(X_train).reshape(X_train.shape[0], img_rows, img_cols, 1)
X_test = np.array(X_test).reshape(X_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
# convert class vectors to binary class matrices
respiratory systemnum_class=len(label1.iloc[:,0].value_counts().index) #len(label1.iloc[:,0].unique())
保持联络
y_train = _categorical(y_train, num_class)
y_test = _categorical(y_test, num_class)
搭建模型框架
input_shape=input_shape,
padding='same'
)
in ca)
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(16,
kernel_size=(3,3),
activation='relu',
padding='same'
)
)
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Den(32*32*16,activation='relu'))
model.add(Dropout(0.25))
model.add(Den(128,activation='relu'))
model.add(Dropout(0.25))
model.add(Den(num_class,activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-4, momentum=0.9, nesterov=True) pile(loss='categorical_crosntropy',
optimizer=sgd,metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=128,epochs=5,
verbo=1,validation_data=(X_test, y_test))
模型评估
1. 测试集
score = model.evaluate(X_test, y_test, verbo=0)
print('Test loss:', score[0])
湖北大学自考
print('Test accuracy:', score[1])
Test loss: 0.3388944187760353
Test accuracy: 0.91
2. 训练集
score = model.evaluate(X_train, y_train, verbo=0)
print('Train loss:', score[0])
print('Train accuracy:', score[1])
Train loss: 1.37
Train accuracy: 0.655
模型建⽴(⼀个卷积层)
搭建⼀个卷积层容易报错
搭建模型框架
input_shape=input_shape,
padding='same'
)
)
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
aunt是什么意思model.add(Den(16*16*16,activation='relu'))
lounge是什么意思
model.add(Dropout(0.25))
model.add(Den(128,activation='relu'))
model.add(Dropout(0.25))
model.add(Den(num_class,activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-4, momentum=0.9, nesterov=True)
model.fit(X_train, y_train, batch_size=128,epochs=5, verbo=1,validation_data=(X_test, y_test))模型评估
1. 测试集
score = model.evaluate(X_test, y_test, verbo=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Test loss: 1,。47
Test accuracy: 0.629
2. 训练集
score = model.evaluate(X_train, y_train, verbo=0)
print('Train loss:', score[0])
print('Train accuracy:', score[1])
女娲补天翻译Train loss: 1.192
Train accuracy: 0.6905