def main(): print('Loading model from %s' % (args.ckpt)) net = RetinaNet() load_checkpoint('%s' % (args.ckpt), net) net.eval() use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") print('Device used:', device) net = net.to(device) encoder = DataEncoder() if args.vmode: if not os.path.exists("%s" % (args.pred_root)): os.makedirs("%s" % (args.pred_root)) visualize(encoder, net, device, args.image_root, val_image_list, args.pred_root) if args.mmode: if not os.path.exists("./mPA"): os.makedirs("./mPA") if os.path.exists("./mPA/detection-results"): print("Remove Pred file") os.system("rm -rf ./mPA/detection-results") os.makedirs("./mPA/detection-results") if not os.path.exists("./mPA/detection-results"): os.makedirs("./mPA/detection-results") mPA_pred(encoder, net, device, args.image_root, args.anno_root) return 0
def evaluate_threshold(img_ids, cls_threshold, bbox_dict): dloader = get_test_loader(img_ids, img_dir=settings.IMG_DIR, batch_size=batch_size) # Model net = RetinaNet() net.load_state_dict(torch.load(CKP_FILE)) #net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count())) net.cuda() net.eval() bgtime = time.time() encoder = DataEncoder() encoder.class_threshold = cls_threshold true_objects_num = 0 pred_objects_num = 0 for batch_idx, inputs in enumerate(dloader): inputs = Variable(inputs.cuda()) loc_preds, cls_preds = net(inputs) for i in range(len(loc_preds)): boxes, labels, scores = encoder.decode( loc_preds[i].data, cls_preds[i].data, (settings.IMG_SZ, settings.IMG_SZ)) pred_objects_num += len(scores) for img_idx in range(len(inputs)): img_id = dloader.img_ids[batch_idx * batch_size + img_idx] if img_id in bbox_dict: true_objects_num += len(bbox_dict[img_id]) print('{} / {}, {} / {}, {:.4f}, {:.2f} min'.format( batch_size * (batch_idx + 1), dloader.num, pred_objects_num, true_objects_num, cls_threshold, (time.time() - bgtime) / 60), end='\r') print('\n') print('=>>> {}/{}, {}, {:.4f}\n'.format( pred_objects_num, true_objects_num, pred_objects_num - true_objects_num, cls_threshold))
def predict(): assert torch.cuda.is_available(), 'Error: CUDA not found!' print('==> Preparing data..') dloader = get_test_loader(get_test_ids(), img_dir=settings.TEST_IMG_DIR, batch_size=batch_size) print(dloader.num) # Model net = RetinaNet() net.load_state_dict(torch.load(CKP_FILE)) #net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count())) net.cuda() bgtime = time.time() encoder = DataEncoder() net.eval() prediction_strings = [] for batch_idx, inputs in enumerate(dloader): inputs = Variable(inputs.cuda()) loc_preds, cls_preds = net(inputs) print('{} / {} {:.2f}'.format(batch_size * (batch_idx + 1), dloader.num, (time.time() - bgtime) / 60), end='\r') for i in range(len(loc_preds)): boxes, labels, scores = encoder.decode( loc_preds[i].data, cls_preds[i].data, (settings.IMG_SZ, settings.IMG_SZ)) prediction_strings.append( _get_prediction_string(boxes, labels, scores)) print(len(prediction_strings)) print(prediction_strings[:3]) submission = pd.DataFrame({ 'ImageId': dloader.img_ids, 'PredictionString': prediction_strings }) submission.to_csv('sub7.csv', index=False)
import torchvision.transforms as transforms from torchsummary import summary from torch.autograd import Variable from retinanet import RetinaNet from encoder import DataEncoder from PIL import Image, ImageDraw import cv2 import time print('Loading model..') net = RetinaNet() net.load_state_dict(torch.load('checkpoint/ckpt.pth')['net']) net.eval() net.cuda() #summary(net, (3, 224, 224)) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) ]) print('Loading image..') #img = Image.open('./image/000001.jpg') cap = cv2.VideoCapture('Z:\\u_zhh\\video\\jy2.mp4') while True: ret, cv2img = cap.read() # 名称不能有汉字 if not ret:
test_dataset = DatasetThnSeqSynscapes(thn_data_path="/root/deeplabv3/data/thn") #test_dataset = DatasetThnSeq(thn_data_path="/root/deeplabv3/data/thn") bbox_encoder = BboxEncoder(img_h=test_dataset.img_height, img_w=test_dataset.img_width) num_test_batches = int(len(test_dataset) / batch_size) print("num_test_batches:", num_test_batches) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False, num_workers=4) network.eval() # (set in evaluation mode, this affects BatchNorm and dropout) eval_dict = {} for step, (imgs, img_ids) in enumerate(test_loader): with torch.no_grad( ): # (corresponds to setting volatile=True in all variables, this is done during inference to reduce memory consumption) imgs = Variable( imgs).cuda() # (shape: (batch_size, 3, img_heigth, img_width)) outputs = network(imgs) outputs_regr = outputs[ 0] # (shape: (batch_size, num_anchors, 4)) (x_resid, y_resid, w_resid, h_resid) outputs_class = outputs[ 1] # (shape: (batch_size, num_classes, num_anchors)) ######################################################################## # save data for visualization:
from retinanet import RetinaNet from encoder import DataEncoder from PIL import Image, ImageDraw import os import cv2 import time import math import numpy as np os.environ["CUDA_VISIBLE_DEVICES"] = "1" print('Loading model..') net_det = RetinaNet().cuda() net_det.load_state_dict( torch.load('../pytorch-retinanet/checkpoint/model_10_.pth')['net']) net_det.eval() net_det = torch.nn.DataParallel(net_det, device_ids=range(torch.cuda.device_count())) net_map = FCN().cuda() net_map.load_state_dict( torch.load('./../dense_finetune/models/inception_smallMap_3_0.8880.pth')) net_map.eval() net_map = torch.nn.DataParallel(net_map, device_ids=range(torch.cuda.device_count())) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) ])
from datasets import CocoDetection train_loader = torch.utils.data.DataLoader( CocoDetection(root="./datasets/COCO/train2017", annFile="./datasets/COCO/annotations/instances_train2017.json", transform=transforms.Compose([ transforms.ToTensor(), # normalized because of the pretrained imagenet transforms.Normalize((0.1307,), (0.3081,)) ])), # batch size should be 16 batch_size=1, shuffle=True) model = RetinaNet(classes=80) model.eval() optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=0.0001) scheduler = lr_scheduler.MultiStepLR(optimizer, # Milestones are set assuming batch size is 16: # 60000 / batch_size = 3750 # 80000 / batch_size = 5000 milestones=[3750, 5000], gamma=0.1) criterion = FocalLoss(80)
def test(): import torchvision transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.485,0.456,0.406), (0.229,0.224,0.225)) ]) dataType = 'train2017' root_path = "../COCO_dataset/images/%s"%(dataType) list_root_path = "./data/%s.txt"%(dataType) dataset = ListDataset(root=root_path,list_file=list_root_path, train=True, transform=transform, input_size=360) dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True, num_workers=1, collate_fn=dataset.collate_fn) for images, loc_targets, cls_targets in dataloader: print(images.shape) print(loc_targets.shape) print(cls_targets.shape) grid = torchvision.utils.make_grid(images, 1) torchvision.utils.save_image(grid, 'a.jpg') print('Loading image..') net = RetinaNet() net.eval() img = Image.open('a.jpg') w = h = 360 img = img.resize((w,h)) print('Predicting..') x = transform(img) x = x.unsqueeze(0) x = Variable(x) use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") print('Device used:', device) if torch.cuda.is_available(): net = net.to(device) x = x.to(device) loc_targets = loc_targets.to(device) with torch.no_grad(): loc_preds, cls_preds = net(x) print(loc_preds.shape) print(cls_preds.shape) print('Decoding..') encoder = DataEncoder() boxes, labels, _ = encoder.decode(loc_targets.data.cpu()[0], cls_preds.data.cpu().squeeze(), (w,h)) print("Label : ",labels) draw = ImageDraw.Draw(img) # use a truetype font font = ImageFont.truetype("./font/DELIA_regular.ttf", 20) for i,(box,label) in enumerate(zip(boxes,labels)): draw.rectangle(list(box), outline=color_map(int(label)),width = 5) draw.rectangle(list([box[0],box[1]-17,box[0]+10*len(my_cate[int(label)])+5,box[1]]), outline=color_map(int(label)),width = 3,fill=color_map(int(label))) draw.text((box[0]+3, box[1]-16), my_cate[int(label)],font = font,fill = (0, 0, 0, 100),width = 5) plt.imshow(img) plt.savefig("./test.jpg") break