Python 如何在pytorch中使用共享编码器交替培训3个型号?
我想建立3个类似U-Net的模型,它们共享相同的编码器,但有独立的解码器(前两个模型与U-Net相同,用于分割,第三个模型没有短连接,用于重建)。我想交替地训练这3个模型,因为我有3个不同的数据集,它们有自己的标签Python 如何在pytorch中使用共享编码器交替培训3个型号?,python,deep-learning,pytorch,torch,Python,Deep Learning,Pytorch,Torch,我想建立3个类似U-Net的模型,它们共享相同的编码器,但有独立的解码器(前两个模型与U-Net相同,用于分割,第三个模型没有短连接,用于重建)。我想交替地训练这3个模型,因为我有3个不同的数据集,它们有自己的标签 model0, model1, model2 = build3models() models = [model0, model1, model2] dataloaders = [None, None, None] dataloaders[0] = Dataloader_for_mo
model0, model1, model2 = build3models()
models = [model0, model1, model2]
dataloaders = [None, None, None]
dataloaders[0] = Dataloader_for_model0
dataloaders[1] = Dataloader_for_model1
dataloaders[2] = Dataloader_for_model2
max_steps = 10000
for step in range(max_steps):
for data, model in zip(dataloaders, models]:
x, y = next(dataloader[i])
pred = model(x)
opt = torch.optim.Adam(model.parameters(), lr=1e-4)
opt.zero_grad()
criterion = nn.MSE()
loss = criterion_seg(pred , y)
loss.backward()
opt.step()
我的问题是:如何使用共享编码器构建3个模型,并针对不同的模型使用不同的损耗/opt/lr等单独或交替地对它们进行培训
我已经构建了以下编码器和解码器,但我不知道如何正确连接和使用它们
class Encoder(nn.Module):
def __init__(
self,
dimensions: int = 3,
in_channels: int = 1,
features: Sequence[int] = (32, 32, 64, 128, 256, 32),
act: Union[str, tuple] = ("LeakyReLU", {"negative_slope": 0.1, "inplace": True}),
norm: Union[str, tuple] = ("instance", {"affine": True}),
dropout: Union[float, tuple] = 0.0,
upsample: str = "deconv",
):
"""
A UNet implementation with 1D/2D/3D supports.
Based on:
Falk et al. "U-Net – Deep Learning for Cell Counting, Detection, and
Morphometry". Nature Methods 16, 67–70 (2019), DOI:
http://dx.doi.org/10.1038/s41592-018-0261-2
Args:
dimensions: number of spatial dimensions. Defaults to 3 for spatial 3D inputs.
in_channels: number of input channels. Defaults to 1.
features: six integers as numbers of features.
Defaults to ``(32, 32, 64, 128, 256, 32)``,
- the first five values correspond to the five-level encoder feature sizes.
- the last value corresponds to the feature size after the last upsampling.
act: activation type and arguments. Defaults to LeakyReLU.
norm: feature normalization type and arguments. Defaults to instance norm.
dropout: dropout ratio. Defaults to no dropout.
upsample: upsampling mode, available options are
``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``.
Examples::
# for spatial 2D
>>> net = BasicUNet(dimensions=2, features=(64, 128, 256, 512, 1024, 128))
# for spatial 2D, with group norm
>>> net = BasicUNet(dimensions=2, features=(64, 128, 256, 512, 1024, 128), norm=("group", {"num_groups": 4}))
# for spatial 3D
>>> net = BasicUNet(dimensions=3, features=(32, 32, 64, 128, 256, 32))
See Also
- :py:class:`monai.networks.nets.DynUNet`
- :py:class:`monai.networks.nets.UNet`
"""
super().__init__()
fea = ensure_tuple_rep(features, 6)
print(f"BasicUNet features: {fea}.")
self.conv_0 = TwoConv(dimensions, in_channels, features[0], act, norm, dropout)
self.down_1 = Down(dimensions, fea[0], fea[1], act, norm, dropout)
self.down_2 = Down(dimensions, fea[1], fea[2], act, norm, dropout)
self.down_3 = Down(dimensions, fea[2], fea[3], act, norm, dropout)
self.down_4 = Down(dimensions, fea[3], fea[4], act, norm, dropout)
def forward(self, x: torch.Tensor):
"""
Args:
x: input should have spatially N dimensions
``(Batch, in_channels, dim_0[, dim_1, ..., dim_N])``, N is defined by `dimensions`.
It is recommended to have ``dim_n % 16 == 0`` to ensure all maxpooling inputs have
even edge lengths.
Returns:
A torch Tensor of "raw" predictions in shape
``(Batch, out_channels, dim_0[, dim_1, ..., dim_N])``.
"""
x0 = self.conv_0(x)
x1 = self.down_1(x0)
x2 = self.down_2(x1)
x3 = self.down_3(x2)
x4 = self.down_4(x3)
return x0, x1, x2, x3, x4
class DecoderSeg(nn.Module):
def __init__(
self,
dimensions: int = 3,
in_channels: int = 1,
out_channels: int = 2,
features: Sequence[int] = (32, 32, 64, 128, 256, 32),
act: Union[str, tuple] = ("LeakyReLU", {"negative_slope": 0.1, "inplace": True}),
norm: Union[str, tuple] = ("instance", {"affine": True}),
dropout: Union[float, tuple] = 0.0,
upsample: str = "deconv",
):
"""
A UNet implementation with 1D/2D/3D supports.
Based on:
Falk et al. "U-Net – Deep Learning for Cell Counting, Detection, and
Morphometry". Nature Methods 16, 67–70 (2019), DOI:
http://dx.doi.org/10.1038/s41592-018-0261-2
Args:
dimensions: number of spatial dimensions. Defaults to 3 for spatial 3D inputs.
in_channels: number of input channels. Defaults to 1.
out_channels: number of output channels. Defaults to 2.
features: six integers as numbers of features.
Defaults to ``(32, 32, 64, 128, 256, 32)``,
- the first five values correspond to the five-level encoder feature sizes.
- the last value corresponds to the feature size after the last upsampling.
act: activation type and arguments. Defaults to LeakyReLU.
norm: feature normalization type and arguments. Defaults to instance norm.
dropout: dropout ratio. Defaults to no dropout.
upsample: upsampling mode, available options are
``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``.
Examples::
# for spatial 2D
>>> net = BasicUNet(dimensions=2, features=(64, 128, 256, 512, 1024, 128))
# for spatial 2D, with group norm
>>> net = BasicUNet(dimensions=2, features=(64, 128, 256, 512, 1024, 128), norm=("group", {"num_groups": 4}))
# for spatial 3D
>>> net = BasicUNet(dimensions=3, features=(32, 32, 64, 128, 256, 32))
See Also
- :py:class:`monai.networks.nets.DynUNet`
- :py:class:`monai.networks.nets.UNet`
"""
super().__init__()
fea = ensure_tuple_rep(features, 6)
print(f"BasicUNet features: {fea}.")
self.upcat_4 = UpCat(dimensions, fea[4], fea[3], fea[3], act, norm, dropout, upsample)
self.upcat_3 = UpCat(dimensions, fea[3], fea[2], fea[2], act, norm, dropout, upsample)
self.upcat_2 = UpCat(dimensions, fea[2], fea[1], fea[1], act, norm, dropout, upsample)
self.upcat_1 = UpCat(dimensions, fea[1], fea[0], fea[5], act, norm, dropout, upsample, halves=False)
self.final_conv = Conv["conv", dimensions](fea[5], out_channels, kernel_size=1)
def forward(self,
x0: torch.Tensor,
x1: torch.Tensor,
x2: torch.Tensor,
x3: torch.Tensor,
x4: torch.Tensor):
"""
Args:
x: input should have spatially N dimensions
``(Batch, in_channels, dim_0[, dim_1, ..., dim_N])``, N is defined by `dimensions`.
It is recommended to have ``dim_n % 16 == 0`` to ensure all maxpooling inputs have
even edge lengths.
Returns:
A torch Tensor of "raw" predictions in shape
``(Batch, out_channels, dim_0[, dim_1, ..., dim_N])``.
"""
u4 = self.upcat_4(x4, x3)
u3 = self.upcat_3(u4, x2)
u2 = self.upcat_2(u3, x1)
u1 = self.upcat_1(u2, x0)
u_out = self.final_conv(u1)
return u_out
class DecoderRec(nn.Module):
def __init__(
self,
dimensions: int = 3,
in_channels: int = 1,
out_channels: int = 1,
features: Sequence[int] = (32, 32, 64, 128, 256, 32),
act: Union[str, tuple] = ("LeakyReLU", {"negative_slope": 0.1, "inplace": True}),
norm: Union[str, tuple] = ("instance", {"affine": True}),
dropout: Union[float, tuple] = 0.0,
upsample: str = "deconv",
):
"""
A UNet implementation with 1D/2D/3D supports.
Based on:
Falk et al. "U-Net – Deep Learning for Cell Counting, Detection, and
Morphometry". Nature Methods 16, 67–70 (2019), DOI:
http://dx.doi.org/10.1038/s41592-018-0261-2
Args:
dimensions: number of spatial dimensions. Defaults to 3 for spatial 3D inputs.
in_channels: number of input channels. Defaults to 1.
out_channels: number of output channels. Defaults to 2.
features: six integers as numbers of features.
Defaults to ``(32, 32, 64, 128, 256, 32)``,
- the first five values correspond to the five-level encoder feature sizes.
- the last value corresponds to the feature size after the last upsampling.
act: activation type and arguments. Defaults to LeakyReLU.
norm: feature normalization type and arguments. Defaults to instance norm.
dropout: dropout ratio. Defaults to no dropout.
upsample: upsampling mode, available options are
``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``.
Examples::
# for spatial 2D
>>> net = BasicUNet(dimensions=2, features=(64, 128, 256, 512, 1024, 128))
# for spatial 2D, with group norm
>>> net = BasicUNet(dimensions=2, features=(64, 128, 256, 512, 1024, 128), norm=("group", {"num_groups": 4}))
# for spatial 3D
>>> net = BasicUNet(dimensions=3, features=(32, 32, 64, 128, 256, 32))
See Also
- :py:class:`monai.networks.nets.DynUNet`
- :py:class:`monai.networks.nets.UNet`
"""
super().__init__()
fea = ensure_tuple_rep(features, 6)
print(f"BasicUNet features: {fea}.")
self.up_4 = Up(dimensions, fea[4], fea[3], fea[3], act, norm, dropout, upsample)
self.up_3 = Up(dimensions, fea[3], fea[2], fea[2], act, norm, dropout, upsample)
self.up_2 = Up(dimensions, fea[2], fea[1], fea[1], act, norm, dropout, upsample)
self.up_1 = Up(dimensions, fea[1], fea[0], fea[5], act, norm, dropout, upsample, halves=False)
self.final_conv = Conv["conv", dimensions](fea[5], out_channels, kernel_size=1)
def forward(self, x: torch.Tensor):
"""
Args:
x: input should have spatially N dimensions
``(Batch, in_channels, dim_0[, dim_1, ..., dim_N])``, N is defined by `dimensions`.
It is recommended to have ``dim_n % 16 == 0`` to ensure all maxpooling inputs have
even edge lengths.
Returns:
A torch Tensor of "raw" predictions in shape
``(Batch, out_channels, dim_0[, dim_1, ..., dim_N])``.
"""
r4 = self.up_4(x)
r3 = self.up_3(r4)
r2 = self.up_2(r3)
r1 = self.up_1(r2)
r_out = self.final_conv(r1)
return r_out