IndexError: too many indices for tensor of dimension 3

Carolmeir: <p>Hello,<br>
I am getting the below error while trying to finetune my own data set.</p>
<blockquote>
<p>Traceback (most recent call last):<br>
File “finetune.py”, line 254, in <br>
main()<br>
File “finetune.py”, line 127, in main<br>
train(TrainImgLoader, model, optimizer, log, epoch)<br>
File “finetune.py”, line 171, in train<br>
for x in range(num_out)]<br>
File “finetune.py”, line 171, in <br>
for x in range(num_out)]<br>
IndexError: too many indices for tensor of dimension 3</p>
</blockquote>
<p>This is the <a href="https://github.com/mileyan/AnyNet/blob/master/finetune.py" rel="noopener nofollow ugc">finetune.py</a> code. Please let me know how to do this <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>Based on the error message it seems the error is raised in <a href="https://github.com/mileyan/AnyNet/blob/42365358b51ec0b213215da37b4dfb990eada0a6/finetune.py#L172">this line of code</a>.<br>
However, it seems there is no tensor involved in these operations.</p>
<p>Generally you would get this error, if you are trying to index a 4th dimension for a 3-dimensional tensor:</p>
<pre><code class="lang-python">x = torch.randn(1, 2, 3)
x[0, 0, 0, 0]
&gt; IndexError: too many indices for tensor of dimension 3
</code></pre>

Carolmeir: <p>Thank you for your reply! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>Here’s the code where I think this is happening:</p>
<blockquote>
<p>import torch<br>
import torchvision.transforms as transforms<br>
import random</p>
<p>__imagenet_stats = {‘mean’: [0.485, 0.456, 0.406],<br>
‘std’: [0.229, 0.224, 0.225]}</p>
<p><span class="hashtag-raw">#__imagenet_stats</span> = {‘mean’: [0.5, 0.5, 0.5],</p>
<h1>‘std’: [0.5, 0.5, 0.5]}</h1>
<p>__imagenet_pca = {<br>
‘eigval’: torch.Tensor([0.2175, 0.0188, 0.0045]),<br>
‘eigvec’: torch.Tensor([<br>
[-0.5675,  0.7192,  0.4009],<br>
[-0.5808, -0.0045, -0.8140],<br>
[-0.5836, -0.6948,  0.4203],<br>
])<br>
}</p>
<p>def scale_crop(input_size, scale_size=None, normalize=__imagenet_stats):<br>
t_list = [<br>
transforms.ToTensor(),<br>
transforms.Normalize(**normalize),<br>
]<br>
<span class="hashtag-raw">#if</span> scale_size != input_size:<br>
<span class="hashtag-raw">#t_list</span> = [transforms.Scale((960,540))] + t_list</p>
<pre><code>return transforms.Compose(t_list)
</code></pre>
<p>def scale_random_crop(input_size, scale_size=None, normalize=__imagenet_stats):<br>
t_list = [<br>
transforms.RandomCrop(input_size),<br>
transforms.ToTensor(),<br>
transforms.Normalize(**normalize),<br>
]<br>
if scale_size != input_size:<br>
t_list = [transforms.Scale(scale_size)] + t_list</p>
<pre><code>transforms.Compose(t_list)
</code></pre>
<p>def pad_random_crop(input_size, scale_size=None, normalize=__imagenet_stats):<br>
padding = int((scale_size - input_size) / 2)<br>
return transforms.Compose([<br>
transforms.RandomCrop(input_size, padding=padding),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize(**normalize),<br>
])</p>
<p>def inception_preproccess(input_size, normalize=__imagenet_stats):<br>
return transforms.Compose([<br>
transforms.RandomSizedCrop(input_size),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize(**normalize)<br>
])<br>
def inception_color_preproccess(input_size, normalize=__imagenet_stats):<br>
return transforms.Compose([<br>
<span class="hashtag-raw">#transforms</span>.RandomSizedCrop(input_size),<br>
<span class="hashtag-raw">#transforms</span>.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
ColorJitter(<br>
brightness=0.4,<br>
contrast=0.4,<br>
saturation=0.4,<br>
),<br>
Lighting(0.1, __imagenet_pca[‘eigval’], __imagenet_pca[‘eigvec’]),<br>
transforms.Normalize(**normalize)<br>
])</p>
<p>def get_transform(name=‘imagenet’, input_size=None,<br>
scale_size=None, normalize=None, augment=True):<br>
normalize = __imagenet_stats<br>
input_size = 256<br>
if augment:<br>
return inception_color_preproccess(input_size, normalize=normalize)<br>
else:<br>
return scale_crop(input_size=input_size,<br>
scale_size=scale_size, normalize=normalize)</p>
<p>class Lighting(object):<br>
“”“Lighting noise(AlexNet - style PCA - based noise)”“”</p>
<pre><code>def __init__(self, alphastd, eigval, eigvec):
    self.alphastd = alphastd
    self.eigval = eigval
    self.eigvec = eigvec

