【发布时间】:2020-12-25 15:10:44
【问题描述】:
我正在尝试在 Colab 中训练一个模型,然后将其转移到 Kaggle。该模型似乎在 Colab 中作为 .h5 模型运行良好。问题似乎出在 Efficient net B4 和 Kaggle 之后。没有关于此的文档。我在 TPU 上训练这个模型并在 GPU 上进行推理,但即使我在 GPU 上训练模型,这个问题仍然存在。
我的错误日志
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-2ca8d6accabd> in <module>
10 policy = mixed_precision.Policy('mixed_bfloat16')
11 mixed_precision.set_policy(policy)
---> 12 model = tf.keras.models.load_model(r"../input/model-for-training/effieceintnettpurandomcrop.h5")
13
14 model.summary()
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
182 if (h5py is not None and (
183 isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 184 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
185
186 if sys.version_info >= (3, 4) and isinstance(filepath, pathlib.Path):
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
176 model_config = json.loads(model_config.decode('utf-8'))
177 model = model_config_lib.model_from_config(model_config,
--> 178 custom_objects=custom_objects)
179
180 # set weights
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/model_config.py in model_from_config(config, custom_objects)
53 '`Sequential.from_config(config)`?')
54 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top
---> 55 return deserialize(config, custom_objects=custom_objects)
56
57
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
107 module_objects=globs,
108 custom_objects=custom_objects,
--> 109 printable_module_name='layer')
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
371 custom_objects=dict(
372 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 373 list(custom_objects.items())))
374 with CustomObjectScope(custom_objects):
375 return cls.from_config(cls_config)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/sequential.py in from_config(cls, config, custom_objects)
396 for layer_config in layer_configs:
397 layer = layer_module.deserialize(layer_config,
--> 398 custom_objects=custom_objects)
399 model.add(layer)
400 if (not model.inputs and build_input_shape and
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
107 module_objects=globs,
108 custom_objects=custom_objects,
--> 109 printable_module_name='layer')
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
373 list(custom_objects.items())))
374 with CustomObjectScope(custom_objects):
--> 375 return cls.from_config(cls_config)
376 else:
377 # Then `cls` may be a function returning a class.
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in from_config(cls, config)
653 A layer instance.
654 """
--> 655 return cls(**config)
656
657 def compute_output_shape(self, input_shape):
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/normalization.py in __init__(self, axis, momentum, epsilon, center, scale, beta_initializer, gamma_initializer, moving_mean_initializer, moving_variance_initializer, beta_regularizer, gamma_regularizer, beta_constraint, gamma_constraint, renorm, renorm_clipping, renorm_momentum, fused, trainable, virtual_batch_size, adjustment, name, **kwargs)
198 **kwargs):
199 super(BatchNormalizationBase, self).__init__(
--> 200 name=name, **kwargs)
201 if isinstance(axis, (list, tuple)):
202 self.axis = axis[:]
/opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
454 self._self_setattr_tracking = False # pylint: disable=protected-access
455 try:
--> 456 result = method(self, *args, **kwargs)
457 finally:
458 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
336 # fields, like the loss scale, are used by Models. For subclassed networks,
337 # the compute and variable dtypes are used as like any ordinary layer.
--> 338 self._set_dtype_policy(dtype)
339 # Boolean indicating whether the layer automatically casts its inputs to the
340 # layer's compute_dtype.
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _set_dtype_policy(self, dtype)
1986 self._dtype_policy = dtype
1987 elif isinstance(dtype, dict):
-> 1988 self._dtype_policy = policy.deserialize(dtype)
1989 elif dtype:
1990 self._dtype_policy = policy.Policy(dtypes.as_dtype(dtype).name)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/mixed_precision/experimental/policy.py in deserialize(config, custom_objects)
628 module_objects=module_objects,
629 custom_objects=custom_objects,
--> 630 printable_module_name='dtype policy')
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
360 config = identifier
361 (cls, cls_config) = class_and_config_for_serialized_keras_object(
--> 362 config, module_objects, custom_objects, printable_module_name)
363
364 if hasattr(cls, 'from_config'):
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
319 cls = get_registered_object(class_name, custom_objects, module_objects)
320 if cls is None:
--> 321 raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
322
323 cls_config = config['config']
ValueError: Unknown dtype policy: PolicyV1
我的模型训练代码:
import tensorflow as tf
from tensorflow.keras.mixed_precision import experimental as mixed_precision
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.regularizers import l1
policy = mixed_precision.Policy('mixed_bfloat16')
mixed_precision.set_policy(policy)
reg = l1(0.001)
with strategy.scope():
base_model = tf.keras.applications.EfficientNetB3(weights="imagenet", include_top=False)
base_model.trainable = True
model = tf.keras.Sequential([
tf.keras.layers.BatchNormalization(),
base_model,
BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(256),
BatchNormalization(),
tf.keras.layers.LeakyReLU(),
BatchNormalization(),
tf.keras.layers.Dense(128),
BatchNormalization(),
tf.keras.layers.LeakyReLU(),
BatchNormalization(),
tf.keras.layers.Dropout(0.4),
BatchNormalization(),
tf.keras.layers.Dense(64),
BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dense(32),
BatchNormalization(),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dense(16),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dense(8),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
])
model.compile(
optimizer=tf.keras.optimizers.SGD(lr=0.04),
loss='sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'])
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
early = EarlyStopping(monitor='val_loss',
mode='min',
patience=5)
STEPS_PER_EPOCH = 17118 // BATCH_SIZE
VALID_STEPS = 4279 // BATCH_SIZE
checkpoint_filepath = 'gs://mithil/tmp/checkpoint_temp'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_filepath,
save_weights_only=True,
monitor='val_sparse_categorical_accuracy',
mode='max',
save_best_only=True)
history = model.fit(train_dataset,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=25,
validation_data=valid_dataset,
validation_steps=VALID_STEPS, callbacks=[early,model_checkpoint_callback])
model.load_weights(checkpoint_filepath)
推理代码
import tensorflow as tf
import numpy as np
import os
import pandas as pd
import cv2
from collections import Counter
from tensorflow.keras.mixed_precision import experimental as mixed_precision
from tensorflow.keras.regularizers import l1
policy = mixed_precision.Policy('mixed_bfloat16')
mixed_precision.set_policy(policy)
model = tf.keras.models.load_model(r"../input/model-for-training/effieceintnettpurandomcrop.h5")
model.summary()
path = "../input/cassava-leaf-disease-classification/test_images"
【问题讨论】:
标签: python python-3.x tensorflow keras deep-learning