100일 챌린지/컴퓨터비전

Day 3 - PIL, pytorch-torchvision, logging-wandb

ksyke 2024. 11. 19. 22:09

목차

    PIL(Python Imaging Library)

    : 이미지 처리에 중점을 둔 라이브러리

    - 픽셀 단위의 이미지 조작이나, 마스킹, 투명도 제어, 윤곽 보정 밒 검출 등의 다양한 이미지 조작을 할 수 있다.

    pip install pillow

    Image read and Visualize

    from PIL import Image
    import matplotlib.pyplot as plt
    
    # image read
    img=Image.open('../data/image.jpeg')
    
    # image visualize
    plt.imshow(img)
    plt.show()
    img

    Image resize

    w,h=img.size
    img_resize=img.resize((w///2,h//2))
    plt.imshow(img_resize)
    plt.show()
    
    img_resize=img.resize((w*2,h*2))
    plt.imshow(img_resize)
    plt.show()

    Image crop

    def two_image_show(im1,im2):
    	plt.subplot(1,2,1)
        plt.imshow(im1)
        plt.subplot(1,2,2)
        plt.imshow(im2)
        plt.show()
        
    ## (lift, upper,right,lower)
    img_crop=img.crop((w//4,h//4,w*3//4,h*3//4))
    
    plt.imshow(img_crop)
    
    two_image_show(img,img_crop)

    Image rotate/flip

    img_rotate=img.rotate(45) # degree
    # img_rotate.show()
    plt.imshow(img_rotate)
    
    img_flip=img.transpose(Image.FLIP_LEFT_RIGHT)
    two_image_show(img,img_flip)
    
    img_flip_and_rotate=img.rotate(45).transpose(Image.FLIP_LEFT_RIGHT)
    img_flip_and_rotate.save('save_img.png')

    Scikit-image

    pil과 마찬가지로 이미지 필터링이 가능하다.

    PyTorch, TorchVision

    gpu와 cpu를 통해 딥러닝을 가능하게 하는 tensor library.

     

    추가 라이브러리 - Albumentation

    https://albumentations.ai/

     

    Albumentations: fast and flexible image augmentations

    Albumentations provides a comprehensive, high-performance framework for augmenting images to improve machine learning models.

    albumentations.ai

    Tensor 생성하기

    import torch
    import numpy as np
    
    data=[[1,2],3,4]]
    # 2x2 tensor
    x_data=torch.tensor(data)
    
    # scalar tensor
    x_data=torch.tensor(3.14149)
    
    # empty tensor
    x_data=torch.tensor([])
    
    # numpy array에서 tensor 만들기
    np_array=np.array(data)
    x_np=torch.from_numpy(np_array)
    
    # API를 이용해 tensor 만들기
    shape=(2,3)
    torch.rand(shape)
    torch.ones(shape)
    torch.zeros(shape)
    
    # 다른 tensor를 이용해 tensor 만들기
    x_ones=torch.ones_like(x_data) # x_data의 property만 갖음
    x_ones=torch.rand_like(x_data,dtype=torch.flaot) # x_data의 datatype을 덮어쓴다.

     

    Tensor attribute와 GPU

    # Tensor attributes
    tensor=torch.rand(3,4)
    tensor.shape
    tensor.dtype
    tensor.device
    
    # GPU에서 tensor 실행하기
    tensor=tensor.to('cuda')
    tensor.device

    Tensor operations

    Loading data

    데이터셋을 읽고 multi-process mini-batch 데이터 로딩을 해결해준다.

    # getting FashionMNIST datasets from torchvision
    from torch.utils.data import Dataset
    from torchvison import datasets
    from torchvision.transforms import ToTensor
    import matplotlib.pyplot as plt
    
    training_data=datasets.FashionMNIST(
    	root="data",
        train=True,
        download=True,
        transform=ToTensor())
        
    test_data=datasets.FashionMNIST(
    	root="data",
        train=False,
        download=True,
        transform=ToTensor())

    Custom dataset 만들때 

    •  __init__(self,[]): 데이터셋 루트 디렉토리/transform/이미지 path...
    • __len__(self): 데이터셋의 length(크기) 반환
    • __getitem__(self,idx): (idx)th data를 return. *Dataloader calls this method

    Neural Network 만들기

    __init__함수와 forward 함수 필

    class NeuralNetwork(nn.Module):
    	def __init__(self):
        	super(NeuralNetwork, self).__init__()
            self.flatten=nn.Flatten()
            self.linear_relu_stack=nn.Sequential(
            	nn.Linear(28*28,512),
                nn.ReLU(),
                nn.Linear(512,512),
                nn.ReLU(),
                nn.Linear(512,10),
    		)
    	def forward(self,x):
        	x=self.flatten(x)
            logits=selg.linear_relu_stack(x)
            return logits

    Training

    def train_loop(dataloader,model,loss_fn,optimizer):
    	size=len(dataloader.dataset)
        for batch,(X,y) in enumerate(dataloader):
        	# Compute prediction and loss
            pred=model(X)
            loss=loss_fn(pred,y)
            
            # Backpropagation
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            
            if batch%100==0:
            	loss,current=loss.item(),batch*len(X)
                print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")

    Transfer learning

    pre-trained model을 사용하기

    model_ft=models.resnet18(pretrained=True)
    num_ftrs=model_ft.fc.in_features
    
    # Here the size of each output sample is set to 2
    # Alternatively, it can be generalized to nn.Linear(num_ftrs,len(class_names))
    model_ft.fc=nn.Linear(num_ftrs,2)
    
    model_ft=model_ft.to(device)
    
    criterion=nn.CrossEntropyLoss()
    
    # Observe that all parameters are being optimized
    optimizer_ft=optim.SGD(model_ft.parameters(),lr=0.001,momentum=0.9)
    
    # Decay LR a factor of 0.1 every 7 epochs
    exp_lr_scheduler=lr_scheduler.StepLR(optimizer_ft,step_size=7,gamma=0.1)

    Logging, Wandb, Kornia

    Logging

    어떤 소프트웨어가 실행될 때 발생하는 이벤트를 추적하는 수단.

    • print()
    • logging.info()/logging.warn()
    • ...
    import logging
    
    logging.warning('watch out!') # will print out a message to the console
    logging.info('I told you so') # will not print anything
    
    logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
    logging.debug('This message should  go to the log file')
    logging.info('So should this')
    logging.warning('And this, too')
    logging.error('And non-ASCII stuff, too..')

    Wandb

    학습 모델 개발 시에 하이퍼파라미터나, 시스템메트릭, 결과를 로깅해주는 패키지

    tensorboard/tensorboardX나 hydra 같은 패키지들이 있다.

    pip install wandb

    Kornia

    AI를 위한 컴퓨터비전 알고리즘의 구현체를 모아놓은 라이브러리 

    '100일 챌린지 > 컴퓨터비전' 카테고리의 다른 글

    Day 2 - Probability, OpenCV_matplotlib  (2) 2024.11.13
    Day 1 - linear algebra  (1) 2024.11.12