keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
60 stars 28 forks source link

Error loading the trained model. #780

Closed clebemachado closed 3 months ago

clebemachado commented 4 months ago

Hi guys, how are you? I'm trying to load my trained model but I'm getting the following error:File not found: filepath=modelo\model_74.keras. Please ensure the file is an accessible .keras zip file.

Keras Version: 3.0.5 Tensorflow Version: 2.16.1

Code I'm using to load the model

from tensorflow.keras.utils import CustomObjectScope
from tensorflow.keras.models import load_model

from keras import backend as K # Estrutura de manipulação de tensores simbólicos

""" IoU """
def iou(y_true, y_pred, smooth=1):
    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])
    union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection
    iou = K.mean((intersection + smooth) / (union + smooth), axis=0) # Média de um tensor, ao longo do eixo especificado
    return iou

""" Dice Coefficient """
def dice_coef(y_true, y_pred, smooth=1):
    intersection = K.sum(y_true * y_pred, axis=[1,2,3])
    union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3])
    return K.mean( (2. * intersection + smooth) / (union + smooth), axis=0)

""" Dice Coefficient Loss """
def dice_coef_loss(y_true, y_pred):
    return 1 - dice_coef(y_true, y_pred)

scope = {'iou': iou, 'dice_coef': dice_coef, 'dice_coef_loss': dice_coef_loss}
path_model = 'modelo/model_74.keras'

with CustomObjectScope(scope):
    modelo = load_model(Path(path_model))

I tried other PATHS: path_model = './modelo/model_74.keras' or path_model = 'modelo//model_74.keras' or path_model = './/modelo//model_74.keras' or path_model = '//modelo//model_74.keras'

tilakrayal commented 4 months ago

@clebemachado, Could you please confirm whether you are trying to execute the code with the keras3.0 or tf-keras. Also I tried to execute the mentioned code and it was executed with a different error. Kindly find the gist of it here. Thank you!

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 3 months ago

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

goodevile commented 3 months ago

same problem:

load_model("/home/****/notebooks/model_from_points_2D/custom_model.keras")

Result in:

miniconda3/envs/yolo_env/lib/python3.10/site-packages/keras/src/saving/saving_api.py", line 193, in load_model
    raise ValueError(
ValueError: File not found: filepath=/home/****/notebooks/model_from_points_2D/custom_model.keras. Please ensure the file is an accessible `.keras` zip file.
clebemachado commented 2 months ago

It was the keras version, I changed it to 3 and it worked.

wurzel400 commented 1 month ago

Hi, I am having the same issue. I got a model saved to "myfile.keras" by the ModelCheckpoint callback using TF 2.16.1 and Keras 3.3.3 in Python 3.10.12. When trying to reload the model with

import keras
model=keras.saving.load_model("myfile.keras")

I also get the "File not found: ... Please ensure the file is an accessible .keras zip file." error message from the initial posting by clebemachado. The file is definitely there and looks integer as far as I can say. At the beginning it has a "keras_version" : "3.3.3" entry. Looking at the code around line 187 of saving_api.py (which is referenced in the error output), I could imagine that the problem is that the file is not in keras .zip format, but has a ".keras" file ending. However, assuming this is the case, how would I make ModelCheckpoint saving the model in .zip format then? And would there still be a way to access my already trained and saved model?

Note that the same code for saving and reloading the model worked absolutely fine on another machine with TF V2.15 and Keras V2.15 (yes, the reported versions are identical).

I am a bit desperate at the moment, as my already trained model seems lost and I also don´t know what to change for a re-run. Any help is very appreciated!

@clebemachado: Could you maybe explain what exactly you did here:

It was the keras version, I changed it to 3 and it worked.