Tensorboard AttributeError:'ModelCheckpoint'对象没有属性'on_train_batch_begin'

时间:2019-07-20 08:08:40

标签: python tensorflow keras tensorboard

我目前正在使用Tensorboard,使用此SO post概述的以下回调,如下所示。

from keras.callbacks import ModelCheckpoint

CHECKPOINT_FILE_PATH = '/{}_checkpoint.h5'.format(MODEL_NAME)
checkpoint = ModelCheckpoint(CHECKPOINT_FILE_PATH, monitor='val_acc', verbose=1, save_best_only=True, mode='max', period=1)

运行Keras的密集网络模型时,出现以下错误。我在任何其他模型上以这种方式运行Tensorboard时都没有任何问题,这使此错误非常奇怪。根据此Github post,官方解决方案是使用官方Tensorboard实施;但是,这需要升级到Tensorflow 2.0,这对我来说并不理想。谁知道为什么我为此特定的密集网络收到以下错误,并且有人知道解决方法/修复程序吗?

  

AttributeError跟踪(最近的调用)   最后)在()        26 batch_size = 32,        27 class_weight = class_weights_dict,   ---> 28个callbacks = callbacks_list        29)        30

     

2幅   /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py   在_call_batch_hook中(自身,模式,钩子,批处理,日志)       245 t_before_callbacks = time.time()       246 for self.callbacks中的回调:   -> 247 batch_hook = getattr(回调,hook_name)       248 batch_hook(批处理,日志)       249 self._delta_ts [hook_name] .append(time.time()-t_before_callbacks)

     

AttributeError:'ModelCheckpoint'对象没有属性   'on_train_batch_begin'

我正在运行的密集网络

from tensorflow.keras import layers, Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.densenet import preprocess_input, DenseNet121
from keras.optimizers import SGD, Adagrad
from keras.utils.np_utils import to_categorical

IMG_SIZE = 256
NUM_CLASSES = 5
NUM_EPOCHS = 100

x_train = np.asarray(x_train)
x_test = np.asarray(x_test)

y_train = to_categorical(y_train, NUM_CLASSES)
y_test = to_categorical(y_test, NUM_CLASSES)


x_train = x_train.reshape(x_train.shape[0], IMG_SIZE, IMG_SIZE, 3)
x_test = x_test.reshape(x_test.shape[0], IMG_SIZE, IMG_SIZE, 3)

densenet = DenseNet121(
    include_top=False,
    input_shape=(IMG_SIZE, IMG_SIZE, 3)
)

model = Sequential()
model.add(densenet)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dense(NUM_CLASSES, activation='softmax'))
model.summary()

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

history = model.fit(x_train,
                    y_train,
                    epochs=NUM_EPOCHS,
                    validation_data=(x_test, y_test),
                    batch_size=32,
                    class_weight=class_weights_dict,
                    callbacks=callbacks_list
                   )

4 个答案:

答案 0 :(得分:1)

在您的导入中,您混合使用{strong>不兼容的kerastf.keras,因为您会遇到类似的奇怪错误。

因此,一个简单的解决方案是选择kerastf.keras,然后从该程序包中进行所有导入,而切勿将其与其他程序混在一起。

答案 1 :(得分:0)

ProjectCount = CALCULATE( COUNT(DIM_Project[%ProjKey]), DIM_Project[Startdate] = YEAR(TODAY())) keras进行所有导入

我希望这可以解决问题!

答案 2 :(得分:0)

是,进口是来自keras和tensorflow

尝试粘贴到tensorflow.keras上,例如:

from tensorflow.keras.callbacks import EarlyStopping

答案 3 :(得分:0)

我替换此行

from keras.callbacks import EarlyStopping, ModelCheckpoint

此行

from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint