You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import torch
from torch import nn
import torchvision
from torchvision import datasets
from torchvision import transforms
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
import torch
from torch import nn
import torchvision
from torchvision import datasets
from torchvision import transforms
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
train_data = datasets.FashionMNIST(
root='data',
download=True,
train=True,
transform=ToTensor(),
target_transform=None
)
test_data = datasets.FashionMNIST(
root='data',
train=False,
download=True,
transform=ToTensor(),
target_transform=None
)
image, label = train_data[0]
print(image.shape)
class_names = train_data.classes
print(class_names)
class_to_idx = train_data.class_to_idx
print(class_to_idx)
print(image.shape)
plt.imshow(image.squeeze(), cmap='gray')
plt.title(class_names[label])
plt.axis(False)
from torch.utils.data import DataLoader
BATCH_SIZE = 32
train_dataloader = DataLoader(dataset=train_data,
batch_size=BATCH_SIZE,
shuffle=True)
test_dataloader = DataLoader(dataset=test_data,
batch_size=BATCH_SIZE,
shuffle=True)
print(f"length of test dataloader: {len(test_dataloader)}")
print(train_dataloader, test_dataloader)
train_features_batch, train_labels_batch = next(iter(train_dataloader))
print(train_features_batch.shape, train_labels_batch.shape)
def accuracy_fn(y_true, y_pred):
correct = torch.eq(y_true, y_pred).sum().item()
acc = (correct / len(y_pred)) * 100
return acc
import requests
from pathlib import Path
if Path('helper_functions.py').is_file():
print("exist")
else:
print("downloading helper functions")
request = requests.get(
"https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/refs/heads/main/helper_functions.py")
with open("helper_functions.py", "wb") as f:
f.write(request.content)
from helper_functions import accuracy_fn
from timeit import default_timer as timer
def print_train_time(start: float,
end: float,
device: torch.device = None):
total_time = end - start
print(f"Train time on {device}: {total_time:.3f} seconds")
return total_time
from tqdm.auto import tqdm
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def eval_model(model: torch.nn.Module,
data_loader: torch.utils.data.DataLoader,
loss_fn: torch.nn.Module,
accuracy_fn,
device=device):
loss, acc = 0, 0
model.eval()
model.to(device)
with torch.inference_mode():
for X, y in tqdm(data_loader):
X, y = X.to(device), y.to(device)
y_pred = model(X)
loss += loss_fn(y_pred, y)
acc += accuracy_fn(y_true=y,
y_pred=y_pred.argmax(dim=1))
def train_step(model: torch.nn.Module,
data_loader: torch.utils.data.DataLoader,
loss_fn: torch.nn.Module,
optimizer: torch.optim.Optimizer,
accuracy_fn,
device: torch.device = device):
train_loss, train_acc = 0, 0
model.train()
for batch, (X, y) in enumerate(data_loader):
X, y = X.to(device), y.to(device)
def test_step(model: torch.nn.Module,
data_loader: torch.utils.data.DataLoader,
loss_fn: torch.nn.Module,
optimizer: torch.optim.Optimizer,
accuracy_fn,
device: torch.device = device):
test_loss, test_acc = 0, 0
model.eval()
with torch.inference_mode():
for X, y_test in data_loader:
X, y_test = X.to(device), y_test.to(device)
test_pred = model(X)
test_loss += loss_fn(test_pred, y_test)
test_acc += accuracy_fn(y_true=y_test,
y_pred=test_pred.argmax(dim=1))
test_loss /= len(data_loader)
test_acc /= len(data_loader)
print(f"Test loss: {test_loss:.4f} | Test accuracy: {test_acc:.4f}\n")
class FashionMNISTModelV2(nn.Module):
def init(self, input_shape: int,
hidden_units: int,
output_shape: int):
super().init()
self.conv_block_1 = nn.Sequential(
nn.Conv2d(in_channels=input_shape,
out_channels=hidden_units,
kernel_size=3,
stride=1,
padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=hidden_units,
out_channels=hidden_units,
kernel_size=3,
padding=1,
stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.conv_block_2 = nn.Sequential(
nn.Conv2d(in_channels=hidden_units,
out_channels=hidden_units,
kernel_size=3,
stride=1,
padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=hidden_units,
out_channels=hidden_units,
stride=1,
kernel_size=3,
padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(in_features=hidden_units * 7 * 7,
out_features=output_shape)
)
torch.manual_seed(42)
torch.cuda.manual_seed(42)
model_2 = FashionMNISTModelV2(input_shape=1,
hidden_units=10,
output_shape=len(class_names)).to(device)
from timeit import default_timer as timer
train_time_start_model_2 = timer()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(params=model_2.parameters(),
lr=0.1)
epochs = 3
for epoch in tqdm(range(epochs)):
print(f"Epoch: {epoch}")
train_step(model=model_2,
data_loader=train_dataloader,
loss_fn=loss_fn,
optimizer=optimizer,
accuracy_fn=accuracy_fn,
device=device)
test_step(model=model_2,
data_loader=test_dataloader,
loss_fn=loss_fn,
optimizer=optimizer,
accuracy_fn=accuracy_fn,
device=device)
train_time_end_model_2 = timer()
total_train_time_model_2 = print_train_time(start=train_time_start_model_2,
end=train_time_end_model_2,
device=device)
print(len(test_dataloader))
def make_predictions(model:torch.nn.Module,
data: list,
davice: torch.device= device):
pred_probs = []
model.to(device)
model.eval()
with torch.inference_mode():
for sample in data:
sample = torch.unsqueeze(sample, dim=0).to(device)
pred_logit = model(sample)
pred_prob = torch.softmax(pred_logit.squeeze(),dim=0)
pred_probs.append(pred_prob.cpu())
return torch.stack(pred_probs)
import random
test_samples = []
test_labels = []
for sample, label in random.sample(list(test_data),k=9):
test_samples.append(sample)
test_labels.append(label)
test_samples[0].shape
plt.imshow(test_samples[0].squeeze(),cmap='gray')
plt.title(class_names[test_labels[0]])
pred_probs = make_predictions(model = model_2,
data = test_samples)
print(pred_probs[:2])
pred_classes = pred_probs.argmax(dim=1)
print(pred_classes)
plt.figure(figsize=(9,9))
nrows = 3
ncols = 3
for i, sample in enumerate(test_samples):
plt.subplot(nrows,ncols,i+1)
plt.imshow(sample.squeeze(),cmap = 'gray')
pred_label= class_names[pred_classes[i]]
truth_label = class_names[test_labels[i]]
title_text = f"Pred: {pred_label} | Truth: {truth_label}"
if pred_label == truth_label:
plt.title(title_text, fontsize=10, c='g')
else:
plt.title(title_text, fontsize=10,c='r')
plt.axis(False);
import mlxtend
mlxtend.version
y_preds = []
model_2.eval()
print(len(test_dataloader))
with torch.inference_mode():
for X,y in tqdm(test_dataloader, desc = 'Making predictions...'):
X,y= X.to(device), y.to(device)
y_logit = model_2(X)
y_pred = torch.softmax(y_logit.squeeze(),dim=1).argmax(dim=1)
print(y_logit.shape)
print(len(y_preds))
y_pred_tensor = torch.cat(y_preds)
print(y_pred_tensor)
print(test_data.targets)
try:
import torchmetrics, mlxtend
print(f"mlxtetnd version: {mlxtend.version}")
assert int(mlxtend.version.split(".")[1]) >= 19, "ml version should be 0.19.0 or higher"
except:
!pip install -q torchmetrics -U mlxtend
import torchmetrics, mlxtend
print(f"mltend version: {mlxtend.version}")
import mlxtend
print(mlxtend.version)
from torchmetrics import ConfusionMatrix
from mlxtend.plotting import plot_confusion_matrix
confmat = ConfusionMatrix(task="multiclass",
num_classes = len(class_names))
confmat_tensor = confmat(preds = y_pred_tensor,
target = test_data.targets)
fig, ax = plot_confusion_matrix(
conf_mat = confmat_tensor.numpy(),
class_names = class_names,
figsize = (10,7)
)
Beta Was this translation helpful? Give feedback.
All reactions