Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference single custom images #8

Closed
yirs2001 opened this issue May 10, 2021 · 9 comments
Closed

Inference single custom images #8

yirs2001 opened this issue May 10, 2021 · 9 comments

Comments

@yirs2001
Copy link

yirs2001 commented May 10, 2021

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.

@Turoad
Copy link
Member

Turoad commented May 10, 2021

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()

@yirs2001
Copy link
Author

yirs2001 commented May 10, 2021

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

@Turoad
Copy link
Member

Turoad commented May 10, 2021

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.

@yirs2001
Copy link
Author

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.

@Turoad
Copy link
Member

Turoad commented May 10, 2021

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.

@yirs2001
Copy link
Author

yirs2001 commented May 10, 2021

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!!!

@Turoad
Copy link
Member

Turoad commented May 10, 2021

Congratulations. 😁

@KnightHarute
Copy link

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

@Turoad
Copy link
Member

Turoad commented Jun 15, 2021

you can refer to this code: https://github.com/Turoad/lanedet/blob/main/tools/detect.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants