카테고리 없음

LSTM AutoEncoder로 영상 시퀀스 이해하기

저긍 2025. 5. 30. 20:00
반응형

논문 아주 간단히 정리

 

Unsupervised Learning of Video Representations using LSTMs” (ICML 2015)

 

이 논문은 영상 시퀀스를 이해하기 위한 LSTM 기반 AutoEncoder 구조를 제안한다. 간단히 말해 라벨 없이 비디오 데이터를 학습하고, 그 안의 중요한 패턴을 뽑아내는 방법이다.

 

 


왜 LSTM AutoEncoder?

 

비디오 = 여러 장의 이미지 프레임 (시퀀스 데이터)

 

  • 고차원 + 시간적 흐름 포함
  • 라벨 구하기 어렵고 비용 큼
  • 따라서 Unsupervised + Temporal modeling이 동시에 필요

 

기존 AutoEncoder는 한 장 이미지에 적합하지, 시간 흐름을 반영하긴 어렵다. 그래서 등장한 게 바로 LSTM AutoEncoder임.

 

 

 

 

 

 

Encoder

  • LSTM 구조 사용
  • 시퀀스 데이터를 받아서 고정된 feature vector로 압축
  • 이미지 모양 + 움직임 방향 등의 정보 내포

Reconstruction Decoder

  • Encoder에서 만든 feature vector를 이용해 과거 시퀀스 복원
  • 역순으로 복원하는 것이 특징 → 시점 간 관계 학습에 도움

Prediction Decoder

  • 같은 feature vector로 미래 시퀀스 생성
  • 하나의 feature로 "과거 복원 + 미래 예측"을 같이 수행함

 

 


Encoder & Decoder 모듈

class Encoder(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers):
        super().__init__()
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
    def forward(self, x):
        return self.lstm(x)[1]  # (hidden, cell)

class Decoder(nn.Module):
    def __init__(self, input_size, hidden_size, output_size, num_layers):
        super().__init__()
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)
    def forward(self, x, hidden):
        output, hidden = self.lstm(x, hidden)
        return self.fc(output), hidden

 

전체 Seq2Seq 모델

class Seq2Seq(nn.Module):
    def __init__(self, args):
        super().__init__()
        self.encoder = Encoder(args.input_size, args.hidden_size, args.num_layers)
        self.reconstruct_decoder = Decoder(args.input_size, args.hidden_size, args.output_size, args.num_layers)
        self.predict_decoder = Decoder(args.input_size, args.hidden_size, args.output_size, args.num_layers)
        self.criterion = nn.MSELoss()

    def forward(self, src, trg):
        encoder_hidden = self.encoder(src)
        # Prediction
        predict_input = torch.zeros_like(src[:, :1])
        predict_outputs = []
        hidden = encoder_hidden
        for _ in range(trg.size(1)):
            out, hidden = self.predict_decoder(predict_input, hidden)
            predict_outputs.append(out)
            predict_input = out.detach()
        predict_output = torch.cat(predict_outputs, dim=1)
        predict_loss = self.criterion(predict_output, trg)

        # Reconstruction (reverse)
        inv_idx = torch.arange(src.size(1) - 1, -1, -1)
        reconstruct_input = torch.zeros_like(src[:, :1])
        reconstruct_outputs = []
        hidden = encoder_hidden
        for _ in range(src.size(1)):
            out, hidden = self.reconstruct_decoder(reconstruct_input, hidden)
            reconstruct_outputs.append(out)
            reconstruct_input = out.detach()
        reconstruct_output = torch.cat(reconstruct_outputs, dim=1)
        reconstruct_loss = self.criterion(reconstruct_output, src[:, inv_idx, :])

        return reconstruct_loss, predict_loss

 

 

학습 구성

args = {
    "batch_size": 128,
    "device": torch.device("cuda" if torch.cuda.is_available() else "cpu"),
    "input_size": 64 * 64,
    "hidden_size": 2048,
    "output_size": 64 * 64,
    "num_layers": 2,
    "learning_rate": 5e-4,
    "max_iter": 10000,
}

손실함수: 논문은 BCE 사용했지만, MSE가 시각적으로 더 좋은 결과 도출될 것 같아 MSE 사용

 

 

 

시각화

def animation_show(original, generated, save_path):
    fig = plt.figure(figsize=(8, 4))
    camera = Camera(fig)
    for i in range(len(original)):
        plt.subplot(1, 2, 1)
        plt.imshow(original[i], cmap='gray')
        plt.title('Original')
        plt.subplot(1, 2, 2)
        plt.imshow(generated[i], cmap='gray')
        plt.title('Generated')
        camera.snap()
    anim = camera.animate()
    anim.save(save_path, dpi=150)

 


 

LSTM AutoEncoder는 영상 시퀀스 데이터에 Self-Supervised 방식으로 접근할 수 있다

 

Anomaly Detection

Sequence Forecasting

Representation Learning

 

등 다양한 응용이 가능

 

 

 

Srivastava et al. (ICML 2015), Unsupervised Learning of Video Representations using LSTMs

[1502.04681] Unsupervised Learning of Video Representations using LSTMs

 

 

반응형