WongKinYiu / yolor

implementation of paper - You Only Learn One Representation: Unified Network for Multiple Tasks (https://arxiv.org/abs/2105.04206)
GNU General Public License v3.0
1.99k stars 518 forks source link

can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() #113

Open samyeh0527 opened 3 years ago

samyeh0527 commented 3 years ago

Hi WongKinYiu , when training is the end will show some issues but my environment python is [3.9] didn't know the version difference is the main reason for this .

As just mentioned Error issues [ can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to cpoy the tensor to host memory first ]

I know the numpy is cpu only but how can i fix this issues ?

wish you a nice day

WongKinYiu commented 3 years ago

Use Tensor.cpu() to cpoy the tensor to host memory first.

samyeh0527 commented 3 years ago

Traceback (most recent call last): File "/home/umeida/Desktop/yolor/train.py", line 537, in train(hyp, opt, device, tb_writer, wandb) File "/home/umeida/Desktop/yolor/train.py", line 336, in train results, maps, times = test.test(opt.data, File "/home/umeida/Desktop/yolor/test.py", line 226, in test plot_images(img, output_to_target(output, width, height), paths, f, names) # predictions File "/home/umeida/Desktop/yolor/utils/plots.py", line 108, in output_to_target return np.array(targets)
File "/home/umeida/anaconda3/envs/yolor/lib/python3.9/site-packages/torch/_tensor.py", line 643, in array return self.numpy() <-------Replace Tensor.cpu() ?

WongKinYiu commented 3 years ago

return np.array(targets.cpu())

tesorrells commented 3 years ago
return np.array(targets.cpu())

AttributeError: 'list' object has no attribute 'cpu'

WongKinYiu commented 3 years ago

https://github.com/WongKinYiu/yolor/pull/44/commits/840e56e86e69692c6c1cac1a32caf1c59ff6dbba

KKami91 commented 3 years ago

@WongKinYiu in this code https://github.com/WongKinYiu/yolor/commit/840e56e86e69692c6c1cac1a32caf1c59ff6dbba run to code,

ValurError Traceback (most recent call last): File "test.py", line 332, in save_conf=opt.save_conf, File "test.py", line 228, in test plot_images(img, output_to_target(output, width, height), paths, f, names) # predictions File "/opt/ml/yolor/utils/plots.py", line 126, in output_to_target return np.array(targets) ValueError: object array method not producing an array

why in yolor/utils/plots.py
def output_to_target(output, width, height): output is list.

[tensor([[2.29000e+02, 2.24375e+02, 7.82500e+02, 6.96000e+02, 9.19922e-01, 0.00000e+00], [2.27500e+02, 2.24500e+02, 7.80500e+02, 6.95000e+02, 7.84302e-02, 1.00000e+00], [2.15500e+02, 2.26875e+02, 7.83500e+02, 6.94500e+02, 4.02832e-03, 7.00000e+00], [3.29375e+01, 1.65375e+02, 7.09375e+01, 2.31125e+02, 3.62396e-03, 7.00000e+00], [2.28250e+02, 2.25000e+02, 7.80000e+02, 6.98000e+02, 2.83051e-03, 9.00000e+00], [2.11500e+02, 1.03800e+03, 2.49000e+02, 1.05600e+03, 2.40517e-03, 0.00000e+00], [2.28250e+02, 2.25000e+02, 7.80000e+02, 6.98000e+02, 1.37711e-03, 6.00000e+00], [2.11375e+02, 1.03800e+03, 2.49375e+02, 1.05600e+03, 1.21403e-03, 7.00000e+00]], device='cuda:0'), ..... ..... ]

this output len is 32, not in if instance(output, torch.Tensor) so, i modify output = output.cpu() -> AttributeError: 'list' object has no attribute 'cpu'

and, i modify output = output.cpu().numpy() -> AttributeError: 'list' object has no attribute 'cpu'

and i modify output = output[0].cpu().numpy -> AttributeError: 'numpy.float32' object has no attribute 'cpu' python test.py --data data/coco.yaml --img 1024 --batch 1 --conf 0.001 --iou 0.5 --device 0 --cfg cfg/yolor_p6.cfg --weights runs/train/yolor_p658/weights/best_ap50.pt --name yolor_p6_val

why in function output is type list?

samyeh0527 commented 3 years ago

Thanks , I change the 840e56e can fix this issue . it's work successful

danielcrane commented 2 years ago

Is there any reason this change isn't being merged to the main branches?

samyeh0527 commented 2 years ago

may be just rarely happens

MehmetAlpkaya commented 2 years ago

ı have same error, but ı dont solution error can you halp me?

def dist(args, model, device, train_loader, fea_loader, tar_loader, centers): model.eval() dizi=[] d0 = [] d1 = [] d2 = [] d3 = []

mean_d0 = 0
mean_d1 = 0
mean_d2 = 0
mean_d3 = 0

var_d0 = 0
var_d1 = 0
var_d2 = 0
var_d3 = 0

with torch.no_grad():
    for batch_idx, data in enumerate(train_loader):
        sample = data[:, 0:784]
        #print("sample:{}".format(sample))
        target = data[:, 784:785]
        #print("target:{}".format(target))
        sample = sample.reshape((-1, 1, 28, 28))
        #print("sample:{}".format(sample))
        sample = Variable(torch.from_numpy(sample))
        #print("sample:{}".format(sample))
        sample = sample.cuda()
       # print("sample:{}".format(sample))
        sample = sample.type(torch.cuda.FloatTensor)
        #print("sample:{}".format(sample))
        target = Variable(torch.from_numpy(target))
        #print("target:{}".format(target))
        target = target.view(64)
        #print("target:{}".format(target))
        target = target.cuda()
        #print("target:{}".format(target))
        target = target.type(torch.cuda.LongTensor)
        #print("target:{}".format(target))

        fea, output = model(sample) #[64,2]
        #print("fea:{}".format(fea))
        fea_loader.append(fea)
        #print("fea_loader:{}".format(fea_loader))
        tar_loader.append(target)
        #print("tar_loader:{}".format(fea_loader))

        expanded_centers = centers.expand(64, -1, -1)  # [64,4,2]
       # print("expanded_centers:{}".format(expanded_centers))
        expanded_feature = fea.expand(4, -1, -1).transpose(1, 0)  # [64,4,2]
        #print("expanded_feature:{}".format(expanded_feature))
        distance_centers = (expanded_feature - expanded_centers).pow(2).sum(dim=-1)  # [64,4]
        #print("distance_centers:{}".format(distance_centers))

        for i in range(0, 64):
            if target[i]==0:
                d0.append(distance_centers[i,0]**0.5)
            if target[i]==1:
                d1.append(distance_centers[i,1]**0.5)
            if target[i]==2:
                d2.append(distance_centers[i,2]**0.5)
            if target[i]==3:
                d3.append(distance_centers[i,3]**0.5)
#print(len(d0))
mean_d0 = np.mean(d0)  #i am getting the error here
mean_d1 = np.mean(d1)
mean_d2 = np.mean(d2)
mean_d3 = np.mean(d3)

var_d0 = np.std(d0)
var_d1 = np.std(d1)
var_d2 = np.std(d2)
var_d3 = np.std(d3)

return mean_d0,mean_d1,mean_d2,mean_d3,var_d0,var_d1,var_d2,var_d3