本例程是一个处理图像数据的标准的分布过程,利用CNN 实现对狗的品种的鉴定。
实现过程在代码中会有相关注释。
本例程数据集资源在这 https://download.csdn.net/download/rh8866/12532963
import os
import cv2
import numpy as np
import pandas as pd
from tqdm import tqdm
import seaborn as sns
import matplotlib.pyplot as plt
import sklearn
from sklearn.model_selection import train_test_split
labels = pd.read_csv('./dataset/dog-breed/labels.csv')
labels.head()
breed_count = labels['breed'].value_counts()
breed_count.head(10)
breed_count.shape
targets = pd.Series(labels['breed'])
one_hot = pd.get_dummies(targets,sparse=True)
one_hot_labels = np.asarray(one_hot)
img_rows = 128
img_cols = 128
num_channel=1
'img_1 = cv2.imread('./dataset/dog-breed/train/001513dfcb2ffafc82cccf4d8bbaba97.jpg',0)
plt.title('Original Image')
plt.imshow(img_1)
print(img_1.shape)
img_1_resize = cv2.resize(img_1,(img_rows,img_cols))
print(img_1_resize.shape)
plt.title('Resized Image')
plt.imshow(img_1_resize)
x_feature = []
y_feature = []
i = 0
for f,img in tqdm(labels.values):
train_img = cv2.imread('./dataset/dog-breed/train/{}.jpg'.format(f),0)
label = one_hot_labels[i]
train_img_resize = cv2.resize(train_img,(img_rows,img_cols))
x_feature.append(train_img_resize)
y_feature.append(label)
i += 1
x_train_data = np.array(x_feature,np.float32) / 255.
print(x_train_data.shape)
x_train_data = np.expand_dims(x_train_data,axis = 3)
print(x_train_data.shape)
y_train_data = np.array(y_feature,np.uint8)
print(y_train_data.shape)
x_train,x_val,y_train,y_val = train_test_split(x_train_data,y_train_data,test_size=0.2,random_state=2)
print(x_train.shape)
print(x_val.shape)
submission = pd.read_csv('./dataset/dog-breed/sample_submission.csv')
test_img = submission['id']
test_img.head()
x_test_feature = []
i = 0
for f in tqdm(test_img.values):
img = cv2.imread('./dataset/dog-breed/test/{}.jpg'.format(f),0)
img_resize = cv2.resize(img,(img_rows,img_cols))
x_test_feature.append(img_resize)
x_test_data = np.array(x_test_feature, np.float32) / 255.
print (x_test_data.shape)
x_test_data = np.expand_dims(x_test_data, axis = 3)
print (x_test_data.shape)
from keras.models import Sequential
from keras.layers import Dense,Dropout
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
model = Sequential()
model.add(Convolution2D(filters = 64,kernel_size = (4,4),padding = 'Same',
activation='relu',input_shape = (img_rows,img_cols,num_channel)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(filters = 64,kernel_size = (4,4),padding='Same',
activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(units=120,activation='relu'))
model.add(Dense(units=120,activation='softmax'))
model.compile(optimizer='adam',loss = "categorical_crossentropy",metrics=['accuracy'])
model.summary()
batch_size = 128
nb_epochs = 2
history = model.fit(x_train,y_train,
batch_size=batch_size,
epochs=nb_epochs,
verbose=2,
validation_data=(x_val,y_val),
initial_epoch=0)
results = model.predict(x_test_data)
prediction = pd.DataFrame(results)
col_names = one_hot.columns.values
prediction.columns = col_names
prediction.insert(0, 'id', submission['id'])
submission = prediction
submission.head()
submission.to_csv('new_submission.csv', index=False)
至此本例程就结束了,精确度暂不考虑,勿喷,有好的建议欢迎评论讨论。
相关知识
基于卷积神经网络的宠物识别
基于卷积神经网络的宠物猫品种分类研究 csdn
基于卷积神经网络通过声音识别动物情绪的方法及系统
基于卷积神经网络通过声音识别动物情绪的方法及系统与流程
基于卷积神经网络的鸟类(声音和图片)识别项目
基于卷积神经网络的宠物识别 Pet Recognition Based on Convolutional Neural Network
【卷积神经网络】CNN详解以及猫狗识别实例
基于卷积神经网络的宠物猫品种分类研究
100基于卷积神经网络之鸟鸣识别鸟的种类
深度学习设计基于Tensorflow卷积神经网络猫的品种识别系统
网址: 用卷积神经网络实现对小狗品种的识别 https://m.mcbbbk.com/newsview642279.html
上一篇: 小狗都有什么品种图片 |
下一篇: 宠物租赁,羊驼出租,企鹅出租 |