def testImage(self,image_location,image_size):
mean = self.readMeanFile()
image = ImageProcess(image_location,image_size,mean).getImage()
fileOp = FileOperation()
#1.read the pb file as a string
init_path = self.MODEL_PATH+"/"+self.MODEL_NAME+"/"+self.INIT_NAME
#print(init_path)
init_pb = fileOp.readFile(filePath = init_path ,openMode = "rb")
predict_path = self.MODEL_PATH+"/"+self.MODEL_NAME+"/"+self.PREDICT_NAME
predict_pb = fileOp.readFile(filePath = predict_path ,openMode = "rb")
workspace.ResetWorkspace()
#2.use the string to generate an instance of Net
init_net = caffe2_pb2.NetDef()
init_net.ParseFromString(init_pb)
init_net.name = unicode("test_init")
#print("wb->",workspace.StringifyNetName("test_init"))
#init_net = Net(init_net)
predict_net = caffe2_pb2.NetDef()
predict_net.ParseFromString(predict_pb)
#print(predict_net)
predict_net.name = unicode("test")
workspace.RunNetOnce(init_net)
workspace.FeedBlob(unicode("data"),image)
workspace.CreateNet(predict_net)
workspace.RunNet(predict_net.name)
output = predict_net.external_output
#print("run predict and fetch output ",workspace.FetchBlob(output[0]))
return workspace.FetchBlob(output[0])
分析:be patient and be smart
def testImage(self,image_location,image_size):
#1. 读取核心文件的内容:图片文件、init_net 和 predict_net;后两个文件读出的是protobuf 格式的string,需要用特定的_pb2.py文件进行解析。
mean = self.readMeanFile()
image = ImageProcess(image_location,image_size,mean).getImage()#读图片内容
fileOp = FileOperation()
init_path = self.MODEL_PATH+"/"+self.MODEL_NAME+"/"+self.INIT_NAME
init_pb = fileOp.readFile(filePath = init_path ,openMode = "rb")#读init
predict_path = self.MODEL_PATH+"/"+self.MODEL_NAME+"/"+self.PREDICT_NAME
predict_pb = fileOp.readFile(filePath = predict_path ,openMode = "rb")#读predict
#2. workspace中存放了大量的blobs,每个blob都是{key,value}的形式:key是blob的名称,value是一个叫做tensor的数据结构,类似于python中的array。
2.1 worspace的作用
Similar to Matlab, the Caffe2 workspace consists of blobs you create and store in memory. A blob is actually a typed pointer that can store any type of C++ objects, but Tensor is the most common type stored in a blob.
https://github.com/caffe2/tutorials/blob/master/Basics.ipynb
https://caffe2.ai/docs/tutorial-basics-of-caffe2.html
workspace.ResetWorkspace()#置空当前的workspace。
#2.use the string to generate an instance of Net
init_net = caffe2_pb2.NetDef()
init_net.ParseFromString(init_pb)
init_net.name = unicode("test_init") #创建pb的过程中缺失了filed name,这里需要补充上。如果不明确给出name,将pb转为ND对象时,ND会自动对name赋值。
predict_net = caffe2_pb2.NetDef()
predict_net.ParseFromString(predict_pb)
predict_net.name = unicode("test")
#3.真正的运行net:Then there are two ways to run a net from Python. We will do the first option in the example below.
- Call
workspace.RunNetOnce()
, which instantiates, runs and immediately destructs the network。 - Call
workspace.CreateNet()
to create the C++ net object owned by the workspace, then callworkspace.RunNet()
, passing the name of the network to it
workspace.RunNetOnce(init_net) #所有的input和output内容都实例化成一个blob,存放到了workspace里。但是,data字段没有填入内容。
workspace.FeedBlob(unicode("data"),image)#把image信息填入到data字段
workspace.CreateNet(predict_net)#实例化对象,创建对应的blob,并挂载到workspace上。
workspace.RunNet(predict_net.name)#运行net
output = predict_net.external_output
#print("run predict and fetch output ",workspace.FetchBlob(output[0]))#取对应的结果
return workspace.FetchBlob(output[0])