使用训练好的caffemodel,对测试图像做自动分类预测

      笔者硕士阶段论文研究方向为:基于卷积神经网络的绘画图像分类研究, 需要使用训练好的caffemodel,对测试绘画图像进行分类和预测其类别。

      本测试程序依赖python版本的caffe,需要将binaryproto格式均值文件转化为npy格式均值文件,代码如下:

#coding=utf-8

import sys
sys.path.append("D:\\Anaconda2\\libs")

import caffe
import numpy as np

MEAN_PROTO_PATH = 'huihua_dongfang_mean.binaryproto'               # 待转换的pb格式图像均值文件路径
MEAN_NPY_PATH = 'huihua_dongfang_mean.npy'                         # 转换后的numpy格式图像均值文件路径

blob = caffe.proto.caffe_pb2.BlobProto()           # 创建protobuf blob
data = open(MEAN_PROTO_PATH, 'rb' ).read()         # 读入mean.binaryproto文件内容
blob.ParseFromString(data)                         # 解析文件内容到blob

array = np.array(caffe.io.blobproto_to_array(blob))# 将blob中的均值转换成numpy格式,array的shape (mean_number,channel, hight, width)
mean_npy = array[0]  
print mean_npy                              # 一个array中可以有多组均值存在,故需要通过下标选择其中一组均值
np.save(MEAN_NPY_PATH ,mean_npy)

下面对测试图像进行预测,并将测试图像保存到不同的阈值区间,代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append("D:\\Anaconda2\\libs")
import caffe
import cv2
import numpy as np
import os



#c根目录
root = 'E:/mtcnn_DuinoDu/lunwen_test_class/'
#测试阶段用的网络模型文件
deploy = root + 'deploy.prototxt'
#训练好的caffemodel,用于预测图片属于各个朝代的概率
caffe_model = root + 'V1_6w.caffemodel'
#训练图像均值文件
mean_file = root + 'huihua_dongfang_mean.npy' 
#类别名称文件,将数字标签转换为类别名称
labels_filename = "G:/huihua_shiyan/tain-data-sample/" + 'dir.txt'

net = caffe.Net(deploy,caffe_model,caffe.TEST)   #加载model和network

#图片预处理设置
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})  #设定图片blob为(1,3,227,227)
transformer.set_transpose('data', (2,0,1))    #改变维度顺序,由原始图片(227,227,3)变为(3,227,227)
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))    #减去均值
transformer.set_raw_scale('data', 256)    # 缩放到[0,256]之间
transformer.set_channel_swap('data', (2,1,0))   #交换通道,将图片由RGB变为BGR

num_all = 0
num_true = 0

f = open(labels_filename, 'r')
for line in f.readlines():
    line = line.strip('\n').split(' ')
    img_name = line[0]
    #label = int(line[1])
    num_all = num_all + 1
    print num_all
    img_dir_name = "G:/huihua_shiyan/tain-data-sample/"  + img_name
    image = cv2.imread(img_dir_name, -1)
    if image == None:
        continue
    img = caffe.io.load_image(img_dir_name)
    net.blobs['data'].data[...] = transformer.preprocess('data',img)
    out = net.forward()

    prob = net.blobs['prob'].data[0].flatten()
    order = prob.argsort()[-1]
    # if order == label:
    #     num_true = num_true + 1
    score_thord1 = 0.7
    score_thord2 = 0.8
    if order == 0:
        if prob[order] >= score_thord1 and prob[order] < score_thord2:
            save_dir = "G:/huihua_shiyan/selective_data_0.7_0.8" + "/flowers/" + str(num_all) + ".jpg"
            cv2.imwrite(save_dir, image)
    if order == 1:
        if prob[order] >= score_thord1 and prob[order] < score_thord2:
            save_dir = "G:/huihua_shiyan/selective_data_0.7_0.8"  + "/people/" + str(num_all) + ".jpg"
            cv2.imwrite(save_dir, image)
    if order == 2:
        if prob[order] >= score_thord1 and prob[order] < score_thord2:
            save_dir = "G:/huihua_shiyan/selective_data_0.7_0.8"  + "/landscape/" + str(num_all) + ".jpg"
            cv2.imwrite(save_dir, image)


print "num_true: " , num_true
print "num_all: " , num_all
print "precision: " , float(num_true) / float(num_all)

f.close()



在python脚本运行该程序就可以实现自动分类预测。

你可能感兴趣的:(CNN图像分类,程序代码)