Convolutional Neural Network Model - Why do I get different results on the same image
I'm new to Neural Networks and I'm trying to train a CNN model on a custom dataset (cats and dogs images in a single directory). So I guess I do the very usual stuff here which is in the most tutorials, but just in case I will give here my full code.
First I generate .csv file to be processed:
import os
import torch

device = (&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)

train_df = pd.DataFrame(columns=[&quot;img_name&quot;,&quot;label&quot;])
train_df[&quot;img_name&quot;] = os.listdir(&quot;train/&quot;)
for idx, i in enumerate(os.listdir(&quot;train/&quot;)):
    if &quot;cat&quot; in i:
        train_df[&quot;label&quot;][idx] = 0
    if &quot;dog&quot; in i:
        train_df[&quot;label&quot;][idx] = 1

train_df.to_csv (r'train_csv.csv', index = False, header=True)

Then I prepare the dataset:
from torch.utils.data import Dataset
import pandas as pd
import os
from PIL import Image
import torch

class CatsAndDogsDataset(Dataset):
    def __init__(self, root_dir, annotation_file, transform=None):
        self.root_dir = root_dir
        self.annotations = pd.read_csv(annotation_file)
        self.transform = transform

    def __len__(self):
        return len(self.annotations)

    def __getitem__(self, index):
        img_id = self.annotations.iloc[index, 0]
        img = Image.open(os.path.join(self.root_dir, img_id)).convert(&quot;RGB&quot;)
        y_label = torch.tensor(float(self.annotations.iloc[index, 1]))

        if self.transform is not None:
            img = self.transform(img)

        return (img, y_label)

This is my model:
import torch.nn as nn
import torchvision.models as models

class CNN(nn.Module):
    def __init__(self, train_CNN=False, num_classes=1):
        super(CNN, self).__init__()
        self.train_CNN = train_CNN
        self.inception = models.inception_v3(pretrained=True, aux_logits=False)
        self.inception.fc = nn.Linear(self.inception.fc.in_features, num_classes)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(0.5)
        self.sigmoid = nn.Sigmoid()

    def forward(self, images):
        features = self.inception(images)
        return self.sigmoid(self.dropout(self.relu(features))).squeeze(1)

This is my hyper-params, transformations and dataloaders:
from torch.utils.data import DataLoader
import torchvision.transforms as transforms

num_epochs = 10
learning_rate = 0.00001
train_CNN = False
batch_size = 32
shuffle = True
pin_memory = True
num_workers = 0
transform = transforms.Compose(
        [
            transforms.Resize((356, 356)),
            transforms.RandomCrop((299, 299)),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
        ]
    )
dataset = CatsAndDogsDataset(&quot;train&quot;,&quot;train_csv.csv&quot;,transform=transform)
print(len(dataset))
train_set, validation_set = torch.utils.data.random_split(dataset,[162,40])
train_loader = DataLoader(dataset=train_set, shuffle=shuffle, batch_size=batch_size,num_workers=num_workers,pin_memory=pin_memory)
validation_loader = DataLoader(dataset=validation_set, shuffle=shuffle, batch_size=batch_size,num_workers=num_workers, pin_memory=pin_memory)

model = CNN().to(device)
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for name, param in model.inception.named_parameters():
    if &quot;fc.weight&quot; in name or &quot;fc.bias&quot; in name:
        param.requires_grad = True
    else:
        param.requires_grad = train_CNN

and accuracy check:

def check_accuracy(loader, model):
    if loader == train_loader:
        print(&quot;Checking accuracy on training data&quot;)
    else:
        print(&quot;Checking accuracy on validation data&quot;)

    num_correct = 0
    num_samples = 0
    model.eval()

    with torch.no_grad():
        for x, y in loader:
            x = x.to(device=device)
            y = y.to(device=device)

            scores = model(x)
            predictions = torch.tensor([1.0 if i &gt;= 0.5 else 0.0 for i in scores]).to(device)
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)
    print(
            f&quot;Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}&quot;
        )
    model.train()
    return f&quot;{float(num_correct)/float(num_samples)*100:.2f}&quot;    

And this is my training function:
from tqdm import tqdm

def train():
    model.train()
    for epoch in range(num_epochs):
        loop = tqdm(train_loader, total = len(train_loader), leave = True)
        if epoch % 2 == 0:
            loop.set_postfix(val_acc = check_accuracy(validation_loader, model))
        for imgs, labels in loop:
            imgs = imgs.to(device)
            labels = labels.to(device)
            outputs = model(imgs)
            loss = criterion(outputs, labels)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            loop.set_description(f&quot;Epoch [{epoch}/{num_epochs}]&quot;)
            loop.set_postfix(loss = loss.item())
if __name__ == &quot;__main__&quot;:
    train()

0%|          | 0/6 [00:00&lt;?, ?it/s]Checking accuracy on validation data
0%|          | 0/6 [01:13&lt;?, ?it/s, val_acc=60.00]Got 24 / 40 with accuracy 60.00
Epoch [0/10]: 100%|██████████| 6/6 [06:02&lt;00:00, 60.39s/it, loss=0.693]
Epoch [1/10]: 100%|██████████| 6/6 [04:49&lt;00:00, 48.23s/it, loss=0.693]
...
Epoch [8/10]: 100%|██████████| 6/6 [06:07&lt;00:00, 61.29s/it, loss=0.693]
Epoch [9/10]: 100%|██████████| 6/6 [04:55&lt;00:00, 49.19s/it, loss=0.781]

The model gets trained fine but when I try to use it for prediction I get different results each time I run this last piece in my Jupyter Notebooks:
model.eval()
img = Image.open('train/cat.22.png').convert(&quot;RGB&quot;)
img_t = transform(img)
batch_t = torch.unsqueeze(img_t, 0)
out = model(batch_t)
print(out)


tensor([0.5276], grad_fn=)


tensor([0.5000], grad_fn=)


tensor([0.5064], grad_fn=)

etc. Each time different result for the same image. Is this normal? Why this is happening?

I don't see you loading your trained model. This means every time you initialize the CNN module, the inception.fc layer will get initialized with random weights, this is most probably the reason why you are getting different results on each inference.

Edit: You have a random transform in your transformation pipeline, namely RandomCrop.
