Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert the handwriting recognition example to Keras 3. #1831

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

fkouteib
Copy link
Contributor

No description provided.

Copy link

google-cla bot commented Apr 13, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. The code looks good so far -- do you need any help?

@fkouteib
Copy link
Contributor Author

Hi @fchollet Thanks for the review. I am currently having issues with loading the dataset followed by an early crash for lack of input.

Total training samples: 86810
Total validation samples: 4823
Total test samples: 4823
Maximum length: 21
Vocab size: 78
2024-04-15 01:12:03.619746: W tensorflow/core/kernels/data/cache_dataset_ops.cc:858] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to dataset.cache().take(k).repeat(). You should use dataset.take(k).cache().repeat() instead.
2024-04-15 01:12:03.755489: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
Model: "handwriting_recognizer"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ image (InputLayer) │ (None, 128, 32, 1) │ 0 │ - │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ Conv1 (Conv2D) │ (None, 128, 32, 32) │ 320 │ image[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ pool1 (MaxPooling2D) │ (None, 64, 16, 32) │ 0 │ Conv1[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ Conv2 (Conv2D) │ (None, 64, 16, 64) │ 18,496 │ pool1[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ pool2 (MaxPooling2D) │ (None, 32, 8, 64) │ 0 │ Conv2[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ reshape (Reshape) │ (None, 32, 512) │ 0 │ pool2[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ dense1 (Dense) │ (None, 32, 64) │ 32,832 │ reshape[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ dropout (Dropout) │ (None, 32, 64) │ 0 │ dense1[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ bidirectional (Bidirectional) │ (None, 32, 256) │ 197,632 │ dropout[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ bidirectional_1 │ (None, 32, 128) │ 164,352 │ bidirectional[0][0] │
│ (Bidirectional) │ │ │ │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ label (InputLayer) │ (None, None) │ 0 │ - │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ softmax (Dense) │ (None, 32, 81) │ 10,449 │ bidirectional_1[0][0] │
└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘
Total params: 424,081 (1.62 MB)
Trainable params: 424,081 (1.62 MB)
Non-trainable params: 0 (0.00 B)
2024-04-15 01:12:04.273326: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
Traceback (most recent call last):
File "/home/fkouteib/devspace/keras-io/examples/vision/handwriting_recognition.py", line 519, in
prediction_model = keras.models.Model(
File "/home/fkouteib/.keras_venv/lib/python3.10/site-packages/keras/src/models/model.py", line 141, in new
return functional.Functional(*args, **kwargs)
File "/home/fkouteib/.keras_venv/lib/python3.10/site-packages/keras/src/utils/tracking.py", line 28, in wrapper
return fn(*args, **kwargs)
File "/home/fkouteib/.keras_venv/lib/python3.10/site-packages/keras/src/models/functional.py", line 162, in init
Function.init(self, inputs, outputs, name=name, **kwargs)
File "/home/fkouteib/.keras_venv/lib/python3.10/site-packages/keras/src/ops/function.py", line 62, in init
raise ValueError(
ValueError: inputs argument cannot be empty. Received:
inputs=[]

outputs=<KerasTensor shape=(None, 32, 81), dtype=float32, sparse=False, name=keras_tensor_19>

When I change
return dataset.batch(batch_size).cache.prefetch(AUTOTUNE)
to
return dataset.batch(batch_size).prefetch(AUTOTUNE)

The local rendezvous abort error and subsequent early exit persist. Have you encountered either of these issues before? any debug pointers are appreciated. Thanks.

@fchollet
Copy link
Member

Did you take a look at the data on local disk and checked everything looked good?

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Does it work with all backends or does it still require TF?

@fkouteib
Copy link
Contributor Author

fkouteib commented May 4, 2024

It's still TF only. I still have the following TF dependencies unless agnostic Keras equivalents exist that I am not aware of.

  • tf.data.Dataset() // I think works across all Keras backends.
  • tf.io.read_file()
  • tf.image.decode_png()
  • tf.strings..unicode_split()
  • tf.strings.reduce_join()
  • tf.sparse.from_dense()

Of these, the biggest blocker seems like support for sparse tensors in Keras with non-TF backend. I found keras-team/keras#18420 but don't know the current status.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants