Variable input resultis strange

username: KKView
cooked: <p>I am training a Classification network<br>
“arch” : “densenet161”,<br>
“epochs” : 50,<br>
“batch-size” : 24,<br>
“learning-rate” : 0.0005,<br>
“momentum” : 0.9,<br>
“weight-decay” : 2e-5,<br>
“image_size” : [112, 112]<br>
If you use the usual method</p>
<pre><code class="lang-auto">criterion = nn.CrossEntropyLoss().cuda(args.gpu)
optimizer = torch.optim.SGD(model.parameters(), args.lr,
                                momentum=args.momentum, 
                                weight_decay=args.weight_decay)

train_dataset = datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.Resize(args.image_size),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]))
train_loader = torch.utils.data.DataLoader(
    train_dataset, batch_size=args.batch_size, shuffle=None, pin_memory=True, sampler=None)

for i, (images, target) in enumerate(train_loader):
    if torch.cuda.is_available(): 
        if args.gpu is not None: 
            images = images.cuda(args.gpu, non_blocking=True)
        target = target.cuda(args.gpu, non_blocking=True)

    # compute output
    output = model(images)
    loss = criterion(output, target)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
</code></pre>
<p>the result will be normal.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/5/45661bfa209a0db798096ff3655e4874bd93960c.png" data-download-href="https://discuss.pytorch.org/uploads/default/45661bfa209a0db798096ff3655e4874bd93960c" title="densenet161_loss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45661bfa209a0db798096ff3655e4874bd93960c_2_500x500.png" alt="densenet161_loss" data-base62-sha1="9TVIf3U3vvqT6RnINMUKDCibVGQ" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45661bfa209a0db798096ff3655e4874bd93960c_2_500x500.png, https://discuss.pytorch.org/uploads/default/original/3X/4/5/45661bfa209a0db798096ff3655e4874bd93960c.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/5/45661bfa209a0db798096ff3655e4874bd93960c.png 2x" data-dominant-color="FCFCFD"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">densenet161_loss</span><span class="informations">600×600 13.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/2/523658b8a4592248272f4a1b0249cc9a916fba2a.png" data-download-href="https://discuss.pytorch.org/uploads/default/523658b8a4592248272f4a1b0249cc9a916fba2a" title="densenet161_valacc1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/523658b8a4592248272f4a1b0249cc9a916fba2a_2_500x500.png" alt="densenet161_valacc1" data-base62-sha1="bJhAygM5wuzhViQECnnVNpzuvfI" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/523658b8a4592248272f4a1b0249cc9a916fba2a_2_500x500.png, https://discuss.pytorch.org/uploads/default/original/3X/5/2/523658b8a4592248272f4a1b0249cc9a916fba2a.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/2/523658b8a4592248272f4a1b0249cc9a916fba2a.png 2x" data-dominant-color="FBFCFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">densenet161_valacc1</span><span class="informations">600×600 18.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/b/7b636dac5f27cc84ce927fb0cc800ca8bad056a7.png" data-download-href="https://discuss.pytorch.org/uploads/default/7b636dac5f27cc84ce927fb0cc800ca8bad056a7" title="densenet161_valloss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/b/7b636dac5f27cc84ce927fb0cc800ca8bad056a7_2_500x500.png" alt="densenet161_valloss" data-base62-sha1="hBxKHvAd81Po3myYXJEokf8Q6iP" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/b/7b636dac5f27cc84ce927fb0cc800ca8bad056a7_2_500x500.png, https://discuss.pytorch.org/uploads/default/original/3X/7/b/7b636dac5f27cc84ce927fb0cc800ca8bad056a7.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/b/7b636dac5f27cc84ce927fb0cc800ca8bad056a7.png 2x" data-dominant-color="FCFCFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">densenet161_valloss</span><span class="informations">600×600 17.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>But actually the resolution of my dataset is different, So I want my input to be variable size<br>
I’ve seen <a href="https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278/3">this</a> and <a href="https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278/13">this</a><br>
and change my program to<br>
(1) gradient accumulation: batch-size = 1</p>
<pre><code class="lang-auto">train_dataset = datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]))

