카테고리 없음

VS code와 anaconda 연동

ksh950510 2023. 2. 19. 16:51
728x90

VS Code와 Anaconda 설치

 

VS Code에서

 

 

Anaconda에서

 - base 환경에서 python 설치 후 새로운 Environment 생성

 - 해당 Environment에서 https://pytorch.org/get-started/locally/ stable 버전 확인 후 설치

conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

실행시 python, CUDA, pip 등 필수 패키지가 설치됨

 

VS Code에서

 - python과 python extension 확장 설치

 

 - Interpreter 변경 

Command palette (Ctrl + Shift + P)에서 Python: Select Interpreter > Python 3.9.7 ('base')

 

 - 기본 terminal 변경

Settings(Ctrl + ,)에서
1. terminal.integrated.defaultProfile.windows 검색 > 

PowerShell or Null 에서 Command Prompt 로 변경
2. python.condaPath 검색 > 
C:\Users\(username)\anaconda3\Scripts\conda.exe 로 변경

이제 terminal을 띄우면 자동으로 conda Environment를 잡는걸 확인할 수 있다.

 

Anaconda와 git 연결

 

training 하거나 modify할 git 을 선택해서 clone 한 후 (아래는 U-Net 예시)

git clone https://github.com/milesial/Pytorch-UNet.git

git의 read.me 를 읽고 requirement를 설치하거나 게시자가 명시한 다른 사전작업 해줌

pip install -r requirements.txt

 

이후 데이터 셋을 다운받아서 training 해볼 수 있다.

아래의 예시는  Cifar-10 dataset을 다운받는 예시이다.

transform의 코드는 U-net의 paper에서 572*572*3의 입력을 받기 때문에 존재하는데,

요즘은 어떤 입력이든 다 받을 수 있게 model의 input을 구성하기 때문에 위의 git에서 model을 다운받았다면 필요없는 코드이다.

import os
import torch
from torchvision import datasets, transforms

# Define the data path
data_path = "./data"

# Create the data directory if it doesn't exist
if not os.path.exists(data_path):
    os.makedirs(data_path)

# Define the preprocessing transforms
transform = transforms.Compose([transforms.Resize((572, 572)),
                                transforms.ToTensor()])

# Download the CIFAR-10 dataset and apply the transforms
train_dataset = datasets.CIFAR10(root=data_path, train=True, download=True, transform=transform)
test_dataset = datasets.CIFAR10(root=data_path, train=False, download=True, transform=transform)