Skip to content

Commit 2ef164a

Browse files
datumboxNicolasHugpmeierthommaaAnton Thomma
authored andcommitted
Port Multi-weight support from prototype to main (pytorch#5618)
* Moving basefiles outside of prototype and porting Alexnet, ConvNext, Densenet and EfficientNet. * Porting googlenet * Porting inception * Porting mnasnet * Porting mobilenetv2 * Porting mobilenetv3 * Porting regnet * Porting resnet * Porting shufflenetv2 * Porting squeezenet * Porting vgg * Porting vit * Fix docstrings * Fixing imports * Adding missing import * Fix mobilenet imports * Fix tests * Fix prototype tests * Exclude get_weight from models on test * Fix init files * Porting googlenet * Porting inception * porting mobilenetv2 * porting mobilenetv3 * porting resnet * porting shufflenetv2 * Fix test and linter * Fixing docs. * Porting Detection models (pytorch#5617) * fix inits * fix docs * Port faster_rcnn * Port fcos * Port keypoint_rcnn * Port mask_rcnn * Port retinanet * Port ssd * Port ssdlite * Fix linter * Fixing tests * Fixing tests * Fixing vgg test * Porting Optical Flow, Segmentation, Video models (pytorch#5619) * Porting raft * Porting video resnet * Porting deeplabv3 * Porting fcn and lraspp * Fixing the tests and linter * Porting docs, examples, tutorials and galleries (pytorch#5620) * Fix examples, tutorials and gallery * Update gallery/plot_optical_flow.py Co-authored-by: Nicolas Hug <[email protected]> * Fix import * Revert hardcoded normalization * fix uncommitted changes * Fix bug * Fix more bugs * Making resize optional for segmentation * Fixing preset * Fix mypy * Fixing documentation strings * Fix flake8 * minor refactoring Co-authored-by: Nicolas Hug <[email protected]> * Resolve conflict * Porting model tests (pytorch#5622) * Porting tests * Remove unnecessary variable * Fix linter * Move prototype to extended tests * Fix download models job * Update CI on Multiweight branch to use the new weight download approach (pytorch#5628) * port Pad to prototype transforms (pytorch#5621) * port Pad to prototype transforms * use literal * Bump up LibTorchvision version number for Podspec to release Cocoapods (pytorch#5624) Co-authored-by: Anton Thomma <[email protected]> Co-authored-by: Vasilis Vryniotis <[email protected]> * pre-download model weights in CI docs build (pytorch#5625) * pre-download model weights in CI docs build * move changes into template * change docs image * Regenerated config.yml Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Anton Thomma <[email protected]> Co-authored-by: Anton Thomma <[email protected]> * Porting reference scripts and updating presets (pytorch#5629) * Making _preset.py classes * Remove support of targets on presets. * Rewriting the video preset * Adding tests to check that the bundled transforms are JIT scriptable * Rename all presets from *Eval to *Inference * Minor refactoring * Remove --prototype and --pretrained from reference scripts * remove pretained_backbone refs * Corrections and simplifications * Fixing bug * Fixing linter * Fix flake8 * restore documentation example * minor fixes * fix optical flow missing param * Fixing commands * Adding weights_backbone support in detection and segmentation * Updating the commands for InceptionV3 * Setting `weights_backbone` to its fully BC value (pytorch#5653) * Replace default `weights_backbone=None` with its BC values. * Fixing tests * Fix linter * Update docs. * Update preprocessing on reference scripts. * Change qat/ptq to their full values. * Refactoring preprocessing * Fix video preset * No initialization on VGG if pretrained * Fix warning messages for backbone utils. * Adding star to all preset constructors. * Fix mypy. Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Anton Thomma <[email protected]> Co-authored-by: Anton Thomma <[email protected]>
1 parent 834c8d9 commit 2ef164a

File tree

129 files changed

+4581
-7146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+4581
-7146
lines changed

.circleci/config.yml

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.circleci/config.yml.in

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,19 +366,28 @@ jobs:
366366
resource_class: xlarge
367367
steps:
368368
- checkout
369-
- download_model_weights:
370-
extract_roots: torchvision/prototype/models
371369
- install_torchvision
372370
- install_prototype_dependencies
373371
- pip_install:
374372
args: scipy pycocotools h5py
375373
descr: Install optional dependencies
376-
- run:
377-
name: Enable prototype tests
378-
command: echo 'export PYTORCH_TEST_WITH_PROTOTYPE=1' >> $BASH_ENV
379374
- run_tests_selective:
380375
file_or_dir: test/test_prototype_*.py
381376

377+
unittest_extended:
378+
docker:
379+
- image: circleci/python:3.7
380+
resource_class: xlarge
381+
steps:
382+
- checkout
383+
- download_model_weights
384+
- install_torchvision
385+
- run:
386+
name: Enable extended tests
387+
command: echo 'export PYTORCH_TEST_WITH_EXTENDED=1' >> $BASH_ENV
388+
- run_tests_selective:
389+
file_or_dir: test/test_extended_*.py
390+
382391
binary_linux_wheel:
383392
<<: *binary_common
384393
docker:
@@ -1115,6 +1124,7 @@ workflows:
11151124
- unittest_torchhub
11161125
- unittest_onnx
11171126
- unittest_prototype
1127+
- unittest_extended
11181128
{{ unittest_workflows() }}
11191129

11201130
cmake:

android/test_app/make_assets.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import torch
2-
import torchvision
32
from torch.utils.mobile_optimizer import optimize_for_mobile
3+
from torchvision.models.detection import (
4+
fasterrcnn_mobilenet_v3_large_320_fpn,
5+
FasterRCNN_MobileNet_V3_Large_320_FPN_Weights,
6+
)
47

58
print(torch.__version__)
69

7-
model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(
8-
pretrained=True, box_score_thresh=0.7, rpn_post_nms_top_n_test=100, rpn_score_thresh=0.4, rpn_pre_nms_top_n_test=150
10+
model = fasterrcnn_mobilenet_v3_large_320_fpn(
11+
weights=FasterRCNN_MobileNet_V3_Large_320_FPN_Weights.DEFAULT,
12+
box_score_thresh=0.7,
13+
rpn_post_nms_top_n_test=100,
14+
rpn_score_thresh=0.4,
15+
rpn_pre_nms_top_n_test=150,
916
)
1017

1118
model.eval()

docs/source/models.rst

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -98,58 +98,6 @@ You can construct a model with random weights by calling its constructor:
9898
convnext_large = models.convnext_large()
9999
100100
We provide pre-trained models, using the PyTorch :mod:`torch.utils.model_zoo`.
101-
These can be constructed by passing ``pretrained=True``:
102-
103-
.. code:: python
104-
105-
import torchvision.models as models
106-
resnet18 = models.resnet18(pretrained=True)
107-
alexnet = models.alexnet(pretrained=True)
108-
squeezenet = models.squeezenet1_0(pretrained=True)
109-
vgg16 = models.vgg16(pretrained=True)
110-
densenet = models.densenet161(pretrained=True)
111-
inception = models.inception_v3(pretrained=True)
112-
googlenet = models.googlenet(pretrained=True)
113-
shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
114-
mobilenet_v2 = models.mobilenet_v2(pretrained=True)
115-
mobilenet_v3_large = models.mobilenet_v3_large(pretrained=True)
116-
mobilenet_v3_small = models.mobilenet_v3_small(pretrained=True)
117-
resnext50_32x4d = models.resnext50_32x4d(pretrained=True)
118-
wide_resnet50_2 = models.wide_resnet50_2(pretrained=True)
119-
mnasnet = models.mnasnet1_0(pretrained=True)
120-
efficientnet_b0 = models.efficientnet_b0(pretrained=True)
121-
efficientnet_b1 = models.efficientnet_b1(pretrained=True)
122-
efficientnet_b2 = models.efficientnet_b2(pretrained=True)
123-
efficientnet_b3 = models.efficientnet_b3(pretrained=True)
124-
efficientnet_b4 = models.efficientnet_b4(pretrained=True)
125-
efficientnet_b5 = models.efficientnet_b5(pretrained=True)
126-
efficientnet_b6 = models.efficientnet_b6(pretrained=True)
127-
efficientnet_b7 = models.efficientnet_b7(pretrained=True)
128-
efficientnet_v2_s = models.efficientnet_v2_s(pretrained=True)
129-
efficientnet_v2_m = models.efficientnet_v2_m(pretrained=True)
130-
efficientnet_v2_l = models.efficientnet_v2_l(pretrained=True)
131-
regnet_y_400mf = models.regnet_y_400mf(pretrained=True)
132-
regnet_y_800mf = models.regnet_y_800mf(pretrained=True)
133-
regnet_y_1_6gf = models.regnet_y_1_6gf(pretrained=True)
134-
regnet_y_3_2gf = models.regnet_y_3_2gf(pretrained=True)
135-
regnet_y_8gf = models.regnet_y_8gf(pretrained=True)
136-
regnet_y_16gf = models.regnet_y_16gf(pretrained=True)
137-
regnet_y_32gf = models.regnet_y_32gf(pretrained=True)
138-
regnet_x_400mf = models.regnet_x_400mf(pretrained=True)
139-
regnet_x_800mf = models.regnet_x_800mf(pretrained=True)
140-
regnet_x_1_6gf = models.regnet_x_1_6gf(pretrained=True)
141-
regnet_x_3_2gf = models.regnet_x_3_2gf(pretrained=True)
142-
regnet_x_8gf = models.regnet_x_8gf(pretrained=True)
143-
regnet_x_16gf = models.regnet_x_16gf(pretrainedTrue)
144-
regnet_x_32gf = models.regnet_x_32gf(pretrained=True)
145-
vit_b_16 = models.vit_b_16(pretrained=True)
146-
vit_b_32 = models.vit_b_32(pretrained=True)
147-
vit_l_16 = models.vit_l_16(pretrained=True)
148-
vit_l_32 = models.vit_l_32(pretrained=True)
149-
convnext_tiny = models.convnext_tiny(pretrained=True)
150-
convnext_small = models.convnext_small(pretrained=True)
151-
convnext_base = models.convnext_base(pretrained=True)
152-
convnext_large = models.convnext_large(pretrained=True)
153101

154102
Instancing a pre-trained model will download its weights to a cache directory.
155103
This directory can be set using the `TORCH_HOME` environment variable. See
@@ -525,7 +473,7 @@ Obtaining a pre-trained quantized model can be done with a few lines of code:
525473
.. code:: python
526474
527475
import torchvision.models as models
528-
model = models.quantization.mobilenet_v2(pretrained=True, quantize=True)
476+
model = models.quantization.mobilenet_v2(weights=MobileNet_V2_QuantizedWeights.IMAGENET1K_QNNPACK_V1, quantize=True)
529477
model.eval()
530478
# run the model with quantized inputs and weights
531479
out = model(torch.rand(1, 3, 224, 224))

examples/cpp/hello_world/trace_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
HERE = osp.dirname(osp.abspath(__file__))
77
ASSETS = osp.dirname(osp.dirname(HERE))
88

9-
model = torchvision.models.resnet18(pretrained=False)
9+
model = torchvision.models.resnet18()
1010
model.eval()
1111

1212
traced_model = torch.jit.script(model)

gallery/plot_optical_flow.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import torch
2020
import matplotlib.pyplot as plt
2121
import torchvision.transforms.functional as F
22-
import torchvision.transforms as T
2322

2423

2524
plt.rcParams["savefig.bbox"] = "tight"
@@ -88,24 +87,19 @@ def plot(imgs, **imshow_kwargs):
8887
# reduce the image sizes for the example to run faster. Image dimension must be
8988
# divisible by 8.
9089

90+
from torchvision.models.optical_flow import Raft_Large_Weights
9191

92-
def preprocess(batch):
93-
transforms = T.Compose(
94-
[
95-
T.ConvertImageDtype(torch.float32),
96-
T.Normalize(mean=0.5, std=0.5), # map [0, 1] into [-1, 1]
97-
T.Resize(size=(520, 960)),
98-
]
99-
)
100-
batch = transforms(batch)
101-
return batch
92+
weights = Raft_Large_Weights.DEFAULT
93+
transforms = weights.transforms()
10294

10395

104-
# If you can, run this example on a GPU, it will be a lot faster.
105-
device = "cuda" if torch.cuda.is_available() else "cpu"
96+
def preprocess(img1_batch, img2_batch):
97+
img1_batch = F.resize(img1_batch, size=[520, 960])
98+
img2_batch = F.resize(img2_batch, size=[520, 960])
99+
return transforms(img1_batch, img2_batch)
100+
106101

107-
img1_batch = preprocess(img1_batch).to(device)
108-
img2_batch = preprocess(img2_batch).to(device)
102+
img1_batch, img2_batch = preprocess(img1_batch, img2_batch)
109103

110104
print(f"shape = {img1_batch.shape}, dtype = {img1_batch.dtype}")
111105

@@ -121,7 +115,10 @@ def preprocess(batch):
121115

122116
from torchvision.models.optical_flow import raft_large
123117

124-
model = raft_large(pretrained=True, progress=False).to(device)
118+
# If you can, run this example on a GPU, it will be a lot faster.
119+
device = "cuda" if torch.cuda.is_available() else "cpu"
120+
121+
model = raft_large(weights=Raft_Large_Weights.DEFAULT, progress=False).to(device)
125122
model = model.eval()
126123

127124
list_of_flows = model(img1_batch.to(device), img2_batch.to(device))
@@ -182,10 +179,9 @@ def preprocess(batch):
182179
# from torchvision.io import write_jpeg
183180
# for i, (img1, img2) in enumerate(zip(frames, frames[1:])):
184181
# # Note: it would be faster to predict batches of flows instead of individual flows
185-
# img1 = preprocess(img1[None]).to(device)
186-
# img2 = preprocess(img2[None]).to(device)
182+
# img1, img2 = preprocess(img1, img2)
187183

188-
# list_of_flows = model(img1_batch, img2_batch)
184+
# list_of_flows = model(img1.to(device), img1.to(device))
189185
# predicted_flow = list_of_flows[-1][0]
190186
# flow_img = flow_to_image(predicted_flow).to("cpu")
191187
# output_folder = "/tmp/" # Update this to the folder of your choice

gallery/plot_repurposing_annotations.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,14 @@ def show(imgs):
139139
# Here is demo with a Faster R-CNN model loaded from
140140
# :func:`~torchvision.models.detection.fasterrcnn_resnet50_fpn`
141141

142-
from torchvision.models.detection import fasterrcnn_resnet50_fpn
142+
from torchvision.models.detection import fasterrcnn_resnet50_fpn, FasterRCNN_ResNet50_FPN_Weights
143143

144-
model = fasterrcnn_resnet50_fpn(pretrained=True, progress=False)
144+
weights = FasterRCNN_ResNet50_FPN_Weights.DEFAULT
145+
model = fasterrcnn_resnet50_fpn(weights=weights, progress=False)
145146
print(img.size())
146147

147-
img = F.convert_image_dtype(img, torch.float)
148+
tranforms = weights.transforms()
149+
img = tranforms(img)
148150
target = {}
149151
target["boxes"] = boxes
150152
target["labels"] = labels = torch.ones((masks.size(0),), dtype=torch.int64)

gallery/plot_scripted_tensor_transforms.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,16 @@ def show(imgs):
8585
# Let's define a ``Predictor`` module that transforms the input tensor and then
8686
# applies an ImageNet model on it.
8787

88-
from torchvision.models import resnet18
88+
from torchvision.models import resnet18, ResNet18_Weights
8989

9090

9191
class Predictor(nn.Module):
9292

9393
def __init__(self):
9494
super().__init__()
95-
self.resnet18 = resnet18(pretrained=True, progress=False).eval()
96-
self.transforms = nn.Sequential(
97-
T.Resize([256, ]), # We use single int value inside a list due to torchscript type restrictions
98-
T.CenterCrop(224),
99-
T.ConvertImageDtype(torch.float),
100-
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
101-
)
95+
weights = ResNet18_Weights.DEFAULT
96+
self.resnet18 = resnet18(weights=weights, progress=False).eval()
97+
self.transforms = weights.transforms()
10298

10399
def forward(self, x: torch.Tensor) -> torch.Tensor:
104100
with torch.no_grad():

gallery/plot_visualization_utils.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ def show(imgs):
7373
# :func:`~torchvision.models.detection.ssd300_vgg16`. For more details
7474
# on the output of such models, you may refer to :ref:`instance_seg_output`.
7575

76-
from torchvision.models.detection import fasterrcnn_resnet50_fpn
77-
from torchvision.transforms.functional import convert_image_dtype
76+
from torchvision.models.detection import fasterrcnn_resnet50_fpn, FasterRCNN_ResNet50_FPN_Weights
7877

7978

8079
batch_int = torch.stack([dog1_int, dog2_int])
81-
batch = convert_image_dtype(batch_int, dtype=torch.float)
8280

83-
model = fasterrcnn_resnet50_fpn(pretrained=True, progress=False)
81+
weights = FasterRCNN_ResNet50_FPN_Weights.DEFAULT
82+
transforms = weights.transforms()
83+
84+
batch = transforms(batch_int)
85+
86+
model = fasterrcnn_resnet50_fpn(weights=weights, progress=False)
8487
model = model.eval()
8588

8689
outputs = model(batch)
@@ -120,13 +123,15 @@ def show(imgs):
120123
# images must be normalized before they're passed to a semantic segmentation
121124
# model.
122125

123-
from torchvision.models.segmentation import fcn_resnet50
126+
from torchvision.models.segmentation import fcn_resnet50, FCN_ResNet50_Weights
124127

128+
weights = FCN_ResNet50_Weights.DEFAULT
129+
transforms = weights.transforms(resize_size=None)
125130

126-
model = fcn_resnet50(pretrained=True, progress=False)
131+
model = fcn_resnet50(weights=weights, progress=False)
127132
model = model.eval()
128133

129-
normalized_batch = F.normalize(batch, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
134+
normalized_batch = transforms(batch)
130135
output = model(normalized_batch)['out']
131136
print(output.shape, output.min().item(), output.max().item())
132137

@@ -262,8 +267,14 @@ def show(imgs):
262267
# of them may not have masks, like
263268
# :func:`~torchvision.models.detection.fasterrcnn_resnet50_fpn`.
264269

265-
from torchvision.models.detection import maskrcnn_resnet50_fpn
266-
model = maskrcnn_resnet50_fpn(pretrained=True, progress=False)
270+
from torchvision.models.detection import maskrcnn_resnet50_fpn, MaskRCNN_ResNet50_FPN_Weights
271+
272+
weights = MaskRCNN_ResNet50_FPN_Weights.DEFAULT
273+
transforms = weights.transforms()
274+
275+
batch = transforms(batch_int)
276+
277+
model = maskrcnn_resnet50_fpn(weights=weights, progress=False)
267278
model = model.eval()
268279

269280
output = model(batch)
@@ -378,13 +389,17 @@ def show(imgs):
378389
# Note that the keypoint detection model does not need normalized images.
379390
#
380391

381-
from torchvision.models.detection import keypointrcnn_resnet50_fpn
392+
from torchvision.models.detection import keypointrcnn_resnet50_fpn, KeypointRCNN_ResNet50_FPN_Weights
382393
from torchvision.io import read_image
383394

384395
person_int = read_image(str(Path("assets") / "person1.jpg"))
385-
person_float = convert_image_dtype(person_int, dtype=torch.float)
386396

387-
model = keypointrcnn_resnet50_fpn(pretrained=True, progress=False)
397+
weights = KeypointRCNN_ResNet50_FPN_Weights.DEFAULT
398+
transforms = weights.transforms()
399+
400+
person_float = transforms(person_int)
401+
402+
model = keypointrcnn_resnet50_fpn(weights=weights, progress=False)
388403
model = model.eval()
389404

390405
outputs = model([person_float])

ios/VisionTestApp/make_assets.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import torch
2-
import torchvision
32
from torch.utils.mobile_optimizer import optimize_for_mobile
3+
from torchvision.models.detection import (
4+
fasterrcnn_mobilenet_v3_large_320_fpn,
5+
FasterRCNN_MobileNet_V3_Large_320_FPN_Weights,
6+
)
47

58
print(torch.__version__)
69

7-
model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(
8-
pretrained=True, box_score_thresh=0.7, rpn_post_nms_top_n_test=100, rpn_score_thresh=0.4, rpn_pre_nms_top_n_test=150
10+
model = fasterrcnn_mobilenet_v3_large_320_fpn(
11+
weights=FasterRCNN_MobileNet_V3_Large_320_FPN_Weights.DEFAULT,
12+
box_score_thresh=0.7,
13+
rpn_post_nms_top_n_test=100,
14+
rpn_score_thresh=0.4,
15+
rpn_pre_nms_top_n_test=150,
916
)
1017

1118
model.eval()

0 commit comments

Comments
 (0)