Skip to content

Fix bug on detection backbones when trainable_layers == 0 #3906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2021

Conversation

datumbox
Copy link
Contributor

Fixes #3905

@datumbox
Copy link
Contributor Author

@vfdev-5 We seem to have a flaky test related to bicubic interpolation:

test_resize - test.test_functional_tensor.Tester
AssertionError: False is not true : 32, InterpolationMode.BICUBIC: err=8.103449821472168, tol=8.0: 
tensor([[ 25.2775, 148.1662, 233.0238,  83.5125, -15.8439, 142.6998, 256.5565,
          52.2321, 106.0589,  81.9318],
        [214.0534,   2.6076, 145.1635, 144.5974,  59.9883,  53.9808, 114.2171,
         111.9034, 218.1618, 231.2049],
        [ 91.5266,  64.6589,  90.6705,  34.7546, 115.5283, 216.9745,  46.6837,
         119.1068,  64.1918, -21.9570],
        [ 73.2423, 113.9135, 148.4705,  44.2233,  67.1236, 179.8834, 147.6364,
           1.9517, 157.2070,  60.4069],
        [187.4002, 228.6713, 145.6868, 189.7539, 176.9514, 176.2497, 116.5859,
          58.8360,  81.7546, 150.9302],
        [164.8305, 198.0723, 184.3424, 156.4343, 103.0237,  55.1967,  92.3567,
         141.3022,  34.0764, 189.4771],
        [104.2335, 101.3937, 124.7642,  86.1514, 121.1024,  18.6053,  18.3503,
         113.3352,  74.6096,  47.2548],
        [107.6250,  37.7976, 230.7211, 130.3768, 132.3119,  62.0180,  34.5880,
         106.9791,  58.9930,   2.2620],
        [ 71.1050, 220.0119,  86.0258,  63.5385, 105.9684,  84.7433, 124.6291,
         243.1278,  58.8375, 186.7885],
        [188.9010,  24.8764,  -4.5545,  23.4752, 195.5183, 221.1723, 247.1341,
         241.4969, 160.7936, 177.2207]])
vs
tensor([[ 34., 141., 225.,  91.,   2., 136., 244.,  61., 105.,  92.],
        [194.,  24., 141., 139.,  63.,  65., 117., 117., 205., 211.],
        [ 94.,  65.,  94.,  40., 107., 198.,  60., 110.,  75.,   0.],
        [ 76., 115., 138.,  55.,  75., 178., 139.,  15., 145.,  71.],
        [187., 217., 154., 178., 170., 171., 116.,  63.,  84., 147.],
        [164., 194., 182., 156., 107.,  62.,  92., 133.,  48., 176.],
        [104., 105., 124.,  95., 117.,  25.,  26., 109.,  76.,  53.],
        [103.,  57., 208., 131., 127.,  64.,  43., 106.,  62.,  17.],
        [ 82., 197.,  90.,  69., 110.,  94., 131., 225.,  77., 174.],
        [178.,  47.,   0.,  28., 184., 212., 232., 233., 163., 181.]])
self = <test_functional_tensor.Tester testMethod=test_resize>

    def test_resize(self):
        script_fn = torch.jit.script(F.resize)
        tensor, pil_img = self._create_data(26, 36, device=self.device)
        batch_tensors = self._create_data_batch(16, 18, num_samples=4, device=self.device)
    
        for dt in [None, torch.float32, torch.float64, torch.float16]:
    
            if dt == torch.float16 and torch.device(self.device).type == "cpu":
                # skip float16 on CPU case
                continue
    
            if dt is not None:
                # This is a trivial cast to float of uint8 data to test all cases
                tensor = tensor.to(dt)
                batch_tensors = batch_tensors.to(dt)
    
            for size in [32, 26, [32, ], [32, 32], (32, 32), [26, 35]]:
                for max_size in (None, 33, 40, 1000):
                    if max_size is not None and isinstance(size, Sequence) and len(size) != 1:
                        continue  # unsupported, see assertRaises below
                    for interpolation in [BILINEAR, BICUBIC, NEAREST]:
                        resized_tensor = F.resize(tensor, size=size, interpolation=interpolation, max_size=max_size)
                        resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation, max_size=max_size)
    
                        assert_equal(
                            resized_tensor.size()[1:],
                            resized_pil_img.size[::-1],
                            msg="{}, {}".format(size, interpolation),
                        )
    
                        if interpolation not in [NEAREST, ]:
                            # We can not check values if mode = NEAREST, as results are different
                            # E.g. resized_tensor  = [[a, a, b, c, d, d, e, ...]]
                            # E.g. resized_pil_img = [[a, b, c, c, d, e, f, ...]]
                            resized_tensor_f = resized_tensor
                            # we need to cast to uint8 to compare with PIL image
                            if resized_tensor_f.dtype == torch.uint8:
                                resized_tensor_f = resized_tensor_f.to(torch.float)
    
                            # Pay attention to high tolerance for MAE
                            self.approxEqualTensorToPIL(
>                               resized_tensor_f, resized_pil_img, tol=8.0, msg="{}, {}".format(size, interpolation)
                            )

