nagadomi / waifu2x

Image Super-Resolution for Anime-Style Art
http://waifu2x.udp.jp/
MIT License
27.56k stars 2.71k forks source link

import the models into Chainer #420

Open CuteZombie7 opened 2 years ago

CuteZombie7 commented 2 years ago

https://github.com/tsurumeso/waifu2x-chainer This is the Chainer implementation of waifu2x, but cunet models haven't been added to this project. Is there any way to convert original luatorch models to Chainer models? It would be very helpful to me.

nagadomi commented 2 years ago

The following repository has PyTorch implementations of vgg_7, upconv_7, and cunet (training does not work due to inplace_clip gradient issue). It is not difficult to convert to Chainer model, I think. https://github.com/nagadomi/nunif/tree/master/nunif/models/waifu2x

Also, waifu2x-caffe supports both cunet and upresnet10. It does not support training, though.

CuteZombie7 commented 2 years ago

I want to deploy the cunet model to that project, and just use it for inference. And now, the problem is how to convert t7 format to npz format. The models in that project are all in npz format.

nagadomi commented 2 years ago
  1. port from pytorch model to chainer model
  2. load model parameters from the json file into the chainer model (json file can be loaded in Python and contains the same data as t7).
  3. serialize the chainer model parameters in npz format.

ref. code that loads the json file into the pytorch model. https://github.com/nagadomi/nunif/blob/eab6952d93e85951ed4e4cff30cd26c09e1dbb63/nunif/models/load_save.py#L10-L28

CuteZombie7 commented 2 years ago

thank you, I will have a try.

CuteZombie7 commented 2 years ago

Is there any .caffemodel file of cunet? So that I can directly use CaffeFunction to convert it. I'm a novice in deep learning, it's too difficult for me to port it at the code level, because I'm familiar with neither of the frameworks.

nagadomi commented 2 years ago

waifu2x-caffe has it. The following threads may be helpful. https://github.com/lltcggie/waifu2x-caffe/issues/115

CuteZombie7 commented 2 years ago

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "input", since CaffeFunction does not support Input layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Flatten1", since CaffeFunction does not support Flatten layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Crop1", since CaffeFunction does not support Crop layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Flatten2", since CaffeFunction does not support Flatten layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Flatten3", since CaffeFunction does not support Flatten layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Crop2", since CaffeFunction does not support Crop layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Flatten4", since CaffeFunction does not support Flatten layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Crop3", since CaffeFunction does not support Crop layer 'support %s layer' % (layer.name, layer.type)) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/links/caffe/caffe_function.py:165: UserWarning: Skip the layer "Crop4", since CaffeFunction does not support Crop layer 'support %s layer' % (layer.name, layer.type)) ------------------------------OK----------------------------- 2.0x scaling... Traceback (most recent call last): File "waifu2x.py", line 270, in main() File "waifu2x.py", line 245, in main args, dst, models['noise_scale'], models['alpha']) File "waifu2x.py", line 47, in upscale_image dst = reconstruct.image(dst, model, cfg.block_size, cfg.batch_size) File "/home/aistudio/work/waifu2x-chainer/lib/reconstruct.py", line 135, in image dst = blockwise(src, model, block_size, batch_size) File "/home/aistudio/work/waifu2x-chainer/lib/reconstruct.py", line 52, in blockwise batch_y = model(batch_x) File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/chainer/link.py", line 287, in call out = forward(*args, **kwargs) TypeError: forward() missing 1 required positional argument: 'outputs'

It seems that caffeFunction doesn't surpport some layers. Is there any way to solve this problem? Or should i port the model at code level ? :(

nagadomi commented 2 years ago

I am not familiar with the chainer code, but the documentation says the following:

It does not support full compatibility against Caffe. Some layers and configurations are not implemented in Chainer yet, though the reference models provided by the BVLC team are supported except data layers. https://docs.chainer.org/en/v7.8.0/reference/generated/chainer.links.caffe.CaffeFunction.html

Also, chainer has already decided to discontinue development.

CuteZombie7 commented 2 years ago

All right. I will go on to port the model at code level. Since i want to install waifu2x on a platform where torch, caffe and some other frameworks are banned, chainer is the only choice, maybe. And thanks for your help.