From b3612d98a8336091082f8ea57ea03ffcb1160196 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 11 May 2022 12:11:09 +0100 Subject: [PATCH 1/6] POC --- docs/source/conf.py | 17 ++++++++++++++--- torchvision/models/resnet.py | 8 ++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 3b12fedfb0e..f4b38075c8b 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -320,7 +320,14 @@ def inject_weight_metadata(app, what, name, obj, options, lines): "The model builder above accepts the following values as the ``weights`` parameter.", f"``{obj.__name__}.DEFAULT`` is equivalent to ``{obj.DEFAULT}``.", ] + + if obj.__doc__ != "An enumeration.": + # We only show the custom enum doc if it was overriden. The default one from Python is "An enumeration" + lines.append("") + lines.append(obj.__doc__) + lines.append("") + for field in obj: lines += [f"**{str(field)}**:", ""] if field == obj.DEFAULT: @@ -335,10 +342,14 @@ def inject_weight_metadata(app, what, name, obj, options, lines): metrics = meta.pop("metrics", {}) meta_with_metrics = dict(meta, **metrics) + meta_with_metrics.pop("categories", None) # We don't want to document these, they can be too long + + custom_docs = meta_with_metrics.pop("_docs", None) # Custom per-Weights docs + if custom_docs is not None: + lines += [custom_docs, ""] + for k, v in meta_with_metrics.items(): - if k == "categories": - continue - elif k == "recipe": + if k == "recipe": v = f"`link <{v}>`__" table.append((str(k), str(v))) table = tabulate(table, tablefmt="rst") diff --git a/torchvision/models/resnet.py b/torchvision/models/resnet.py index bc5d952368e..f7d90a3948c 100644 --- a/torchvision/models/resnet.py +++ b/torchvision/models/resnet.py @@ -344,6 +344,7 @@ class ResNet34_Weights(WeightsEnum): class ResNet50_Weights(WeightsEnum): + """""" IMAGENET1K_V1 = Weights( url="https://download.pytorch.org/models/resnet50-0676ba61.pth", transforms=partial(ImageClassification, crop_size=224), @@ -355,6 +356,10 @@ class ResNet50_Weights(WeightsEnum): "acc@1": 76.130, "acc@5": 92.862, }, + # Putting this here but the _docs key could also just be a field of the Weights dataclass + "_docs": """ + + """ }, ) IMAGENET1K_V2 = Weights( @@ -368,6 +373,9 @@ class ResNet50_Weights(WeightsEnum): "acc@1": 80.858, "acc@5": 95.434, }, + "_docs": """ + + """ }, ) DEFAULT = IMAGENET1K_V2 From 1b047b76d01d8105dc26b7f7dd30dc5ab93651bb Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 11 May 2022 13:34:42 +0100 Subject: [PATCH 2/6] Update torchvision/models/resnet.py Co-authored-by: Vasilis Vryniotis --- torchvision/models/resnet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/models/resnet.py b/torchvision/models/resnet.py index f7d90a3948c..af05e1dd58f 100644 --- a/torchvision/models/resnet.py +++ b/torchvision/models/resnet.py @@ -344,7 +344,7 @@ class ResNet34_Weights(WeightsEnum): class ResNet50_Weights(WeightsEnum): - """""" + """Below you can find the available pre-trained weights for ResNet50:""" IMAGENET1K_V1 = Weights( url="https://download.pytorch.org/models/resnet50-0676ba61.pth", transforms=partial(ImageClassification, crop_size=224), From 3c47de6ce81ab0609319fa1967a0f6a66880347e Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 11 May 2022 13:34:55 +0100 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Vasilis Vryniotis --- torchvision/models/resnet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/models/resnet.py b/torchvision/models/resnet.py index af05e1dd58f..6c7b084395c 100644 --- a/torchvision/models/resnet.py +++ b/torchvision/models/resnet.py @@ -358,7 +358,7 @@ class ResNet50_Weights(WeightsEnum): }, # Putting this here but the _docs key could also just be a field of the Weights dataclass "_docs": """ - + Standard weights using the basic recipe of the paper: """ }, ) @@ -374,7 +374,7 @@ class ResNet50_Weights(WeightsEnum): "acc@5": 95.434, }, "_docs": """ - + Improved weights using TorchVision's `new recipe `_: """ }, ) From c91e2b3537298e50a0fbd8b7e85a0e34f0332fb8 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 11 May 2022 13:41:11 +0100 Subject: [PATCH 4/6] Fix tests --- test/test_extended_models.py | 1 + torchvision/models/resnet.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/test_extended_models.py b/test/test_extended_models.py index 651585cfa7d..a39ca62ca78 100644 --- a/test/test_extended_models.py +++ b/test/test_extended_models.py @@ -90,6 +90,7 @@ def test_schema_meta_validation(model_fn): "num_params", "recipe", "unquantized", + "_docs", } # mandatory fields for each computer vision task classification_fields = {"categories", ("metrics", "acc@1"), ("metrics", "acc@5")} diff --git a/torchvision/models/resnet.py b/torchvision/models/resnet.py index 6c7b084395c..89fd462474e 100644 --- a/torchvision/models/resnet.py +++ b/torchvision/models/resnet.py @@ -356,9 +356,8 @@ class ResNet50_Weights(WeightsEnum): "acc@1": 76.130, "acc@5": 92.862, }, - # Putting this here but the _docs key could also just be a field of the Weights dataclass "_docs": """ - Standard weights using the basic recipe of the paper: + These are standard weights using the basic recipe of the paper. """ }, ) @@ -374,7 +373,8 @@ class ResNet50_Weights(WeightsEnum): "acc@5": 95.434, }, "_docs": """ - Improved weights using TorchVision's `new recipe `_: + These are improved weights, using TorchVision's `new recipe + `_. """ }, ) From 736facd32442e32e55160602b94b63b8f545789a Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 11 May 2022 13:49:20 +0100 Subject: [PATCH 5/6] ufmt --- torchvision/models/resnet.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/torchvision/models/resnet.py b/torchvision/models/resnet.py index 89fd462474e..66f811be16e 100644 --- a/torchvision/models/resnet.py +++ b/torchvision/models/resnet.py @@ -345,6 +345,7 @@ class ResNet34_Weights(WeightsEnum): class ResNet50_Weights(WeightsEnum): """Below you can find the available pre-trained weights for ResNet50:""" + IMAGENET1K_V1 = Weights( url="https://download.pytorch.org/models/resnet50-0676ba61.pth", transforms=partial(ImageClassification, crop_size=224), @@ -358,7 +359,7 @@ class ResNet50_Weights(WeightsEnum): }, "_docs": """ These are standard weights using the basic recipe of the paper. - """ + """, }, ) IMAGENET1K_V2 = Weights( @@ -375,7 +376,7 @@ class ResNet50_Weights(WeightsEnum): "_docs": """ These are improved weights, using TorchVision's `new recipe `_. - """ + """, }, ) DEFAULT = IMAGENET1K_V2 From bacd1ddb992d766fa1dedaa884645991cc63c391 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 11 May 2022 15:02:20 +0100 Subject: [PATCH 6/6] Remove useless docstring --- torchvision/models/resnet.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/torchvision/models/resnet.py b/torchvision/models/resnet.py index 66f811be16e..66d3b9d6370 100644 --- a/torchvision/models/resnet.py +++ b/torchvision/models/resnet.py @@ -344,8 +344,6 @@ class ResNet34_Weights(WeightsEnum): class ResNet50_Weights(WeightsEnum): - """Below you can find the available pre-trained weights for ResNet50:""" - IMAGENET1K_V1 = Weights( url="https://download.pytorch.org/models/resnet50-0676ba61.pth", transforms=partial(ImageClassification, crop_size=224),