Questions about TenCrop/FiveCrop

username: 29233
cooked: <p>I am trying to deal increase my resnet50’s proformance on a relatively small size of training examples and would like to use TenCrop to enlarge my dataset.<br>
Knowing that TenCrop may cause the problem of 4D/5D, I copied the codes on the document.However, this still doesn’t works and I got following error:<br>
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 3, 7, 7], but got 5-dimensional input of size [16, 10, 3, 224, 224] instead</p>
<p>Can I have any idea how to fix this?</p>
<p>Thanks in advance!<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/8/983b1e380425a1b4b2c73c8ba9d45708e7e3a0f0.png" data-download-href="https://discuss.pytorch.org/uploads/default/983b1e380425a1b4b2c73c8ba9d45708e7e3a0f0" title="LN)(})D)HFYH@RZA0{P4BD"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/8/983b1e380425a1b4b2c73c8ba9d45708e7e3a0f0_2_690x136.png" alt="LN)(})D)HFYH@RZA0{P4BD" data-base62-sha1="lIHeABDjwUUieHQFzWaA4GMxkg8" width="690" height="136" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/8/983b1e380425a1b4b2c73c8ba9d45708e7e3a0f0_2_690x136.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/8/983b1e380425a1b4b2c73c8ba9d45708e7e3a0f0_2_1035x204.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/8/983b1e380425a1b4b2c73c8ba9d45708e7e3a0f0.png 2x" data-dominant-color="F5F5F1"><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">LN)(})D)HFYH@RZA0{P4BD</span><span class="informations">1045×206 25.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></p>

username: Andrei_Cristea
cooked: <p>I think you’re missing the later part where you roll out the stacked tensor along the first (minibatch) dimension, marked with !! in the comments below. Not sure how to do this as part of a transforms chain but here’s an example below of how to do it as part of your forward pass.</p>
<p>Also remember that at the end you’ll have X labels and 10 * X amount of data now, so you have to do some kind of collapsing function at the end to get the 10 * X model outputs back to X ultimate labels.</p>
<p>Here’s an example using FiveCrop (so the <code>5</code> below would be instead <code>10</code> if you used TenCrop):</p>
<pre><code class="lang-auto">for i_step, sample in enumerate(train_loader):
    inputs = sample[0].to(device)
    inputs = torch.stack(transforms.FiveCrop(512)(inputs), dim=0)  # this results in a (5, batchsize, 3, 512, 512) shaped Tensor
    inputs = inputs.view(-1, 3, 512, 512)  # !! i think you were missing this !! rolling out the tensor along the first (minibatch) dimension, effectively treating this as if it were a minibatch of size 5 * batchsize
    inputs = transforms.Resize((HEIGHT, WIDTH))(inputs)
    net_output = net(inputs)
    net_output = net_output.view(5, -1, n_classes)
    net_output = net_output.max(dim=0)  # This "collapses" the output back onto the desired size.
    loss = criterion(net_output, sample[1].to(device))
    loss.backward()
    optimizer.step()
</code></pre>
<p>Hope this helps!</p>

username: 29233
cooked: <p>Thank you so much!<br>
this really helps me a lot <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>