我在 roboflow 数据集上微调了 YOLOv8 模型(任务:分类)。我下载了最佳参数并尝试使用以下代码使用它们进行预测:
model2 = YOLO("spectrogramfinetunedyolo.pt")
result = model2("640_X_640.jpg")
我不断收到以下错误,但我不知道出了什么问题。我尝试更改文件类型,并尝试使用不同的图像大小,但错误仍然存在。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 4
1 model2 = YOLO("spectrogramfinetunedyolo.pt")
2 # results = model(['s1Voicespectro/'+ i for i in os.listdir('s1Voicespectro')])
3 # result = model2("640_X_640.jpg")
----> 4 model2.predict("640_X_640.jpg")
File ~\anaconda3\Lib\site-packages\ultralytics\engine\model.py:239, in Model.predict(self, source, stream, predictor, **kwargs)
237 if prompts and hasattr(self.predictor, 'set_prompts'): # for SAM-type models
238 self.predictor.set_prompts(prompts)
--> 239 return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
File ~\anaconda3\Lib\site-packages\ultralytics\engine\predictor.py:198, in BasePredictor.__call__(self, source, model, stream, *args, **kwargs)
196 return self.stream_inference(source, model, *args, **kwargs)
197 else:
--> 198 return list(self.stream_inference(source, model, *args, **kwargs))
File ~\anaconda3\Lib\site-packages\torch\utils\_contextlib.py:35, in _wrap_generator.<locals>.generator_context(*args, **kwargs)
32 try:
33 # Issuing `None` to a generator fires it up
34 with ctx_factory():
---> 35 response = gen.send(None)
37 while True:
38 try:
39 # Forward the response to our caller and get its next request
File ~\anaconda3\Lib\site-packages\ultralytics\engine\predictor.py:261, in BasePredictor.stream_inference(self, source, model, *args, **kwargs)
259 # Preprocess
260 with profilers[0]:
--> 261 im = self.preprocess(im0s)
263 # Inference
264 with profilers[1]:
File ~\anaconda3\Lib\site-packages\ultralytics\models\yolo\classify\predict.py:36, in ClassificationPredictor.preprocess(self, img)
34 """Converts input image to model-compatible data type."""
35 if not isinstance(img, torch.Tensor):
---> 36 img = torch.stack([self.transforms(im) for im in img], dim=0)
37 img = (img if isinstance(img, torch.Tensor) else torch.from_numpy(img)).to(self.model.device)
38 return img.half() if self.model.fp16 else img.float()
File ~\anaconda3\Lib\site-packages\ultralytics\models\yolo\classify\predict.py:36, in <listcomp>(.0)
34 """Converts input image to model-compatible data type."""
35 if not isinstance(img, torch.Tensor):
---> 36 img = torch.stack([self.transforms(im) for im in img], dim=0)
37 img = (img if isinstance(img, torch.Tensor) else torch.from_numpy(img)).to(self.model.device)
38 return img.half() if self.model.fp16 else img.float()
File ~\anaconda3\Lib\site-packages\torchvision\transforms\transforms.py:95, in Compose.__call__(self, img)
93 def __call__(self, img):
94 for t in self.transforms:
---> 95 img = t(img)
96 return img
File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1518, in Module._wrapped_call_impl(self, *args, **kwargs)
1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1517 else:
-> 1518 return self._call_impl(*args, **kwargs)
File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1527, in Module._call_impl(self, *args, **kwargs)
1522 # If we don't have any hooks, we want to skip the rest of the logic in
1523 # this function, and just call forward.
1524 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1525 or _global_backward_pre_hooks or _global_backward_hooks
1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
1529 try:
1530 result = None
File ~\anaconda3\Lib\site-packages\torchvision\transforms\transforms.py:361, in Resize.forward(self, img)
353 def forward(self, img):
354 """
355 Args:
356 img (PIL Image or Tensor): Image to be scaled.
(...)
359 PIL Image or Tensor: Rescaled image.
360 """
--> 361 return F.resize(img, self.size, self.interpolation, self.max_size, self.antialias)
File ~\anaconda3\Lib\site-packages\torchvision\transforms\functional.py:476, in resize(img, size, interpolation, max_size, antialias)
470 if max_size is not None and len(size) != 1:
471 raise ValueError(
472 "max_size should only be passed if size specifies the length of the smaller edge, "
473 "i.e. size should be an int or a sequence of length 1 in torchscript mode."
474 )
--> 476 _, image_height, image_width = get_dimensions(img)
477 if isinstance(size, int):
478 size = [size]
File ~\anaconda3\Lib\site-packages\torchvision\transforms\functional.py:78, in get_dimensions(img)
75 if isinstance(img, torch.Tensor):
76 return F_t.get_dimensions(img)
---> 78 return F_pil.get_dimensions(img)
File ~\anaconda3\Lib\site-packages\torchvision\transforms\_functional_pil.py:31, in get_dimensions(img)
29 width, height = img.size
30 return [channels, height, width]
---> 31 raise TypeError(f"Unexpected type {type(img)}")
TypeError: Unexpected type <class 'numpy.ndarray'>
看起来 Roboflow 做了一些预处理任务,例如调整大小(基于错误):
File ~\anaconda3\Lib\site-packages\torchvision\transforms\transforms.py:361, in Resize.forward(self, img)
353 def forward(self, img):
354 """
355 Args:
356 img (PIL Image or Tensor): Image to be scaled.
(...)
359 PIL Image or Tensor: Rescaled image.
360 """
--> 361 return F.resize(img, self.size, self.interpolation, self.max_size, self.antialias)
如您所见,它需要一个 PIL Image 或 Tensor。只需使用 Pillow 模块(pip install Pillow)即可直接将图像加载为 PIL 图像。
from PIL import Image
image_path = "640_X_640.jpg"
image = Image.open(image_path)
result = model2(image)