AssertionError: Traceback (most recent call last):

username: Yellow-Champagne
cooked: <p>D:\Program Files\Anaconda3\envs\lei\envs\py36-pytorch\python.exe" D:/zgy/GCC/GCC-SFCN-master/train.py<br>
Traceback (most recent call last):<br>
File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 250, in <br>
main()<br>
File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 100, in main<br>
i_tb = train(train_loader, net, optimizer, epoch, i_tb)<br>
File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 120, in train<br>
for i, data in enumerate(train_loader, 0):<br>
File “D:\Program Files\Anaconda3\envs\lei\envs\py36-pytorch\lib\site-packages\torch\utils\data\dataloader.py”, line 286, in <strong>next</strong><br>
return self._process_next_batch(batch)<br>
File “D:\Program Files\Anaconda3\envs\lei\envs\py36-pytorch\lib\site-packages\torch\utils\data\dataloader.py”, line 307, in _process_next_batch<br>
raise batch.exc_type(batch.exc_msg)<br>
AssertionError: Traceback (most recent call last):<br>
File “D:\Program Files\Anaconda3\envs\lei\envs\py36-pytorch\lib\site-packages\torch\utils\data\dataloader.py”, line 57, in _worker_loop<br>
samples = collate_fn([dataset[i] for i in batch_indices])<br>
File “D:\Program Files\Anaconda3\envs\lei\envs\py36-pytorch\lib\site-packages\torch\utils\data\dataloader.py”, line 57, in <br>
samples = collate_fn([dataset[i] for i in batch_indices])<br>
File “D:\zgy\GCC\GCC-SFCN-master\datasets\UCF_QNRF.py”, line 40, in <strong>getitem</strong><br>
img, den = self.main_transform(img,den)<br>
File “D:\zgy\GCC\GCC-SFCN-master\misc\transforms.py”, line 16, in <strong>call</strong><br>
img, mask = t(img, mask)<br>
File “D:\zgy\GCC\GCC-SFCN-master\misc\transforms.py”, line 50, in <strong>call</strong><br>
assert img.size == mask.size<br>
AssertionError</p>

username: ptrblck
cooked: <p>Could you post the code for <code>main_transform</code> and <code>t(img, mask)</code>?<br>
Based on the error message this function needs <code>img</code> and <code>mask</code> to have the same size, which is not the case for the current pair.</p>

username: Yellow-Champagne
cooked: <p>THANK YOU VERY MUCH</p>
<pre><code class="lang-python">import numbers
import random
import numpy as np
from PIL import Image, ImageOps, ImageFilter
from config import cfg
import torch
# ===============================img tranforms============================

class Compose(object):
    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, img, mask, bbx=None):
        if bbx is None:
            for t in self.transforms:
                img, mask = t(img, mask)
            return img, mask
        for t in self.transforms:
            img, mask, bbx = t(img, mask, bbx)
        return img, mask, bbx

class RandomHorizontallyFlip(object):
    def __call__(self, img, mask, bbx=None):
        if random.random() &lt; 0.5:
            if bbx is None:
                return img.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT)
            w, h = img.size
            xmin = w - bbx[:,3]
            xmax = w - bbx[:,1]
            bbx[:,1] = xmin
            bbx[:,3] = xmax
            return img.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT), bbx
        if bbx is None:
            return img, mask
        return img, mask, bbx

class RandomCrop(object):
    def __init__(self, size, padding=0):
        if isinstance(size, numbers.Number):
            self.size = (int(size), int(size))
        else:
            self.size = size
        self.padding = padding

    def __call__(self, img, mask):
        if self.padding &gt; 0:
            img = ImageOps.expand(img, border=self.padding, fill=0)
            mask = ImageOps.expand(mask, border=self.padding, fill=0)

        assert img.size == mask.size
        w, h = img.size
        th, tw  = self.size
        if w == tw and h == th:
            return img, mask
        if w &lt; tw or h &lt; th:
            return img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST)

        x1 = random.randint(0, w - tw)
        y1 = random.randint(0, h - th)
        return img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th))


class CenterCrop(object):
    def __init__(self, size):
        if isinstance(size, numbers.Number):
            self.size = (int(size), int(size))
        else:
            self.size = size

    def __call__(self, img, mask):
        w, h = img.size
        th, tw = self.size
        x1 = int(round((w - tw) / 2.))
        y1 = int(round((h - th) / 2.))
        return img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th))



class FreeScale(object):
    def __init__(self, size):
        self.size = size  # (h, w)

    def __call__(self, img, mask):
        return img.resize((self.size[1], self.size[0]), Image.BILINEAR), mask.resize((self.size[1], self.size[0]), Image.NEAREST)


class ScaleDown(object):
    def __init__(self, size):
        self.size = size  # (h, w)

    def __call__(self, mask):
        return  mask.resize((self.size[1]/cfg.TRAIN.DOWNRATE, self.size[0]/cfg.TRAIN.DOWNRATE), Image.NEAREST)


