1101
[11주차] CNN Monthly Project
최종 제출 마지막 코드만 간단하게
나머지는 깃허브에 정리해서 올리기
net = torchvision.models.resnet50(pretrained=True)
# 마지막 레이어의 차원을 6차원으로 조절
num_features = net.fc.in_features
net.fc = nn.Linear(num_features, 6)
net = net.to(device)
epoch = 30
learning_rate = 0.001
file_name = "CustomResnetModel.pt"
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9, weight_decay=0.0002)
train_result = []
val_result = []
start_time = time.time() # 시작 시간
for i in range(epoch):
train_acc, train_loss = train_with_mixup(net, i, optimizer, criterion, train_dataloader_transferred) # 학습(training)
val_acc, val_loss = validate(net, i + 1, val_dataloader_transferred) # 검증(validation)
adjust_learning_rate(optimizer,i)
# 학습된 모델 저장하기
state = {
'net': net.state_dict()
}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, './checkpoint/' + file_name)
print(f'Model saved! (time elapsed: {time.time() - start_time})')
# 현재 epoch에서의 정확도(accuracy)와 손실(loss) 값 저장하기
train_result.append((train_acc, train_loss))
val_result.append((val_acc, val_loss))