Layer

생성일 비교 결과 만료 없음
7 삭제
48 행
0 추가
48 행
class TFViTMAELayer(tf.keras.layers.Layer):
class TFViTLayer(tf.keras.layers.Layer):
"""This corresponds to the Block class in the timm implementation."""
"""This corresponds to the Block class in the timm implementation."""


def __init__(self, config: ViTMAEConfig, **kwargs):
def __init__(self, config: ViTConfig, **kwargs):
super().__init__(**kwargs)
super().__init__(**kwargs)


self.attention = TFViTMAEAttention(config, name="attention")
self.attention = TFViTAttention(config, name="attention")
self.intermediate = TFViTMAEIntermediate(config, name="intermediate")
self.intermediate = TFViTIntermediate(config, name="intermediate")
self.vit_output = TFViTMAEOutput(config, name="output")
self.vit_output = TFViTOutput(config, name="output")


self.layernorm_before = tf.keras.layers.LayerNormalization(
self.layernorm_before = tf.keras.layers.LayerNormalization(
epsilon=config.layer_norm_eps, name="layernorm_before"
epsilon=config.layer_norm_eps, name="layernorm_before"
)
)
self.layernorm_after = tf.keras.layers.LayerNormalization(
self.layernorm_after = tf.keras.layers.LayerNormalization(
epsilon=config.layer_norm_eps, name="layernorm_after"
epsilon=config.layer_norm_eps, name="layernorm_after"
)
)


def call(
def call(
self,
self,
hidden_states: tf.Tensor,
hidden_states: tf.Tensor,
head_mask: tf.Tensor,
head_mask: tf.Tensor,
output_attentions: bool,
output_attentions: bool,
training: bool = False,
training: bool = False,
) -> Tuple[tf.Tensor]:
) -> Tuple[tf.Tensor]:
attention_outputs = self.attention(
attention_outputs = self.attention(
# in ViT MAE, layernorm is applied before self-attention
# in ViT, layernorm is applied before self-attention
input_tensor=self.layernorm_before(inputs=hidden_states),
input_tensor=self.layernorm_before(inputs=hidden_states),
head_mask=head_mask,
head_mask=head_mask,
output_attentions=output_attentions,
output_attentions=output_attentions,
training=training,
training=training,
)
)
attention_output = attention_outputs[0]
attention_output = attention_outputs[0]


# first residual connection
# first residual connection
hidden_states = attention_output + hidden_states
hidden_states = attention_output + hidden_states


# in ViT MAE, layernorm is also applied after self-attention
# in ViT, layernorm is also applied after self-attention
layer_output = self.layernorm_after(inputs=hidden_states)
layer_output = self.layernorm_after(inputs=hidden_states)


intermediate_output = self.intermediate(hidden_states=layer_output)
intermediate_output = self.intermediate(hidden_states=layer_output)


# second residual connection is done here
# second residual connection is done here
layer_output = self.vit_output(
layer_output = self.vit_output(
hidden_states=intermediate_output, input_tensor=hidden_states, training=training
hidden_states=intermediate_output, input_tensor=hidden_states, training=training
)
)
outputs = (layer_output,) + attention_outputs[1:] # add attentions if we output them
outputs = (layer_output,) + attention_outputs[1:] # add attentions if we output them


return outputs
return outputs