test/test_functional_tensor.py:368: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
test/common_utils.py:372: in approxEqualTensorToPIL
    msg="{}: err={}, tol={}: \n{}\nvs\n{}".format(msg, err, tol, tensor[0, :10, :10], pil_tensor[0, :10, :10])
E   AssertionError: False is not true : 32, InterpolationMode.BICUBIC: err=8.103449821472168, tol=8.0: 
E   tensor([[ 25.2775, 148.1662, 233.0238,  83.5125, -15.8439, 142.6998, 256.5565,
E             52.2321, 106.0589,  81.9318],
E           [214.0534,   2.6076, 145.1635, 144.5974,  59.9883,  53.9808, 114.2171,
E            111.9034, 218.1618, 231.2049],
E           [ 91.5266,  64.6589,  90.6705,  34.7546, 115.5283, 216.9745,  46.6837,
E            119.1068,  64.1918, -21.9570],
E           [ 73.2423, 113.9135, 148.4705,  44.2233,  67.1236, 179.8834, 147.6364,
E              1.9517, 157.2070,  60.4069],
E           [187.4002, 228.6713, 145.6868, 189.7539, 176.9514, 176.2497, 116.5859,
E             58.8360,  81.7546, 150.9302],
E           [164.8305, 198.0723, 184.3424, 156.4343, 103.0237,  55.1967,  92.3567,
E            141.3022,  34.0764, 189.4771],
E           [104.2335, 101.3937, 124.7642,  86.1514, 121.1024,  18.6053,  18.3503,
E            113.3352,  74.6096,  47.2548],
E           [107.6250,  37.7976, 230.7211, 130.3768, 132.3119,  62.0180,  34.5880,
E            106.9791,  58.9930,   2.2620],
E           [ 71.1050, 220.0119,  86.0258,  63.5385, 105.9684,  84.7433, 124.6291,
E            243.1278,  58.8375, 186.7885],
E           [188.9010,  24.8764,  -4.5545,  23.4752, 195.5183, 221.1723, 247.1341,
E            241.4969, 160.7936, 177.2207]])
E   vs
E   tensor([[ 34., 141., 225.,  91.,   2., 136., 244.,  61., 105.,  92.],
E           [194.,  24., 141., 139.,  63.,  65., 117., 117., 205., 211.],
E           [ 94.,  65.,  94.,  40., 107., 198.,  60., 110.,  75.,   0.],
E           [ 76., 115., 138.,  55.,  75., 178., 139.,  15., 145.,  71.],
E           [187., 217., 154., 178., 170., 171., 116.,  63.,  84., 147.],
E           [164., 194., 182., 156., 107.,  62.,  92., 133.,  48., 176.],
E           [104., 105., 124.,  95., 117.,  25.,  26., 109.,  76.,  53.],
E           [103.,  57., 208., 131., 127.,  64.,  43., 106.,  62.,  17.],
E           [ 82., 197.,  90.,  69., 110.,  94., 131., 225.,  77., 174.],
E           [178.,  47.,   0.,  28., 184., 212., 232., 233., 163., 181.]])

@vfdev-5
Copy link
Collaborator

vfdev-5 commented May 24, 2021

@datumbox I refactored the test using pytest and could find seed value with MAE larger 8 as here. The case when it is failing looks like to be related to max_size=33. I suppose that odd values are not handled in the same way between PIL and pytorch. If we use max_size=34 I could not find a seed value between 0 to 2000 such that MAE > 8.0.
Let me send a PR with test refactor and using another max_size.

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@datumbox datumbox merged commit c58d5d1 into pytorch:master May 25, 2021
@datumbox datumbox deleted the bug/mobilenet_backbone branch May 25, 2021 11:06
facebook-github-bot pushed a commit that referenced this pull request May 25, 2021
…3906)

Summary:
* Fix a bug when trainable_layers == 0

* Fix same issue on ssd.

Reviewed By: vincentqb, cpuhrsch

Differential Revision: D28679976

fbshipit-source-id: 79061750c7bb6b3d9f069056df15f6d682c3ca56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mobilenet_backbone behavior when trainable_layers = 0
4 participants