2017-03-10 14 views
1

tensorflow를 사용하여 디콘 볼 루션 네트워크를 구축하려고합니다.tensorflow conv2d_transpose gradient

여기 내 코드입니다.

def decoder(self, activations): 
    with tf.variable_scope("Decoder") as scope: 

     h0 = conv2d(activations, 128, name = "d_h0_conv_1") 
     h0 = lrelu(h0) 
     shape = activations.get_shape().as_list() 
     h0 = deconv2d(h0, [shape[0], 2 * shape[1], 2 * shape[2], 128], name = "d_h0_deconv_1") 
     h0 = lrelu(h0) 

     h1 = conv2d(h0, 128, name = "d_h1_conv_1") 
     h1 = lrelu(h1) 
     h1 = conv2d(h1, 64, name = "d_h1_conv_2") 
     h1 = lrelu(h1) 
     shape = h1.get_shape().as_list() 
     h1 = deconv2d(h1, [shape[0], 2 * shape[1], 2 * shape[2], 64], name = "d_h1_deconv_1") 
     h1 = lrelu(h1) 

     h2 = conv2d(h1, 64, name = "d_h2_conv_1") 
     h2 = lrelu(h2) 
     h2 = conv2d(h2, 3, name = "d_h2_conv_2") 

     output = h2 
     print shape 


    return output 

매개 변수 활성화는 기본적으로 VGG19 네트워크에서 활성화됩니다. 여기

는 deconv2d() 함수

def deconv2d(input_, output_shape, 
     k_h=3, k_w=3, d_h=1, d_w=1, stddev=0.02, 
     name="deconv2d", with_w=False): 
with tf.variable_scope(name): 
    # filter : [height, width, output_channels, in_channels] 
    w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]], 
         initializer=tf.contrib.layers.variance_scaling_initializer()) 

    deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape, 
         strides=[1, d_h, d_w, 1], padding='SAME') 


    biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0)) 
    deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape()) 

    return deconv 

이며 이는 출력 호환 형상 오차를 생성하지 않는

with tf.name_scope("total_loss"): 
     self.loss = tf.nn.l2_loss(self.output - self.images) 

손실이다. 최적화 그러나 ,

with tf.variable_scope("Optimizer"): 
     optimizer = tf.train.AdamOptimizer(config.learning_rate) 
     grad_and_vars = optimizer.compute_gradients(self.loss, var_list = self.d_vars) 
     self.d_optim = optimizer.apply_gradients(grad_and_vars) 

tensorflow 오류를 생산

Traceback (most recent call last): 
File "main.py", line 74, in <module> 
tf.app.run() 
File "/usr/local/lib/python2.7/dist- packages/tensorflow/python/platform/app.py", line 44, in run 
_sys.exit(main(_sys.argv[:1] + flags_passthrough)) 
File "main.py", line 59, in main 
dcgan.train(FLAGS) 
File "/home/junyonglee/workspace/bi_sim/sumGAN/model.py", line 121, in train 
grad_and_vars = optimizer.compute_gradients(self.loss, var_list = self.d_vars) 
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 354, in compute_gradients 
colocate_gradients_with_ops=colocate_gradients_with_ops) 
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gradients_impl.py", line 500, in gradients 
in_grad.set_shape(t_in.get_shape()) 
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 425, in set_shape 
self._shape = self._shape.merge_with(shape) 
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 585, in merge_with 
(self, other)) 
ValueError: Shapes (30, 256, 256, 64) and (30, 128, 128, 64) are not compatible 

디코더의 출력의 크기 (30)가 배치 사이즈 (30, 256, 256 3)이다.

전역 그라디언트 (op 단위로 그라디언트 흐름)는 (30, 256, 256, 64)의 모양이며 로컬 그라디언트 (입력 그라데이션 그라디언트)의 모양은 (" 30, 128, 128, 64). 이것은 중첩 된 컨볼 루션을 수행한다는 사실을 아주 명백하게 보여줍니다.

누구든지 conv2d_transpose()를 사용하여 올바르게 역순환하는 방법을 알고 있습니까? 감사합니다.

답변

2

deconv2d 기능을 표시 할 수 있습니까? 그것 없이는 조언을 많이 줄 수는 없습니다. 이 작동하는 경우

def transpose_deconvolution_layer(input_tensor,used_weights,new_shape,stride,scope_name): 
    with tf.variable_scope(scope_name): 
    output = tf.nn.conv2d_transpose(input_tensor, used_weights, output_shape=new_shape,strides=[1,stride,stride,1], padding='SAME') 
    output = tf.nn.relu(output) 
    return output 


def resize_deconvolution_layer(input_tensor,used_weights,new_shape,stride,scope_name): 
    with tf.variable_scope(scope_name): 
     output = tf.image.resize_images(input_tensor,(new_shape[1],new_shape[2]))#tf.nn.conv2d_transpose(input_tensor, used_weights, output_shape=new_shape,strides=[1,stride,stride,1], padding='SAME') 
     output, unused_weights = conv_layer(output,3,new_shape[3]*2,new_shape[3],1,scope_name+"_awesome_deconv") 
     return output 

테스트하십시오

여기에 나는 그런 디컨 볼 루션 기능을 구현 두 가지 방법이 있습니다. 두 가지를 프로그래밍 한 이유에 대해 자세히 알고 싶다면 http://www.pinchofintelligence.com/photorealistic-neural-network-gameboy/ 및 본 문서를 확인하십시오. http://distill.pub/2016/deconv-checkerboard/

도움이 되었으면 알려주세요!

감사합니다.

+0

답변 해 주셔서 대단히 감사합니다. 나는 당신이 연결 한 것을 보게 될 것이다. 나는 doconv2d() 함수를 공격했다. 문제가 있는지 확인하십시오. 고맙습니다! – codebugger

+0

resize_deconvolution_layer()를 호출 하시겠습니까? transpose_deconvolution_layer는 내 것과 같습니다. 또한, 왜 "used_weights"에 가중치를 명명 했습니까 (컨볼 루션 (인코딩) 프로세스에 사용되는 가중치를 사용 했습니까?) deconv 레이어의 가중치를 업데이트하는 것이 적절합니까? (이름 때문에 혼란 스러움) – codebugger

+0

조금 늦었지만 네트워크에서 직접 사용할 수있는 데콘 네트 용 라이브러리를 만들었습니다 : https://github.com/InFoCusp/tf_cnnvis – optimist