keras在第一个时代停止工作

时间:2017-12-30 22:28:34

标签: tensorflow keras

我正在运行图像分类模型。这是我被卡住的地方。尝试将keras版本降级到1.0.2并再次运行脚本没有用。

Jupyter笔记本只是继续处理,并且在第一个时代之后没有运行任何东西,使用python 3.5在keras 1.2上运行代码

输出:

/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/__main__.py:19: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(101, activation="softmax", kernel_initializer="glorot_uniform", kernel_regularizer=<keras.reg...)`
/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/__main__.py:21: UserWarning: Update your `Model` call to the Keras 2 API: `Model(outputs=Tensor("de..., inputs=Tensor("in...)`
/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/__main__.py:44: UserWarning: The semantics of the Keras 2 argument `steps_per_epoch` is not the same as the Keras 1 argument `samples_per_epoch`. `steps_per_epoch` is the number of batches to draw from the generator at each epoch. Basically steps_per_epoch = samples_per_epoch/batch_size. Similarly `nb_val_samples`->`validation_steps` and `val_samples`->`steps` arguments have changed. Update your method calls accordingly.
/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/__main__.py:44: UserWarning: Update your `fit_generator` call to the Keras 2 API: `fit_generator(<image_gen..., verbose=2, epochs=32, validation_steps=25250, validation_data=<image_gen..., steps_per_epoch=1183, callbacks=[<keras.ca...)`
Epoch 1/32

INPUT:

%%time
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D, GlobalAveragePooling2D, AveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, CSVLogger, LearningRateScheduler, ReduceLROnPlateau
from keras.optimizers import SGD
from keras.regularizers import l2
import keras.backend as K
import math

K.clear_session()

base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3)))
x = base_model.output
x = AveragePooling2D(pool_size=(8, 8))(x)
x = Dropout(.4)(x)
x = Flatten()(x)
predictions = Dense(n_classes, kernel_initializer='glorot_uniform', W_regularizer=l2(.0005), activation='softmax')(x)





model = Model(input=base_model.input, output=predictions)

opt = SGD(lr=.01, momentum=.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

checkpointer = ModelCheckpoint(filepath='model4.{epoch:02d}-{val_loss:.2f}.hdf5', verbose=1, save_best_only=True)
csv_logger = CSVLogger('model4.log')

def schedule(epoch):
    if epoch < 15:
        return .01
    elif epoch < 28:
        return .002
    else:
        return .0004
lr_scheduler = LearningRateScheduler(schedule)

model.fit_generator(train_generator,
                    validation_data=test_generator,
                    nb_val_samples=X_test.shape[0],
                    samples_per_epoch=X_train.shape[0],
                    nb_epoch=32,
                    verbose=2,
                    callbacks=[lr_scheduler, csv_logger, checkpointer])

2 个答案:

答案 0 :(得分:1)

verbose = 1来电中尝试使用model.fit,它会打印进度条。它可能正常工作,但是由于给出了详细参数的值2,它只会在纪元结束后打印一行输出,这可能需要一些时间,具体取决于您的CPU / GPU和数据量。

答案 1 :(得分:0)

Co-Lab在云中提供有限的内存(最多12 GB),这在解决问题时会产生许多问题。这就是为什么只使用300张图像进行训练和测试的原因。当图像以600x600的尺寸进行预处理并将批处理大小设置为128时,在第1阶段冻结了Keras模型。编译器未显示此错误。实际上该错误是运行时受限的内存CoLab无法处理,因为它只提供了12GB的有限使用内存。 通过将批量大小更改为4并将图像尺寸减小为300x300,可以解决上述问题,因为在600x600下它仍然无法正常工作。  最后,建议解决方案是减小图像尺寸和Batch_size,直到没有错误为止。通过进一步将批处理大小和图像尺寸更改为较小尺寸来一次又一次地运行,直到没有运行时错误为止。