Git Product home page Git Product logo

super-resolution's Introduction

Travis CI

Single Image Super-Resolution with EDSR, WDSR and SRGAN

A Tensorflow 2.x based implementation of

This is a complete re-write of the old Keras/Tensorflow 1.x based implementation available here. Some parts are still work in progress but you can already train models as described in the papers via a high-level training API. Furthermore, you can also fine-tune EDSR and WDSR models in an SRGAN context. Training and usage examples are given in the notebooks

A DIV2K data provider automatically downloads DIV2K training and validation images of given scale (2, 3, 4 or 8) and downgrade operator ("bicubic", "unknown", "mild" or "difficult").

Important: if you want to evaluate the pre-trained models with a dataset other than DIV2K please read this comment (and replies) first.

Environment setup

Create a new conda environment with

conda env create -f environment.yml

and activate it with

conda activate sisr

Introduction

You can find an introduction to single-image super-resolution in this article. It also demonstrates how EDSR and WDSR models can be fine-tuned with SRGAN (see also this section).

Getting started

Examples in this section require following pre-trained weights for running (see also example notebooks):

Pre-trained weights

  • weights-edsr-16-x4.tar.gz
    • EDSR x4 baseline as described in the EDSR paper: 16 residual blocks, 64 filters, 1.52M parameters.
    • PSNR on DIV2K validation set = 28.89 dB (images 801 - 900, 6 + 4 pixel border included).
  • weights-wdsr-b-32-x4.tar.gz
    • WDSR B x4 custom model: 32 residual blocks, 32 filters, expansion factor 6, 0.62M parameters.
    • PSNR on DIV2K validation set = 28.91 dB (images 801 - 900, 6 + 4 pixel border included).
  • weights-srgan.tar.gz
    • SRGAN as described in the SRGAN paper: 1.55M parameters, trained with VGG54 content loss.

After download, extract them in the root folder of the project with

tar xvfz weights-<...>.tar.gz

EDSR

from model import resolve_single
from model.edsr import edsr

from utils import load_image, plot_sample

model = edsr(scale=4, num_res_blocks=16)
model.load_weights('weights/edsr-16-x4/weights.h5')

lr = load_image('demo/0851x4-crop.png')
sr = resolve_single(model, lr)

plot_sample(lr, sr)

result-edsr

WDSR

from model.wdsr import wdsr_b

model = wdsr_b(scale=4, num_res_blocks=32)
model.load_weights('weights/wdsr-b-32-x4/weights.h5')

lr = load_image('demo/0829x4-crop.png')
sr = resolve_single(model, lr)

plot_sample(lr, sr)

result-wdsr

Weight normalization in WDSR models is implemented with the new WeightNormalization layer wrapper of Tensorflow Addons. In its latest version, this wrapper seems to corrupt weights when running model.predict(...). A workaround is to set model.run_eagerly = True or compile the model with model.compile(loss='mae') in advance. This issue doesn't arise when calling the model directly with model(...) though. To be further investigated ...

SRGAN

from model.srgan import generator

model = generator()
model.load_weights('weights/srgan/gan_generator.h5')

lr = load_image('demo/0869x4-crop.png')
sr = resolve_single(model, lr)

plot_sample(lr, sr)

result-srgan

DIV2K dataset

For training and validation on DIV2K images, applications should use the provided DIV2K data loader. It automatically downloads DIV2K images to .div2k directory and converts them to a different format for faster loading.

Training dataset

from data import DIV2K

train_loader = DIV2K(scale=4,             # 2, 3, 4 or 8
                     downgrade='bicubic', # 'bicubic', 'unknown', 'mild' or 'difficult' 
                     subset='train')      # Training dataset are images 001 - 800
                     
# Create a tf.data.Dataset          
train_ds = train_loader.dataset(batch_size=16,         # batch size as described in the EDSR and WDSR papers
                                random_transform=True, # random crop, flip, rotate as described in the EDSR paper
                                repeat_count=None)     # repeat iterating over training images indefinitely

# Iterate over LR/HR image pairs                                
for lr, hr in train_ds:
    # .... 

Crop size in HR images is 96x96.

Validation dataset

from data import DIV2K

valid_loader = DIV2K(scale=4,             # 2, 3, 4 or 8
                     downgrade='bicubic', # 'bicubic', 'unknown', 'mild' or 'difficult' 
                     subset='valid')      # Validation dataset are images 801 - 900
                     
