bonlime / keras-deeplab-v3-plus

Keras implementation of Deeplab v3+ with pretrained weights
MIT License
1.35k stars 429 forks source link

Load this model in tensorflowjs #73

Open Johannes0Horn opened 5 years ago

Johannes0Horn commented 5 years ago

I would like to save a deeplab tensorflowjs model locally, after loading it. For now its only possible for models loaded by "tf.loadLayersModel()" (https://js.tensorflow.org/api/latest/#loadLayersModel). So one has to use keras instead of pure tensorflow. I used this keras implementation to load my weights, which i got from training of the official tensorflow deeplabv3+ implementation and saved it as a .H5 file. I also could convert the H5. file to a tensorflowjs model using the tensorflowjs_converter released with tensorflowjs 1.0.0.

When i try to load the model in tfjs using "tf.loadLayersModel()" i get the error: "Error: Unknown layer: BilinearUpsampling", because its a custom Layer defined in https://github.com/bonlime/keras-deeplab-v3-plus/blob/master/model.py, which isnt defined in tfjs.

Is there a way to load this model in tfjs, without rewriting this custom Layer in tensorflowjs? Does anyone know a decent example how to rewrite a CustomLayer? I only can find very vague examples.

bonlime commented 5 years ago

Hi, Unfortunately, I don't have any experience with TFJS. Right now there is a 'bilinear' upsampling layer in Keras, but it lacks the option align_corners=True. Maybe if you will somehow manage to pass this parameter, you could get rid of custom layers. If you manage to do it, you could make a pull request here. I wanted to get rid of this custom layer for a long time but don't have time to do it. (also in README there is an example on how to load model with custom layers, but I don't know if it works for TFJS)

bonlime commented 5 years ago

also check this https://github.com/tensorflow/tensorflow/issues/23041 it looks like a working option