首页 > 解决方案 > 在多个图像上运行保存的训练模型

问题描述

我在 tensorflow 中有一个深度学习 CNN 分类器,我已经对其进行了训练并保存到了磁盘中。

目前,我一次只在一张图像上运行模型。但是,我想将其自动化以对一批 100 多张(如果不是 1000 张)图像进行分类。

有没有办法做到这一点并记录输出?下面是我目前实现这一目标的代码块。

非常感激。

# since we are using Jupyter Notebooks we can replace our argument
# parsing code with *hard coded* arguments and values
args = {
    "model": "pokedex.model",
    "labelbin": "lb.pickle",
    "image": "examples/Roxy.jpg"
}

# load the image
image = cv2.imread(args["image"])
output = image.copy()

# pre-process the image for classification
image = cv2.resize(image, (96, 96))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)

# load the trained convolutional neural network and the label
# binarizer
print("[INFO] loading network...")
model = load_model(args["model"])
lb = pickle.loads(open(args["labelbin"], "rb").read())

# classify the input image
print("[INFO] classifying image...")
proba = model.predict(image)[0]
idx = np.argmax(proba)
label = lb.classes_[idx]

# we'll mark our prediction as "correct" of the input image filename
# contains the predicted label text (obviously this makes the
# assumption that you have named your testing image files this way)
filename = args["image"][args["image"].rfind(os.path.sep) + 1:]
correct = "correct" if filename.rfind(label) != -1 else "none_detected"

# build the label and draw the label on the image
label = "{}: {:.2f}% ({})".format(label, proba[idx] * 100, correct)
output = imutils.resize(output, width=400)
cv2.putText(output, label, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX,
    0.7, (0, 255, 0), 2)

# show the output image
print("[INFO] {}".format(label))
plt_imshow("Output", output)

标签: deep-learning

解决方案


推荐阅读