|
59 | 59 |
|
60 | 60 | # Image datasets and image manipulation |
61 | 61 | import torchvision |
62 | | -import torchvision.transforms as transforms |
| 62 | +from torchvision.transforms import v2 |
63 | 63 |
|
64 | 64 | # Image display |
65 | 65 | import matplotlib.pyplot as plt |
|
68 | 68 | # PyTorch TensorBoard support |
69 | 69 | from torch.utils.tensorboard import SummaryWriter |
70 | 70 |
|
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 | | - |
79 | 71 | ###################################################################### |
80 | 72 | # Showing Images in TensorBoard |
81 | 73 | # ----------------------------- |
|
84 | 76 | # |
85 | 77 |
|
86 | 78 | # 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,))]) |
90 | 83 |
|
91 | 84 | # Store separate training and validations splits in ./data |
92 | 85 | training_set = torchvision.datasets.FashionMNIST('./data', |
@@ -171,7 +164,7 @@ def matplotlib_imshow(img, one_channel=False): |
171 | 164 |
|
172 | 165 | class Net(nn.Module): |
173 | 166 | def __init__(self): |
174 | | - super(Net, self).__init__() |
| 167 | + super().__init__() |
175 | 168 | self.conv1 = nn.Conv2d(1, 6, 5) |
176 | 169 | self.pool = nn.MaxPool2d(2, 2) |
177 | 170 | self.conv2 = nn.Conv2d(6, 16, 5) |
@@ -214,18 +207,18 @@ def forward(self, x): |
214 | 207 |
|
215 | 208 | running_loss += loss.item() |
216 | 209 | if i % 1000 == 999: # Every 1000 mini-batches... |
217 | | - print('Batch {}'.format(i + 1)) |
| 210 | + print(f'Batch {i+1}') |
218 | 211 | # Check against the validation set |
219 | 212 | running_vloss = 0.0 |
220 | 213 |
|
221 | 214 | # 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 |
| 215 | + net.eval() # Switching to evaluation mode, eg. turning off regularisation |
223 | 216 | for j, vdata in enumerate(validation_loader, 0): |
224 | 217 | vinputs, vlabels = vdata |
225 | 218 | voutputs = net(vinputs) |
226 | 219 | vloss = criterion(voutputs, vlabels) |
227 | 220 | running_vloss += vloss.item() |
228 | | - net.train(True) # Switching back to training mode, eg. turning on regularisation |
| 221 | + net.train() # Switching back to training mode, eg. turning on regularisation |
229 | 222 |
|
230 | 223 | avg_loss = running_loss / 1000 |
231 | 224 | avg_vloss = running_vloss / len(validation_loader) |
|
0 commit comments