无法使用tf.keras.applications.MobileNetV2进行训练

时间:2019-12-31 03:28:39

标签: python tensorflow keras deep-learning

环境: TF2.0 Python 3.5 ubuntu 16.04

问题: 我尝试使用预先训练的mobilenet_V2,但准确性不会增加:

base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')

该脚本是从tensorflow 2.0(https://www.tensorflow.org/tutorials/images/transfer_learning?hl=zh-cn)的教程中复制的

我所做的唯一更改是馈入网络的数据集。原始代码对猫和狗进行了二进制分类,一切正常。但是,使用诸如“ mnist”,“ tf_flowers”之类的多类数据集时,准确性永远不会提高。请注意,我使用了正确的损失函数和指标。

代码如下:

from __future__ import absolute_import, division, print_function, unicode_literals
import os
import numpy as np
import tensorflow as tf

keras = tf.keras
import tensorflow_datasets as tfds
# tfds.disable_progress_bar()

SPLIT_WEIGHTS = (8, 1, 1)
splits = tfds.Split.TRAIN.subsplit(weighted=SPLIT_WEIGHTS)

(raw_train, raw_validation, raw_test), metadata = tfds.load(
    'mnist', split=list(splits),
    with_info=True, as_supervised=True)
get_label_name = metadata.features['label'].int2str


print(raw_train)
print(raw_validation)
print(raw_test)

IMG_SIZE = 160 # All images will be resized to 160x160

def format_example(image, label):
    if image.shape[-1] == 1:
        image = tf.concat([image, image, image], 2)
    image = tf.cast(image, tf.float32)
    image = (image/127.5) - 1
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
    return image, label

train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)

BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000

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


IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')
for image_batch, label_batch in train_batches.take(1):
   pass


feature_batch = base_model(image_batch)

base_model.trainable = False
base_model.summary()

print("image batch size:", image_batch.shape)
print("feature batch size:", feature_batch.shape)


global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
# print(feature_batch_average.shape)

prediction_layer = keras.layers.Dense(10)


model = tf.keras.Sequential([
  base_model,
  global_average_layer,
  prediction_layer
])

tf.keras.backend.set_learning_phase(True)

base_learning_rate = 0.01
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=base_learning_rate),
              loss='sparse_categorical_crossentropy',
              metrics=['sparse_categorical_accuracy'])


num_train, num_val, num_test = (
  metadata.splits['train'].num_examples*weight/10
  for weight in SPLIT_WEIGHTS
)

initial_epochs = 10
steps_per_epoch = round(num_train)//BATCH_SIZE
validation_steps = 20

loss0,accuracy0 = model.evaluate(validation_batches, steps = validation_steps)

print("initial loss: {:.2f}".format(loss0))
print("initial accuracy: {:.2f}".format(accuracy0))

history = model.fit(train_batches,
                    epochs=initial_epochs,
                    validation_data=validation_batches)

1 个答案:

答案 0 :(得分:0)

问题是您将所有参数设置为不可训练,请在模型摘要中进行检查,您将看到类似这样的

enter image description here

更改此行,(或只是删除它)

base_model.trainable = False

收件人

base_model.trainable = True

一切都会很好