keras-team / keras-core

A multi-backend implementation of the Keras API, with support for TensorFlow, JAX, and PyTorch.
Apache License 2.0
1.27k stars 117 forks source link

Improve Documentation on KERAS_BACKEND #833

Closed giild closed 1 year ago

giild commented 1 year ago

I was testing a sample Kaggle competition notebook found here https://www.kaggle.com/competitions/rsna-2023-abdominal-trauma-detection/code

I wrote a simple script to load the model and ran into an issue. It took me a few days to figure out why.

if I set KERAS_BACKEND after importing keras libraries, models.load_model fails with a strange error. Once I moved line to the top, it got rid of the error. It would be good to update the documentation so that users can resolve this quickly without wasting a bunch of time. I tend to import all the libraries first and then set any environment variables after the imports. I can make a documentation pull request if that helps.

import os

backend must be set before loading keras_core to avoid load errors

os.environ["KERAS_BACKEND"] = "tensorflow" import sys import keras_cv import keras_core as keras from keras_core import layers

import numpy as np import pandas as pd import tensorflow as tf

class Config: SEED = 61 IMAGE_SIZE = [256, 256] BATCH_SIZE = 160 EPOCHS = 10 TARGET_COLS = [ "bowel_injury", "extravasation_injury", "kidney_healthy", "kidney_low", "kidney_high", "liver_healthy", "liver_low", "liver_high", "spleen_healthy", "spleen_low", "spleen_high", ] BASE_PATH = "./input/rsna-2023-abdominal-trauma-detection" AUTOTUNE = tf.data.AUTOTUNE

config = Config()

def main(): args = sys.argv[0:] if len(args) == 1: print('Example usage:') print(' python train_model.py checkpoint_path') else: print(args) filename = args[1] print("try to load the model") loadmodel = keras.models.load_model(filename) print(loadmodel.summary())

if name == "main": main()

giild commented 1 year ago

Some additional information, in case someone else runs into the same issue. If I move backend setting to after the imports, load throws this error. Here is the log from console.

Using TensorFlow backend 2.12.0 Traceback (most recent call last): File "/Users/peterlin/kaggle/ATD-keras/keras/convert_keras_to_json.py", line 98, in main() File "/Users/peterlin/kaggle/ATD-keras/keras/convert_keras_to_json.py", line 25, in main run(input, outputfile) File "/Users/peterlin/kaggle/ATD-keras/keras/convert_keras_to_json.py", line 29, in run inputmodel = tf.keras.models.load_model(inputfile) File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras/saving/saving_api.py", line 204, in load_model return saving_lib.load_model( File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras/saving/saving_lib.py", line 277, in load_model raise e File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras/saving/saving_lib.py", line 242, in load_model model = deserialize_keras_object( File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras/saving/serialization_lib.py", line 502, in deserialize_keras_object instance = cls.from_config(inner_config) File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras_core/src/models/model.py", line 515, in from_config return functional_from_config( File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras_core/src/models/functional.py", line 519, in functional_from_config process_node(layer, node_data) File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras_core/src/models/functional.py", line 466, in process_node layer(*args, **kwargs) File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/Users/peterlin/miniconda3/envs/tf2_kg/lib/python3.10/site-packages/keras/engine/input_spec.py", line 251, in assert_input_compatibility ndim = x.shape.rank AttributeError: Exception encountered when calling layer 'res_net_backbone' (type ResNetBackbone).

'tuple' object has no attribute 'rank'

Call arguments received by layer 'res_net_backbone' (type ResNetBackbone): • inputs=<KerasTensor shape=(160, 256, 256, 3), dtype=float32, name=input_layer> • training=None • mask=None

SuryanarayanaY commented 1 year ago

Hi @giild ,

There is a this note already in documentation.

Note that the that the backend must be configured before importing keras_core, and the backend cannot be changed after the package has been imported.

Please refer the attached snapshot.

Screenshot 2023-09-05 at 12 51 48 PM
giild commented 1 year ago

thanks for pointing out it was at the bottom. Not gonna lie, I looked at the home page several times and didn't even see it. I've been contributing to Apache for 20 years and often we have troubleshooting page/section for common errors. I would suggest moving the note above example and giving it more emphasis. It's too easy to miss the way it is formatted it right now. I've seen this countless times on a variety of apache projects. Often what apache devs thought was obvious wasn't due to how we formatted or organized the documentation.

giild commented 1 year ago

Something else to consider. https://keras.io/keras_core/ is the official docs. Often, I refer to the source instead of docs out of habit. Would it make sense to update the readme on https://github.com/keras-team/keras-core with similar note? I know most devs would rather write code than docs and maintaining the same information in multiple places sucks. Maybe have the readme link to the official documentation URL to lessen the maintenance overhead.

SuryanarayanaY commented 1 year ago

@giild ,

Thanks for the explanation. I think we can add a note in README.md for better visibility. I can create a PR once Team reviews this and gives nod.