def __call__(self, img):
    if self.alphastd == 0:
        return img

    alpha = img.new().resize_(3).normal_(0, self.alphastd)
    rgb = self.eigvec.type_as(img).clone()\
        .mul(alpha.view(1, 3).expand(3, 3))\
        .mul(self.eigval.view(1, 3).expand(3, 3))\
        .sum(1).squeeze()

    return img.add(rgb.view(3, 1, 1).expand_as(img))
</code></pre>
<p>class Grayscale(object):</p>
<pre><code>def __call__(self, img):
    gs = img.clone()
    gs[0].mul_(0.299).add_(0.587, gs[1]).add_(0.114, gs[2])
    gs[1].copy_(gs[0])
    gs[2].copy_(gs[0])
    return gs
</code></pre>
<p>class Saturation(object):</p>
<pre><code>def __init__(self, var):
    self.var = var

def __call__(self, img):
    gs = Grayscale()(img)
    alpha = random.uniform(0, self.var)
    return img.lerp(gs, alpha)
</code></pre>
<p>class Brightness(object):</p>
<pre><code>def __init__(self, var):
    self.var = var

def __call__(self, img):
    gs = img.new().resize_as_(img).zero_()
    alpha = random.uniform(0, self.var)
    return img.lerp(gs, alpha)
</code></pre>
<p>class Contrast(object):</p>
<pre><code>def __init__(self, var):
    self.var = var

def __call__(self, img):
    gs = Grayscale()(img)
    gs.fill_(gs.mean())
    alpha = random.uniform(0, self.var)
    return img.lerp(gs, alpha)
</code></pre>
<p>class RandomOrder(object):<br>
“”" Composes several transforms together in random order.<br>
“”"</p>
<pre><code>def __init__(self, transforms):
    self.transforms = transforms

def __call__(self, img):
    if self.transforms is None:
        return img
    order = torch.randperm(len(self.transforms))
    for i in order:
        img = self.transforms[i](img)
    return img
</code></pre>
<p>class ColorJitter(RandomOrder):</p>
<pre><code>def __init__(self, brightness=0.4, contrast=0.4, saturation=0.4):
    self.transforms = []
    if brightness != 0:
        self.transforms.append(Brightness(brightness))
    if contrast != 0:
        self.transforms.append(Contrast(contrast))
    if saturation != 0:
        self.transforms.append(Saturation(saturation))
