# Implementing custom InstanceNormalization layer in tensorflowjs

**URL:** <https://discuss.ai.google.dev/t/implementing-custom-instancenormalization-layer-in-tensorflowjs/28141>\
**Category:** General Discussion\
**Tags:** models, tfjs, help\_request\
**Created:** [March 29, 2022, 2:18am UTC](https://discuss.ai.google.dev/t/implementing-custom-instancenormalization-layer-in-tensorflowjs/28141 "2022-03-29T02:18:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![G11](https://avatars.discourse-cdn.com/v4/letter/g/ba9def/32.png) [@G11](https://discuss.ai.google.dev/u/G11)\
**Post date:** [March 29, 2022, 2:18am UTC](https://discuss.ai.google.dev/t/implementing-custom-instancenormalization-layer-in-tensorflowjs/28141/1 "2022-03-29T02:18:38Z")

</div>

I am trying to import and load a trained keras cycleGAN model into tensorflowjs and angular typescript.  
but i ran into the error that says “ERROR Error: Uncaught (in promise): Error: Unknown layer: InstanceNormalization”

I googled and tried implementing my own custom layer as a .js file:

```auto
import * as tf from'@tensorflow/tfjs';

class InstanceNormalization extends tf.layers.Layer {
    epsilon = 1e-7;
    gamma;
    beta;

    constructor() {
        super({});
    }

    build(input_shape) {
        let shape = tf.tensor(input_shape);
        // initialize gamma
        this.gamma = this.addWeight(
            'gamma',
            shape,
            undefined,
            'ones',
            undefined,
            true,
        );

        // initialize beta
        this.beta = this.addWeight(
            'beta',
            shape,
            undefined,
            'zeros',
            undefined,
            true,
        )
    }

    call(inputs, kwargs) {
        this.invokeCallHook(inputs, kwargs);
        let mean = tf.math.reduce_mean(inputs, axis=[-1], keepdims=true);
        let variance = tf.math.reduce_mean(tf.math.square(inputs - mean), axis=[-1], keepdims=true);
        let std = tf.math.sqrt(variance + this.epsilon);
        let outputs = (inputs - mean) / std;
        outputs = outputs * this.gamma;
        outputs = outputs + this.beta;
        return outputs;
    }

    static get className() {
        return 'InstanceNormalization';
    }

}

tf.serialization.registerClass(InstanceNormalization);

export function instancenormalization() {
    return new InstanceNormalization();
}

```

the above effort did not resolve the issue where the error message still persisted.  
these are my references:

1. [javascript - Writing custom InstantLayerNormalization in tensorflow js - Stack Overflow](https://stackoverflow.com/questions/67602835/writing-custom-instantlayernormalization-in-tensorflow-js)
2. [tfjs-examples/custom-layer/custom\_layer.js at master · tensorflow/tfjs-examples · GitHub](https://github.com/tensorflow/tfjs-examples/blob/master/custom-layer/custom_layer.js)

---

<div class="post-metadata">

**Author:** ![Jason](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/jason/32/6193_2.png) [@Jason](https://discuss.ai.google.dev/u/Jason)\
**Post date:** [April 1, 2022, 8:04pm UTC](https://discuss.ai.google.dev/t/implementing-custom-instancenormalization-layer-in-tensorflowjs/28141/3 "2022-04-01T20:04:48Z")

</div>

@pyu or @lina128 May have some thoughts on this one.

---

<div class="post-metadata">

**Author:** ![pyu](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/pyu/32/6224_2.png) [@pyu](https://discuss.ai.google.dev/u/pyu)\
**Post date:** [April 1, 2022, 9:38pm UTC](https://discuss.ai.google.dev/t/implementing-custom-instancenormalization-layer-in-tensorflowjs/28141/4 "2022-04-01T21:38:24Z")

</div>

@G11 Your code looks like having all the plumbings, not sure why it is working for your model. Do you have a codepen example that can reproduce this error? thanks
