YOLOX_deepsort_tracker icon indicating copy to clipboard operation
YOLOX_deepsort_tracker copied to clipboard

raise ImportError("{} doesn't contains class named 'Exp'".format(exp_file))

Open Sarouch opened this issue 3 years ago • 5 comments

Hello, I have this error, could you please help me to find out the problem ?

File "track.py", line 33, in init self.detector=build_detector(cfg,use_cuda=use_cuda) File "\yolox-pytorch\detector.py", line 71, in build_detector model= Detector(cfg.YOLOX.MODEL, cfg.YOLOX.WEIGHT) File "\yolox-pytorch\detector.py", line 31, in init self.exp = get_exp_by_name(model) File "\yolox-pytorch\exp\build.py", line 36, in get_exp_by_name return get_exp_by_file(exp_path) File "\yolox-pytorch\exp\build.py", line 17, in get_exp_by_file raise ImportError("{} doesn't contains class named 'Exp'".format(exp_file)) ImportError: \yolox-pytorch\exp\custom.py doesn't contains class named 'Exp

' Thank you

Sarouch avatar Nov 17 '21 20:11 Sarouch

It seems that maybe you changed the project structure and the program could not find the path of the exp file. You can change detector.py to solve it.

Firstly import another function to load exp-file:

from YOLOX.yolox.exp.build import get_exp_by_file    # detector.py   line 9

And then change self.exp = get_exp_by_name(model) to self.exp = get_exp_by_file(path), where path is the manually specified exp file path.

For example:

exp_path = 'YOLOX/exps/default/yolox_m.py'  
self.exp = get_exp_by_file(exp_path )   # detector.py   line 31

pmj110119 avatar Nov 19 '21 08:11 pmj110119

It seems that maybe you changed the project structure and the program could not find the path of the exp file. You can change detector.py to solve it.

Firstly import another function to load exp-file:

from YOLOX.yolox.exp.build import get_exp_by_file    # detector.py   line 9

And then change self.exp = get_exp_by_name(model) to self.exp = get_exp_by_file(path), where path is the manually specified exp file path.

For example:

exp_path = 'YOLOX/exps/default/yolox_m.py'  
self.exp = get_exp_by_file(exp_path )   # detector.py   line 31

@pmj110119 Hello, thank you very much for your quick response. The problem I think is that the code requests for an EXP in the yolox_m.py in your exemple.

I obtained this when I tried to modify as you suggested to me:

  File "track.py", line 33, in __init__
    self.detector=build_detector(cfg,use_cuda=use_cuda)
  File "yolox-pytorch\detector.py", line 74, in build_detector
    model= Detector(cfg.YOLOX.MODEL, cfg.YOLOX.WEIGHT)
  File "yolox-pytorch\detector.py", line 34, in __init__
    self.exp = get_exp_by_file(exp_path)
  File "\yolox-pytorch\exp\build.py", line 17, in get_exp_by_file

raise ImportError("{} doesn't contains class named 'Exp'".format(exp_file))
ImportError: ./exp/custom.py doesn't contains class named 'Exp'

I add you my architecture: image

Thank you in advance,

Sarouch avatar Nov 19 '21 08:11 Sarouch

Yes, class EXP should be defined in a py file, and then use get_exp_by_file(file) to load it.

The yolox_m.py in my example is YOLOX's official exp-file example, you can find it here.

pmj110119 avatar Nov 19 '21 09:11 pmj110119

Yes, class EXP should be defined in a py file, and then use get_exp_by_file(file) to load it.

The yolox_m.py in my example is YOLOX's official exp-file example, you can find it here.

Thank you @pmj110119 I have this in my custom.py ( so i have EXP) what do you think ? image

Sarouch avatar Nov 19 '21 12:11 Sarouch

Hello @pmj110119 I have another error, do you know why model is not detected please ? File "track.py", line 33, in __init__ self.detector=build_detector(cfg,use_cuda=use_cuda) File "C:\Users\chouchen2-admin\PycharmProjects\pythonProjecttest\YoloX\yolox-pytorch\detector.py", line 71, in build_detector model= Detector(cfg.YOLOX.MODEL, cfg.YOLOX.WEIGHT) File "C:\Users\chouchen2-admin\PycharmProjects\pythonProjecttest\YoloX\yolox-pytorch\detector.py", line 40, in __init__ self.model.load_state_dict(checkpoint["model"]) KeyError: 'model' detector.py :

#sys.path.insert(0, './YOLOX')
import torch
import numpy as np
import cv2

from data.data_augment import preproc
#from data.datasets import COCO_CLASSES
from data.voc_classes import Customer_classes
from exp.build import get_exp_by_name
from exp.build import get_exp_by_file
from utils.visualize import vis
from models import post_process



COCO_MEAN = (0.485, 0.456, 0.406)
COCO_STD = (0.229, 0.224, 0.225)




class Detector():
    """ 图片检测器 """
    def __init__(self, model='custom', ckpt='./weights/model_custom_last.pth'):
        super(Detector, self).__init__()



        self.device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')

        #self.exp = get_exp_by_name
        exp_path = "./exp/custom/custom.py"
        self.exp = get_exp_by_file(exp_path)
        self.test_size = self.exp.test_size  # TODO: 改成图片自适应大小
        self.model = self.exp.get_model()
        self.model.to(self.device)
        self.model.eval()
        checkpoint = torch.load(ckpt, map_location='cuda:0')
        self.model.load_state_dict(checkpoint["model"])


    def detect(self, raw_img, visual=True, conf=0.5):
        #test_size = [640, 640]
        info = {}
        img, ratio = preproc(raw_img, self.test_size, COCO_MEAN, COCO_STD)
        info['raw_img'] = raw_img
        info['img'] = img

        img = torch.from_numpy(img).unsqueeze(0)
        img = img.to(self.device)

        with torch.no_grad():
            outputs = self.model(img)
            outputs = post_process(
                outputs, self.exp.num_classes, self.exp.test_conf, self.exp.nmsthre  # TODO:用户可更改
            )[0].cpu().numpy()

        info['boxes'] = outputs[:, 0:4]/ratio
        info['scores'] = outputs[:, 4] * outputs[:, 5]
        info['class_ids'] = outputs[:, 6]
        info['box_nums'] = outputs.shape[0]
        # 可视化绘图
        if visual:
            info['visual'] = vis(info['raw_img'], info['boxes'], info['scores'], info['class_ids'], conf, Customer_classes)
        return info



def build_detector(cfg, use_cuda):
    model= Detector(cfg.YOLOX.MODEL, cfg.YOLOX.WEIGHT)
    # if use_cuda:
    #     model.cuda()
    return model


if __name__=='__main__':
    detector = Detector()
    img = cv2.imread('dog.jpg')
    img_,out = detector.detect(img)
    print(out)```

Sarouch avatar Nov 21 '21 20:11 Sarouch

for me pip install yacs helped

Kaschi14 avatar Oct 23 '23 14:10 Kaschi14