전체 글 72

matplotlib subplot 으로 이미지 여러개 확인하기

이미지 기반 딥러닝 모델을 만들다 보면 Label 이랑 Image 샘플을 보고싶을 때가 있는데, matplotlib 의 subplot으로 한번에 확인할 수 있는 코드 import matplotlib.pyplot as plt import cv2 %matplotlib inline %config InlineBackend.figure_format = 'retina' # check data (class 1~5) fig = plt.figure(figsize=(10,5)) # rows*cols 행렬의 i번째 subplot 생성 xlabels = classes rows = 1 cols = 4 for c in range(0, 4): sample_path = train_flist[ xlabels[c] ][0] img = ..

Python Code 2023.11.02

ViT(Vision Transformer) 비전 트랜스포머 Pytorch 코드

이미지 데이터셋 생성 from PIL import Image as Image from torch.utils.data import Dataset, DataLoader from torchvision import models, transforms class ImageDataset(Dataset): ''' dataset class overloads the __init__, __len__, __getitem__ methods of the Dataset class. Parameters : df: DataFrame object for the csv file. data_path: Location of the dataset. image_transform: Transformations to apply to the imag..

Python Code 2023.11.01

딥러닝 서버 세팅-우분투 설치 후 gpu 환경 설치

계정 생성 및 sudo 권한 부여 sudo adduser 아이디 sudo usermod -aG sudo 아이디 NVIDIA-DRIVER 찾기 ubuntu-drivers devices 명령어 실행하여, 아래와 같이 recommended 버전 확인 (아래 캡처에서는 535 버전으로 나옴) NVIDIA-DRIVER 설치 sudo apt install nvidia-driver-535 (535 대신 위에서 찾은 recommended version을 쓰면 됨) 시스템 재시작 (반드시 해줘야함) sudo reboot now NVIDIA-DRIVER 설치 확인 nvidia-smi 실행하여 아래와 같이 GPU 상태가 나오는지 확인. 아나콘다 다운로드 https://www.anaconda.com/download#downl..

Deep Learning 2023.10.31

python Image Tranforms (이미지 전처리)

Image 간의 사이즈 조절, Noramlization, 텐서화 등 전처리를 한번에 적용해 줄 수 있는 torchvision.transforms 를 활용할 수 있음. transforms 코드 예시 from torchvision import transforms image_transform = transforms.Compose([ transforms.Resize([512, 512]), transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) ]) transforms 코드 적용 (Dataset Class) import torch from torch.utils.data import Dataset, DataLo..

Python Code 2023.10.28

파이썬(python) 통계분석 코드 [T test][Chi-square]

correlation heatmap 그리기 import seaborn as sns import matplotlib.pyplot as plt %matplotlib inline %config InlineBackend.figure_format = 'retina' corr_df = df[COLUMNS].corr() plt.figure(figsize=(8, 8)) sns.heatmap(corr_df, annot = True, # 실제 값 화면에 나타내기 cmap = 'RdYlBu_r', # Red, Yellow, Blue 색상으로 표시 vmin = -1, vmax = 1, #컬러차트 -1 ~ 1 범위로 표시 ) T-Test : Numeric 변수 단변량 비교 from scipy.stats import chi2_c..

Python Code 2023.10.28

Tensor를 detach() 하는 이유

모델을 학습시키면서 AUROC 지표를 뽑을 때, inference 결과를 저장해서 확인해야할 때가 있다. 그러면 예측 결과와 확률값, 타겟값(ground truth)을 numpy array 에 담거나 list 에 넣어서 저장해야 하는데, 이때 가끔 detach() 에러가 발생한다. detach 하는 이유는 주로 그래디언트 계산과 자동 미분(autograd) 시스템과 관련이 있음. 그래디언트 추적 중단: PyTorch와 같은 딥러닝 프레임워크에서는 기본적으로 텐서의 연산을 추적하여 그래디언트(미분)를 자동으로 계산함. 딥러닝 모델을 학습할 때 텐서에 대한 그래디언트를 추적하는 것은 중요하지만, 때로는 그래디언트를 추적하지 않아야 하는 경우가 있는데, 이때 .detach()를 사용하여 그래디언트 추적을 중단..

Deep Learning 2023.10.19