|
| 1 | +import torch |
| 2 | +import torchvision |
| 3 | +from torch import nn, optim |
| 4 | +from torchvision import datasets,models,transforms |
| 5 | +from torch.utils.data import Dataset,DataLoader |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import numpy as np |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | + |
| 11 | +torch.__version__,torchvision.__version__ |
| 12 | + |
| 13 | + |
| 14 | +# Setup training data |
| 15 | +train_data = datasets.FashionMNIST(root="data/03/",train=True,download=True,transform=transforms.ToTensor()) |
| 16 | +test_data = datasets.FashionMNIST(root="data/03/",train=False,download=True,transform=transforms.ToTensor()) |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +len(train_data),len(test_data) |
| 21 | + |
| 22 | + |
| 23 | +# See the first training example |
| 24 | +image,label = train_data[0] |
| 25 | + |
| 26 | + |
| 27 | +image.shape,label |
| 28 | + |
| 29 | + |
| 30 | +class_names = train_data.classes |
| 31 | + |
| 32 | + |
| 33 | +class_names |
| 34 | + |
| 35 | + |
| 36 | +class_to_idx = train_data.class_to_idx |
| 37 | + |
| 38 | + |
| 39 | +class_to_idx |
| 40 | + |
| 41 | + |
| 42 | +image.shape,label # C,H,W |
| 43 | + |
| 44 | + |
| 45 | +plt.imshow(image.view(28,28,1),cmap="gray") |
| 46 | +plt.title(class_names[label]) |
| 47 | +plt.axis(False); |
| 48 | + |
| 49 | + |
| 50 | +# Plot more images |
| 51 | +# torch.manual_seed(42) |
| 52 | +fig = plt.figure(figsize=(9,9)) |
| 53 | +row,cols = 4,4 |
| 54 | +for i in range(1,row*cols+1): |
| 55 | + random_idx = torch.randint(0,len(train_data),size=[1]).item() |
| 56 | + img,label = train_data[random_idx] |
| 57 | + fig.add_subplot(row,cols,i) |
| 58 | + plt.imshow(img.view(28,28,1),cmap="gray") |
| 59 | + plt.title(class_names[label]) |
| 60 | + plt.axis(False) |
| 61 | + |
| 62 | + |
| 63 | +BATCH_SIZE = 32 |
| 64 | + |
| 65 | +train_dataloader = DataLoader(train_data,batch_size=BATCH_SIZE,shuffle=True) |
| 66 | +test_dataloader = DataLoader(test_data,batch_size=BATCH_SIZE,shuffle=True) |
| 67 | + |
| 68 | + |
| 69 | +train_features_batch,train_labels_batch = next(iter(train_dataloader)) |
| 70 | +train_features_batch.shape,train_labels_batch.shape |
| 71 | + |
| 72 | + |
| 73 | +rdm_idx = torch.randint(0,len(train_features_batch),size = [1]).item() |
| 74 | +img,label = train_features_batch[rdm_idx],train_labels_batch[rdm_idx] |
| 75 | +plt.imshow(img.squeeze(),cmap="gray") |
| 76 | +plt.title(class_names[label]) |
| 77 | +plt.axis(False) |
| 78 | + |
| 79 | + |
| 80 | +# Create a flatten layer |
| 81 | +flatten_model = nn.Flatten() |
| 82 | +x = train_features_batch[0] |
| 83 | +x.shape,flatten_model(x).shape |
| 84 | + |
| 85 | + |
| 86 | +from torch import nn |
| 87 | +class FashionMNISTModelV0(nn.Module): |
| 88 | + def __init__(self,input_shape:int,hidden_units: int, output_shape:int) -> None: |
| 89 | + super().__init__() |
| 90 | + self.layer_stack = nn.Sequential( |
| 91 | + nn.Flatten(), # No learning parameters |
| 92 | + nn.Linear(in_features=input_shape,out_features=hidden_units), |
| 93 | + nn.Linear(in_features=hidden_units,out_features=output_shape) |
| 94 | + ) |
| 95 | + |
| 96 | + def forward(self,X) -> torch.Tensor(): |
| 97 | + return self.layer_stack(X) |
| 98 | + |
| 99 | + |
| 100 | +device = 'cuda' if torch.cuda.is_available() else 'cpu' |
| 101 | + |
| 102 | + |
| 103 | +torch.manual_seed(42) |
| 104 | + |
| 105 | +# Setup model with input parameters |
| 106 | + |
| 107 | +model_0 = FashionMNISTModelV0(input_shape=28*28,hidden_units=2048,output_shape=len(class_names)) |
| 108 | + |
| 109 | + |
| 110 | +optimizer = optim.Adam(model_0.parameters(),lr=0.01) |
| 111 | +criterion = nn.CrossEntropyLoss() |
| 112 | + |
| 113 | + |
| 114 | +dummy_X = torch.rand([1,1,28,28]) |
| 115 | +model_0(dummy_X) |
| 116 | + |
| 117 | + |
1 | 118 |
|
0 commit comments