def test_training(self): model = Yolov4P5(self.cfg) optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9, nesterov=False) model = trainingModel(model.half(), optimizer=optimizer) loss = model(self.input_tensor)
def prepare_model(cfg): opt = argparse.ArgumentParser() opt.weights = 'weights/yolov4-p5-sd.pt' model = Yolov4P5(cfg) model.eval() model = load_and_fuse_pretrained_weights(model, opt) model.optimize_for_inference() ipu_opts = ipu_options(opt, cfg, model) return inferenceModel(model, ipu_opts)
def test_inference_cpu_and_ipu(self): self.cfg.model.half = False self.cfg.model.image_size = 416 self.cfg.inference.nms = False # Create CPU model torch.manual_seed(0) self.cfg.model.ipu = False model = Yolov4P5(self.cfg) cpu_model = model.eval() y_cpu = cpu_model(self.input_tensor) # Create IPU model torch.manual_seed(0) self.cfg.model.ipu = True model = Yolov4P5(self.cfg) ipu_model = inferenceModel(model.eval()) y_ipu = ipu_model(self.input_tensor) assert torch.max(torch.abs(y_cpu[0] - y_ipu[0])) <= 0.002 assert torch.max(torch.abs(y_cpu[1] - y_ipu[1])) <= 0.002 assert torch.max(torch.abs(y_cpu[2] - y_ipu[2])) <= 0.002
def prepare_model(cfg, debugging_nms=False): opt = argparse.ArgumentParser() opt.weights = os.environ['PYTORCH_APPS_DETECTION_PATH'] + '/weights/yolov4-p5-sd.pt' model = Yolov4P5(cfg, debugging_nms=debugging_nms) model.eval() model = load_and_fuse_pretrained_weights(model, opt) model.optimize_for_inference() if cfg.model.ipu: ipu_opts = ipu_options(opt, cfg, model) return inferenceModel(model, ipu_opts) else: return model
def get_model_and_loader(opt: argparse.ArgumentParser, cfg: yacs.config.CfgNode): """Prepares the model and gets a new loader for the model. Parameters: opt: opt object containing options introduced in the command line cfg: yacs object containing the config Returns: model[Detector]: a torch Detector Model loader[DataLoader]: a torch or poptorch DataLoader containing the specified dataset on "cfg" """ # Create model model = Yolov4P5(cfg) if cfg.model.mode == "train": model.train() else: model.eval() # Load weights and fuses some batch normalizations with some convolutions if cfg.model.normalization == 'batch': if opt.weights: print("loading pretrained weights") model = load_and_fuse_pretrained_weights(model, opt) model.optimize_for_inference() # Create the specific ipu options if cfg.model.ipu ipu_opts = ipu_options(opt, cfg, model) if cfg.model.ipu else None # Creates the loader loader = get_loader(opt, cfg, ipu_opts) # Calls the poptorch wrapper and compiles the model if cfg.model.ipu: if cfg.model.mode == "train": model = trainingModel(model, ipu_opts) else: model = inferenceModel(model, ipu_opts) try: img, _, _, _ = next(iter(loader)) model.compile(img) warm_up_iterations = 100 for _ in range(warm_up_iterations): _ = model(img) except Exception as e: print(e.args) exit(0) return model, loader
def test_inference(self): self.cfg.inference.nms = False model = Yolov4P5(self.cfg) model = inferenceModel(model.half().eval()) y = model(self.input_tensor) expected_output_size = model.output_shape((64, 64)) p3 = expected_output_size['p3'] p4 = expected_output_size['p4'] p5 = expected_output_size['p5'] assert y[0].shape == torch.Size([p3[0], p3[1] * p3[2] * p3[3], p3[4]]) assert y[1].shape == torch.Size([p4[0], p4[1] * p4[2] * p4[3], p4[4]]) assert y[2].shape == torch.Size([p5[0], p5[1] * p5[2] * p5[3], p5[4]])
def test_fused_inference(self): self.cfg.model.normalization = 'batch' self.cfg.model.half = False self.cfg.inference.nms = False model = Yolov4P5(self.cfg) before_fuse_model = inferenceModel(model.eval()) before_fuse_output = before_fuse_model(self.input_tensor) model.optimize_for_inference() after_fuse_model = inferenceModel(model.eval()) after_fuse_output = after_fuse_model(self.input_tensor) assert torch.max( torch.abs(after_fuse_output[0] - before_fuse_output[0])) <= 1e-4 assert torch.max( torch.abs(after_fuse_output[1] - before_fuse_output[1])) <= 1e-4 assert torch.max( torch.abs(after_fuse_output[2] - before_fuse_output[2])) <= 1e-4