Пример #1
0
    def test_resunet(self):
        import BrainMaGe
        from BrainMaGe.models.networks import fetch_model

        weights = Path(BrainMaGe.__file__).parent / 'weights' / 'resunet_ma.pt'
        pt_model = fetch_model(modelname="resunet",
                               num_channels=1,
                               num_classes=2,
                               num_filters=16)
        checkpoint = torch.load(weights, map_location=torch.device('cpu'))
        pt_model.load_state_dict(checkpoint["model_state_dict"])
        pt_model.eval()

        # Get reference output
        inp = torch.randn([1, 1, 128, 128, 128])
        ref = pt_model(inp).detach().numpy()

        # Perform multiple runs with other inputs to make sure that InstanceNorm layer does not stuck
        for _ in range(2):
            dummy_inp = torch.randn(inp.shape)
            pt_model(dummy_inp)

        # Generate OpenVINO IR
        mo_pytorch.convert(pt_model,
                           input_shape=list(inp.shape),
                           model_name='model')

        # Run model with OpenVINO and compare outputs
        net = self.ie.read_network('model.xml', 'model.bin')
        exec_net = self.ie.load_network(net, 'CPU')
        out = exec_net.infer({'input': inp.detach().numpy()})
        out = next(iter(out.values()))

        diff = np.max(np.abs(out - ref))
        self.assertLessEqual(diff, 5e-4)
Пример #2
0
    def check_torchvision_model(self, model_func, size, threshold=1e-5):
        inp_size = [1, 3, size[0], size[1]]

        inp = cv.resize(self.test_img, (size[1], size[0]))
        inp = np.expand_dims(inp.astype(np.float32).transpose(2, 0, 1), axis=0)
        inp /= 255
        inp = torch.tensor(inp)

        # Create model
        model = model_func(pretrained=True, progress=False)
        model.eval()
        ref = model(inp)

        # Convert to OpenVINO IR
        mo_pytorch.convert(model, input_shape=inp_size, model_name='model')

        # Run model with OpenVINO and compare outputs
        net = self.ie.read_network('model.xml', 'model.bin')
        exec_net = self.ie.load_network(net, 'CPU')
        out = exec_net.infer({'input': inp.detach().numpy()})

        if isinstance(ref, torch.Tensor):
            ref = {'': ref}
        for out0, ref0 in zip(out.values(), ref.values()):
            diff = np.max(np.abs(out0 - ref0.detach().numpy()))
            self.assertLessEqual(diff, threshold)
Пример #3
0
    def check_torchvision_model(self, model_func, size, threshold=1e-5):
        inp_size = [1, 3, size[0], size[1]]

        inp = cv.resize(self.test_img, (size[1], size[0]))
        inp = np.expand_dims(inp.astype(np.float32).transpose(2, 0, 1), axis=0)
        inp /= 255
        inp = torch.tensor(inp)

        # Create model
        model = model_func(pretrained=True, progress=False)
        model.eval()
        ref = model(inp)

        # Forward random input through the model to check that nothing got stuck from reference dat
        rand_inp = torch.rand(inp.size(), dtype=inp.dtype)
        model(rand_inp)

        # Convert to OpenVINO IR
        mo_pytorch.convert(model, input_shape=inp_size, model_name='model')

        # Run model with OpenVINO and compare outputs
        net = self.ie.compile_model('model.xml', 'CPU')
        out = net.infer_new_request({'input': inp.detach().numpy()})

        if isinstance(ref, torch.Tensor):
            ref = {'': ref}
        for out0, ref0 in zip(out.values(), ref.values()):
            diff = np.max(np.abs(out0 - ref0.detach().numpy()))
            self.assertLessEqual(diff, threshold)
Пример #4
0
    def test_detectron2_retinanet(self):
        width = 320
        height = 320

        # Load model
        model = model_zoo.get("COCO-Detection/retinanet_R_50_FPN_1x.yaml",
                              trained=True)
        model.eval()

        # Prepare input tensor
        img = cv.resize(self.test_img, (width, height))
        inp = img.transpose(2, 0, 1).astype(np.float32)

        # Get reference prediction
        ref = model([{'image': torch.tensor(inp)}])
        ref = ref[0]['instances'].get_fields()
        ref_boxes = []
        for box, score, class_idx in zip(ref['pred_boxes'], ref['scores'],
                                         ref['pred_classes']):
            xmin, ymin, xmax, ymax = box
            ref_boxes.append([xmin, ymin, xmax, ymax])
            if score > 0.45:
                cv.rectangle(img, (xmin, ymin), (xmax, ymax),
                             color=(0, 180, 255),
                             thickness=3)

        # Convert model to OpenVINO IR
        mo_pytorch.convert(model,
                           input_shape=[1, 3, height, width],
                           model_name='retinanet_R_50_FPN_1x')

        # Get OpenVINO prediction
        net = self.ie.read_network('retinanet_R_50_FPN_1x.xml',
                                   'retinanet_R_50_FPN_1x.bin')
        exec_net = self.ie.load_network(net, 'CPU')
        outs = exec_net.infer({'input': inp.reshape(1, 3, height, width)})
        ie_detections = next(iter(outs.values()))
        ie_detections = ie_detections.reshape(-1, 7)

        for det in ie_detections:
            conf = det[2]
            if conf > 0.45:
                xmin, ymin, xmax, ymax = det[3:]
                cv.rectangle(img, (xmin, ymin), (xmax, ymax),
                             color=(210, 9, 179))

        # Uncomment to visualize detections
        # cv.imshow('RetinaNet (Detectron2)', img)
        # cv.waitKey()

        self.normAssertDetections(ref['pred_classes'], ref['scores'],
                                  ref_boxes, ie_detections[:, 1],
                                  ie_detections[:, 2], ie_detections[:, 3:])
