Slight mistake in CNN model

I was training a simple CNN Model for Binary Image Classification. But I am getting issues, here is the code

import tqdm
import torch 
import torch.nn as nn 
import os 
import numpy as np 
import tqdm
import seaborn as sns
import pandas as pd 

cats = np.load('/kaggle/input/notebook98410ea555/cats.npy')
dogs = np.load('/kaggle/input/notebook98410ea555/dogs.npy')

cats_tars = np.array([0 for _ in cats])
dogs_tars = np.array([1 for _ in dogs])

r = np.concatenate([cats , dogs])

data = pd.DataFrame({
    'feats' : [val for val in r] , 
    'targs' : np.concatenate([cats_tars , dogs_tars])
})

data = data.sample(frac = 1).reset_index(drop = True)

class mod(nn.Module):
    
    def __init__(self):
        
        super().__init__()
        
        self.conv_1 = nn.Conv2d(3 , 3 , (3 , 3))
        self.conv_2 = nn.Conv2d(3 , 3 , (3 , 3))
        self.conv_3 = nn.Conv2d(3 , 3 , (3 , 3))
        self.conv_4 = nn.Conv2d(3 , 3 , (3 , 3))
        self.max_pool = nn.MaxPool2d(3 , 3)
        
        self.linear_1 = nn.Linear(12288 , 1400)
        self.linear_2 = nn.Linear(1400 , 1)
        
    def forward(self , inps , middle = False):
        
        embs = self.conv_1(inps)
        embs = self.conv_2(embs)
        embs = self.conv_3(embs)
        embs = self.conv_4(embs)
        embs = self.max_pool(embs)
        embs = embs.flatten()
        
        if middle : 
            return embs
        
        embs = self.linear_1(embs)
        embs = torch.sigmoid(embs)
        embs = self.linear_2(embs)
        
        embs = torch.sigmoid(embs)
        
        return embs

model = mod().to('cuda')

optim = torch.optim.SGD(model.parameters() , lr = 0.001)

los = nn.BCELoss()

loss_ = []
o = []

for index in tqdm.tqdm(range(data.shape[0])) , total = data.shape[0]):
    
    inps = torch.tensor(data['feats'][index] , dtype = torch.float32).to('cuda')
    
    out = model(inps)
    o.append(out)
    loss = los(
        out , 
        torch.tensor([data['targs'][index]] , dtype = torch.float32).to('cuda'))
    
    loss.backward()
    optim.step()
    loss_.append(loss)
    
    torch.cuda.empty_cache()    

The model is not able to learn something at all. The loss is sometimes 0 and sometimes 100. I am not able to understand what slight thing I am doing wrong here

What I have tried Effect
Sending cats/dogs alternative None
More shuffling of the data None
Different Learning Rate None
Different Optimizers → Adam/Adagrad/SGD None
Using MSE None
Using (preds/targs) - (targs/preds) None
Changing ([0 , 2]/[1 , 2]) and corresponding changes to BCE formula None

I know that I am doing something slightly wrong, but what it is

The model consists of extra parameter middle, I will be using the CNN Layers as an embedder for a project. Here is the Dataset

Observation: your convolution layers do not have activation functions.

Thanks Akt42 for pointing that out :slight_smile: