Event-AHU / OpenPAR

[OpenPAR] An open-source framework for Pedestrian Attribute Recognition, based on PyTorch
MIT License
63 stars 6 forks source link

Trained PromptPAR on custom dataset But getting same prediction every time #30

Open bilal6414 opened 1 week ago

bilal6414 commented 1 week ago

I have trained promptPAR on my own dataset, But when I try to get prediciton on images it is giving same output / predciton every time. What I opesered was the checkpoint dictionary is differnet from the weights I get after training. in training file you write line to use eval.py but this file is not present there. Please help in this regard. I am also sharing weights file

`import torch import numpy as np import os import pprint from collections import OrderedDict, defaultdict import sys import numpy as np import torch from torch.utils.data import DataLoader import time from torch import nn,optim from batch_engine import valid_trainer from config import argument_parser from dataset.AttrDataset import MultiModalAttrDataset, get_transform from loss.CE_loss import from models_2.base_block import from tools.function import get_pedestrian_metrics,get_signle_metrics from tools.utils import time_str, save_ckpt, ReDirectSTD, set_seed, select_gpus from solver import make_optimizer from solver.scheduler_factory import create_scheduler,make_scheduler from clip_2 import clip from clip_2.model import * num= 30 custom_attributes = [ 'A pedestrian with long hair','A pedestrian with hat','A pedestrian wearing kamiz','A pedestrian wearing pant','A pedestrian with glasses', 'A pedestrian is male','A pedestrian with a backpack','A pedestrian with a hand bag','A pedestrian with a Fat body','A pedestrian is a kid', 'A pedestrian is a teenager','A pedestrian is a adult','A pedestrian is a old', 'A pedestrian with black upper body', 'A pedestrian with white upper body','A pedestrian with red upper body','A pedestrian with purple upper body','A pedestrian with yellow upper body', 'A pedestrian with gray upper body','A pedestrian with blue upper body','A pedestrian with green upper body', 'A pedestrian with black lower body','A pedestrian with white lower body','A pedestrian with pink lower body','A pedestrian with purple lower body', 'A pedestrian with yellow lower body','A pedestrian with gray lower body','A pedestrian with blue lower body','A pedestrian with green lower body', 'A pedestrian with brown lower body']

checkpoint loading

checkpoint = torch.load("/UNITY-NFS/Data_B/OpenPAR/PromptPAR/logs/custom/2024-09-11_12_45_59/epoch43.pth") checkpoint.keys() clip_model = build_model(checkpoint['clip_model']) model = TransformerClassifier(clip_model,num, custom_attributes) main/PromptPAR/logs/PETA/2024-05-23_14_59_25/epoch21.pth model.load_state_dict(checkpoint['model_state_dict'], strict=False) if torch.cuda.is_available(): model = model.cuda() clip_model = clip_model.cuda()

import cv2 img = cv2.imread("images/20240807114139768329_t.jpg")

from torchvision import transforms

preprocess = transforms.Compose([ transforms.ToPILImage(), # Convert NumPy array to PIL image (if using OpenCV) transforms.Resize((224, 224)), # Resize to required input size transforms.ToTensor(), # Convert PIL image to Tensor (CxHxW) transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # Normalization ]) def valid_trainer_single(model, clip_model, img):

model.eval()

model.eval()
clip_model.eval()
preds_probs = []

with torch.no_grad():
    print(f"Type of image: {type(img)}")

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Convert BGR (OpenCV default) to RGB if needed
    img = preprocess(img)

    print(f"Type of image after reading: {type(img)} with shape {img.shape}")

    # Normalize and convert to tensor
    img = img / 255.0  # Normalize to [0,1]
    #img = torch.from_numpy(img).permute(2, 0, 1).float() 
    img = img.cuda().unsqueeze(0)  # Add batch dimension for a single image

    print(img.shape)

    valid_logits, final_similarity = model(img, clip_model=clip_model)

    valid_probs = torch.sigmoid(valid_logits)
    preds_probs.append(valid_probs.cpu().numpy())

preds_probs = np.array(preds_probs)

return  preds_probs

pred=valid_trainer_single(model, clip_model, img)

`

1125178969 commented 1 week ago

you can cancel the strict=False in your evaluation code and clip/model.py/bulid_model when loading the pre-trained weights. Then, check the parameters that were updated in training were loaded correctly. If you can't confirm that it's loading correctly, you can put up the logs.