From 70c0d5cead17947de979b615f634f16a18e56c08 Mon Sep 17 00:00:00 2001 From: Serein Date: Tue, 11 Apr 2023 13:27:10 +0800 Subject: [PATCH 1/2] replace out+=residual with out=out+residual --- model.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/model.py b/model.py index 80d94db..7343d5f 100644 --- a/model.py +++ b/model.py @@ -12,13 +12,14 @@ nonlinearity = partial(F.relu, inplace=True) - - def downsample(): return nn.MaxPool3d(kernel_size=2, stride=2) + + def deconv(in_channels, out_channels): return nn.ConvTranspose3d(in_channels, out_channels, kernel_size=2, stride=2) + def initialize_weights(*models): for model in models: for m in model.modules(): @@ -32,6 +33,7 @@ def initialize_weights(*models): class ResDecoder(nn.Module): + def __init__(self, in_channels): super(ResDecoder, self).__init__() self.conv1 = nn.Conv3d(in_channels, in_channels, kernel_size=3, padding=1) @@ -45,10 +47,13 @@ def forward(self, x): residual = self.conv1x1(x) out = self.relu(self.bn1(self.conv1(x))) out = self.relu(self.bn2(self.conv2(out))) - out += residual + out = out + residual out = self.relu(out) return out + + class SFConv(nn.Module): + def __init__(self, features, M=2, r=4, L=32): """ Constructor Args: @@ -75,10 +80,9 @@ def __init__(self, features, M=2, r=4, L=32): self.fc = nn.Linear(features, d) self.fcs = nn.ModuleList([]) for i in range(M): - self.fcs.append( - nn.Linear(d, features) - ) + self.fcs.append(nn.Linear(d, features)) self.softmax = nn.Softmax(dim=1) + def forward(self, x1, x2): # for i, conv in enumerate(self.convs): # fea = conv(x).unsqueeze_(dim=1) @@ -101,10 +105,10 @@ def forward(self, x1, x2): attention_vectors = attention_vectors.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) fea_v = (feas * attention_vectors).sum(dim=1) return fea_v - class SF_Decoder(nn.Module): + def __init__(self, out_channels): super(SF_Decoder, self).__init__() self.conv1 = SFConv(out_channels) @@ -125,7 +129,9 @@ def forward(self, x1, x2): # out = self.relu(out) return out + class ResEncoder(nn.Module): + def __init__(self, in_channels, out_channels): super(ResEncoder, self).__init__() self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, padding=1) @@ -139,10 +145,13 @@ def forward(self, x): residual = self.conv1x1(x) out = self.relu(self.bn1(self.conv1(x))) out = self.relu(self.bn2(self.conv2(out))) - out += residual + out = out + residual out = self.relu(out) return out + + class ER_Net(nn.Module): + def __init__(self, classes, channels): # def __init__(self): From b16baf2ad70dd7fa39a78b9d4d9885458f470a3f Mon Sep 17 00:00:00 2001 From: Serein Date: Tue, 11 Apr 2023 13:29:20 +0800 Subject: [PATCH 2/2] replace out+=residual with out=out+residual --- model.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/model.py b/model.py index 7343d5f..b0e7c10 100644 --- a/model.py +++ b/model.py @@ -12,14 +12,13 @@ nonlinearity = partial(F.relu, inplace=True) -def downsample(): - return nn.MaxPool3d(kernel_size=2, stride=2) +def downsample(): + return nn.MaxPool3d(kernel_size=2, stride=2) def deconv(in_channels, out_channels): return nn.ConvTranspose3d(in_channels, out_channels, kernel_size=2, stride=2) - def initialize_weights(*models): for model in models: for m in model.modules(): @@ -33,7 +32,6 @@ def initialize_weights(*models): class ResDecoder(nn.Module): - def __init__(self, in_channels): super(ResDecoder, self).__init__() self.conv1 = nn.Conv3d(in_channels, in_channels, kernel_size=3, padding=1) @@ -50,10 +48,7 @@ def forward(self, x): out = out + residual out = self.relu(out) return out - - class SFConv(nn.Module): - def __init__(self, features, M=2, r=4, L=32): """ Constructor Args: @@ -80,9 +75,10 @@ def __init__(self, features, M=2, r=4, L=32): self.fc = nn.Linear(features, d) self.fcs = nn.ModuleList([]) for i in range(M): - self.fcs.append(nn.Linear(d, features)) + self.fcs.append( + nn.Linear(d, features) + ) self.softmax = nn.Softmax(dim=1) - def forward(self, x1, x2): # for i, conv in enumerate(self.convs): # fea = conv(x).unsqueeze_(dim=1) @@ -105,10 +101,10 @@ def forward(self, x1, x2): attention_vectors = attention_vectors.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) fea_v = (feas * attention_vectors).sum(dim=1) return fea_v + class SF_Decoder(nn.Module): - def __init__(self, out_channels): super(SF_Decoder, self).__init__() self.conv1 = SFConv(out_channels) @@ -129,9 +125,7 @@ def forward(self, x1, x2): # out = self.relu(out) return out - class ResEncoder(nn.Module): - def __init__(self, in_channels, out_channels): super(ResEncoder, self).__init__() self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, padding=1) @@ -148,10 +142,7 @@ def forward(self, x): out = out + residual out = self.relu(out) return out - - class ER_Net(nn.Module): - def __init__(self, classes, channels): # def __init__(self):