Compare commits

...

2 Commits

Author SHA1 Message Date
fc01163995
feat: train on single GPU 2024-05-12 01:48:39 +08:00
bf905e9e03
feat: dataset 2024-05-12 00:43:50 +08:00
3 changed files with 110 additions and 0 deletions

37
train/dataset.py Normal file
View File

@ -0,0 +1,37 @@
import os
import numpy as np
from torch.utils.data import Dataset, DataLoader
class Cifar10Dataset(Dataset):
def __init__(self, data_dir):
self.imgs = []
self.labels = []
for file in os.listdir(data_dir):
if 'data_batch' in file:
batch = self.unpickle(f'{data_dir}/{file}')
length = len(batch[b'data'])
self.labels += batch[b'labels']
# read image data
values = np.array(batch[b'data']) / 255.0
imgs = np.zeros((length, 3, 32, 32))
for index in range(length):
for channel in range(3):
imgs[index][channel] = values[index][32*32*channel : 32*32*(channel+1)].reshape((32, 32))
self.imgs.append(imgs)
self.imgs = np.concatenate(self.imgs)
print(f"load images : {self.imgs.shape}")
print(f"load labels : {len(self.labels)}")
def unpickle(self, file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
def __getitem__(self, index):
return self.imgs[index], self.labels[index]
def __len__(self):
return len(self.imgs)

34
train/model.py Normal file
View File

@ -0,0 +1,34 @@
from torch import nn
import torch.nn.functional as F
class Network(nn.Module):
def __init__(self):
super().__init__()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding='same')
self.conv2 = nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding='same')
self.conv3 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding='same')
self.conv4 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding='same')
self.fc1 = nn.Linear(2048, 1024)
self.fc2 = nn.Linear(1024, 128)
self.fc3 = nn.Linear(128, 10)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.3)
def forward(self, x):
x = self.relu(self.conv1(x))
x = self.relu(self.conv2(x))
x = self.pool(x)
x = self.dropout(x)
x = self.relu(self.conv3(x))
x = self.relu(self.conv4(x))
x = self.pool(x)
x = self.dropout(x)
x = x.reshape((x.shape[0], -1))
x = self.relu(self.fc1(x))
x = self.dropout(x)
x = self.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x

39
train/train.py Normal file
View File

@ -0,0 +1,39 @@
import torch
from torch import optim
from torch import nn
from dataset import Cifar10Dataset
from model import Network
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Network().to(device)
dataset = Cifar10Dataset('./dataset_dir/cifar-10-batches-py')
loader = DataLoader(dataset, batch_size=32, shuffle=True)
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
for epoch in range(50):
model.train()
train_loss_sum = 0
train_correct_sum = 0
for x, y in loader:
x = x.float()
x, y = x.to(device), y.to(device)
predict = model(x)
loss = criterion(predict, y)
loss.backward()
# evaluate
train_loss_sum += loss.item()
predicted_classes = torch.argmax(predict, dim=1)
train_correct_sum += (predicted_classes == y).sum()
optimizer.step()
optimizer.zero_grad()
print(train_loss_sum / len(loader))
print((train_correct_sum / len(dataset)).item(),'%')
print()