【问题标题】:Vanishing gradient and very low accuracy in InceptionV3InceptionV3 中的梯度消失和非常低的准确度
【发布时间】:2021-05-07 02:58:19
【问题描述】:

我正在使用 InceptionV3 使用 tensorflow 进行多类分类。 我以为我搞定了,但结果很奇怪,与我想要的相差甚远。

Epoch 1/10
58/58 [==============================] - 47s 591ms/step - loss: 0.0000e+00 - accuracy: 0.0279
Epoch 2/10
58/58 [==============================] - 38s 591ms/step - loss: 0.0000e+00 - accuracy: 0.0286
Epoch 3/10
58/58 [==============================] - 38s 596ms/step - loss: 0.0000e+00 - accuracy: 0.0249
Epoch 4/10
58/58 [==============================] - 38s 603ms/step - loss: 0.0000e+00 - accuracy: 0.0250

这是我的代码。

这段代码是我处理数据的地方。 raw_train 来自 oxford_iiit_pet。

dataset, metadata = tfds.load('oxford_iiit_pet', with_info=True, as_supervised=True)
raw_train, raw_test = dataset['train'], dataset['test']
IMAGE_SIZE = (224, 224)


def preprocess_dataset(image, label):
  image = tf.cast(image, tf.float32)
  image = (image/127.5)-1 # might need to fix this part
  image = tf.image.resize(image, IMAGE_SIZE)
  return image, label

BATCH_SIZE = 64
SHUFFLE_BUFFER_SIZE = 1024

train = raw_train.map(preprocess_dataset)
test = raw_test.map(preprocess_dataset)

train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)

这是我的模型。

IMG_SHAPE = (IMAGE_SIZE[0], IMAGE_SIZE[1], 3)
model_inception = tf.keras.applications.InceptionV3(input_shape=IMG_SHAPE, include_top = False, weights = 'imagenet')

model_inception.trainable = False # freeze the model

learning_rate = 0.001

model = tf.keras.Sequential([
    model_inception,
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(
    optimizer = tf.keras.optimizers.RMSprop(lr=learning_rate),
    loss = 'categorical_crossentropy',
    metrics=['accuracy']
)

EPOCHS = 10

history = model.fit(
    train_batches,
    epochs = EPOCHS
)

我不完全确定这是我预处理数据的方式还是我设置模型的方式。 在我真正运行模型之前,一切似乎都很好。 似乎正在发生梯度消失,但我不知道为什么以及它是否真的是问题所在。 我查看了该模型的使用方式,但似乎没有给出明确的答案。

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    因此,您输出的是[0.0,1.0] 域中的单个值。但是你说有多个类,而不仅仅是一个类:所以你要让最后一层中的神经元数量不管有多少类,并将其从sigmoid 激活更改为softmax 激活.像这样:

    NUM_CLASSES = 8   # edit this number to suit your problem
    
    model = tf.keras.Sequential([
        model_inception,
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')
    ])
    

    sigmoid 将无法正常工作,即使它 看起来 像它一样(因为所有值都是介于 0 和 1 之间的浮点数,就像 softmax 会给你一样)。

    坦率地说,我什至不确定您在调用fit() 时如何没有遇到某种错误。这是 Keras 的问题之一,恕我直言:它对用户非常友好,即使它应该“工作”(也就是说,run,而不是 work)提示您如何不正确地设置数据。除非您的训练集中的训练目标包含每个输入图像的单个浮点值,否则它甚至不应该运行。

    【讨论】:

    • 你最后一句话完全正确。我想知道是否应该出于这个原因应用一种热编码,但是有一些限制。
    猜你喜欢
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2020-10-16
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    相关资源
    最近更新 更多