Skip to content

Commit f601db2

Browse files
authored
Merge pull request #6 from dIronmanb/develop
chapter02
2 parents 5a30b4b + ee5d45b commit f601db2

File tree

18 files changed

+464
-0
lines changed

18 files changed

+464
-0
lines changed
1.77 KB
Binary file not shown.
603 Bytes
Binary file not shown.
4.16 KB
Binary file not shown.
599 Bytes
Binary file not shown.

Chapter02/config.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# config 파일에 대하여
2+
# config 파일에는
3+
# 딥러닝 모델
4+
# 손실 함수
5+
# 옵티마이저
6+
# 하이퍼파라미터
7+
# 에폭
8+
# 등 모든 것들이 담겨져 있다.
9+
# 뭔가 모델을 바꾸거나 에폭을 바꾸고 싶을 때는 여기서 해당 이름에 대응하는
10+
# 값만 조정하면 된다.
11+
12+
# 단, 모델, 손실 함수, 옵티마이저의 경우
13+
# 내가 원하는 것의 이름으로 변경 해당 모델, 손실함수, 옵티마이저 파일에
14+
# 정의되어 있어야 한다.
15+
16+
---
17+
use_cuda: true
18+
epoch: 100
19+
batch_size: 64
20+
learning_rate: 0.001
21+
dataset_name: "CIFAR10"
22+
23+
# train_dataset
24+
# test_dataset
25+
# 은 CIFAR10 Dataloader에서만 작업할 것이므로 pass
26+
27+
num_workers: 2
28+
dataset_shuffle: True
29+
30+
model: "CNN_5"
31+
model_layer: 3 # [3, 5, 9, 12]
32+
loss_function: "CrossEntropyLoss"
33+
optimizer: "Adam"
34+
momentum: 0.9
35+
weight_decay : 0.01
36+
metrics: "accuracy_score"
37+
38+
39+
# momentum이랑 weight_decay 정의하기
40+
41+
42+
43+

Chapter02/data_loaders.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pickle
2+
import numpy as np
3+
from torch.utils.data import TensorDataset, DataLoader
4+
import torch
5+
6+
def unpickle(file):
7+
with open(file, "rb") as fo:
8+
dict = pickle.load(fo, encoding = "bytes")
9+
return dict
10+
11+
def pickle_to_images_and_labels(root):
12+
# Get dataset
13+
data = unpickle(root)
14+
# normalize
15+
data_images = data[b'data'] / 255
16+
# reshape data >> -1 * 3 * 32 * 32
17+
# num of data, colour, width, height
18+
data_images = data_images.reshape(-1, 3, 32, 32).astype("float32")
19+
data_labels = data[b'labels']
20+
return data_images, data_labels
21+
22+
def data_load(config):
23+
# preprocessing data
24+
train_images_and_labels = []
25+
for i in range(5):
26+
train_images_and_labels.append(pickle_to_images_and_labels("/data/CNN_model/dataset/cifar-10-batches-py/data_batch_" + str(i + 1)))
27+
28+
train_images = np.concatenate([i[0] for i in train_images_and_labels], axis = 0)
29+
train_labels = np.concatenate([i[1] for i in train_images_and_labels], axis = 0)
30+
31+
test_images, test_labels = pickle_to_images_and_labels("/data/CNN_model/dataset/cifar-10-batches-py/test_batch")
32+
test_images = np.concatenate([test_images], axis = 0)
33+
test_labels = np.concatenate([test_labels], axis = 0)
34+
35+
36+
# Convert numpy to tensor
37+
train_images_tensor = torch.tensor(train_images)
38+
train_labels_tensor = torch.tensor(train_labels)
39+
train_tensor = TensorDataset(train_images_tensor, train_labels_tensor)
40+
train_loader = DataLoader(train_tensor,
41+
batch_size = config['batch_size'],
42+
num_workers=config['num_workers'],
43+
shuffle = config['dataset_shuffle']
44+
)
45+
46+
test_images_tensor = torch.tensor(test_images)
47+
48+
test_loader = (test_images_tensor, test_labels)
49+
return train_loader, test_loader
50+
51+
52+

Chapter02/metric.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score
2+
3+
4+
def get_metrics(test, predicted, config):
5+
name = config['metrics']
6+
if name == "accuracy_score":
7+
return accuracy_score(test, predicted)
8+
elif name == "recall_score":
9+
return recall_score(test, predicted)
10+
elif name == "precision_score":
11+
return precision_score(test, predicted)
12+
elif name == "f1_score":
13+
return f1_score(test, predicted)
14+
else:
15+
print("There is no metrics in metrics_name")