# Create a tf.data.Dataset          
valid_ds = valid_loader.dataset(batch_size=1,           # use batch size of 1 as DIV2K images have different size
                                random_transform=False, # use DIV2K images in original size 
                                repeat_count=1)         # 1 epoch
                                
# Iterate over LR/HR image pairs                                
for lr, hr in valid_ds:
    # ....                                 

Training

The following training examples use the training and validation datasets described earlier. The high-level training API is designed around steps (= minibatch updates) rather than epochs to better match the descriptions in the papers.

EDSR

from model.edsr import edsr
from train import EdsrTrainer

# Create a training context for an EDSR x4 model with 16 
# residual blocks.
trainer = EdsrTrainer(model=edsr(scale=4, num_res_blocks=16), 
                      checkpoint_dir=f'.ckpt/edsr-16-x4')
                      
# Train EDSR model for 300,000 steps and evaluate model
# every 1000 steps on the first 10 images of the DIV2K
# validation set. Save a checkpoint only if evaluation
# PSNR has improved.
trainer.train(train_ds,
              valid_ds.take(10),
              steps=300000, 
              evaluate_every=1000, 
              save_best_only=True)
              
# Restore from checkpoint with highest PSNR.
trainer.restore()

# Evaluate model on full validation set.
psnr = trainer.evaluate(valid_ds)
print(f'PSNR = {psnr.numpy():3f}')

# Save weights to separate location.
trainer.model.save_weights('weights/edsr-16-x4/weights.h5')                                    

Interrupting training and restarting it again resumes from the latest saved checkpoint. The trained Keras model can be accessed with trainer.model.

WDSR

from model.wdsr import wdsr_b
from train import WdsrTrainer

# Create a training context for a WDSR B x4 model with 32 
# residual blocks.
trainer = WdsrTrainer(model=wdsr_b(scale=4, num_res_blocks=32), 
                      checkpoint_dir=f'.ckpt/wdsr-b-8-x4')

# Train WDSR B model for 300,000 steps and evaluate model
# every 1000 steps on the first 10 images of the DIV2K
# validation set. Save a checkpoint only if evaluation
# PSNR has improved.
trainer.train(train_ds,
              valid_ds.take(10),
              steps=300000, 
              evaluate_every=1000, 
              save_best_only=True)

# Restore from checkpoint with highest PSNR.
trainer.restore()

# Evaluate model on full validation set.
psnr = trainer.evaluate(valid_ds)
print(f'PSNR = {psnr.numpy():3f}')

# Save weights to separate location.
trainer.model.save_weights('weights/wdsr-b-32-x4/weights.h5')

SRGAN

Generator pre-training

from model.srgan import generator
from train import SrganGeneratorTrainer

# Create a training context for the generator (SRResNet) alone.
pre_trainer = SrganGeneratorTrainer(model=generator(), checkpoint_dir=f'.ckpt/pre_generator')

# Pre-train the generator with 1,000,000 steps (100,000 works fine too). 
pre_trainer.train(train_ds, valid_ds.take(10), steps=1000000, evaluate_every=1000)

# Save weights of pre-trained generator (needed for fine-tuning with GAN).
pre_trainer.model.save_weights('weights/srgan/pre_generator.h5')

Generator fine-tuning (GAN)

from model.srgan import generator, discriminator
from train import SrganTrainer

# Create a new generator and init it with pre-trained weights.
gan_generator = generator()
gan_generator.load_weights('weights/srgan/pre_generator.h5')

# Create a training context for the GAN (generator + discriminator).
gan_trainer = SrganTrainer(generator=gan_generator, discriminator=discriminator())

# Train the GAN with 200,000 steps.
gan_trainer.train(train_ds, steps=200000)

# Save weights of generator and discriminator.
gan_trainer.generator.save_weights('weights/srgan/gan_generator.h5')
gan_trainer.discriminator.save_weights('weights/srgan/gan_discriminator.h5')

SRGAN for fine-tuning EDSR and WDSR models

It is also possible to fine-tune EDSR and WDSR x4 models with SRGAN. They can be used as drop-in replacement for the original SRGAN generator. More details in this article.

# Create EDSR generator and init with pre-trained weights
generator = edsr(scale=4, num_res_blocks=16)
generator.load_weights('weights/edsr-16-x4/weights.h5')

