说说如何写基于caffemodel的python测试脚本

很多教程对如何去测试caffemodel介绍的不够具体,其实在我看来如何去测试caffemodel其实很简单,比如下面一段代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import numpy as np
import argparse
import sys
import cv2

caffe_root = '/home/zhao/opt/caffe/build/install'
sys.path.insert(0, caffe_root + 'python')
import caffe
sys.path.append('/usr/local/lib/python2.7/site-packages')

def make_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model', type=str, required=True, help='.prototxt file for inference')
    parser.add_argument('--weights', type=str, required=True, help='.caffemodel file')
    parser.add_argument('--list', type=str, default='/home/zhao/vp/log.txt', help='input image list for processing')
if __name__ == '__main__':
    parser1 = make_parser()
    args = parser1.parse_args()

    caffe.set_mode_gpu()
    net = caffe.Net(args.model, args.weights, caffe.TEST)
    input_shape = net.blobs['data'].data.shape
    img_row_start = 0
    img_height_in = 0
    resize_height = 320
    resize_width = 640
    lines = [line.strip() for line in open(args.list, 'r').readlines()]
    for line in lines:
        input_image = cv2.imread(line, 1)
        input_image = input_image[img_row_start:img_row_start + img_height_in, :, :]
        input_image = cv2.resize(input_image, (resize_width, resize_height))
        input_image2 = input_image.transpose((2, 0, 1)).astype(np.float32)
        out = net.forward_all(**{net.inputs[0]: input_image2})
        data_pt_y = net.blobs['pt_y'].data[0]
        data_pt_score = net.blobs['pt_score'].data[0]
        。。。。。。
        。。。。。。继续后处理操作即可


 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页