Different output after Save/Load for Keras model using CTC Layer

91 views
Skip to first unread message

Tathagata Ghosh

unread,
Nov 22, 2021, 4:08:22 AM11/22/21
to Keras-users
I try to train CNN-LSTM model using custom CTC Layer( code from https://keras.io/examples/vision/handwriting_recognition/ ) in google colab. I trained the model following all the steps mentioned in the keras example code, and the only additional change I made was to add a model.save . At first all the predictions were perfectly correct, however after saving the model in my drive and loading them at a new colab session I was getting total random predictions. I found similar problems in link1 and link2, with no clear solution.  I also tried model.save_weights, but its not working.

Sayak Paul

unread,
Nov 22, 2021, 4:14:38 AM11/22/21
to Tathagata Ghosh, Keras-users
Hi.

You will need to ensure the vocabulary preprocessing layer in both cases is exactly the same i.e., their index mapping is exactly the same. 

Sayak Paul | sayak.dev



On Mon, Nov 22, 2021 at 2:38 PM Tathagata Ghosh <tathagh...@gmail.com> wrote:
I try to train CNN-LSTM model using custom CTC Layer( code from https://keras.io/examples/vision/handwriting_recognition/ ) in google colab. I trained the model following all the steps mentioned in the keras example code, and the only additional change I made was to add a model.save . At first all the predictions were perfectly correct, however after saving the model in my drive and loading them at a new colab session I was getting total random predictions. I found similar problems in link1 and link2, with no clear solution.  I also tried model.save_weights, but its not working.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/de159cc4-6e64-4f8d-8176-adffafd6d66dn%40googlegroups.com.
Message has been deleted

Lance Norskog

unread,
Nov 22, 2021, 8:33:05 PM11/22/21
to Tathagata Ghosh, Keras-users
In Python, a Dictionary is always randomized in order. Iterating keys gives you a different order each time you create the dictionary.
(This is, believe it or not, a safety feature to avoid mysterious order-based bugs in long-lived code.)

The Keras example you mention uses a newer Keras Layer StringLookup that solves this problem, but I think it uses the StringLookup in the wrong way. StringLookup should be in the model, so that its state is saved with the model state. I don't know how to code this.

Cheers,

Lance Norskog

On Mon, Nov 22, 2021 at 1:26 AM Tathagata Ghosh <tathagh...@gmail.com> wrote:
Hi
Thanks for the quick reply.
Can you suggest any good method to ensure the same vocabulary preprocessing layer. I tried using pickle files but it fails.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA
Reply all
Reply to author
Forward
0 new messages