본문 바로가기

인공지능/Python

(22)
Python 그래프 부드럽게 그리기 (Python plot line smooth) Problem matplotlib.pyplot을 이용해 plot을 그리려고하는데 time scale이 달라서 모양이 이쁘지 않게 나오는 경우가 있다. 아래 plot에서 origin은 time step interval이 1이고, wavelet_n 은 time step interval이 2**n으로 나오게 된다. 즉 wavelet_5는 두 값간의 time step interval이 32로 나오게 되서 굉장히 딱딱한 모양이 나오게 되며, 경향성을 알기도 어렵다. Solution scipy의 make_interp_spline함수를 이용해 해결했다. 예시 코드는 다음과 같다. # example origin_y = [10,5,2,6,20,30] origin_x = [0,4,8,12,16,20] # time step..
nvidia-smi ; Failed to initialize NVML: Driver/library version mismatch 에러 해결 Problem jini1114@user1:~/git/data/mp4$ nvidia-smi Failed to initialize NVML: Driver/library version mismatch 어제까지만해도 잘 실행되던 nvidia-smi가 갑자기 아침부터 안되기 시작했다. 뭐가 원인인지는 모르겠지만 예전에 해결해본적이 있어서 간단하게 해결한 방법을 정리하겠다. Solution nvidia 관련 모듈을 전부 지워준 뒤, nvidia-smi를 실행하면 된다. 우선 nvidia 관련 모듈을 확인해준다. jini1114@user1:~/git/data/mp4$ lsmod | grep nvidia nvidia_uvm 995328 0 nvidia_drm 57344 3 drm_kms_helper 188416 1 n..
RuntimeError: Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment 해결 Problem Multi-GPU DDP Deep Learning 중에 loss를 copy하는 부분에서 문제가 발생했다.(고 생각된다..) 에러 전문은 다음과 같다. 더보기 Traceback (most recent call last): File "/home/ubuntu/jini1114/ddp_test/maskrcnn_trainer.py", line 54, in trainer.fit(ddpmaskrcnn, datamodule=dl) File "/home/ubuntu/anaconda3/envs/pytorch/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py", line 460, in fit Traceback (most recent call la..
model 학습 시 loss대신 predict 결과가 나올 경우 Problem mask rcnn transfer learning중에 loss dict를 구하는 상황이었는데, model(image,target)넣어도 다음처럼 predict 결과만 숭숭 나왔다. {'boxes': tensor([[ 23.0990, 377.5876, 305.9265, 562.2125], [ 11.2159, 242.4047, 369.8665, 428.1471], [155.2247, 96.2208, 580.0000, 248.9312], [ 5.0806, 275.1395, 101.7696, 426.0195], [ 20.9149, 266.0938, 331.9341, 614.7516], [ 11.2956, 134.7541, 386.5382, 542.4605], [ 57.4241, 228.0799, ..
Dataloader dictionary에서 batch를 밖으로 빼내기 Problem 기존에 pytorch로 구현되어 있는 maskrcnn training 코드를 pytorch lightning으로 바꾸다가 발생한 문제. 모델의 loss를 계산하기위해 입력하는 데이터의 형식이 약간 다른 문제가 발생. batch가 dictionary 안에 들어가 있어서 이를 dictionary 밖으로 빼내야 한다. # 현재 상태 { 'boxes': tensor([[[ 25., 209., 343., 610.]], [[181., 138., 550., 608.]]], device='cuda:1') } # 되어야하는 상태 # batch가 dictionary 밖으로 나와야함 [ {'boxes': tensor([[ 25., 209., 343., 610.]], device='cuda:1')}, {'box..
RuntimeError: stack expects each tensor to be equal size 에러 해결 Problem mask rcnn 모델을 학습하기위해 dataset과 dataloader를 구성한 뒤 코드를 실행했을 때, 다음과같은 에러가 발생했다 RuntimeError: stack expects each tensor to be equal size, but got [3, 680, 720] at entry 0 and [3, 580, 620] at entry 4 Solution batch로 묶는 이미지의 사이즈가 달라서 묶을 수 없다고하는 뜻이라, dataloader setup의 transform에 resize를 넣어서 해결해야 한다. def get_transform(self): transform = [ transforms.Resize((580,620)), ToTensor() ] return Compose..
change edge thickness (using opencv Morphological Transformations ) Problem cv2.canny를 이용해 추출한 edge의 thickness를 올리고 싶어서 방법을 찾던중 형태학적 변환(Morphological Transformations)을 opencv에서 이미 제공을 하고 있는 것을 발견했다. thickness를 늘릴수도있고, 줄일수도있고, 잡티를 제거할수도 있는등, 다양한 방법을 제공하고 있었다. 난 그중에 thickness를 늘리는 것이 목적이었으므로 해당 함수를 적용했다. Solution plt.subplot(1,3,1) plt.imshow(edges,cmap='gray') plt.subplot(1,3,2) kernel = np.ones((3,3),np.uint8) dilation = cv2.dilate(edges,kernel,iterations = 1) p..
gray scale image to RGB image (using opencv) Problem image에서 egde를 추출할 일이 있었는데, edge는 gray scale이라 1channel만 가지고 있었다. 원본 image에 edge를 추가할 목적이라 1channel의 edge를 3channel로 맞춰주는 작업이 필요했다. Solution # original image read img = cv2.imread('/path/to/image.jpg') img.shape # (680,720,3) # Canny Edge Detection edges = cv2.Canny(image=img, threshold1=50, threshold2=200) edges.shape # (680,720) # 1channel image to 3channel image img2 = cv2.merge((edges..