From 4bee9be28efad952e85179ea3e42fe4b84b54f08 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Fri, 12 Apr 2024 10:47:19 +0800 Subject: [PATCH] use tensor.shape bug not paddle.shape(tensor) (#690) * use tensor.shape bug not paddle.shape(tensor) * refine --- paddlevideo/modeling/backbones/swin_transformer.py | 6 +++--- paddlevideo/modeling/backbones/toshift_vit.py | 4 ++-- paddlevideo/modeling/backbones/vit.py | 4 ++-- paddlevideo/modeling/backbones/vit_tweaks.py | 4 ++-- paddlevideo/modeling/heads/i3d_head.py | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/paddlevideo/modeling/backbones/swin_transformer.py b/paddlevideo/modeling/backbones/swin_transformer.py index 2bbc6b364..4887d0ddd 100644 --- a/paddlevideo/modeling/backbones/swin_transformer.py +++ b/paddlevideo/modeling/backbones/swin_transformer.py @@ -38,7 +38,7 @@ def drop_path(x, drop_prob=0., training=False): if drop_prob == 0. or not training: return x keep_prob = paddle.to_tensor(1 - drop_prob) - shape = (paddle.shape(x)[0], ) + (1, ) * (x.ndim - 1) + shape = (x.shape[0], ) + (1, ) * (x.ndim - 1) random_tensor = keep_prob + paddle.rand(shape, dtype=x.dtype) random_tensor = paddle.floor(random_tensor) # binarize output = x.divide(keep_prob) * random_tensor @@ -317,7 +317,7 @@ def __init__(self, drop=drop) def forward_part1(self, x, mask_matrix): - B = paddle.shape(x)[0] + B = x.shape[0] _, D, H, W, C = x.shape window_size, shift_size = get_window_size((D, H, W), self.window_size, self.shift_size) @@ -513,7 +513,7 @@ def forward(self, x): x: Input feature, tensor size (B, C, D, H, W). """ # calculate attention mask for SW-MSA - B = paddle.shape(x)[0] + B = x.shape[0] _, C, D, H, W = x.shape window_size, shift_size = get_window_size((D, H, W), self.window_size, self.shift_size) diff --git a/paddlevideo/modeling/backbones/toshift_vit.py b/paddlevideo/modeling/backbones/toshift_vit.py index a4819968e..3cce49de3 100644 --- a/paddlevideo/modeling/backbones/toshift_vit.py +++ b/paddlevideo/modeling/backbones/toshift_vit.py @@ -43,7 +43,7 @@ def drop_path(x, drop_prob=0., training=False): if drop_prob == 0. or not training: return x keep_prob = paddle.to_tensor(1 - drop_prob) - shape = (paddle.shape(x)[0], ) + (1, ) * (x.ndim - 1) + shape = (x.shape[0], ) + (1, ) * (x.ndim - 1) random_tensor = keep_prob + paddle.rand(shape).astype(x.dtype) random_tensor = paddle.floor(random_tensor) # binarize output = x.divide(keep_prob) * random_tensor @@ -374,7 +374,7 @@ def _init_fn(self, m): def forward_features(self, x): # B = x.shape[0] - B = paddle.shape(x)[0] + B = x.shape[0] x, T, W = self.patch_embed(x) # [BT,nH*nW,F] cls_tokens = self.cls_token.expand((B * T, -1, -1)) # [1,1,F]->[BT,1,F] x = paddle.concat((cls_tokens, x), axis=1) diff --git a/paddlevideo/modeling/backbones/vit.py b/paddlevideo/modeling/backbones/vit.py index 84f434f93..453d845d4 100644 --- a/paddlevideo/modeling/backbones/vit.py +++ b/paddlevideo/modeling/backbones/vit.py @@ -43,7 +43,7 @@ def drop_path(x, drop_prob=0., training=False): if drop_prob == 0. or not training: return x keep_prob = paddle.to_tensor(1 - drop_prob, dtype=x.dtype) - shape = (paddle.shape(x)[0], ) + (1, ) * (x.ndim - 1) + shape = (x.shape[0], ) + (1, ) * (x.ndim - 1) random_tensor = keep_prob + paddle.rand(shape).astype(x.dtype) random_tensor = paddle.floor(random_tensor) # binarize output = x.divide(keep_prob) * random_tensor @@ -394,7 +394,7 @@ def _init_fn(self, m): def forward_features(self, x): # B = x.shape[0] - B = paddle.shape(x)[0] + B = x.shape[0] x, T, W = self.patch_embed(x) # [BT,nH*nW,F] cls_tokens = self.cls_token.expand((B * T, -1, -1)) # [1,1,F]->[BT,1,F] x = paddle.concat((cls_tokens, x), axis=1) diff --git a/paddlevideo/modeling/backbones/vit_tweaks.py b/paddlevideo/modeling/backbones/vit_tweaks.py index a20af30f1..a90a7b4ed 100644 --- a/paddlevideo/modeling/backbones/vit_tweaks.py +++ b/paddlevideo/modeling/backbones/vit_tweaks.py @@ -65,7 +65,7 @@ def drop_path(x, drop_prob=0., training=False): if drop_prob == 0. or not training: return x keep_prob = paddle.to_tensor(1 - drop_prob) - shape = (paddle.shape(x)[0], ) + (1, ) * (x.ndim - 1) + shape = (x.shape[0], ) + (1, ) * (x.ndim - 1) random_tensor = keep_prob + paddle.rand(shape, dtype=x.dtype) random_tensor = paddle.floor(random_tensor) # binarize output = x.divide(keep_prob) * random_tensor @@ -444,7 +444,7 @@ def _init_fn(self, m): def forward_features(self, x): # B = x.shape[0] - B = paddle.shape(x)[0] + B = x.shape[0] x, T, W = self.patch_embed(x) # [BT,nH*nW,F] cls_tokens = self.cls_token.expand((B * T, -1, -1)) # [1,1,F]->[BT,1,F] x = paddle.concat((cls_tokens, x), axis=1) diff --git a/paddlevideo/modeling/heads/i3d_head.py b/paddlevideo/modeling/heads/i3d_head.py index 269c8184e..068aa39c8 100644 --- a/paddlevideo/modeling/heads/i3d_head.py +++ b/paddlevideo/modeling/heads/i3d_head.py @@ -87,7 +87,7 @@ def forward(self, x): if self.dropout is not None: x = self.dropout(x) # [N, in_channels, 1, 1, 1] - N = paddle.shape(x)[0] + N = x.shape[0] x = x.reshape([N, -1]) # [N, in_channels] cls_score = self.fc(x)