facebookarchive / tutorials

Caffe2 Tutorials
Apache License 2.0
124 stars 60 forks source link

test one image for MNIST #4

Open ycAlex11 opened 6 years ago

ycAlex11 commented 6 years ago

I went through the tutorial of MNIST and loading squeezenet,, after I run the code , it generates a few of pbtxt files. can any one tell me would it possible to test only one image by using these pbtxt files. If it is possible, which pbtxt I should load? Is it using workspace.Predictor() and run() function loading into model?

cheers

MatthewInkawhich commented 6 years ago

Hello @ycAlex11!

The easiest way to run inference on a single image using your model's protobuf files is by using workspace.Predictor() like this:

# Bring up the network with the .pb files
with open(init_net_path, 'rb') as f:
    INIT_NET = f.read()
with open(predict_net_path, 'rb') as f:
    PREDICT_NET = f.read()

# Create Predictor object
p = workspace.Predictor(INIT_NET, PREDICT_NET)

# Test img_data (make sure it is correct shape)
results = p.run([img_data])

# results contains softmax results
results = np.asarray(results)
ycAlex11 commented 6 years ago

@MatthewInkawhich thanks for your reply, I have figured it out. but I got another problem, I am trying to save model for RNN model(char_rnn.py in caffe2), this way looks does not work !!!

orionr commented 6 years ago

Hi @ycAlex11 - what particular issue are you seeing on saving out the char_rnn model? Can you post more details here? Thanks.

ycAlex11 commented 6 years ago

@orionr Hi dude, I can not remember what was showing on terminal !! It is a holiday in China now and the code is on the host, I will look it again when I back to work. I'd like to save the model, load the model and give a input string so can generate few words follow it. The whole problem is I don't know how to save the model'weight and set the input to the model. I was trying saving by INIT_NET and PREDICT_NET. I guess predict_net.pb is forward_net(in Char_rnn). By now I could save sth into INIT_NET and PREDICT_NET, but got error for loading. So I guess it has problem during saving! I hope it does make sense. My writing is getting worse since I have graduated from uni. I am not sure it would possible to saving the char_rnn model and give a input to generate following string. If so could you give me a hint or some useful reference? Cheers

parth1595 commented 5 years ago

Hi @MatthewInkawhich

I have tried the code which you provided in the comment for inference of caffe2 cifar model but it throws some error do you know why this error occurs ?

Error: File "inference.py", line 24, in p = workspace.Predictor(INIT_NET, PREDICT_NET) File "/home/anaconda2/lib/python2.7/site-packages/caffe2/python/workspace.py", line 159, in Predictor return C.Predictor(StringifyProto(init_net), StringifyProto(predict_net)) RuntimeError: [enforce fail at workspace.cc:222] net_def->has_name(). Net definition should have a name.

Here is the code:

import caffe2.python.predictor.predictor_exporter as pe from caffe2.proto import caffe2_pb2 from caffe2.python.predictor import mobile_exporter from caffe2.python import ( brew, core, model_helper, net_drawer, optimizer, visualize, workspace, )

with open('cifar10_init_net.pb', 'rb') as f: INIT_NET = f.read() with open('cifar10_init_net.pb', 'rb') as f: PREDICT_NET = f.read()

p = workspace.Predictor(INIT_NET, PREDICT_NET)

results = p.run([img_data])

results = np.asarray(results

@ycAlex11