Is my training loop for BC done in the right way?

username: SlimShadys
cooked: <p>Hello everyone. I’m doing a BC task on DeepFake recognition.</p>
<p>I have the following CSV for the features of a video as follow:</p>
<pre><code class="lang-auto">===================================================
IDX    ldmk1_x     ldmk2_x     ldmk3_x     ldmk4_x     ldmk5_x  ...    ldmk64_y    ldmk65_y    ldmk66_y    ldmk67_y    ldmk68_y
0   199.000000  199.666667  200.666667  201.833333  205.666667  ...  140.166667  140.666667  140.333333  140.833333  140.000000      
1   198.166667  198.500000  199.833333  201.500000  205.833333  ...  143.833333  144.666667  144.500000  145.166667  144.333333      
2   197.833333  198.166667  199.666667  202.000000  206.000000  ...  147.000000  147.833333  147.333333  148.000000  147.333333      
3   197.000000  197.666667  199.000000  200.833333  204.833333  ...  146.333333  147.500000  147.333333  147.666667  147.000000      
4   190.000000  190.333333  191.500000  193.500000  196.833333  ...  147.166667  148.000000  148.166667  148.500000  148.166667      
..         ...         ...         ...         ...         ...  ...         ...         ...         ...         ...         ...      
59  258.666667  258.666667  259.166667  260.666667  262.666667  ...   69.166667   69.833333   70.833333   71.333333   71.166667      
60  255.666667  256.333333  257.333333  259.333333  262.500000  ...   67.333333   67.000000   67.833333   67.833333   68.500000      
61  266.500000  266.666667  267.500000  268.500000  270.833333  ...   63.000000   64.500000   65.000000   65.000000   65.000000      
62  295.333333  294.500000  294.000000  294.000000  295.166667  ...   68.666667   69.500000   69.333333   69.166667   68.666667      
63  277.000000  276.500000  276.500000  277.166667  279.166667  ...   68.333333   69.000000   70.833333   71.000000   70.833333   
</code></pre>
<p>Which is composed by 64 rows and 136 columns, for now (I’m still adding a lot of rows).<br>
Each 16 rows denote a video (made of 16 frames) and the corresponding label (0/1) where 0 is denoted as Fake and 1 is denoted as Real.<br>
Each column corresponds to an anchor point on a person face and how it is evolving during the various 16 frames.</p>
<p>The labels are located into another CSV:</p>
<pre><code class="lang-auto">IDX  Label
0       0
16      0
32      1
48      1
</code></pre>
<p>It means that the first 16 rows of the features I have label 0.<br>
The rows from 16 to 32 have label 0.<br>
The rows from 32 to 48 have label 1.<br>
The rows from 48 to 64 have label 1.</p>
<p>Now, with this being said I instantiated my model as follows:</p>
<pre><code class="lang-auto">class LinearModel(nn.Module):
    def __init__(self):
        super(LinearModel, self).__init__()
        
        # 24 Features
        self.flatten = nn.Flatten()
        self.relu = nn.ReLU()

        self.fc1 = nn.Linear(2176, 1088, dtype=torch.float64, device=device)
        self.fc2 = nn.Linear(1088, 512, dtype=torch.float64, device=device)
        self.fc3 = nn.Linear(512, 256, dtype=torch.float64, device=device)
        self.fc4 = nn.Linear(256, 128, dtype=torch.float64, device=device)
        self.fc5 = nn.Linear(128, 1, dtype=torch.float64, device=device)
                
    def forward(self, x):
        x = self.flatten(x)

        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.relu(self.fc3(x))
        x = self.relu(self.fc4(x))

        x = self.fc5(x)
        
        return x.reshape(-1)
</code></pre>
<p>Where <code>2176</code> parameter in the <code>self.fc1</code> layer is given by the Flatten output of <code>self.flatten(x)</code></p>
<p>Now, the tricky part, the dataset. Knowing that my DataFrame is composed as I explained above, I did the following Dataset class:</p>
<pre><code class="lang-auto">class DeepFakeDataset(Dataset):
    
    def __init__(self, X, y, batch_sz):
        self.X = X
        self.y = y
        self.count = 0
        self.b = batch_sz
        
    def __len__(self):
        return len(self.X)
    
    def __getitem__(self, idx):

        if (idx % self.b == 0):
            self.count = 0

        row = self.count * 16
        
        features = self.X.iloc[row:row+16,:]
        label = self.y.iloc[self.count,0]

        features = torch.tensor(features.values)
        label = torch.tensor(label)

        self.count += 1

        return features, label
