NVIDIA / flownet2-pytorch

Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks
Other
3.15k stars 740 forks source link

invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number #113

Open zhuker opened 5 years ago

zhuker commented 5 years ago

pytorch 1.0

python3 main.py --inference --model FlowNet2 --save_flow --inference_dataset ImagesFromFolder --inference_dataset_root ~/fs24_512/ --resume mycheckpoints/FlowNet2_checkpoint.pth.tar

Initializing Datasets
[384, 512]
  [0.012s] Inference Dataset: ImagesFromFolder
  [0.025s] Inference Input: [3, 2, 384, 512]
  [0.037s] Inference Targets: [3, 2, 384, 512]
  [0.037s] Operation finished

Building FlowNet2 model
  [2.646s] Effective Batch Size: 16
  [2.647s] Number of parameters: 162518834
  [2.647s] Initializing CUDA
  [4.606s] Parallelizing
  [4.606s] Loading checkpoint 'mycheckpoints/FlowNet2_checkpoint.pth.tar'
  [4.907s] Loaded checkpoint 'mycheckpoints/FlowNet2_checkpoint.pth.tar' (at epoch 0)
  [4.907s] Initializing save directory: ./work
  [4.912s] Operation finished

Initializing Adam Optimizer
  [0.001s] amsgrad = False (<class 'bool'>)
  [0.001s] lr = 0.001 (<class 'float'>)
  [0.001s] eps = 1e-08 (<class 'float'>)
  [0.001s] weight_decay = 0 (<class 'int'>)
  [0.001s] betas = (0.9, 0.999) (<class 'tuple'>)
  [0.001s] Operation finished

Overall Progress:   0%|                                                       | 0/1 [00:00<?, ?it/s]/home/ubuntu/.local/lib/python3.5/site-packages/torch/nn/modules/upsampling.py:129: UserWarning: nn.Upsample is deprecated. Use nn.functional.interpolate instead.
  warnings.warn("nn.{} is deprecated. Use nn.functional.interpolate instead.".format(self.name))t/s]
