Last active
February 14, 2022 17:02
-
-
Save ganindu7/351906087bd899193c9115c2be8b9187 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
from torchvision.io import read_image | |
from torch.utils.data import Dataset | |
class CustomImageDataset(Dataset): | |
def __init__(self, annotation_file, img_dir, transform=None, target_transform=None): | |
self.img_labels = pd.read_csv(annotation_file) | |
self.img_dir = img_dir | |
self.transform = transform | |
self.target_transform = target_transform | |
def __len__(self): | |
return len(self.img_labels) | |
def __getitem__(self, index): | |
img_path = os.path.join(self.img_dir, self.img_labels.iloc[index, 0]) | |
image = read_image(img_path) | |
label = self.img_labels.iloc[index, 1] | |
if self.transform: | |
image = self.transform(image) | |
if self.target_transform: | |
label = self.target_transform(label) | |
return image, label | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from torchvision import datasets | |
from torchvision.transforms import ToTensor | |
import matplotlib.pyplot as plt | |
from torch.utils.data import DataLoader | |
training_data = datasets.FashionMNIST(root="data", | |
train="True", | |
download=True, | |
transform=ToTensor() | |
) | |
labels_map = { | |
0: "Tshirt", | |
1: "Trouser", | |
2: "Pullover", | |
3: "Dress", | |
4: "Coat", | |
5: "Sandal", | |
6: "Shirt", | |
7: "Sneaker", | |
8: "Bag", | |
9: "Ankle boot", | |
} | |
figure = plt.figure(figsize=(8,8)) | |
train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True) | |
train_features, train_labels = next(iter(train_dataloader)) | |
print(f"Feature batch shape: {train_features.size()}") | |
print(f"Labels batch shape: {train_labels.size()}") | |
img = train_features[0].squeeze() | |
label = int(train_labels[0]) | |
print(f"Label: {labels_map[label]}") | |
plt.title(labels_map[label]) | |
plt.imshow(img, cmap="gray") | |
plt.show() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import torch | |
from torch import nn | |
from torch.utils.data import DataLoader | |
from torchvision import datasets, transforms | |
device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
print(f'Using {device} device') | |
class NeuralNetwork(nn.Module): | |
def __init__(self): | |
super(NeuralNetwork, self).__init__() | |
self.flatten = nn.Flatten() | |
self.linear_relu_stack = nn.Sequential( | |
nn.Linear(28*28, 512), | |
nn.ReLU(), | |
nn.Linear(512, 512), | |
nn.ReLU(), | |
nn.Linear(512, 10), | |
) | |
def forward(self, x): | |
x = self.flatten(x) | |
logits = self.linear_relu_stack(x) | |
return logits | |
model = NeuralNetwork().to(device) | |
print(model) | |
X = torch.rand(1, 28, 28, device=device) | |
logits = model(X) | |
pred_prob = nn.Softmax(dim=1)(logits) | |
y_pred = pred_prob.argmax(1) | |
print(f"Prediction: {y_pred}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torchvision import datasets | |
from torchvision.transforms import ToTensor | |
from quickstart import NeuralNetwork | |
# the process of loading models includes re-creating the model structure and loading the state dictionary into it | |
model = NeuralNetwork() | |
model.load_state_dict(torch.load("model.pth")) | |
model.eval() | |
classes = [ | |
"T-shirt/top", | |
"Trouser", | |
"Pullover", | |
"Dress", | |
"Coat", | |
"Sandal", | |
"Shirt", | |
"Sneaker", | |
"Bag", | |
"Ankle boot", | |
] | |
test_data = datasets.FashionMNIST( | |
root="data", | |
train=False, | |
download=True, | |
transform=ToTensor(), | |
) | |
x, y = test_data[0][0], test_data[0][1] | |
with torch.no_grad(): | |
pred = model(x) | |
predicted, actual = classes[pred[0].argmax(0)], classes[y] | |
print(f"Predicted: {predicted}, Actual: {actual}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torch import nn | |
from torchvision import datasets | |
from torchvision.transforms import ToTensor | |
from torchvision import datasets, transforms | |
device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
print(f'Using {device} device') | |
''' | |
Define a NN | |
''' | |
class NeuralNetwork(nn.Module): | |
def __init__(self): | |
super(NeuralNetwork, self).__init__() | |
self.flatten = nn.Flatten() | |
self.linear_relu_stack = nn.Sequential( | |
nn.Linear(28*28, 512), | |
nn.ReLU(), | |
nn.Linear(512, 512), | |
nn.ReLU(), | |
nn.Linear(512, 10), | |
) | |
def forward(self, x): | |
x = self.flatten(x) | |
logits = self.linear_relu_stack(x) | |
return logits | |
''' | |
acquire some training data | |
''' | |
training_data = datasets.FashionMNIST(root="data", | |
train="True", | |
download=True, | |
transform=ToTensor() | |
) | |
''' | |
get some image tensors from the dataset | |
''' | |
listsize = 3 | |
image_list = [] | |
for i in range(1, listsize + 1): | |
sample_idx = torch.randint(len(training_data), size=(1,)).item() | |
img, *_ = training_data[sample_idx] | |
image_list.append(img) | |
''' | |
instantiate the model and pass an image through it | |
Note: The model is still untrained | |
''' | |
model = NeuralNetwork().to(device) | |
image = image_list[0].to(device) | |
logits = model(image) | |
pred_prob = nn.Softmax(dim=1)(logits) | |
y_pred = pred_prob.argmax(1) | |
print(f"Prediction: {y_pred}") | |
print(f"Model Structure: {model} ") | |
for name, param in model.named_parameters(): | |
print(f"Layer: {name} | Size: {param.size()} | Values : {param[:2]} \n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torch import nn | |
from torchvision import datasets | |
from torchvision.transforms import ToTensor | |
# import matplotlib.pyplot as plt | |
# import numpy as np | |
training_data = datasets.FashionMNIST(root="data", | |
train="True", | |
download=True, | |
transform=ToTensor() | |
) | |
listsize = 3 | |
image_list = [] | |
for i in range(1, listsize + 1): | |
sample_idx = torch.randint(len(training_data), size=(1,)).item() | |
img, label = training_data[sample_idx] | |
image_list.append(img) | |
''' | |
The image pulled from the training data has dimensions [1=channels, 28=dim1(width?), 28=dim2(height?)] | |
the n image stack now has dimensions [n, 1, 28, 28] | |
the following step will squeeze out the dimension with one resulting in a [n=3?, 28, 28] tensor. | |
you can uncomment the two lines below to verify this behavour. | |
''' | |
# intermediate_raw_stack = torch.stack(image_list,0) | |
# print(f"intermediate dimaneions {intermediate_raw_stack.size()}") | |
images = torch.squeeze(torch.stack(image_list,0), 1) # image tensor | |
print(f"Images are now stacked into a tensor of dims: {images.size()}") | |
''' | |
Flattten layer conditions the 2D input by reducing the dimensions | |
''' | |
flatten = nn.Flatten() | |
flat_images = flatten(images) | |
print(f"Now we have a tensor of 1D values (flattened 2D values) representing images: {flat_images.size()}") | |
layer1 = nn.Linear(in_features=28*28, out_features=512) | |
hidden1 = layer1(flat_images) | |
print(f"Size of the hidden linear layer [1] = {hidden1.size()}") | |
print(f"Before ReLU: {hidden1}") | |
hidden1 = nn.ReLU()(hidden1) | |
print(f"After ReLU: {hidden1}") | |
''' | |
sequential net made from the modules above. | |
''' | |
seq_modules = nn.Sequential(flatten, # flattened (from 28x28/2D to 784/1D) feature tensors | |
layer1, # input: flattened features, output 512 units | |
nn.ReLU(), | |
nn.Linear(512, 10) | |
) | |
logits = seq_modules(images) | |
print(f"logits after seqential network = {logits}") | |
softmax = nn.Softmax(dim=1) # 'dim' parameter indicates the dimension along the values must sum to 1 | |
predicted_probabilities = softmax(logits) | |
print(f"predicted probababilities = {predicted_probabilities}") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torch import nn | |
from torch.utils.data import DataLoader | |
from torchvision import datasets | |
from torchvision.transforms import ToTensor, Lambda, Compose | |
import matplotlib.pyplot as plt | |
''' | |
Model creation | |
''' | |
# Model definition | |
class NeuralNetwork(nn.Module): | |
def __init__(self): | |
super(NeuralNetwork, self).__init__() | |
self.flatten = nn.Flatten() | |
self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
self.linear_relu_stack = nn.Sequential( | |
nn.Linear(28 * 28, 512), | |
nn.ReLU(), | |
nn.Linear(512, 512), | |
nn.ReLU(), | |
nn.Linear(512, 10) | |
) | |
self.loss_fn = nn.CrossEntropyLoss() | |
self.optimizer = torch.optim.SGD(self.parameters(), lr=1e-3) | |
def forward(self, x): | |
x = self.flatten(x) | |
logits = self.linear_relu_stack(x) | |
return logits | |
def train(dataloader, model, loss_fn, optimizer): | |
size = len(dataloader.dataset) | |
model.train() | |
for batch, (X, y ) in enumerate(dataloader): | |
X, y = X.to(model.device), y.to(model.device) | |
# compute prediction error | |
pred = model(X) | |
loss = loss_fn(pred, y) | |
# Backprop | |
optimizer.zero_grad() | |
loss.backward() | |
optimizer.step() | |
if batch % 100 == 0: | |
loss, current = loss.item(), batch * len(X) | |
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]") | |
def test(dataloader, model, loss_fn): | |
size = len(dataloader.dataset) | |
num_batches = len(dataloader) | |
model.eval() | |
test_loss, correct = 0, 0 | |
with torch.no_grad(): | |
for X, y in dataloader: | |
X, y = X.to(model.device), y.to(model.device) | |
pred = model(X) | |
test_loss += loss_fn(pred, y).item() | |
correct += (pred.argmax(1) == y).type(torch.float).sum().item() | |
test_loss /= num_batches | |
correct /= size | |
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n") | |
if __name__ == "__main__": | |
# download training data from open datasets | |
training_data = datasets.FashionMNIST( | |
root="data", | |
train=True, | |
download=True, | |
transform=ToTensor(), | |
) | |
# download test data from open datsets | |
test_data = datasets.FashionMNIST( | |
root="data", | |
train=False, | |
download=True, | |
transform=ToTensor(), | |
) | |
batch_size = 64 | |
''' | |
pass the data to a Dataloader, the Dataloader wraps an iterable over the dataset | |
and support automatic batching, random sampling, then loads the data into our training functions | |
Here our batch size is 64 each elemement will trturn a batch size of 64 features and labels. | |
''' | |
train_dataloader = DataLoader(training_data, batch_size=batch_size) | |
test_dataloader = DataLoader(test_data, batch_size=batch_size) | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
model = NeuralNetwork().to(device) | |
epochs = 5 | |
for t in range(epochs): | |
print(f"Epoch {t+1}\n-----------------------") | |
train(train_dataloader, model, model.loss_fn, model.optimizer) | |
test(test_dataloader, model, model.loss_fn) | |
print("Done") | |
torch.save(model.state_dict(), "model.pth") | |
print("saved model") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
# from torch.utils.data import Dataset | |
from torchvision import datasets | |
from torchvision.transforms import ToTensor | |
import matplotlib.pyplot as plt | |
training_data = datasets.FashionMNIST(root="data", | |
train="True", | |
download=True, | |
transform=ToTensor() | |
) | |
labels_map = { | |
0: "Tshirt", | |
1: "Trouser", | |
2: "Pullover", | |
3: "Dress", | |
4: "Coat", | |
5: "Sandal", | |
6: "Shirt", | |
7: "Sneaker", | |
8: "Bag", | |
9: "Ankle boot", | |
} | |
figure = plt.figure(figsize=(8,8)) | |
cols, rows = 3, 3 | |
for i in range(1, cols*rows + 1): | |
sample_idx = torch.randint(len(training_data), size=(1,)).item() | |
img, label = training_data[sample_idx] | |
figure.add_subplot(rows, cols, i) | |
plt.title(labels_map[label]) | |
plt.axis("off") | |
# print(f"image shape before squeeze = {img.shape}") | |
plt.imshow(img.squeeze(), cmap="gray") | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment