Пример #1
0
    def _test_model(self, config_path, inference_func, batch=1):
        model = model_zoo.get(config_path, trained=True)
        image = get_sample_coco_image()
        inputs = tuple(image.clone() for _ in range(batch))

        wrapper = TracingAdapter(model, inputs, inference_func)
        wrapper.eval()
        with torch.no_grad():
            # trace with smaller images, and the trace must still work
            trace_inputs = tuple(
                nn.functional.interpolate(
                    image, scale_factor=random.uniform(0.5, 0.7))
                for _ in range(batch))
            traced_model = torch.jit.trace(wrapper, trace_inputs)

            outputs = inference_func(model, *inputs)
            traced_outputs = wrapper.outputs_schema(traced_model(*inputs))
        if batch > 1:
            for output, traced_output in zip(outputs, traced_outputs):
                assert_instances_allclose(output,
                                          traced_output,
                                          size_as_tensor=True)
        else:
            assert_instances_allclose(outputs,
                                      traced_outputs,
                                      size_as_tensor=True)
    def _test_model(self, config_path, inference_func):
        model = model_zoo.get(config_path, trained=True)
        image = get_sample_coco_image()

        class Wrapper(nn.ModuleList):  # a wrapper to make the model traceable
            def forward(self, image):
                outputs = inference_func(self[0], image)
                flattened_outputs, schema = flatten_to_tuple(outputs)
                if not hasattr(self, "schema"):
                    self.schema = schema
                return flattened_outputs

            def rebuild(self, flattened_outputs):
                return self.schema(flattened_outputs)

        wrapper = Wrapper([model])
        wrapper.eval()
        with torch.no_grad(), patch_builtin_len():
            small_image = nn.functional.interpolate(image, scale_factor=0.5)
            # trace with a different image, and the trace must still work
            traced_model = torch.jit.trace(wrapper, (small_image,))

            output = inference_func(model, image)
            traced_output = wrapper.rebuild(traced_model(image))
        assert_instances_allclose(output, traced_output, size_as_tensor=True)
Пример #3
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:])
Пример #4
0
    def _test_model(self, config_path, inference_func):
        model = model_zoo.get(config_path, trained=True)
        image = get_sample_coco_image()

        wrapper = TracingAdapter(model, image, inference_func)
        wrapper.eval()
        with torch.no_grad():
            small_image = nn.functional.interpolate(image, scale_factor=0.5)
            # trace with a different image, and the trace must still work
            traced_model = torch.jit.trace(wrapper, (small_image,))

            output = inference_func(model, image)
            traced_output = wrapper.outputs_schema(traced_model(image))
        assert_instances_allclose(output, traced_output, size_as_tensor=True)
    def _test_model(self, config_path, WrapperCls):
        # TODO wrapper should be handled by export API in the future
        model = model_zoo.get(config_path, trained=True)
        image = get_sample_coco_image()

        model = WrapperCls([model])
        model.eval()
        with torch.no_grad(), patch_builtin_len():
            small_image = nn.functional.interpolate(image, scale_factor=0.5)
            # trace with a different image, and the trace must still work
            traced_model = torch.jit.trace(model, (small_image, ))

            output = WrapperCls.convert_output(model(image))
            traced_output = WrapperCls.convert_output(traced_model(image))
        assert_instances_allclose(output, traced_output)
Пример #6
0
    def _test_retinanet_model(self, config_path):
        model = model_zoo.get(config_path, trained=True)
        model.eval()

        fields = {
            "pred_boxes": Boxes,
            "scores": Tensor,
            "pred_classes": Tensor,
        }
        script_model = export_torchscript_with_instances(model, fields)

        img = get_sample_coco_image()
        inputs = [{"image": img}]
        with torch.no_grad():
            instance = model(inputs)[0]["instances"]
            scripted_instance = convert_scripted_instances(script_model(inputs)[0])
            scripted_instance = detector_postprocess(scripted_instance, img.shape[1], img.shape[2])
        assert_instances_allclose(instance, scripted_instance)
