Loading model using detectron 2

username: Raza_Nayaz
cooked: <p>I am trying to load a trained model using a code that my boss gave me to use. However, there is a slight issue. It keeps saying ‘Please load the model first.’ which is supposed to be printed as an except while trying to load the model. I set the directory of the pth file properly, but it still doesn’t work. Here’s the code for the loading part:</p>
<pre><code class="lang-auto">                def load_model(self):
                    try:
                        model_path= r"C:\GUI\output\model_final.pth" #model_final.pth
                        self.cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
                        self.cfg.MODEL.ROI_HEADS.NUM_CLASSES = 3
                        self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.8
                        self.cfg.MODEL.WEIGHTS = os.path.join(self.cfg.OUTPUT_DIR, 'model_final.pth')
                        return DefaultPredictor(self.cfg)
                    except:
                        print('There is no model exist, please train the model first.')
                        return -1
                
                def predict(self, frame):
                    try:
                        outputs = self.predictor(frame)
                        # print position info
                        result = self.fish_position(outputs)
                        print(result)
                        # draw the 
                        v = self.draw_predict_position(frame,outputs)
                        return v.get_image()[:, :, ::-1]
                    # cv2.imshow('frame', v.get_image()[:, :, ::-1]) 
                    except:
                        print('Please load the model first.')

</code></pre>
<p>Here’s the main code:</p>
<pre><code class="lang-auto">                           model = FishRecogModel("use")
                                #model.register_coco()

                                cap = cv2.VideoCapture(3)
                                while(True):
                                    ret, frame = cap.read()
                                    model.predict(frame)
                                    if cv2.waitKey(1) &amp; 0xFF == ord('q'):
                                        break
                                cap.release()
                                cv2.destroyAllWindows()
</code></pre>
<p>Can somebody help me out in figuring out why the ‘Please load the model first.’ keeps being printed out?</p>

username: ptrblck
cooked: <p>Remove the <code>try/except</code> part and allow Python to raise the actual error. Catching <em>all</em> exceptions without re-raising the error is usually not a good idea, since now you don’t know what is actually failing.<br>
Based on the code snippet it also seems the exception is raised during the <code>predict</code> call and might be failing in any of these lines:</p>
<pre><code class="lang-python">                    try:
                        outputs = self.predictor(frame)
                        # print position info
                        result = self.fish_position(outputs)
                        print(result)
                        # draw the 
                        v = self.draw_predict_position(frame,outputs)
                        return v.get_image()[:, :, ::-1]
</code></pre>

username: Raza_Nayaz
cooked: <p>I tried to find the error. It says failed to load plugin gstreamer-1.0\gstogg.dll’ module<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/0/70dc0752defacbaf8bf859e19c4a1cd26677f436.png" data-download-href="https://discuss.pytorch.org/uploads/default/70dc0752defacbaf8bf859e19c4a1cd26677f436" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/0/70dc0752defacbaf8bf859e19c4a1cd26677f436.png" alt="image" data-base62-sha1="g6oSV3b0ljxatauli3BYS5nRZvo" width="690" height="24" data-dominant-color="2E2E2E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1752×61 6.65 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Any idea why? The module is there in the correct folder though.</p>

username: ptrblck
cooked: <p>It seems your <code>gstreamer</code> installation might be broken. I don’t know how exactly you are using it, but it seems your application depends on this library so you might need to fix it.</p>