Combine multiple outputs in tensorflowjs

Viewed 341

I have been given a model by a college and want to use it to create a heatmap. To do so, i need to get the result of the last convolutional layer. So I've tried to create a new model using the definitions of the old. I've seen this is possible in python and it seems it should be possible according to the docs.

My code looks something like this:

const combined_output_model = (cam_model) => {
    const conv_layer = cam_model.layers.length - 3
    const input_layer = cam_model.input
    const output_layer = cam_model.layers[conv_layer].output
    return model = tf.model(
        inputs=input_layer,
        outputs=output_layer
    )

However, when I run the code I get the following error:

container.ts:156 Uncaught (in promise) TypeError: Cannot read property 'sourceLayer' of undefined
    at e [as constructor] (container.ts:156)
    at new e (training.ts:483)
    at Object.t.model (exports.ts:74)
    at predictor (prediction.js:28)

when looking at both input_layer and output_layer in the local variables show they both have sourceLayers

Any help appreciated.

Regards

2 Answers

You are using a python-like sentence for creating the model in js. In js, tf.model takes an object

return model = tf.model(
        {inputs: input_layer,
        outputs: output_layer}
    )

Similar answer can be found here

to construct a new layers model that is a subset of the original model (i.e., cam_model), you can try something like this:

const model = tf.sequential()
for (let i = 0; i < cam_model.layers.length - 3; i++) {
    model.add(cam_model.layers[i])
}

please note if you take this approach but want to skip/omit the first/input layer you would need to define a new input layer: https://js.tensorflow.org/api/latest/#layers.inputLayer

Related