Repository: pytorch
Issue: how can i run two model in a simple project?
Labels: caffe2

I have two models trained with caffe2 - detectron , i  have convert the detectron model( pkl) to caffe2 models(pbtxt), and i have test the models succsssfully.
but i need run the two models one by one in a same project , so i can get the infor of the image,
but here is the problem
how can i Realization the function ? 
what i want realization most like this,
class get_info:
    def _init_():
          init the network
    def classify_img():
          get_img_info()
main():
  model_a = get_info()
  model_b = get_info()
  for( listdir()):
      model_a.classify_img()
      model_b.classify_img()
but now i can't realization this , here is my code 

class caffe2DetectronClassify:
    def _create_input_blobs_for_net(self,net_def):
        for op in net_def.op:
            for blob_in in op.input:
                if not workspace.HasBlob(blob_in):
                    workspace.CreateBlob(blob_in)
    def __init__(self, path_model_init, path_model_weights, scale, max_size, mean_file,root_dir):
        detectron_ops_lib = _get_detectron_ops_lib()
        dyndep.InitOpsLibrary(detectron_ops_lib)

        self.init_net = caffe2_pb2.NetDef()
        with open(path_model_init, 'rb') as f:
            self.init_net.ParseFromString(f.read())
        self.net = caffe2_pb2.NetDef()
        with open(path_model_weights, 'rb') as f:
            self.net.ParseFromString(f.read())

        self.scale = scale
        self.max_size = max_size
        self.mean_file = mean_file

        **workspace.ResetWorkspace()**
        workspace.RunNetOnce(self.init_net)
        self._create_input_blobs_for_net(self.net)
        workspace.CreateNet(self.net)
        print("net create over ...")


    def img_classify(self,img_path):
        if(not os.path.exists(img_path)):
            return [],[],[]
        im = cv2.imread(img_path)
        if(im.any() == None):
            print('None image')
            return [],[],[]
        input_blob = self._prepare_blobs(im,self.mean_file,self.scale,self.max_size)
        gpu_blobs = ['data']
        for k,v in input_blob.items():
            workspace.FeedBlob(
                core.ScopedName(k),
                v,
                self._get_device_option_cuda() if k in gpu_blobs else
                self._get_device_option_cpu()
            )
        try:
            workspace.RunNet(self.net.name)
            scores = workspace.FetchBlob('score_nms')
            classids = workspace.FetchBlob('class_nms')
            boxes = workspace.FetchBlob('bbox_nms')
        except Exception as e:
            print('error with {}'.format(e))
            R = 0
            scores = np.zeros((R,), dtype=np.float32)
            boxes = np.zeros((R, 4), dtype=np.float32)
            classids = np.zeros((R,), dtype=np.float32)
        return scores,classids,boxes

when i run ,got error

Found Detectron ops lib: /home/jansonm/caffe2/pytorch/build/lib/libcaffe2_detectron_ops_gpu.so
WARNING: Logging before InitGoogleLogging() is written to STDERR
W0825 16:40:33.968032 21053 init.h:99] Caffe2 GlobalInit should be run before any other API calls.
W0825 16:40:34.370723 21053 init.h:99] Caffe2 GlobalInit should be run before any other API calls.
net create over ...
Found Detectron ops lib: /home/jansonm/caffe2/pytorch/build/lib/libcaffe2_detectron_ops_gpu.so
net create over ...
++++++++++++++++++++++++++++++++++++++++++++++++
/home/jansonm/detectron/test_img/5498233_1012130_1530585802475_10606738.jpeg
[]
pre imag time:1.59693694115
-----------------------------------------------
No handlers could be found for logger "caffe2.python.workspace"
error with [enforce fail at pybind_state.cc:1054] gWorkspace->GetNet(name). Can't find net detectron_detect_box 
[]
pre imag time:0.141842126846
++++++++++++++++++++++++++++++++++++++++++++++++
sh: pause: command not found
++++++++++++++++++++++++++++++++++++++++++++++++
/home/jansonm/detectron/test_img/5498235_1023198_1530580487026_19862435.jpeg
[]
pre imag time:0.643126964569
-----------------------------------------------
error with [enforce fail at pybind_state.cc:1054] gWorkspace->GetNet(name). Can't find net detectron_detect_box 
[]
pre imag time:0.144562005997
++++++++++++++++++++++++++++++++++++++++++++++++
sh: pause: command not found

you see , the second model can not work, can not found its model by name
i think it is because when init the model
 **workspace.ResetWorkspace()**
        workspace.RunNetOnce(self.init_net)
        self._create_input_blobs_for_net(self.net)
        workspace.CreateNet(self.net)
when i init workspace.ResetWorkspace() , it recover the first model, how can i let then work at the same time in a sample project,??
@eklitzke 
@gchanan