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:
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.')
Here’s the main code:
model = FishRecogModel("use")
#model.register_coco()
cap = cv2.VideoCapture(3)
while(True):
ret, frame = cap.read()
model.predict(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Can somebody help me out in figuring out why the ‘Please load the model first.’ keeps being printed out?