HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
장지원 페이지/
📕
2024 UGRP
/
Member Page
Member Page
/
권태완
권태완
/
2024/05/01

2024/05/01

[pytorch] pretrained model의 활용과 fine tuning 하기
torchvision의 pretrained model의 활용과 추가 학습을 위한 fine tuning에 대해 알아보자.
[pytorch] pretrained model의 활용과 fine tuning 하기
https://yganalyst.github.io/dl/pytorch_3/
[pytorch] pretrained model의 활용과 fine tuning 하기

VScode에서 진행


wsl에서 vscode 실행
notion image
notion image
 
나와있는 대로 필요한 라이브러리 다운로드 후 실행
notion image
 
데이터셋 불러오기
notion image
 
샘플 데이터 시각화
notion image

Modeling

pretrained model

pytorch의 torchvision.models에서는 ImageNet기반의 Pretrained model들을 제공하고 있다
예를 들면, ResNet, AlexNet, VGG등이 있다
여기서는 ResNet18을 사용한다
  • 기존 레이어의 weight를 고정한다 (freeze)
  • 마지막 fully-connected layer를 task에 맞도록 변경(fine tuning)
이렇게 하면 기존 weight들은 그대로 사용하고, 마지막 fc layer만 튜닝해서 최소한의 학습을 통해 모델링이 가능하다.
아래와 같이 [pretrained=True]인자를 넣어주면, 기본적으로 ImageNet에 학습된 weight을 불러와 주지만, 최근에는 ImageNet version에 따라 weight를 불러와서 입력해주는 것을 권장하고 있다.
freezing 및 fc_layer수정
notion image
 
torchsummary
전체 파라미터 수: 11,181,641
학습 가능한 파라미터 수: 5,130
freeze된 파라미터 수: 11,176,512
notion image

Training

learning process에 맞게 간단한 code를 구현하고 돌려보자
loss, optimizer, lr scheduler를 정의
notion image
 
Train과 Test를 위한 function정의
notion image
 
Main code에서 학습 시작
notion image
notion image
 
Tags