Open zhengkai17 opened 5 years ago
#
# demo.py
#
import argparse
import os
import numpy as np
from modeling.deeplab import *
from dataloaders import custom_transforms as tr
from PIL import Image
from torchvision import transforms
from dataloaders.utils import *
from torchvision.utils import make_grid, save_image
def main():
parser = argparse.ArgumentParser(description="PyTorch DeeplabV3Plus Training")
parser.add_argument('--in-path', type=str, required=True, help='image to test')
parser.add_argument('--out-path', type=str, required=True, help='mask image to save')
parser.add_argument('--backbone', type=str, default='resnet',
choices=['resnet', 'xception', 'drn', 'mobilenet'],
help='backbone name (default: resnet)')
parser.add_argument('--ckpt', type=str, default='deeplab-resnet.pth',
help='saved model')
parser.add_argument('--out-stride', type=int, default=16,
help='network output stride (default: 8)')
parser.add_argument('--no-cuda', action='store_true', default=True,
help='disables CUDA training')
parser.add_argument('--gpu-ids', type=str, default='0',
help='use which gpu to train, must be a \
comma-separated list of integers only (default=0)')
parser.add_argument('--dataset', type=str, default='pascal',
choices=['pascal', 'coco', 'cityscapes'],
help='dataset name (default: pascal)')
parser.add_argument('--crop-size', type=int, default=513,
help='crop image size')
parser.add_argument('--sync-bn', type=bool, default=None,
help='whether to use sync bn (default: auto)')
parser.add_argument('--freeze-bn', type=bool, default=False,
help='whether to freeze bn parameters (default: False)')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
if args.cuda:
try:
args.gpu_ids = [int(s) for s in args.gpu_ids.split(',')]
except ValueError:
raise ValueError('Argument --gpu_ids must be a comma-separated list of integers only')
if args.sync_bn is None:
if args.cuda and len(args.gpu_ids) > 1:
args.sync_bn = True
else:
args.sync_bn = False
model = DeepLab(num_classes=21,
backbone=args.backbone,
output_stride=args.out_stride,
sync_bn=args.sync_bn,
freeze_bn=args.freeze_bn)
ckpt = torch.load(args.ckpt, map_location='cpu')
model.load_state_dict(ckpt['state_dict'])
composed_transforms = transforms.Compose([
tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
tr.ToTensor()])
image = Image.open(args.in_path).convert('RGB')
target = Image.open(args.in_path).convert('L')
sample = {'image': image, 'label': target}
tensor_in = composed_transforms(sample)['image'].unsqueeze(0)
model.eval()
if args.cuda:
image = image.cuda()
with torch.no_grad():
output = model(tensor_in)
grid_image = make_grid(decode_seg_map_sequence(torch.max(output[:3], 1)[1].detach().cpu().numpy()),
3, normalize=False, range=(0, 255))
print("type(grid) is: ", type(grid_image))
print("grid_image.shape is: ", grid_image.shape)
save_image(grid_image, args.out_path)
if __name__ == "__main__":
main()
download the pretrained model and test with the following line:
python demo.py --in-path your_file --out-path your_dst_file
Just enjoy it.
@FantasyJXF Hi, when I run the demo.py, I met a problem as follows:
$ python demo.py --in-path test32.jpg --backbone mobilenet --ckpt mobilenet_v2-6a65762b.pth --out-path result.jpg
Traceback (most recent call last):
File "demo.py", line 86, in <module>
main()
File "demo.py", line 63, in main
model.load_state_dict(ckpt['state_dict'])
KeyError: 'state_dict'
What is the 'state_dict', and how should I slove the problem? Thank you!
@beizhengren download the pretrained model first. Learn some basic pytorch grammars
@FantasyJXF Thank you!
@FantasyJXF thanks!!!
# # demo.py # import argparse import os import numpy as np from modeling.deeplab import * from dataloaders import custom_transforms as tr from PIL import Image from torchvision import transforms from dataloaders.utils import * from torchvision.utils import make_grid, save_image def main(): parser = argparse.ArgumentParser(description="PyTorch DeeplabV3Plus Training") parser.add_argument('--in-path', type=str, required=True, help='image to test') parser.add_argument('--out-path', type=str, required=True, help='mask image to save') parser.add_argument('--backbone', type=str, default='resnet', choices=['resnet', 'xception', 'drn', 'mobilenet'], help='backbone name (default: resnet)') parser.add_argument('--ckpt', type=str, default='deeplab-resnet.pth', help='saved model') parser.add_argument('--out-stride', type=int, default=16, help='network output stride (default: 8)') parser.add_argument('--no-cuda', action='store_true', default=True, help='disables CUDA training') parser.add_argument('--gpu-ids', type=str, default='0', help='use which gpu to train, must be a \ comma-separated list of integers only (default=0)') parser.add_argument('--dataset', type=str, default='pascal', choices=['pascal', 'coco', 'cityscapes'], help='dataset name (default: pascal)') parser.add_argument('--crop-size', type=int, default=513, help='crop image size') parser.add_argument('--sync-bn', type=bool, default=None, help='whether to use sync bn (default: auto)') parser.add_argument('--freeze-bn', type=bool, default=False, help='whether to freeze bn parameters (default: False)') args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() if args.cuda: try: args.gpu_ids = [int(s) for s in args.gpu_ids.split(',')] except ValueError: raise ValueError('Argument --gpu_ids must be a comma-separated list of integers only') if args.sync_bn is None: if args.cuda and len(args.gpu_ids) > 1: args.sync_bn = True else: args.sync_bn = False model = DeepLab(num_classes=21, backbone=args.backbone, output_stride=args.out_stride, sync_bn=args.sync_bn, freeze_bn=args.freeze_bn) ckpt = torch.load(args.ckpt, map_location='cpu') model.load_state_dict(ckpt['state_dict']) composed_transforms = transforms.Compose([ tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), tr.ToTensor()]) image = Image.open(args.in_path).convert('RGB') target = Image.open(args.in_path).convert('L') sample = {'image': image, 'label': target} tensor_in = composed_transforms(sample)['image'].unsqueeze(0) model.eval() if args.cuda: image = image.cuda() with torch.no_grad(): output = model(tensor_in) grid_image = make_grid(decode_seg_map_sequence(torch.max(output[:3], 1)[1].detach().cpu().numpy()), 3, normalize=False, range=(0, 255)) print("type(grid) is: ", type(grid_image)) print("grid_image.shape is: ", grid_image.shape) save_image(grid_image, args.out_path) if __name__ == "__main__": main()
download the pretrained model and test with the following line:
python demo.py --in-path your_file --out-path your_dst_file
Just enjoy it.
HI I want to ask a question, I want to know which version of deeplab-resnet?
@JasonChenhx resnet101, you could find the download link in the file modeling/backbone/resnet.py
Thanks. But I have another question. I have already downloaded 'deeplabv3_resnet101_coco.pth' in pytorch official web, but the result of the run is in loading state_dict for Deeplab: Missing key(s) in state_dict. Thanks again.
------------------ 原始邮件 ------------------ 发件人: "FantasyJXF"<notifications@github.com>; 发送时间: 2020年1月9日(星期四) 下午3:19 收件人: "jfzhang95/pytorch-deeplab-xception"<pytorch-deeplab-xception@noreply.github.com>; 抄送: "JasonChenhx"<chx880324@163.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [jfzhang95/pytorch-deeplab-xception] can you provide the testing for single image? (#107)
@JasonChenhx resnet101, you could find the download link in the file modeling/backbone/resnet.py
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
@FantasyJXF Thank you!
HI, I want to ask you a question about which model and where to download the model?
@JasonChenhx Idon't know why you download deeplabv3_resnet101_coco.pth
from pytorch official web? This repo is Deeplab v3+, and the author has provided pretrained model with both resnet101 and mobilenet v2 backbone.
You could just download them and run my test code.
What's more, try to read the code.
Thanks. I have already read the author's code and your code, I am a new graduate student, so I have no experience in this field and I' am studying the CV. Thank you for your help.
------------------ 原始邮件 ------------------ 发件人: "FantasyJXF"<notifications@github.com>; 发送时间: 2020年1月9日(星期四) 下午4:01 收件人: "jfzhang95/pytorch-deeplab-xception"<pytorch-deeplab-xception@noreply.github.com>; 抄送: "JasonChenhx"<chx880324@163.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [jfzhang95/pytorch-deeplab-xception] can you provide the testing for single image? (#107)
@JasonChenhx Idon't know why you download deeplabv3_resnet101_coco.pth from pytorch official web? This repo is Deeplab v3+, and the author has provided pretrained model with both resnet101 and mobilenet v2 backbone.
You could just download them and run my test code.
What's more, try to read the code.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
Hi sorry to bother you again,I just went to download the resnet and mobilnet the github provided, but I found that I unable to download successfully. If you have the model path of resnet and mobilenet , can you send me? Thanks again.
------------------ 原始邮件 ------------------ 发件人: "FantasyJXF"<notifications@github.com>; 发送时间: 2020年1月9日(星期四) 下午4:01 收件人: "jfzhang95/pytorch-deeplab-xception"<pytorch-deeplab-xception@noreply.github.com>; 抄送: "JasonChenhx"<chx880324@163.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [jfzhang95/pytorch-deeplab-xception] can you provide the testing for single image? (#107)
@JasonChenhx Idon't know why you download deeplabv3_resnet101_coco.pth from pytorch official web? This repo is Deeplab v3+, and the author has provided pretrained model with both resnet101 and mobilenet v2 backbone.
You could just download them and run my test code.
What's more, try to read the code.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
Is there a way to set the confidence threshold for prediction in the script ?
this demo.py can only two types of targets (background + 1 target) be drawn?
@FantasyJXF I have a question,in save_image(grid_image, args.out_path),It will make an error,
Traceback (most recent call last):
File "demo.py", line 117, in
Hi, I'm training it on my custom 2 class dataset(1 background+1 object). Anyone can share what is the data type of input and label images. Also, what value should be assigned to each class pixel in the label image?
# # demo.py # import argparse import os import numpy as np from modeling.deeplab import * from dataloaders import custom_transforms as tr from PIL import Image from torchvision import transforms from dataloaders.utils import * from torchvision.utils import make_grid, save_image def main(): parser = argparse.ArgumentParser(description="PyTorch DeeplabV3Plus Training") parser.add_argument('--in-path', type=str, required=True, help='image to test') parser.add_argument('--out-path', type=str, required=True, help='mask image to save') parser.add_argument('--backbone', type=str, default='resnet', choices=['resnet', 'xception', 'drn', 'mobilenet'], help='backbone name (default: resnet)') parser.add_argument('--ckpt', type=str, default='deeplab-resnet.pth', help='saved model') parser.add_argument('--out-stride', type=int, default=16, help='network output stride (default: 8)') parser.add_argument('--no-cuda', action='store_true', default=True, help='disables CUDA training') parser.add_argument('--gpu-ids', type=str, default='0', help='use which gpu to train, must be a \ comma-separated list of integers only (default=0)') parser.add_argument('--dataset', type=str, default='pascal', choices=['pascal', 'coco', 'cityscapes'], help='dataset name (default: pascal)') parser.add_argument('--crop-size', type=int, default=513, help='crop image size') parser.add_argument('--sync-bn', type=bool, default=None, help='whether to use sync bn (default: auto)') parser.add_argument('--freeze-bn', type=bool, default=False, help='whether to freeze bn parameters (default: False)') args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() if args.cuda: try: args.gpu_ids = [int(s) for s in args.gpu_ids.split(',')] except ValueError: raise ValueError('Argument --gpu_ids must be a comma-separated list of integers only') if args.sync_bn is None: if args.cuda and len(args.gpu_ids) > 1: args.sync_bn = True else: args.sync_bn = False model = DeepLab(num_classes=21, backbone=args.backbone, output_stride=args.out_stride, sync_bn=args.sync_bn, freeze_bn=args.freeze_bn) ckpt = torch.load(args.ckpt, map_location='cpu') model.load_state_dict(ckpt['state_dict']) composed_transforms = transforms.Compose([ tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), tr.ToTensor()]) image = Image.open(args.in_path).convert('RGB') target = Image.open(args.in_path).convert('L') sample = {'image': image, 'label': target} tensor_in = composed_transforms(sample)['image'].unsqueeze(0) model.eval() if args.cuda: image = image.cuda() with torch.no_grad(): output = model(tensor_in) grid_image = make_grid(decode_seg_map_sequence(torch.max(output[:3], 1)[1].detach().cpu().numpy()), 3, normalize=False, range=(0, 255)) print("type(grid) is: ", type(grid_image)) print("grid_image.shape is: ", grid_image.shape) save_image(grid_image, args.out_path) if __name__ == "__main__": main()
download the pretrained model and test with the following line:
python demo.py --in-path your_file --out-path your_dst_file
Just enjoy it.
python demo.py --in-path your_file --out-path your_dst_file #'your_file' not a folder is a **.jpg
hello,i also want to test single one image in my own path,have you solve it?