[딥러닝] 파이토치로 딥러닝 구현 (모델 생성, 모델 훈련, 모델 검증)(2) (feat. 파라미터 수 확인, torchsummary)

김영민·2022년 9월 28일
0

DeepLearning

목록 보기
20/33
post-custom-banner

너무 길어질 것 같아 1과 2로 나누어 정리하겠습니다.

(1) 링크 : https://velog.io/@ym980118/딥러닝-파이토치로-딥러닝-구현-데이터-전처리-이미지-시각화-모델-구현

1. 모델 생성

class LeNet(nn.Module):
  def __init__(self):
    super(LeNet,self).__init__()
    self.cnn1 = nn.Conv2d(3, 16, 5)
    self.relu1 = nn.ReLU()
    self.maxpool1 = nn.MaxPool2d(2)
    self.cnn2 = nn.Conv2d(16,32, 5)
    self.relu2 = nn.ReLU()
    self.maxpool2 = nn.MaxPool2d(2)

    self.fc1 = nn.Linear(32*53*53,512)
    self.relu3 = nn.ReLU()
    self.fc2 = nn.Linear(512,2)
    self.output = nn.Softmax(dim=1)


  def forward(self,x):
    out = self.cnn1(x)
    out = self.relu1(out)
    out = self.maxpool1(out)
    out = self.cnn2(out)
    out = self.relu2(out)
    out = self.maxpool2(out)

    out = out.view(out.size(0),-1)
    out = self.fc1(out)
    out = self.relu3(out)
    out = self.fc2(out)
    out = self.output(out)

    return out
    
model = LeNet()
model = model.to(device)
model
  • LeNet 사용 (모델 확인)


torchsummary를 통해 직관적으로 모델 확인 가능


파라미터 수 확인

def count_parameters(model):
  return sum(p.numel() for p in model.parameters() if p.require_grad)

2. 손실함수 & 옵티마이저 설정

criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

3. 모델 훈련

def train_model(model, dataloader_dict, criterion, optimizer, num_epoch):
  since = time.time()
  best_acc=0.0

  for epoch in range(num_epoch):
    print("{} / {}".format(epoch, num_epoch))
    print("-"*20)

    for phase in ['train','val']:
      if phase == 'train':
        model.train()
      else:
        model.eval()

      epoch_loss = 0.0
      epoch_corrects = 0.0

      for inputs, labels in tqdm(dataloader_dict[phase]):
        inputs = inputs.to(device)
        labels = labels.to(device)

        optimizer.zero_grad()

        with torch.set_grad_enabled(phase=='train'):
          outputs = model(inputs)
          _,preds = torch.max(outputs,1)
          loss = criterion(outputs, labels)

          if phase == 'train':
            loss.backward()
            optimizer.step()
          
          epoch_loss += loss.item() * inputs.size(0)
          epoch_corrects += torch.sum(preds==labels.data)
      
      epoch_loss = epoch_loss / len(dataloader_dict[phase].dataset)
      epoch_acc = epoch_corrects.double() / len(dataloader_dict[phase].dataset)

      print("{} LOSS : {:.4f} ACC: {:.4f}".format(phase, epoch_loss, epoch_acc))

      if phase == 'val' and epoch_acc > best_acc:
        best_acc = epoch_acc
        best_model_wts = model.state_dict()

  time_elapsed = time.time() - since


  print('Training Complete {}m {}s'.format(time_elapsed //60, time_elapsed % 60))
  print('Best val ACC : {:.4f}'.format(best_acc))

  return model
post-custom-banner

0개의 댓글