当我训练基于VGG的模型以产生显着性图时,损失不会减少

时间:2017-11-23 05:58:17

标签: tensorflow deep-learning training-data loss

我的工作是通过基于VGG的网络获取显着性图。 但是,按照我的想象,这种损失不会减少。所以我无法找到为什么我的损失不会减少。 PS。训练数据集是SALICON。 这是输出:

training epoch 1, loss value is 0.041423566639423
training epoch 2, loss value is 0.041423123329878
training epoch 3, loss value is 0.041430559009314
training epoch 4, loss value is 0.041424177587032
...
training epoch 20, loss value is 0.041416928172112

我尝试更改优化器,学习速率,丢失功能,但没有人工作。 这是我的代码:

def shuffle(photo, grdtr, shuffle=True):
    idx = np.arange(0, len(photo))
    if shuffle:
        np.random.shuffle(idx)
    photo_shuffle = [photo[i] for i in idx]
    grdtr_shuffle = [grdtr[i] for i in idx]

    return np.asarray(photo_shuffle), np.asarray(grdtr_shuffle)


if __name__ == '__main__':
    # create the model
    x = tf.placeholder(tf.float32, [None, 48, 64, 3])
    y_ = tf.placeholder(tf.float32, [None, 48 * 64, 1])
    h = tf.placeholder(tf.float32, [None, 48, 64, 1])

    y = deepnn(x)

    # define loss and optimizer

    # cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=(tf.clip_by_value(y,1e-8,tf.reduce_max(y))), labels=y_))
    y_ = tf.nn.softmax(y_, dim=1)

    cross_entropy = tf.reduce_mean(tf.pow(y - y_, 2))
    # cross_entropy = tf.reduce_mean(y_ * tf.log(y_ / y)) #KL
    tf.summary.scalar('loss', cross_entropy)
    train_step = tf.train.AdamOptimizer(learning_rate = 0.001, beta1 = 0.9, beta2 = 0.999).minimize(cross_entropy)
    # do the training
    with tf.Session() as sess:
        ...

        # load the training data
        photos = np.load('./data/npy/SALICON100/photos_queue.npy')
        grdtrs = np.load('./data/npy/SALICON100/grdtrs_queue.npy')
        photos = photos / 255.0
        grdtrs = grdtrs / 255.0
        EPOCH = 20
        BATCH_SIZE = 20
        TRAINING_SET_SIZE = 20
        for j in range(EPOCH):
            # photos, grdtrs = shuffle(photos, grdtrs, shuffle=False)
            grdtrs = np.resize(grdtrs, [TRAINING_SET_SIZE, 48, 64, 20])
            grdtrs = np.reshape(grdtrs,[TRAINING_SET_SIZE, 48 * 64, 20])
            _, loss_value, pred_y = sess.run([train_step, cross_entropy,y],feed_dict={x: photos[:20], y_: grdtrs[:20]})
            if (j + 1) % 1 == 0:
                print('training epoch %d, loss value is %.15f' % (j + 1, loss_value))
            np.save('./data/20_photos_test/net_output.npy', pred)
            np.save('./data/20_photos_test/net_grdtrs.npy', grdtrs[:20])

                # stop the queue threads and properly close the session
       ...

我在sess中提出了一些关于张量的代码:

x = tf.placeholder(tf.float32, [None, 48, 64, 3])
y_ = tf.placeholder(tf.float32, [None, 48 * 64, 1])
y = deepnn(x)
cross_entropy = tf.reduce_sum(tf.pow(y-y_sm,2))

1 个答案:

答案 0 :(得分:1)

在您发布的代码中,您实际上从未执行过火车步骤。您需要调用sess.run(train_step, feed_dict)行的内容来实际训练您的网络。如果你不训练你的网络,那么你的损失就不会减少。

您还确定要在标签上使用softmax吗?