コード例 #1
0
    def test_normalize_video(self):
        def samples_from_standard_normal(tensor):
            p_value = stats.kstest(list(tensor.view(-1)), 'norm',
                                   args=(0, 1)).pvalue
            return p_value > 0.0001

        random_state = random.getstate()
        random.seed(42)
        for channels in [1, 3]:
            numFrames = random.randint(4, 128)
            height = random.randint(32, 256)
            width = random.randint(32, 256)
            mean = random.random()
            std = random.random()
            clip = torch.normal(mean,
                                std,
                                size=(channels, numFrames, height, width))
            mean = [clip[c].mean().item() for c in range(channels)]
            std = [clip[c].std().item() for c in range(channels)]
            normalized = transforms.NormalizeVideo(mean, std)(clip)
            self.assertTrue(samples_from_standard_normal(normalized))
        random.setstate(random_state)

        # Checking the optional in-place behaviour
        tensor = torch.rand((3, 128, 16, 16))
        tensor_inplace = transforms.NormalizeVideo((0.5, 0.5, 0.5),
                                                   (0.5, 0.5, 0.5),
                                                   inplace=True)(tensor)
        assert_equal(tensor, tensor_inplace)

        transforms.NormalizeVideo((0.5, 0.5, 0.5), (0.5, 0.5, 0.5),
                                  inplace=True).__repr__()
コード例 #2
0
    def test_write_video_with_audio(self):
        f_name = os.path.join(VIDEO_DIR, "R6llTwEh07w.mp4")
        video_tensor, audio_tensor, info = io.read_video(f_name,
                                                         pts_unit="sec")

        with get_tmp_dir() as tmpdir:
            out_f_name = os.path.join(tmpdir, "testing.mp4")
            io.video.write_video(
                out_f_name,
                video_tensor,
                round(info["video_fps"]),
                video_codec="libx264rgb",
                options={'crf': '0'},
                audio_array=audio_tensor,
                audio_fps=info["audio_fps"],
                audio_codec="aac",
            )

            out_video_tensor, out_audio_tensor, out_info = io.read_video(
                out_f_name, pts_unit="sec")

            assert info["video_fps"] == out_info["video_fps"]
            assert_equal(video_tensor, out_video_tensor)

            audio_stream = av.open(f_name).streams.audio[0]
            out_audio_stream = av.open(out_f_name).streams.audio[0]

            assert info["audio_fps"] == out_info["audio_fps"]
            assert audio_stream.rate == out_audio_stream.rate
            assert pytest.approx(out_audio_stream.frames, rel=0.0,
                                 abs=1) == audio_stream.frames
            assert audio_stream.frame_size == out_audio_stream.frame_size
コード例 #3
0
    def test_write_video_with_audio(self):
        f_name = os.path.join(VIDEO_DIR, "R6llTwEh07w.mp4")
        video_tensor, audio_tensor, info = io.read_video(f_name, pts_unit="sec")

        with get_tmp_dir() as tmpdir:
            out_f_name = os.path.join(tmpdir, "testing.mp4")
            io.video.write_video(
                out_f_name,
                video_tensor,
                round(info["video_fps"]),
                video_codec="libx264rgb",
                options={'crf': '0'},
                audio_array=audio_tensor,
                audio_fps=info["audio_fps"],
                audio_codec="aac",
            )

            out_video_tensor, out_audio_tensor, out_info = io.read_video(
                out_f_name, pts_unit="sec"
            )

            self.assertEqual(info["video_fps"], out_info["video_fps"])
            assert_equal(video_tensor, out_video_tensor)

            audio_stream = av.open(f_name).streams.audio[0]
            out_audio_stream = av.open(out_f_name).streams.audio[0]

            self.assertEqual(info["audio_fps"], out_info["audio_fps"])
            self.assertEqual(audio_stream.rate, out_audio_stream.rate)
            self.assertAlmostEqual(audio_stream.frames, out_audio_stream.frames, delta=1)
            self.assertEqual(audio_stream.frame_size, out_audio_stream.frame_size)
