【超解像】MXNet 2.0(ベータ)で超解像(Super Resolution)モデルを使用する

2022年5月5日記事を修正しました。

はじめに

MXNet 1.xで使用できる超解像モデルのためのスクリプトをMXNet 2.0用に書き直しました。

もとのスクリプトはこちらです。
touch-sp.hatenablog.com

MXNet 2.0用のスクリプト

import os
from mxnet import np, npx, gluon, image

device = npx.gpu() if npx.num_gpus() > 0 else npx.cpu()

img_file = gluon.utils.download('https://s3.amazonaws.com/onnx-mxnet/examples/super_res_input.jpg')

img = image.imread(img_file).astype('float32')/255
img = np.expand_dims(np.transpose(img, (2,0,1)), axis=0)

os.makedirs('models', exist_ok=True)
json_file = gluon.utils.download('https://raw.githubusercontent.com/WolframRhodium/Super-Resolution-Zoo/master/ARAN/aran_c0_s1_x4-symbol.json', path='models')
params_files = gluon.utils.download('https://raw.githubusercontent.com/WolframRhodium/Super-Resolution-Zoo/master/ARAN/aran_c0_s1_x4-0000.params', path='models')

net = gluon.SymbolBlock.imports(json_file, ['data'], params_files)
net.reset_device(device)

output = net(img.to_device(device))
output = np.squeeze(output)
output = (np.transpose(output, (1,2,0))*255).astype('uint8')

from PIL import Image
img = Image.fromarray(output.asnumpy())
img.show()

動作確認した環境

GPUあり

Ubuntu 20.04 LTS

Ubuntu 20.04 LTS on WSL2
Python 3.8.10
certifi==2021.10.8
charset-normalizer==2.0.12
graphviz==0.8.4
idna==3.3
mxnet-cu112==2.0.0b20220329
numpy==1.22.3
Pillow==9.1.0
requests==2.27.1
urllib3==1.26.9

Ubuntu 22.04 LTS

Ubuntu 22.04 LTS on WSL2
Python 3.10.4
certifi==2021.10.8
charset-normalizer==2.0.12
graphviz==0.8.4
idna==3.3
mxnet-cu112==2.0.0b20220504
numpy==1.22.3
Pillow==9.1.0
requests==2.27.1
urllib3==1.26.9

GPUなし

Ubuntu 20.04 LTS on WSL2
Python 3.8.10
certifi==2021.10.8
charset-normalizer==2.0.8
graphviz==0.8.4
idna==3.3
mxnet==2.0.0b20211126
numpy==1.22.0rc1
Pillow==8.4.0
pkg_resources==0.0.0
requests==2.26.0
urllib3==1.26.7

補足

1

画像表示に関してはこちらを参照して下さい。
touch-sp.hatenablog.com

2

ArrayをCPUからGPUに移す方法は「as_in_context」「copyto」「to_device」などいろいろありますが「to_device」が推奨されているようです。

ModelのパラメーターをGPUに移す方法も以前は「reset_ctx」を使っていましたが現在は「reset_device」が推奨されているようです。

以前の方法はいずれ使えなくなるのでしょう。