Chapter02/models.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import torch.nn as nn
2+
import torch.nn.functional as F
3+
4+
def get_cnn_model(name):
5+
if name == 'CNN_3':
6+
return CNN_3()
7+
elif name == 'CNN_5':
8+
return CNN_5()
9+
elif name == 'CNN_9':
10+
return CNN_9()
11+
elif name == 'CNN_12':
12+
return CNN_12()
13+
else:
14+
print("There is no name in models")
15+
16+
# class CNN_3(nn.Module):
17+
# def __init__(self):
18+
# super(CNN_3, self).__init__()
19+
# # Output = (Input - Kernel_size + 2*Padding_size) / Stride + 1
20+
# self.conv1 = nn.Conv2d(in_channels = 3, out_channels = 8, kernel_size = 3, padding = 1)
21+
# self.conv2 = nn.Conv2d(in_channels = 8, out_channels = 8, kernel_size = 3, padding = 1)
22+
# self.conv3 = nn.Conv2d(in_channels = 8, out_channels = 8, kernel_size = 3, padding = 1)
23+
24+
# # Output = (Input - kernel_size) / stride + 1
25+
# self.pool = nn.MaxPool2d(kernel_size = 2, stride = 2)
26+
27+
# self.fc1 = nn.Linear(4 * 4 * 8, 64)
28+
# self.fc2 = nn.Linear(64, 32)
29+
# self.fc3 = nn.Linear(32, 10)
30+
31+
32+
33+
# def forward(self,x):
34+
# x = self.conv1(x) # 32 * 32 * 8
35+
# x = self.pool(x) # 16 * 16 * 8
36+
# x = F.relu(x)
37+
38+
# x = self.conv2(x) # 16 * 16 * 8
39+
# x = self.pool(x) # 8 * 8 * 8
40+
# x = F.relu(x)
41+
42+
# x = self.conv2(x) # 8 * 8 * 8
43+
# x = self.pool(x) # 4 * 4 * 8
44+
# x = F.relu(x)
45+
46+
47+
# x = x.view(-1, 4 * 4 * 8) # flatten
48+
# x = self.fc1(x)
49+
# x = F.relu(x)
50+
# x = self.fc2(x)
51+
# x = F.relu(x)
52+
# x = self.fc3(x)
53+
# x = F.log_softmax(x)
54+
# return x
55+
56+
class CNN_5(nn.Module):
57+
def __init__(self):
58+
super(CNN_5, self).__init__()
59+
# Output = (Input - Kernel_size + 2*Padding_size) / Stride + 1
60+
self.conv_in = nn.Conv2d(in_channels = 3, out_channels = 8, kernel_size = 3, padding = 1)
61+
self.conv_hidden = nn.Conv2d(in_channels = 8, out_channels = 8, kernel_size = 3, padding = 1)
62+
self.conv_out = nn.Conv2d(in_channels = 8, out_channels = 16, kernel_size = 3, padding = 1)
63+
64+
# Output = (Input - kernel_size) / stride + 1
65+
self.pool = nn.MaxPool2d(kernel_size = 2, stride = 2)
66+
67+
self.fc1 = nn.Linear(8 * 8 * 16, 64)
68+
self.fc2 = nn.Linear(64, 32)
69+
self.fc3 = nn.Linear(32, 10)
70+
71+
72+
73+
def forward(self,x):
74+
x = self.conv_in(x) # 32 * 32 * 8
75+
x = self.pool(x) # 16 * 16 * 8
76+
x = F.relu(x)
77+
78+
x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
79+
x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
80+
x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
81+
82+
x = self.conv_out(x) # 16 * 16 * 16
83+
x = self.pool(x) # 8 * 8 * 16
84+
x = F.relu(x)
85+
86+
87+
x = x.view(-1, 8 * 8 * 16) # flatten
88+
x = self.fc1(x)
89+
x = F.relu(x)
90+
x = self.fc2(x)
91+
x = F.relu(x)
92+
x = self.fc3(x)
93+
x = F.log_softmax(x)
94+
return x
95+
96+
class CNN_9(nn.Module):
97+
def __init__(self):
98+
super(CNN_9, self).__init__()
99+
# Output = (Input - Kernel_size + 2*Padding_size) / Stride + 1
100+
self.conv_in = nn.Conv2d(in_channels = 3, out_channels = 8, kernel_size = 3, padding = 1)
101+
self.conv_hidden = nn.Conv2d(in_channels = 8, out_channels = 8, kernel_size = 3, padding = 1)
102+
self.conv_out = nn.Conv2d(in_channels = 8, out_channels = 16, kernel_size = 3, padding = 1)
103+
104+
# Output = (Input - kernel_size) / stride + 1
105+
self.pool = nn.MaxPool2d(kernel_size = 2, stride = 2)
106+
107+
self.fc1 = nn.Linear(8 * 8 * 16, 64)
108+
self.fc2 = nn.Linear(64, 32)
109+
self.fc3 = nn.Linear(32, 10)
110+
111+
112+
113+
def forward(self,x):
114+
x = self.conv_in(x) # 32 * 32 * 8
115+
x = self.pool(x) # 16 * 16 * 8
116+
x = F.relu(x)
117+
118+
for _ in range(7):
119+
x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
120+
121+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
122+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
123+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
124+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
125+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
126+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
127+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
128+
129+
x = self.conv_out(x) # 16 * 16 * 16
130+
x = self.pool(x) # 8 * 8 * 16
131+
x = F.relu(x)
132+
133+
134+
x = x.view(-1, 8 * 8 * 16) # flatten
135+
x = self.fc1(x)
136+
x = F.relu(x)
137+
x = self.fc2(x)
138+
x = F.relu(x)
139+
x = self.fc3(x)
140+
x = F.log_softmax(x)
141+
return x
142+
143+
class CNN_12(nn.Module):
144+
145+
def __init__(self):
146+
super(CNN_12, self).__init__()
147+
# Output = (Input - Kernel_size + 2*Padding_size) / Stride + 1
148+
self.conv_in = nn.Conv2d(in_channels = 3, out_channels = 8, kernel_size = 3, padding = 1)
149+
self.conv_hidden = nn.Conv2d(in_channels = 8, out_channels = 8, kernel_size = 3, padding = 1)
150+
self.conv_out = nn.Conv2d(in_channels = 8, out_channels = 16, kernel_size = 3, padding = 1)
151+
152+
# Output = (Input - kernel_size) / stride + 1
153+
self.pool = nn.MaxPool2d(kernel_size = 2, stride = 2)
154+
155+
self.fc1 = nn.Linear(8 * 8 * 16, 64)
156+
self.fc2 = nn.Linear(64, 32)
157+
self.fc3 = nn.Linear(32, 10)
158+
159+
160+
161+
def forward(self,x):
162+
x = self.conv_in(x) # 32 * 32 * 8
163+
x = self.pool(x) # 16 * 16 * 8
164+
x = F.relu(x)
165+
166+
for _ in range(10):
167+
x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
168+
169+
# x = F.relu(self.conv_hidden(x)) # 16 * 16 * 8
170+
171+
x = self.conv_out(x) # 16 * 16 * 16
172+
x = self.pool(x) # 8 * 8 * 16
173+
x = F.relu(x)
174+
175+
176+
x = x.view(-1, 8 * 8 * 16) # flatten
177+
x = self.fc1(x)
178+
x = F.relu(x)
179+
x = self.fc2(x)
180+
x = F.relu(x)
181+
x = self.fc3(x)
182+
x = F.log_softmax(x)
183+
return x
184+
185+
186+
class CNN_3(nn.Module):
187+
def __init__(self):
188+
super(CNN_3, self).__init__()
189+
190+
# # Output = (Input - Kernel_size + 2*Padding_size) / Stride + 1
191+
# self.conv_in = nn.Conv2d(in_channels = 3, out_channels = 8, kernel_size = 3, padding = 1)
192+
#input = 3, output = 6, kernal = 5
193+
self.conv1 = nn.Conv2d(3, 6, 5) # 32 * 32 * 6
194+
195+
196+
# # Output = (Input - kernel_size) / stride + 1
197+
# self.pool = nn.MaxPool2d(kernel_size = 2, stride = 2)
198+
#kernal = 2, stride = 2, padding = 0 (default)
199+
self.pool = nn.MaxPool2d(2, 2)
200+
self.conv2 = nn.Conv2d(6, 16, 5)
201+
#input feature, output feature
202+
self.fc1 = nn.Linear(16 * 5 * 5, 120)
203+
self.fc2 = nn.Linear(120, 84)
204+
self.fc3 = nn.Linear(84, 10)
205+
206+
207+
# 값 계산
208+
def forward(self, x):
209+
x = self.pool(F.relu(self.conv1(x)))
210+
x = self.pool(F.relu(self.conv2(x)))
211+
x = x.view(-1, 16 * 5 * 5)
212+
x = F.relu(self.fc1(x))
213+
x = F.relu(self.fc2(x))
214+
x = self.fc3(x)
215+
return x
216+
217+
444 Bytes
Binary file not shown.
607 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)