问题1
self.update(yaml.load(fo.read()))
TypeError: load() missing 1 required positional argument: 'Loader'
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master/main.py", line 259, in <module>
with VideoTracker(args) as vdo_trk:
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master/main.py", line 53, in __init__
cfg.merge_from_file(args.config_deepsort)
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master/utils_ds/parser.py", line 23, in merge_from_file
self.update(yaml.load(fo.read()))
TypeError: load() missing 1 required positional argument: 'Loader'
这个问题是因为这个错误是因为在使用yaml.load()
函数时缺少了一个必需的参数Loader
。
需要在DeepSORT_YOLOv5_Pytorch-master-2/utils_ds/parser.py第23行代码修改为
self.update(yaml.load(fo.read(),Loader=yaml.Loader))
def merge_from_file(self, config_file):
with open(config_file, 'r') as fo:
self.update(yaml.load(fo.read(),Loader=yaml.Loader))
问题二:
with VideoTracker(args) as vdo_trk:
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master/main.py", line 81, in __enter__
assert os.path.isfile(self.args.input_path), "Path error"
AssertionError: Path error
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master/main.py", line 259, in <module>
with VideoTracker(args) as vdo_trk:
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master/main.py", line 81, in __enter__
assert os.path.isfile(self.args.input_path), "Path error"
AssertionError: Path error
这个问题是在指定的路径上找不到文件。请确保你提供的 input_path
参数指向一个有效的文件路径 修改main.py文件夹内230行parser.add_argument('--input_path', type=str, default='input_480.mp4', help='source')
parser.add_argument('--input_path', type=str, default='input_480.mp4', help='source')
修改视频路径
default='input_480.mp4'
问题三:
/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /Users/runner/work/_temp/anaconda/conda-bld/pytorch_1695391828857/work/aten/src/ATen/native/TensorShape.cpp:3527.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
<class 'AttributeError'> module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations <traceback object at 0x28f891300>
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 260, in <module>
vdo_trk.run()
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 123, in run
outputs, yt, st = self.image_track(img0) # (#ID, 5) x1,y1,x2,y2,id
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 218, in image_track
outputs = self.deepsort.update(bbox_xywh, confs, im0)
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep_sort.py", line 42, in update
detections = [Detection(bbox_tlwh[i], conf, features[i]) for i,conf in enumerate(confidences) if conf>self.min_confidence]
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep_sort.py", line 42, in <listcomp>
detections = [Detection(bbox_tlwh[i], conf, features[i]) for i,conf in enumerate(confidences) if conf>self.min_confidence]
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/sort/detection.py", line 30, in __init__
self.tlwh = np.asarray(tlwh, dtype=np.float) # x1, y1, w, h
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/numpy/__init__.py", line 305, in __getattr__
raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /Users/runner/work/_temp/anaconda/conda-bld/pytorch_1695391828857/work/aten/src/ATen/native/TensorShape.cpp:3527.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
<class 'AttributeError'> module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations <traceback object at 0x28f891300>
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 260, in <module>
vdo_trk.run()
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 123, in run
outputs, yt, st = self.image_track(img0) # (#ID, 5) x1,y1,x2,y2,id
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 218, in image_track
outputs = self.deepsort.update(bbox_xywh, confs, im0)
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep_sort.py", line 42, in update
detections = [Detection(bbox_tlwh[i], conf, features[i]) for i,conf in enumerate(confidences) if conf>self.min_confidence]
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep_sort.py", line 42, in <listcomp>
detections = [Detection(bbox_tlwh[i], conf, features[i]) for i,conf in enumerate(confidences) if conf>self.min_confidence]
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/sort/detection.py", line 30, in __init__
self.tlwh = np.asarray(tlwh, dtype=np.float) # x1, y1, w, h
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/numpy/__init__.py", line 305, in __getattr__
raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
这个错误提示表明在使用 numpy
库时,使用了已经被弃用的 np.float
,更推荐直接使用内置的 float
。需要更新代码中使用的 np.float
为 float
来避免这个错误
点击每一个报错的路径如果里面有np.float则直接修改为float np.int同理修改为int
def __init__(self, tlwh, confidence, feature):
self.tlwh = np.asarray(tlwh, dtype=np.float) # x1, y1, w, h
self.confidence = float(confidence)
self.feature = np.asarray(feature, dtype=np.float32)
修改后:
def __init__(self, tlwh, confidence, feature):
self.tlwh = np.asarray(tlwh, dtype=np.float) # x1, y1, w, h
self.confidence = float(confidence)
self.feature = np.asarray(feature, dtype=float32)
问题四:
在使用自己的yolo模型报错:
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 259, in <module>
with VideoTracker(args) as vdo_trk:
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 59, in __init__
self.detector = torch.load(args.weights, map_location=self.device)['model'].float() # load to FP32
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/serialization.py", line 1014, in load
return _load(opened_zipfile,
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/serialization.py", line 1422, in _load
result = unpickler.load()
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/serialization.py", line 1415, in find_class
return super().find_class(mod_name, name)
AttributeError: Can't get attribute 'C3' on <module 'models.common' from '/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/yolov5/models/common.py'>
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 259, in <module>
with VideoTracker(args) as vdo_trk:
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 59, in __init__
self.detector = torch.load(args.weights, map_location=self.device)['model'].float() # load to FP32
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/serialization.py", line 1014, in load
return _load(opened_zipfile,
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/serialization.py", line 1422, in _load
result = unpickler.load()
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/serialization.py", line 1415, in find_class
return super().find_class(mod_name, name)
AttributeError: Can't get attribute 'C3' on <module 'models.common' from '/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/yolov5/models/common.py'>
这是因为你使用的yolov5的版本和他不同
解决方法1:使用他的yolo版本来训练模型
解决办法2:将他里面的yolov5修改为你使用的版本 本文使用的是yolov5的v7
去github下载下来: GitHub - ultralytics/yolov5 at v7.0
之后在DeepSort_YOLOv5_Pytorch里面的yolov5整个文件替换为上面👆下载来的yolov5 之后把报错的地方路径改下 就可以使用了
问题五:
在使用自己训练的Deepsort模型的时候报错
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 259, in <module>
with VideoTracker(args) as vdo_trk:
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 56, in __init__
self.deepsort = build_tracker(cfg, use_cuda=use_cuda)
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/__init__.py", line 8, in build_tracker
return DeepSort(cfg.DEEPSORT.REID_CKPT,
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep_sort.py", line 19, in __init__
self.extractor = Extractor(model_path, use_cuda=use_cuda)
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep/feature_extractor.py", line 14, in __init__
self.net.load_state_dict(state_dict)
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 2152, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for Net:
size mismatch for classifier.4.weight: copying a param with shape torch.Size([2, 256]) from checkpoint, the shape in current model is torch.Size([751, 256]).
size mismatch for classifier.4.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([751]).
Traceback (most recent call last):
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 259, in <module>
with VideoTracker(args) as vdo_trk:
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/main.py", line 56, in __init__
self.deepsort = build_tracker(cfg, use_cuda=use_cuda)
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/__init__.py", line 8, in build_tracker
return DeepSort(cfg.DEEPSORT.REID_CKPT,
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep_sort.py", line 19, in __init__
self.extractor = Extractor(model_path, use_cuda=use_cuda)
File "/Users/gan/Downloads/DeepSORT_YOLOv5_Pytorch-master-2/deep_sort/deep/feature_extractor.py", line 14, in __init__
self.net.load_state_dict(state_dict)
File "/Users/gan/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 2152, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for Net:
size mismatch for classifier.4.weight: copying a param with shape torch.Size([2, 256]) from checkpoint, the shape in current model is torch.Size([751, 256]).
size mismatch for classifier.4.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([751]).
这个错误是由于模型加载时发生了形状不匹配的问题在当前模型中,classifier.4.weight
的形状为 [751, 256]
,而加载的检查点中的形状为 [2, 256]
;classifier.4.bias
的形状为 [751]
,而加载的检查点中的形状为 [2]
。这是因为你训练的类别只有2个 而原本的模型有751个
修改deep内的model.py 第48行 的 num_classes=751 751修改为自己的类别数量。如果你的Deepsort只有两个类别的话 则把751修改为2
上文是我在使用Deepsort时候遇到的使用问题 点个小星星✨