【问题标题】:Can't get past first epoch -- just hangs [Keras Transfer Learning Inception]无法通过第一个 epoch - 只是挂起 [Keras Transfer Learning Inception]
【发布时间】:2017-11-19 23:02:41
【问题描述】:

我基本上使用的是 Keras Inception 迁移学习 API 教程中的大部分代码,

https://faroit.github.io/keras-docs/2.0.0/applications/#inceptionv3

只需进行一些小的更改以适应我的数据。

我正在使用 Tensorflow-gpu 1.4、Windows 7 和 Keras 2.03(?最新的 Keras)。

代码:

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K


img_width, img_height = 299, 299
train_data_dir = r'C:\Users\Moondra\Desktop\Keras Applications\data\train'
nb_train_samples = 8
nb_validation_samples = 100 
batch_size = 10
epochs = 5


train_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
zoom_range = 0.1,
rotation_range=15)



train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size, 
class_mode = 'categorical')  #class_mode = 'categorical'


# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(12, activation='softmax')(x)

# this is the model we will train
model = Model(input=base_model.input, output=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# train the model on the new data for a few epochs
model.fit_generator(
train_generator,
steps_per_epoch = 5,
epochs = epochs)


# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.

# let's visualize layer names and layer indices to see how many layers
# we should freeze:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 172 layers and unfreeze the rest:
for layer in model.layers[:172]:
   layer.trainable = False
for layer in model.layers[172:]:
   layer.trainable = True

# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit_generator(
train_generator,
steps_per_epoch = 5,
epochs = epochs)

输出(无法超过第一个 epoch):

 Epoch 1/5

1/5 [=====>........................] - ETA: 8s - loss: 2.4869
2/5 [===========>..................] - ETA: 3s - loss: 5.5591
3/5 [=================>............] - ETA: 1s - loss: 6.6299

4/5 [=======================>......] - ETA: 0s - loss: 8.4925

它只是挂在这里。

更新:

我使用 tensorflow 1.3(降级一个版本)和 Keras 2.03(最新的 pip 版本)创建了一个虚拟环境,但仍然遇到同样的问题。

更新 2

我不认为这是一个内存问题,就像我在 epoch 中更改步骤一样——它会一直运行到最后一步,然后就冻结了。

所以在一个 epoch 中执行 30 步,它将运行到 29。

5 步,它会一直运行到第 4 步,然后挂起。

更新 3

还按照 Keras API 中的建议尝试了第 249 层。

【问题讨论】:

  • 据我所知,keras的最新版本是2.11
  • 您的代码对我来说似乎很好。可能是内存溢出问题,请检查您的内存。另请检查 inceptionv3 网络中的层数,目前您正在考虑 172 层。
  • @TusharGupta 我将检查初始模型的层,但我假设它是正确的,因为此代码是在官方 Kera API 页面上提供的。至于内存泄漏——我不完全确定如何检查。我使用 tf 作为支持,并且已知 tensorflow 将所有空闲内存分配给它自己,即使它不使用内存。所以每次我使用显卡监控工具,内存在95%,谢谢。

标签: python deep-learning keras


【解决方案1】:

显然这是通过 Keras 更新修复的错误(但是,有些问题仍然存在)

【讨论】:

    【解决方案2】:

    tensorflow.__version__==1.10.1 和 keras.__version__==2.2.2 也出现了同样的问题,我的解决方法是将 keras 降级为 2.2.0 使用 pip3 install -I keras==2.2.0。请注意,这可能会破坏兼容性,您可能还需要降级 tensorflow。

    【讨论】:

      【解决方案3】:

      似乎大多数冻结问题发生在代码中的某些错误发生时。 在my case 中,我构建了一个生成器,它在纪元结束时抛出异常并且进程停止。但是没有关于异常的消息,所以我也花了一些时间来弄清楚发生了什么。

      【讨论】:

        【解决方案4】:

        正如@thomas-e 所提到的,我在 keras/tf 兼容性方面也遇到了类似的问题。具体来说,我的配置是:cuda-10.0, cudnn-7, tensorflow_gpu=1.14.0, keras=2.2.5.

        通过降级修复它:cuda-9.0, cudnn-7, tensorflow-gpu=1.10.0 and keras=2.2.0

        从这篇文章中得到了关于不兼容的想法:https://github.com/tensorflow/tensorflow/issues/15604

        另外,您可以参考以下文章中的 keras 和 tensorflow 兼容性:

        1. https://www.tensorflow.org/install/source#tested_build_configurations
        2. https://docs.floydhub.com/guides/environments/

        【讨论】:

          【解决方案5】:

          保持这些关系解决了问题:

          steps_per_epoch = number of train samples/batch_size
          
          validation_steps= number of validation samples/batch_size
          

          更多相同@ https://github.com/keras-team/keras/issues/8595

          【讨论】:

            猜你喜欢
            • 2019-09-16
            • 1970-01-01
            • 2019-05-15
            • 1970-01-01
            • 2017-02-18
            • 2020-05-05
            • 2021-12-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多