コード例 #4
0
 def test_convert_boxes_to_roi_format(self, box_sequence):
     # Ensure common sequences of tensors yield the same result
     ref_tensor = None
     if ref_tensor is None:
         ref_tensor = box_sequence
     else:
         assert_equal(ref_tensor, ops._utils.convert_boxes_to_roi_format(box_sequence))
コード例 #5
0
def test_encode_jpeg_windows(img_path):
    # This test is *wrong*.
    # It compares a torchvision-encoded jpeg with a PIL-encoded jpeg, but it
    # starts encoding the torchvision version from an image that comes from
    # decode_jpeg, which can yield different results from pil.decode (see
    # test_decode... which uses a high tolerance).
    # Instead, we should start encoding from the exact same decoded image, for a
    # valid comparison. This is done in test_encode_jpeg, but unfortunately
    # these more correct tests fail on windows (probably because of a difference
    # in libjpeg) between torchvision and PIL.
    # FIXME: make the correct tests pass on windows and remove this.
    dirname = os.path.dirname(img_path)
    filename, _ = os.path.splitext(os.path.basename(img_path))
    write_folder = os.path.join(dirname, 'jpeg_write')
    expected_file = os.path.join(
        write_folder, '{0}_pil.jpg'.format(filename))
    img = decode_jpeg(read_file(img_path))

    with open(expected_file, 'rb') as f:
        pil_bytes = f.read()
        pil_bytes = torch.as_tensor(list(pil_bytes), dtype=torch.uint8)
    for src_img in [img, img.contiguous()]:
        # PIL sets jpeg quality to 75 by default
        jpeg_bytes = encode_jpeg(src_img, quality=75)
        assert_equal(jpeg_bytes, pil_bytes)
コード例 #6
0
    def test_read_video_pts_unit_sec(self):
        with temp_video(10, 300, 300, 5, lossless=True) as (f_name, data):
            lv, _, info = io.read_video(f_name, pts_unit='sec')

            assert_equal(data, lv)
            self.assertEqual(info["video_fps"], 5)
            self.assertEqual(info, {"video_fps": 5})
コード例 #7
0
    def test_distributed_sampler_and_uniform_clip_sampler(self):
        with get_list_of_videos(num_videos=3, sizes=[25, 25,
                                                     25]) as video_list:
            video_clips = VideoClips(video_list, 5, 5)
            clip_sampler = UniformClipSampler(video_clips, 3)

            distributed_sampler_rank0 = DistributedSampler(
                clip_sampler,
                num_replicas=2,
                rank=0,
                group_size=3,
            )
            indices = torch.tensor(list(iter(distributed_sampler_rank0)))
            self.assertEqual(len(distributed_sampler_rank0), 6)
            assert_equal(indices, torch.tensor([0, 2, 4, 10, 12, 14]))

            distributed_sampler_rank1 = DistributedSampler(
                clip_sampler,
                num_replicas=2,
                rank=1,
                group_size=3,
            )
            indices = torch.tensor(list(iter(distributed_sampler_rank1)))
            self.assertEqual(len(distributed_sampler_rank1), 6)
            assert_equal(indices, torch.tensor([5, 7, 9, 0, 2, 4]))
コード例 #8
0
    def test_read_partial_video_pts_unit_sec(self, start, offset):
        with temp_video(10, 300, 300, 5, lossless=True) as (f_name, data):
            pts, _ = io.read_video_timestamps(f_name, pts_unit='sec')

            lv, _, _ = io.read_video(f_name,
                                     pts[start],
                                     pts[start + offset - 1],
                                     pts_unit='sec')
            s_data = data[start:(start + offset)]
            assert len(lv) == offset
            assert_equal(s_data, lv)

            with av.open(f_name) as container:
                stream = container.streams[0]
                lv, _, _ = io.read_video(f_name,
                                         int(pts[4] *
                                             (1.0 / stream.time_base) + 1) *
                                         stream.time_base,
                                         pts[7],
                                         pts_unit='sec')
            if get_video_backend() == "pyav":
                # for "video_reader" backend, we don't decode the closest early frame
                # when the given start pts is not matching any frame pts
                assert len(lv) == 4
                assert_equal(data[4:8], lv)
コード例 #9
0
def _test_class_op(method, device, meth_kwargs=None, test_exact_match=True, **match_kwargs):
    # TODO: change the name: it's not a method, it's a class.
    meth_kwargs = meth_kwargs or {}

    # test for class interface
    f = method(**meth_kwargs)
    scripted_fn = torch.jit.script(f)

    tensor, pil_img = _create_data(26, 34, device=device)
    # set seed to reproduce the same transformation for tensor and PIL image
    torch.manual_seed(12)
    transformed_tensor = f(tensor)
    torch.manual_seed(12)
    transformed_pil_img = f(pil_img)
    if test_exact_match:
        _assert_equal_tensor_to_pil(transformed_tensor, transformed_pil_img, **match_kwargs)
    else:
        _assert_approx_equal_tensor_to_pil(transformed_tensor.float(), transformed_pil_img, **match_kwargs)

    torch.manual_seed(12)
    transformed_tensor_script = scripted_fn(tensor)
    assert_equal(transformed_tensor, transformed_tensor_script)

    batch_tensors = _create_data_batch(height=23, width=34, channels=3, num_samples=4, device=device)
    _test_transform_vs_scripted_on_batch(f, scripted_fn, batch_tensors)

    with get_tmp_dir() as tmp_dir:
        scripted_fn.save(os.path.join(tmp_dir, f"t_{method.__name__}.pt"))
コード例 #10
0
    def test_compose(self):
        tensor, _ = _create_data(26, 34, device=self.device)
        tensor = tensor.to(dtype=torch.float32) / 255.0

        transforms = T.Compose([
            T.CenterCrop(10),
            T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        ])
        s_transforms = torch.nn.Sequential(*transforms.transforms)

        scripted_fn = torch.jit.script(s_transforms)
        torch.manual_seed(12)
        transformed_tensor = transforms(tensor)
        torch.manual_seed(12)
        transformed_tensor_script = scripted_fn(tensor)
        assert_equal(transformed_tensor,
                     transformed_tensor_script,
                     msg="{}".format(transforms))

        t = T.Compose([
            lambda x: x,
        ])
        with self.assertRaisesRegex(
                RuntimeError, r"Could not get name of python class object"):
            torch.jit.script(t)
コード例 #11
0
def test_x_crop(fn, method, out_length, size, device):
    meth_kwargs = fn_kwargs = {'size': size}
    scripted_fn = torch.jit.script(fn)

    tensor, pil_img = _create_data(height=20, width=20, device=device)
    transformed_t_list = fn(tensor, **fn_kwargs)
    transformed_p_list = fn(pil_img, **fn_kwargs)
    assert len(transformed_t_list) == len(transformed_p_list)
    assert len(transformed_t_list) == out_length
    for transformed_tensor, transformed_pil_img in zip(transformed_t_list, transformed_p_list):
        _assert_equal_tensor_to_pil(transformed_tensor, transformed_pil_img)

    transformed_t_list_script = scripted_fn(tensor.detach().clone(), **fn_kwargs)
    assert len(transformed_t_list) == len(transformed_t_list_script)
    assert len(transformed_t_list_script) == out_length
    for transformed_tensor, transformed_tensor_script in zip(transformed_t_list, transformed_t_list_script):
        assert_equal(transformed_tensor, transformed_tensor_script)

    # test for class interface
    fn = method(**meth_kwargs)
    scripted_fn = torch.jit.script(fn)
    output = scripted_fn(tensor)
    assert len(output) == len(transformed_t_list_script)

    # test on batch of tensors
    batch_tensors = _create_data_batch(height=23, width=34, channels=3, num_samples=4, device=device)
    torch.manual_seed(12)
    transformed_batch_list = fn(batch_tensors)

    for i in range(len(batch_tensors)):
        img_tensor = batch_tensors[i, ...]
        torch.manual_seed(12)
        transformed_img_list = fn(img_tensor)
        for transformed_img, transformed_batch in zip(transformed_img_list, transformed_batch_list):
            assert_equal(transformed_img, transformed_batch[i, ...])
コード例 #12
0
    def test_anchor_generator(self):
        images = torch.randn(2, 3, 15, 15)
        features = self.get_features(images)
        image_shapes = [i.shape[-2:] for i in images]
        images = ImageList(images, image_shapes)

        model = self._init_test_anchor_generator()
        model.eval()
        anchors = model(images, features)

        # Estimate the number of target anchors
        grid_sizes = [f.shape[-2:] for f in features]
        num_anchors_estimated = 0
        for sizes, num_anchors_per_loc in zip(grid_sizes, model.num_anchors_per_location()):
            num_anchors_estimated += sizes[0] * sizes[1] * num_anchors_per_loc

        anchors_output = torch.tensor([[-5., -5., 5., 5.],
                                       [0., -5., 10., 5.],
                                       [5., -5., 15., 5.],
                                       [-5., 0., 5., 10.],
                                       [0., 0., 10., 10.],
                                       [5., 0., 15., 10.],
                                       [-5., 5., 5., 15.],
                                       [0., 5., 10., 15.],
                                       [5., 5., 15., 15.]])

        self.assertEqual(num_anchors_estimated, 9)
        self.assertEqual(len(anchors), 2)
        self.assertEqual(tuple(anchors[0].shape), (9, 4))
        self.assertEqual(tuple(anchors[1].shape), (9, 4))
        assert_equal(anchors[0], anchors_output)
        assert_equal(anchors[1], anchors_output)
コード例 #13
0
    def test_random_apply(self):
        tensor, _ = _create_data(26, 34, device=self.device)
        tensor = tensor.to(dtype=torch.float32) / 255.0

        transforms = T.RandomApply([
            T.RandomHorizontalFlip(),
            T.ColorJitter(),
        ],
                                   p=0.4)
        s_transforms = T.RandomApply(torch.nn.ModuleList([
            T.RandomHorizontalFlip(),
            T.ColorJitter(),
        ]),
                                     p=0.4)

        scripted_fn = torch.jit.script(s_transforms)
        torch.manual_seed(12)
        transformed_tensor = transforms(tensor)
        torch.manual_seed(12)
        transformed_tensor_script = scripted_fn(tensor)
        assert_equal(transformed_tensor,
                     transformed_tensor_script,
                     msg="{}".format(transforms))

        if torch.device(self.device).type == "cpu":
            # Can't check this twice, otherwise
            # "Can't redefine method: forward on class: __torch__.torchvision.transforms.transforms.RandomApply"
            transforms = T.RandomApply([
                T.ColorJitter(),
            ], p=0.3)
            with self.assertRaisesRegex(
                    RuntimeError,
                    r"Module 'RandomApply' has no attribute 'transforms'"):
                torch.jit.script(transforms)
    def test_forward_negative_sample_ssd(self):
        model = torchvision.models.detection.ssd300_vgg16(
            num_classes=2, pretrained_backbone=False)

        images, targets = self._make_empty_sample()
        loss_dict = model(images, targets)

        assert_equal(loss_dict["bbox_regression"], torch.tensor(0.))
コード例 #15
0
def _assert_equal_tensor_to_pil(tensor, pil_image, msg=None):
    np_pil_image = np.array(pil_image)
    if np_pil_image.ndim == 2:
        np_pil_image = np_pil_image[:, :, None]
    pil_tensor = torch.as_tensor(np_pil_image.transpose((2, 0, 1)))
    if msg is None:
        msg = "tensor:\n{} \ndid not equal PIL tensor:\n{}".format(tensor, pil_tensor)
    assert_equal(tensor.cpu(), pil_tensor, check_stride=False, msg=msg)
コード例 #16
0
 def test_uniform_clip_sampler_insufficient_clips(self):
     with get_list_of_videos(num_videos=3, sizes=[10, 25,
                                                  25]) as video_list:
         video_clips = VideoClips(video_list, 5, 5)
         sampler = UniformClipSampler(video_clips, 3)
         self.assertEqual(len(sampler), 3 * 3)
         indices = torch.tensor(list(iter(sampler)))
         assert_equal(indices, torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11]))
    def test_forward_negative_sample_retinanet(self):
        model = torchvision.models.detection.retinanet_resnet50_fpn(
            num_classes=2, min_size=100, max_size=100, pretrained_backbone=False)

        images, targets = self._make_empty_sample()
        loss_dict = model(images, targets)

        assert_equal(loss_dict["bbox_regression"], torch.tensor(0.))
コード例 #18
0
def test_save_image_single_pixel_file_object():
    with tempfile.NamedTemporaryFile(suffix='.png') as f:
        t = torch.rand(1, 3, 1, 1)
        utils.save_image(t, f.name)
        img_orig = Image.open(f.name)
        fp = BytesIO()
        utils.save_image(t, fp, format='png')
        img_bytes = Image.open(fp)
        assert_equal(F.to_tensor(img_orig), F.to_tensor(img_bytes), msg='Image not stored in file object')
 def test_transform_copy_targets(self):
     transform = GeneralizedRCNNTransform(300, 500, torch.zeros(3),
                                          torch.ones(3))
     image = [torch.rand(3, 200, 300), torch.rand(3, 200, 200)]
     targets = [{'boxes': torch.rand(3, 4)}, {'boxes': torch.rand(2, 4)}]
     targets_copy = copy.deepcopy(targets)
     out = transform(image, targets)  # noqa: F841
     assert_equal(targets[0]['boxes'], targets_copy[0]['boxes'])
     assert_equal(targets[1]['boxes'], targets_copy[1]['boxes'])
    def test_forward_negative_sample_frcnn(self, name):
        model = torchvision.models.detection.__dict__[name](
            num_classes=2, min_size=100, max_size=100)

        images, targets = self._make_empty_sample()
        loss_dict = model(images, targets)

        assert_equal(loss_dict["loss_box_reg"], torch.tensor(0.))
        assert_equal(loss_dict["loss_rpn_box_reg"], torch.tensor(0.))
コード例 #21
0
def test_read_1_bit_png_consistency(shape, mode):
    with get_tmp_dir() as root:
        image_path = os.path.join(root, f'test_{shape}.png')
        pixels = np.random.rand(*shape) > 0.5
        img = Image.fromarray(pixels)
        img.save(image_path)
        img1 = read_image(image_path, mode)
        img2 = read_image(image_path, mode)
        assert_equal(img1, img2)
コード例 #22
0
def test_read_1_bit_png(shape):
    with get_tmp_dir() as root:
        image_path = os.path.join(root, f'test_{shape}.png')
        pixels = np.random.rand(*shape) > 0.5
        img = Image.fromarray(pixels)
        img.save(image_path)
        img1 = read_image(image_path)
        img2 = normalize_dimensions(
            torch.as_tensor(pixels * 255, dtype=torch.uint8))
        assert_equal(img1, img2, check_stride=False)
コード例 #23
0
 def _test_transform_vs_scripted(self,
                                 transform,
                                 s_transform,
                                 tensor,
                                 msg=None):
     torch.manual_seed(12)
     out1 = transform(tensor)
     torch.manual_seed(12)
     out2 = s_transform(tensor)
     assert_equal(out1, out2, msg=msg)
コード例 #24
0
    def test_nms_cuda_float16(self):
        boxes = torch.tensor([[285.3538, 185.5758, 1193.5110, 851.4551],
                              [285.1472, 188.7374, 1192.4984, 851.0669],
                              [279.2440, 197.9812, 1189.4746, 849.2019]]).cuda()
        scores = torch.tensor([0.6370, 0.7569, 0.3966]).cuda()

        iou_thres = 0.2
        keep32 = ops.nms(boxes, scores, iou_thres)
        keep16 = ops.nms(boxes.to(torch.float16), scores.to(torch.float16), iou_thres)
        assert_equal(keep32, keep16)
コード例 #25
0
def test_encode_png(img_path):
    pil_image = Image.open(img_path)
    img_pil = torch.from_numpy(np.array(pil_image))
    img_pil = img_pil.permute(2, 0, 1)
    png_buf = encode_png(img_pil, compression_level=6)

    rec_img = Image.open(io.BytesIO(bytes(png_buf.tolist())))
    rec_img = torch.from_numpy(np.array(rec_img))
    rec_img = rec_img.permute(2, 0, 1)

    assert_equal(img_pil, rec_img)
コード例 #26
0
def test_read_file_non_ascii():
    with get_tmp_dir() as d:
        fname, content = '日本語(Japanese).bin', b'TorchVision\211\n'
        fpath = os.path.join(d, fname)
        with open(fpath, 'wb') as f:
            f.write(content)

        data = read_file(fpath)
        expected = torch.tensor(list(content), dtype=torch.uint8)
        os.unlink(fpath)
        assert_equal(data, expected)
コード例 #27
0
 def test_random_clip_sampler(self):
     with get_list_of_videos(num_videos=3, sizes=[25, 25,
                                                  25]) as video_list:
         video_clips = VideoClips(video_list, 5, 5)
         sampler = RandomClipSampler(video_clips, 3)
         self.assertEqual(len(sampler), 3 * 3)
         indices = torch.tensor(list(iter(sampler)))
         videos = torch.div(indices, 5, rounding_mode='floor')
         v_idxs, count = torch.unique(videos, return_counts=True)
         assert_equal(v_idxs, torch.tensor([0, 1, 2]))
         assert_equal(count, torch.tensor([3, 3, 3]))
コード例 #28
0
def _test_fn_on_batch(batch_tensors, fn, scripted_fn_atol=1e-8, **fn_kwargs):
    transformed_batch = fn(batch_tensors, **fn_kwargs)
    for i in range(len(batch_tensors)):
        img_tensor = batch_tensors[i, ...]
        transformed_img = fn(img_tensor, **fn_kwargs)
        assert_equal(transformed_img, transformed_batch[i, ...])

    if scripted_fn_atol >= 0:
        scripted_fn = torch.jit.script(fn)
        # scriptable function test
        s_transformed_batch = scripted_fn(batch_tensors, **fn_kwargs)
        torch.testing.assert_close(transformed_batch, s_transformed_batch, rtol=1e-5, atol=scripted_fn_atol)
コード例 #29
0
def _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors, msg=None):
    torch.manual_seed(12)
    transformed_batch = transform(batch_tensors)

    for i in range(len(batch_tensors)):
        img_tensor = batch_tensors[i, ...]
        torch.manual_seed(12)
        transformed_img = transform(img_tensor)
        assert_equal(transformed_img, transformed_batch[i, ...], msg=msg)

    torch.manual_seed(12)
    s_transformed_batch = s_transform(batch_tensors)
    assert_equal(transformed_batch, s_transformed_batch, msg=msg)
コード例 #30
0
def test_write_png(img_path):
    with get_tmp_dir() as d:
        pil_image = Image.open(img_path)
        img_pil = torch.from_numpy(np.array(pil_image))
        img_pil = img_pil.permute(2, 0, 1)

        filename, _ = os.path.splitext(os.path.basename(img_path))
        torch_png = os.path.join(d, '{0}_torch.png'.format(filename))
        write_png(img_pil, torch_png, compression_level=6)
        saved_image = torch.from_numpy(np.array(Image.open(torch_png)))
        saved_image = saved_image.permute(2, 0, 1)

        assert_equal(img_pil, saved_image)