CNN trained model doesn't appear to be working

alameer: <p>I’ve trained a CNN model and I would like to run the trained model against new data. However, it seems that the trained model isn’t predicting the count correctly as it done during the training. I have a feeling that the model is not using the PTH file. Could someone please advise what I am doing wrong, please?</p>
<pre><code class="lang-auto">mport argparse
import datetime
import glob
import os
import random
import shutil
import time
from os.path import join

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import ToTensor
from tqdm import tqdm
import torch.optim as optim

from convnet3_eval import Convnet
from dataset2_eval import CellsDataset

parser = argparse.ArgumentParser('Predicting hits from pixels')
parser.add_argument('name',type=str,help='Name of experiment')
parser.add_argument('data_dir',type=str,help='Path to data directory containing images and gt.csv')
parser.add_argument('--weight_decay',type=float,default=0.0,help='Weight decay coefficient (something like 10^-5)')
parser.add_argument('--lr',type=float,default=0.0001,help='Learning rate')
args = parser.parse_args()

metadata = pd.read_csv(join(args.data_dir,'gt.csv'))
metadata.set_index('filename', inplace=True)


dataset = CellsDataset(args.data_dir,transform=ToTensor(),return_filenames=True)
dataset = DataLoader(dataset,num_workers=4,pin_memory=True)
model_path = '/base_model.pth'

model = Convnet()
optimizer = torch.optim.Adam(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)

for images, paths in tqdm(dataset):

    targets = torch.tensor([metadata['count'][os.path.split(path)[-1]] for path in paths]) # B
    targets = targets.float()

    # code to print training data to a csv file
    filename=CellsDataset(args.data_dir,transform=ToTensor(),return_filenames=True)
    output = model(images) # B x 1 x 9 x 9 (analogous to a heatmap)
    preds = output.sum(dim=[1,2,3]) # predicted cell counts (vector of length B)
    print(preds)
    paths_test = np.array([paths])
    names_preds = np.hstack(paths)
    print(names_preds)                
    df=pd.DataFrame({'Image_Name':names_preds, 'Target':targets.detach(), 'Prediction':preds.detach()})
    print(df) 
    # save image name, targets, and predictions
    df.to_csv(r'model.csv', index=False, mode='a')


model.load_state_dict(torch.load(model_path))
model.eval()</code></pre>

RaLo4: <p>I am assuming this is your code for testing the model whose parameters are saved in base_model.pth.<br>
So I am guessing you want to run this:</p>
<aside class="quote no-group" data-username="alameer" data-post="1" data-topic="93176">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/9dc877/48.png" class="avatar"> alameer:</div>
<blockquote>
<pre><code class="lang-auto">for images, paths in tqdm(dataset):

    targets = torch.tensor([metadata['count'][os.path.split(path)[-1]] for path in paths]) # B
    targets = targets.float()

    # code to print training data to a csv file
    filename=CellsDataset(args.data_dir,transform=ToTensor(),return_filenames=True)
    output = model(images) # B x 1 x 9 x 9 (analogous to a heatmap)
    preds = output.sum(dim=[1,2,3]) # predicted cell counts (vector of length B)
    print(preds)
    paths_test = np.array([paths])
    names_preds = np.hstack(paths)
    print(names_preds)                
    df=pd.DataFrame({'Image_Name':names_preds, 'Target':targets.detach(), 'Prediction':preds.detach()})
    print(df) 
    # save image name, targets, and predictions
    df.to_csv(r'model.csv', index=False, mode='a')
</code></pre>
</blockquote>
</aside>
<p>With the parameters saved in this:</p>
<aside class="quote no-group" data-username="alameer" data-post="1" data-topic="93176">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/9dc877/48.png" class="avatar"> alameer:</div>
<blockquote>
<p><code>model_path = '/base_model.pth'</code></p>
</blockquote>
</aside>
<p>But you load your model parameters at the very end and do nothing with it:</p>
<aside class="quote no-group" data-username="alameer" data-post="1" data-topic="93176">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/9dc877/48.png" class="avatar"> alameer:</div>
<blockquote>
<p><code>model.load_state_dict(torch.load(model_path))</code></p>
</blockquote>
</aside>
<p>Try putting these two lines:</p>
<aside class="quote no-group" data-username="alameer" data-post="1" data-topic="93176">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/9dc877/48.png" class="avatar"> alameer:</div>
<blockquote>
<pre><code class="lang-auto">model.load_state_dict(torch.load(model_path))
model.eval()
</code></pre>
</blockquote>
</aside>
<p>After this line:</p>
<aside class="quote no-group" data-username="alameer" data-post="1" data-topic="93176">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/9dc877/48.png" class="avatar"> alameer:</div>
<blockquote>
<p><code>model = Convnet()</code></p>
</blockquote>
</aside>
<p>Also. I think you know this, but this would mean you are trying to load trained parameters into <code>Convnet</code>, to do so the parameters saved in <code>/base_model.pth</code> should have also been training on said <code>Convnet</code></p>

alameer: <p>This is the exact issue. Many Thanks for your help.</p>