Skip to content

How to change the input shape of trained model? #288

Closed

Description

@micosacak

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;

my_model = model(input_shape = (128,128,1))
my_model.load_weights("model/variables/variables")

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?

Activity

  1. rnett commented on Apr 13, 2021

    @rnett
    Contributor

    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.

  2. micosacak commented on Apr 14, 2021

    @micosacak
    Author

    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.

  3. micosacak commented on Apr 16, 2021

    @micosacak
    Author

    @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.

  4. Craigacp commented on Apr 16, 2021

    @Craigacp
    Collaborator

    You can load in a saved model from Python using the SavedModelBundle class and then use it to score new examples. Alternatively there are some example models in https://github.com/tensorflow/java-models.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions