Skip to content

Commit d96595e

Browse files
committed
[Add] Initial Commit
0 parents  commit d96595e

22 files changed

+630
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Virtual Environment
2+
.venv/
3+
4+
# Cache
5+
**/__pycache__/**
6+
7+
# VScode
8+
.vscode
9+
10+
# Data
11+
data/*
12+
!data/temp.txt
13+
14+
# Weights File
15+
saved/*
16+
!saved/temp.txt

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# PyTorch Project Template
2+
* 본 Repository는 PyTorch를 통해 프로젝트를 수행할 때 기반이 되는 코드베이스 템플릿입니다.
3+
4+
# Installation
5+
```
6+
# Docker Container Setup
7+
docker pull ubuntu:22.04
8+
docker run -itd --gpus=all --shm-size=16G --name=<container_name> <image_name>
9+
10+
# Ubuntu
11+
apt-get update
12+
apt-get install sudo
13+
sudo apt-get install python3
14+
python3 -m venv .venv
15+
source .venv/bin/activate
16+
```
17+
# Train & Test
18+
```
19+
# Train
20+
python train.py --config=convnet
21+
python train.py --config=convnet2
22+
23+
# Test
24+
python test.py --config=convnet
25+
python test.py --config=convnet2
26+
```
27+
# Performances
28+
```
29+
# ConvNet (20 Epochs)
30+
Accuracy: 0.9540
31+
F1-Score(Macro): 0.9539
32+
Precision(Macro): 0.9572
33+
Recall(Macro): 0.9537
34+
35+
# ConvNet2 (20 Epochs)
36+
Accuracy: 0.9700
37+
F1-Score(Macro): 0.9699
38+
Precision(Macro): 0.9701
39+
Recall(Macro): 0.9701
40+
```

configs/test.convnet.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
expr: "Test ConvNet"
2+
3+
device: cuda:0
4+
parallel: False
5+
6+
data:
7+
dataset: MNIST
8+
root: data/
9+
download: True
10+
mode: test
11+
12+
model:
13+
name: ConvNet
14+
in_channels: 1
15+
layers: [8, 16, 32]
16+
class_num: 10
17+
18+
hyperparameters:
19+
batch_size: 32
20+
21+
save_dir: saved
22+
weights_file_name: convnet_mnist.epochs_100.pth

configs/test.convnet2.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
expr: "Test ConvNet2"
2+
3+
device: cuda:0
4+
parallel: False
5+
6+
data:
7+
dataset: MNIST
8+
root: data/
9+
download: True
10+
mode: test
11+
12+
model:
13+
name: ConvNet2
14+
in_channels: 1
15+
layers: [8, 16, 32]
16+
class_num: 10
17+
18+
hyperparameters:
19+
batch_size: 32
20+
21+
save_dir: saved
22+
weights_file_name: convnet2_mnist.epochs_100.pth

configs/train.convnet.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
expr: "Train ConvNet"
2+
3+
device: cuda:0
4+
parallel: False
5+
6+
data:
7+
dataset: MNIST
8+
root: data/
9+
download: True
10+
mode: train
11+
12+
model:
13+
name: ConvNet
14+
in_channels: 1
15+
layers: [8, 16, 32]
16+
class_num: 10
17+
18+
hyperparameters:
19+
batch_size: 32
20+
optim: AdamW
21+
lr: 0.0001
22+
loss_fn: cross-entropy
23+
epochs: 100
24+
25+
save_dir: saved
26+
save_name: convnet_mnist

configs/train.convnet2.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
expr: "Train ConvNet2"
2+
3+
device: cuda:0
4+
parallel: False
5+
6+
data:
7+
dataset: MNIST
8+
root: data/
9+
download: True
10+
mode: train
11+
12+
model:
13+
name: ConvNet2
14+
in_channels: 1
15+
layers: [8, 16, 32]
16+
class_num: 10
17+
18+
hyperparameters:
19+
batch_size: 32
20+
optim: AdamW
21+
lr: 0.0001
22+
loss_fn: cross-entropy
23+
epochs: 100
24+
25+
save_dir: saved
26+
save_name: convnet2_mnist

data/temp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
should include this directory

datasets/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .mnist_dataset import MNIST_Dataset
2+
3+
def load_dataset(**cfg):
4+
if cfg['dataset'] == 'MNIST':
5+
return MNIST_Dataset(root=cfg['root'],
6+
download=cfg['download'],
7+
mode=cfg['mode'])
8+
9+
else:
10+
raise Exception(f"Dataset: {cfg['dataset']} is not supported.")

datasets/mnist_dataset.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import torch
2+
from torch.utils.data import dataset
3+
from torchvision.datasets import MNIST
4+
import numpy as np
5+
6+
class MNIST_Dataset():
7+
def __init__(self,
8+
root="data/", # make MNIST directory below data/
9+
download=True, # download if there is no data, else pass
10+
mode='train'):
11+
12+
if mode not in ['train', 'test']:
13+
raise AssertionError(f"Mode-related Error: [{mode}] is not supported.")
14+
15+
self.data = MNIST(root=root,
16+
download=download)
17+
18+
self.data=list(self.data)
19+
20+
train_size = 5000
21+
test_size = 500
22+
23+
if mode == 'train':
24+
self.data = self.data[:train_size]
25+
elif mode == 'test':
26+
self.data = self.data[train_size:train_size+test_size]
27+
28+
def __len__(self):
29+
return len(self.data)
30+
31+
def __getitem__(self, idx):
32+
image, label = self.data[idx]
33+
34+
image = np.array(image.getdata()).reshape(28, 28).astype(np.float32)
35+
image /= 255.
36+
image = torch.tensor(image, dtype=torch.float32).unsqueeze(0)
37+
38+
label = torch.tensor(label, dtype=torch.int64)
39+
40+
return image, label

models/ConvNet/block.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from torch import nn
2+
3+
class Block(nn.Module):
4+
def __init__(self,
5+
in_dim:int = 16,
6+
out_dim:int = 32):
7+
8+
super().__init__()
9+
10+
# Convolution layer
11+
self.conv1 = nn.Conv2d(in_dim, out_dim, kernel_size=3, stride=1, padding=1)
12+
self.conv2 = nn.Conv2d(out_dim, out_dim, kernel_size=3, stride=1, padding=1)
13+
14+
# Normalization layer
15+
self.bn1 = nn.BatchNorm2d(out_dim)
16+
self.bn2 = nn.BatchNorm2d(out_dim)
17+
18+
# Activation funciton
19+
self.relu = nn.ReLU()
20+
21+
def forward(self, x):
22+
x = self.bn1(self.conv1(x))
23+
x = self.relu(x)
24+
25+
x = self.bn2(self.conv2(x))
26+
x = self.relu(x)
27+
28+
return x

0 commit comments

Comments
 (0)