# Fine-tune EDSR model via SRGAN training.
gan_trainer = SrganTrainer(generator=generator, discriminator=discriminator())
gan_trainer.train(train_ds, steps=200000)
# Create WDSR B generator and init with pre-trained weights
generator = wdsr_b(scale=4, num_res_blocks=32)
generator.load_weights('weights/wdsr-b-16-32/weights.h5')

# Fine-tune WDSR B  model via SRGAN training.
gan_trainer = SrganTrainer(generator=generator, discriminator=discriminator())
gan_trainer.train(train_ds, steps=200000)

super-resolution's People

Contributors

krasserm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

super-resolution's Issues

GAN is now "tearing" my images

ISR with weights='gan' was working fine -- all models were working great -- but then recently all of my images are getting messed up with identical "stairstep" or tearing errors. I don't know what this artifact is usually called so I'm struggling to google for answers.

I've tried swapping out cv2.imread() and cv2.imwrite() for the PIL.Image.open() and PIL.Image.save() functions with no effect. It looks like either the initial numpy conversion or the predict() function are undercounting pixels, and when it gets written to a 4x image there's not enough width in a row to fill out the image. Is there some chunking code in ISR that could be broken?

cat_gan4x
cat

Train on custom dataset

First of all, thanks for sharing this repository. Very interesting!

I was wondering if it is possible to train or test the models on other datasets, like Set5 or Urban100 for instance? If so, how?

train custom datasets error

Thank you for doing a great job firstly.
I organized my dataset according to the format of DIV2K, when i trained the model with python train.py --dataset ./DIV2K_BIN --outdir ./output --profile wdsr-a-32 --scale 2, the following error occurs:

Epoch 00001: LearningRateScheduler setting learning rate to 0.0010000000474974513.
Traceback (most recent call last):
  File "train.py", line 395, in <module>
    main(args)
  File "train.py", line 182, in main
    callbacks=callbacks)
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/site-packages/keras/engine/training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/site-packages/keras/engine/training_generator.py", line 181, in fit_generator
    generator_output = next(output_generator)
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/site-packages/keras/utils/data_utils.py", line 601, in get
    six.reraise(*sys.exc_info())
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/site-packages/six.py", line 686, in reraise
    raise value
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/site-packages/keras/utils/data_utils.py", line 595, in get
    inputs = self.queue.get(block=True).get()
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/data1/wangguozhi/anaconda3/envs/sisr/lib/python3.6/site-packages/keras/utils/data_utils.py", line 401, in get_index
    return _SHARED_SEQUENCES[uid][i]
  File "/data1/wangguozhi/algrithm/super-resolution/data.py", line 75, in __getitem__
    return self._batch_n(self.image_ids[beg:end])
  File "/data1/wangguozhi/algrithm/super-resolution/data.py", line 96, in _batch_n
    hr_batch[i] = hr
ValueError: could not broadcast input array from shape (96,9,3) into shape (96,96,3)

could you offer me some suggestions?

Remove explicit padding

I am curious about the reason for padding in the test phase. What is the general purpose for this?

pad = Lambda(lambda x: K.in_train_phase(x, tf.pad(x, tf.constant([[0, 0], [2, 2], [2, 2], [0, 0]]), 'SYMMETRIC')))

And there is also this function for training def _crop_hr_in_training(hr, sr):

margin = (tf.shape(hr)[1] - tf.shape(sr)[1]) // 2

# crop only if margin > 0
hr_crop = tf.cond(tf.equal(margin, 0),
                  lambda: hr,
                  lambda: hr[:, margin:-margin, margin:-margin, :])

hr = K.in_train_phase(hr_crop, hr)
hr.uses_learning_phase = True
return hr, sr`

Where it states that

Remove margin of size scale*2 from hr in training phase. The margin is computed from size difference of hr and sr so that no explicit scale parameter is needed. This is only needed for WDSR models.

I assume these two code snippets are related. My question is why not just train och test on the same image sizes instead of removing margins and padding in different phases?

Thanks!

Getting Low value of PSNR when evaluating wdsr-a-8-x2 on Set5 dataset

The evaluation results for DIV2K validation dataset are similar to the mentioned one. But when I tried to evaluate on Set5, Set14 and other benchmark datasets, the results which I got is very low. These results should be more that 36 db of PSNR for x2 scale factor but I got nearly about 31 db of PSNR for x2 scale factor on Set5 dataset.

In your evaluation code, I just replace the DIV2K validation set images with Set5 images.

Can you help me to solve this problem?

Not working on PNGs

I get this message:

InvalidArgumentError: Incompatible shapes: [1,363,258,4] vs. [3] [Op:Sub]

When I removed the alpha channel:

InvalidArgumentError: Incompatible shapes: [1,365,260] vs. [3] [Op:Sub]

ValueError: Cannot create group in read only mode.

when i try to load the trained model using demo.py, there is an error

Traceback (most recent call last):
  File "F:/zhangqianqian/super-resolution-master/demo.py", line 78, in <module>
    main(args)
  File "F:/zhangqianqian/super-resolution-master/demo.py", line 48, in main
    model = load_model(args.model)
  File "F:\super-resolution-master\model\__init__.py", line 7, in load_model
    return train._load_model(path)
  File "F:\super-resolution-master\train.py", line 76, in _load_model
    return load_model(path, custom_objects={**_custom_objects, **_custom_objects_backwards_compat})
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\saving.py", line 419, in load_model
    model = _deserialize_model(f, custom_objects, compile)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\saving.py", line 221, in _deserialize_model
    model_config = f['model_config']
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\io_utils.py", line 302, in __getitem__
    raise ValueError('Cannot create group in read only mode.')
ValueError: Cannot create group in read only mode.

Color shift with srgan model

Hello

First of all, thank you for your work on this.
I've noticed a slight color shift on inference from srgan model.
Tried tweaking and normalize methods to modify with div2k mean values and retrain the mode, but did not improve the results.

Thank you

training my own data very slowly

Hi, kraserm, thank you for your great work.
But when I training my own data, It is very slow. I have 4303 train data, and I set iter_steps 4000 per epoch, the epoch is 300, the batch size is 32. After 20 hours, only training 2000 iterations in the one epoch.
I used one GPU (GTX1080 11G). and the GPU-util is all zero percent. so I think maybe some bottleneck in the data load. or another something leads to so slowly. Could you help me to boost the training speed? thank you very much.

Multi scale SR

Is there any way to modify your implementation for multiscale SR?

load re-trained model error

at the beginning, 'demo.py' ran successfully with pretrained model.
then, after preparing and converting data, I retrained the model by

python train.py --dataset ./DIV2K_BIN --outdir ./output --profile wdsr-a-8 --scale 2

it ran normally for 300 epochs, and I got many new h5 files. when I loaded one of new h5 files, error occurred:

TypeError: Expected float32, got {'type': 'ndarray', 'value': [114.44399999999999, 111.4605, 103.02000000000001]} of type 'dict' instead.
could you offer me some suggestions?

How to train the model for x8 scale?

Thanks for creating this repo and very helpful blog post.
I tried inference x2 and x4 of EDSR model and it is good.
I'm changing source code to train for x8 model. In the upsampling method of model/edsr.py, I change it like this
Screen Shot 2019-10-30 at 10 28 39
The training code of x8 run for 10000 step but the PSNR is just around 15. How should I update the EDSR training code for x8?
Thanks in advance.

evaluate.py requires compiled model

Hi Martin,
Thanks again for sharing your code. I'm working with a custom dataset and following your guidelines on training SRGAN with EDSR baseline model. Leaving the results aside, things appear to be working and therefore I tried doing evaluation using evaluate.py but it gives me this error

INFO: Evaluate model ./tmp/generator-epoch-001.h5
Traceback (most recent call last):
  File "evaluate.py", line 104, in <module>
    main(parser().parse_args())
  File "evaluate.py", line 60, in main
    psnr = evaluate_model(mp, generator)
  File "evaluate.py", line 39, in evaluate_model
    return model.evaluate_generator(generator, steps=100, verbose=1)[1]
  File "/home/miniconda2/envs/py36tfsr-cpu/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/miniconda2/envs/py36tfsr-cpu/lib/python3.6/site-packages/keras/engine/training.py", line 1472, in evaluate_generator
    verbose=verbose)
  File "/home/miniconda2/envs/py36tfsr-cpu/lib/python3.6/site-packages/keras/engine/training_generator.py", line 275, in evaluate_generator
    model._make_test_function()
  File "/home/miniconda2/envs/py36tfsr-cpu/lib/python3.6/site-packages/keras/engine/training.py", line 523, in _make_test_function
    raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.

Evaluation on WDSR models works fine (I patched the code to do a custom range of input indices and can do a PR if you want). Demo using EDSR models works fine too. Why does Keras complain about EDSR models and not about WDSR?

[WDSR] order of residual scaling and element-wise sum

From the paper of EDSR,

We resolve this issue by adopting the residual scaling [24] with factor 0.1. In each residual block, constant scaling layers are placed after the last convolution layers.

In this re-implemented code, it seem that scaling is placed after element-wise sum.

def res_block_a(x_in, num_filters, expansion, kernel_size, scaling):
x = Conv2D(num_filters * expansion, kernel_size, padding='same')(x_in)
x = Activation('relu')(x)
x = Conv2D(num_filters, kernel_size, padding='same')(x)
x = Add()([x_in, x])
if scaling:
x = Lambda(lambda t: t * scaling)(x)
return x
def res_block_b(x_in, num_filters, expansion, kernel_size, scaling):
linear = 0.8
x = Conv2D(num_filters * expansion, 1, padding='same')(x_in)
x = Activation('relu')(x)
x = Conv2D(int(num_filters * linear), 1, padding='same')(x)
x = Conv2D(num_filters, kernel_size, padding='same')(x)
x = Add()([x_in, x])
if scaling:
x = Lambda(lambda t: t * scaling)(x)
return x

Original WDSR PyTorch implementation:
https://github.com/JiahuiYu/wdsr_ntire2018/blob/cd2053e20579a020f609459d795656590dc98307/wdsr_b.py#L25-L28

I'm not familiar with PyTorch. I'm wondering should the residual scaling put before element-wise sum?

load_model error

hello! when i load WDSR model trained by myself ,it shows :
"NameError: name 'tf' is not defined"
in "pad = Lambda(lambda x: K.in_train_phase(x, tf.pad(x, tf.constant([[0, 0], [2, 2], [2, 2], [0, 0]]), 'SYMMETRIC')))"

Issues trying to reproduce results

Hello,

I am trying to run the sran notebook only for evaluation and I get the error shown below. I think that the error is that images = [lr, pre_sr, gan_sr] computed from

pre_sr = resolve_single(pre_generator, lr)
gan_sr = resolve_single(gan_generator, lr)

return Tensor datatype instead of numpy array and imshow function expect a numpy array. In that case, in the sran notebook I don't understand why the output from resulve_single function is not converted to numpy array and more important why it is working in the notebook shown in the github.


TypeError Traceback (most recent call last)
in
----> 1 resolve_and_plot('demo/0869x4-crop.png')

in resolve_and_plot(lr_image_path)
16 for i, (img, title, pos) in enumerate(zip(images, titles, positions)):
17 plt.subplot(2, 2, pos)
---> 18 plt.imshow(img)
19 plt.title(title)
20 plt.xticks([])

~/anaconda2/envs/keras/lib/python3.6/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, data, **kwargs)
2699 filternorm=filternorm, filterrad=filterrad, imlim=imlim,
2700 resample=resample, url=url, **({"data": data} if data is not
-> 2701 None else {}), **kwargs)
2702 sci(__ret)
2703 return __ret

~/anaconda2/envs/keras/lib/python3.6/site-packages/matplotlib/init.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.name),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.doc = _add_data_doc(inner.doc,

~/anaconda2/envs/keras/lib/python3.6/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
5492 resample=resample, **kwargs)
5493
-> 5494 im.set_data(X)
5495 im.set_alpha(alpha)
5496 if im.get_clip_path() is None:

~/anaconda2/envs/keras/lib/python3.6/site-packages/matplotlib/image.py in set_data(self, A)
640 if (self._A.dtype != np.uint8 and
641 not np.can_cast(self._A.dtype, float, "same_kind")):
--> 642 raise TypeError("Image data cannot be converted to float")
643
644 if not (self._A.ndim == 2

TypeError: Image data cannot be converted to float

Evaluation on RGB or YCbCr

I have a question regarding the evaluation of the models. In this implementation, just as in the EDSR and the WDSR papers, the models are evaluated with PSNR and SSIM on all three RGB-channels. However, in other papers, I have seen the evaluation being performed by converting the images from RGB to YCbCr and only evaluating the Y-channel (luminance).

So I guess I have two questions.

  1. Do you know why different methods are used and if so if there are any pros/cons of the two approaches?
  2. Do you know if the methods differ on the PSNR/SSIM score?

Thanks in advance!

train error

Traceback (most recent call last):
File "train_EDSR.py", line 39, in
valid_ds.take(10),
AttributeError: 'NoneType' object has no attribute 'take'

Support loading weights by name

In order to use model.load_weights(..., by_name=True) model layers should be named appropriately. Main use case is to initialize weights of x4 models with pre-trained weights from x2 models.

Training WDSR on custom input/output sizes

Thanks for this OS implementation! The results look pretty amazing.
If I wanted to train a model for a custom super resolution scale, say 3.4x, would I be able to the following?

  1. Create a dataset of downscaled images and 3.4x larger HR images structured as you describe here
  2. Modify checks that scale be one of [2, 3, 4] in places like data.py and train.py
  3. Run train.py with the argument --scale 3.4

how can run

Hello!!

i try to Xilinx alveo card used by user code. but, Xilinx was recommended to run with inference mode not training mode. So, I modified to set to tf.keras.backend.set_learning_phase(1) into the ./optimizer/weightnorm.py. is it right?

would you pls advice to set of inference mode runing for your code.

the below is Xilinx msg

check that “is_training” flag is set to False before freezing the network?

How to get the frozen inference graph?
Frozen inference graph:
Variables are frozen to Constants
Graph and weights are in one single file
Only inference phase graph should be kept to make the graph clear for pattern matching
Set phase to eval/inference the graph before freezing
For tf.layers.batchnorm/dropout, set is_training to False
For tf.keras.layers.BatchNorm/Dropout, call tf.keras.backend.set_learning_phase(0) # 0 for test, 1 for train

Discriminator loss goes to zero

Hi krasserm,

I am training the argan model with slight change, instead of using gradient.tape() inside train loop, I am using tf.keras "train_on_batch" .

I am also doing one sided label smoothing for discriminator training.

During training , fake loss of discriminator goes close zero after few steps ,generator adversial loss also fluctuates around a small value(0.4~0.6, is that even possible given that discriminator is doing a very good job?) and only the vgg content loss decreases, which also stops going down after stabilizing and there is no further improvement in image after that.

All the hyper parameters values are same. Can you think of any reason for this? Any suggestions?

indentation

gradients = tape.gradient(loss_value, self.checkpoint.model.trainable_variables)

should this line be one more level indented as tape exists in with context?

Training SRResNet with scale=2 not possible

Hi,
I'm trying to train a SR-ResNet for scale = 2, but unfortunately this always fails. As I see it, the SR-ResNet implementation only works for scaling factor = 4. Removing line 49 in srgan.py (x = upsample(x, num_filters)) fixes this issue.
Is there a special reason that the model only works for this scaling factor? Or is this just a "forgotten" feature? :-)
And do I have to pay attention if I take factor 2? I wanted to use SR-ResNet as my base model, as this is also stated in many papers as a baseline network.

error in training srgan

While running this function in file -------'example-srgan.ipynb'-----
gan_trainer.train(train_ds, steps=200000)

AttributeError: 'Operation' object has no attribute '_graph'

NotImplementedError: Cannot convert a symbolic Tensor (Cast_80:0) to a numpy array.

When I am running the examples, attained super resolution image can not be show because it is a symbolic Tensor. The detail error is given as the following:

NotImplementedError Traceback (most recent call last)
in ()
9 lr = load_image('superresolution/demo/0851x4-crop.png')
10 sr = resolve_single(model, lr)
---> 11 plot_sample(lr,sr)
12
13 type(sr)

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in array(self)
734 def array(self):
735 raise NotImplementedError("Cannot convert a symbolic Tensor ({}) to a numpy"
--> 736 " array.".format(self.name))
737
738 def len(self):

NotImplementedError: Cannot convert a symbolic Tensor (Cast_80:0) to a numpy array.

Computation

Can you mention the gpu u used to train this and how long.

convert the keras h5 model to tflite error

@krasserm HI, thanks for your great work. the project give much help to me. Now I want to convert the Keras h5 model to tflite. And I will deploy the model on Andriod. but when I used
'converter =tf.lite.TFLiteConverter.from_keras_model_file('XXXXX.h5')
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)`
that error,
Traceback (most recent call last):
File "h5totflite.py", line 4, in
converter =tf.lite.TFLiteConverter.from_keras_model_file(model_path)
File "/home//anaconda2/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/lite/python/lite.py", line 747, in from_keras_model_file
keras_model = _keras.models.load_model(model_file, custom_objects)
File "/home/
/anaconda2/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/saving/save.py", line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "/home//anaconda2/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 227, in load_model_from_hdf5
optimizer_config, custom_objects=custom_objects)
File "/home/
/anaconda2/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/optimizers.py", line 815, in deserialize
printable_module_name='optimizer')
File "/home//anaconda2/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 181, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "/home/
/anaconda2/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 166, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown optimizer: AdamWithWeightnorm

