Open hyunsu-yang opened 4 years ago
합성곱 방법
conv = nn.Conv2d(1,1,11, stride=4, padding=0)
inputs = torch.Tensor(1,1,227,227)
out = conv(inputs)
Cvolution 의 output 크기 측정
pooling 방법
#max/avg pooling
pool = nn.MaxPool2d(3)
CNN implementation
input1 = torch.Tensor(1,1,28,28)
conv1 = nn.Conv2d(1,5,5)
pool = nn.MaxPool2d(2)
out = conv1(input1)
out2 = pool(out)
out.size()
out2.size()
Convolution 과 Cross-correlation 의 차이
padding을 넣는 이유는?
5x5 이미지에 대하여 stride가 1인 경우와 2인 경우 연산량이 몇 배 차이 나는가? (단, filter는 3x3이고 padding 등은 없는 것으로 한다.)
학습 단계
핵심 모델 CNN 구조 만들기
# CNN Model (2 conv layers)
class CNN(torch.nn.Module):
def __init__(self):
super(CNN, self).__init__()
# L1 ImgIn shape=(?, 28, 28, 1)
# Conv -> (?, 28, 28, 32)
# Pool -> (?, 14, 14, 32)
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2))
# L2 ImgIn shape=(?, 14, 14, 32)
# Conv ->(?, 14, 14, 64)
# Pool ->(?, 7, 7, 64)
self.layer2 = torch.nn.Sequential(
torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2))
# Final FC 7x7x64 inputs -> 10 outputs
self.fc = torch.nn.Linear(7 * 7 * 64, 10, bias=True)
torch.nn.init.xavier_uniform_(self.fc.weight)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.view(out.size(0), -1) # Flatten them for FC
out = self.fc(out)
return out
# instantiate CNN model
model = CNN().to(device)
# train my model
total_batch = len(data_loader)
print('Learning started. It takes sometime.')
for epoch in range(training_epochs):
avg_cost = 0
for X, Y in data_loader:
# image is already size of (28x28), no reshape
# label is not one-hot encoded
X = X.to(device)
Y = Y.to(device)
optimizer.zero_grad()
hypothesis = model(X)
cost = criterion(hypothesis, Y)
cost.backward()
optimizer.step()
avg_cost += cost / total_batch
print('[Epoch: {:>4}] cost = {:>.9}'.format(epoch + 1, avg_cost))
print('Learning Finished!')
# Test model and check accuracy
with torch.no_grad():
X_test = mnist_test.test_data.view(len(mnist_test), 1, 28, 28).float().to(device)
Y_test = mnist_test.test_labels.to(device)
prediction = model(X_test)
correct_prediction = torch.argmax(prediction, 1) == Y_test
accuracy = correct_prediction.float().mean()
print('Accuracy:', accuracy.item())
cpu를 사용하는 경우와 cuda를 사용할 경우 random seed를 줄 때 코드 상에 어떤 차이가 있는지?
# for reproducibility
torch.manual_seed(777)
if device == 'cuda':
torch.cuda.manual_seed_all(777)
레이어를 깊게 쌓을 수록 정확도가 더 좋아질까?
강의
목차