update riformer mmpretrain

pull/1453/head
techmonsterwang 2023-04-04 13:17:16 +08:00 committed by Ezra-Yu
parent e7da3f29f4
commit a6c24d104e
7 changed files with 5 additions and 100 deletions

View File

@ -27,7 +27,6 @@ model = dict(
loss=dict(type='CrossEntropyLoss', loss_weight=1.0),
))
# schedule settings
optim_wrapper = dict(
optimizer=dict(lr=4e-3),

View File

@ -27,7 +27,6 @@ model = dict(
loss=dict(type='CrossEntropyLoss', loss_weight=1.0),
))
# schedule settings
optim_wrapper = dict(
optimizer=dict(lr=4e-3),

View File

@ -27,7 +27,6 @@ model = dict(
loss=dict(type='CrossEntropyLoss', loss_weight=1.0),
))
# schedule settings
optim_wrapper = dict(
optimizer=dict(lr=4e-3),

View File

@ -27,7 +27,6 @@ model = dict(
loss=dict(type='CrossEntropyLoss', loss_weight=1.0),
))
# schedule settings
optim_wrapper = dict(
optimizer=dict(lr=4e-3),

View File

@ -3,67 +3,12 @@ from typing import Sequence
import torch
import torch.nn as nn
from mmcv.cnn.bricks import DropPath, build_activation_layer, build_norm_layer
from mmcv.cnn.bricks import DropPath, build_norm_layer
from mmengine.model import BaseModule
from mmpretrain.registry import MODELS
from .base_backbone import BaseBackbone
class PatchEmbed(nn.Module):
"""Patch Embedding module implemented by a layer of convolution.
Input: tensor in shape [B, C, H, W]
Output: tensor in shape [B, C, H/stride, W/stride]
Args:
patch_size (int): Patch size of the patch embedding. Defaults to 16.
stride (int): Stride of the patch embedding. Defaults to 16.
padding (int): Padding of the patch embedding. Defaults to 0.
in_chans (int): Input channels. Defaults to 3.
embed_dim (int): Output dimension of the patch embedding.
Defaults to 768.
norm_layer (module): Normalization module. Defaults to None (not use).
"""
def __init__(self,
patch_size=16,
stride=16,
padding=0,
in_chans=3,
embed_dim=768,
norm_layer=None):
super().__init__()
self.proj = nn.Conv2d(
in_chans,
embed_dim,
kernel_size=patch_size,
stride=stride,
padding=padding)
self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
def forward(self, x):
x = self.proj(x)
x = self.norm(x)
return x
class Pooling(nn.Module):
"""Pooling module.
Args:
pool_size (int): Pooling size. Defaults to 3.
"""
def __init__(self, pool_size=3):
super().__init__()
self.pool = nn.AvgPool2d(
pool_size,
stride=1,
padding=pool_size // 2,
count_include_pad=False)
def forward(self, x):
return self.pool(x) - x
from .poolformer import Mlp, PatchEmbed
class Affine(nn.Module):
@ -88,43 +33,6 @@ class Affine(nn.Module):
return self.affine(x) - x
class Mlp(nn.Module):
"""Mlp implemented by with 1*1 convolutions.
Input: Tensor with shape [B, C, H, W].
Output: Tensor with shape [B, C, H, W].
Args:
in_features (int): Dimension of input features.
hidden_features (int): Dimension of hidden features.
out_features (int): Dimension of output features.
act_cfg (dict): The config dict for activation between pointwise
convolution. Defaults to ``dict(type='GELU')``.
drop (float): Dropout rate. Defaults to 0.0.
"""
def __init__(self,
in_features,
hidden_features=None,
out_features=None,
act_cfg=dict(type='GELU'),
drop=0.):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Conv2d(in_features, hidden_features, 1)
self.act = build_activation_layer(act_cfg)
self.fc2 = nn.Conv2d(hidden_features, out_features, 1)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class RIFormerBlock(BaseModule):
"""RIFormer Block.

View File

@ -3,6 +3,7 @@ import argparse
from pathlib import Path
import torch
from mmpretrain.apis import init_model
from mmpretrain.models.classifiers import ImageClassifier