|
| 1 | +import keras |
| 2 | + |
| 3 | +from keras_hub.src.api_export import keras_hub_export |
| 4 | +from keras_hub.src.models.backbone import Backbone |
| 5 | +from keras_hub.src.models.parseq.parseq_decoder import PARSeqDecoder |
| 6 | + |
| 7 | + |
| 8 | +@keras_hub_export("keras_hub.models.PARSeqBackbone") |
| 9 | +class PARSeqBackbone(Backbone): |
| 10 | + """Scene Text Detection with PARSeq. |
| 11 | +
|
| 12 | + Performs OCR in natural scenes using the PARSeq model described in [Scene |
| 13 | + Text Recognition with Permuted Autoregressive Sequence Models]( |
| 14 | + https://arxiv.org/abs/2207.06966). PARSeq is a ViT-based model that allows |
| 15 | + iterative decoding by performing an autoregressive decoding phase, followed |
| 16 | + by a refinement phase. |
| 17 | +
|
| 18 | + Args: |
| 19 | + image_encoder: keras.Model. The image encoder model. |
| 20 | + vocabulary_size: int. The size of the vocabulary. |
| 21 | + max_label_length: int. The maximum length of the label sequence. |
| 22 | + decoder_hidden_dim: int. The dimension of the decoder hidden layers. |
| 23 | + num_decoder_layers: int. The number of decoder layers. |
| 24 | + num_decoder_heads: int. The number of attention heads in the decoder. |
| 25 | + decoder_mlp_dim: int. The dimension of the decoder MLP hidden layer. |
| 26 | + dropout_rate: float. The dropout rate for the decoder network. |
| 27 | + Defaults to `0.1`. |
| 28 | + attention_dropout: float. The dropout rate for the attention weights. |
| 29 | + Defaults to `0.1`. |
| 30 | + dtype: str. `None`, str, or `keras.mixed_precision.DTypePolicy`. The |
| 31 | + dtype to use for the computations and weights. |
| 32 | + **kwargs: Additional keyword arguments passed to the base |
| 33 | + `keras.Model` constructor. |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + image_encoder, |
| 39 | + vocabulary_size, |
| 40 | + max_label_length, |
| 41 | + decoder_hidden_dim, |
| 42 | + num_decoder_layers, |
| 43 | + num_decoder_heads, |
| 44 | + decoder_mlp_dim, |
| 45 | + dropout_rate=0.1, |
| 46 | + attention_dropout=0.1, |
| 47 | + dtype=None, |
| 48 | + **kwargs, |
| 49 | + ): |
| 50 | + # === Layers === |
| 51 | + self.image_encoder = image_encoder |
| 52 | + self.decoder = PARSeqDecoder( |
| 53 | + vocabulary_size=vocabulary_size, |
| 54 | + max_label_length=max_label_length, |
| 55 | + num_layers=num_decoder_layers, |
| 56 | + num_heads=num_decoder_heads, |
| 57 | + hidden_dim=decoder_hidden_dim, |
| 58 | + mlp_dim=decoder_mlp_dim, |
| 59 | + dropout_rate=dropout_rate, |
| 60 | + attention_dropout=attention_dropout, |
| 61 | + name="decoder", |
| 62 | + dtype=dtype, |
| 63 | + ) |
| 64 | + self.head = keras.layers.Dense( |
| 65 | + vocabulary_size - 2, # We don't predict <bos> nor <pad> |
| 66 | + dtype=dtype, |
| 67 | + ) |
| 68 | + |
| 69 | + # === Functional Model === |
| 70 | + image_input = self.image_encoder.input |
| 71 | + |
| 72 | + token_id_input = keras.Input( |
| 73 | + shape=(None,), dtype="int32", name="token_ids" |
| 74 | + ) |
| 75 | + padding_mask_input = keras.Input( |
| 76 | + shape=(None,), dtype="int32", name="padding_mask" |
| 77 | + ) |
| 78 | + |
| 79 | + memory = self.image_encoder(image_input) |
| 80 | + target_out = self.decoder( |
| 81 | + token_id_input, memory, padding_mask=padding_mask_input |
| 82 | + ) |
| 83 | + logits = self.head(target_out) |
| 84 | + |
| 85 | + # === Config === |
| 86 | + self.vocabulary_size = vocabulary_size |
| 87 | + self.max_label_length = max_label_length |
| 88 | + self.decoder_hidden_dim = decoder_hidden_dim |
| 89 | + self.num_decoder_layers = num_decoder_layers |
| 90 | + self.num_decoder_heads = num_decoder_heads |
| 91 | + self.decoder_mlp_dim = decoder_mlp_dim |
| 92 | + self.dropout_rate = dropout_rate |
| 93 | + self.attention_dropout = attention_dropout |
| 94 | + |
| 95 | + super().__init__( |
| 96 | + inputs={ |
| 97 | + "images": image_input, |
| 98 | + "token_ids": token_id_input, |
| 99 | + "padding_mask": padding_mask_input, |
| 100 | + }, |
| 101 | + outputs=logits, |
| 102 | + dtype=dtype, |
| 103 | + **kwargs, |
| 104 | + ) |
| 105 | + |
| 106 | + def get_config(self): |
| 107 | + config = super().get_config() |
| 108 | + config.update( |
| 109 | + { |
| 110 | + "image_encoder": keras.layers.serialize(self.image_encoder), |
| 111 | + "vocabulary_size": self.vocabulary_size, |
| 112 | + "max_label_length": self.max_label_length, |
| 113 | + "decoder_hidden_dim": self.decoder_hidden_dim, |
| 114 | + "num_decoder_layers": self.num_decoder_layers, |
| 115 | + "num_decoder_heads": self.num_decoder_heads, |
| 116 | + "decoder_mlp_dim": self.decoder_mlp_dim, |
| 117 | + "dropout_rate": self.dropout_rate, |
| 118 | + "attention_dropout": self.attention_dropout, |
| 119 | + } |
| 120 | + ) |
| 121 | + |
| 122 | + return config |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def from_config(cls, config): |
| 126 | + config.update( |
| 127 | + { |
| 128 | + "image_encoder": keras.layers.deserialize( |
| 129 | + config["image_encoder"] |
| 130 | + ), |
| 131 | + } |
| 132 | + ) |
| 133 | + |
| 134 | + return super().from_config(config) |
0 commit comments