From 791a4111251c9cfbee6aedfdd5d696cd8cc45eca Mon Sep 17 00:00:00 2001 From: DaeyeolKim Date: Fri, 30 Jul 2021 14:24:32 +0900 Subject: [PATCH] modify attentionblock --- main.py | 8 ++++++-- nets/blocks/attentionBlocks.py | 18 +++++++++++++++++- nets/models/DeepPhys.py | 2 +- nets/models/sub_models/MotionModel.py | 2 +- params.json | 10 +++++----- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 92f2b4e..67e0d0d 100755 --- a/main.py +++ b/main.py @@ -198,10 +198,14 @@ else: inference_array.extend(outputs.cpu().numpy()) target_array.extend(target.cpu().numpy()) + if tepoch.n == 0 and __TIME__: + save_time = time.time() + if model_params["name"] == "DeepPhys": inference_array = scipy.signal.detrend(np.cumsum(inference_array)) target_array = scipy.signal.detrend(np.cumsum(target_array)) + if __TIME__ and epoch == 0: + log_info_time("inference time \t: ", datetime.timedelta(seconds=save_time - start_time)) + plot_graph(0, 300, target_array, inference_array) - if __TIME__ and epoch == 0: - log_info_time("inference time \t: ", datetime.timedelta(seconds=time.time() - start_time)) diff --git a/nets/blocks/attentionBlocks.py b/nets/blocks/attentionBlocks.py index 43a6ef7..74d303c 100755 --- a/nets/blocks/attentionBlocks.py +++ b/nets/blocks/attentionBlocks.py @@ -4,7 +4,7 @@ from ..modules.modules import DAModule -class AttentionBlock(nn.Module): +class AttentionBlock_DA(nn.Module): def __init__(self, in_channels): super().__init__() self.attention = DAModule(in_channels) @@ -19,3 +19,19 @@ def forward(self, input): mask = torch.div(mask * H * W, norm) mask = self.conv1x1(mask) return mask + + + +class AttentionBlock(torch.nn.Module): + def __init__(self, in_channels): + super().__init__() + self.attention = torch.nn.Conv2d(in_channels, 1, kernel_size=1, stride=1, padding=0) + + def forward(self, input): + mask = self.attention(input) + mask = torch.sigmoid(mask) + B, _, H, W = input.shape + norm = 2 * torch.norm(mask, p=1, dim=(1, 2, 3)) + norm = norm.reshape(B, 1, 1, 1) + mask = torch.div(mask * H * W, norm) + return mask diff --git a/nets/models/DeepPhys.py b/nets/models/DeepPhys.py index e828a86..ffc66d8 100644 --- a/nets/models/DeepPhys.py +++ b/nets/models/DeepPhys.py @@ -14,7 +14,7 @@ def __init__(self): self.attention_mask1 = None self.attention_mask2 = None - self.appearance_model = AppearanceModel_2D(in_channels=self.in_channels, out_channels=self.out_channels * 4, + self.appearance_model = AppearanceModel_2D(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=self.kernel_size) self.motion_model = MotionModel(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=self.kernel_size) diff --git a/nets/models/sub_models/MotionModel.py b/nets/models/sub_models/MotionModel.py index f7e512b..7bb1ae0 100644 --- a/nets/models/sub_models/MotionModel.py +++ b/nets/models/sub_models/MotionModel.py @@ -28,7 +28,7 @@ def forward(self, inputs, mask1, mask2): M1 = torch.tanh(self.m_batch_Normalization1(self.m_conv1(inputs))) M2 = self.m_batch_Normalization2(self.m_conv2(M1)) # element wise multiplication Mask1 - g1 = torch.tanh(torch.mul(1 * mask1, M2)) + g1 = torch.tanh(torch.mul(torch.ones(size= mask1.shape).to('cuda')@mask1, M2)) M3 = self.m_dropout1(g1) # pooling M4 = self.m_avg1(M3) diff --git a/params.json b/params.json index cb4bab8..0221471 100755 --- a/params.json +++ b/params.json @@ -1,7 +1,7 @@ { "__TIME__" : 1, "__PREPROCESSING__" : 0, - "__MODEL_SUMMARY__" : 0, + "__MODEL_SUMMARY__" : 1, "options":{ "parallel_criterion" : 1, "parallel_criterion_comment" : "TODO need to verification" @@ -18,7 +18,7 @@ "validation_ratio_comment" : "split train dataset using validation_ratio", "train_batch_size" : 32, "train_shuffle" : 0, - "test_batch_size" : 1, + "test_batch_size" : 32, "test_shuffle" : 0 }, "hyper_params": @@ -38,16 +38,16 @@ "adam","sgd","rms_prop","ada_delta","ada_grad","ada_max", "ada_mw","a_sgd","lbfgs","n_adam","r_adam","rprop","sparse_adam" ], - "learning_rate": 1, + "learning_rate": 0.001, "learning_rate_comment": [ "DeepPhys : lr = 1", "PhysNet : lr = 0.001" ], - "epochs" : 1 + "epochs" : 30 }, "model_params": { - "name": "PhysNet", + "name": "DeepPhys", "name_comment": [ "DeepPhys",