Пример #7
0
    def _test_rcnn_model(self, config_path):
        model = model_zoo.get(config_path, trained=True)
        model.eval()

        fields = {
            "proposal_boxes": Boxes,
            "objectness_logits": Tensor,
            "pred_boxes": Boxes,
            "scores": Tensor,
            "pred_classes": Tensor,
            "pred_masks": Tensor,
        }
        script_model = export_torchscript_with_instances(model, fields)

        inputs = [{"image": get_sample_coco_image()}]
        with torch.no_grad():
            instance = model.inference(inputs, do_postprocess=False)[0]
            scripted_instance = script_model.inference(inputs, do_postprocess=False)[0]
        assert_instances_allclose(instance, scripted_instance)
Пример #8
0
    def _test_model(self, config_path, device="cpu"):
        # requires extra dependencies
        from detectron2.export import Caffe2Model, add_export_config, Caffe2Tracer

        cfg = model_zoo.get_config(config_path)
        add_export_config(cfg)
        cfg.MODEL.DEVICE = device
        model = model_zoo.get(config_path, trained=True, device=device)

        inputs = [{"image": get_sample_coco_image()}]
        c2_model = Caffe2Tracer(cfg, model,
                                copy.deepcopy(inputs)).export_caffe2()

        with tempfile.TemporaryDirectory(prefix="detectron2_unittest") as d:
            c2_model.save_protobuf(d)
            c2_model.save_graph(os.path.join(d, "test.svg"),
                                inputs=copy.deepcopy(inputs))
            c2_model = Caffe2Model.load_protobuf(d)
        c2_model(inputs)[0]["instances"]
Пример #9
0
    def _test_model(self, config_path, device="cpu"):
        cfg = model_zoo.get_config(config_path)
        cfg.MODEL.DEVICE = device
        model = model_zoo.get(config_path, trained=True, device=device)

        inputs = [{"image": get_sample_coco_image()}]
        tracer = Caffe2Tracer(cfg, model, copy.deepcopy(inputs))

        with tempfile.TemporaryDirectory(prefix="detectron2_unittest") as d:
            if not os.environ.get("CI"):
                # This requires onnx, which is not yet available on public CI
                c2_model = tracer.export_caffe2()
                c2_model.save_protobuf(d)
                c2_model.save_graph(os.path.join(d, "test.svg"),
                                    inputs=copy.deepcopy(inputs))

                c2_model = Caffe2Model.load_protobuf(d)
                c2_model(inputs)[0]["instances"]

            ts_model = tracer.export_torchscript()
            ts_model.save(os.path.join(d, "model.ts"))
Пример #10
0
    def _test_rcnn_model(self, config_path):
        model = model_zoo.get(config_path, trained=True)
        model.eval()

        fields = {
            "proposal_boxes": Boxes,
            "objectness_logits": Tensor,
            "pred_boxes": Boxes,
            "scores": Tensor,
            "pred_classes": Tensor,
            "pred_masks": Tensor,
        }
        script_model = scripting_with_instances(model, fields)

        # Test that batch inference with different shapes are supported
        image = get_sample_coco_image()
        small_image = nn.functional.interpolate(image, scale_factor=0.5)
        inputs = [{"image": image}, {"image": small_image}]
        with torch.no_grad():
            instance = model.inference(inputs, do_postprocess=False)[0]
            scripted_instance = script_model.inference(inputs, do_postprocess=False)[0]
        assert_instances_allclose(instance, scripted_instance)
Пример #11
0
    def _test_model(self, config_path, inference_func, batch=1):
        model = model_zoo.get(config_path, trained=True)
        image = get_sample_coco_image()
        inputs = tuple(image.clone() for _ in range(batch))

        wrapper = TracingAdapter(model, inputs, inference_func)
        wrapper.eval()
        with torch.no_grad():
            # trace with smaller images, and the trace must still work
            trace_inputs = tuple(
                nn.functional.interpolate(image, scale_factor=random.uniform(0.5, 0.7))
                for _ in range(batch)
            )
            traced_model = torch.jit.trace(wrapper, trace_inputs)

        testing_devices = self._get_device_casting_test_cases(model)
        # save and load back the model in order to show traceback of TorchScript
        with tempfile.TemporaryDirectory(prefix="detectron2_test") as d:
            basename = "model"
            jitfile = f"{d}/{basename}.jit"
            torch.jit.save(traced_model, jitfile)
            traced_model = torch.jit.load(jitfile)

            if any(device and "cuda" in device for device in testing_devices):
                self._check_torchscript_no_hardcoded_device(jitfile, d, "cuda")

        for device in testing_devices:
            print(f"Testing casting to {device} for inference (traced on {model.device}) ...")
            with torch.no_grad():
                outputs = inference_func(copy.deepcopy(model).to(device), *inputs)
                traced_outputs = wrapper.outputs_schema(traced_model.to(device)(*inputs))
            if batch > 1:
                for output, traced_output in zip(outputs, traced_outputs):
                    assert_instances_allclose(output, traced_output, size_as_tensor=True)
            else:
                assert_instances_allclose(outputs, traced_outputs, size_as_tensor=True)