</code></pre>
</blockquote>
<p>How can I correct this?</p>
<p>Thanks in advance <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>Which line of code is throwing the error?<br>
The code is currently not formatted, which makes debugging hard.<br>
You can add code snippets using three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Carolmeir: <p>This is the error I am getting now.</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "finetune.py", line 254, in &lt;module&gt;
    main()
  File "finetune.py", line 127, in main
    train(TrainImgLoader, model, optimizer, log, epoch)
  File "finetune.py", line 170, in train
    loss = [args.loss_weights[x] * F.smooth_l1_loss(outputs[x][mask], disp_L[mask], size_average=True)
  File "finetune.py", line 170, in &lt;listcomp&gt;
    loss = [args.loss_weights[x] * F.smooth_l1_loss(outputs[x][mask], disp_L[mask], size_average=True)
IndexError: too many indices for tensor of dimension 3
</code></pre>
<p>Here’s the code snippet:</p>
<pre><code class="lang-auto"> for epoch in range(args.start_epoch, args.epochs):
        log.info('This is {}-th epoch'.format(epoch))
        adjust_learning_rate(optimizer, epoch)

        train(TrainImgLoader, model, optimizer, log, epoch)

        savefilename = args.save_path + '/checkpoint.tar'
        torch.save({
            'epoch': epoch,
            'state_dict': model.state_dict(),
            'optimizer': optimizer.state_dict(),
        }, savefilename)

        if epoch % 1 ==0:
            test(TestImgLoader, model, log)

    test(TestImgLoader, model, log)
    log.info('full training time = {:.2f} Hours'.format((time.time() - start_full_time) / 3600))


def train(dataloader, model, optimizer, log, epoch=0):

    stages = 3 + args.with_spn
    losses = [AverageMeter() for _ in range(stages)]
    length_loader = len(dataloader)

    model.train()

    for batch_idx, (imgL, imgR, disp_L) in enumerate(dataloader):
        imgL = imgL.float()#.cuda()
        imgR = imgR.float()#.cuda()
        disp_L = disp_L.float()#.cuda()

        optimizer.zero_grad()
        mask = disp_L &gt; 0
        mask.detach_()
        outputs = model(imgL, imgR)

        if args.with_spn:
            if epoch &gt;= args.start_epoch_for_spn:
                num_out = len(outputs)
            else:
                num_out = len(outputs) - 1
        else:
            num_out = len(outputs)

        outputs = [torch.squeeze(output, 1) for output in outputs]
        loss = [args.loss_weights[x] * F.smooth_l1_loss(outputs[x][mask], disp_L[mask], size_average=True)
                for x in range(num_out)]
        sum(loss).backward()
        optimizer.step()
</code></pre>
<p>Thank you for your time <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

ptrblck: <p>Thanks for the stack trace and code.<br>
Based on the code and error message, I would guess <code>outputs[x][mask]</code> or <code>disp_L[mask]</code> is raising this error.<br>
Could you check the shape of all tensors in this operation and the values of <code>mask</code>?</p>

Carolmeir: <p>Thanks for your reply! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>I did <code>print(mask.shape)</code> and <code>print(disp_L[mask].shape)</code>. The shape is:</p>
<pre><code class="lang-auto">torch.Size([2, 256, 512, 4])
torch.Size([484522])
</code></pre>
<p>Is this correct?</p>

Carolmeir: <p>Also, <code>disp_L</code> shape is <code>torch.Size([2, 256, 512, 4])</code><br>
And, <code>outputs</code> is list and it’s length is <code>3</code></p>
<p>Thanks for your time <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

dung2603: <p>i got same problem please help me<br>
Traceback (most recent call last):<br>
File “ZoeDepth-main/train_mono.py”, line 169, in <br>
mp.spawn(main_worker, nprocs=ngpus_per_node,<br>
File “C:\Users\buiti\anaconda3\envs\gpu\lib\site-packages\torch\multiprocessing\spawn.py”, line 239, in spawn<br>
return start_processes(fn, args, nprocs, join, daemon, start_method=‘spawn’)<br>
File “C:\Users\buiti\anaconda3\envs\gpu\lib\site-packages\torch\multiprocessing\spawn.py”, line 197, in start_processes<br>
while not context.join():<br>
File “C:\Users\buiti\anaconda3\envs\gpu\lib\site-packages\torch\multiprocessing\spawn.py”, line 160, in join<br>
raise ProcessRaisedException(msg, error_index, failed_process.pid)<br>
torch.multiprocessing.spawn.ProcessRaisedException:</p>
<p>– Process 0 terminated with the following error:<br>
Traceback (most recent call last):<br>
File “C:\Users\buiti\anaconda3\envs\gpu\lib\site-packages\torch\multiprocessing\spawn.py”, line 69, in _wrap<br>
fn(i, *args)<br>
File “C:\Users\buiti\ZoeDepth-main\train_mono.py”, line 105, in main_worker<br>
trainer.train()<br>
File “C:\Users\buiti\ZoeDepth-main\zoedepth\trainers\base_trainer.py”, line 216, in train<br>
metrics, test_losses = self.validate()<br>
File “C:\Users\buiti\ZoeDepth-main\zoedepth\trainers\base_trainer.py”, line 268, in validate<br>
metrics, losses = self.validate_on_batch(batch, val_step=i)<br>
File “C:\Users\buiti\ZoeDepth-main\zoedepth\trainers\zoedepth_trainer.py”, line 166, in validate_on_batch<br>
l_depth = self.silog_loss(<br>
File “C:\Users\buiti\anaconda3\envs\gpu\lib\site-packages\torch\nn\modules\module.py”, line 1501, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
File “C:\Users\buiti\ZoeDepth-main\zoedepth\trainers\loss.py”, line 65, in forward<br>
input = input[mask]<br>
IndexError: too many indices for tensor of dimension 4</p>
<p>source code:<br>
def forward(self, input, target, mask=None, interpolate=True, return_interpolated=False):<br>
input = extract_key(input, KEY_OUTPUT)<br>
if input.shape[-1] != target.shape[-1] and interpolate:<br>
input = nn.functional.interpolate(<br>
input, target.shape[-2:], mode=‘bilinear’, align_corners=True)<br>
intr_input = input<br>
else:<br>
intr_input = input</p>
<pre><code>    if target.ndim == 3:
        target = target.unsqueeze(1)

    if mask is not None:
        if mask.ndim == 3:
            mask = mask.unsqueeze(1)

        input = input[mask]
        target = target[mask]
</code></pre>
<p>Epoch: 1/5. Loop: Train:  25% 1513/6058 [38:44&lt;1:56:04,  1.53s/it]input shape: torch.Size([4, 1, 480, 640])<br>
target shape: torch.Size([4, 1, 480, 640])<br>
mask shape: torch.Size([4, 1, 480, 640])</p>
<p>Epoch: 1/5. Loop: Validation:   0% 0/651 [00:00&lt;?, ?it/s]input shape: torch.Size([1, 1, 640, 3])<br>
target shape: torch.Size([1, 1, 480, 640, 3])<br>
mask shape: torch.Size([1, 1, 480, 640, 3])<br>
Epoch: 1/5. Loop: Validation:   0% 0/651 [00:04&lt;?, ?it/s]<br>
Epoch: 1/5. Loop: Train:  25% 1513/6058 [38:53&lt;1:56:50,  1.54s/it]</p>

ptrblck: <p>The loss calculation fails with an indexing error using a mask tensor. Check the shape of the <code>input</code> as well as <code>mask</code> tensor and make sure the shapes are valid for the indexing operation.</p>

dung2603: <p>can you give me more suggestions? i don’t have idea</p>

ptrblck: <p>Add a debug <code>print</code> statement right before <code>input = input[mask]</code> and check the shapes. Once confirmed this line fails and the shapes are incompatible, track the tensors down and check which one was created in the wrong shape or reshaped into the wrong shape.</p>