</code></pre>
<p>I couldn’t use the <code>idx</code> iterator of the Dataset Class for the rows, since when reaching <code>idx = 5</code>, the <code>row</code> value would have been <code>5 * 16 = 90</code> which doesn’t exist in my DataFrame.<br>
Hence, I used another counter which resets everytime I reach the 4 iterations.<br>
In this way, the <code>batch_size</code> value I set below must be at most equal to the number of videos I have in my CSV.</p>
<p>As for the <code>label</code> value, everytime, I am choosing the corresponding label associated with the <code>row:row+16</code> features.</p>
<p>This is then, my training loop, knowing that trainingX and trainingY are the CSVs I wrote above (validationX and validationY have 2 videos):</p>
<pre><code class="lang-auto"># Dataloaders
batch_size = 4

# Datasets
train_set = DeepFakeDataset(trainingX, trainingY, batch_size)
val_set = DeepFakeDataset(validationX, validationY, batch_size)

train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False)

trainLosses = []
trainAccuracies = []
valLosses = []
valAccuracies = []

epochs = 100
model = LinearModel().to(device)
criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5)

for epoch in tqdm(range(1, epochs+1), leave=True):
    model.train()
    index = 0
    indexV = 0
    num_correct_train = 0
    num_correct_val = 0

    for i, data in tqdm(enumerate(train_loader), leave=False):

        vid, label = data

        vid = vid.to(device)
        label = label.to(device)

        optimizer.zero_grad() # We empty the gradients

        ypred = model(vid)
        
        # We perform a Binary Classification Entropy Loss with Logits between the real value and our prediction
        lTrain = criterion(ypred, label.float())
        lTrain.backward() # We compute the gradients
        optimizer.step() # Parameters updated -&gt; Single step optimization

        # We round the prediction and:
        # if == ground truth -&gt; Correct prediction
        # if != ground truth -&gt; Wrong prediction
        ypred_tag = torch.round(torch.sigmoid(ypred))

        num_correct_train += (ypred_tag == label).sum().float()
        index += 1

    model.eval()

    for i, data in tqdm(enumerate(val_loader), leave=False):
        
        vid, label = data

        vid = vid.to(device)
        label = label.to(device)

        ypred_ = model(vid)

        # We perform a Binary Classification Entropy Loss with Logits between the real value and our prediction
        lVal = criterion(ypred_, label.float())

        # We round the prediction and:
        # if == ground truth -&gt; Correct prediction
        # if != ground truth -&gt; Wrong prediction
        ypred__tag = torch.round(torch.sigmoid(ypred_))

        num_correct_val += (ypred__tag == label).sum().float()
        indexV += 1

    train_loss_epoch = lTrain.item()
    trainLosses.append(train_loss_epoch)
    val_loss_epoch = lVal.item()
    valLosses.append(val_loss_epoch)

    train_acc_epoch = (num_correct_train/(label.shape[0]*index)).detach().cpu().numpy()
    trainAccuracies.append(train_acc_epoch)
    val_acc_epoch = (num_correct_val/(label.shape[0]*indexV)).detach().cpu().numpy()
    valAccuracies.append(val_acc_epoch)

    if (epoch % 10 == 0):
        print(f'\nEpoch {epoch+0:03}:')     
        print(f'\t- Training accuracy   : {train_acc_epoch:.3f} (avg. {np.average(trainAccuracies):.3f})')
        print(f'\t- Training loss       : {train_loss_epoch:.3f} (avg. {np.average(trainLosses):.3f})')
        print(f'\t- Validation accuracy : {val_acc_epoch:.3f} (avg. {np.average(valAccuracies):.3f})')
        print(f'\t- Validation loss     : {val_loss_epoch:.3f} (avg. {np.average(valLosses):.3f})')
        print("=========================================")
