Ошибка: Неизвестная метрическая функция: 'function'. При попытке получить кэшированную загруженную модель (модель Keras Instance)
Ошибка:
извлечение модели '' из кэша: Неизвестная метрическая функция: 'function'. Убедитесь, что вы используете keras.utils.custom_object_scope
и что этот объект включен в область видимости. Подробности см. в https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object.
Получение строк множественной ML-модели и хранение загруженной модели в Redis
models = MLModel.objects.all()
for model_entry in models:
try:
model_title = model_entry.title
model_name = model_entry.name
model_url = model_entry.file.url
lock_name = f"lock:model:{model_title}"
with redis_lock(lock_name) as acquired:
if not acquired:
logger.info(f"Skipping model {model_name}, another worker is already loading it.")
continue
logger.info(f"Loading model: {model_name} from URL: {model_url}")
local_model_path = os.path.join(models_directory, f"{model_name}.h5")
logger.info(f"Local file path: {local_model_path}")
download_file(model_url, local_model_path)
loss_type = model_entry.loss_type
loss_code = model_entry.loss
loss_function = load_loss(loss_type, loss_code)
model = tf.keras.models.load_model(local_model_path, custom_objects={loss_type: loss_function})
list_of_words = model_entry.list_of_words
ml_utils_code = model_entry.ml_utils
model_details = {
'name': model_name,
'model': model,
'list_of_words': list_of_words,
'ml_utils_code': ml_utils_code,
'loss_type': loss_type,
'loss_code': loss_code
}
cache_key = f"model:{model_title}"
redis_client.set(cache_key, pickle.dumps(model_details))
logger.info(f"Model {model_name} loaded and stored in cache with key: {model_title}.")
except Exception as e:
logger.error(f"Error loading model {model_entry.name}: {e}")
logger.info("All models loaded and stored in Redis cache.")
Теперь при получении конкретной загруженной модели из redis возникает ошибка
import logging
logger = logging.getLogger(__name__)
redis_client = redis.StrictRedis.from_url(settings.REDIS_URL)
def get_loaded_model(title):
try:
model_data = redis_client.get(f"model:{title}")
if model_data:
model_details = pickle.loads(model_data)
ml_utils_code = model_details['ml_utils_code']
ml_utils = load_ml_utils(ml_utils_code)
loss_type = model_details['loss_type']
logger.info(f"Custom Loss Type: {loss_type}")
logger.info(f"Custom Loss Code: {loss_code}")
loss_function = load_loss(loss_type, model_details['loss_code'])
with custom_object_scope({loss_type: loss_function}):
model = model_details['model']
model_details['ml_utils'] = ml_utils
return model_details, model
else:
logger.warning(f"Model with title '{title}' not found in cache.")
return None
except Exception as e:
logger.error(f"Error retrieving model '{title}' from cache: {e}")
return None
Traceback:
Error retrieving model '<title>' from cache: Unknown metric function: 'function'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
Unexpected error during model prediction setup for <title>: cannot unpack non-iterable NoneType object
Traceback (most recent call last):
File "apps.py", line 70, in predict_with_model
model_entry, model = get_loaded_model(title)
TypeError: cannot unpack non-iterable NoneType object