def my_collate(batch):
    data = [item[0] for item in batch]
    target = [item[1] for item in batch]
    target = torch.LongTensor(target)
    return [data, target] 
train_loader = torch.utils.data.DataLoader(
    train_dataset, batch_size=args.batch_size, collate_fn=my_collate, shuffle=None, pin_memory=True, sampler=None)

if torch.cuda.is_available(): 
    if args.gpu is not None: 
        images = images.cuda(args.gpu, non_blocking=True)
    target = target.cuda(args.gpu, non_blocking=True)
# compute output
output = model(images)
if i &lt; int(len(train_loader)/args.true_batch_size)*args.true_batch_size:
    loss = criterion(output, target) / args.true_batch_size
else:
    loss = criterion(output, target) / len(train_loader)%args.true_batch_size
loss.backward() 
if ((i+1) % args.true_batch_size) == 0 or (i+1) == len(train_loader):
    optimizer.step()
    optimizer.zero_grad()
</code></pre>
<p>(2)Stack output: batch-size = 24</p>
<pre><code class="lang-auto">train_dataset = datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]))

def my_collate(batch):
    data = [item[0] for item in batch]
    target = [item[1] for item in batch]
    target = torch.LongTensor(target)
    return [data, target] 
train_loader = torch.utils.data.DataLoader(
    train_dataset, batch_size=args.batch_size, collate_fn=my_collate, shuffle=None, pin_memory=True, sampler=None)

if torch.cuda.is_available(): 
    #if args.gpu is not None: 
    #    images = images.cuda(args.gpu, non_blocking=True)
    target = target.cuda(args.gpu, non_blocking=True)
# compute output
for image in images: ##
    output = model(image.unsqueeze(0).cuda(args.gpu, non_blocking=True))
    if j == 0:
        outputs = output.clone()
    outputs = torch.cat((outputs,output), 0)
    j += 1
output = outputs[1:]
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
</code></pre>
<p>But the results are terrible<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc0cad66e7b3cde09c2cab92b1672e5e93164f54.png" data-download-href="https://discuss.pytorch.org/uploads/default/fc0cad66e7b3cde09c2cab92b1672e5e93164f54" title="densenet161_loss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/c/fc0cad66e7b3cde09c2cab92b1672e5e93164f54_2_500x500.png" alt="densenet161_loss" data-base62-sha1="zXJtZa921QqKwuAiTOHRUoObCTy" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/c/fc0cad66e7b3cde09c2cab92b1672e5e93164f54_2_500x500.png, https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc0cad66e7b3cde09c2cab92b1672e5e93164f54.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc0cad66e7b3cde09c2cab92b1672e5e93164f54.png 2x" data-dominant-color="FBFCFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">densenet161_loss</span><span class="informations">600×600 16.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/6/a6bba65550cf71bc65c5d005cd53242193f77999.png" data-download-href="https://discuss.pytorch.org/uploads/default/a6bba65550cf71bc65c5d005cd53242193f77999" title="densenet161_valacc1"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/6/a6bba65550cf71bc65c5d005cd53242193f77999.png" alt="densenet161_valacc1" data-base62-sha1="nMZisLHZMqPG3ANQBCZNIDrS9EB" width="500" height="500" data-dominant-color="FCFCFD"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">densenet161_valacc1</span><span class="informations">600×600 5.06 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/e/eec898281b0822fed39cfcba04e1b80fbccc79bb.png" data-download-href="https://discuss.pytorch.org/uploads/default/eec898281b0822fed39cfcba04e1b80fbccc79bb" title="densenet161_valloss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/e/eec898281b0822fed39cfcba04e1b80fbccc79bb_2_500x500.png" alt="densenet161_valloss" data-base62-sha1="y4npnKKcyVpgb3ev6jwnO2c5Kjh" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/e/eec898281b0822fed39cfcba04e1b80fbccc79bb_2_500x500.png, https://discuss.pytorch.org/uploads/default/original/3X/e/e/eec898281b0822fed39cfcba04e1b80fbccc79bb.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/e/eec898281b0822fed39cfcba04e1b80fbccc79bb.png 2x" data-dominant-color="FAFBFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">densenet161_valloss</span><span class="informations">600×600 25.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
did i miss something?</p>