Caffe框架,训练model并测试数据

14 篇文章 0 订阅
9 篇文章 0 订阅

1. 训练model

#!/usr/bin/env sh

./build/tools/caffe train --solver=examples/focal_length/focal_solver.prototxt 

2. 测试数据

import caffe
from caffe.proto import caffe_pb2
import numpy as np
import cv2

run_mode = 'gpu'
deploy_file = 'focal_deploy.prototxt'
weight_file = 'focal_iter_5000.caffemodel'
data_path = '/home/lei/project/caffe/caffe-master/data/focal_length/test/'
list_file = 'list.txt'


# init the caffenet
if run_mode == 'gpu':
	caffe.set_device(0)
	caffe.set_mode_gpu()
elif run_mode == 'cpu':
	caffe.set_mode_cpu()
net = caffe.Net(deploy_file, weight_file, caffe.TEST)


# mean array
mean_file      = '/home/lei/project/caffe/caffe-master/data/ilsvrc12/imagenet_mean.binaryproto'
mean_blobproto = caffe_pb2.BlobProto()
fid            = open(mean_file, 'rb')
mean_blobproto.ParseFromString( fid.read() )
mean_array     = caffe.io.blobproto_to_array( mean_blobproto )
mean_array     = mean_array.astype( np.float32 )
mean_array     = np.zeros(mean_array.shape, mean_array.dtype)



with open(data_path + list_file) as fid:
	for key in fid.readlines():
		filename = ( data_path + key ).strip()
		
		img     = cv2.imread(filename)
		cv2.imshow('name', img)
		cv2.waitKey(500)
		img     = img.astype( np.float32 )
		data_in = img.reshape( (1,256,256,3) )
		data_in = data_in.transpose((0,3,1,2))
		data_in = data_in - mean_array
		data_in = data_in * 0.00390625

		netout = net.forward_all(data=data_in)
		guess  = netout['prob'].argmax(axis=1) # the type is np.int64
		prob   = netout['prob'].max(axis=1)

		print '{}: guess: {}, prob: {}'.format(key, guess[0], prob[0])





# /home/lei/project/caffe/caffe-master/examples/focal_length


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值