RuntimeError:Given groups=1，weight of size 256 304 3 3，expected input[1,2096,129,129]to have 304 channels， but got 2096 channels instead

username: kungkookie
cooked: <p>An error occurred while I was trying to train the VOC2007 data set:</p>
<p>RuntimeError:Given groups=1, weight of size 256 304 3 3,expected input[1,2096,129,129]to have 304 channels, but got 2096 channels instead.</p>
<p>The error code is:<br>
class Decoder(nn.Module):<br>
def <strong>init</strong>(self, num_classes, backbone, BatchNorm):<br>
super(Decoder, self).<strong>init</strong>()<br>
if backbone == ‘resnet’ or backbone == ‘drn’:<br>
low_level_inplanes = 256<br>
elif backbone == ‘xception’:<br>
low_level_inplanes = 128<br>
elif backbone == ‘mobilenet’:<br>
low_level_inplanes = 24<br>
else:<br>
raise NotImplementedError<br>
self.conv1 = nn.Conv2d(low_level_inplanes, 48, 1, bias=False)<br>
self.bn1 = BatchNorm(48)<br>
self.relu = nn.ReLU()<br>
self.last_conv = nn.Sequential(nn.Conv2d(304, 256, kernel_size=3, stride=1, padding=1, bias=False),<br>
BatchNorm(256),<br>
nn.ReLU(),<br>
nn.Dropout(0.5),<br>
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),<br>
BatchNorm(256),<br>
nn.ReLU(),<br>
nn.Dropout(0.1),<br>
nn.Conv2d(256, num_classes, kernel_size=1, stride=1))<br>
self._init_weight()</p>
<pre><code>def forward(self, x, low_level_feat):
    low_level_feat = self.conv1(low_level_feat)
    low_level_feat = self.bn1(low_level_feat)
    low_level_feat = self.relu(low_level_feat)
    x = F.interpolate(x, size=low_level_feat.size()[2:], mode='bilinear', align_corners=True)
    x = torch.cat((x, low_level_feat), dim=1)
    x = self.last_conv(x)

    return x
</code></pre>
<p>VOC data set is a 3-channel image, I used it to train deeplab v3plus network, 23 lines of the above code are written into the input channel bit 304, how should I calculate the channel number of the input 3-channel image without a convolution or other layer training? According to the error, directly modify 304 to 2096? Hope to get the answer, thank you very much</p>

username: ptrblck
cooked: <p>It seems this line of code:</p>
<aside class="quote no-group" data-username="kungkookie" data-post="1" data-topic="74985">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/aeb1de/48.png" class="avatar"> kungkookie:</div>
<blockquote>
<p>x = torch.cat((x, low_level_feat), dim=1)</p>
</blockquote>
</aside>
<p>is increasing the number of input channels, so you would have to set <code>in_channels</code> of <code>self.last_conv</code> to <code>2096</code> based on the error message.</p>

username: kungkookie
cooked: <p>Thanks, this problem was solved after the modification, but a new problem appeared:<br>
lonly batches of spatial targets supported(non-empty 3D tensors)but got targets of size:[2，513,513,3]<br>
Error code points to:<br>
1.trainer.py<br>
def training(self, epoch):<br>
train_loss = 0.0<br>
self.model.train()<br>
tbar = tqdm(self.train_loader)<br>
num_img_tr = len(self.train_loader)<br>
for i, sample in enumerate(tbar):<br>
image, target = sample[‘image’], sample[‘label’]<br>
if self.config[‘network’][‘use_cuda’]:<br>
image, target = image.cuda(), target.cuda()<br>
self.scheduler(self.optimizer, i, epoch, self.best_pred)<br>
self.optimizer.zero_grad()<br>
output = self.model(image)<br>
loss = self.criterion(output, target)<br>
loss.backward()<br>
self.optimizer.step()<br>
train_loss += loss.item()<br>
tbar.set_description(‘Train loss: %.3f’ % (train_loss / (i + 1)))<br>
self.writer.add_scalar(‘train/total_loss_iter’, loss.item(), i + num_img_tr * epoch)</p>
<pre><code>        # Show 10 * 3 inference results each epoch
        if i % (num_img_tr // 10) == 0:
            global_step = i + num_img_tr * epoch
            self.summary.visualize_image(self.writer, self.config['dataset']['dataset_name'], image, target, output, global_step)

    self.writer.add_scalar('train/total_loss_epoch', train_loss, epoch)
    print('[Epoch: %d, numImages: %5d]' % (epoch, i * self.config['training']['batch_size'] + image.data.shape[0]))
    print('Loss: %.3f' % train_loss)
</code></pre>
<p>line 113,loss=loss = self.criterion(output, target)<br>
2.loss.py<br>
def CrossEntropyLoss(self, logit, target):<br>
n, c, h, w = logit.size()<br>
criterion = nn.CrossEntropyLoss(weight=self.weight, ignore_index=self.ignore_index,<br>
size_average=self.size_average)<br>
if self.cuda:<br>
criterion = criterion.cuda()</p>
<pre><code>    loss = criterion(logit, target.long())

    if self.batch_average:
        loss /= n

    return loss
</code></pre>
<p>I change loss = criterion(logit, target.long()) to loss = criterion(logit, torch.squeeze(target).long()) in loss.py, but no use ,Maybe why</p>

username: ptrblck
cooked: <p>If you are dealing with a multi-class segmentation use case, the targets should have the shape <code>[batch_size, height, width]</code> and contain the class indices in the range <code>[0, nb_classes-1]</code>.<br>
Your target seems to be a channel-last RGB image tensor, which will not work.</p>
<p>If each class in your target uses a specific color code, you would have to convert these color codes to class indices first.</p>

username: kungkookie
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="74985" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>If you are dealing with a multi-class segmentation use case, the targets should have the shape <code>[batch_size, height, width]</code> and contain the class indices in the range <code>[0, nb_classes-1]</code> .<br>
Your target seems to be a channel-last RGB image tensor, which will not work.</p>
<p>If each class in your target uses a specific color code, you would have to convert these color codes to class indices first.</p>
</blockquote>
</aside>
<p>The data set I used is the VOC2007 version. I have checked the label chart, which is all 8-bit single-channel index chart with 21 categories. I have no idea what the problem is</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="kungkookie" data-post="5" data-topic="74985">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/aeb1de/48.png" class="avatar"> kungkookie:</div>
<blockquote>
<p>which is all 8-bit single-channel index chart with 21 categories.</p>
</blockquote>
</aside>
<p>In that case some transformation or other processing might have created these channel-last 3-channel target tensors in the provided shape <code>[2, 513, 513, 3]</code>, which won’t work.<br>
Could you check, where your single-channel targets get converted to 3-channels?</p>