ビジョン¶
ビジョン関連のモジュール(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
126 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
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¶
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
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 | |
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.
The encoder mode (obs_shape is None) follows the DETR convention: a learnable per-patch
positional embedding is added to the query and key tensors of every self-attention layer
(rather than being added once at the input). When cfg.cls_token is True a CLS token with
its own learnable positional embedding is prepended, and the forward pass returns the CLS
token of shape (B, d_model).
The decoder mode (obs_shape is not None) takes a CLS token of shape (B, d_model) or
(B, 1, d_model) and reconstructs an image. The CLS token is projected to a hidden
dimension and used as the key/value of cross-attention. A fixed set of
P = (H // p) * (W // p) learnable query tokens interacts with this representation
through several cross-attention layers with residual MLP blocks. Each query is then
linearly projected to p * p * C pixels and rearranged into a (B, C, H, W) image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_shape
|
tuple[int, ...]
|
Encoder mode: image shape |
required |
cfg
|
ViTConfig
|
Network configuration. |
required |
obs_shape
|
tuple[int, int, int] | None
|
Output image shape for decoder mode. |
None
|
Examples:
>>> from ml_networks.config 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=2,
... dropout=0.0,
... hidden_activation="GELU",
... output_activation="GELU",
... ),
... init_channel=3,
... )
>>> encoder = ViT(in_shape, cfg)
>>> x = torch.randn(2, *in_shape)
>>> cls = encoder(x)
>>> cls.shape
torch.Size([2, 64])
>>> decoder = ViT(in_shape=(64,), cfg=cfg, obs_shape=(3, 64, 64))
>>> y = decoder(cls)
>>> y.shape
torch.Size([2, 3, 64, 64])
Source code in src/ml_networks/torch/vision.py
Attributes¶
obs_shape
instance-attribute
¶
Functions¶
forward ¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Encoder mode: image of shape |
required |
return_cls_token
|
bool
|
Retained for backward compatibility; the encoder always returns the CLS token. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Encoder mode: CLS token of shape |
Source code in src/ml_networks/torch/vision.py
get_input_shape
staticmethod
¶
Input shape consumed by the ViT decoder: the CLS token has dimension d_model.
Source code in src/ml_networks/torch/vision.py
get_n_patches ¶
get_patch_dim ¶
patchify ¶
画像をパッチに分割する.
Source code in src/ml_networks/torch/vision.py
unpatchify ¶
パッチを画像に戻す.