def eval_augment_voc(input_size, device): return augment.Compose([ augment.Resize(input_size), augment.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), augment.ToTensor(device), ])
def set_train_augment(self, augment_cfg: CN, input_size, img_path_sampler): self.train_augment = augment.Compose([ augment.RandomHFlip(p=augment_cfg.hflip_p), augment.RandomVFlip(p=augment_cfg.vflip_p), augment.RandomSafeCrop(p=augment_cfg.crop_p), augment.ColorJitter( brightness=[-0.1, 0.1], contrast=[0.8, 1.2], saturation=[0.1, 2], p=augment_cfg.color_p, ), augment.Resize(input_size), augment.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) sampler = lambda: super(VOCSampleGetter, self).train(img_path_sampler( )) self.compose_augment = augment.Compose([ augment.Mixup(sampler, p=augment_cfg.mixup_p, beta=1.5), augment.ToTensor('cpu'), ]) # aug2 = augment.Compose([ # augment.Mosaic(sampler, p=1, size=input_size), # augment.Mixup(sampler, p=0, beta=1.5), # augment.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # augment.ToTensor('cpu'), # ]) return self
def benchmark_onnx(config, args): torch.backends.cudnn.benchmark = True with open(config.dataset.eval_txt_file, 'r') as fr: files = [ line.strip() for line in fr.readlines() if len(line.strip()) != 0 ][:100] model = onnx_model_for_benchmark(args.onnx, args.device == 'cuda') size = size_fix(args.size) # warm up print('warmimg up') for _ in range(50): model(np.random.randn(1, 3, *size).astype(np.float32)) process = augment.Compose([ augment.Resize(size), augment.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), augment.HWCtoCHW(), ]) print('loading images') images = _prepare_images(files, process) total_timer = tools.TicToc('TOTAL') forward_timer = tools.TicToc('FORWARD') convert_timer = tools.TicToc('CONVERT') nms_timer = tools.TicToc('NMS') input_shape = torch.FloatTensor(size).to(args.device, non_blocking=False) threshold = args.threshold nms_iou = args.nms_iou for image, shape in tqdm(images): image = image[None, ...] shape = shape[None, ...].to(args.device) total_timer.tic() forward_timer.tic() pred = model(image) forward_timer.toc() convert_timer.tic() bboxes = RECOVER_BBOXES_REGISTER[args.dataset]( torch.from_numpy(pred).to(args.device), input_shape, shape)[0] convert_timer.toc() nms_timer.tic() tools.torch_nms(bboxes, threshold, nms_iou) nms_timer.toc() total_timer.toc() print('BENCHMARK: {} images in {} size'.format(len(images), size)) timers = [total_timer, forward_timer, convert_timer, nms_timer] stats = [t.statistics() for t in timers] for s in stats: s['percent'] = s['mean'] / stats[0]['mean'] _print_statistics(s)
def process_sample(img, bboxes: np.ndarray): process = augment.Compose([ augment.RandomHFlip(p=0.5), augment.RandomSafeCrop(p=0.75), augment.CutOut(p=0.8, size=30, n_holes=3), augment.ColorJitter( brightness=[-0.1, 0.1], contrast=[0.8, 1.2], saturation=[0.1, 2], ), augment.Resize(lambda: [512, 512]), augment.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) img, bboxes = process(img, bboxes) return img, bboxes
def benchmark(config, args): torch.backends.cudnn.benchmark = True with open(config.dataset.eval_txt_file, 'r') as fr: files = [ line.strip() for line in fr.readlines() if len(line.strip()) != 0 ][:100] model = tools.build_model( config.model.cfg_path, args.weight, None, device=args.device, dataparallel=False, qat=args.qat, quantized=args.quant, backend=args.backend, )[0] size = size_fix(args.size) process = augment.Compose([ augment.Resize(size), augment.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), augment.ToTensor(args.device), ]) print('loading images') images = _prepare_images(files, process) total_timer = tools.TicToc('TOTAL') forward_timer = tools.TicToc('FORWARD') convert_timer = tools.TicToc('CONVERT') nms_timer = tools.TicToc('NMS') input_shape = torch.FloatTensor(size).to(args.device, non_blocking=False) threshold = args.threshold nms_iou = args.nms_iou # warm up print('warmimg up') with torch.no_grad(): for _ in range(5): model(torch.rand(1, 3, *size).to(args.device)) torch.cuda.synchronize() for image, shape in tqdm(images): image = image.unsqueeze_(0).to(args.device, non_blocking=False) shape = shape.unsqueeze_(0).to(args.device, non_blocking=False) total_timer.tic() forward_timer.tic() pred = model(image) torch.cuda.synchronize() forward_timer.toc() convert_timer.tic() bboxes = RECOVER_BBOXES_REGISTER[args.dataset](pred, input_shape, shape)[0] torch.cuda.synchronize() convert_timer.toc() nms_timer.tic() tools.torch_nms(bboxes, threshold, nms_iou) torch.cuda.synchronize() nms_timer.toc() total_timer.toc() print('BENCHMARK: {} images in {} size'.format(len(images), size)) timers = [total_timer, forward_timer, convert_timer, nms_timer] stats = [t.statistics() for t in timers] for s in stats: s['percent'] = s['mean'] / stats[0]['mean'] _print_statistics(s)