Skip to content

Commit 93c85bb

Browse files
jdsgomesdatumbox
andauthored
Consolidate repr (#5392)
* Consolidating __repr__ strings Co-authored-by: Vasilis Vryniotis <[email protected]>
1 parent c39c23e commit 93c85bb

File tree

16 files changed

+196
-192
lines changed

16 files changed

+196
-192
lines changed

references/classification/transforms.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ def forward(self, batch: Tensor, target: Tensor) -> Tuple[Tensor, Tensor]:
7272
return batch, target
7373

7474
def __repr__(self) -> str:
75-
s = self.__class__.__name__ + "("
76-
s += "num_classes={num_classes}"
77-
s += ", p={p}"
78-
s += ", alpha={alpha}"
79-
s += ", inplace={inplace}"
80-
s += ")"
81-
return s.format(**self.__dict__)
75+
s = (
76+
f"{self.__class__.__name__}("
77+
f"num_classes={self.num_classes}"
78+
f", p={self.p}"
79+
f", alpha={self.alpha}"
80+
f", inplace={self.inplace}"
81+
f")"
82+
)
83+
return s
8284

8385

8486
class RandomCutmix(torch.nn.Module):
@@ -162,10 +164,12 @@ def forward(self, batch: Tensor, target: Tensor) -> Tuple[Tensor, Tensor]:
162164
return batch, target
163165

164166
def __repr__(self) -> str:
165-
s = self.__class__.__name__ + "("
166-
s += "num_classes={num_classes}"
167-
s += ", p={p}"
168-
s += ", alpha={alpha}"
169-
s += ", inplace={inplace}"
170-
s += ")"
171-
return s.format(**self.__dict__)
167+
s = (
168+
f"{self.__class__.__name__}("
169+
f"num_classes={self.num_classes}"
170+
f", p={self.p}"
171+
f", alpha={self.alpha}"
172+
f", inplace={self.inplace}"
173+
f")"
174+
)
175+
return s

test/test_datasets_download.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def __init__(self, url, md5=None, id=None):
180180
self.md5 = md5
181181
self.id = id or url
182182

183-
def __repr__(self):
183+
def __repr__(self) -> str:
184184
return self.id
185185

186186

torchvision/models/detection/anchor_utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,15 @@ def _grid_default_boxes(
239239
return torch.cat(default_boxes, dim=0)
240240

241241
def __repr__(self) -> str:
242-
s = self.__class__.__name__ + "("
243-
s += "aspect_ratios={aspect_ratios}"
244-
s += ", clip={clip}"
245-
s += ", scales={scales}"
246-
s += ", steps={steps}"
247-
s += ")"
248-
return s.format(**self.__dict__)
242+
s = (
243+
f"{self.__class__.__name__}("
244+
f"aspect_ratios={self.aspect_ratios}"
245+
f", clip={self.clip}"
246+
f", scales={self.scales}"
247+
f", steps={self.steps}"
248+
")"
249+
)
250+
return s
249251

250252
def forward(self, image_list: ImageList, feature_maps: List[Tensor]) -> List[Tensor]:
251253
grid_sizes = [feature_map.shape[-2:] for feature_map in feature_maps]

torchvision/models/detection/transform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def postprocess(
260260
return result
261261

262262
def __repr__(self) -> str:
263-
format_string = self.__class__.__name__ + "("
263+
format_string = f"{self.__class__.__name__}("
264264
_indent = "\n "
265265
format_string += f"{_indent}Normalize(mean={self.image_mean}, std={self.image_std})"
266266
format_string += f"{_indent}Resize(min_size={self.min_size}, max_size={self.max_size}, mode='bilinear')"

torchvision/models/efficientnet.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ def __init__(
6161
self.num_layers = self.adjust_depth(num_layers, depth_mult)
6262

6363
def __repr__(self) -> str:
64-
s = self.__class__.__name__ + "("
65-
s += "expand_ratio={expand_ratio}"
66-
s += ", kernel={kernel}"
67-
s += ", stride={stride}"
68-
s += ", input_channels={input_channels}"
69-
s += ", out_channels={out_channels}"
70-
s += ", num_layers={num_layers}"
71-
s += ")"
72-
return s.format(**self.__dict__)
64+
s = (
65+
f"{self.__class__.__name__}("
66+
f"expand_ratio={self.expand_ratio}"
67+
f", kernel={self.kernel}"
68+
f", stride={self.stride}"
69+
f", input_channels={self.input_channels}"
70+
f", out_channels={self.out_channels}"
71+
f", num_layers={self.num_layers}"
72+
f")"
73+
)
74+
return s
7375

7476
@staticmethod
7577
def adjust_channels(channels: int, width_mult: float, min_value: Optional[int] = None) -> int:

torchvision/ops/deform_conv.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,17 @@ def forward(self, input: Tensor, offset: Tensor, mask: Optional[Tensor] = None)
179179
)
180180

181181
def __repr__(self) -> str:
182-
s = self.__class__.__name__ + "("
183-
s += "{in_channels}"
184-
s += ", {out_channels}"
185-
s += ", kernel_size={kernel_size}"
186-
s += ", stride={stride}"
187-
s += ", padding={padding}" if self.padding != (0, 0) else ""
188-
s += ", dilation={dilation}" if self.dilation != (1, 1) else ""
189-
s += ", groups={groups}" if self.groups != 1 else ""
182+
s = (
183+
f"{self.__class__.__name__}("
184+
f"{self.in_channels}"
185+
f", {self.out_channels}"
186+
f", kernel_size={self.kernel_size}"
187+
f", stride={self.stride}"
188+
)
189+
s += f", padding={self.padding}" if self.padding != (0, 0) else ""
190+
s += f", dilation={self.dilation}" if self.dilation != (1, 1) else ""
191+
s += f", groups={self.groups}" if self.groups != 1 else ""
190192
s += ", bias=False" if self.bias is None else ""
191193
s += ")"
192-
return s.format(**self.__dict__)
194+
195+
return s

torchvision/ops/ps_roi_align.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ def forward(self, input: Tensor, rois: Tensor) -> Tensor:
7878
return ps_roi_align(input, rois, self.output_size, self.spatial_scale, self.sampling_ratio)
7979

8080
def __repr__(self) -> str:
81-
tmpstr = self.__class__.__name__ + "("
82-
tmpstr += "output_size=" + str(self.output_size)
83-
tmpstr += ", spatial_scale=" + str(self.spatial_scale)
84-
tmpstr += ", sampling_ratio=" + str(self.sampling_ratio)
85-
tmpstr += ")"
86-
return tmpstr
81+
s = (
82+
f"{self.__class__.__name__}("
83+
f"output_size={self.output_size}"
84+
f", spatial_scale={self.spatial_scale}"
85+
f", sampling_ratio={self.sampling_ratio}"
86+
f")"
87+
)
88+
return s

torchvision/ops/ps_roi_pool.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,5 @@ def forward(self, input: Tensor, rois: Tensor) -> Tensor:
6464
return ps_roi_pool(input, rois, self.output_size, self.spatial_scale)
6565

6666
def __repr__(self) -> str:
67-
tmpstr = self.__class__.__name__ + "("
68-
tmpstr += "output_size=" + str(self.output_size)
69-
tmpstr += ", spatial_scale=" + str(self.spatial_scale)
70-
tmpstr += ")"
71-
return tmpstr
67+
s = f"{self.__class__.__name__}(output_size={self.output_size}, spatial_scale={self.spatial_scale})"
68+
return s

torchvision/ops/roi_align.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ def forward(self, input: Tensor, rois: Tensor) -> Tensor:
8686
return roi_align(input, rois, self.output_size, self.spatial_scale, self.sampling_ratio, self.aligned)
8787

8888
def __repr__(self) -> str:
89-
tmpstr = self.__class__.__name__ + "("
90-
tmpstr += "output_size=" + str(self.output_size)
91-
tmpstr += ", spatial_scale=" + str(self.spatial_scale)
92-
tmpstr += ", sampling_ratio=" + str(self.sampling_ratio)
93-
tmpstr += ", aligned=" + str(self.aligned)
94-
tmpstr += ")"
95-
return tmpstr
89+
s = (
90+
f"{self.__class__.__name__}("
91+
f"output_size={self.output_size}"
92+
f", spatial_scale={self.spatial_scale}"
93+
f", sampling_ratio={self.sampling_ratio}"
94+
f", aligned={self.aligned}"
95+
f")"
96+
)
97+
return s

torchvision/ops/roi_pool.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,5 @@ def forward(self, input: Tensor, rois: Tensor) -> Tensor:
6666
return roi_pool(input, rois, self.output_size, self.spatial_scale)
6767

6868
def __repr__(self) -> str:
69-
tmpstr = self.__class__.__name__ + "("
70-
tmpstr += "output_size=" + str(self.output_size)
71-
tmpstr += ", spatial_scale=" + str(self.spatial_scale)
72-
tmpstr += ")"
73-
return tmpstr
69+
s = f"{self.__class__.__name__}(output_size={self.output_size}, spatial_scale={self.spatial_scale})"
70+
return s

0 commit comments

Comments
 (0)