Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE : Sequential model 'sequential_1' has already been configured to use input shape (None, 224, 224, 3). You cannot build it with input_shape [None, 224, 224, 3] #20214

Closed
thayyilakul opened this issue Sep 5, 2024 · 7 comments

Comments

@thayyilakul
Copy link

Currently learning tensorflow with keras and got this error while loading saved model which is saved in keras extension.
ValueError: Sequential model 'sequential_1' has already been configured to use input shape (None, 224, 224, 3). You cannot build it with input_shape [None, 224, 224, 3]

Below is my model creation function

def create_model(input_shape = INPUT_SHAPE, output_shape = OUTPUT_SHAPE, model_url = MODEL_URL):
  print('Building model with: ',MODEL_URL)

  hubs_layer = hub.KerasLayer(MODEL_URL)
  model = tf.keras.Sequential([
       tf.keras.layers.Lambda(lambda x: hubs_layer(x)), # Layer 1 (input layer)
      tf.keras.layers.Dense(units = OUTPUT_SHAPE,
                            activation = 'softmax') # Layer 2 (output layer)
  ])
  model.compile(
      loss = tf.keras.losses.CategoricalCrossentropy(),
      optimizer = tf.keras.optimizers.Adam(),
      metrics = ['accuracy']
  )
  model.build(INPUT_SHAPE)
  return model`

MODEL_URL is just a kaggle dataset mobilenet-v2

Below is my load model function

# Create a function to load a trained model
def load_model(model_path):
  '''
  Loads a saved model from a specified path
  '''
  print(f'Loading saved model from: {model_path}')

  # Enable unsafe deserialization
  # tf.keras.config.enable_unsafe_deserialization()

  # model = tf.keras.models.load_model(model_path, 
  #                                    custom_objects = {'KerasLayer' : hub.KerasLayer})
  model = tf.keras.models.load_model(model_path)
  return model

Below is how i am calling load function
loaded_1000_image_model = load_model('filename.keras')

@mehtamansi29
Copy link
Collaborator

Hi @thayyilakul -

Thanks for reporting this issue. Can you please let me know exactly at which line you are getting error ? I am not able to get error while running this function(create_model()). Attached gist for the reference.

@thayyilakul
Copy link
Author

Hi, thank you for your reply, well it's happening when i call the function load_model() after i save the model.

@mehtamansi29
Copy link
Collaborator

Hi @thayyilakul -

Here in this gist you can find successfully load model with custom_objects and without custom_objects.

Copy link

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 github-actions bot added the stale label Sep 21, 2024
Copy link

github-actions bot commented Oct 5, 2024

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

@github-actions github-actions bot closed this as completed Oct 5, 2024
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@metnbyar
Copy link

çözebildiniz mi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants