You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to change the input shape of trained model? #288
Currently, I am trying to update my models from Python/tensorflow (version 2.2) to version 2.4.1, so it can be compatible with Java/tensorflow-core-paltform (version 0.3.1).
In Python, I can always generate a new model with different input shapes and then load only the weights from the trained models. for instance;
Now, how can do in tensorflow/java. If just load the model by SavedModelBundle.load("model", "serve");, my saved model input shape is (None, 128, 128, 1).
Is there a way, to change the input_shape of the model in tensorflow/java, as my input shape will be variable?
Not really, you're recreating the model in Python with model (I think, I'm not aware of any Python functionality that allows you to change the input shape inplace) and then loading the new weights.
You can do something similar in Java, by loading the new, untrained model, then in the saved model's session running Assign operations to set the weights to the trained weights, but there's no support yet for doing it as one step (it will most likely be part of framework's model API).
However, the easiest way would be to load your model in python, update the weights there, and then re-export and use the new export from Java.
All of this is assuming that you have a strict input shape model that you want to change. If you have a model where the data has variable input shapes (rather than wanting to create a new model w/ a different input shape), you'll want to use unknown input shapes in Python (i.e. your input shape would be [None, None, 1] for a single channel image of unknown size), those transfer to Java.
I just asked the same question on tensorflow/tensorflow, based on your suggestion above, thus using [None, None, 1] as input shape. 48513.
As a note, I do not have any problem performing all analyses using the trained models in Python. However, I want to create an ImageJ plugin. While trying to write the similar pipeline in Java, I always have a problem after solving one problem in tensorflow/java. I have already asked alot of questions here on GitHub and thanks for getting answers to my questions I have already made a big progress in one month. But I still have a lot to learn to be familiar with Java and tensorflow/java as well.
@rnett
I think I will have to create the same model in Java and load weights from the model trained in Python. Do you think that is possible?
I wonder if there is any tutorial or example that I can learn how to generate a model in tensorflow/java? I have checked here, but an example will make it faster for me to learn and create new models.
Activity
rnett commentedon Apr 13, 2021
Not really, you're recreating the model in Python with
model(I think, I'm not aware of any Python functionality that allows you to change the input shape inplace) and then loading the new weights.You can do something similar in Java, by loading the new, untrained model, then in the saved model's session running
Assignoperations to set the weights to the trained weights, but there's no support yet for doing it as one step (it will most likely be part of framework's model API).However, the easiest way would be to load your model in python, update the weights there, and then re-export and use the new export from Java.
All of this is assuming that you have a strict input shape model that you want to change. If you have a model where the data has variable input shapes (rather than wanting to create a new model w/ a different input shape), you'll want to use unknown input shapes in Python (i.e. your input shape would be
[None, None, 1]for a single channel image of unknown size), those transfer to Java.micosacak commentedon Apr 14, 2021
I just asked the same question on tensorflow/tensorflow, based on your suggestion above, thus using [None, None, 1] as input shape. 48513.
As a note, I do not have any problem performing all analyses using the trained models in Python. However, I want to create an ImageJ plugin. While trying to write the similar pipeline in Java, I always have a problem after solving one problem in tensorflow/java. I have already asked alot of questions here on GitHub and thanks for getting answers to my questions I have already made a big progress in one month. But I still have a lot to learn to be familiar with Java and tensorflow/java as well.
micosacak commentedon Apr 16, 2021
@rnett
I think I will have to create the same model in Java and load weights from the model trained in Python. Do you think that is possible?
I wonder if there is any tutorial or example that I can learn how to generate a model in tensorflow/java? I have checked here, but an example will make it faster for me to learn and create new models.
Craigacp commentedon Apr 16, 2021
You can load in a saved model from Python using the
SavedModelBundleclass and then use it to score new examples. Alternatively there are some example models in https://github.com/tensorflow/java-models.