def create_features(file_list, voc12_root, output_file): os.makedirs(os.path.dirname(output_file), exist_ok=True) dataset = dataloader.VOC12ImageDataset(file_list, voc12_root=voc12_root, resize_long=(229, 229)) data_loader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=1, pin_memory=True, drop_last=False) model_ft, _ = load_network('resnet152', pretrained=True, finetune=False, image_size=299, return_backbone=True) model_ft.eval() model_ft.cuda() features = [] for pack in tqdm(data_loader, total=len(data_loader.dataset)): img = pack['img'].cuda() img_name = pack['name'][0] y = model_ft(img) features.append({ "img_name": img_name, "feature": y.cpu().detach().numpy() }) np.save(output_file, features)
def run(): voc12_root = './data/compcars/' bbox_out_dir = yolo_result_path dataset = dataloader.VOC12ImageDataset('./data/compcars/train/test.txt', voc12_root=voc12_root, img_normal=None, to_torch=False) preds = [] error_cnt = 0 for data in tqdm(dataset): img_name = data['name'] bbox_org_path = os.path.join( voc12_root, img_name.replace('image', 'label') + '.txt') try: with open(bbox_org_path, mode='r') as f: bbox_org = f.readlines()[-1].strip().split(' ') bbox_path = os.path.join(bbox_out_dir, img_name + '.txt').replace( 'data/image/', '') with open(bbox_path, mode='r') as f: bbox = f.readlines()[0].strip().split('\t') iou = bb_intersection_over_union(bbox, bbox_org) preds.append(iou) except Exception as e: print(e) error_cnt += 1 print({'miou': mean(preds)}, len(preds), error_cnt, bbox_out_dir)
def run(args): infer_dataset = dataloader.VOC12ImageDataset(args.infer_list, voc12_root=args.voc12_root) infer_data_loader = DataLoader(infer_dataset, shuffle=False, num_workers=args.num_workers, pin_memory=True) val_json = json.load( open(os.path.join(VOC2012_JSON_FOLDER, 'pascal_val2012.json'))) # Do not use this file for evaluation! coco_output = {} coco_output["images"] = [] coco_output["annotations"] = [] coco_output['categories'] = val_json['categories'] coco_output['type'] = val_json['type'] for iter, pack in tqdm(enumerate(infer_data_loader), total=len(infer_dataset)): img_name = pack['name'][0] img_id = int(img_name[:4] + img_name[5:]) img_size = pack['img'].shape[2:] image_info = pycococreatortools.create_image_info( img_id, img_name + ".jpg", (img_size[1], img_size[0])) coco_output["images"].append(image_info) ann = np.load(os.path.join(args.ins_seg_out_dir, img_name) + '.npy', allow_pickle=True).item() instance_id = 1 for score, mask, class_id in zip(ann['score'], ann['mask'], ann['class']): if score < 1e-5: continue category_info = {'id': class_id, 'is_crowd': False} annotation_info = pycococreatortools.create_annotation_info( instance_id, img_id, category_info, mask, img_size[::-1], tolerance=0) instance_id += 1 coco_output['annotations'].append(annotation_info) with open('voc2012_train_custom.json', 'w') as outfile: json.dump(coco_output, outfile)
def run(args): assert args.voc12_root is not None assert args.train_list is not None assert args.ir_label_out_dir is not None assert args.cam_out_dir is not None dataset = dataloader.VOC12ImageDataset(args.train_list, voc12_root=args.voc12_root, img_normal=None, to_torch=False) dataset = torchutils.split_dataset(dataset, args.num_workers) print('[ ', end='') multiprocessing.spawn(_work, nprocs=args.num_workers, args=(dataset, args), join=True) print(']')
import os import numpy as np from torch.utils.data import DataLoader from wsl_survey.base.nets.networks import load_network from wsl_survey.segmentation.irn.voc12 import dataloader train_dataset = dataloader.VOC12ImageDataset( "data/voc12/train_aug.txt", voc12_root='./datasets/voc2012/VOCdevkit/VOC2012/', resize_long=(229, 229)) train_data_loader = DataLoader(train_dataset, batch_size=1, shuffle=True, num_workers=1, pin_memory=True, drop_last=True) model_ft, _ = load_network('resnet152', pretrained=True, finetune=False, image_size=299, return_backbone=True) model_ft.eval() features = [] counter = 0 for pack in train_data_loader: img = pack['img'] img_name = pack['name'][0] y = model_ft(img)
def run(args): assert args.voc12_root is not None assert args.class_label_dict_path is not None assert args.train_list is not None assert args.ir_label_out_dir is not None assert args.infer_list is not None assert args.irn_network is not None assert args.irn_network_module is not None path_index = indexing.PathIndex(radius=10, default_size=(args.irn_crop_size // 4, args.irn_crop_size // 4)) model = getattr(importlib.import_module(args.irn_network_module), args.irn_network + 'AffinityDisplacementLoss')(path_index) train_dataset = dataloader.VOC12AffinityDataset( args.train_list, label_dir=args.ir_label_out_dir, voc12_root=args.voc12_root, indices_from=path_index.src_indices, indices_to=path_index.dst_indices, hor_flip=True, crop_size=args.irn_crop_size, crop_method="random", rescale=(0.5, 1.5)) train_data_loader = DataLoader(train_dataset, batch_size=args.irn_batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True, drop_last=True) max_step = (len(train_dataset) // args.irn_batch_size) * args.irn_num_epoches param_groups = model.trainable_parameters() optimizer = torchutils.PolyOptimizer([{ 'params': param_groups[0], 'lr': 1 * args.irn_learning_rate, 'weight_decay': args.irn_weight_decay }, { 'params': param_groups[1], 'lr': 10 * args.irn_learning_rate, 'weight_decay': args.irn_weight_decay }], lr=args.irn_learning_rate, weight_decay=args.irn_weight_decay, max_step=max_step) if use_gpu: model = torch.nn.DataParallel(model).cuda() model.train() avg_meter = pyutils.AverageMeter() timer = pyutils.Timer() for ep in range(args.irn_num_epoches): print('Epoch %d/%d' % (ep + 1, args.irn_num_epoches)) for iter, pack in tqdm(enumerate(train_data_loader), total=len(train_dataset) // args.irn_batch_size): img = pack['img'] bg_pos_label = pack['aff_bg_pos_label'] fg_pos_label = pack['aff_fg_pos_label'] neg_label = pack['aff_neg_label'] if use_gpu: img = img.cuda(non_blocking=True) bg_pos_label = bg_pos_label.cuda(non_blocking=True) fg_pos_label = fg_pos_label.cuda(non_blocking=True) neg_label = neg_label.cuda(non_blocking=True) pos_aff_loss, neg_aff_loss, dp_fg_loss, dp_bg_loss = model( img, True) bg_pos_aff_loss = torch.sum( bg_pos_label * pos_aff_loss) / (torch.sum(bg_pos_label) + 1e-5) fg_pos_aff_loss = torch.sum( fg_pos_label * pos_aff_loss) / (torch.sum(fg_pos_label) + 1e-5) pos_aff_loss = bg_pos_aff_loss / 2 + fg_pos_aff_loss / 2 neg_aff_loss = torch.sum( neg_label * neg_aff_loss) / (torch.sum(neg_label) + 1e-5) dp_fg_loss = torch.sum(dp_fg_loss * torch.unsqueeze( fg_pos_label, 1)) / (2 * torch.sum(fg_pos_label) + 1e-5) dp_bg_loss = torch.sum(dp_bg_loss * torch.unsqueeze( bg_pos_label, 1)) / (2 * torch.sum(bg_pos_label) + 1e-5) avg_meter.add({ 'loss1': pos_aff_loss.item(), 'loss2': neg_aff_loss.item(), 'loss3': dp_fg_loss.item(), 'loss4': dp_bg_loss.item() }) total_loss = (pos_aff_loss + neg_aff_loss) / 2 + (dp_fg_loss + dp_bg_loss) / 2 optimizer.zero_grad() total_loss.backward() optimizer.step() if (optimizer.global_step - 1) % 50 == 0: timer.update_progress(optimizer.global_step / max_step) print('step:%5d/%5d' % (optimizer.global_step - 1, max_step), 'loss:%.4f %.4f %.4f %.4f' % (avg_meter.pop('loss1'), avg_meter.pop('loss2'), avg_meter.pop('loss3'), avg_meter.pop('loss4')), 'imps:%.1f' % ((iter + 1) * args.irn_batch_size / timer.get_stage_elapsed()), 'lr: %.4f' % (optimizer.param_groups[0]['lr']), 'etc:%s' % (timer.str_estimated_complete()), flush=True) else: timer.reset_stage() infer_dataset = dataloader.VOC12ImageDataset(args.infer_list, voc12_root=args.voc12_root, crop_size=args.irn_crop_size, crop_method="top_left") infer_data_loader = DataLoader(infer_dataset, batch_size=args.irn_batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True, drop_last=True) model.eval() print('Analyzing displacements mean ... ', end='') dp_mean_list = [] with torch.no_grad(): for iter, pack in tqdm(enumerate(infer_data_loader), total=len(infer_dataset) // args.irn_batch_size): img = pack['img'] if use_gpu: img = img.cuda(non_blocking=True) aff, dp = model(img, False) dp_mean_list.append(torch.mean(dp, dim=(0, 2, 3)).cpu()) try: model.module.mean_shift.running_mean = torch.mean( torch.stack(dp_mean_list), dim=0) except: model.mean_shift.running_mean = torch.mean( torch.stack(dp_mean_list), dim=0) print('done.') try: state_dict = model.module.state_dict() except: state_dict = model.state_dict() torch.save(state_dict, args.irn_weights_name) if use_gpu: torch.cuda.empty_cache()