Skip to content

Commit

Permalink
Static analysis improvements python (#3263)
Browse files Browse the repository at this point in the history
* Simplify code and remove used vars.

* Simplify expressions and remove used parenthesis.

* Jit fixes.

* Making check more readable.

* fixing styles
  • Loading branch information
datumbox authored Jan 19, 2021
1 parent bf211da commit f7fae49
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions torchvision/datasets/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ def read_sn3_pascalvincent_tensor(path: Union[str, IO], strict: bool = True) ->
magic = get_int(data[0:4])
nd = magic % 256
ty = magic // 256
assert nd >= 1 and nd <= 3
assert ty >= 8 and ty <= 14
assert 1 <= nd <= 3
assert 8 <= ty <= 14
m = SN3_PASCALVINCENT_TYPEMAP[ty]
s = [get_int(data[4 * (i + 1): 4 * (i + 2)]) for i in range(nd)]
parsed = np.frombuffer(data, dtype=m[1], offset=(4 * (nd + 1)))
Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/phototour.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ def read_info_file(data_dir: str, info_file: str) -> torch.Tensor:
"""Return a Tensor containing the list of labels
Read the file and keep only the ID of the 3D point.
"""
labels = []
with open(os.path.join(data_dir, info_file), 'r') as f:
labels = [int(line.split()[0]) for line in f]
return torch.LongTensor(labels)
Expand Down
2 changes: 1 addition & 1 deletion torchvision/io/_video_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def _read_video_timestamps_from_file(filename):
1, # audio_timebase_den
)
_vframes, vframe_pts, vtimebase, vfps, vduration, \
_aframes, aframe_pts, atimebase, asample_rate, aduration = (result)
_aframes, aframe_pts, atimebase, asample_rate, aduration = result
info = _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration)

vframe_pts = vframe_pts.numpy().tolist()
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/backbone_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def resnet_fpn_backbone(
norm_layer=norm_layer)

# select layers that wont be frozen
assert trainable_layers <= 5 and trainable_layers >= 0
assert 0 <= trainable_layers <= 5
layers_to_train = ['layer4', 'layer3', 'layer2', 'layer1', 'conv1'][:trainable_layers]
# freeze layers only if pretrained backbone is used
for name, parameter in backbone.named_parameters():
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/generalized_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ def forward(self, images, targets=None):
if not self._has_warned:
warnings.warn("RCNN always returns a (Losses, Detections) tuple in scripting")
self._has_warned = True
return (losses, detections)
return losses, detections
else:
return self.eager_outputs(losses, detections)
3 changes: 1 addition & 2 deletions torchvision/models/detection/roi_heads.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _onnx_heatmaps_to_keypoints(maps, maps_i, roi_map_width, roi_map_height,

xy_preds_i_0 = x + offset_x_i.to(dtype=torch.float32)
xy_preds_i_1 = y + offset_y_i.to(dtype=torch.float32)
xy_preds_i_2 = torch.ones((xy_preds_i_1.shape), dtype=torch.float32)
xy_preds_i_2 = torch.ones(xy_preds_i_1.shape, dtype=torch.float32)
xy_preds_i = torch.stack([xy_preds_i_0.to(dtype=torch.float32),
xy_preds_i_1.to(dtype=torch.float32),
xy_preds_i_2.to(dtype=torch.float32)], 0)
Expand Down Expand Up @@ -795,7 +795,6 @@ def forward(self,
mask_features = self.mask_head(mask_features)
mask_logits = self.mask_predictor(mask_features)
else:
mask_logits = torch.tensor(0)
raise Exception("Expected mask_roi_pool to be not None")

loss_mask = {}
Expand Down
6 changes: 3 additions & 3 deletions torchvision/models/video/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self,

@staticmethod
def get_downsample_stride(stride):
return (stride, stride, stride)
return stride, stride, stride


class Conv2Plus1D(nn.Sequential):
Expand All @@ -53,7 +53,7 @@ def __init__(self,

@staticmethod
def get_downsample_stride(stride):
return (stride, stride, stride)
return stride, stride, stride


class Conv3DNoTemporal(nn.Conv3d):
Expand All @@ -75,7 +75,7 @@ def __init__(self,

@staticmethod
def get_downsample_stride(stride):
return (1, stride, stride)
return 1, stride, stride


class BasicBlock(nn.Module):
Expand Down
8 changes: 2 additions & 6 deletions torchvision/ops/feature_pyramid_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def get_result_from_inner_blocks(self, x: Tensor, idx: int) -> Tensor:
This is equivalent to self.inner_blocks[idx](x),
but torchscript doesn't support this yet
"""
num_blocks = 0
for m in self.inner_blocks:
num_blocks += 1
num_blocks = len(self.inner_blocks)
if idx < 0:
idx += num_blocks
i = 0
Expand All @@ -117,9 +115,7 @@ def get_result_from_layer_blocks(self, x: Tensor, idx: int) -> Tensor:
This is equivalent to self.layer_blocks[idx](x),
but torchscript doesn't support this yet
"""
num_blocks = 0
for m in self.layer_blocks:
num_blocks += 1
num_blocks = len(self.layer_blocks)
if idx < 0:
idx += num_blocks
i = 0
Expand Down
2 changes: 1 addition & 1 deletion torchvision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _max_value(dtype: torch.dtype) -> float:
max_value = next_value
bits *= 2
else:
return max_value.item()
break
return max_value.item()


Expand Down

0 comments on commit f7fae49

Please sign in to comment.