Skip to content

Commit bdb3186

Browse files
authored
Merge branch 'main' into fix/3855-deprecated-inplace-ops
2 parents 59c03e5 + 51bdefb commit bdb3186

5 files changed

Lines changed: 59 additions & 57 deletions

File tree

beginner_source/basics/data_tutorial.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@
4848
import torch
4949
from torch.utils.data import Dataset
5050
from torchvision import datasets
51-
from torchvision.transforms import ToTensor
51+
from torchvision.transforms import v2
5252
import matplotlib.pyplot as plt
5353

5454

5555
training_data = datasets.FashionMNIST(
5656
root="data",
5757
train=True,
5858
download=True,
59-
transform=ToTensor()
59+
transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])
6060
)
6161

6262
test_data = datasets.FashionMNIST(
6363
root="data",
6464
train=False,
6565
download=True,
66-
transform=ToTensor()
66+
transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])
6767
)
6868

6969

beginner_source/basics/optimization_tutorial.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@
2828
from torch import nn
2929
from torch.utils.data import DataLoader
3030
from torchvision import datasets
31-
from torchvision.transforms import ToTensor
31+
from torchvision.transforms import v2
3232

3333
training_data = datasets.FashionMNIST(
3434
root="data",
3535
train=True,
3636
download=True,
37-
transform=ToTensor()
37+
transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])
3838
)
3939

4040
test_data = datasets.FashionMNIST(
4141
root="data",
4242
train=False,
4343
download=True,
44-
transform=ToTensor()
44+
transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])
4545
)
4646

4747
train_dataloader = DataLoader(training_data, batch_size=64)

