|
| 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 | + |
0 commit comments