MCPcopy Create free account
hub / github.com/PaddlePaddle/Research / vit_encode

Method vit_encode

NLP/UNIMO-2/src/model/unimo_grounded.py:488–564  ·  view source on GitHub ↗

encode image by pre-trained ViT

(self, image_input, image_mask)

Source from the content-addressed store, hash-verified

486 raise ValueError("The input is invalid")
487
488 def vit_encode(self, image_input, image_mask):
489 """encode image by pre-trained ViT"""
490 assert image_mask is not None, "text_mask should not be none"
491 image_self_attn_mask = paddle.matmul(x=paddle.transpose(image_mask, perm=[0, 2, 1]), y=image_mask)
492 self_attn_mask = paddle.scale(
493 x=image_self_attn_mask, scale=1e4, bias=-1.0, bias_after_scale=False)
494 n_head_self_attn_mask = paddle.stack(
495 x=[self_attn_mask] * self._vit_n_head, axis=1)
496 n_head_self_attn_mask.stop_gradient = True
497
498 pixel_embeddings = paddle.static.nn.conv2d(
499 input=image_input['pixel_embedding'],
500 num_filters=self._emb_size,
501 filter_size=self.resolution,
502 stride=self.resolution,
503 padding=(self.resolution - 1) // 2,
504 param_attr=paddle.ParamAttr(
505 name="vit.patch_embeddings_projection_weight",
506 trainable=self._vit_encoder_trainable),
507 bias_attr=paddle.ParamAttr(
508 name="vit.patch_embeddings_projection_bias",
509 trainable=self._vit_encoder_trainable),
510 data_format="NHWC")
511 # paddle.static.Print(paddle.shape(pixel_embeddings), message="pixel_embeddings", summarize=-1)
512 pixel_embeddings = paddle.reshape(pixel_embeddings, shape=[-1, self.patch_seq_len, self._emb_size])
513
514 cls_token_emb = paddle.static.create_parameter(
515 shape=[1, 1, self._emb_size],
516 dtype=self._emb_dtype,
517 attr=paddle.ParamAttr(
518 name="vit.cls_token_embeddings",
519 trainable=self._vit_encoder_trainable,
520 initializer=self._param_initializer))
521 cls_token_emb = paddle.expand(x=cls_token_emb,
522 shape=[paddle.shape(pixel_embeddings)[0], 1, self._emb_size])
523
524 # cpncate global [CLS] token with image patches
525 # (batch_size, patch_seq_len + 1, emb_dim)
526 all_pixel_embeddings = paddle.concat(x=[cls_token_emb, pixel_embeddings], axis=1)
527
528 # default image_size=224, resolution=16, patch_seq_len=196
529 pixel_pos_emb = paddle.static.create_parameter(
530 shape=[1, 197, self._emb_size],
531 dtype=self._emb_dtype,
532 attr=paddle.ParamAttr(name="vit.position_embeddings",
533 trainable=self._vit_encoder_trainable,
534 initializer=self._param_initializer))
535 # paddle.static.Print(paddle.shape(pixel_pos_emb), message="pixel_pos_emb", summarize=-1)
536 if self.patch_seq_len > 196: # when image_size > 224
537 pixel_pos_emb = interpolate_pos_embed(pixel_pos_emb, self.patch_seq_len)
538
539 emb_v_out = all_pixel_embeddings + pixel_pos_emb
540 emb_v_out = vit_pre_process_layer(
541 emb_v_out, 'd', self._vit_prepostprocess_dropout, name='vit.pre_encoder',
542 trainable=self._vit_encoder_trainable)
543
544 vit_enc_out, checkpoints = vit_encoder(
545 enc_input=emb_v_out,

Callers 1

_gen_inputMethod · 0.95

Calls 2

interpolate_pos_embedFunction · 0.90
transposeMethod · 0.45

Tested by

no test coverage detected