Prevent Overfitting

beto16: <p>How can I prevent overfitting when the dataset is not to large. My dataset consists of 5 classes with a total dataset size about 15k images.</p>
<p>I have tried data augmentation but doesn’t help too much.</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self, num_channels):
        super(Net, self).__init__()

        self.num_channels = num_channels

        self.conv1 = nn.Conv2d(in_channels=1,
                               out_channels=self.num_channels,
                               kernel_size=3,
                               stride=2,
                               padding=0)
        self.bn1 = nn.BatchNorm2d(self.num_channels)

        self.conv2 = nn.Conv2d(in_channels=self.num_channels,
                               out_channels=self.num_channels * 2,
                               kernel_size=3,
                               stride=1,
                               padding=0)
        self.bn2 = nn.BatchNorm2d(self.num_channels * 2)

        self.conv3 = nn.Conv2d(in_channels=self.num_channels * 2,
                               out_channels=self.num_channels * 4,
                               kernel_size=3,
                               stride=1,
                               padding=0)
        self.bn3 = nn.BatchNorm2d(self.num_channels * 4)

        self.fc1 = nn.Linear(self.num_channels * 4 * 6 * 6, self.num_channels * 4)
        self.fcbn1 = nn.BatchNorm1d(self.num_channels * 4)
        self.fc2 = nn.Linear(self.num_channels * 4, 5)

    def forward(self, x):
        x = self.bn1(self.conv1(x))  # Salida: self.num_channels x 128 x 128
        x = F.relu(F.max_pool2d(x, 2))  # salida: num_channels x 63 x 63
        x = self.bn2(self.conv2(x))  # Salida: self.num_channels*2 x 31 x 31
        x = F.relu(F.max_pool2d(x, 2))  # salida: num_channels*2 x 29 x 29
        x = self.bn3(self.conv3(x))  # Salida: self.num_channels*4 x 14 x 14
        x = F.relu(F.max_pool2d(x, 2))  # salida: num_channels*4 x 12 x 12

        # Flatten
        x = x.view(-1, self.num_channels * 4 * 6 * 6)

        # fc
        x = F.relu(self.fcbn1(self.fc1(x)))
        x = F.dropout(x, p=0.5, training=True)

        x = self.fc2(x)

        return x
</code></pre>
<p>Here is the loss<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/b/9b7aa7e0ea1ed98601ad1ab58a2541b46ebad00f.png" data-download-href="https://discuss.pytorch.org/uploads/default/9b7aa7e0ea1ed98601ad1ab58a2541b46ebad00f" title="Loss - AdamW - lr_1e-05 - bs_32 - ep_1400"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b7aa7e0ea1ed98601ad1ab58a2541b46ebad00f_2_690x459.png" alt="Loss - AdamW - lr_1e-05 - bs_32 - ep_1400" data-base62-sha1="mbqNoxAxzuIyk6ePwNs9OUiW8vt" width="690" height="459" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b7aa7e0ea1ed98601ad1ab58a2541b46ebad00f_2_690x459.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b7aa7e0ea1ed98601ad1ab58a2541b46ebad00f_2_1035x688.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/b/9b7aa7e0ea1ed98601ad1ab58a2541b46ebad00f.png 2x" data-dominant-color="F7F1EC"><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">Loss - AdamW - lr_1e-05 - bs_32 - ep_1400</span><span class="informations">1200×800 70.6 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>