realmanise commited on
Commit
2a0b37c
·
verified ·
1 Parent(s): d25790c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -4,27 +4,32 @@ import tensorflow as tf
4
  import keras
5
  import inspect
6
 
7
- # 1. Stand-in function for your model's Lambda layer
8
  def preprocess_input(x):
9
  return x
10
 
11
- # 2. Universal Safe Dense layer that automatically drops ANY unsupported arguments
12
  class SafeDense(keras.layers.Dense):
13
  @classmethod
14
  def from_config(cls, config):
 
15
  accepted_args = inspect.signature(keras.layers.Dense.__init__).parameters.keys()
16
 
17
- # Build a cleaned config containing ONLY keys the current version expects
18
  cleaned_config = {}
19
  for key, value in config.items():
20
  if key in accepted_args:
21
  cleaned_config[key] = value
22
- elif key == 'name': # 'name' is handled by the base layer initialization
23
  cleaned_config[key] = value
24
 
25
  return super().from_config(cleaned_config)
26
 
27
- # 3. Load your model mapping BOTH custom objects
 
 
 
 
28
  def load_garbage_model():
29
  model = keras.models.load_model(
30
  'garbage_classifier.keras',
 
4
  import keras
5
  import inspect
6
 
7
+ # 1. Stand-in function for your model's Lambda layer structure
8
  def preprocess_input(x):
9
  return x
10
 
11
+ # 2. Universal Argument Interceptor that drops ANY unsupported modern layer properties
12
  class SafeDense(keras.layers.Dense):
13
  @classmethod
14
  def from_config(cls, config):
15
+ # Dynamically inspect what arguments this version of Keras Dense layer accepts
16
  accepted_args = inspect.signature(keras.layers.Dense.__init__).parameters.keys()
17
 
18
+ # Build a cleaned config containing ONLY keys the current engine version expects
19
  cleaned_config = {}
20
  for key, value in config.items():
21
  if key in accepted_args:
22
  cleaned_config[key] = value
23
+ elif key == 'name':
24
  cleaned_config[key] = value
25
 
26
  return super().from_config(cleaned_config)
27
 
28
+ # 3. FORCE Keras to use SafeDense globally whenever it requests a standard "Dense" layer layout
29
+ keras.utils.register_keras_serializable(package="Custom", name="Dense")(SafeDense)
30
+ # This overrides the internal string registry map directly
31
+ keras.src.saving.serialization_lib.ObjectRegistrar.register(keras.layers.Dense, SafeDense)
32
+
33
  def load_garbage_model():
34
  model = keras.models.load_model(
35
  'garbage_classifier.keras',