• 大小: 12.32MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-25
  • 语言: 其他
  • 标签: autoencoder  CNN  

资源简介

tensorflow下构建三层卷积层,三层反卷积层实现卷积自编码,针对系数为0.5的高斯噪声亦有较好效果,可通过tensorboard查看输入输出图像

资源截图

代码片段和文件信息

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
import utils
mnist = input_data.read_data_sets(“MNIST_data/“ one_hot=True)
sess = tf.InteractiveSession()
batch_size = 256
noise_scale = 0.5
def weights_variable(shape):
    initial = tf.truncated_normal(shape stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1 shape=shape)
    return  tf.Variable(initial)

#strides 第一个参数:batch上的步长,第二个:height上的步长,第三个:weights上的步长,第四个:channel上的步长
def conv2d(x W):
    return tf.nn.conv2d(x W strides=[1 1 1 1] padding=‘SAME‘)

def deconv2d(x W output):
    return tf.nn.conv2d_transpose(x W output strides=[1 2 2 1] padding=‘SAME‘)
#ksize 第一个参数:batch上的池化,第二个:height上的池化,第三个:weights上的池化,第四个:channel上的池化
def max_pool_2x2(x):
    return tf.nn.max_pool(x ksize=[1 2 2 1] strides=[1 2 2 1] padding=‘SAME‘)


x = tf.placeholder(tf.float32 [None 784] name=‘input‘)
x_noise = tf.placeholder(tf.float32 [None 784] name=‘input‘)
#y_ = tf.placeholder(tf.float32 [None 10])
#-1代表样本数量不固定,最后的1代表颜色通道数量
x_image = tf.reshape(x [-1 28 28 1])
x_image_noise = tf.reshape(x_noise [-1 28 28 1])
#编码
W_conv1 = weights_variable([3 3 1 64])
b_conv1 = bias_variable([64])
h_conv1 = tf.nn.relu(conv2d(x_image_noise W_conv1) + b_conv1 name=‘noise_layer‘)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weights_variable([3 3 64 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1 W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_conv3 = weights_variable([3 3 64 32])
b_conv3 = bias_variable([32])
h_conv3 = tf.nn.relu(conv2d(h_pool2 W_conv3) + b_conv3)
h_pool3 = max_pool_2x2(h_conv3)
#解码
W_deconv1 = weights_variable([3 3 32 32])
h_deconv1 = deconv2d(h_pool3 W_deconv1 [batch_size 7 7 32])
W_deconv2 = weights_variable([3 3 64 32])
h_deconv2 = deconv2d(h_deconv1 W_deconv2 [batch_size 14 14 64])
W_deconv3 = weights_variable([3 3 64 64])
h_deconv3 = deconv2d(h_deconv2 W_deconv3 [batch_size 28 28 64])
#卷积层
W_conv_final = weights_variable([3 3 64 1])
b_conv_final = bias_variable([1])
h_conv_final = tf.nn.bias_add(conv2d(h_deconv3 W_conv_final) b_conv_final name=‘output_layer‘)

#输出图像

output_img = tf.reshape(h_conv_final shape=[-1 28 28 1])
output_img_fomat = utils.convert2int(output_img)
output = tf.reshape(h_conv_final shape=[-1 784])
input = tf.reshape(x shape=[-1 784])
#训练
cost = tf.reduce_mean(tf.pow(tf.subtract(output input) 2.0)) #计算平方误差
train_steps = tf.train.AdamOptimizer(0.001).minimize(cost)
with tf.name_scope(‘images‘):
    tf.summary.image(‘input‘ x_image 1)
    tf.summary.image(‘gaussian‘ x_image_noise 1)
    tf.summary.image(‘reconstruction‘ (output_img_fomat) 1)

merged = tf.summary.merge_all()

n_samples = int(mnist.train.num_examples)
print(‘train samples: %d‘ % n_samples)
print(‘batch size: %d‘ % batch_size)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-11-30 15:36  6_ConvAutoEncoder\
     文件        4245  2018-11-17 15:56  6_ConvAutoEncoder\6.py
     目录           0  2018-11-15 19:45  6_ConvAutoEncoder\MNIST_data\
     文件     1648877  2018-11-15 19:45  6_ConvAutoEncoder\MNIST_data\t10k-images-idx3-ubyte.gz
     文件        4542  2018-11-15 19:45  6_ConvAutoEncoder\MNIST_data\t10k-labels-idx1-ubyte.gz
     文件     9912422  2018-11-15 19:45  6_ConvAutoEncoder\MNIST_data\train-images-idx3-ubyte.gz
     文件       28881  2018-11-15 19:45  6_ConvAutoEncoder\MNIST_data\train-labels-idx1-ubyte.gz
     目录           0  2018-11-17 15:49  6_ConvAutoEncoder\__pycache__\
     文件        2219  2018-11-17 15:49  6_ConvAutoEncoder\__pycache__\utils.cpython-36.pyc
     目录           0  2018-11-17 16:03  6_ConvAutoEncoder\checkpoint_dir\
     文件     1453460  2018-11-17 16:03  6_ConvAutoEncoder\checkpoint_dir\CAEmodel.data-00000-of-00001
     文件        1290  2018-11-17 16:03  6_ConvAutoEncoder\checkpoint_dir\CAEmodel.index
     文件       84899  2018-11-17 16:03  6_ConvAutoEncoder\checkpoint_dir\CAEmodel.meta
     文件          73  2018-11-17 16:03  6_ConvAutoEncoder\checkpoint_dir\checkpoint
     目录           0  2018-11-15 20:17  6_ConvAutoEncoder\logs\
     目录           0  2018-11-15 20:17  6_ConvAutoEncoder\logs\mnist_with_summaries\
     目录           0  2018-11-30 15:39  6_ConvAutoEncoder\logs\mnist_with_summaries\test\
     目录           0  2018-11-30 15:39  6_ConvAutoEncoder\pred\
     文件        1735  2018-11-17 15:49  6_ConvAutoEncoder\utils.py

评论

共有 条评论