class Scale(object):
    def __init__(self, size):
        self.size = size

    def __call__(self, img, mask):
        if img.size != mask.size:
            print(img.size)
            print(mask.size)           
        assert img.size == mask.size
        w, h = img.size
        if (w &lt;= h and w == self.size) or (h &lt;= w and h == self.size):
            return img, mask
        if w &lt; h:
            ow = self.size
            oh = int(self.size * h / w)
            return img.resize((ow, oh), Image.BILINEAR), mask.resize((ow, oh), Image.NEAREST)
        else:
            oh = self.size
            ow = int(self.size * w / h)
            return img.resize((ow, oh), Image.BILINEAR), mask.resize((ow, oh), Image.NEAREST)


class RandomRotate(object):
    """Crops the given PIL.Image at a random location to have a region of
    the given size. size can be a tuple (target_height, target_width)
    or an integer, in which case the target will be of a square shape (size, size)
    """

    def __init__(self, angle):
        self.angle = angle

    def __call__(self, image, label):
        assert label is None or image.size == label.size

        w, h = image.size
        p = max((h, w))
        angle = random.randint(0, self.angle * 2) - self.angle

        label = pad_image('constant', label, h, h, w, w, value=0)
        label = label.rotate(angle, resample=Image.NEAREST)
        label = label.crop((w, h, w + w, h + h))

        image = pad_image('reflection', image, h, h, w, w)
        image = image.rotate(angle, resample=Image.BILINEAR)
        image = image.crop((w, h, w + w, h + h))
        return image, label


class RoadRegionCrop(object):
    def __init__(self, road_rate_h):
        self.road_rate_h = road_rate_h

    def __call__(self, img, mask):
        w, h = img.size
        road_rate_h = self.road_rate_h
        y1 = int(round((h*road_rate_h)))
        crop_bbx = (0, y1, w, h)
        return img.crop(crop_bbx), mask.crop(crop_bbx)


# ===============================label tranforms============================

class DeNormalize(object):
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    def __call__(self, tensor):
        for t, m, s in zip(tensor, self.mean, self.std):
            t.mul_(s).add_(m)
        return tensor


class MaskToTensor(object):
    def __call__(self, img):
        return torch.from_numpy(np.array(img, dtype=np.int32)).long()


class LabelNormalize(object):
    def __init__(self, para):
        self.para = para

    def __call__(self, tensor):
        # tensor = 1./(tensor+self.para).log()
        tensor = torch.from_numpy(np.array(tensor))
        tensor = tensor*self.para
        return tensor

class GTScaleDown(object):
    def __init__(self, factor=8):
        self.factor = factor

    def __call__(self, img):
        w, h = img.size
        if self.factor==1:
            return img
        tmp = np.array(img.resize((w/self.factor, h/self.factor), Image.BICUBIC))*self.factor*self.factor
        img = Image.fromarray(tmp)
        return img
</code></pre>

username: ptrblck
cooked: <p>Thanks for the code!<br>
As said before, your transformations (e.g. <code>RandomCrop</code>) need an image and the corresponding mask in t he same size, which is not the case.<br>
Are the images and masked stored in different resolutions or are you resizing (one of) them?</p>

username: Yellow-Champagne
cooked: <p>Sorry, I don’t understand what the mask means. Can you give me a brief talk?Thank you.</p>

username: ptrblck
cooked: <p>It looks like you are dealing with e.g. a segmentation task, since you are loading an image and a mask.<br>
Is this correct?<br>
If so, you should make sure both inputs have the same size as your transformations won’t work otherwise.</p>

username: Yellow-Champagne
cooked: <p>This code I downloaded from github for crowd counting, this happened after I replaced the author’s data set with me. I think it should be that my picture is different from the author’s picture. But I don’t know how to modify it.Thank you very much.</p>

username: ptrblck
cooked: <p>Could you explain your input data a bit?<br>
I.e. what kind of input images and masks are you using?<br>
Somewhere in your code you are most likely using a <code>Dataset</code>, which loads the image and the corresponding mask. Are you assuming the mask size is different than the image size?<br>
If so, would resizing work?</p>

username: Yellow-Champagne
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="12" data-topic="45258">
<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>mask</p>
</blockquote>
</aside>
<p>My dataset includes images and label. The image is a crowd map, and the label is the coordinates of the center point of each person’s head in the image. Does the mask mean labeling?</p>

username: ptrblck
cooked: <p>The code base you are using assumes that you are providing an image mask, not coordinates.<br>
If you don’t have this type of mask, I would recommend to write a custom <code>Dataset</code>, loading the input images and the corresponding head positions.</p>

username: Yellow-Champagne
cooked: <p>Ok, I will try your method, thank you very very much.</p>