Comments (9)
I'm not following. Would you mind elaborating on where I should insert the code for the get_weights()/set_weights() workaround. I am using a script to load and use the model and that's wear the issue is happening.
`import keras
import numpy as np
import tensorflow as tf
from keras.models import load_model
import matplotlib.pyplot as plt
class_names = ['apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', 'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant', 'garlic', 'ginger', 'grapes', 'jalepeno', 'kiwi', 'lemon', 'lettuce', 'mango', 'onion', 'orange', 'paprika', 'pear', 'peas', 'pineapple', 'pomegranate', 'potato', 'raddish', 'soy beans', 'spinach', 'sweetcorn', 'sweetpotato', 'tomato', 'turnip', 'watermelon']
image_path = "download.jpg"
model = keras.models.load_model("TrainedModel.keras")
img = tf.keras.utils.load_img(
image_path, target_size=(180, 180)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)
`
from keras-core.
Also -- as a workaround, you can do weights = model.get_weights(); new_model.set_weights(weights)
. This will work.
from keras-core.
Can you print model.summary()
on each side (before saving/loading)?
from keras-core.
Here's the output of model summary.
Model summary before saving:
Model: "my_model"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩
│ input_layer (InputLayer) │ (None, None, None, 3) │ 0 │
├────────────────────────────────────┼───────────────────────────────┼─────────────┤
│ conv2d (Conv2D) │ (None, None, None, 1) │ 4 │
└────────────────────────────────────┴───────────────────────────────┴─────────────┘
Total params: 4 (16.00 B)
Trainable params: 4 (16.00 B)
Non-trainable params: 0 (0.00 B)
Model summary before loading:
Model: "my_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, None, None, 3)] 0
conv2d (Conv2D) (None, None, None, 1) 4
=================================================================
Total params: 4 (16.00 Byte)
Trainable params: 4 (16.00 Byte)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
from keras-core.
This is probably caused by a naming discrepancy. We should fix it. @nkovela1 do you have cycles to take a look?
from keras-core.
I found the cause -- basically some change in TF seems to have broken the save file namespace in tf.keras (introduction of a _layer_checkpoint_dependencies
path which overrides layers
. We have to fix this on the tf.keras side. Things are nominal in Keras Core.
from keras-core.
Sure thing, I just created a Colab to inspect the h5 file and found that same discrepancy with _layer_checkpoint_dependencies
: https://colab.sandbox.google.com/drive/1Ir1AQp6DUtYXk-nomRVgjM11ukTPXnnt
from keras-core.
This is now fixed at HEAD but the issue will persist in TF 2.13 and TF 2.14. Use get_weights()
/set_weights()
as a workaround.
from keras-core.
Sounds good, thanks for the quick fix!
from keras-core.
Related Issues (20)
- tflite cannot fuse `BatchNormalization` in Keras Core as effectively as in the original Keras
- Improve Documentation on KERAS_BACKEND HOT 5
- How can we use `ops.reshape` when the `new_shape` is obtained from `ops.shape`? HOT 3
- GSOC Program Organization ( question ) HOT 1
- Constant Initializer supports only Scalar values HOT 1
- Add support for RaggedTensors to JAX and Pytorch Backend HOT 1
- Ensure workflow reliability by hash-pinning GitHub Actions HOT 1
- Run actions.yml with read-only permissions
- Segmentation metrics HOT 3
- Use of PyTorch loss functions inside Keras
- Is possible serialize models which use torch functions? HOT 5
- Expose `Operation` without `src` import hacks HOT 1
- v0.1.6 bug: AttributeError: 'GPT2CausalLM' object has no attribute 'compiled' HOT 2
- Adam with amsgrad=True + JAX backend is broken HOT 1
- Inconsistent type handling between backends HOT 1
- Matmul - tensorflow does not broadcast/expand dimensions correctly HOT 3
- Casting dtype in losses.Loss base class HOT 2
- TypeError: copy() got an unexpected keyword argument 'overwrite' HOT 2
- Error when running the model.fit on the wrapped model "Only input tensors may be passed as positional arguments."
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from keras-core.