GANimation icon indicating copy to clipboard operation
GANimation copied to clipboard

IndexError: During training 0-dim tensor error

Open divyanshujhawar opened this issue 5 years ago • 2 comments

While executing the train.py file,

IndexError: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T>() in C++ to convert a 0-dim tensor to a number

The complete log has been attached below:

D_adam_b1: 0.5
D_adam_b2: 0.999
G_adam_b1: 0.5
G_adam_b2: 0.999
aus_file: aus_openface.pkl
batch_size: 4
checkpoints_dir: ./checkpoints
cond_nc: 17
data_dir: GANimation-master/sample_dataset1
dataset_mode: aus
display_freq_s: 300
do_saturate_mask: False
gpu_ids: [0]
image_size: 128
images_folder: imgs
is_train: True
lambda_D_cond: 4000
lambda_D_gp: 10
lambda_D_prob: 1
lambda_cyc: 10
lambda_mask: 0.1
lambda_mask_smooth: 1e-05
load_epoch: 0
lr_D: 0.0001
lr_G: 0.0001
model: ganimation
n_threads_test: 1
n_threads_train: 4
name: experiment_1
nepochs_decay: 10
nepochs_no_decay: 20
num_iters_validate: 1
poses_g_sigma: 0.06
print_freq_s: 60
save_latest_freq_s: 3600
serial_batches: False
test_ids_file: test_ids.csv
train_G_every_n_iterations: 5
train_ids_file: train_ids.csv
-------------- End ----------------
./checkpoints/experiment_1
Dataset AusDataset was created
Dataset AusDataset was created
#train images = 15
#test images = 15
Network generator_wasserstein_gan was created
Network discriminator_wasserstein_gan was created
Model GANimation was created
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/content/GANimation-master/train.py in <module>()
    139 
    140 if __name__ == "__main__":
--> 141     Train()

4 frames
/content/GANimation-master/models/ganimation.py in get_current_errors(self)
    318         loss_dict = OrderedDict([('g_fake', self._loss_g_fake.data[0]),
    319                                  ('g_cond', self._loss_g_cond.data[0]),
--> 320                                  ('g_mskd_fake', self._loss_g_masked_fake.data[0]),
    321                                  ('g_mskd_cond', self._loss_g_masked_cond.data[0]),
    322                                  ('g_cyc', self._loss_g_cyc.data[0]),

IndexError: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T>() in C++ to convert a 0-dim tensor to a number


I have run all the previous commands, including the requirements.txt file. While looking for this over the net, I found that it is due to the update in the Pytorch version which does not allow accessing the last element as [0], we need to use .item(). But, even after changing it, I am getting the same error.

Anyone facing the same issues?

divyanshujhawar avatar Jul 24 '20 17:07 divyanshujhawar

Hi @divyanshujhawar , I have encountered the same problem. Could you find the solution?

Sinerei avatar Oct 14 '22 10:10 Sinerei

I think i have the solution for the upper version of pytorch hier: Change he indexed loss.data[0] to loss.data:

in ganimation.py change:

    def get_current_errors(self):
        loss_dict = OrderedDict([('g_fake', self._loss_g_fake.data[0]),
                                 ('g_cond', self._loss_g_cond.data[0]),
                                 ('g_mskd_fake', self._loss_g_masked_fake.data[0]),
                                 ('g_mskd_cond', self._loss_g_masked_cond.data[0]),
                                 ('g_cyc', self._loss_g_cyc.data[0]),
                                 ('g_rgb', self._loss_rec_real_img_rgb.data[0]),
                                 ('g_rgb_un', self._loss_g_unmasked_rgb.data[0]),
                                 ('g_rgb_s', self._loss_g_fake_imgs_smooth.data[0]),
                                 ('g_m1', self._loss_g_mask_1.data[0]),
                                 ('g_m2', self._loss_g_mask_2.data[0]),
                                 ('g_m1_s', self._loss_g_mask_1_smooth.data[0]),
                                 ('g_m2_s', self._loss_g_mask_2_smooth.data[0]),
                                 ('g_idt', self._loss_g_idt.data[0]),
                                 ('d_real', self._loss_d_real.data[0]),
                                 ('d_cond', self._loss_d_cond.data[0]),
                                 ('d_fake', self._loss_d_fake.data[0]),
                                 ('d_gp', self._loss_d_gp.data[0])])
        return loss_dict

to:

def get_current_errors(self):
        loss_dict = OrderedDict([('g_fake', self._loss_g_fake.data),
                                 ('g_cond', self._loss_g_cond.data),
                                 ('g_mskd_fake', self._loss_g_masked_fake.data),
                                 ('g_mskd_cond', self._loss_g_masked_cond.data),
                                 ('g_cyc', self._loss_g_cyc.data),
                                 ('g_rgb', self._loss_rec_real_img_rgb.data),
                                 ('g_rgb_un', self._loss_g_unmasked_rgb.data),
                                 ('g_rgb_s', self._loss_g_fake_imgs_smooth.data),
                                 ('g_m1', self._loss_g_mask_1.data),
                                 ('g_m2', self._loss_g_mask_2.data),
                                 ('g_m1_s', self._loss_g_mask_1_smooth.data),
                                 ('g_m2_s', self._loss_g_mask_2_smooth.data),
                                 ('g_idt', self._loss_g_idt.data),
                                 ('d_real', self._loss_d_real.data),
                                 ('d_cond', self._loss_d_cond.data),
                                 ('d_fake', self._loss_d_fake.data),
                                 ('d_gp', self._loss_d_gp.data)])
        return loss_dict

Sinerei avatar Oct 14 '22 11:10 Sinerei