/home/ubuntu/.local/lib/python3.5/site-packages/torch/nn/functional.py:2423: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode))
/home/ubuntu/.local/lib/python3.5/site-packages/torch/nn/parallel/_functions.py:61: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.
  warnings.warn('Was asked to gather along dimension 0, but all '
Traceback (most recent call last):
  File "main.py", line 403, in <module>
    stats = inference(args=args, epoch=epoch - 1, data_loader=inference_loader, model=model_and_loss, offset=offset)
  File "main.py", line 371, in inference
    total_loss += loss_val.data[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
huangbiubiu commented 5 years ago

That's because in PyTorch>=0.5, the index of 0-dim tensor is invalid. The master branch is designed for PyTorch 0.4.1, loss_val.data[0] works well.

Try to change

total_loss += loss_val.data[0]
loss_values = [v.data[0] for v in losses]

to

total_loss += loss_val.data
loss_values = [v.data for v in losses]

might fix the problem.

toprocker commented 5 years ago

`Overall Progress: 0%| | 0/1 [00:00<?, ?it/s] Inferencing : 0%| | 0/1041.0 [00:00<?, ?it/s]/home/toprocker/.local/lib/python3.6/site-packages/torch/nn/modules/upsampling.py:122: UserWarning: nn.Upsampling is deprecated. Use nn.functional.interpolate instead. warnings.warn("nn.Upsampling is deprecated. Use nn.functional.interpolate instead.") /home/toprocker/.local/lib/python3.6/site-packages/torch/nn/functional.py:1961: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details. "See the documentation of nn.Upsample for details.".format(mode))

Inference Averages for Epoch 0: L1: 0.205, EPE: 0.326: 0%| | 0/1041.0 [00:02<?, ?it/s] Inference Averages for Epoch 0: L1: 0.205, EPE: 0.326: 0%| | 1/1041.0 [00:02<49:24, 2.85s/it] Inference Averages for Epoch 0: L1: 0.205, EPE: 0.326: 0%| | 1/1041.0 [00:02<49:24, 2.85s/it]error in correlation_forward_cuda_kernel: no kernel image is available for execution on the device Traceback (most recent call last):`

I tried your method, the two warnings disappeared, but the problem has not been solved.

huangbiubiu commented 5 years ago

@toprocker This is another error caused by incorrect compile parameters. See https://github.com/NVIDIA/flownet2-pytorch/issues/86#issuecomment-449621437 for the solution.

AbdelnasserMostafa commented 5 years ago

@huangbiubiu, Thank you very much, that fixed my problem.

sahinurlaskar commented 5 years ago

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

sahinurlaskar commented 5 years ago

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number How i can fix this error.

sahinurlaskar commented 5 years ago

File "/content/MultimodalNMT/onmt/TrainerMultimodal.py", line 286, in _gradient_accumulation trunc_size, self.shard_size, normalization) File "/content/MultimodalNMT/onmt/Loss.py", line 122, in sharded_compute_loss loss, stats = self._compute_loss(batch, **shard) File "/content/MultimodalNMT/onmt/Loss.py", line 207, in _compute_loss stats = self._stats(loss_data, scores.data, target.view(-1).data) File "/content/MultimodalNMT/onmt/Loss.py", line 144, in _stats return onmt.Statistics(loss[0], non_padding.sum(), num_correct) IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

How can i solve this issue?

sahinurlaskar commented 5 years ago

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

zisuina commented 5 years ago

total_loss += loss_val.item()

caramelslice commented 5 years ago

huangbiubiu that fixed my problem too.

azraar commented 5 years ago

@huangbiubiu, Thank you very much, that fixed my problem.

iroshansam78 commented 4 years ago

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

for i, data in enumerate(dataloader, 0): # We iterate over the images of the dataset.

    # 1st Step: Updating the weights of the neural network of the discriminator

    netD.zero_grad() # We initialize to 0 the gradients of the discriminator with respect to the weights.

    # Training the discriminator with a real image of the dataset
    real, _ = data # We get a real image of the dataset which will be used to train the discriminator.
    input = Variable(real) # We wrap it in a variable.
    target = Variable(torch.ones(input.size()[0])) # We get the target.
    output = netD(input) # We forward propagate this real image into the neural network of the discriminator to get the prediction (a value between 0 and 1).
    errD_real = criterion(output, target) # We compute the loss between the predictions (output) and the target (equal to 1).

    # Training the discriminator with a fake image generated by the generator
    noise = Variable(torch.randn(input.size()[0], 100, 1, 1)) # We make a random input vector (noise) of the generator.
    fake = netG(noise) # We forward propagate this random input vector into the neural network of the generator to get some fake generated images.
    target = Variable(torch.zeros(input.size()[0])) # We get the target.
    output = netD(fake.detach()) # We forward propagate the fake generated images into the neural network of the discriminator to get the prediction (a value between 0 and 1).
    errD_fake = criterion(output, target) # We compute the loss between the prediction (output) and the target (equal to 0).

    # Backpropagating the total error
    errD = errD_real + errD_fake # We compute the total error of the discriminator.
    errD.backward() # We backpropagate the loss error by computing the gradients of the total error with respect to the weights of the discriminator.
    optimizerD.step() # We apply the optimizer to update the weights according to how much they are responsible for the loss error of the discriminator.

    # 2nd Step: Updating the weights of the neural network of the generator

    netG.zero_grad() # We initialize to 0 the gradients of the generator with respect to the weights.
    target = Variable(torch.ones(input.size()[0])) # We get the target.
    output = netD(fake) # We forward propagate the fake generated images into the neural network of the discriminator to get the prediction (a value between 0 and 1).
    errG = criterion(output, target) # We compute the loss between the prediction (output between 0 and 1) and the target (equal to 1).
    errG.backward() # We backpropagate the loss error by computing the gradients of the total error with respect to the weights of the generator.
    optimizerG.step() # We apply the optimizer to update the weights according to how much they are responsible for the loss error of the generator.

    # 3rd Step: Printing the losses and saving the real images and the generated images of the minibatch every 100 steps

    print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f' % (epoch, 25, i, len(dataloader), errD.data[0], errG.data[0])) # We print les losses of the discriminator (Loss_D) and the generator (Loss_G).
    if i % 100 == 0: # Every 100 steps:
        vutils.save_image(real, '%s/real_samples.png' % "./results", normalize = True) # We save the real images of the minibatch.
        fake = netG(noise) # We get our fake generated images.
        vutils.save_image(fake.data, '%s/fake_samples_epoch_%03d.png' % ("./results", epoch), normalize = True) # We also save the fake generated images of the minibatch.
gjian0115 commented 4 years ago

@huangbiubiu Thank you very much, that fixed my problem.

Light-- commented 4 years ago

recommend solution: https://blog.csdn.net/LYKXHTP/article/details/81565453