Esempio n. 1
0
 def __init__(self):
     self.gen = 1000
     self.cfg = get_cfg_defaults()
     self.cfg.model.mode = "test_inference"
     self.dataset = Dataset(None, self.cfg)
     self.auto_anchors = AutoAnchors(self.dataset, self.cfg.model, self.gen)
     self.k_points = torch.ones((12, 2)) * 2.0
     self.wh = torch.ones((1000, 2)) * 2.0
Esempio n. 2
0
    def test_preprocessing(self):
        config = get_cfg_defaults()

        config.model.strides = [8, 16, 32]
        config.model.n_classes = 80
        config.model.image_size = 640
        config.model.input_channels = 3
        config.freeze()

        model = PreprocessTargets(config.model)
        model = inferenceModel(model.half())

        x = torch.zeros((1, 90, 5))
        real_labels = torch.tensor([[45.0000,  0.4795,  0.6416,  0.9556,  0.4466], [45.0000,  0.7365,  0.3104,  0.4989,  0.3573]])
        x[:, :real_labels.shape[0]] = real_labels

        t_indices_boxes_p5, t_indices_boxes_p4, t_indices_boxes_p3 = model(x)
        anchor_ind_p5, yind_p5, xind_p5, t_boxes_p5 = t_indices_boxes_p5
        anchor_ind_p4, yind_p4, xind_p4, t_boxes_p4 = t_indices_boxes_p4
        anchor_ind_p3, yind_p3, xind_p3, t_boxes_p3 = t_indices_boxes_p3

        n_digits = 3
        t_boxes_p4 = torch.round(t_boxes_p4 * 10**n_digits) / (10**n_digits)
        t_boxes_p3 = torch.round(t_boxes_p3 * 10**n_digits) / (10**n_digits)

        size = 5 * len(config.model.anchors.p3width) * config.dataset.max_bbox_per_scale
        assert anchor_ind_p5.shape == torch.Size([size])
        assert yind_p4.shape == torch.Size([size])
        assert xind_p3.shape == torch.Size([size])
        assert t_boxes_p5.shape == torch.Size([size, 5])
        assert xind_p5[xind_p5 != 0.].numel() == 0

        _, index_t_boxes_p4 = torch.sort(t_boxes_p4.sum(axis=-1))
        _, index_t_boxes_p3 = torch.sort(t_boxes_p3.sum(axis=-1))
        t_boxes_p4 = t_boxes_p4[index_t_boxes_p4]
        t_boxes_p3 = t_boxes_p3[index_t_boxes_p3]

        assert torch.all(torch.eq(anchor_ind_p4[anchor_ind_p4 != 0.].sum(), torch.tensor([3, 3, 3, 1, 3, 2, 2, 3, 2, 1, 3, 1]).sum()))
        assert torch.all(torch.eq(yind_p3[yind_p3 != 0.].sum(), torch.tensor([13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 12,  6,  6,  6,  6,  6,  6, 6,  6,  5,  5,  5,  5]).sum()))
        assert t_boxes_p5[t_boxes_p5 != 0.].numel() == 0
        assert torch.all(torch.eq(t_boxes_p4[t_boxes_p4 != 0.].view(-1, 5)[-2:], torch.tensor([[45.0000,  0.1800,  0.6640, 38.2240, 17.8640],
                                                                                               [45.0000, 1.1800,  0.6640, 38.2240, 17.8640]])))
        assert torch.all(torch.eq(t_boxes_p3[t_boxes_p3 != 0.].view(-1, 5)[-2:], torch.tensor([[45.0000,  0.5900, 0.8320, 19.1120,  8.9320],
                                                                                               [45.0000,  0.5900,  0.8320, 19.1120,  8.9320]])))
Esempio n. 3
0
def get_cfg():
    cfg = get_cfg_defaults()

    cfg.model.image_size = 416
    cfg.inference.nms = True
    cfg.inference.class_conf_threshold = 0.001
    cfg.inference.iou_threshold = 0.65
    cfg.inference.nms_max_detections = 10
    cfg.inference.pre_nms_topk_k = 1180
    cfg.ipuopts.batches_per_step = 1
    cfg.model.normalization = "batch"
    cfg.model.activation = "mish"
    cfg.model.half = False
    cfg.model.uint_io = True
    cfg.model.input_channels = 3
    cfg.model.micro_batch_size = 1
    cfg.model.mode = "test"
    cfg.model.ipu = True

    return cfg
