没问题!
第一步:安装您选择的深度学习工具包。这些天他们都有很好的教程。
第二步:获取预训练的 imagenet 模型。在该模型中,已经内置了一些计算机类! (“desktop_computer”、“laptop”、“notebook”,以及手持计算机的另一个类“hand-held_computer”)
第三步:使用模型进行预测。为此,您需要使图片尺寸正确。
更多步骤:进一步微调模型...更高级一些,但会给您一些收获。
要考虑的是你的目标是什么?准确性?误报/负数等?从一开始就确定你需要完成的目标总是好的。
编辑:可能最简单的入门方法(如果您没有库、gpu 等)是转到 google colab (https://colab.research.google.com/notebooks/welcome.ipynb) 并在浏览器中创建一个笔记本并运行以下代码。
#some code take and modded from https://www.learnopencv.com/keras-tutorial- using-pre-trained-imagenet-models/
import keras
import numpy as np
from keras.applications import vgg16
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
%matplotlib inline
vgg_model = vgg16.VGG16(weights='imagenet')
def predict_image(image_url, model):
response = requests.get(image_url)
original = Image.open(BytesIO(response.content))
newsize = (224, 224)
original = original.resize(newsize)
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular axis
# We want the input matrix to the network to be of the form (batchsize, height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
plt.imshow(np.uint8(image_batch[0]))
plt.show()
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = model.predict(processed_image)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
label = decode_predictions(predictions)
print label[0][0:2] #just display top 2
urls = ['https://4.imimg.com/data4/CO/YS/MY-29352968/samsung-desktop-computer-500x500.jpg', 'https://cdn.britannica.com/77/170477-050-1C747EE3/Laptop-computer.jpg']
for u in urls:
predict_image(u, vgg_model)
这应该是一个很好的起点。哦,如果最上面的预测标签不在计算机、笔记本电脑等集合中,那么它就不是计算机!