这是我的功能提取网络。
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(self.imgs1)
.....
if weights is not None and sess is not None:
self.load_weights(weights, sess)
scope.reuse_variables()
self.o2 = self.convlayers(self.imgs2)
.......
if weights is not None and sess is not None:
self.load_weights(weights, sess)
#create loss function
with tf.variable_scope("loss") as scope:
self.cd=self.cos_dist(self.o1,self.o2,sess)
self.loss(self.cd,self.o1,self.o2)
def convlayers(self,_image):
.....
def load_weights(self, weight_file, sess):
....
def loss(self,la,i1,j2):
label=tf.cast(la, tf.float32)
i=tf.nn.l2_normalize(i1, 1, epsilon=1e-2, name='normed')
j=tf.nn.l2_normalize(j2, 1, epsilon=1e-2, name='normed')
sub= tf.subtract(i,j)
magi=tf.sqrt(tf.reduce_sum(tf.square(sub)))
ab=tf.abs(magi)
sq=tf.square(ab)
self.left=sq*label
#working on right now
second_part=1-label
tpart=tf.constant(0.675, dtype=tf.float32, name='T')-magi
tp=tf.maximum(0.0,tpart)
self.right=second_part*tp
self.final_loss=self.left+self.right
o= tf.train.GradientDescentOptimizer(0.09)
self.l=o.compute_gradients(self.final_loss)
self.o=o.apply_gradients(self.l)
def cos_dist(self,net_1,net_2,sess):
......
if __name__ == '__main__':
# for batch processing
sess = tf.InteractiveSession()
imgs1 = tf.placeholder(tf.float32, [None, 224, 224, 3])
imgs2 = tf.placeholder(tf.float32, [None, 224, 224, 3])
vgg = vgg16(imgs1,imgs2, 'vgg16_weights.npz', sess)
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(vgg.final_loss)
#try:
img1 = imread('c2.jpeg', mode='RGB')
img1 = imresize(img1,(224, 224))
img2 = imread('c7.jpeg', mode='RGB')
img2 = imresize(img2,(224, 224))
M= sess.run([vgg.k,vgg.o,vgg.final_loss],feed_dict={vgg.imgs1: [img1],vgg.imgs2: [img2]})
这是一个亚洲时尚的CNN。问题是后面的层没有得到更新。第一批更新为Nan。 sess.run()有什么问题吗?我调用属性的方式。 通常其中一个原因是输入错误。我尝试打印值img1和img2,矩阵中包含的值仅为255