KHUDA 4th/Computer Vision
[KHUDA 4th] CV 2주차 세션 (09.27)
시데브
2023. 9. 28. 14:49
2주차 세션은 추석 이슈로 비대면으로 진행
Perceptron 구현 과제
perceptron 학습 코드에서 neural network 내부의 forward, backward함수와 softmax, one_hot_encoding 함수를 구현!
class NeuralNetwork(object):
def forward(self, x):
x = x.reshape(-1, 1) #image 1열로 나열
output = self.w * x #w의 형태??
output = np.sum(output, axis=0)
output = output + self.b.T #bias의 전치
return output
def backward(self, output, label, learning_rate):
output = softmax(output)
error = label - output
grad_w = -np.outer(image, error.T)
grad_b = -error.T
self.w -= learning_rate * grad_w
self.b -= learning_rate * grad_b
def softmax(a):
sum_all = 0.0
for index in range(a.shape[0]):
sum_all += np.exp(a[index])
softmax_output = np.exp(a)/ sum_all
return softmax_output
def one_hot_encoding(labels):
output = np.zeros(shape=(labels.shape[0], 10))
for i in range(labels.shape[0]):
output[i][labels[i]] = 1
return output
AlexNet 구현 코드
이번 주차 실습은 우리 조가 준비했는데, 목적은 CIFAR10 학습, 모델은 AlexNet으로 준비했다.
Data Augmentation
transforms.Pad(4),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32),
- Pad: 패딩값 추가
- RandomHorizontalFlip: HorizontalFlip의 확률을 조정
- RandomCrop: 설정한 크기만큼 랜덤하게 crop
이외에도
- TrivialAugmentWide, RandAugment와 같이 Augmentation을 랜덤하게 진행하여 최적의 성능을 맞춰주는 방법도 있음
AlexNet 학습
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
#Conv2d(입력채널수, 출력채널수, 필터 크기)
#Conv1
nn.Conv2d(3,64,kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(2,2),
#Conv2
nn.Conv2d(64, 192, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2,2),
#Conv3
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(),
#Conv4
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(),
#Conv5
nn.Conv2d(256, 256, kernel_size=1),
nn.ReLU(),
nn.MaxPool2d(2,2)
)
#FC 구축
self.classifier = nn.Sequential(
#fc1
nn.Dropout(0.5),
nn.Linear(256*3*3, 1024),
nn.ReLU(),
#fc2
nn.Dropout(0.5),
nn.Linear(1024,512),
nn.ReLU(),
nn.Linear(512,10)
)
>> 10에폭에서 약 73%의 최고성능, 20에폭에서 약 78%의 최고성능을 보임
CIFAR10에 맞게 변형된 AlexNet Model Summary
728x90
반응형