question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No easy way to change the input shapes for a built model for transfer learning

See original GitHub issue

TensorFlow.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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
aman-tiwaricommented, Jul 9, 2018

This works for now:

type ShapeMap = { [name: string]: [number, number, number, number] };

function mapInputShapes<T extends tf.Model>(model: T, newShapeMap: ShapeMap) {
  const cfg = {...model.getConfig()};
  cfg.layers = (cfg.layers as any[]).map(l => {
    if(l.name in newShapeMap) {
      return {...l, config: {
        ...l.config,
        batchInputShape: newShapeMap[l.name]
      }};
    } else {
      return l;
    }
  });

  const map = tf.serialization.SerializationMap.getMap().classNameMap;
  const [cls, fromConfig] = map[model.getClassName()];

  return fromConfig(cls, cfg) as T;
}

But not sure if tf.serialization.SerializationMap.getMap().classNameMap is public or whether this will break for certain kinds of models.

0reactions
schematicalcommented, Mar 28, 2021

I used @aman-tiwari’s solution above but now getting this:

Input 0 is incompatible with layer dense_Dense1: expected axis -1 of input shape to have value 25088 but got shape 51,38400.

Any ideas?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found