Skip to content

Commit 08435cc

Browse files
Fix tensorboardyt v2 transforms API (pytorch#3866)
Fixes pytorch#3857 ## Description Updated 1. v2 transforms migration 2. net.eval() / net.train() 3. f-string updates 4. stale comment removal <img width="991" height="569" alt="image" src="https://github.com/user-attachments/assets/30f1eadd-db97-42d0-8ec2-cf47c5b07f70" /> ## Checklist <!--- Make sure to add `x` to all items in the following checklist: --> - [ ] The issue that is being fixed is referred in the description (see above "Fixes #ISSUE_NUMBER") - [ ] Only one issue is addressed in this pull request - [ ] Labels from the issue that this PR is fixing are added to this pull request - [ ] No unnecessary issues are included into this pull request. cc @subramen --------- Co-authored-by: sekyondaMeta <127536312+sekyondaMeta@users.noreply.github.com>
1 parent 68ed483 commit 08435cc

2 files changed

Lines changed: 31 additions & 35 deletions

File tree

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)