import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
# Check Device
if torch.cuda.is_available():
DEVICE = torch.device('cuda') # use GPU
else:
DEVICE = torch.device('cpu') # use CPU
print('Using PyTorch Version: ', torch.__version__, ', Device: ', DEVICE)
# Download MNIST Dataset
training_data = datasets.MNIST(
root="Dataset/MNIST",
train=True,
download=True,
transform=ToTensor()
)
test_data = datasets.MNIST(
root="Dataset/MNIST",
train=False,
download=True,
transform=ToTensor()
)
# Visualize MNIST Dataset
labels_map = {
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
}
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")
plt.imshow(img.squeeze(), cmap="gray")
plt.show()
# split dataset per mini_batch
BATCH_SIZE = 64
train_dataloader = DataLoader(training_data, batch_size=BATCH_SIZE, shuffle=True)
test_dataloader = DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=True)
# check data
for (train_features, train_labels) in train_dataloader:
print('x_train: ', train_features.size(), ', type: ', train_features.type())
print('y_train: ', train_labels.size(), ', type: ', train_labels.type())
img = train_features[0].squeeze()
label = train_labels[0]
plt.imshow(img, cmap='gray')
plt.show()
print(f"Label: {label}")
[PyTorch] CIFAR-10 데이터셋 써보기 (0) | 2023.08.07 |
---|---|
Diffusion Model 정리 (Generative model) (0) | 2023.07.28 |
[PyTorch 시작하기] 1. 텐서(TENSOR) (0) | 2023.07.10 |