我已经使用tensorflow构建了全卷积自动编码器,自动编码器可以很好地重建图像,但是在每个图像中,最后三行和最后三列都是灰色的,无论给定图像中的最后三行和列(以像素为单位)图片保持不变。我已经在CIFAR-10数据集上训练了我的模型。 以下是图片:Input Images and predicted images
我检查了像素值,这三行和三列中的大多数像素都包含126个值。 这是像素值图像image of pixel values of two different values 如您所见,其余图像重建得很好,无论我通过自动编码器的图像如何,这三行和三列都保持不变。
这是型号代码:
input_images = tf.compat.v1.placeholder(tf.float32, shape=[None, 32, 32,3], name='input_images')
############################### Buliding Encoder ##############################
#1st Convoultional layer
conv1 = conv2d_layer(input_images, input_channels=3, filters=16, kernel_size=5, stride=2, padding='VALID', name='CONV1')
prelu1 = PReLU(conv1, name='PReLu1', var_name='preluV1')
#2nd Convoultional layer
conv2 = conv2d_layer(prelu1, input_channels=16, filters=80, kernel_size=5, stride=2, padding='VALID', name='CONV2')
prelu2 = PReLU(conv2, name='PReLu2', var_name='preluV2')
#3rd Convoultional layer
conv3 = conv2d_layer(prelu2, input_channels=80, filters=50, kernel_size=5, stride=1, padding='SAME', name='CONV3')
prelu3 = PReLU(conv3, name='PReLu3', var_name='preluV3')
#4th Convoultional layer
conv4 = conv2d_layer(prelu3, input_channels=50, filters=40, kernel_size=5, stride=1, padding='SAME', name='CONV4')
prelu4 = PReLU(conv4, name='PReLu4', var_name='preluV4')
#5th Convoultional layer
conv5 = conv2d_layer(prelu4, input_channels=40, filters=32, kernel_size=5, stride=1, padding='SAME', name='CONV5')
z_tilta = PReLU(conv5, name='PReLu5', var_name='preluV5')
Dconv1 = conv2dTranspose_layer(z_tilta, input_channels=32, filters=40, kernel_size=5, output_shape = tf.shape(conv4),
stride=1, padding='SAME', name='CONVTranspose1')
prelu6 = PReLU(Dconv1, name='PReLu6', var_name='preluV6')
#2nd Deconvolutional layer
Dconv2 = conv2dTranspose_layer(prelu6, input_channels=40, filters=50, kernel_size=5, output_shape = tf.shape(conv3),
stride=1, padding='SAME', name='CONVTranspose2')
prelu7 = PReLU(Dconv2, name='PReLu7', var_name='preluV7')
#3rd Deconvolutional layer
Dconv3 = conv2dTranspose_layer(prelu7, input_channels=50, filters=80, kernel_size=5, output_shape = tf.shape(conv2),
stride=1, padding='SAME', name='CONVTranspose3')
prelu8 = PReLU(Dconv3, name='PReLu8', var_name='preluV8')
#4th Deconvolutional layer
Dconv4 = conv2dTranspose_layer(prelu8, input_channels=80, filters=16, kernel_size=5, output_shape = tf.shape(conv1),
stride=2, padding='VALID', name='CONVTranspose4')
prelu9 = PReLU(Dconv4, name='PReLu9', var_name='preluV9')
#5th Deconvolutional layer
decoded = conv2dTranspose_layer(prelu9, input_channels=16, filters=3, kernel_size=5, output_shape = tf.shape(input_images),
stride=2, padding='VALID', name='CONVTranspose5', activation='sigmoid')
模型是在tensorflow 1.14中构建的,为什么我不使用Keras或tf.Keras,因为我想添加一些包含复数和keras和tf的自定义层,所以Keras不处理复数。 上面使用的功能的代码:
def conv2d_layer(input, input_channels, filters, kernel_size, stride, padding, name):
with tf.name_scope(name):
# Shape of the filter-weights for the convolution
"""For the tf.nn.conv_2d the filter variable should be - A 4-D tensor of shape
[filter_height, filter_width, in_channels, out_channels/filters] """
shape = [kernel_size, kernel_size, input_channels, filters]
#print(shape)
# Create new weights (filters) with the given shape
weights = tf.Variable(tf.random.truncated_normal(shape, stddev=0.05), name='W')
# Create new biases, one for each filter
biases = tf.Variable(tf.constant(0.05, shape=[filters]), name='b')
# TensorFlow operation for convolution
layer = tf.nn.conv2d(input=input, filter=weights, strides=stride, padding=padding, name='nn.conv2d')
# Add the biases to the results of the convolution.
layer += biases
#tf.compat.v1.summary.histogram('weights', weights)
#tf.compat.v1.summary.histogram('biases', biases)
return layer
def conv2dTranspose_layer(input, input_channels, filters, kernel_size, output_shape, stride, padding, name, activation=None):
with tf.name_scope(name):
# Shape of the filter-weights for the convolution
'''For the tf.nn.conv_2d_transpose the filter variable should be - A 4-D tensor of shape
[filter_height, filter_width, out_channels,in_channels] '''
#shape = [kernel_size, kernel_size, input_channels, filters] #that line was causing error reference in [2]
shape = [kernel_size, kernel_size, filters, input_channels]
#print(shape)
# Create new weights (filters) with the given shape
weights = tf.Variable(tf.random.truncated_normal(shape, stddev=0.05), name='W')
# Create new biases, one for each filter
biases = tf.Variable(tf.constant(0.05, shape=[filters]), name='b')
# TensorFlow operation for convolution
layer = tf.nn.conv2d_transpose(input=input, filter=weights, output_shape=output_shape,
strides=stride, padding=padding, name='nn.conv2d_transpose')
#layer = tf.compat.v1.layers.conv2d_transpose(input, filters=weights, kernel_size=kernel_size, strides=stride, padding=padding)
# Add the biases to the results of the convolution.
layer += biases
if activation == 'sigmoid':
return tf.nn.sigmoid(layer, name='sigmoid')
elif activation == None:
return layer
else:
raise ValueError('The value of activation argument must be None or sigmoid as a string')
def PReLU(_x, alpha_value=0.1, name='PReLU', var_name='preluV', complx=False):
with tf.name_scope(name):
alphas = tf.compat.v1.get_variable(var_name, shape=_x.get_shape()[-1],
dtype=_x.dtype, initializer=tf.constant_initializer(alpha_value))
act = tf.maximum(0.0, _x) + alphas * tf.minimum(0.0, _x)
if complx:
return tf.dtypes.cast(act, dtype='complex128', name='ComplexCasting')
elif not complx:
return act
else:
raise ValueError('The value must be boolean i.e True or False')