本博文涉及以下:六
目录:
Zero:导入数据集
一、检测人脸
二、检测狗狗
三、从头实现CNN实现狗狗分类
四、迁移VGG16实现狗狗分类
五、迁移ResNet_50实现狗狗分类
六、自己实现狗狗分类
六、自己实现狗狗分类整体流程实现一个算法,它的输入为图像的路径,它能够区分图像是否包含一个人、狗或两者都不包含,然后:
如果从图像中检测到一只狗,返回被预测的品种。如果从图像中检测到人,返回最相像的狗品种。如果两者都不能在图像中检测到,输出错误提示。可以自己编写检测图像中人类与狗的函数,可以随意使用已经完成的 face_detector 和 dog_detector 函数。使用在步骤5的CNN来预测狗品种。
下面提供了算法的示例输出,也可以自由地设计模型!
1、加载数据集
from sklearn.datasets import load_files
from keras.utils import np_utils
import numpy as np
from glob import glob
def load_dataset(path):
data = load_files(path)
dog_files = np.array(data['filenames'])
dog_targets = np_utils.to_categorical(np.array(data['target']), 133)
return dog_files, dog_targets
train_files, train_targets = load_dataset('dogImages/train')
valid_files, valid_targets = load_dataset('dogImages/valid')
test_files, test_targets = load_dataset('dogImages/test')
dog_names = [item[20:-1] for item in sorted(glob("dogImages/train/*/"))]
print('There are %d total dog categories.' % len(dog_names))
print('There are %s total dog images.n' % len(np.hstack([train_files, valid_files, test_files])))
print('There are %d training dog images.' % len(train_files))
print('There are %d validation dog images.' % len(valid_files))
print('There are %d test dog images.'% len(test_files))
2、检测是否有狗狗
from keras.applications.resnet50 import ResNet50
ResNet50_model = ResNet50(weights='imagenet')
from keras.preprocessing import image
from tqdm import tqdm
def path_to_tensor(img_path):
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
return np.expand_dims(x, axis=0)
def paths_to_tensor(img_paths):
list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
return np.vstack(list_of_tensors)
from keras.applications.resnet50 import preprocess_input, decode_predictions
def ResNet50_predict_labels(img_path):
img = preprocess_input(path_to_tensor(img_path))
return np.argmax(ResNet50_model.predict(img))
def dog_detector(img_path):
prediction = ResNet50_predict_labels(img_path)
return ((prediction <= 268) & (prediction >= 151))
3、检测是否有人
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml')
def face_detector(img_path):
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
return len(faces) > 0
4、得到bottleneck特征:ResNet50
bottleneck_features = np.load('bottleneck_features/DogResnet50Data.npz')
train_Resnet50 = bottleneck_features['train']
valid_Resnet50 = bottleneck_features['valid']
test_Resnet50 = bottleneck_features['test']
5、模型建立、编译、训练和测试
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.layers import Dropout, Flatten, Dense
from keras.models import Sequential
Resnet50_model = Sequential()
Resnet50_model.add(GlobalAveragePooling2D(input_shape=train_Resnet50.shape[1:]))
Resnet50_model.add(Dense(133, activation='softmax'))
Resnet50_model.summary()
Resnet50_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
from keras.callbacks import ModelCheckpoint
checkpointer = ModelCheckpoint(filepath='saved_models/weights.best.Resnet50.hdf5',
verbose=1, save_best_only=True)
Resnet50_model.fit(train_Resnet50, train_targets,
validation_data=(valid_Resnet50, valid_targets),
epochs=20, batch_size=20, callbacks=[checkpointer], verbose=1)
Resnet50_model.load_weights('saved_models/weights.best.Resnet50.hdf5')
Resnet50_predictions = [np.argmax(Resnet50_model.predict(np.expand_dims(feature, axis=0))) for feature in test_Resnet50]
test_accuracy = 100*np.sum(np.array(Resnet50_predictions)==np.argmax(test_targets, axis=1))/len(Resnet50_predictions)
print('Test accuracy: %.4f%%' % test_accuracy)
6、测试新图片
from extract_bottleneck_features import *
def Resnet50_predict_breed(img_path):
bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
predicted_vector = Resnet50_model.predict(bottleneck_feature)
return dog_names[np.argmax(predicted_vector)]
def LastPredict(img_path):
img = cv2.imread(img_path)
cv_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(cv_rgb)
plt.show()
if face_detector(img_path) > 0:
print("Hello, Human")
print("You look like a ... in dog world")
print(Resnet50_predict_breed(img_path))
elif dog_detector(img_path) == True:
print("Hello, Dog")
print("You are a ... ")
print(Resnet50_predict_breed(img_path))
else:
print("Error Input")
(1)6张狗狗:只有第一张被误判为人类,但是检测的相似狗狗对了。另外5张没有错误。
(2)5张人的图片:5张没有误判的。另外,我像Poodle。
(3)3张猫咪:第二张错误,被误判为人类。其他2张正确。
相关知识
【深度学习图像识别课程】毕业项目:狗狗种类识别(4)代码实现
探索AIDog:智能狗狗识别与行为理解的开源项目
深度学习卷积神经图像分类实现鸟类识别含训练代码和鸟类数据集(支持repVGG,googlenet, resnet, inception, mobilenet)
基于深度学习的鸟类识别系统(网页版+YOLOv8/v7/v6/v5代码+训练数据集)
详解pytorch实现猫狗识别98%附代码
毕业设计:基于深度学习的动物叫声识别系统
PyTorch深度学习:猫狗情感识别
PyTorch猫狗:深度学习在宠物识别中的应用
R语言深度学习玩转宠物世界:宠物识别与品种分类
鸟类声音识别技术综述:从传统方法到深度学习
网址: 【深度学习图像识别课程】毕业项目:狗狗种类识别(4)代码实现 https://m.mcbbbk.com/newsview273644.html
上一篇: 宠物领养馆赚钱攻略:宠物用品、护 |
下一篇: 小宠物的互动设施在这里 |