Traceback (most recent call last):
File "train.py", line 511, in <module>
train(hyp, tb_writer, opt, device)
File "train.py", line 368, in train
save_dir=log_dir)
File "../yolov5-master/test.py", line 176, in test
plot_images(img, output_to_target(output, width, height), paths, str(f), names) # predictions
File "../yolov5-master/utils/utils.py", line 914, in output_to_target
return np.array(targets)
File "../lib/python3.7/site-packages/torch/tensor.py", line 492, in __array__
return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
Traceback (most recent call last): ? File "train.py", line 511, in <module> ? ? train(hyp, tb_writer, opt, device) ? File "train.py", line 368, in train ? ? save_dir=log_dir) ? File "../yolov5-master/test.py", line 176, in test ? ? plot_images(img, output_to_target(output, width, height), paths, str(f), names) ?# predictions ? File "../yolov5-master/utils/utils.py", line 914, in output_to_target ? ? return np.array(targets) ? File "../miniconda3/envs/yolov5/lib/python3.7/site-packages/torch/tensor.py", line 492, in __array__ ? ? return self.numpy() TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
解决老版本yolo v5 test时报上述错误:
修改/yolov5-master/utils/utils.py
第895行: output_to_target(output, width, height) 函数中的元素o,将其转到cpu上:?o = o.cpu().numpy()
?output是GPU上的list ,list元素的类型是tensor,需要先转为cpu上的numpy()类型
def output_to_target(output, width, height):
# Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
if isinstance(output, torch.Tensor):
output = output.cpu().numpy()
targets = []
for i, o in enumerate(output):
if o is not None:
# output是gpu上的list ,list元素的类型是tensor,需要先转为cpu删的numpy()类型
o = o.cpu().numpy()
for pred in o:
box = pred[:4]
w = (box[2] - box[0]) / width
h = (box[3] - box[1]) / height
x = box[0] / width + w / 2
y = box[1] / height + h / 2
conf = pred[4]
cls = int(pred[5])
targets.append([i, cls, x, y, w, h, conf])
return np.array(targets)
|