# Converting StyleGan to tensorflow js

**URL:** <https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335>\
**Category:** General Discussion\
**Tags:** keras, models, tfjs, help\_request\
**Created:** [June 8, 2021, 12:25am UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335 "2021-06-08T00:25:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saswat\_Das](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/saswat_das/32/7438_2.png) [@Saswat\_Das](https://discuss.ai.google.dev/u/Saswat_Das)\
**Post date:** [June 8, 2021, 12:25am UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/1 "2021-06-08T00:25:39Z")

</div>

Hello. This doubt may seem a little bit less researched, but here I go. I wanted to convert the stylegan generator to tensorflow js model. But this I understand that stylegan python (using TF2.x) has custom loops and objects in the generator object. I followed the steps mentioned below :

I right now have a keras model stylegan generator with weights loaded and the class has a call function that takes in noise and gives images. The definition is same as “StyleGAN\_G” class in [StyleGAN-Tensorflow2/stylegan.py at master · ialhashim/StyleGAN-Tensorflow2 (github.com)](https://github.com/ialhashim/StyleGAN-Tensorflow2/blob/master/stylegan.py). The weights I loaded was using the tutorial given in the same repository [StyleGAN-Tensorflow2/stylegan.ipynb at master · ialhashim/StyleGAN-Tensorflow2 (github.com)](https://github.com/ialhashim/StyleGAN-Tensorflow2/blob/master/stylegan.ipynb). So which format do I save the generator object (before converting it to tfjs model) ? Tensorflow seems to have a lot of save options

And after saving, what all flag values I have to set tfjsconvertor ?

---

<div class="post-metadata">

**Author:** ![lgusm](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/lgusm/32/6230_2.png) [@lgusm](https://discuss.ai.google.dev/u/lgusm)\
**Post date:** [June 9, 2021, 11:23am UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/2 "2021-06-09T11:23:28Z")

</div>

@Jason might be able to help

---

<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:** [June 9, 2021, 6:49pm UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/3 "2021-06-09T18:49:56Z")

</div>

Let me have a chat with the team and get back as I have not done much in the GAN space myself yet, but welcome to the community Saswat!

---

<div class="post-metadata">

**Author:** ![lina128](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/lina128/32/7138_2.png) [@lina128](https://discuss.ai.google.dev/u/lina128)\
**Post date:** [June 9, 2021, 7:13pm UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/4 "2021-06-09T19:13:14Z")

</div>

Hi @Saswat_Das , please save it as SavedModel, `model.save('my_model')` (doc: [Save, serialize, and export models &nbsp;|&nbsp; TensorFlow Core](https://www.tensorflow.org/guide/keras/save_and_serialize)). And then follow the tfjs-converter doc to convert the SavedModel to tfjs format: [tfjs/tfjs-converter at master · tensorflow/tfjs · GitHub](https://github.com/tensorflow/tfjs/tree/master/tfjs-converter)  
Basic cmd:

```auto
tensorflowjs_converter \
    --input_format=tf_saved_model \
    --output_format=tfjs_graph_model \
    --signature_name=serving_default \
    --saved_model_tags=serve \
    model_path/saved_model \
    new_path/web_model

```

---

<div class="post-metadata">

**Author:** ![Saswat\_Das](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/saswat_das/32/7438_2.png) [@Saswat\_Das](https://discuss.ai.google.dev/u/Saswat_Das)\
**Post date:** [June 10, 2021, 8:35am UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/5 "2021-06-10T08:35:49Z")

</div>

@lina128 Thank you . I converted the model to shard files and model.json. Then I loaded the graph model.

When I tried to run the following code to generate the image, it throws an error .  
`const noise = tf.randomNormal([512]).expandDims(0); let generatedImage = await generator.executeAsync(noise)`

The error is that: util\_base.ts:154 Uncaught (in promise) Error: Error in depthwiseConv2d: number of input channels (8) must match the inChannels dimension in filter 512.  
at Object.assert (util\_base.ts:154)  
at depthwiseConv2d\_ (depthwise\_conv2d.ts:99)  
at Object.depthwiseConv2d\_\_op (operation.ts:51)  
at Object.executeOp (convolution\_executor.ts:195)  
at operation\_executor.ts:69  
at engine.ts:467  
at Engine.scopedRun (engine.ts:478)  
at Engine.tidy (engine.ts:465)  
at Object.tidy (globals.ts:192)  
at operation\_executor.ts:68

---

<div class="post-metadata">

**Author:** ![Saswat\_Das](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/saswat_das/32/7438_2.png) [@Saswat\_Das](https://discuss.ai.google.dev/u/Saswat_Das)\
**Post date:** [June 10, 2021, 5:58pm UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/6 "2021-06-10T17:58:34Z")

</div>

I am guessing it is because of the fact that depthwise convolution in tensorflow doesn’t support ‘BCHW’ format as mentioned in the API reference. But in the actual python code the data\_format specified in the depthwise convolution was ‘BCHW’. Can anyone suggest what could be the error ?

---

<div class="post-metadata">

**Author:** ![lina128](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/lina128/32/7138_2.png) [@lina128](https://discuss.ai.google.dev/u/lina128)\
**Post date:** [June 14, 2021, 4:52pm UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/7 "2021-06-14T16:52:10Z")

</div>

It’s possible. Please add a feature request to add NCHW format or considering contribute. Or change the python model to use NHWC.

---

<div class="post-metadata">

**Author:** ![Saswat\_Das](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/saswat_das/32/7438_2.png) [@Saswat\_Das](https://discuss.ai.google.dev/u/Saswat_Das)\
**Post date:** [June 15, 2021, 5:53am UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/8 "2021-06-15T05:53:13Z")

</div>

Yes thank you. I changed the model to NHWC and it worked like a charm.

---

<div class="post-metadata">

**Author:** ![MadMan](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/madman/32/8665_2.png) [@MadMan](https://discuss.ai.google.dev/u/MadMan)\
**Post date:** [August 19, 2021, 2:55am UTC](https://discuss.ai.google.dev/t/converting-stylegan-to-tensorflow-js/17335/9 "2021-08-19T02:55:46Z")

</div>

Can you please share how did you convert it to NHWC format?
