from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
# CUT MIX 를 구현하시오
from PIL import Image
import numpy as np

def rand_bbox(lam):
    """
    랜덤한 사이즈로 crop 하는 함수 
    size: 이미지 크기
    lam: 이미지 중 몇 퍼센트를 자를지 정하는 함수
    output: crop할 네 좌표 bbx1, bby1, bbx2, bby2
    """
    W = 256
    H = 256
    cut_rat = np.sqrt(1. - lam)
    #자를 이미지의 W, H 선택 (int) 
    cut_w = np.int(W * cut_rat)
    cut_h = np.int(H * cut_rat)

    # Uniform 하게 자를 x,y 좌표를 선택
    cx = np.random.randint(W)
    cy = np.random.randint(H)

    # numpy clip 을 통해서 각 좌표의 크기가 이미지 크기를 벗어나지 못하게 하기 , cut size의 //2를 빼고 더할 것
    bbx1 = np.clip(cx - cut_w // 2, 0, W)
    bby1 = np.clip(cy - cut_h // 2, 0, H)
    bbx2 = np.clip(cx + cut_w // 2, 0, W)
    bby2 = np.clip(cy + cut_h // 2, 0, H)

    print(bbx1, bby1, bbx2, bby2)
    return bbx1, bby1, bbx2, bby2

def cut_generator(img1, img2, lam = 0.5):
    """
    Cut mix 이미지 만드는 함수, img1에 img2를 갖다 붙일 것임
    output: 갖다 붙인 img1
    """
    bbx1, bby1, bbx2, bby2, = rand_bbox(lam)
    # img1[:,:,bbx1:bbx2, bby1:bby2] = img1[rand_index,:,bbx1:bbx2, bby1:bby2]
    print(img1.shape)
    print(img2.shape)
    

    
    image = img1.copy()
    copy_image = img2.copy()
    # image[bbx1:bbx2, bby1:bby2,:] = img2[bbx1:bbx2, bby1:bby2, :]



    # Rainbow Cutmix
    image[bbx1:bbx2, bby1:bby2,:] = img2[bbx1:bbx2, bby1:bby2, :]

    
    return image


# Test용 코드 
# 이미지 확장자가 안 맞으면 jpeg로 바꿔보세요
dir1 = '/content/drive/MyDrive/PyTorch_YearDream/2022-01-18/dog.jpg'
dir2 = '/content/drive/MyDrive/PyTorch_YearDream/2022-01-18/cat.jpg' 
img1 = np.array(Image.open(dir1).convert('RGB').resize((256,256)))
img2 = np.array(Image.open(dir2).convert('RGB').resize((256,256)))


img = cut_generator(img1,img2)
Image.fromarray(img)
20 0 200 175
(256, 256, 3)
(256, 256, 3)

# CUT MIX 를 구현하시오
from PIL import Image
import numpy as np

def rand_bbox(lam):
    """
    랜덤한 사이즈로 crop 하는 함수 
    size: 이미지 크기
    lam: 이미지 중 몇 퍼센트를 자를지 정하는 함수
    output: crop할 네 좌표 bbx1, bby1, bbx2, bby2
    """
    W = 256
    H = 256
    cut_rat = np.sqrt(1. - lam)
    #자를 이미지의 W, H 선택 (int) 
    cut_w = np.int(W * cut_rat)
    cut_h = np.int(H * cut_rat)

    # Uniform 하게 자를 x,y 좌표를 선택
    cx = np.random.randint(W)
    cy = np.random.randint(H)

    # numpy clip 을 통해서 각 좌표의 크기가 이미지 크기를 벗어나지 못하게 하기 , cut size의 //2를 빼고 더할 것
    bbx1 = np.clip(cx - cut_w // 2, 0, W)
    bby1 = np.clip(cy - cut_h // 2, 0, H)
    bbx2 = np.clip(cx + cut_w // 2, 0, W)
    bby2 = np.clip(cy + cut_h // 2, 0, H)

    print(bbx1, bby1, bbx2, bby2)
    return bbx1, bby1, bbx2, bby2

def cut_generator(img1, img2, lam = 0.5):
    """
    Cut mix 이미지 만드는 함수, img1에 img2를 갖다 붙일 것임
    output: 갖다 붙인 img1
    """
    bbx1, bby1, bbx2, bby2, = rand_bbox(lam)
    # img1[:,:,bbx1:bbx2, bby1:bby2] = img1[rand_index,:,bbx1:bbx2, bby1:bby2]
    print(img1.shape)
    print(img2.shape)
    

    
    image = img1.copy()
    copy_image = img2.copy()
    # image[bbx1:bbx2, bby1:bby2,:] = img2[bbx1:bbx2, bby1:bby2, :]



    # Rainbow Cutmix
    
    ave_x = (max(bbx1,bbx2) - min(bbx1,bbx2)) // 7
    ave_y = (max(bby1,bby2) - min(bby1,bby2)) // 7
    print(f"ave_x = {ave_x}, ave_y = {ave_y}")
    # print(f"ave_x = {min(bbx1,bbx2)+ave_x}, ave_y = {min(bby1,bby2)+ave_y}")
    
    # x = min(bbx1,bbx2)+ave_x
    # y = min(bby1,bby2)+ave_y

    rand_index = np.random.permutation(3)
    print(f"first rand_index = {rand_index}")
    image[bbx1:bbx2, bby1:bby2,:] = img2[bbx1:bbx2, bby1:bby2, rand_index]


    rand_index = np.random.permutation(3)
    print(f"second rand_index = {rand_index}")
    image[min(bbx1,bbx2)+ave_x:max(bbx1,bbx2), bby1:bby2,:] = copy_image[min(bbx1,bbx2)+ave_x:max(bbx1,bbx2), bby1:bby2, rand_index]

    rand_index = np.random.permutation(3)
    print(f"third rand_index = {rand_index}")
    image[min(bbx1,bbx2)+(ave_x*2):max(bbx1,bbx2), bby1:bby2,:] = copy_image[min(bbx1,bbx2)+(ave_x*2):max(bbx1,bbx2), bby1:bby2, rand_index]

    rand_index = np.random.permutation(3)
    print(f"second rand_index = {rand_index}")
    image[min(bbx1,bbx2)+(ave_x*3):max(bbx1,bbx2), bby1:bby2,:] = copy_image[min(bbx1,bbx2)+(ave_x*3):max(bbx1,bbx2), bby1:bby2, rand_index]

    rand_index = np.random.permutation(3)
    print(f"second rand_index = {rand_index}")
    image[min(bbx1,bbx2)+(ave_x*4):max(bbx1,bbx2), bby1:bby2,:] = copy_image[min(bbx1,bbx2)+(ave_x*4):max(bbx1,bbx2), bby1:bby2, rand_index]

    rand_index = np.random.permutation(3)
    print(f"second rand_index = {rand_index}")
    image[min(bbx1,bbx2)+(ave_x*5):max(bbx1,bbx2), bby1:bby2,:] = copy_image[min(bbx1,bbx2)+(ave_x*5):max(bbx1,bbx2), bby1:bby2, rand_index]

    rand_index = np.random.permutation(3)
    print(f"second rand_index = {rand_index}")
    image[min(bbx1,bbx2)+(ave_x*6):max(bbx1,bbx2), bby1:bby2,:] = copy_image[min(bbx1,bbx2)+(ave_x*6):max(bbx1,bbx2), bby1:bby2, rand_index]

    rand_index = np.random.permutation(3)
    print(f"second rand_index = {rand_index}")
    image[min(bbx1,bbx2)+(ave_x*7):max(bbx1,bbx2), bby1:bby2,:] = copy_image[min(bbx1,bbx2)+(ave_x*7):max(bbx1,bbx2), bby1:bby2, rand_index]
    
    
    return image


# Test용 코드 
# 이미지 확장자가 안 맞으면 jpeg로 바꿔보세요
dir1 = '/content/drive/MyDrive/PyTorch_YearDream/2022-01-18/dog.jpg'
dir2 = '/content/drive/MyDrive/PyTorch_YearDream/2022-01-18/cat.jpg' 
img1 = np.array(Image.open(dir1).convert('RGB').resize((256,256)))
img2 = np.array(Image.open(dir2).convert('RGB').resize((256,256)))

# for i in range(len(img1[:,:,0])):
#   for j in range(len(img1[:,:,0][i])):
#     print(f"img1[:,:,0][i] = {img1[:,:,0][i]}")

# print(img1[:,:,0][0])
# print(img1[:,:,0][1])
# print(img1[:,:,0][2])
print(img1[:,:,1])
print(img1[:,:,2])
img = cut_generator(img1,img2)
Image.fromarray(img)
[[208 208 210 ... 228 227 227]
 [208 208 210 ... 228 227 227]
 [208 208 209 ... 228 227 227]
 ...
 [189 189 189 ... 102 102 101]
 [189 189 189 ... 100 101 100]
 [189 189 189 ...  99 100 100]]
[[198 197 197 ... 225 225 225]
 [198 197 197 ... 225 225 225]
 [198 197 196 ... 225 225 225]
 ...
 [178 177 177 ...  52  53  54]
 [179 178 177 ...  50  51  53]
 [179 178 177 ...  49  51  54]]
50 141 230 256
(256, 256, 3)
(256, 256, 3)
ave_x = 25, ave_y = 16
first rand_index = [0 1 2]
second rand_index = [0 2 1]
third rand_index = [2 1 0]
second rand_index = [1 0 2]
second rand_index = [1 2 0]
second rand_index = [1 0 2]
second rand_index = [2 1 0]
second rand_index = [1 0 2]


댓글남기기