Пример #5
0
    def test_rugpt3(self):
        from transformers import GPT2LMHeadModel, GPT2Tokenizer

        model_name_or_path = "sberbank-ai/rugpt3medium_based_on_gpt2"
        tokenizer = GPT2Tokenizer.from_pretrained(model_name_or_path)
        model = GPT2LMHeadModel.from_pretrained(model_name_or_path)

        text = "Александр Сергеевич Пушкин родился в "
        input_ids = tokenizer.encode(text, return_tensors="pt")
        seq_len = input_ids.shape[1]
        result = model(input_ids)

        # Forward random input through the model to check that nothing got stuck from reference dat
        dummy_inp = torch.randint(0, 255, input_ids.shape)
        model(dummy_inp)

        # Generate OpenVINO IR
        mo_pytorch.convert(
            model,
            model_name='model',
            input='input_ids{i64},position_ids{i64},attention_mask{f32}',
            input_shape='[1, {}],[{}],[1, {}]'.format(seq_len, seq_len,
                                                      seq_len))

        # Run model with OpenVINO and compare outputs
        net = self.ie.read_network('model.xml', 'model.bin')
        exec_net = self.ie.load_network(net, 'CPU')
        out = exec_net.infer({
            'input_ids':
            input_ids,
            'position_ids':
            np.arange(seq_len),
            'attention_mask':
            np.ones((1, seq_len), np.float32)
        })
        out = next(iter(out.values()))

        ref = result[0].detach().numpy()
        diff = np.max(np.abs(out - ref))
        self.assertLessEqual(diff, 1e-4)
Пример #6
0
    def run_mask_rcnn(self, is_dynamic):
        # For better efficiency, you may reduce parameters <box_detections_per_img> (default 100)
        # and <rpn_post_nms_top_n_test> (default 1000).
        model = models.detection.mask_rcnn.maskrcnn_resnet50_fpn(
            pretrained=True, progress=False)
        model.eval()

        # Preprocess input image
        img_size = 800
        inp = self.test_img[:, :, [2, 1, 0]]  # BGR to RGB
        inp = cv.resize(inp, (img_size, img_size))
        inp = np.expand_dims(inp.astype(np.float32).transpose(2, 0, 1), axis=0)
        inp /= 255
        inp = torch.tensor(inp)

        # Run origin model
        with torch.no_grad():
            ref = model(inp)

        # Convert model to IR
        mo_pytorch.convert(model,
                           input_shape=[1, 3, img_size, img_size],
                           model_name='model',
                           is_dynamic=is_dynamic)

        # Do inference
        net = self.ie.load_network('model.xml', 'CPU')
        out = net.infer({'input': inp})
        detections, masks, _ = out.values()

        # Test boxes
        labels = detections[0, 0, :, 1]
        scores = detections[0, 0, :, 2]
        boxes = detections[0, 0, :, 3:] * img_size

        matches = self.normAssertDetections(ref[0]['labels'], ref[0]['scores'],
                                            ref[0]['boxes'], labels, scores,
                                            boxes)

        # Test masks
        for test_id, ref_id in matches:
            ref_mask = ref[0]['masks'][ref_id].detach().numpy()
            class_id = ref[0]['labels'][ref_id]
            out_mask = masks[test_id, class_id]

            # Resize mask to bounding box shape
            l, t, r, b = [
                int(v) for v in detections[0, 0, test_id, 3:] * img_size
            ]
            w = r - l + 1
            h = b - t + 1
            out_mask = cv.resize(out_mask, (w, h))
            out_mask = np.pad(out_mask,
                              ((t, img_size - 1 - b), (l, img_size - 1 - r)))

            prob_thresh = 0.5
            ref_mask = ref_mask > prob_thresh
            out_mask = out_mask > prob_thresh
            inter = np.sum(np.logical_and(ref_mask, out_mask))
            union = np.sum(np.logical_or(ref_mask, out_mask))
            self.assertGreater(inter / union, 0.93)

        if is_dynamic:
            # Forward zero input to check zero dimensions behavior
            out = net.infer({'input': torch.zeros_like(inp)})
