No easy way to change the input shapes for a built model for transfer learning
See original GitHub issueTensorFlow.js version
0.11.7
Browser version
N/A
Describe the problem or feature request
If I load a pretrained TFJS model, there is no easy way to change the input shapes it takes in.
For instance, if I load Mobilenet
, and I want to perform transfer learning on it, I have no easy way to get the model to take different shapes (even if the model is fully convolutional).
Code to reproduce the bug / link to feature request
const model = await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json')
const test = tf.zeros([1, 512, 512, 3])
const preds = model.predict(test)
Expected output:
preds
is a tensor of the output of the model
Actual output:
Error: Error when checking : expected input_1 to have shape [null,224,224,3] but got array with shape [1,512,512,3].
This is especially needed when you need to transfer some layers of a loaded model into another:
const someLayers = tf.sequential({layers: model.layers.slice(0, 5)})
someLayers.predict(test)
It would be good if there was some way to unbuild
a layer, or if there was some easy way to clone a layer (keeping everything but the shapes & topology), potentially like:
const convLayers = model.layers.slice(0, 5).map(l => l.unbuilt()) // or l.clone(), l.clone({withoutShape: true}), etc
const someLayers = tf.sequential({layers: convLayers})
WDYT? I would be happy to author a PR for it unless someone else is working on something similar.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Change input shape dimensions for fine-tuning with Keras
In this tutorial, you will learn how to change the input shape tensor dimensions for fine-tuning using Keras. After going through this guide ......
Read more >Keras -- Transfer learning -- changing Input tensor shape
You can do this by creating a new VGG16 model instance with the new input shape new_shape and copying over all the layer...
Read more >Changing input size of pre-trained models in Keras
The function first changes the input shape parameters of the network. At this point the internals of the model have not been registered....
Read more >Transfer learning and fine-tuning | TensorFlow Core
Take layers from a previously trained model. · Freeze them, so as to avoid destroying any of the information they contain during future...
Read more >Keras Input shape of a pretrained model not changing
You could try replace all the fully connected layers after the flatten one, which is usually the best practice. While in the debugging...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This works for now:
But not sure if
tf.serialization.SerializationMap.getMap().classNameMap
is public or whether this will break for certain kinds of models.I used @aman-tiwari’s solution above but now getting this:
Any ideas?