# 图像处理：利用神经网络生成新图像和修复旧图像

`pip install tensorflow`

## PixelRNN生成模型

`\$\$x_i \sim p(x_i | x_1, x_2, \cdots, x_{i-1})\$\$`

`\$\$p(x)= \prod_{i=1}^{n^2} p(x_i | x_1, \cdots, x_{i-1})\$\$`

## 一种更快的方法——同时计算多个特征

```def conv2d(
inputs,
num_outputs,
kernel_shape,# [kernel_height, kernel_width]
mask_type,# None, "A" or "B",
strides=[1,1],# [column_wise_stride, row_wise_stride]
activation_fn=None,
weights_initializer=tf.contrib.layers.xavier_initializer(),
weights_regularizer=None,
biases_initializer=tf.zeros_initializer,
biases_regularizer=None,
scope="conv2d"):
with tf.variable_scope(scope):
batch_size, height, width, channel= inputs.get_shape().as_list()
kernel_h, kernel_w= kernel_shape
stride_h, stride_w= strides

center_h= kernel_h// 2
center_w= kernel_w// 2```

```weights_shape= [kernel_h, kernel_w, channel, num_outputs]
weights= tf.get_variable("weights", weights_shape,
tf.float32, weights_initializer, weights_regularizer)```

```if mask_typeis not None:
(kernel_h, kernel_w, channel, num_outputs), dtype=np.float32)

mask[center_h, center_w+1: ,: ,:]= 0.
mask[center_h+1:, :, :, :]= 0.

```outputs= tf.nn.conv2d(inputs,

if biases_initializer != None:
biases= tf.get_variable("biases", [num_outputs,],
tf.float32, biases_initializer, biases_regularizer)
outputs= tf.nn.bias_add(outputs, biases, name='outputs_plus_b')

if activation_fn:
outputs= activation_fn(outputs, name='outputs_with_fn')
return outputs```

## 神经网络的特性

```def pixelRNN(height, width, channel, params):
"""
Args
height, width, channel - the dimensions of the input
params the hyperparameters of the network
"""
input_shape= [None, height, width, channel]
inputs= tf.placeholder(tf.float32, input_shape)```

```# input of main recurrent layers
scope= "conv_inputs"
conv_inputs= conv2d(inputs, params.hidden_dims, [7,7],"A", scope=scope)```

```# main recurrent layers
last_hid= conv_inputs
for idxin xrange(params.recurrent_length):
scope= 'CONV%d' % idx
last_hid= conv2d(last_hid,3, [1,1],"B", scope=scope)
print("Building %s" % scope)

# output recurrent layers
for idxin xrange(params.out_recurrent_length):
scope= 'CONV_OUT%d' % idx
last_hid= tf.nn.relu(conv2d(last_hid, params.out_hidden_dims, [1,1],"B", scope=scope))
print("Building %s" % scope)```

```    conv2d_out_logits= conv2d(last_hid,1, [1,1],"B", scope='conv2d_out_logits')
output= tf.nn.sigmoid(conv2d_out_logits)
return inputs, output, conv2d_out_logits
inputs, output, conv2d_out_logits= pixelRNN(height, width, channel, p)```

## 训练过程

```loss= tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(conv2d_out_logits, inputs, name='loss'))

optimizer= tf.train.RMSPropOptimizer(p.learning_rate)

## 生成和遮挡完成

```def predict(sess, images, inputs, output):
return sess.run(output, {inputs: images})

def generate(sess, height, width, inputs, output):
samples= np.zeros((100, height, width,1), dtype='float32')

for iin range(height):
for jin range(width):
next_sample= binarize(predict(sess, samples, inputs, output))
samples[:, i, j]= next_sample[:, i, j]

return samples

def generate_occlusions(sess, height, width, inputs, output):
samples= occlude(images, height, width)
starting_position= [0,height//2]
for iin range(starting_position[1], height):
for jin range(starting_position[0], width):
next_sample= binarize(predict(sess, samples, inputs, output))
samples[:, i, j]= next_sample[:, i, j]
return samples```

1477 篇文章76 人订阅

0 条评论

## 相关文章

### TensorFlow从0到1 - 7 - TensorFlow线性回归的参数溢出之坑

? 上一篇 6 解锁梯度下降算法解释清楚了学习率（learning rate）。本篇基于对梯度下降算法和学习率的理解，去填下之前在线性回归中发现的一个坑。 ...

3527

971

3928

31910

1612

41512

### 目标检测（CVPR2017）：Feature Pyramid Networks

（a）使用图像金字塔构建特征金字塔网络：每个图像尺度上的特征都是独立计算的，速度缓慢。

1143

3538

975

4144