From 666b4a4a1e37eadd59d55f4cf0d63c39004b1999 Mon Sep 17 00:00:00 2001 From: Jinsoo Kim Date: Mon, 9 Aug 2021 18:16:23 +0900 Subject: [PATCH] add PhysNet_ConvLSTM_2DCNN --- nets/blocks/cnn_blocks.py | 66 ++++++++++++++++++++++++++++----------- nets/models/PhysNet.py | 15 +++------ params.json | 4 +-- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/nets/blocks/cnn_blocks.py b/nets/blocks/cnn_blocks.py index 66f1638..fe1d9dc 100644 --- a/nets/blocks/cnn_blocks.py +++ b/nets/blocks/cnn_blocks.py @@ -1,35 +1,65 @@ import torch.nn +from nets.blocks.blocks import ConvBlock2D from nets.blocks.blocks import ConvBlock3D - class cnn_blocks(torch.nn.Module): def __init__(self): super(cnn_blocks, self).__init__() self.cnn_blocks = torch.nn.Sequential( - ConvBlock3D(3, 16, [1, 5, 5], [1, 1, 1], [0, 2, 2]), - torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), - ConvBlock3D(16, 32, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - ConvBlock3D(32, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), - ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), - ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), - ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), - # torch.nn.AdaptiveMaxPool3d(1) + ConvBlock2D(3, 16, [5, 5], [1, 1], [2, 2]), + torch.nn.MaxPool2d((2, 2), stride=(2, 2)), + ConvBlock2D(16, 32, [3, 3], [1, 1], [1, 1]), + ConvBlock2D(32, 64, [3, 3], [1, 1], [1, 1]), + torch.nn.MaxPool2d((2, 2), stride=(2, 2)), + ConvBlock2D(64, 64, [3, 3], [1, 1], [1, 1]), + ConvBlock2D(64, 64, [3, 3], [1, 1], [1, 1]), + torch.nn.MaxPool2d((2, 2), stride=(2, 2)), + ConvBlock2D(64, 64, [3, 3], [1, 1], [1, 1]), + ConvBlock2D(64, 64, [3, 3], [1, 1], [1, 1]), + torch.nn.MaxPool2d((2, 2), stride=(2, 2)), + ConvBlock2D(64, 64, [3, 3], [1, 1], [1, 1]), + ConvBlock2D(64, 64, [3, 3], [1, 1], [1, 1]), + torch.nn.AdaptiveMaxPool2d(1) ) def forward(self, x): [batch, channel, length, width, height] = x.shape - # x = x.reshape(batch * length, channel, width, height) - # x = self.cnn_blocks(x) - # x = x.reshape(batch,length,-1,1,1) + x = x.view(batch * length, channel, width, height) x = self.cnn_blocks(x) + x = x.view(batch,length,-1,1,1) return x +''' +Conv3D 1x3x3(paper architecture) +''' +# class cnn_blocks(torch.nn.Module): +# def __init__(self): +# super(cnn_blocks, self).__init__() +# self.cnn_blocks = torch.nn.Sequential( +# ConvBlock3D(3, 16, [1, 5, 5], [1, 1, 1], [0, 2, 2]), +# torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), +# ConvBlock3D(16, 32, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# ConvBlock3D(32, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), +# ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), +# ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# torch.nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2)), +# ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# ConvBlock3D(64, 64, [1, 3, 3], [1, 1, 1], [1, 1, 1]), +# # torch.nn.AdaptiveMaxPool3d(1) +# ) +# +# def forward(self, x): +# [batch, channel, length, width, height] = x.shape +# # x = x.reshape(batch * length, channel, width, height) +# # x = self.cnn_blocks(x) +# # x = x.reshape(batch,length,-1,1,1) +# x = self.cnn_blocks(x) +# +# return x diff --git a/nets/models/PhysNet.py b/nets/models/PhysNet.py index 16d552b..4d05c5b 100644 --- a/nets/models/PhysNet.py +++ b/nets/models/PhysNet.py @@ -25,21 +25,14 @@ def __init__(self, frame=32): super(PhysNet_2DCNN_LSTM, self).__init__() self.physnet_lstm = torch.nn.ModuleDict({ 'cnn_blocks' : cnn_blocks(), - # 'lstm' : torch.nn.LSTM(input_size=64, hidden_size=64, num_layers=2, bidirectional=True, batch_first=True), - 'spatial_global_avgpool' : torch.nn.AdaptiveMaxPool3d((frame, 1, 1)), - 'cov_lstm' : ConvLSTM(64,[1,1,64],(1,1),num_layers=3, batch_first=True,bias=True, return_all_layers=False), + 'cov_lstm' : ConvLSTM(64,[1,1,64],(1,1),num_layers=3, batch_first=True, bias=True, return_all_layers=False), 'cnn_flatten' : torch.nn.Conv3d(64, 1, [1, 1, 1], stride=1, padding=0) - }) def forward(self, x): [batch, channel, length, width, height] = x.shape x = self.physnet_lstm['cnn_blocks'](x) - # x,(_,_) = self.physnet_lstm['lstm'](x) - x = self.physnet_lstm['spatial_global_avgpool'](x) - x = x.reshape(batch, length, -1, 1, 1) - x = self.physnet_lstm['cov_lstm'](x) - # x = x.reshape(batch, channel, length, 1, 1) - x = torch.permute(x[0][0], (0, 2, 1, 3, 4)) + x,_ = self.physnet_lstm['cov_lstm'](x) + x = torch.permute(x[0], (0, 2, 1, 3, 4)) x = self.physnet_lstm['cnn_flatten'](x) - return x.reshape(-1, length) \ No newline at end of file + return x.view(-1, length) \ No newline at end of file diff --git a/params.json b/params.json index 1d7e79d..3370395 100755 --- a/params.json +++ b/params.json @@ -16,13 +16,13 @@ "train_ratio_comment" : "generate train dataset using train_ratio", "validation_ratio": 0.9, "validation_ratio_comment" : "split train dataset using validation_ratio", - "train_batch_size" : 16, + "train_batch_size" : 32, "train_batch_size_comment" : [ "PhysNet_LSTM : 8" ], "train_shuffle" : 0, - "test_batch_size" : 16, + "test_batch_size" : 32, "test_shuffle" : 0 }, "hyper_params":