python 24

[PyTorch] torchsummary.summary, pretrain 모델 사용

torchsummary.summary 빌든 Pytorch 모델에 대한 요약 - layer 정보, 총 파라미터 수, 모델 총 사이즈를 확인하고 싶을 때 torchsummary 패키지를 사용하면 된다. 아래는 pretrained resnet50 모델을 불러서 확인하는 간단한 예제이다. from torchvision import models from torchsummary import summary #Load model model = models.resnet50(pretrained=True) #마지막 fc layer output dim 원하는 사이즈로 변경 num_class = 10 fc_in = model.fc.in_features model.fc = nn.Linear(fc_in, num_class) dev..

python 2022.10.27

AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'

opencv-python의 버전을 다르게 설치해주면 된다. pip install opencv-python==4.5.5.64 하지만 난 이어서 ImportError: libGL.so.1: cannot open shared object file: No such file or directory 라는 에러도 생겼는데 .. apt-get update && apt-get install libgl1 libGL.so.1이 포함되어 있는 libl1 패키지를 설치해주면 된다. 참고 #https://stackoverflow.com/questions/72706073/attributeerror-partially-initialized-module-cv2-has-no-attribute-gapi-wip-gs AttributeErro..

python 2022.08.04

[런타임에러] RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

모델(weight tensor)과 입력 데이터(input tensor) 중 하나라도 GPU에 올라가있지 않을 때 생기는 오류라고 한다. 즉, CPU 사용할거면 모두 CPU를, GPU 사용할거면 모두 GPU를 사용해야 한다. 수정 전 #수정 전: input data만 GPU에 올라가있음 network = ResNet50(input_channel=3, outputs=num_classes) images = Variable(images).cuda() labels = Variable(labels).cuda() 수정 후 network = ResNet50(input_channel=3, outputs=num_classes).cuda() images = Variable(images).cuda() labels = Vari..

python 2022.07.28

[numpy] array에서 특정 조건 인덱스 return, numpy.where

numpy.where을 사용하면 array에서 특정 조건을 만족하는 원소의 인덱스들을 추출할 수 있다. numpy.where(condition, [x, y, ]) condition에 따라서 선택된 elements들을 return한다. - condition: array_like, bool True이면 x를, False이면 y를 return한다. 예시: idx array에서 1의 위치 return import numpy as np idx = [0,3,2,1,5,4,4,1,1] idx = np.array(idx) i_1 = np.where(idx==1) # (array([3, 7, 8], dtype=int64),) temp = np.where(idx>1) #(array([1, 2, 4, 5, 6], dtype..

python 2022.07.27

[pytorch] torch.nn.Module - train, eval, no_grad

torch.nn.Module 클래스 모든 뉴럴 네트워크 모델의 베이스 클래스이다. import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) eval() 모듈을 evaluation mode로 변경한다. 이 기능은 특정 모듈에만 영향을 미친다. (Dropout, BatchNorm 등..) Dropout의 ..

python 2022.07.27

[CUDA] cuda 정리하기

CUDA - GPU로 push, synchronize, 병렬처리 등.. 대해 알아보자 CUDA(computed unified device architecture)는 GPU에서 수행하는 병렬 처리 알고리즘을 C 프로그래밍 언어 등을 사용해서 작성할 수 있도록 하는 nvidia에서 개발 중인 GPGPU 기술이다. 1. 메인 메모리를 GPU 메모리로 복사 --> 2. CPU가 GPU에 프로세스 지시 3. GPU가 각 코어에 병렬 수행 --> 4. GPU 메모리부터 결과물을 메인 메모리로 다시 복사 (위 설명은 wiki 출처) 데이터 GPU로 push하기 데이터를 GPU 메모리로 push하기 위해서는 data = data.to('cuda')를 사용하면 되고, print(data.device)를 통해 push한 ..

python 2022.05.27