ZJULearning/resa

Inference single custom images

Closed this issue · 9 comments

Hi there,
Thanks your code,
I want to inference any custom image which is not in Tusimple dataset.
The following is my code:
import torch
import cv2
import torch.nn.functional as F
from models.resa import RESANet
from utils.config import Config
from datasets import build_dataloader
from models.registry import build_net

from PIL import Image
import utils.transforms as tf
from torch.autograd import Variable
from torchvision.utils import save_image
import torchvision.transforms as transforms

loader1 = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((103.939, 116.779, 123.68), (1., 1., 1.)),
transforms.Resize((368,640)),]) # for tusimple

def image_loader(image_name):
"""load image, return cuda tensor"""
image = Image.open(image_name)
image = loader1(image).float()
image = Variable(image, requires_grad=True)
image = image.unsqueeze(0)
return image.cuda()

cfg = Config.fromfile('configs/tusimple.py')

resa = build_net(cfg)
resa = torch.nn.parallel.DataParallel(
resa, device_ids = range(1)).cuda()

loader = build_dataloader(cfg.dataset.val, cfg, is_train=False)
pretrained_model = torch.load('tusimple_resnet34.pth')
resa.load_state_dict(pretrained_model['net'], strict=True)

x = image_loader('./20.jpg') # 20.jpg is copied from tusimple test datasets

with torch.no_grad():
out = resa(x)
probmap, exist = out['seg'], out['exist']
probmap = F.softmax(probmap, dim=1).squeeze().cpu().numpy()
exist = exist.squeeze().cpu().numpy()

coords = loader.dataset.probmap2lane(probmap, exist)

img = cv2.imread('./20.jpg')
loader.dataset.view(img, coords, './test.png')

The result is not as good as choose from x = loader.dataset[img_idx]['img'].unsqueeze(0).cuda()
Can you help that?
thanks so much.

what error do you get?
By the way,

x = loader.dataset['img_idx]['img'].unsqueeze(0).cuda()

should be

x = loader.dataset[img_idx]['img'].unsqueeze(0).cuda()

Sorry, that is a typo.
Actually, there is no error during the code execution.
I use the same image for my code and the reference code(Inference on custom example #1)
The result is different.

thank for your helping

If you're testing on a image from a different dataset, then there's no guarantee the result is going to be useful.
Maybe you can try to train a new model in the new dataset.

appreciated your response.
For my current experiment, the image is picked from the tusimple dataset.
So, I think the result should be the same.

Thanks so much.

Your pre-process of input image is different from the ours.
Please check the __getitem__ in the BaseDataset from https://github.com/ZJULearning/resa/blob/main/datasets/base_dataset.py#L62.

Thanks for your response.
Yes. the pre-process of input is different.
After I modifed the code with that, the result is the same with yours.
BTW, I picked couple images which are not in Tusimple or CUlane dataset and the results are perfect.

appreciated!!!

Congratulations. 😁

I'm struggling with inferencing my custom image.
Would you share your inference code, please?