Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neural network 如何在Pycafe中进行预测?_Neural Network_Caffe - Fatal编程技术网

Neural network 如何在Pycafe中进行预测?

Neural network 如何在Pycafe中进行预测?,neural-network,caffe,Neural Network,Caffe,我有一个已经接受过CIFAR-10培训的专家,但我不知道如何在Pycafe中进行预测 我从lmdb获得了一张图像,但我不知道如何将其加载到网络中并获得一个预测类 我的代码: net = caffe.Net('acc81/model.prototxt', 'acc81/cifar10_full_iter_70000.caffemodel.h5', caffe.TEST) lmdb_env = lmdb.open('cifar10_t

我有一个已经接受过CIFAR-10培训的专家,但我不知道如何在Pycafe中进行预测

我从lmdb获得了一张图像,但我不知道如何将其加载到网络中并获得一个预测类

我的代码:

net = caffe.Net('acc81/model.prototxt',
                'acc81/cifar10_full_iter_70000.caffemodel.h5',
                 caffe.TEST)
lmdb_env = lmdb.open('cifar10_test_lmdb/')
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
for key, value in lmdb_cursor:
    datum = caffe.proto.caffe_pb2.Datum()
    datum.ParseFromString(value)
    image = caffe.io.datum_to_array(datum)
    image = image.astype(np.uint8)

    # What's next with the image variable?
    # If i try:
    # out = net.forward_all(data=np.asarray([image]))
    # I get Exception: Input blob arguments do not match net inputs.

    print("Image class is " + label)
使用这个python脚本

# Run the script with anaconda-python
# $ /home/<path to anaconda directory>/anaconda/bin/python LmdbClassification.py
import sys
import numpy as np
import lmdb
import caffe
from collections import defaultdict
caffe.set_mode_gpu()

# Modify the paths given below
deploy_prototxt_file_path = '/home/<username>/caffe/examples/cifar10/cifar10_deploy.prototxt' # Network definition file
caffe_model_file_path = '/home/<username>/caffe/examples/cifar10/cifar10_iter_5000.caffemodel' # Trained Caffe model file
test_lmdb_path = '/home/<username>/caffe/examples/cifar10/cifar10_test_lmdb/' # Test LMDB database path
mean_file_binaryproto = '/home/<username>/caffe/examples/cifar10/mean.binaryproto' # Mean image file

# Extract mean from the mean image file
mean_blobproto_new = caffe.proto.caffe_pb2.BlobProto()
f = open(mean_file_binaryproto, 'rb')
mean_blobproto_new.ParseFromString(f.read())
mean_image = caffe.io.blobproto_to_array(mean_blobproto_new)
f.close()

# CNN reconstruction and loading the trained weights
net = caffe.Net(deploy_prototxt_file_path, caffe_model_file_path, caffe.TEST)

count = 0
correct = 0
matrix = defaultdict(int) # (real,pred) -> int
labels_set = set()

lmdb_env = lmdb.open(test_lmdb_path)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()

for key, value in lmdb_cursor:
    datum = caffe.proto.caffe_pb2.Datum()
        datum.ParseFromString(value)
        label = int(datum.label)
        image = caffe.io.datum_to_array(datum)
        image = image.astype(np.uint8)
    out = net.forward_all(data=np.asarray([image]) - mean_image)
    plabel = int(out['prob'][0].argmax(axis=0))
    count += 1
    iscorrect = label == plabel
    correct += (1 if iscorrect else 0)
    matrix[(label, plabel)] += 1
    labels_set.update([label, plabel])

    if not iscorrect:
            print("\rError: key = %s, expected %i but predicted %i" % (key, label, plabel))
        sys.stdout.write("\rAccuracy: %.1f%%" % (100.*correct/count))
        sys.stdout.flush()

print("\n" + str(correct) + " out of " + str(count) + " were classified correctly")
print ""
print "Confusion matrix:"
print "(r , p) | count"
for l in labels_set:
    for pl in labels_set:
        print "(%i , %i) | %i" % (l, pl, matrix[(l,pl)])
#使用anaconda python运行脚本
#$/home//anaconda/bin/python LmdbClassification.py
导入系统
将numpy作为np导入
导入lmdb
进口咖啡
从集合导入defaultdict
caffe.set_mode_gpu()
#修改下面给出的路径
deploy_prototxt_file_path='/home//caffe/examples/cifar10/cifar10_deploy.prototxt'#网络定义文件
caffe_model_file_path='/home//caffe/examples/cifar10/cifar10_iter_5000.caffemodel'#经过培训的caffe模型文件
test_lmdb_path='/home//caffe/examples/cifar10/cifar10_test_lmdb/'#test lmdb数据库路径
mean_file_binaryproto='/home//caffe/examples/cifar10/mean.binaryproto'#平均图像文件
#从平均图像文件中提取平均值
mean_blobproto_new=caffe.proto.caffe_pb2.blobproto()
f=打开(平均\u文件\u二进制原型'rb')
mean_blobproto_new.ParseFromString(f.read())
mean_image=caffe.io.blobproto_数组(mean_blobproto_new)
f、 关闭()
#CNN重构与训练权重加载
net=caffe.net(部署文件路径,caffe模型文件路径,caffe.TEST)
计数=0
正确=0
矩阵=defaultdict(int)#(实数,pred)->int
标签\u set=set()
lmdb_env=lmdb.open(测试路径)
lmdb_txn=lmdb_env.begin()
lmdb_cursor=lmdb_txn.cursor()
对于键,lmdb_光标中的值:
datum=caffe.proto.caffe_pb2.datum()
datum.ParseFromString(值)
标签=整数(基准标签)
image=caffe.io.datum\u到数组(datum)
image=image.astype(np.uint8)
out=净前进\全部(数据=np.asarray([图像])-平均值\图像)
plabel=int(out['prob'][0].argmax(axis=0))
计数+=1
iscorrect=label==plabel
正确+=(如果正确则为1,否则为0)
矩阵[(标签,平板)]+=1
标签\u set.update([label,plabel])
如果不正确:
打印(“\r错误:键=%s,应为%i,但应为%i”%(键、标签、标签))
sys.stdout.write(“\r精度:%.1f%%”%(100.*正确/计数))
sys.stdout.flush()
打印(“\n”+str(正确)+”中的“+str(计数)+”已正确分类)
打印“”
打印“混淆矩阵:”
打印“(r,p)|计数”
对于标签集合中的l:
对于标签集合中的pl:
打印“(%i,%i)|%i”%(l,pl,矩阵[(l,pl)])
查看如何将培训协议转换为部署协议。