猫也能明白 Day3
在上一篇博客中介绍了对图像进行分类,接下来看如何对图像进行检测。
方法一: 使用代码扫描图片(不重叠)
import time
import numpy as np
import matplotlib.pyplot as plt
import caffe
%matplotlib inline
MODEL_JOB_DIR = '工作目录'
DATASET_JOB_DIR = '数据集目录'
MODEL_FILE = MODEL_JOB_DIR + '/网络.prototxt'
PRETRAINED = MODEL_JOB_DIR + '/权重.caffemodel'
MEAN_IMAGE = DATASET_JOB_DIR + '/均值图像.jpg'
caffe.set_mode_gpu()
net = caffe.Classifier(MODEL_FILE, PRETRAINED,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
mean_image = caffe.io.load_image(MEAN_IMAGE)
IMAGE_FILE = '需要扫描的图像.png'
input_image= caffe.io.load_image(IMAGE_FILE)
rows = input_image.shape[0]/256
cols = input_image.shape[1]/256
detections = np.zeros((rows,cols))
start = time.time()
for i in range(0,rows):
for j in range(0,cols):
grid_square = input_image[i*256:(i+1)*256,j*256:(j+1)*256]
grid_square -= mean_image
prediction = net.predict([grid_square])
detections[i,j] = prediction[0].argmax()
plt.imshow(detections, interpolation=None)
打开App,阅读手记