2017-01-07 1 views
0

VGG를 미세 조정해야합니다. 5 개의 길쌈 레이어가 있고 세 개의 완전히 연결된 레이어가 있습니다. 마지막으로 완전히 연결된 레이어의 출력은 손실 함수의 입력입니다. 다음은 내 코드입니다.손실 출력 없음

class vgg16: 
    def __init__(self, imgs1,imgs2, weights=None, sess=None): 

     self.imgs1 = imgs1 
     self.imgs2 = imgs2 

     with tf.variable_scope("siamese") as scope: 
      self.o1 = self.convlayers(imgs1) 
     self.fc_layers() 

     self.loss() 

     if weights is not None and sess is not None: 
       self.load_weights(weights, sess) 
      scope.reuse_variables() 
      self.o2 = self.convlayers(imgs2) 
     self.fc_layers() 
     self.loss() 

     if weights is not None and sess is not None: 
      self.load_weights(weights, sess) 
     #create loss function 


    def convlayers(self,imgs): 
     .... 

     # conv1_2 
     with tf.name_scope('conv1_2') as scope: 
      ...... 
     # pool1 


    .. 
) 

     ..... 

     # pool5 
     self.pool5 = tf.nn.max_pool(self.conv5_3, 
           ksize=[1, 2, 2, 1], 
           strides=[1, 2, 2, 1], 
           padding='SAME', 
           name='pool4') 

    def fc_layers(self): 
     # fc1 
     with tf.name_scope('fc1') as scope: 
      .... 
     # fc2 
     with tf.name_scope('fc2') as scope: 
      ... 

     # fc3 
     with tf.name_scope('fc3') as scope: 
      fc3w = tf.Variable(tf.truncated_normal([4096, 1000], 
                dtype=tf.float32, 
                stddev=1e-1), name='weights') 
     fc3b = tf.Variable(tf.constant(1.0, shape=[1000], dtype=tf.float32), 
          trainable=True, name='biases') 
     self.fc3l = tf.nn.bias_add(tf.matmul(self.fc2, fc3w), fc3b) 
    def load_weights(self, weight_file, sess): 
     weights = np.load(weight_file) 
     keys = sorted(weights.keys()) 
     for i, k in enumerate(keys): 
      print i, k, np.shape(weights[k]) 
      sess.run(self.parameters[i].assign(weights[k])) 
    def loss(self): 

    loss=tf.nn.l2_loss(self.fc3l) 


    self.train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss) 



if __name__ == '__main__': 
    sess = tf.Session() 
    imgs1 = tf.placeholder(tf.float32, [None, 224, 224, 3])#jis size ka bhi imaeg hai usko 224x224 may kar diya or RGB chaeay hmay 
    imgs2 = tf.placeholder(tf.float32, [None, 224, 224, 3]) 
    vgg = vgg16(imgs1,imgs2, 'vgg16_weights.npz', sess) 


    img1 = imread('laska.png', mode='RGB') 
    img1 = imresize(img1, (224, 224)) 
    img2 = imread('laska2.jpg', mode='RGB') 
    img2 = imresize(img2,(224, 224)) 

    prob = sess.run(vgg.train_step, feed_dict={vgg.imgs1: [img1],vgg.imgs2: [img2]}) 
    print('loss is:') 
    print(prob) 

문제는 prob 결과가 없음이라는 것입니다. 내가 잘못하고있는 것을 친절하게 나타내십시오.

추신 : 나는 샴 아키텍처를 따르고 있습니다. 두 가지 입력은 서로 다른 이미지입니다.

답변

1

op self.train_step은 아무 것도 반환하지 않으며 그라디언트를 계산하고 변수를 업데이트합니다. here을 참조하십시오.

self.loss=tf.nn.l2_loss(self.fc3l) 

을 다음 단일 sess.run 모두 train_step 손실 작업을 실행 : 당신이해야 할 일은

이처럼 vgg16 클래스 loss 텐서에 대한 참조를 저장하는 것입니다

_, loss_value = sess.run([vgg.train_step, vgg.loss], feed_dict=...) 
print('loss is:') 
print(loss_value)