idealo / image-super-resolution

🔎 Super-scale your images and run experiments with Residual Dense and Adversarial Networks.

Home Page:https://idealo.github.io/image-super-resolution/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't Seem to Use Trained Model

sephethus opened this issue · comments

There are instructions on how to use the pre-trained model, that's fine. However I've trained my own model on my own dataset and I want to use those weights files and test them on upscaling a low res image. I don't see a way to do this except with the predictor, and using that gets me a shape error:

Cannot assign to variable Pre_blocks_conv/kernel:0 due to variable shape (3, 3, 3, 32) and value shape (64, 3, 3, 3) are incompatible

Here's my predictor setup:

import numpy as np
from PIL import Image
from ISR.models import RDN, RRDN, rrdn
from ISR.predict import Predictor

model = RRDN(weights='gans')
predictor = Predictor(input_dir='/home/nate/images', output_dir='/home/nate/images2')
predictor.get_predictions(model, weights_path='weights/rrdn-C4-D3-G64-G064-T10-x4/2021-05-29_1003/rrdn-C4-D3-G64-G064-T10-x4_best-val_generator_PSNR_Y_epoch133.hdf5')

Is there a way to just test it out on a single image?

Hi, I'm reaching out on the fly so I can't elaborate, but with RRDN(weights='gans') you're loading the pretrained model. You need to initialize the model with the correct architecture parameters with RRDN(arch_param={...}) (please check the correct naming) and then load weights or use the predictor as you did.

Hope this helps

Hi, I'm reaching out on the fly so I can't elaborate, but with RRDN(weights='gans') you're loading the pretrained model. You need to initialize the model with the correct architecture parameters with RRDN(arch_param={...}) (please check the correct naming) and then load weights or use the predictor as you did.

Hope this helps

Thanks! I'm still not sure how to initialize the model with a local weights file location instead of a URL. There doesn't appear to be any way to do this for a single image.

img = Image.open('/home/nate/test.png')
model = RRDN(arch_params={'C': 4, 'D': 3, 'G': 32, 'G0': 32, 'x': 4, 'T': 10}, weights_path='weights/rrdn-C4-D3-G32-G032-T10-x4/2021-05-30_0952/rrdn-C4-D3-G32-G032-T10-x4_best-val_generator_PSNR_Y_epoch005.hdf5')


sr_img = model.predict(np.array(img))

Image.fromarray(sr_img).save('/home/nate/test-result.png', 'PNG')

Hi,
from your model weights, these should be the parameters:
rrdn = RRDN(arch_params={'C':4, 'D':3, 'G':64, 'G0':64, 'T':10, 'x':4})
then you can

import numpy as np
from PIL import Image
from ISR.models import RDN, RRDN, rrdn
from ISR.predict import Predictor

model = RRRDN(arch_params={'C':4, 'D':3, 'G':64, 'G0':64, 'T':10, 'x':4})
predictor = Predictor(input_dir='/home/nate/images', output_dir='/home/nate/images2')
predictor.get_predictions(model, weights_path='weights/rrdn-C4-D3-G64-G064-T10-x4/2021-05-29_1003/rrdn-C4-D3-G64-G064-T10-x4_best-val_generator_PSNR_Y_epoch133.hdf5')

For individual images you should be able to just use a load_weights call
model.load_weights('weights/rrdn-C4-D3-G64-G064-T10-x4/2021-05-29_1003/rrdn-C4-D3-G64-G064-T10-x4_best-val_generator_PSNR_Y_epoch133.hdf5')
edit:
you actually need to use
model.model.load_weights('weights/rrdn-C4-D3-G64-G064-T10-x4/2021-05-29_1003/rrdn-C4-D3-G64-G064-T10-x4_best-val_generator_PSNR_Y_epoch133.hdf5')
as the model is an attribute of RRDN

If you have an issue with a str has no attribute decode(), try first doing this:
pip install h5py==2.10.0 --force-reinstall
it's being added to the requirements and still needs to be merged.