使用基本的低级TensorFlow训练循环训练tf.keras模型不起作用

Ale*_*lex 17 python keras tensorflow

注意:可以在下面找到重现我的问题的自包含示例的所有代码.

我有一个tf.keras.models.Model实例,需要使用低级TensorFlow API编写的训练循环来训练它.

问题:使用基本的标准低级TensorFlow训练循环训练完全相同的tf.keras模型,并使用Keras自己的model.fit()方法训练一次产生非常不同的结果.我想在我的低级TF训练循环中找出我做错了什么.

该模型是一个简单的图像分类模型,我在Caltech256上训练(链接到下面的tfrecords).

使用低级别的TensorFlow训练循环,训练损失首先会减少,但是经过1000次训练后,失去平稳状态然后再次开始增加:

在此输入图像描述

另一方面,使用普通的Keras训练循环在同一数据集上训练相同的模型,按预期工作:

在此输入图像描述

在我的低级TensorFlow训练循环中我缺少什么?

以下是重现问题的代码(下载带有底部链接的TFRecords):

import tensorflow as tf
from tqdm import trange
import sys
import glob
import os

sess = tf.Session()
tf.keras.backend.set_session(sess)

num_classes = 257
image_size = (224, 224, 3)

# Build a tf.data.Dataset from TFRecords.

tfrecord_directory = 'path/to/tfrecords/directory'

tfrecord_filennames = glob.glob(os.path.join(tfrecord_directory, '*.tfrecord'))

feature_schema = {'image': tf.FixedLenFeature([], tf.string),
                  'filename': tf.FixedLenFeature([], tf.string),
                  'label': tf.FixedLenFeature([], tf.int64)}

dataset = tf.data.Dataset.from_tensor_slices(tfrecord_filennames)
dataset = dataset.shuffle(len(tfrecord_filennames)) # Shuffle the TFRecord file names.
dataset = dataset.flat_map(lambda filename: tf.data.TFRecordDataset(filename))
dataset = dataset.map(lambda single_example_proto: tf.parse_single_example(single_example_proto, feature_schema)) # Deserialize tf.Example objects.
dataset = dataset.map(lambda sample: (sample['image'], sample['label']))
dataset = dataset.map(lambda image, label: (tf.image.decode_jpeg(image, channels=3), label)) # Decode JPEG images.
dataset = dataset.map(lambda image, label: (tf.image.resize_image_with_pad(image, target_height=image_size[0], target_width=image_size[1]), label))
dataset = dataset.map(lambda image, label: (tf.image.per_image_standardization(image), label))
dataset = dataset.map(lambda image, label: (image, tf.one_hot(indices=label, depth=num_classes))) # Convert labels to one-hot format.
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.repeat()
dataset = dataset.batch(32)

iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()

# Build a simple model.

input_tensor = tf.keras.layers.Input(shape=image_size)
x = tf.keras.layers.Conv2D(64, (3,3), strides=(2,2), activation='relu', kernel_initializer='he_normal')(input_tensor)
x = tf.keras.layers.Conv2D(64, (3,3), strides=(2,2), activation='relu', kernel_initializer='he_normal')(x)
x = tf.keras.layers.Conv2D(128, (3,3), strides=(2,2), activation='relu', kernel_initializer='he_normal')(x)
x = tf.keras.layers.Conv2D(256, (3,3), strides=(2,2), activation='relu', kernel_initializer='he_normal')(x)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(num_classes, activation=None, kernel_initializer='he_normal')(x)
model = tf.keras.models.Model(input_tensor, x)
Run Code Online (Sandbox Code Playgroud)

这是简单的TensorFlow训练循环:

# Build the training-relevant part of the graph.

model_output = model(features)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf.stop_gradient(labels), logits=model_output))

train_op = tf.train.AdamOptimizer().minimize(loss)

# The next block is for the metrics.
with tf.variable_scope('metrics') as scope:
    predictions_argmax = tf.argmax(model_output, axis=-1, output_type=tf.int64)
    labels_argmax = tf.argmax(labels, axis=-1, output_type=tf.int64)
    mean_loss_value, mean_loss_update_op = tf.metrics.mean(loss)
    acc_value, acc_update_op = tf.metrics.accuracy(labels=labels_argmax, predictions=predictions_argmax)
    local_metric_vars = tf.contrib.framework.get_variables(scope=scope, collection=tf.GraphKeys.LOCAL_VARIABLES)
    metrics_reset_op = tf.variables_initializer(var_list=local_metric_vars)

# Run the training

epochs = 3
steps_per_epoch = 1000

fetch_list = [mean_loss_value,
              acc_value,
              train_op,
              mean_loss_update_op,
              acc_update_op]

sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())

with sess.as_default():

    for epoch in range(1, epochs+1):

        tr = trange(steps_per_epoch, file=sys.stdout)
        tr.set_description('Epoch {}/{}'.format(epoch, epochs))

        sess.run(metrics_reset_op)

        for train_step in tr:

            ret = sess.run(fetch_list, feed_dict={tf.keras.backend.learning_phase(): 1})

            tr.set_postfix(ordered_dict={'loss': ret[0],
                                         'accuracy': ret[1]})
Run Code Online (Sandbox Code Playgroud)

下面是标准的Keras训练循环,它按预期工作.请注意,None为了使Keras循环工作,需要将上述模型中的密集层激活从"softmax" 更改为"softmax".

epochs = 3
steps_per_epoch = 1000

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(dataset,
                    epochs=epochs,
                    steps_per_epoch=steps_per_epoch)
Run Code Online (Sandbox Code Playgroud)

您也可以下载TFRecords为Caltech256数据集在这里(约850 MB).

更新:

我设法解决了这个问题:更换低级TF丢失功能

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf.stop_gradient(labels), logits=model_output))
Run Code Online (Sandbox Code Playgroud)

通过它的Keras等价物

loss = tf.reduce_mean(tf.keras.backend.categorical_crossentropy(target=labels, output=model_output, from_logits=True))
Run Code Online (Sandbox Code Playgroud)

诀窍.现在低级别的TensorFlow训练循环表现得像model.fit().

这提出了一个新问题:

这是什么tf.keras.backend.categorical_crossentropy()tf.nn.softmax_cross_entropy_with_logits_v2()不导致后者执行更加恶化?(我知道后者需要logits,而不是softmax输出,所以这不是问题)

Ale*_*lex 6

取代低阶TF损失功能

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf.stop_gradient(labels), logits=model_output))
Run Code Online (Sandbox Code Playgroud)

相当于Keras

loss = tf.reduce_mean(tf.keras.backend.categorical_crossentropy(target=labels, output=model_output, from_logits=True))
Run Code Online (Sandbox Code Playgroud)

绝招。现在,低级TensorFlow训练循环的行为类似于model.fit()

但是,我不知道为什么会这样。如果有人知道为什么tf.keras.backend.categorical_crossentropy()表现良好而tf.nn.softmax_cross_entropy_with_logits_v2()根本不起作用,请发布答案。

另一个重要说明:

为了tf.keras用低级TF训练循环和tf.data.Dataset对象训练模型,通常不应在迭代器输出上调用该模型。也就是说,不应该这样做:

model_output = model(features)
Run Code Online (Sandbox Code Playgroud)

相反,应该创建一个模型,在该模型中将输入层设置为基于迭代器输出构建,而不是创建占位符,如下所示:

input_tensor = tf.keras.layers.Input(tensor=features)
Run Code Online (Sandbox Code Playgroud)

在此示例中这无关紧要,但是如果模型中的任何层具有在训练期间需要运行的内部更新(例如BatchNormalization),则变得有意义。