0

I am using a source code file named SRGAN. This will 4x upscale photos. https://github.com/tensorlayer/srgan

However, I wanted to 2x upscale the photos. The developer asked to remove one subpixel as a way to do that. https://github.com/tensorlayer/srgan/issues/20

So I followed the way he gave.

def SRGAN_g(t_image, is_train=False, reuse=False):
""" Generator in Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network
feature maps (n) and stride (s) feature maps (n) and stride (s)
"""
w_init = tf.random_normal_initializer(stddev=0.02)
b_init = None  # tf.constant_initializer(value=0.0)
g_init = tf.random_normal_initializer(1., 0.02)
with tf.variable_scope("SRGAN_g", reuse=reuse) as vs:
    # tl.layers.set_name_reuse(reuse) # remove for TL 1.8.0+
    n = InputLayer(t_image, name='in')
    n = Conv2d(n, 64, (3, 3), (1, 1), act=tf.nn.relu, padding='SAME', W_init=w_init, name='n64s1/c')
    temp = n

    # B residual blocks
    for i in range(16):
        nn = Conv2d(n, 64, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, b_init=b_init, name='n64s1/c1/%s' % i)
        nn = BatchNormLayer(nn, act=tf.nn.relu, is_train=is_train, gamma_init=g_init, name='n64s1/b1/%s' % i)
        nn = Conv2d(nn, 64, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, b_init=b_init, name='n64s1/c2/%s' % i)
        nn = BatchNormLayer(nn, is_train=is_train, gamma_init=g_init, name='n64s1/b2/%s' % i)
        nn = ElementwiseLayer([n, nn], tf.add, name='b_residual_add/%s' % i)
        n = nn

    n = Conv2d(n, 64, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, b_init=b_init, name='n64s1/c/m')
    n = BatchNormLayer(n, is_train=is_train, gamma_init=g_init, name='n64s1/b/m')
    n = ElementwiseLayer([n, temp], tf.add, name='add3')
    # B residual blacks end

    n = Conv2d(n, 256, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='n256s1/1')
    n = SubpixelConv2d(n, scale=2, n_out_channel=None, act=tf.nn.relu, name='pixelshufflerx2/1')

    n = Conv2d(n, 256, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='n256s1/2')
    n = SubpixelConv2d(n, scale=2, n_out_channel=None, act=tf.nn.relu, name='pixelshufflerx2/2')

    n = Conv2d(n, 3, (1, 1), (1, 1), act=tf.nn.tanh, padding='SAME', W_init=w_init, name='out')
    return n

This is the original source code. and

def SRGAN_g(t_image, is_train=False, reuse=False):
""" Generator in Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network
feature maps (n) and stride (s) feature maps (n) and stride (s)
"""
w_init = tf.random_normal_initializer(stddev=0.02)
b_init = None  # tf.constant_initializer(value=0.0)
g_init = tf.random_normal_initializer(1., 0.02)
with tf.variable_scope("SRGAN_g", reuse=reuse) as vs:
    # tl.layers.set_name_reuse(reuse) # remove for TL 1.8.0+
    n = InputLayer(t_image, name='in')
    n = Conv2d(n, 64, (3, 3), (1, 1), act=tf.nn.relu, padding='SAME', W_init=w_init, name='n64s1/c')
    temp = n

    # B residual blocks
    for i in range(16):
        nn = Conv2d(n, 64, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, b_init=b_init, name='n64s1/c1/%s' % i)
        nn = BatchNormLayer(nn, act=tf.nn.relu, is_train=is_train, gamma_init=g_init, name='n64s1/b1/%s' % i)
        nn = Conv2d(nn, 64, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, b_init=b_init, name='n64s1/c2/%s' % i)
        nn = BatchNormLayer(nn, is_train=is_train, gamma_init=g_init, name='n64s1/b2/%s' % i)
        nn = ElementwiseLayer([n, nn], tf.add, name='b_residual_add/%s' % i)
        n = nn

    n = Conv2d(n, 64, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, b_init=b_init, name='n64s1/c/m')
    n = BatchNormLayer(n, is_train=is_train, gamma_init=g_init, name='n64s1/b/m')
    n = ElementwiseLayer([n, temp], tf.add, name='add3')
    # B residual blacks end

    n = Conv2d(n, 256, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='n256s1/1')
    n = SubpixelConv2d(n, scale=2, n_out_channel=None, act=tf.nn.relu, name='pixelshufflerx2/1')

    n = Conv2d(n, 256, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='n256s1/2')

    n = Conv2d(n, 3, (1, 1), (1, 1), act=tf.nn.tanh, padding='SAME', W_init=w_init, name='out')
    return n

I deleted one subpixel block as he said. Then the following error occurred :

ValueError: Dimension 2 in both shapes must be equal, but are 256 and 64. Shapes are [1,1,256,3] and [1,1,64,3]. for 'Assign_171' (op: 'Assign') with input shapes: [1,1,256,3], [1,1,64,3].

How do I resolve this error?

Jonathan DEKHTIAR
  • 3,456
  • 1
  • 21
  • 42

1 Answers1

1

The problem is that you are (probably) trying to initialize the new network(x2) with parameters from the old one (x4). This doesn't work because, as the error states, the dimensions aren't compatible.

There should be a similar line to this one that is causing the error.

tl.files.load_and_assign_npz(sess=sess, name='model.npz', network=network)

To fix this you have to retrain the network with the new scaling factor and use the new model for evaluation.