beginner_source/basics/transforms_tutorial.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,47 @@
2323
2424
The FashionMNIST features are in PIL Image format, and the labels are integers.
2525
For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors.
26-
To make these transformations, we use ``ToTensor`` and ``Lambda``.
26+
To make these transformations, we use the ``torchvision.transforms.v2`` API along with ``torch.nn.functional.one_hot``.
2727
"""
2828

2929
import torch
30+
import torch.nn.functional as F
3031
from torchvision import datasets
31-
from torchvision.transforms import ToTensor, Lambda
32+
from torchvision.transforms import v2
3233

3334
ds = datasets.FashionMNIST(
3435
root="data",
3536
train=True,
3637
download=True,
37-
transform=ToTensor(),
38-
target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
38+
transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),
39+
target_transform=v2.Lambda(
40+
lambda y: F.one_hot(torch.tensor(y), num_classes=10).float()
41+
),
3942
)
4043

4144
#################################################
42-
# ToTensor()
45+
# ToImage() and ToDtype()
4346
# -------------------------------
4447
#
45-
# `ToTensor <https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.ToTensor>`_
46-
# converts a PIL image or NumPy ``ndarray`` into a ``FloatTensor``. and scales
47-
# the image's pixel intensity values in the range [0., 1.]
48+
# The ``torchvision.transforms.v2`` API replaces the legacy ``ToTensor`` transform with a two-step pipeline.
49+
# `v2.ToImage <https://pytorch.org/vision/stable/generated/torchvision.transforms.v2.ToImage.html>`_
50+
# converts a PIL image or NumPy ``ndarray`` into a ``torchvision.tv_tensors.Image`` tensor, and
51+
# `v2.ToDtype <https://pytorch.org/vision/stable/generated/torchvision.transforms.v2.ToDtype.html>`_
52+
# with ``scale=True`` casts it to ``float32`` and scales the pixel intensity values to the range [0., 1.].
4853
#
4954

5055
##############################################
5156
# Lambda Transforms
5257
# -------------------------------
5358
#
54-
# Lambda transforms apply any user-defined lambda function. Here, we define a function
55-
# to turn the integer into a one-hot encoded tensor.
56-
# It first creates a zero tensor of size 10 (the number of labels in our dataset) and calls
57-
# `scatter_ <https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_.html>`_ which assigns a
58-
# ``value=1`` on the index as given by the label ``y``.
59+
# Lambda transforms apply any user-defined lambda function. Here, we use
60+
# `torch.nn.functional.one_hot <https://pytorch.org/docs/stable/generated/torch.nn.functional.one_hot.html>`_
61+
# to turn the integer label into a one-hot encoded tensor of size 10 (the number of labels in our dataset),
62+
# then cast it to ``float`` to match the expected dtype.
5963

60-
target_transform = Lambda(lambda y: torch.zeros(
61-
10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))
64+
target_transform = v2.Lambda(
65+
lambda y: F.one_hot(torch.tensor(y), num_classes=10).float()
66+
)
6267

6368
######################################################################
6469
# --------------
@@ -67,4 +72,5 @@
6772
#################################################################
6873
# Further Reading
6974
# ~~~~~~~~~~~~~~~~~
70-
# - `torchvision.transforms API <https://pytorch.org/vision/stable/transforms.html>`_
75+
# - `Getting started with transforms v2 <https://pytorch.org/vision/stable/auto_examples/transforms/plot_transforms_getting_started.html>`_
76+
# - `torchvision.transforms.v2 API <https://pytorch.org/vision/stable/transforms.html#v2-api-reference-recommended>`_

beginner_source/introyt/tensorboardyt_tutorial.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
# Image datasets and image manipulation
6161
import torchvision
62-
import torchvision.transforms as transforms
62+
from torchvision.transforms import v2
6363

6464
# Image display
6565
import matplotlib.pyplot as plt
@@ -68,14 +68,6 @@
6868
# PyTorch TensorBoard support
6969
from torch.utils.tensorboard import SummaryWriter
7070

71-
# In case you are using an environment that has TensorFlow installed,
72-
# such as Google Colab, uncomment the following code to avoid
73-
# a bug with saving embeddings to your TensorBoard directory
74-
75-
# import tensorflow as tf
76-
# import tensorboard as tb
77-
# tf.io.gfile = tb.compat.tensorflow_stub.io.gfile
78-
7971
######################################################################
8072
# Showing Images in TensorBoard
8173
# -----------------------------
@@ -84,9 +76,10 @@
8476
#
8577

8678
# Gather datasets and prepare them for consumption
87-
transform = transforms.Compose(
88-
[transforms.ToTensor(),
89-
transforms.Normalize((0.5,), (0.5,))])
79+
transform = v2.Compose([
80+
v2.ToImage(),
81+
v2.ToDtype(torch.float32, scale=True),
82+
v2.Normalize((0.5,), (0.5,))])
9083

9184
# Store separate training and validations splits in ./data
9285
training_set = torchvision.datasets.FashionMNIST('./data',
@@ -171,7 +164,7 @@ def matplotlib_imshow(img, one_channel=False):
171164

172165
class Net(nn.Module):
173166
def __init__(self):
174-
super(Net, self).__init__()
167+
super().__init__()
175168
self.conv1 = nn.Conv2d(1, 6, 5)
176169
self.pool = nn.MaxPool2d(2, 2)
177170
self.conv2 = nn.Conv2d(6, 16, 5)
@@ -214,18 +207,19 @@ def forward(self, x):
214207

215208
running_loss += loss.item()
216209
if i % 1000 == 999: # Every 1000 mini-batches...
217-
print('Batch {}'.format(i + 1))
210+
print(f'Batch {i + 1}')
218211
# Check against the validation set
219212
running_vloss = 0.0
220213

221214
# In evaluation mode some model specific operations can be omitted eg. dropout layer
222-
net.train(False) # Switching to evaluation mode, eg. turning off regularisation
223-
for j, vdata in enumerate(validation_loader, 0):
224-
vinputs, vlabels = vdata
225-
voutputs = net(vinputs)
226-
vloss = criterion(voutputs, vlabels)
227-
running_vloss += vloss.item()
228-
net.train(True) # Switching back to training mode, eg. turning on regularisation
215+
net.eval() # Switching to evaluation mode, eg. turning off regularisation
216+
with torch.no_grad():
217+
for j, vdata in enumerate(validation_loader, 0):
218+
vinputs, vlabels = vdata
219+
voutputs = net(vinputs)
220+
vloss = criterion(voutputs, vlabels)
221+
running_vloss += vloss.item()
222+
net.train() # Switching back to training mode, eg. turning on regularisation
229223

230224
avg_loss = running_loss / 1000
231225
avg_vloss = running_vloss / len(validation_loader)

beginner_source/introyt/trainingyt.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,26 @@
5757
of data they contain.
5858
5959
For this tutorial, we’ll be using the Fashion-MNIST dataset provided by
60-
TorchVision. We use ``torchvision.transforms.Normalize()`` to
60+
TorchVision. We use ``torchvision.transforms.v2.Normalize()`` to
6161
zero-center and normalize the distribution of the image tile content,
6262
and download both training and validation data splits.
6363
6464
"""
6565

