[AnimeSR] AIでアニメの画質を上げる [超解像]

2023年2月24日金曜日

Artificial Intelligence

本記事では、AnimeSRと呼ばれる機械学習手法を用いて、任意のアニメ動画を超解像する方法をご紹介します。

アイキャッチ
出典: TencentARC/AnimeSR

AnimeSR

概要

AnimeSRは、現実世界のアニメーションビデオの超解像に特化した、Video Super Resolution(VSR)技術です。

AnimeSRでは、ぼかし、ノイズ、圧縮などを実際の低品質のアニメーションから学習し、学習したものを劣化生成パイプライン(degradation generation pipeline)に組み込んでいます。
また、効率的なマルチスケールネットワーク構造を構築し、現実世界の低品質アニメーションを効率的に復元しSOTAを達成しています。

Architecture
出典: AnimeSR: Learning Real-World Super-Resolution Models for Animation Videos

詳細はこちらの論文をご参照ください。

本記事では上記手法を用いて、任意のアニメーションを超解像していきます。

デモ(Colaboratory)

それでは、実際に動かしながらアニメの超解像を行っていきます。
ソースコードは本記事にも記載していますが、下記のGitHubでも取得可能です。
GitHub - Colaboratory demo

また、下記から直接Google Colaboratoryで開くこともできます。
Open In Colab

なお、このデモはPythonで実装しています。
Pythonの実装に不安がある方、Pythonを使った機械学習について詳しく勉強したい方は、以下の書籍やオンライン講座などがおすすめです。

環境セットアップ

それではセットアップしていきます。 Colaboratoryを開いたら下記を設定しGPUを使用するようにしてください。

「ランタイムのタイプを変更」→「ハードウェアアクセラレータ」をGPUに変更

初めにGithubからソースコードを取得します。

%cd /content

!git clone https://github.com/TencentARC/AnimeSR.git

%cd /content/AnimeSR
# Commits on Feb 10, 2023
!git checkout 2c1a74231632ec70d485d09f45bce8afed688193

次にライブラリをインストールします。

%cd /content/AnimeSR

# Install dependent packages
!pip install -r requirements.txt
!pip install --upgrade gdown
!pip install moviepy==0.2.3.5 imageio==2.4.1
!pip install yt-dlp

# Install AnimeSR
!python setup.py develop

最後にライブラリをインポートします。

from yt_dlp import YoutubeDL
from moviepy.video.fx.resize import resize
from moviepy.editor import VideoFileClip

以上で環境セットアップは完了です。

学習済みモデルのセットアップ

ここでは、Google Driveから学習済みモデルをダウンロードします。

!gdown --id 1wpBXS5PIKDAC8IvMuCGNnrrA_fMTzF9P \
        -O ./weights/AnimeSR_v1-PaperModel.pth

!gdown --id 1E_qsFqlIre-fMUSSLADQboBssNIWSqmm \
        -O ./weights/AnimeSR_v2.pth

学習済みモデルのセットアップ

続いて、Youtubeから動画をダウンロードします。

video_url = 'https://www.youtube.com/watch?v=nlpSopy0d-U' #@param {type:"string"}

#@markdown 動画の切り抜き範囲(秒)を指定してください。\
#@markdown 30秒以上の場合OOM発生の可能性が高いため注意
start_sec =  210#@param {type:"integer"}
end_sec =  215#@param {type:"integer"}

(start_pt, end_pt) = (start_sec, end_sec)

%cd /content/AnimeSR

!mkdir -p test_video

download_resolution = 720
full_video_path = './test_video/full_video.mp4'
input_clip_path = './test_video/clip_video.mp4'

# 動画ダウンロード
ydl_opts = {'format': f'best[height<={download_resolution}]', 'overwrites': True, 'outtmpl': full_video_path}
with YoutubeDL(ydl_opts) as ydl:
    ydl.download([video_url])

# 指定区間切り抜き
with VideoFileClip(full_video_path) as video:
    subclip = video.subclip(start_pt, end_pt)
    subclip.write_videofile(input_clip_path)

超解像

それでは、スクリプトを用いて、超解像を実行します。

%cd /content/AnimeSR

'''
Usage:
  -i --input           Input video path or extracted frames folder
  -n --model_name      AnimeSR model name. Default: AnimeSR_v2, can also be AnimeSR_v1-PaperModel
  -s --outscale        The netscale is x4, but you can achieve arbitrary output scale (e.g., x2 or x1) with the argument outscale.
                       The program will further perform cheap resize operation after the AnimeSR output. Default: 4
  -o -output           Output root. Default: results
  -expname             Identify the name of your current inference. The outputs will be saved in $output/$expname
  -fps                 The fps of the (possible) saved videos. Default: None
  -extract_frame_first If input is a video, you can still extract the frames first, other wise AnimeSR will read from stream
  -num_process_per_gpu Since the slow I/O speed will make GPU utilization not high enough, so as long as the
                       video memory is sufficient, we recommend placing multiple processes on one GPU to increase the utilization of each GPU.
                       The total process will be number_process_per_gpu * num_gpu
  -suffix              You can add a suffix string to the sr video name, for example, 1gpu3processx2 which means the SR video is generated with one GPU and three process and the outscale is x2
  -half                Use half precision for inference, it won't make big impact on the visual results
'''

!CUDA_VISIBLE_DEVICES=0 python scripts/inference_animesr_video.py \
  -i ./test_video/clip_video.mp4 \
  -n AnimeSR_v2 \
  -s 4 \
  --expname animesr_v2 \
  --num_process_per_gpu 1 \
  --suffix 1gpu1process

出力結果は以下の通りです。

# 動画の確認
clip = VideoFileClip("/content/AnimeSR/results/animesr_v2/videos/clip_video/clip_video_1gpu1process.mp4")
clip = resize(clip, height=420)
clip.ipython_display()

GIFに変換しているので分かりにくいですが、画質の向上が見られます。

超解像結果

まとめ

本記事では、AnimeSRを用いたアニメの超解像をご紹介しました。

過去のアニメーション作品などの復元に活用されそうです。

また本記事では、機械学習を動かすことにフォーカスしてご紹介しました。
もう少し学術的に体系立てて学びたいという方には以下の書籍などがお勧めです。ぜひご一読下さい。


また動かせるだけから理解して応用できるエンジニアの足掛かりに下記のUdemyなどもお勧めです。

参考文献

1.  論文 - AnimeSR: Learning Real-World Super-Resolution Models for Animation Videos

2. GitHub - TencentARC/AnimeSR

AIで副業ならココから!

まずは無料会員登録

プロフィール

メーカーで研究開発を行う現役エンジニア
組み込み機器開発や機会学習モデル開発に従事しています

本ブログでは最新AI技術を中心にソースコード付きでご紹介します


Twitter

カテゴリ

このブログを検索

ブログ アーカイブ

TeDokology