-
Notifications
You must be signed in to change notification settings - Fork 331
Implemented Coca architecture #2371
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
20cdf41
b8c0ba4
bbe17c4
202526f
367dd39
80ea7d3
3feacb6
f15408f
960873f
33cff54
145d7b5
e8623a9
c9e1ec1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from keras import layers | ||
|
|
||
|
|
||
| class AttentionPooling(layers.Layer): | ||
| """Implements the Pooled Attention Layer used in "coca": Contrastive Captioners are Image-Text Foundation Models" | ||
| (https://arxiv.org/pdf/2205.01917.pdf), consisting of a Multiheaded Attention followed by Layer Normalization. | ||
|
|
||
| Args: | ||
| proj_dim: The dimensions of the attention heads | ||
VarunS1997 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| num_heads: The number of attention heads in the multi-headed attention layer | ||
| """ | ||
| def __init__(self, | ||
VarunS1997 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| proj_dim, | ||
| num_heads, | ||
| **kwargs): | ||
| super().__init__(self, **kwargs) | ||
|
|
||
| self.proj_dim = proj_dim | ||
| self.num_heads = num_heads | ||
|
|
||
| self.multi_head_attn = layers.MultiHeadAttention( | ||
VarunS1997 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.num_heads, | ||
| self.proj_dim | ||
| ) | ||
|
|
||
| self.layer_norm = layers.LayerNormalization() | ||
VarunS1997 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def build(self, input_shape): | ||
| super().build(input_shape) | ||
|
|
||
| self.multi_head_attn.build(input_shape) | ||
| self.layer_norm.build(input_shape) | ||
|
||
|
|
||
| def call(self, query, value): | ||
| x = self.multi_head_attn(query, value) | ||
| return self.layer_norm(x) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,244 @@ | ||||||||||||
| # Copyright 2024 The KerasCV Authors | ||||||||||||
| # | ||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||
| # You may obtain a copy of the License at | ||||||||||||
| # | ||||||||||||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||
| # | ||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||
| # limitations under the License. | ||||||||||||
| import numpy as np | ||||||||||||
| from keras import Sequential | ||||||||||||
| from keras_cv.api_export import keras_cv_export | ||||||||||||
| from keras_nlp.layers import RotaryEmbedding, TransformerDecoder | ||||||||||||
|
||||||||||||
| if keras_nlp is None: | |
| raise ValueError( | |
| "ClipTokenizer requires keras-nlp. Please install " | |
| "using pip `pip install -U keras-nlp && pip install -U keras`" | |
| ) |
We could reconsider if keras-cv should hard depend on keras-nlp if we want more stuff like this? No strong feelings. @divyashreepathihalli fyi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think as we support more multi modal models we should depend on KerasNLP. If the tf-text install issue is resolved, we should add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sgtm! Though if we switch to a hard dependency here, we should probably add keras-nlp as a dependency in setup.py (which comes with a transitive dependency on tensorflow-text and tensorflow just fyi).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to include that in this PR? There's already some imports of Keras-NLP in other places of Keras CV.
If we make it a separate PR, it'll make it easier to rollback if we need to. Considering it's a new dependency, might be worth separating.
VarunS1997 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has to be changed to Example: since we follow only Example or Examples: as a standard format.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could keep batch_size as None
example
self.image_encoder.build((None, self.encoder_width, num_patches))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my understanding, is there a specific reason to do that over setting the batch_size?
VarunS1997 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't tell if this mask is the right shape or not. Usually you want something (batch_size, seq_length, seq_length) (or seq lenth + 1 if that is the effective sequence length. What is text_dim here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
text_dim is the dimensionality of the text embeddings
Uh oh!
There was an error while loading. Please reload this page.