</code></pre>
<p>I use <code>BCEWithLogitsLoss()</code>, since I don’t have the <code>Sigmoid</code> function at the end of my model. Plus I calculate the accuracy by summing the rounding Sigmoid predictions and dividing them by the total number of samples, which I think is the correct way of doing so.</p>
<p>I have the following results after 100 epochs:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/4/24a395df4ef8b80603181c3c51d8795742381cb3.png" data-download-href="https://discuss.pytorch.org/uploads/default/24a395df4ef8b80603181c3c51d8795742381cb3" title="Figure_1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24a395df4ef8b80603181c3c51d8795742381cb3_2_666x500.png" alt="Figure_1" data-base62-sha1="5e7Fu6xa6DuIlg8D6xRws2StZTR" width="666" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24a395df4ef8b80603181c3c51d8795742381cb3_2_666x500.png, https://discuss.pytorch.org/uploads/default/original/3X/2/4/24a395df4ef8b80603181c3c51d8795742381cb3.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/4/24a395df4ef8b80603181c3c51d8795742381cb3.png 2x" data-dominant-color="FAF5F5"><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">Figure_1</span><span class="informations">704×528 32.4 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/6/6/664f16516e40626b1b3c0bafd11c0ed1afcee817.png" data-download-href="https://discuss.pytorch.org/uploads/default/664f16516e40626b1b3c0bafd11c0ed1afcee817" title="Figure_2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/664f16516e40626b1b3c0bafd11c0ed1afcee817_2_666x500.png" alt="Figure_2" data-base62-sha1="eB491fL4jcr5OgG1PKcmjbRlBHh" width="666" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/664f16516e40626b1b3c0bafd11c0ed1afcee817_2_666x500.png, https://discuss.pytorch.org/uploads/default/original/3X/6/6/664f16516e40626b1b3c0bafd11c0ed1afcee817.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/6/664f16516e40626b1b3c0bafd11c0ed1afcee817.png 2x" data-dominant-color="FCF7F7"><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">Figure_2</span><span class="informations">704×528 31.1 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>Now, ending this topic, I know that 4 samples might be very very very small, but I want to know if everything is set up correctly, before working on a very bigger dataset.<br>
I want to know if the results I’m having on the validation set (the fact that is constantly 0.5) are a reflection of the overfitting issue or there is something logically wrong in the snippet of code I provided.</p>
<p>Thanks to anyone who had patience to read this. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: marksaroufim
cooked: <p>Your dataset seems fairly small relative to the number of parameters so overfitting should be expected, did you try. Did you try making your model smaller and see if things look better?</p>

username: SlimShadys
cooked: <p>Honestly, I did scale down my model up to just one hidden layer from 2176 to 64 and then 64 to 1 (the prediction) but it seems way worse than the results I’m having.<br>
Of course, the main problem is the very very small dataset I’m using.</p>
<p>Here the question was regarding the <strong>getitem</strong> function of the DeepFakeDataset class.<br>
I want to be able to iterate over the self.X DataFrame every 16 rows and return the corresponding label from the self.y DataFrame.<br>
Is there any way I can make this iteration easier to compute without relying on the external self.count counter variable? Because that is the only solution I found.</p>
<p>Also, I wanted to know if there was any mistake in the training loop process, as with computing accuracies/losses and so on.</p>
<p>Thank you!</p>

username: thecho7
cooked: <p>Check the below code works</p>
<pre><code class="lang-auto">class DeepFakeDataset(Dataset):
    def __init__(self, X, y):
        self.X = X
        self.y = y
        
    def __len__(self):
        return len(self.X) // 16
    
    def __getitem__(self, idx):        
        features = self.X.iloc[idx * 16:(idx + 1) * 16, :]
        label = self.y.iloc[idx % 16, 0]

        features = torch.tensor(features.values)
        label = torch.tensor(label)

        return features, label
</code></pre>

username: SlimShadys
cooked: <p>This seems to be working correctly when taking items from the DataLoader.<br>
I just can’t understand how, in the label DataFrame, the modulo (%) index works though for the row counting.</p>
<p>I tested this with the model I have and I have less overfitting now, but still I think it’s necessary to have a way bigger dataset. My main concern was about the training loop and how I took my samples from the DataFrame, which you correctly issued in your reply.</p>