我正在使用label_image.py来预测训练有素的图像类别,并进行了一些调整以使其在整个图像目录中运行,而不仅仅是一个目录。 一次只能运行一张图像。
我已经训练了新的课程“ 车辆” ,其中包含4个子类别的“ 2轮车,4轮车,免税车辆和重型车辆”
我正在针对每个预测类别打印特定数量的关税。
在下面共享我的代码:
graph = load_graph(model_file)
labels = load_labels(label_file)
setup_dirs(labels)
data_list = []
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name)
output_operation = graph.get_operation_by_name(output_name)
image_list = os.listdir(image_dir)
with open(output_file_name, 'w') as myfile:
wr = csv.writer(myfile)
wr.writerow(['Results','Timestamp','Category','Price'])
for i, image in enumerate(image_list):
image_time = image.split('.')[0].replace('_', ':')
image_path = "%s/%s" % (image_dir, image)
t = read_tensor_from_image_file(image_path,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
with tf.Session(graph=graph) as sess:
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
print('\nProgress: {:.2f}%'.format(i /len(image_list) * 100))
if top_k[0]==0:
# 2 wheeler
data = [results[0], image_time,labels[0],20]
elif top_k[0]==1:
# 4 wheeler
data = [results[1], image_time,labels[1],40]
elif top_k[0]==2:
#exempt vehicle
data = [results[2], image_time,labels[2],0]
elif top_k[0]==3:
#heavy vehicle
data = [results[3], image_time,labels[3],80]
print(data)
wr.writerow(data)
myfile.flush()
从上述代码共享我的示例输出: 结果时间戳记类别价格
0.9735643图片:001 2惠勒20
0.7835166图片:003 2惠勒20
0.9380553图片:004 2惠勒20
0.5584447图片:005 2轮车20
0.96052194图片:007 2惠勒20
实际输出应已显示根据图片预测的实际类别。 PS: 此代码在单个图像上运行良好,这排除了该模型未受过良好训练以正确识别类别的可能性