使用像素转置卷积层进行上采样

时间:2019-06-10 20:07:54

标签: python tensorflow keras aws-lambda

我正在运行GAN模型以对图像进行升采样2倍。我想使用https://github.com/HongyangGao/PixelTCN处的像素转置卷积层。

我尝试使用keras Lambda创建自定义图层,但出现以下错误:

tensorflow.python.framework.errors_impl.FailedPreconditionError:尝试使用未初始化的值1 / conv2 / weights          [[{{node 1 / conv2 / weights / read}}]]          [[{{node activation / Tanh}}]]

这是Lambda层

def pixel_dcl(inputs, out_num, kernel_size, scope, scale, activation_fn=tf.nn.leaky_relu, d_format='NHWC'):
    def pixel_shape(inputs):
        dims = [inputs[0],inputs[1] * scale,inputs[2] * scale,int(inputs[3] / (scale ** 2))]
        output_shape = tuple(dims)
        return output_shape

    def conv2d(inputs, out_num, kernel_size, scope, stride=1, d_format='NHWC'):
        outputs = tf.contrib.layers.conv2d(inputs, out_num, kernel_size, scope=scope, stride=stride, data_format=d_format, activation_fn=None, biases_initializer=None, reuse=tf.AUTO_REUSE)
        return outputs

    def dilate_tensor(inputs, axes, shifts, scope):
        for index, axis in enumerate(axes):
            eles = tf.unstack(inputs, axis=axis, name=scope+'/unstack%s' % index)
            zeros = tf.zeros_like(eles[0], dtype=tf.float32, name=scope+'/zeros%s' % index)
            for ele_index in range(len(eles), 0, -1):
                eles.insert(ele_index-shifts[index], zeros)
            inputs = tf.stack(eles, axis=axis, name=scope+'/stack%s' % index)
        return inputs

    def get_mask(shape, scope):
        new_shape = (np.prod(shape[:-2]), shape[-2], shape[-1])
        mask = np.ones(new_shape, dtype=np.float32)
        for i in range(0, new_shape[0], 2):
            mask[i, :, :] = 0
        mask = np.reshape(mask, shape, 'F')
        return tf.constant(mask, dtype=tf.float32, name=scope+'/mask')

    def pixel(x):
        """
        inputs: input tensor
        out_num: output channel number
        kernel_size: convolutional kernel size
        scope: operation scope
        activation_fn: activation function, could be None if needed
        """
        axis = (d_format.index('H'), d_format.index('W'))
        conv0 = conv2d(x, out_num, kernel_size,
                   scope+'/conv0', d_format=d_format)
        conv1 = conv2d(conv0, out_num, kernel_size,
                   scope+'/conv1', d_format=d_format)
        dilated_conv0 = dilate_tensor(conv0, axis, (0, 0), scope+'/dialte_conv0')
        dilated_conv1 = dilate_tensor(conv1, axis, (1, 1), scope+'/dialte_conv1')
        conv1 = tf.add(dilated_conv0, dilated_conv1, scope+'/add1')
        with tf.variable_scope(scope+'/conv2', reuse=tf.AUTO_REUSE):
            shape = list(kernel_size) + [out_num, out_num]
            weights = tf.get_variable(
                'weights', shape, initializer=tf.truncated_normal_initializer())
            weights = tf.multiply(weights, get_mask(shape, scope))
            strides = [1, 1, 1, 1]
            conv2 = tf.nn.conv2d(conv1, weights, strides, padding='SAME',
                             data_format=d_format)

        outputs = tf.add(conv1, conv2, name=scope+'/add2')
        if activation_fn:
         outputs = activation_fn(outputs, alpha=0.2)
        return outputs

    return keras.layers.Lambda(pixel, output_shape=pixel_shape)

这是我叫它的地方

def up_sampling_block(model, out_num, kernel_size, scale, scope):

    model = Utils.pixel_dcl(model, out_num, kernel_size, str(scope), scale)(model)

    return model

当预测发电机的输出而不是编译发电机的输出时,会出现此错误。

0 个答案:

没有答案