Could you help me to resolve that? thank you very much. I used the super-resolution version is TensorFlow 1.x.

Training on UC Merced dataset

So I swapped out images from the .div2k directory with 800 images of UC Merced Dataset(converted .tif to .png and downsampled by 0.25 scale using Matlab imresize) in train and 100 image for test directory. However, during pre-training I get the following error
InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [93,96,3], [batch]: [96,96,3]
Kindly help me figure out the problem. My guess is after cropping some pixels from the 96x96 are getting omitted or not being read.

Training train_step freezes when using @tf.function

I tried training on a custom dataset, but the train function always got stuck in train_step at the return statement. After spending 2 hours to understand why the function is called twice without ever returning and why it gets stuck I realized it's because of the @tf.function decorator. As soon as I removed that decorator the training worked as expected.

Why does that decorator make the train_step function get stuck? Is it safe to remove it? Is it something wrong with the function that makes it incompatible with @tf.cunction ?

@tf.function
def train_step(self, lr, hr):
with tf.GradientTape() as tape:
lr = tf.cast(lr, tf.float32)
hr = tf.cast(hr, tf.float32)
sr = self.checkpoint.model(lr, training=True)
loss_value = self.loss(hr, sr)
gradients = tape.gradient(loss_value, self.checkpoint.model.trainable_variables)
self.checkpoint.optimizer.apply_gradients(zip(gradients, self.checkpoint.model.trainable_variables))
return loss_value

