ValueError:Expected input batch_size(3) to match target' batch_size(9)

kungkookie: <p>my code as:<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()<br>
target=target.reshape(-1,513,513)<br>
loss = criterion(logit, target.long())</p>
<pre><code>    if self.batch_average:
        loss /= n

    return loss
</code></pre>
<p>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)

    #save last checkpoint
    self.saver.save_checkpoint({
        'epoch': epoch + 1,
</code></pre>
<h1>‘state_dict’: self.model.module.state_dict(),</h1>
<pre><code>        'state_dict': self.model.state_dict(),
        'optimizer': self.optimizer.state_dict(),
        'best_pred': self.best_pred,
    }, is_best = False, filename='checkpoint_last.pth.tar')

    #if training on a subset reshuffle the data 
    if self.config['training']['train_on_subset']['enabled']:
        self.train_loader.dataset.shuffle_dataset()</code></pre>

ptrblck: <p>Could you print the shape of <code>target</code> before the <code>reshape</code> operation as well as the <code>output</code> for the failing batch?</p>

kungkookie: <p>yep，before reshape:torchsize[3,21,513,513]，output:torchsize[3,21,513,513]</p>

ptrblck: <p>In this case, <code>target = target.reshape(-1,513,513)</code> will create a <code>target</code> tensor of <code>[63, 513, 513]</code>, which won’t match the batch size of the output of your model.</p>
<p>Is your target one-hot encoded?<br>
If so, create class indices via <code>target = torch.argmax(target, dim=1)</code> and rerun your code.</p>

kungkookie: <p>i did，but RuntimeError:input and target batch or spatial batch sizes dont match:target[3×513×3]，input[3×21×513×513] at /pytorch/aten/src/THCUNN/generic/SpaticialClassNLLcriterion.cu:23</p>

ptrblck: <p>If you call <code>target = torch.argmax(target, dim=1)</code> on a <code>target</code> tensor of the shape <code>[3, 21, 513, 513]</code>, the result will have a shape of <code>[3, 513, 513]</code>.<br>
I’m not sure, why your target has now the shape <code>[3, 513, 3]</code>.</p>
<p>Could you recheck the shape of the <code>target</code> before applying the <code>torch.argmax</code> operation, please?</p>

kungkookie: <p>new error:<br>
NotImplementedError</p>
<p>trainer.py<br>
if i % (num_img_tr // 10) == 0:<br>
global_step = i + num_img_tr * epoch<br>
<strong>self.summary.visualize_image(self.writer, self.config[‘dataset’][‘dataset_name’], image, target, output, global_step)</strong></p>
<p>summaries.py<br>
def visualize_image(self, writer, dataset, image, target, output, global_step):<br>
grid_image = make_grid(image[:3].clone().cpu().data, 3, normalize=True)<br>
writer.add_image(‘Image’, grid_image, global_step)<br>
grid_image = make_grid(decode_seg_map_sequence(torch.max(output[:3], 1)[1].<strong>detach().cpu().numpy(),</strong><br>
**                                                       dataset=dataset), 3, normalize=False, range=(0, 255))**<br>
**        writer.add_image(‘Predicted label’, grid_image, global_step)**<br>
grid_image = make_grid(decode_seg_map_sequence(torch.squeeze(target[:3], 1).detach().cpu().numpy(),<br>
dataset=dataset), 3, normalize=False, range=(0, 255))<br>
writer.add_image(‘Groundtruth label’, grid_image, global_step)</p>
<p>datagen_utils.py<br>
def decode_segmap(label_mask, dataset, plot=False):<br>
“”“Decode segmentation class labels into a color image<br>
Args:<br>
label_mask (np.ndarray): an (M,N) array of integer values denoting<br>
the class label at each spatial location.<br>
plot (bool, optional): whether to show the resulting color image<br>
in a figure.<br>
Returns:<br>
(np.ndarray, optional): the resulting decoded color image.<br>
“””<br>
if dataset == ‘pascal’ or dataset == ‘coco’:<br>
n_classes = 21<br>
label_colours = get_pascal_labels()<br>
elif dataset == ‘cityscapes’:<br>
n_classes = 19<br>
label_colours = get_cityscapes_labels()</p>
<pre><code>elif dataset == 'deepfashion':
    n_classes = 13
    label_colours = get_deepfashion_labels()

elif dataset == 'braintumor':
    n_classes = 3
    label_colours = get_braintumor_labels()


**else:**
</code></pre>
<p>**        raise NotImplementedError**</p>

ptrblck: <p>It seems that your specified dataset is missing in the <code>visualize_image</code> method and you might need to write the function manually.</p>