Пример #12
0

if __name__ == "__main__":
    ALL_START = time.time()
    print("Start counting time. ALL_START")
    # mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    setup_logger(name="fvcore")
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    if args.config_file.endswith("yaml"):
        cfg = setup_cfg(args)
        model = VisualizationDemo(cfg)
    elif args.config_file.endswith("py"):
        model = model_zoo.get(args.config_file, trained=True)
        model.cuda()
        model.eval()
    else:
        raise Exception(f"{args.config_file} is not supported.")

    if args.input:
        if len(args.input) == 1:
            args.input = glob.glob(osp.expanduser(args.input[0]))
            assert args.input, "The input path(s) was not found"

        data_dir = osp.join(args.input[0], args.split)
        if args.video_src_names:  # only process the given video sources
            video_src_names = args.video_src_names
        else:
            video_src_names = [
Пример #13
0
from torch.autograd import Variable
import torch
from torch import nn
import numpy as np
import onnx
from onnx_tf.backend import prepare
from detectron2 import model_zoo
import torchvision.transforms as transforms

# Load the trained model from file
my_model = model_zoo.get("COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml",
                         trained=False)
#my_model = torch.load('/home/ubuntu/cs/publaynet/output/model_0021999.pth')

if torch.cuda.is_available():
    my_model.cuda()
else:
    my_model.cpu()

my_model.eval()
'''
for key in my_model['model'].keys():
    #print(my_model['model'].keys())# ['backbone.bottom_up.stem.conv1.weight'].size())
    print(key)
    print(my_model['model'][key].size())
'''

# Export the trained model to ONNX
input_list = []
toTensor = transforms.Compose([transforms.ToTensor()])
#dummy_input = Variable(torch.randn(1, 1, 1)) # nchw one black and white 28 x 28 picture will be the input to the model
Пример #14
0
 def test_get_returns_model(self):
     model = model_zoo.get("Misc/scratch_mask_rcnn_R_50_FPN_3x_gn.yaml",
                           trained=False)
     self.assertIsInstance(model, GeneralizedRCNN)
     self.assertIsInstance(model.backbone, FPN)
Пример #15
0
metadata = MetadataCatalog.get('debris_dataset_train')
dataset_dicts = get_dataset(data_paths[0])
for d in random.sample(dataset_dicts, 10):
    img = cv2.imread(d['file_name'])
    visualizer = Visualizer(img[:, :, ::-1], metadata=metadata, scale=0.3)
    out = visualizer.draw_dataset_dict(d)
    cv2.imshow('img{}'.format(d), out.get_image()[:, :, ::-1])
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# Setup cfg parameters#
# Use Faster-RCNN 101 layer model with ResNet backbone (R101-C4)
cfg = get_cfg()
model_zoo.get(
    'COCO-Detection/faster_rcnn_R_50_C4_3x.yaml', True
)  # True: Lets training initialize from model #faster_rcnn_R_101_C4_3x.yaml
cfg.DATASETS.TRAIN = ('debris_dataset_train', )
cfg.DATASETS.TEST = ('debris_dataset_test', )

cfg.DATALOADER.NUM_WORKERS = 4
#cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url('COCO-Detection/faster_rcnn_R_101_C4_3x.yaml')
cfg.SOLVER.IMS_PER_BATCH = 2  # CUDA out of memory if > 1
cfg.SOLVER.BASE_LR = 0.001

cfg.SOLVER.WARMUP_ITERS = 80000  #(1000)
cfg.SOLVER.MAX_ITER = 100000  # maximum number of training iterations (1500)
cfg.SOLVER.STEPS = (80000, 100000)  # (1000, 1500)
cfg.SOLVER.GAMMA = 0.05
cfg.SOLVER.CHECKPOINT_PERIOD = 25000  # make a checkpoint .weights file every 'x' iterations