멀티 입력 모델

PyTorch로 배우는 Intermediate Deep Learning

Michal Oleszak

Machine Learning Engineer

왜 멀티 입력인가?

더 많은 정보 사용

두 장의 자동차 이미지를 입력으로 받고 하나의 출력을 내는 모델 도식.

멀티모달 모델

이미지와 텍스트를 입력으로 받아 텍스트를 출력하는 모델 도식.

메트릭 러닝

두 장의 얼굴 이미지를 입력으로 받아 동일인 여부를 예측하는 모델 도식.

자기지도학습

같은 이미지의 두 증강본을 입력으로 받아 동일함을 학습하는 모델 도식.

PyTorch로 배우는 Intermediate Deep Learning

Omniglot 데이터셋

Omniglot 데이터셋의 이미지 샘플.

1 Lake, B. M., Salakhutdinov, R., and Tenenbaum, J. B. (2015). Human-level concept learning through probabilistic program induction. Science, 350(6266), 1332-1338.
PyTorch로 배우는 Intermediate Deep Learning

문자 분류

모델 도식: 문자 이미지가 신경망에 입력됨.

PyTorch로 배우는 Intermediate Deep Learning

문자 분류

모델 도식: 원-핫 알파벳 벡터가 신경망에 입력됨.

PyTorch로 배우는 Intermediate Deep Learning

문자 분류

모델 도식: 문자와 알파벳 임베딩이 결합됨.

PyTorch로 배우는 Intermediate Deep Learning

문자 분류

모델 도식: 결합된 임베딩에서 분류기가 문자를 예측함.

PyTorch로 배우는 Intermediate Deep Learning

두 입력 Dataset

from PIL import Image

class OmniglotDataset(Dataset):

def __init__(self, transform, samples): self.transform = transform self.samples = samples
def __len__(self): return len(self.samples)
def __getitem__(self, idx): img_path, alphabet, label = self.samples[idx] img = Image.open(img_path).convert('L') img = self.transform(img) return img, alphabet, label
  • 샘플과 변환 할당

    print(samples[0])
    
    [(
      'omniglot_train/.../0459_14.png',
       array([1., 0., 0., ..., 0., 0., 0.]),
       0
     )]
    
  • __len__() 구현

  • 이미지 로드 및 변환

  • 두 입력과 레이블 반환
PyTorch로 배우는 Intermediate Deep Learning

텐서 연결(concatenation)

x = torch.tensor([
  [1, 2, 3],
])

y = torch.tensor([
  [4, 5, 6],
])

축 0으로 연결

torch.cat((x, y), dim=0)
[[1, 2, 3],
 [4, 5, 6]]

축 1으로 연결

torch.cat((x, y), dim=1)
[[1, 2, 3, 4, 5, 6]]
PyTorch로 배우는 Intermediate Deep Learning

두 입력 아키텍처

class Net(nn.Module):
    def __init__(self):
        super().__init__()

self.image_layer = nn.Sequential( nn.Conv2d(1, 16, kernel_size=3, padding=1), nn.MaxPool2d(kernel_size=2), nn.ELU(), nn.Flatten(), nn.Linear(16*32*32, 128) )
self.alphabet_layer = nn.Sequential( nn.Linear(30, 8), nn.ELU(), )
self.classifier = nn.Sequential( nn.Linear(128 + 8, 964), )
  • 이미지 처리 레이어 정의
  • 알파벳 처리 레이어 정의
  • 분류기 레이어 정의
PyTorch로 배우는 Intermediate Deep Learning

두 입력 아키텍처

def forward(self, x_image, x_alphabet):

x_image = self.image_layer(x_image)
x_alphabet = self.alphabet_layer(x_alphabet)
x = torch.cat((x_image, x_alphabet), dim=1)
return self.classifier(x)
  • 이미지를 이미지 레이어에 통과
  • 알파벳을 알파벳 레이어에 통과
  • 두 출력을 연결
  • 결과를 분류기에 통과
PyTorch로 배우는 Intermediate Deep Learning

학습 루프

net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)

for epoch in range(10):
    for img, alpha, labels in dataloader_train:
        optimizer.zero_grad()
        outputs = net(img, alpha)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
  • 학습 데이터는 세 항목으로 구성:
    • 이미지
    • 알파벳 벡터
    • 레이블
  • 모델에 이미지와 알파벳을 함께 입력
PyTorch로 배우는 Intermediate Deep Learning

Ayo berlatih!

PyTorch로 배우는 Intermediate Deep Learning

Preparing Video For Download...