ビジョン¶
ビジョン関連のモジュール(Encoder、Decoderなど)を提供します。
ml_networks.torch.vision(PyTorch)とml_networks.jax.vision(JAX)の両方で提供されています。
Encoder¶
Encoder ¶
Bases: BaseModule
Encoder with various architectures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_dim
|
int | tuple[int, int, int]
|
Dimension of the feature tensor. If int, Encoder includes full connection layer to downsample the feature tensor. Otherwise, Encoder does not include full connection layer and directly process with backbone network. |
required |
obs_shape
|
tuple[int, int, int]
|
shape of the input tensor |
required |
backbone_cfg
|
ViTConfig | ConvNetConfig | ResNetConfig
|
configuration of the network |
required |
fc_cfg
|
MLPConfig | LinearConfig | SpatialSoftmaxConfig | None
|
configuration of the full connection layer. If feature_dim is tuple, fc_cfg is ignored. If feature_dim is int, fc_cfg must be provided. Default is None. |
None
|
Examples:
>>> feature_dim = 128
>>> obs_shape = (3, 64, 64)
>>> cfg = ConvNetConfig(
... channels=[16, 32, 64],
... conv_cfgs=[
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ]
... )
>>> fc_cfg = LinearConfig(
... activation="ReLU",
... bias=True
... )
>>> encoder = Encoder(feature_dim, obs_shape, cfg, fc_cfg)
>>> x = torch.randn(2, *obs_shape)
>>> y = encoder(x)
>>> y.shape
torch.Size([2, 128])
>>> encoder
Encoder(
(encoder): ConvNet(
(conv): Sequential(
(0): ConvNormActivation(
(conv): Conv2d(3, 16, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(pixel_shuffle): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(1): ConvNormActivation(
(conv): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(pixel_shuffle): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(2): ConvNormActivation(
(conv): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(pixel_shuffle): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
)
)
(fc): Sequential(
(0): Flatten(start_dim=1, end_dim=-1)
(1): LinearNormActivation(
(linear): Linear(in_features=4096, out_features=128, bias=True)
(norm): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
)
)
Source code in src/ml_networks/torch/vision.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
Attributes¶
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
input tensor of shape (batch_size, *obs_shape) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
output tensor of shape (batch_size, *feature_dim) |
Source code in src/ml_networks/torch/vision.py
Decoder¶
Decoder ¶
Bases: BaseModule
Decoder with various architectures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_dim
|
int | tuple[int, int, int]
|
dimension of the feature tensor, if int, Decoder includes full connection layer to upsample the feature tensor. Otherwise, Decoder does not include full connection layer and directly process with backbone network. |
required |
obs_shape
|
tuple[int, int, int]
|
shape of the output tensor |
required |
backbone_cfg
|
ConvNetConfig | ViTConfig | ResNetConfig
|
configuration of the network |
required |
fc_cfg
|
MLPConfig | LinearConfig | None
|
configuration of the full connection layer. If feature_dim is tuple, fc_cfg is ignored. If feature_dim is int, fc_cfg must be provided. Default is None. |
None
|
Examples:
>>> feature_dim = 128
>>> obs_shape = (3, 64, 64)
>>> cfg = ConvNetConfig(
... channels=[64, 32, 16],
... conv_cfgs=[
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ]
... )
>>> fc_cfg = MLPConfig(
... hidden_dim=256,
... n_layers=2,
... output_activation= "ReLU",
... linear_cfg= LinearConfig(
... activation= "ReLU",
... bias= True
... )
... )
>>> decoder = Decoder(feature_dim, obs_shape, cfg, fc_cfg)
>>> x = torch.randn(2, feature_dim)
>>> y = decoder(x)
>>> y.shape
torch.Size([2, 3, 64, 64])
>>> decoder
Decoder(
(fc): MLPLayer(
(dense): Sequential(
(0): LinearNormActivation(
(linear): Linear(in_features=128, out_features=256, bias=True)
(norm): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(1): LinearNormActivation(
(linear): Linear(in_features=256, out_features=256, bias=True)
(norm): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(2): LinearNormActivation(
(linear): Linear(in_features=256, out_features=1024, bias=True)
(norm): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
)
)
(decoder): ConvTranspose(
(first_conv): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))
(conv): Sequential(
(0): ConvTransposeNormActivation(
(conv): ConvTranspose2d(64, 32, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(1): ConvTransposeNormActivation(
(conv): ConvTranspose2d(32, 16, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(2): ConvTransposeNormActivation(
(conv): ConvTranspose2d(16, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
)
)
)
Source code in src/ml_networks/torch/vision.py
Attributes¶
decoder
instance-attribute
¶
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
input tensor of shape (batch_size, *feature_dim) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
output tensor of shape (batch_size, *obs_shape) |
Source code in src/ml_networks/torch/vision.py
ConvNet¶
ConvNet ¶
Bases: Module
Convolutional Neural Network for Encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_shape
|
tuple[int, int, int]
|
shape of input tensor |
required |
cfg
|
ConvNetConfig
|
configuration of the network |
required |
Examples:
>>> obs_shape = (3, 64, 64)
>>> cfg = ConvNetConfig(
... channels=[16, 32, 64],
... conv_cfgs=[
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ]
... )
>>> encoder = ConvNet(obs_shape, cfg)
>>> encoder
ConvNet(
(conv): Sequential(
(0): ConvNormActivation(
(conv): Conv2d(3, 16, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(pixel_shuffle): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(1): ConvNormActivation(
(conv): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(pixel_shuffle): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(2): ConvNormActivation(
(conv): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(pixel_shuffle): Identity()
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
)
)
>>> x = torch.randn(2, *obs_shape)
>>> y = encoder(x)
>>> y.shape
torch.Size([2, 64, 8, 8])
Source code in src/ml_networks/torch/vision.py
Attributes¶
conved_shape
property
¶
Get the shape of the output tensor after convolutional layers.
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
shape of the output tensor |
Examples:
>>> obs_shape = (3, 64, 64)
>>> cfg = ConvNetConfig(
... channels=[64, 32, 16],
... conv_cfgs=[
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=3, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ]
... )
>>> encoder = ConvNet(obs_shape, cfg)
>>> encoder.conved_shape
(8, 8)
conved_size
property
¶
Get the size of the output tensor after convolutional layers.
Returns:
| Type | Description |
|---|---|
int
|
size of the output tensor |
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
input tensor of shape (batch_size, *obs_shape) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
output tensor of shape (batch_size, self.last_channel, *self.conved_shape) |
Source code in src/ml_networks/torch/vision.py
ConvTranspose¶
ConvTranspose ¶
Bases: Module
Convolutional Transpose Network for Decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_shape
|
tuple[int, int, int]
|
shape of input tensor |
required |
obs_shape
|
tuple[int, int, int]
|
shape of output tensor |
required |
cfg
|
ConvNetConfig
|
configuration of the network |
required |
Examples:
>>> in_shape = (128, 8, 8)
>>> obs_shape = (3, 64, 64)
>>> cfg = ConvNetConfig(
... channels=[64, 32, 16],
... conv_cfgs=[
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ]
... )
>>> decoder = ConvTranspose(in_shape, obs_shape, cfg)
>>> decoder
ConvTranspose(
(first_conv): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1))
(conv): Sequential(
(0): ConvTransposeNormActivation(
(conv): ConvTranspose2d(64, 32, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(1): ConvTransposeNormActivation(
(conv): ConvTranspose2d(32, 16, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
(2): ConvTransposeNormActivation(
(conv): ConvTranspose2d(16, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
(norm): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(activation): Activation(
(activation): ReLU()
)
(dropout): Identity()
)
)
)
>>> x = torch.randn(2, *in_shape)
>>> y = decoder(x)
>>> y.shape
torch.Size([2, 3, 64, 64])
Source code in src/ml_networks/torch/vision.py
Attributes¶
first_conv
instance-attribute
¶
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Tensor
|
input tensor of shape (batch_size, *in_shape) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
output tensor of shape (batch_size, *obs_shape) |
Source code in src/ml_networks/torch/vision.py
get_input_shape
staticmethod
¶
Get input shape of the decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_shape
|
tuple[int, int, int]
|
shape of the output tensor |
required |
cfg
|
ConvNetConfig
|
configuration of the network |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
shape of the input tensor |
Examples:
>>> obs_shape = (3, 64, 64)
>>> cfg = ConvNetConfig(
... channels=[64, 32, 16],
... conv_cfgs=[
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ConvConfig(kernel_size=4, stride=2, padding=1, activation="ReLU", norm="batch", dropout=0.0),
... ]
... )
>>> ConvTranspose.get_input_shape(obs_shape, cfg)
(16, 8, 8)
Source code in src/ml_networks/torch/vision.py
ResNetPixUnshuffle¶
ResNetPixUnshuffle ¶
Bases: Module
ResNet with PixelUnshuffle for Encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_shape
|
tuple[int, int, int]
|
shape of input tensor |
required |
cfg
|
ResNetConfig
|
configuration of the network |
required |
Examples:
>>> obs_shape = (3, 64, 64)
>>> cfg = ResNetConfig(
... conv_channel=64,
... conv_kernel=3,
... f_kernel=3,
... conv_activation="ReLU",
... out_activation="ReLU",
... n_res_blocks=2,
... scale_factor=2,
... n_scaling=3,
... norm="batch",
... norm_cfg={},
... dropout=0.0
... )
>>> encoder = ResNetPixUnshuffle(obs_shape, cfg)
>>> x = torch.randn(2, *obs_shape)
>>> y = encoder(x)
>>> y.shape
torch.Size([2, 64, 8, 8])
Source code in src/ml_networks/torch/vision.py
Attributes¶
conved_shape
property
¶
Get the shape of the output tensor after convolutional layers.
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
shape of the output tensor |
conved_size
property
¶
Get the size of the output tensor after convolutional layers.
Returns:
| Type | Description |
|---|---|
int
|
size of the output tensor |
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
input tensor of shape (batch_size, *obs_shape) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
output tensor of shape (batch_size, self.last_channel, *self.conved_shape) |
Source code in src/ml_networks/torch/vision.py
ResNetPixShuffle¶
ResNetPixShuffle ¶
Bases: Module
ResNet with PixelShuffle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_shape
|
tuple[int, int, int]
|
shape of input tensor |
required |
obs_shape
|
tuple[int, int, int]
|
shape of output tensor |
required |
cfg
|
ResNetConfig
|
configuration of the network |
required |
Examples:
>>> in_shape = (128, 16, 16)
>>> obs_shape = (3, 64, 64)
>>> cfg = ResNetConfig(
... conv_channel=64,
... conv_kernel=3,
... f_kernel=3,
... conv_activation="ReLU",
... out_activation="ReLU",
... n_res_blocks=2,
... scale_factor=2,
... n_scaling=2,
... norm="batch",
... norm_cfg={},
... dropout=0.0
... )
>>> decoder = ResNetPixShuffle(in_shape, obs_shape, cfg)
>>> x = torch.randn(2, *in_shape)
>>> y = decoder(x)
>>> y.shape
torch.Size([2, 3, 64, 64])
Source code in src/ml_networks/torch/vision.py
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 | |
Attributes¶
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
input tensor of shape (batch_size, *in_shape) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
output tensor of shape (batch_size, *obs_shape) |
Source code in src/ml_networks/torch/vision.py
get_input_shape
staticmethod
¶
Get input shape of the decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_shape
|
tuple[int, int, int]
|
shape of the output tensor |
required |
cfg
|
ResNetConfig
|
configuration of the network |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
shape of the input tensor |
Source code in src/ml_networks/torch/vision.py
ViT¶
ViT ¶
Bases: Module
Vision Transformer for Encoder and Decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_shape
|
tuple[int, int, int]
|
shape of input tensor |
required |
cfg
|
ViTConfig
|
configuration of the network |
required |
obs_shape
|
tuple[int, int, int] | None
|
shape of output tensor. If None, it is considered as Encoder. Default is None. |
None
|
Examples:
>>> from ml_networks.layers import TransformerConfig
>>> in_shape = (3, 64, 64)
>>> cfg = ViTConfig(
... patch_size=8,
... cls_token=True,
... transformer_cfg=TransformerConfig(
... d_model=64,
... nhead=8,
... dim_ff=256,
... n_layers=3,
... dropout=0.0,
... hidden_activation="ReLU",
... output_activation="ReLU"
... ),
... init_channel=3
... )
>>> encoder = ViT(in_shape, cfg)
>>> x = torch.randn(2, *in_shape)
>>> y = encoder(x)
>>> y.shape
torch.Size([2, 1, 64, 64])
Source code in src/ml_networks/torch/vision.py
Attributes¶
out_patch_dim
instance-attribute
¶
patch_embed
instance-attribute
¶
positional_embedding
instance-attribute
¶
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
input tensor of shape (batch_size, *in_shape) |
required |
return_cls_token
|
bool
|
whether to return cls_token. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor
|
output tensor of shape (batch_size, *obs_shape) |
Tensor
|
cls_token of shape (batch_size, self.out_patch_dim) if return_cls_token |
Source code in src/ml_networks/torch/vision.py
get_input_shape
staticmethod
¶
get_n_patches ¶
get_patch_dim ¶
patchify ¶
画像をパッチに分割する.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
imgs
|
Tensor
|
入力画像. (N, C, H, W) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
パッチ化した画像. (N, L, patch_size**2 * D) |
Source code in src/ml_networks/torch/vision.py
unpatchify ¶
パッチを画像に戻す.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
入力. (N, L, patch_size**2 * D) |
required |
Returns:
| Type | Description |
|---|---|
画像. (N, C, H, W)
|
|