6666
import torch
6767
import torchvision
68-
import torchvision.transforms as transforms
68+
from torchvision.transforms import v2
6969

7070
# PyTorch TensorBoard support
7171
from torch.utils.tensorboard import SummaryWriter
7272
from datetime import datetime
7373

7474

75-
transform = transforms.Compose(
76-
[transforms.ToTensor(),
77-
transforms.Normalize((0.5,), (0.5,))])
75+
transform = v2.Compose([
76+
v2.ToImage(),
77+
v2.ToDtype(torch.float32, scale=True),
78+
v2.Normalize((0.5,), (0.5,))
79+
])
7880

7981
# Create datasets for training & validation, download if necessary
8082
training_set = torchvision.datasets.FashionMNIST('./data', train=True, transform=transform, download=True)
@@ -89,8 +91,8 @@
8991
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot')
9092

9193
# Report split sizes
92-
print('Training set has {} instances'.format(len(training_set)))
93-
print('Validation set has {} instances'.format(len(validation_set)))
94+
print(f'Training set has {len(training_set)} instances')
95+
print(f'Validation set has {len(validation_set)} instances')
9496

9597

9698
######################################################################
@@ -134,7 +136,7 @@ def matplotlib_imshow(img, one_channel=False):
134136
# PyTorch models inherit from torch.nn.Module
135137
class GarmentClassifier(nn.Module):
136138
def __init__(self):
137-
super(GarmentClassifier, self).__init__()
139+
super().__init__()
138140
self.conv1 = nn.Conv2d(1, 6, 5)
139141
self.pool = nn.MaxPool2d(2, 2)
140142
self.conv2 = nn.Conv2d(6, 16, 5)
@@ -176,7 +178,7 @@ def forward(self, x):
176178
print(dummy_labels)
177179

178180
loss = loss_fn(dummy_outputs, dummy_labels)
179-
print('Total loss for this batch: {}'.format(loss.item()))
181+
print(f'Total loss for this batch: {loss.item()}')
180182

181183

182184
#################################################################################
@@ -251,7 +253,7 @@ def train_one_epoch(epoch_index, tb_writer):
251253
running_loss += loss.item()
252254
if i % 1000 == 999:
253255
last_loss = running_loss / 1000 # loss per batch
254-
print(' batch {} loss: {}'.format(i + 1, last_loss))
256+
print(f' batch {i + 1} loss: {last_loss}')
255257
tb_x = epoch_index * len(training_loader) + i + 1
256258
tb_writer.add_scalar('Loss/train', last_loss, tb_x)
257259
running_loss = 0.
@@ -276,15 +278,15 @@ def train_one_epoch(epoch_index, tb_writer):
276278

277279
# Initializing in a separate cell so we can easily add more epochs to the same run
278280
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
279-
writer = SummaryWriter('runs/fashion_trainer_{}'.format(timestamp))
281+
writer = SummaryWriter(f'runs/fashion_trainer_{timestamp}')
280282
epoch_number = 0
281283

282284
EPOCHS = 5
283285

284286
best_vloss = 1_000_000.
285287

286288
for epoch in range(EPOCHS):
287-
print('EPOCH {}:'.format(epoch_number + 1))
289+
print(f'EPOCH {epoch_number + 1}:')
288290

289291
# Make sure gradient tracking is on, and do a pass over the data
290292
model.train(True)
@@ -305,7 +307,7 @@ def train_one_epoch(epoch_index, tb_writer):
305307
running_vloss += vloss
306308

307309
avg_vloss = running_vloss / (i + 1)
308-
print('LOSS train {} valid {}'.format(avg_loss, avg_vloss))
310+
print(f'LOSS train {avg_loss} valid {avg_vloss}')
309311

310312
# Log the running loss averaged per batch
311313
# for both training and validation
@@ -317,7 +319,7 @@ def train_one_epoch(epoch_index, tb_writer):
317319
# Track best performance, and save the model's state
318320
if avg_vloss < best_vloss:
319321
best_vloss = avg_vloss
320-
model_path = 'model_{}_{}'.format(timestamp, epoch_number)
322+
model_path = f'model_{timestamp}_{epoch_number}'
321323
torch.save(model.state_dict(), model_path)
322324

323325
epoch_number += 1

0 commit comments

Comments
 (0)