Пример #7
0
    'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table', 'N/A',
    'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
    'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator',
    'N/A', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier',
    'toothbrush'
]

# Load origin model
model = models.detection.mask_rcnn.maskrcnn_resnet50_fpn(pretrained=True)
model.eval()

# Convert model to OpenVINO IR
img_size = 800
mo_pytorch.convert(model,
                   input_shape=[1, 3, img_size, img_size],
                   model_name="Mask_RCNN",
                   scale=255,
                   reverse_input_channels=True)

# Prepare input image
img = cv.imread(args.input)
inp = cv.resize(img, (img_size, img_size))
inp = np.expand_dims(inp.astype(np.float32).transpose(2, 0, 1), axis=0)

# Do inference
ie = IECore()
net = ie.read_network('Mask_RCNN.xml')
exec_net = ie.load_network(net, args.device)
out = exec_net.infer({'input': inp})
detections, masks, _ = out.values()
Пример #8
0
    np.random.standard_normal([1, 1, 128, 128, 128]).astype(np.float32))

print("Running PyTorch Inference on random data...")
# Run Pytorch Inference
with torch.no_grad():
    pt_output = pt_model(input_image)

pt_out = pt_output.detach().numpy()

print("Converting PyTorch model to OpenVINO IR...")

ov_model_name = 'resunet_ma'
model_name = ov_model_dir / ov_model_name

mo_pytorch.convert(pt_model,
                   input_shape=[1, 1, 128, 128, 128],
                   data_type="FP32",
                   model_name=model_name)

print(f"\nOpenVINO model saved at {ov_model_dir} \n")

# Run OpenVINO Inference and Compare Inference results
model_xml = f'{ov_model_dir}/{ov_model_name}.xml'

ie = IECore()
net = ie.read_network(model_xml)
exec_net = ie.load_network(net, 'CPU')
inp_name = next(iter(net.inputs.keys()))
out = exec_net.infer({inp_name: input_image})
out = next(iter(out.values()))

# Print Results.
    def test_usrnet(self):
        sys.path.append('USRNet')
        from models.network_usrnet import USRNet as net  # for pytorch version <= 1.7.1
        from utils import utils_deblur
        from utils import utils_sisr as sr
        from utils import utils_image as util

        np.random.seed(324)
        torch.manual_seed(32)
        inp = np.random.standard_normal([1, 3, 56, 112]).astype(np.float32)
        sf = 4

        # source: https://github.com/cszn/USRNet/blob/master/main_test_realapplication.py
        def get_kernel_sigma():
            noise_level_img = 2  # noise level for LR image, 0.5~3 for clean images
            kernel_width_default_x1234 = [
                0.4, 0.7, 1.5, 2.0
            ]  # default Gaussian kernel widths of clean/sharp images for x1, x2, x3, x4

            noise_level_model = noise_level_img / 255.  # noise level of model
            kernel_width = kernel_width_default_x1234[sf - 1]

            k = utils_deblur.fspecial('gaussian', 25, kernel_width)
            k = sr.shift_pixel(k, sf)  # shift the kernel
            k /= np.sum(k)
            kernel = util.single2tensor4(k[..., np.newaxis])
            sigma = torch.tensor(noise_level_model).float().view([1, 1, 1, 1])

            return kernel, sigma

        kernel, sigma = get_kernel_sigma()

        model = net(n_iter=8,
                    h_nc=64,
                    in_nc=4,
                    out_nc=3,
                    nc=[64, 128, 256, 512],
                    nb=2,
                    act_mode="R",
                    downsample_mode='strideconv',
                    upsample_mode="convtranspose")
        model.eval()

        ref = model(torch.tensor(inp), kernel, sf, sigma)

        # Forward random input through the model to check that nothing got stuck from reference data
        dummy_inp = torch.randn(inp.shape)
        dummy_kernel = torch.randn(kernel.shape)
        dummy_sigma = torch.randn(sigma.shape)
        model(dummy_inp, dummy_kernel, sf, dummy_sigma)

        # Generate OpenVINO IR
        mo_pytorch.convert(
            model,
            input_shape='[1, 3, 56, 112],[1, 1, 25, 25],[1, 1, 1, 1],[1]',
            input='x{f32},k{f32},sigma{f32},sf{f32}->4',
            model_name='model')

        # Run model with OpenVINO and compare outputs
        net = self.ie.read_network('model.xml')
        exec_net = self.ie.load_network(net, 'CPU')
        out = exec_net.infer({'x': inp, 'k': kernel, 'sigma': sigma})
        out = next(iter(out.values()))

        diff = np.max(np.abs(ref.detach().numpy() - out))
        self.assertLessEqual(diff, 1e-4)