load_model error in demo.py

I tried run demo.py with this model but this error occurred :

D:\Artificial Intelligence\SuperResolution\Images\wdsr>python demo.py
Using TensorFlow backend.
WARNING: Logging before flag parsing goes to stderr.
W0817 19:36:48.204280  8632 deprecation_wrapper.py:119] From D:\Artificial Intelligence\SuperResolution\Images\wdsr\util.py:30: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

W0817 19:36:48.204280  8632 deprecation_wrapper.py:119] From D:\Artificial Intelligence\SuperResolution\Images\wdsr\util.py:33: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2019-08-17 19:36:48.228737: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-08-17 19:36:48.238670: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library nvcuda.dll
2019-08-17 19:36:48.860316: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:01:00.0
2019-08-17 19:36:48.868030: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-08-17 19:36:48.874822: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2019-08-17 19:36:50.439236: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-08-17 19:36:50.445774: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187]      0
2019-08-17 19:36:50.448722: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0:   N
2019-08-17 19:36:50.453298: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3276 MB memory) -> physical GPU (device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0)
Began Load Image
W0817 19:36:50.487827  8632 deprecation_wrapper.py:119] From C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

Traceback (most recent call last):
  File "demo.py", line 73, in <module>
    main(args)
  File "demo.py", line 41, in main
    model = load_model(args.model)
  File "D:\Artificial Intelligence\SuperResolution\Images\wdsr\model\__init__.py", line 7, in load_model
    return train._load_model(path)
  File "D:\Artificial Intelligence\SuperResolution\Images\wdsr\train.py", line 76, in _load_model
    return load_model(path, custom_objects={**_custom_objects, **_custom_objects_backwards_compat})
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\saving.py", line 419, in load_model
    model = _deserialize_model(f, custom_objects, compile)
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\saving.py", line 225, in _deserialize_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\saving.py", line 458, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\network.py", line 1032, in from_config
    process_node(layer, node_data)
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\network.py", line 991, in process_node
    layer(unpack_singleton(input_tensors), **kwargs)
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\layers\core.py", line 687, in call
    return self.function(inputs, **arguments)
  File "/home/martin/Development/extern/wdsr/model/common.py", line 14, in <lambda>
IndexError: tuple index out of range

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.