Esempio n. 4
0
    def test_nms(self):
        cfg_inference = get_cfg_defaults().inference
        cfg_inference.class_conf_threshold = 0.1
        cfg_inference.iou_threshold = 0.1
        cfg_inference.nms_max_detections = 6
        batch_size = 2
        n_elements = 6
        n_classes = 2

        ipu_model = Nms(cfg_inference, cpu_mode=False)
        cpu_model = Nms(cfg_inference, cpu_mode=True)

        ipu_model = poptorch.inferenceModel(ipu_model)

        scores = torch.tensor([.9,  .1,  .25, .75, .4, .6, .95, .05, .5,  .5,  .3, .7,
                               .9,  .1,  .25, .75, .4, .6, .95, .05, .5,  .5,  .3, .7], dtype=torch.float32).reshape(batch_size, n_elements, n_classes)
        boxes = torch.tensor([0, 0, 1, 1, 0, 0.1, 1, 1.1, 0, -0.1, 1, 0.9, 0, 10, 1, 11, 0, 10.1, 1, 11.1,
                              0, 100, 1, 101, 0, 0, 1, 1, 0, 0.1, 1, 1.1, 0, -0.1, 1, 0.9, 0, 10, 1, 11,
                              0, 10.1, 1, 11.1, 0, 100,  1, 101], dtype=torch.float32).reshape(batch_size, n_elements, 4)

        res_ipu = ipu_model(scores, boxes)

        classes = torch.tensor([[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]], dtype=torch.int32)

        sorted_idx, _ = torch.arange(0, boxes.shape[1], 1).repeat(n_classes).sort()
        boxes = boxes.index_select(1, sorted_idx)

        shifting = 4.
        box_shift = (classes.float() * shifting).unsqueeze(axis=-1).float()
        shifted_box = boxes.view(batch_size, n_elements * n_classes, 4) + box_shift
        res_cpu = cpu_model(scores.view(2, -1), shifted_box, classes)
        box_shift = (res_cpu[3].float() * shifting).unsqueeze(axis=-1).float()

        assert torch.equal(res_ipu[0].long(), res_cpu[0] // n_classes)
        assert torch.equal(res_ipu[1], res_cpu[1])
        assert torch.all(res_ipu[2] - (res_cpu[2] - box_shift) < 2e-07)
        assert torch.equal(res_ipu[3], res_cpu[3])
        assert torch.equal(res_ipu[4], res_cpu[4])
Esempio n. 5
0
def load_config(parser_config):
    """
    load default config and merge user config
    Returns:

    """
    # parser = set_argparse()
    # load config file
    cfg = get_cfg_defaults()
    config_file = Path(__file__).parent.parent.joinpath("config.yaml")

    assert isinstance(
        parser_config, str), "input must single file->id or single folder"

    if parser_config:
        merge_config(cfg, parser_config)

    elif config_file.exists():
        merge_config(cfg, str(config_file))

    else:
        logger.info("use defalt config")
    return cfg
Esempio n. 6
0
    # def cid(self):
    #     string = self.html.xpath("//a[contains(@class,'sample-box')][1]/@href")[
    #         0].replace('https://pics.dmm.co.jp/digital/video/', '')
    #     self.data.cid = re.sub('/.*?.jpg', '', string)


class JavbusBuilder:
    def __init__(self):
        self._instance = None

    def __call__(self, number, cfg):
        if not self._instance:
            self._instance = Javbus(number, cfg)
            for method in dir(self._instance):
                fun = getattr(self._instance, method)
                if getattr(fun, "is_callable", False):
                    fun()
        return self._instance.data


if __name__ == "__main__":
    pass
    # for test
    # pass
    from utils.config import get_cfg_defaults

    #
    cfgs = get_cfg_defaults()
    jav = Javbus("111720_001", cfgs)
    print(jav.data)
    return max_nlabels


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--data',
                        type=str,
                        default='/localdata/datasets/',
                        help='Dataset')
    parser.add_argument('--config',
                        type=str,
                        default='configs/inference-yolov4p5.yaml',
                        help='Configuration of the model')

    opt = parser.parse_args()
    cfg = get_cfg_defaults()
    cfg.merge_from_file(opt.config)
    cfg.model.auto_anchors = True

    image_sizes = [896]

    for image_size in image_sizes:
        anchors = find_optimal_anchors(opt,
                                       cfg,
                                       gen=1000,
                                       image_size=image_size)
        print("Optimal anchors are: ")
        print("p3 width: ", anchors[0].widths)
        print("p3 height: ", anchors[0].heights)
        print("p4 width: ", anchors[1].widths)
        print("p4 height: ", anchors[1].heights)
Esempio n. 8
0
class TestYolov4P5:
    """Tests for end-to-end training and infernece of Yolov4P5."""

    cfg = get_cfg_defaults()
    cfg.model.image_size = 64

    input_tensor = torch.Tensor(np.random.rand(1, 3, 64, 64))

    @pytest.mark.ipus(1)
    @pytest.mark.skip(reason="to enable when loss is implemented")
    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)
        # TODO implement test when loss is implemented.

    @pytest.mark.ipus(1)
    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]])

    @pytest.mark.ipus(1)
    def test_nms(self):
        cfg = get_cfg()
        transformed_images, transformed_labels, image_sizes = get_image_and_label(
            cfg)
        model = prepare_model(cfg)
        y = model(transformed_images)
        seen, nt, mean_precision, mean_recall, m_ap50, m_ap = post_process_and_eval(
            cfg, y, image_sizes, transformed_labels)

        assert seen == 1
        assert nt == 10
        assert mean_precision == 0.4304926702679917
        assert mean_recall == 0.75
        assert m_ap50 == 0.6837500000000001
        assert m_ap == 0.5860250329380764

    @pytest.mark.ipus(1)
    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 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