def run_demo(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type): if dataset_type == "voc": class_names = VOCDataset.class_names elif dataset_type == 'coco': class_names = COCODataset.class_names else: raise NotImplementedError('Not implemented now.') device = torch.device(cfg.MODEL.DEVICE) model = build_detection_model(cfg) model = model.to(device) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) # dummy_input = torch.randn(1, 3, 300, 300, device='cuda') # input_names = ["input"] # output_names = ["output"] # torch.onnx.export(model, dummy_input, "vgg_ssd300_voc.onnx", verbose=True, input_names=input_names, output_names=output_names) image_paths = glob.glob(os.path.join(images_dir, '*.jpg')) mkdir(output_dir) cpu_device = torch.device("cpu") transforms = build_transforms(cfg, is_train=False) model.eval() for i, image_path in enumerate(image_paths): start = time.time() image_name = os.path.basename(image_path) image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) load_time = time.time() - start start = time.time() result = model(images.to(device))[0] inference_time = time.time() - start result = result.resize((width, height)).to(cpu_device).numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] indices = scores > score_threshold boxes = boxes[indices] labels = labels[indices] scores = scores[indices] meters = ' | '.join([ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ]) print('({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters)) drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8) Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))
def evaluation(cfg, ckpt, distributed): logger = logging.getLogger("SSD.inference") model = build_detection_model(cfg) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR, logger=logger) device = torch.device(cfg.MODEL.DEVICE) model.to(device) checkpointer.load(ckpt, use_latest=ckpt is None) model.eval() device = torch.device(cfg.MODEL.DEVICE) data_loaders_val = make_data_loader(cfg, is_train=False, distributed=distributed) for dataset_name, data_loader in zip(cfg.DATASETS.TEST, data_loaders_val): res = [] for batch in data_loader: images, targets, image_ids = batch with torch.no_grad(): torch.cuda.synchronize(device) start = time.time() outputs = model(images.to(device)) torch.cuda.synchronize(device) end = time.time() res.append(end - start) time_sum = 0.0 for i in res: time_sum += i print("FPS: %f" % (float(len(res) * cfg.TEST.BATCH_SIZE) / time_sum))
def run_demo(cfg, ckpt, score_threshold, images_dir: pathlib.Path, output_dir: pathlib.Path, dataset_type, num_images=None): if dataset_type == "voc": class_names = VOCDataset.class_names elif dataset_type == 'coco': class_names = COCODataset.class_names elif dataset_type == "mnist": class_names = MNISTDetection.class_names elif dataset_type == "tdt4265": class_names = TDT4265Dataset.class_names elif dataset_type == "waymo": class_names = WaymoDataset.class_names else: raise NotImplementedError('Not implemented now.') model = SSDDetector(cfg) model = torch_utils.to_cuda(model) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) image_paths = list(images_dir.glob("*.png")) + list( images_dir.glob("*.jpg")) output_dir.mkdir(exist_ok=True, parents=True) transforms = build_transforms(cfg, is_train=False) model.eval() drawn_images = [] for i, image_path in enumerate( tqdm.tqdm(image_paths[:num_images], desc="Predicting on images")): image_name = image_path.stem image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) result = model(torch_utils.to_cuda(images))[0] result = result.resize((width, height)).cpu().numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] indices = scores > score_threshold boxes = boxes[indices] labels = labels[indices] scores = scores[indices] drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8) drawn_images.append(drawn_image) im = Image.fromarray(drawn_image) output_path = output_dir.joinpath(f"{image_name}.png") im.save(output_path) return drawn_images
def evaluation(cfg, ckpt): logger = logging.getLogger("SSD.inference") model = SSDDetector(cfg) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR, logger=logger) model = torch_utils.to_cuda(model) checkpointer.load(ckpt, use_latest=ckpt is None) do_evaluation(cfg, model)
def run_demo(cfg, ckpt, score_threshold, images_dir: pathlib.Path, output_dir: pathlib.Path, dataset_type): if dataset_type == "voc": class_names = VOCDataset.class_names elif dataset_type == 'coco': class_names = COCODataset.class_names elif dataset_type == "mnist": class_names = MNISTDetection.class_names else: raise NotImplementedError('Not implemented now.') model = SSDDetector(cfg) model = torch_utils.to_cuda(model) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) image_paths = list(images_dir.glob("*.png")) + list( images_dir.glob("*.jpg")) output_dir.mkdir(exist_ok=True, parents=True) transforms = build_transforms(cfg, is_train=False) model.eval() drawn_images = [] for i, image_path in enumerate(image_paths): start = time.time() image_name = image_path.name image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) load_time = time.time() - start start = time.time() result = model(torch_utils.to_cuda(images))[0] inference_time = time.time() - start result = result.resize((width, height)).cpu().numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] indices = scores > score_threshold boxes = boxes[indices] labels = labels[indices] scores = scores[indices] meters = "|".join([ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ]) image_name = image_path.name drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8) drawn_images.append(drawn_image) return drawn_images
def evaluation(cfg, ckpt, distributed): logger = logging.getLogger("SSD.inference") model = build_detection_model(cfg) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR, logger=logger) device = torch.device(cfg.MODEL.DEVICE) model.to(device) checkpointer.load(ckpt, use_latest=ckpt is None) do_evaluation(cfg, model, distributed)
def creat_model(cfg, ckpt): device = torch.device(cfg.MODEL.DEVICE) model = build_detection_model(cfg) model = model.to(device) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) return model
def evaluation(cfg, ckpt, distributed): logger = logging.getLogger("SSD.inference") model = build_detection_model(cfg) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR, logger=logger) device = torch.device(cfg.MODEL.DEVICE) #model.load_state_dict(torch.load('outputs/vgg_ssd300_voc0712.pth'), strict=False) model.to(device) checkpointer.load(ckpt, use_latest=ckpt is None) do_evaluation(cfg, model, distributed)
def evaluation(cfg, ckpt, distributed): logger: logging.RootLogger = logging.getLogger("SSD.inference") model = build_detection_model(cfg) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR, logger=logger) device = torch.device(cfg.MODEL.DEVICE) model.to(device) checkpointer.load(ckpt, use_latest=ckpt is None) for scale in np.linspace(0.5, 1.0, 5): logger.info(f"Running eval with rescale factor: {scale}") eval_result = do_evaluation(cfg, model, distributed, rescale=scale)
def get_detections(cfg, ckpt): model = SSDDetector(cfg) model = torch_utils.to_cuda(model) checkpointer = CheckPointer(cfg, model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) dataset_path = DatasetCatalog.DATASETS["tdt4265_test"]["data_dir"] dataset_path = pathlib.Path(cfg.DATASET_DIR, dataset_path) image_dir = pathlib.Path(dataset_path, "images") image_paths = list(image_dir.glob("*.jpg")) transforms = build_transforms(cfg, is_train=False) model.eval() detections = [] labels = read_labels( image_dir.parent.parent.joinpath("train", "labels.json")) check_all_images_exists(labels, image_paths) # Filter labels on if they are test and only take the 7th frame labels = [label for label in labels if label["is_test"]] labels = [label for label in labels if label["image_id"] % 7 == 0] for i, label in enumerate(tqdm.tqdm(labels, desc="Inference on images")): image_id = label["image_id"] image_path = image_dir.joinpath(f"{image_id}.jpg") image_detections = {"image_id": int(image_id), "bounding_boxes": []} image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) result = model(torch_utils.to_cuda(images))[0] result = result.resize((width, height)).cpu().numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] for idx in range(len(boxes)): box = boxes[idx] label_id = labels[idx] label = TDT4265Dataset.class_names[label_id] assert label != "__background__" score = float(scores[idx]) assert box.shape == (4, ) json_box = { "xmin": float(box[0]), "ymin": float(box[1]), "xmax": float(box[2]), "ymax": float(box[3]), "label": str(label), "label_id": int(label_id), "confidence": float(score) } image_detections["bounding_boxes"].append(json_box) detections.append(image_detections) return detections
def evaluation(cfg, ckpt, distributed, model_path=None): logger = logging.getLogger("SSD.inference") model = build_detection_model(cfg) logger.info("Model :\n{}".format(model)) #如果用print,多gpu会打印两便 checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR, logger=logger) device = torch.device(cfg.MODEL.DEVICE) model.to(device) if model_path is None: checkpointer.load(ckpt, use_latest=ckpt is None) else: model.load_state_dict(torch.load(model_path)) if cfg.TEST.BN_FUSE is True: print('BN_FUSE.') model.backbone.bn_fuse() model.to(device) do_evaluation(cfg, model, distributed)
def start_train(cfg): logger = logging.getLogger('SSD.trainer') model = SSDDetector(cfg) model = torch_utils.to_cuda(model) lr = cfg.SOLVER.LR optimizer = make_optimizer(cfg, model, lr) milestones = [step for step in cfg.SOLVER.LR_STEPS] scheduler = make_lr_scheduler(cfg, optimizer, milestones) arguments = {"iteration": 0} save_to_disk = True checkpointer = CheckPointer(cfg, model, optimizer, scheduler, cfg.OUTPUT_DIR, save_to_disk, logger) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER train_loader = make_data_loader(cfg, is_train=True, max_iter=max_iter, start_iter=arguments['iteration']) model = do_train(cfg, model, train_loader, optimizer, scheduler, checkpointer, arguments) return model
def train(cfg, args): # 工厂模式,加载日志文件设置,这里暂时不同管 logger = logging.getLogger('SSD.trainer') # 建立目标检测模型 model = build_detection_model(cfg) # 设置Device并且把模型部署到设备上 device = torch.device(cfg.MODEL.DEVICE) model.to(device) if args.distributed: model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank], output_device=args.local_rank) # 设置学习率、优化器还有学习率变化步长,可以理解为模拟退火这种,前面的步长比较大,后面的步长比较小 lr = cfg.SOLVER.LR * args.num_gpus # scale by num gpus optimizer = make_optimizer(cfg, model, lr) milestones = [step // args.num_gpus for step in cfg.SOLVER.LR_STEPS] scheduler = make_lr_scheduler(cfg, optimizer, milestones) arguments = {"iteration": 0} save_to_disk = dist_util.get_rank() == 0 # **** 这里应该是从断点开始对模型进行训练 **** checkpointer = CheckPointer(model, optimizer, scheduler, cfg.OUTPUT_DIR, save_to_disk, logger) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) # Important 通过torch的形式去加载数据集 # 关键在于如何加载数据集,模型的构建过程可以简单地看成是黑盒 max_iter = cfg.SOLVER.MAX_ITER // args.num_gpus train_loader = make_data_loader(cfg, is_train=True, distributed=args.distributed, max_iter=max_iter, start_iter=arguments['iteration']) # 正式开始训练, 暂时先不训练? # 不对,不训练也得加载数据集**** 暂时不训练就完事了 *** 直接看数据加载过程 # model = do_train(cfg, model, train_loader, optimizer, scheduler, checkpointer, device, arguments, args) return model
def main(video, config): class_name = ('__background__', 'lubang', 'retak aligator', 'retak melintang', 'retak memanjang') cfg.merge_from_file(config) cfg.freeze() ckpt = None device = torch.device(cfg.MODEL.DEVICE) model = build_detection_model(cfg) model.to(device) checkpoint = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpoint.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpoint.get_checkpoint_file() print(f'Loading weight from {weight_file}')
def start_train(cfg): logger = logging.getLogger('SSD.trainer') model = SSDDetector(cfg) model = torch_utils.to_cuda(model) optimizer = torch.optim.SGD( filter(lambda p: p.requires_grad, model.parameters()), lr=cfg.SOLVER.LR, momentum=cfg.SOLVER.MOMENTUM, weight_decay=cfg.SOLVER.WEIGHT_DECAY, nesterov=True, ) scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer, T_max=int(cfg.SOLVER.MAX_ITER / 1000), eta_min=0) arguments = {"iteration": 0} save_to_disk = True checkpointer = CheckPointer( model, optimizer, cfg.OUTPUT_DIR, save_to_disk, logger, ) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER train_loader = make_data_loader(cfg, is_train=True, max_iter=max_iter, start_iter=arguments['iteration']) model = do_train(cfg, model, train_loader, optimizer, checkpointer, arguments, scheduler) return model
def train(cfg, args): logger = logging.getLogger('SSD.trainer') model = build_detection_model(cfg) device = torch.device(cfg.MODEL.DEVICE) model.to(device) if args.distributed: model = torch.nn.parallel.DistributedDataParallel( model, device_ids=[args.local_rank], output_device=args.local_rank) lr = cfg.SOLVER.LR * args.num_gpus # scale by num gpus optimizer = make_optimizer(cfg, model, lr) milestones = [step // args.num_gpus for step in cfg.SOLVER.LR_STEPS] scheduler = make_lr_scheduler(cfg, optimizer, milestones) arguments = {"iteration": 0} save_to_disk = dist_util.get_rank() == 0 checkpointer = CheckPointer(model, optimizer, scheduler, cfg.OUTPUT_DIR, save_to_disk, logger) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER // args.num_gpus train_loader = make_data_loader(cfg, is_train=True, distributed=args.distributed, max_iter=max_iter, start_iter=arguments['iteration']) model = do_train(cfg, model, train_loader, optimizer, scheduler, checkpointer, device, arguments, args) return model
def start_train(cfg): logger = logging.getLogger('SSD.trainer') model = SSDDetector(cfg) model = torch_utils.to_cuda(model) optimizer = torch.optim.SGD( model.parameters(), lr=cfg.SOLVER.LR, momentum=cfg.SOLVER.MOMENTUM, weight_decay=cfg.SOLVER.WEIGHT_DECAY ) arguments = {"iteration": 0} save_to_disk = True checkpointer = CheckPointer( model, optimizer, cfg.OUTPUT_DIR, save_to_disk, logger, ) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER train_loader = make_data_loader(cfg, is_train=True, max_iter=max_iter, start_iter=arguments['iteration']) model = do_train( cfg, model, train_loader, optimizer, checkpointer, arguments) return model
def train(cfg, args): logger = logging.getLogger('SSD.trainer') model = build_detection_model(cfg) device = torch.device(cfg.MODEL.DEVICE) model.to(device) if args.distributed: model = torch.nn.parallel.DistributedDataParallel( model, device_ids=[args.local_rank], output_device=args.local_rank) lr = cfg.SOLVER.LR * args.num_gpus # scale by num gpus optimizer = make_optimizer(cfg, model, lr) milestones = [step // args.num_gpus for step in cfg.SOLVER.LR_STEPS] scheduler = make_lr_scheduler(cfg, optimizer, milestones) arguments = {"iteration": 0} save_to_disk = dist_util.get_rank() == 0 checkpointer = CheckPointer(model, optimizer, scheduler, cfg.OUTPUT_DIR, save_to_disk, logger) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER // args.num_gpus train_loader = make_data_loader(cfg, is_train=True, distributed=args.distributed, max_iter=max_iter, start_iter=arguments['iteration']) # macs, params = profile(model, inputs=(input, )) # # macs, params = clever_format([flops, params], "%.3f") # net = model.to() # with torch.cuda.device(0): # net = model.to(device) # macs, params = get_model_complexity_info(net, (3, 512, 512), as_strings=True, # print_per_layer_stat=True, verbose=True) # print('{:<30} {:<8}'.format('Computational complexity: ', macs)) # print('{:<30} {:<8}'.format('Number of parameters: ', params)) n_params = sum(p.numel() for name, p in model.named_parameters() if p.requires_grad) print(n_params) # # model = net # inputs = torch.randn(1, 3, 300, 300) #8618 305 # inputs = torch.randn(1, 3, 300, 300) # macs = profile_macs(model, inputs) # print(macs) model = do_train(cfg, model, train_loader, optimizer, scheduler, checkpointer, device, arguments, args) return model
def start_train(cfg, visualize_example=False): logger = logging.getLogger('SSD.trainer') model = SSDDetector(cfg) print(model) model = torch_utils.to_cuda(model) optimizer = torch.optim.SGD(model.parameters(), lr=cfg.SOLVER.LR, momentum=cfg.SOLVER.MOMENTUM, weight_decay=cfg.SOLVER.WEIGHT_DECAY) """ optimizer = torch.optim.Adam( model.parameters(), lr=cfg.SOLVER.LR, weight_decay=cfg.SOLVER.WEIGHT_DECAY ) """ """ lr_scheduler = torch.optim.lr_scheduler.CyclicLR( optimizer= optimizer, base_lr= cfg.SOLVER.LR /10, max_lr=0.05, step_size_up=8000, mode='triangular2' ) """ arguments = {"iteration": 0} save_to_disk = True checkpointer = CheckPointer( model, optimizer, cfg.OUTPUT_DIR, save_to_disk, logger, ) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER train_loader = make_data_loader(cfg, is_train=True, max_iter=max_iter, start_iter=arguments['iteration']) model = do_train(cfg, model, train_loader, optimizer, checkpointer, arguments, visualize_example, lr_scheduler=None) return model
def get_detections(cfg, ckpt): model = SSDDetector(cfg) model = torch_utils.to_cuda(model) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) dataset_path = DatasetCatalog.DATASETS["tdt4265_test"]["data_dir"] dataset_path = pathlib.Path(cfg.DATASET_DIR, dataset_path) image_dir = pathlib.Path(dataset_path) image_paths = list(image_dir.glob("*.jpg")) transforms = build_transforms(cfg, is_train=False) model.eval() detections = [] for image_path in tqdm.tqdm(image_paths, desc="Inference on images"): image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) result = model(torch_utils.to_cuda(images))[0] result = result.resize((width, height)).cpu().numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] for idx in range(len(boxes)): box = boxes[idx] label_id = labels[idx] label = TDT4265Dataset.class_names[label_id] assert label != "__background__" score = float(scores[idx]) assert box.shape == (4, ) xmin, ymin, xmax, ymax = box width = xmax - xmin height = ymax - ymin detections.append({ "image_id": image_path.stem, "category_id": LABEL_MAP[label], "score": score, "bbox": [xmin, ymin, width, height] }) return detections
def evaluation(cfg, ckpt, N_images: int): model = SSDDetector(cfg) logger = logging.getLogger("SSD.inference") checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR, logger=logger) model = torch_utils.to_cuda(model) checkpointer.load(ckpt, use_latest=ckpt is None) model.eval() data_loaders_val = make_data_loader(cfg, is_train=False) for data_loader in data_loaders_val: batch = next(iter(data_loader)) images, targets, image_ids = batch images = torch_utils.to_cuda(images) imshape = list(images.shape[2:]) # warmup print("Checking runtime for image shape:", imshape) for i in range(10): model(images) start_time = time.time() for i in range(N_images): outputs = model(images) total_time = time.time() - start_time print("Runtime for image shape:", imshape) print("Total runtime:", total_time) print("FPS:", N_images / total_time)
def start_train(cfg): logger = logging.getLogger('SSD.trainer') model = SSDDetector(cfg) model = torch_utils.to_cuda(model) if cfg.SOLVER.TYPE == "adam": optimizer = torch.optim.Adam( model.parameters(), lr=cfg.SOLVER.LR, weight_decay=cfg.SOLVER.WEIGHT_DECAY, ) elif cfg.SOLVER.TYPE == "sgd": optimizer = torch.optim.SGD(model.parameters(), lr=cfg.SOLVER.LR, weight_decay=cfg.SOLVER.WEIGHT_DECAY, momentum=cfg.SOLVER.MOMENTUM) else: # Default to Adam if incorrect solver print("WARNING: Incorrect solver type, defaulting to Adam") optimizer = torch.optim.Adam( model.parameters(), lr=cfg.SOLVER.LR, weight_decay=cfg.SOLVER.WEIGHT_DECAY, ) scheduler = LinearMultiStepWarmUp(cfg, optimizer) arguments = {"iteration": 0} save_to_disk = True checkpointer = CheckPointer( model, optimizer, cfg.OUTPUT_DIR, save_to_disk, logger, ) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER train_loader = make_data_loader(cfg, is_train=True, max_iter=max_iter, start_iter=arguments['iteration']) model = do_train(cfg, model, train_loader, optimizer, checkpointer, arguments, scheduler) return model
def train(cfg, args): logger = logging.getLogger('SSD.trainer') model = build_detection_model(cfg) device = torch.device(cfg.MODEL.DEVICE) model.to(device) if args.distributed: model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model) model = torch.nn.parallel.DistributedDataParallel( model, device_ids=[args.local_rank], output_device=args.local_rank) lr = cfg.SOLVER.LR * args.num_gpus # scale by num gpus optimizer = make_optimizer(cfg, model, lr) milestones = [step // args.num_gpus for step in cfg.SOLVER.LR_STEPS] scheduler = make_lr_scheduler(cfg, optimizer, milestones) arguments = {"iteration": 0} save_to_disk = dist_util.get_rank() == 0 checkpointer = CheckPointer(model, optimizer, scheduler, cfg.OUTPUT_DIR, save_to_disk, logger) extra_checkpoint_data = checkpointer.load(args.ckpt) arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER // args.num_gpus train_loader = make_data_loader(cfg, is_train=True, distributed=args.distributed, max_iter=max_iter, start_iter=arguments['iteration']) logging.info('==>Start statistic') do_run(cfg, model, distributed=args.distributed) logging.info('==>End statistic') for ops in model.modules(): if isinstance(ops, torch.nn.ReLU): ops.collectStats = False # ops.c.data = ops.running_mean + (ops.running_b * laplace[args.actBitwidth]) ops.c.data = ops.running_mean + (3 * ops.running_std) ops.quant = True torch.cuda.empty_cache() model = do_train(cfg, model, train_loader, optimizer, scheduler, checkpointer, device, arguments, args) return model
def start_train(cfg): logger = logging.getLogger('SSD.trainer') model = SSDDetector(cfg) model = torch_utils.to_cuda(model) # SGD # optimizer = torch.optim.SGD( # model.parameters(), # lr=cfg.SOLVER.LR, # momentum=cfg.SOLVER.MOMENTUM, # weight_decay=cfg.SOLVER.WEIGHT_DECAY # ) # Adam optimizer = torch.optim.Adam(model.parameters(), lr=cfg.SOLVER.LR, weight_decay=cfg.SOLVER.WEIGHT_DECAY) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer=optimizer, milestones=[6000, 10000], gamma=cfg.SOLVER.GAMMA) arguments = {"iteration": 0} save_to_disk = True checkpointer = CheckPointer( model, optimizer, cfg.OUTPUT_DIR, save_to_disk, logger, ) extra_checkpoint_data = checkpointer.load() arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER train_loader = make_data_loader(cfg, is_train=True, max_iter=max_iter, start_iter=arguments['iteration']) model = do_train(cfg, model, train_loader, optimizer, checkpointer, arguments, scheduler) return model
def train(cfg: CfgNode, args: Namespace, output_dir: Path, model_manager: Dict[str, Any], freeze_non_sigma: bool = False): logger = logging.getLogger('SSD.trainer') model = build_detection_model(cfg) device = torch.device(cfg.MODEL.DEVICE) model.to(device) if args.distributed: model = torch.nn.parallel.DistributedDataParallel( model, device_ids=[args.local_rank], output_device=args.local_rank) lr = cfg.SOLVER.LR * args.num_gpus # scale by num gpus optimizer = make_optimizer(cfg, model, lr) milestones = [step // args.num_gpus for step in cfg.SOLVER.LR_STEPS] scheduler = make_lr_scheduler(cfg, optimizer, milestones) arguments = {"iteration": 0} save_to_disk = dist_util.get_rank() == 0 checkpointer = CheckPointer(model, optimizer, scheduler, cfg.OUTPUT_DIR, save_to_disk, logger) resume_from = checkpointer.get_best_from_experiment_dir(cfg) extra_checkpoint_data = checkpointer.load(f=resume_from) arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER // args.num_gpus train_loader = make_data_loader(cfg, is_train=True, distributed=args.distributed, max_iter=max_iter, start_iter=arguments['iteration']) # Weight freezing test: # print_model(model) # freeze_weights(model) print_model(model) model = do_train(cfg, model, train_loader, optimizer, scheduler, checkpointer, device, arguments, args, output_dir, model_manager) return model
def train(cfg, args): logger = logging.getLogger('SSD.trainer') model = build_detection_model(cfg) # 建立模型 device = torch.device(cfg.MODEL.DEVICE) # 看cfg怎么组织的,把文件和args剥离开 model.to(device) if args.distributed: model = torch.nn.parallel.DistributedDataParallel( model, device_ids=[args.local_rank], output_device=args.local_rank) # model = nn.DataParallel(model) lr = cfg.SOLVER.LR * args.num_gpus # scale by num gpus optimizer = make_optimizer(cfg, model, lr) # 建立优化器 milestones = [step // args.num_gpus for step in cfg.SOLVER.LR_STEPS] scheduler = make_lr_scheduler(cfg, optimizer, milestones) arguments = {"iteration": 0} save_to_disk = dist_util.get_rank() == 0 checkpointer = CheckPointer(model, optimizer, scheduler, save_dir=cfg.OUTPUT_DIR, save_to_disk=save_to_disk, logger=logger) # 建立模型存储载入类,给save_dir赋值表示 extra_checkpoint_data = checkpointer.load(f='', use_latest=False) # 载入模型 arguments.update(extra_checkpoint_data) max_iter = cfg.SOLVER.MAX_ITER // args.num_gpus train_loader = make_data_loader(cfg, is_train=True, distributed=args.distributed, max_iter=max_iter, start_iter=arguments['iteration']) # 建立数据库 print("dataloader: ", train_loader.batch_size) # exit(1232) model = do_train(cfg, model, train_loader, optimizer, scheduler, checkpointer, device, arguments, args) # 训练 return model
def run_demo(cfg, ckpt, score_threshold, images_dir, output_dir, onnx_dir, dataset_type): if dataset_type == "voc": class_names = VOCDataset.class_names elif dataset_type == 'coco': class_names = COCODataset.class_names else: raise NotImplementedError('Not implemented now.') device = torch.device(cfg.MODEL.DEVICE) device = "cpu" if not torch.cuda.is_available() else device model = build_detection_model(cfg) model = model.to(device) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) image_paths = glob.glob(os.path.join(images_dir, '*.jpg')) mkdir(output_dir) cpu_device = torch.device("cpu") transforms = build_transforms(cfg, is_train=False) model.eval() # get model ready for onnx export mkdir(onnx_dir) model_onnx = build_detection_model(cfg) model_onnx = model_onnx.to(device) checkpointer_onnx = CheckPointer(model_onnx, save_dir=cfg.OUTPUT_DIR) checkpointer_onnx.load(ckpt, use_latest=ckpt is None) # replace the SSD box head postprocessor with the onnx version for exporting model_onnx.box_head.post_processor = PostProcessorOnnx(cfg) model_onnx.eval() # export with ONNX # onnx modle takes the name of the pth ckpt file model_onnx_name = os.path.basename(ckpt).split('.')[0] + ".onnx" model_onnx_path = os.path.join(onnx_dir, model_onnx_name) if not os.path.exists(model_onnx_path): print(f'Model exported as onnx to {model_onnx_path}') dummy_input = torch.zeros( [1, 3, cfg.INPUT.IMAGE_SIZE, cfg.INPUT.IMAGE_SIZE]).to(device) torch.onnx.export(model_onnx, dummy_input, model_onnx_path, export_params=True, do_constant_folding=True, opset_version=11, input_names=['input'], output_names=['boxes', 'scores', 'labels'], dynamic_axes={ 'input': {0: 'batch_size', 2: "height", 3: "width"}}, verbose=False) # load exported onnx model for inference test print( f'Loading exported onnx model from {model_onnx_path} for inference comparison test') onnx_runtime_sess = onnxruntime.InferenceSession(model_onnx_path) for i, image_path in enumerate(image_paths): start = time.time() image_name = os.path.basename(image_path) image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) load_time = time.time() - start start = time.time() result = model(images.to(device))[0] inference_time = time.time() - start result = result.resize((width, height)).to(cpu_device).numpy() boxes, labels, scores = result['boxes'], result['labels'], result['scores'] indices = scores > score_threshold boxes, labels, scores = boxes[indices], labels[indices], scores[indices] meters = ' | '.join( [ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ] ) print('Pytorch: ({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters)) drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8) Image.fromarray(drawn_image).save( os.path.join(output_dir, "pytorch_" + image_name)) """ Compute ONNX Runtime output prediction """ start = time.time() ort_inputs = {onnx_runtime_sess.get_inputs()[0].name: np.array(images)} boxes, scores, labels = onnx_runtime_sess.run(None, ort_inputs) inference_time = time.time() - start indices = scores > score_threshold boxes, labels, scores = boxes[indices], labels[indices], scores[indices] # resize bounding boxes to size of the original image boxes[:, 0::2] *= (width) boxes[:, 1::2] *= (height) meters = ' | '.join( [ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ] ) print('Onnx: ({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters)) drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8) Image.fromarray(drawn_image).save( os.path.join(output_dir, "onnx_" + image_name))
def run_demo(cfg, ckpt, score_threshold, images_dir, output_dir): class_names = VOCDataset.class_names device = torch.device(cfg.MODEL.DEVICE) model = build_detection_model(cfg) model = model.to(device) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) image_paths = glob.glob(os.path.join(images_dir, '*.bmp')) mkdir(output_dir) cpu_device = torch.device("cpu") transforms = build_transforms(cfg, is_train=False) model.eval() for i, image_path in enumerate(image_paths): start = time.time() image_name = os.path.basename(image_path) image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) load_time = time.time() - start start = time.time() result = model(images.to(device))[0] inference_time = time.time() - start result = result.resize((width, height)).to(cpu_device).numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] indices = scores > score_threshold boxes = boxes[indices] labels = labels[indices] meters = ' | '.join([ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ]) print('({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters)) text = ['__background__'] resDic = {} for j in range(len(boxes)): xmin = int(boxes[j, 0]) ymin = int(boxes[j, 1]) xmax = int(boxes[j, 2]) ymax = int(boxes[j, 3]) if labels[j] == 1: xmin += 140 xmax -= 130 elif labels[j] == 2: xmin += 130 elif labels[j] == 4: xmin += 40 hight = ymax - ymin width = xmax - xmin cropImg = image[ymin:ymin + hight, xmin:xmin + width] cropImg = local_threshold(cropImg) boxes[j, 0] = xmin boxes[j, 1] = ymin boxes[j, 2] = xmax boxes[j, 3] = ymax text_tmp = crnnOcr(Image.fromarray(cropImg)) if labels[j] == 2: text_tmp = re.sub('[^\x00-\xff]', '/', text_tmp) text.append(text_tmp) resDic[class_names[labels[j]]] = text_tmp result = json.dumps(resDic, ensure_ascii=False) print(result)
def get_model(self, cfg, weightfile): model = build_detection_model(cfg) model = model.to(self.device) checkpointer = CheckPointer(model) checkpointer.load(weightfile, use_latest=weightfile is None) return model
result_file = './results/feature_maps_frame75.jpg' class_name = { '__background__', 'lubang', 'retak aligator', 'retak melintang', 'retak memanjang' } cfg.merge_from_file(config) cfg.freeze() ckpt = None device = torch.device('cpu') model = build_detection_model(cfg) model.to(device) checkpoint = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpoint.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpoint.get_checkpoint_file() transforms = build_transforms(cfg, is_train=False) model.eval() conv_layers = [] model_children = list(model.children()) print(len(model_children)) print(type(model_children[0])) print(type(model_children[1])) counter = 0 for i in range(len(model_children)): if type(model_children[i]) == VGG: