我正在使用经过改进的预训练VGG16对医学图像进行分类。在训练过程中,权重不会改变,每步偏差仅改变0.001,而损失只是第一步会发生变化,以后会保持大量。
batch_size = 8,标签在每次迭代中固定为[1,1,1,1,0,0,0,0]。图像形状:(182,182,2)。输入数据归一化为0- 1,我试图将学习率从10 ^(-5)修改为0.1,什么都没有改变。所有可训练变量都初始化为非零值,并且打印出中间结果而没有找到任何非或inf值。输出8结果总是相同和相当大,例如[8.2265,8.2265,...,8.2265],损失始终在23左右,并且在第一步之后就不会下降
这是我的代码:
def train():
x = tf.placeholder(tf.float32, [None, 182, 182, 2], name = 'image_input')
y_ = tf.placeholder(tf.float32, [None, 8], name='label_input')
global_step = tf.Variable(0, trainable=False)
learning_rate = tf.train.exponential_decay(learning_rate=0.0001,
decay_rate=0.9,
global_step=TRAINING_STEPS,
decay_steps=50,
staircase=True)
# read the image dataset
# pos refers to the image with label '1', neg : label'0'
pos, neg = get_data.get_image(img_path)
label_batch = np.reshape(np.array([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]),[1, 8])
vgg = vgg16.Vgg16()
vgg.build(x)
#see the loss function definition below
loss = vgg.side_loss( y_,vgg.output1, vgg.output2, vgg.output3, vgg.output4)
train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step)
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.device('/gpu:0'):
with tf.Session() as sess:
sess.run(init_op)
for i in range(TRAINING_STEPS):
#batch_size = 4,defined in other area of train.py
start = i * batch_size
end = start + batch_size
#creating image_batch
#the first four images are from pos,and then 4 from neg
image_list = []
image_list.append(pos[start:end])
image_list.append(neg[start:end])
image_batch = np.reshape(np.array(image_list),[-1,182,182,2])
_,loss_val,step = sess.run([train_step,loss,global_step], feed_dict={x: image_batch,y_:label_batch})
if i % 50 == 0:
print("the step is %d,loss is %f" % (step, loss_val))
if loss_val < min_loss:
min_loss = loss_val
saver.save(sess, 'ckpt/vgg.ckpt', global_step=2000)
class Vgg16:
#a,b,c,d refers to the output in the model
#my modified vgg model has 4 outputs
def side_loss(self,yi,a,b,c,d):
self.loss1 = self.f_indicator(yi, a)
self.loss2 = self.f_indicator(yi, b)
self.loss3 = self.f_indicator(yi, c)
self.loss4 = self.f_indicator(yi, d)
self.loss_side = self.loss1 + self.loss2 + self.loss3 + self.loss4
res_loss = tf.reduce_sum(self.loss_side)
return res_loss
#here is the definition of my loss function
def f_indicator(self,yi,yj):
b = tf.where(yj>=1,yj*50,tf.abs(tf.log(tf.abs(1 - yj))))
res=tf.where(tf.equal(yi , 0.0), b,tf.abs(tf.log(tf.clip_by_value(yj, 1e-8, float("inf")))))
return res
有关如何解决此问题的任何建议?损失函数有问题吗?