├── LICENSE ├── README.md └── mim.py /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Memory In Memory Networks 2 | 3 | It is based on the paper Memory In Memory: A Predictive Neural Network for Learning Higher-Order Non-Stationarity from Spatiotemporal Dynamics to be presented at CVPR 2019. [Official Repo](https://github.com/Yunbo426/MIM) 4 | 5 | -------------------------------------------------------------------------------- /mim.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | import torch 4 | import torch.nn as nn 5 | 6 | 7 | class SpatioTemporalLSTMCell(nn.Module): 8 | def __init__(self, in_channel, num_hidden, kernel_size, in_shape, bias=True): 9 | """ 10 | """ 11 | super(SpatioTemporalLSTMCell, self).__init__() 12 | 13 | self.in_channel = in_channel 14 | self.num_hidden = num_hidden 15 | self.kernel_size = kernel_size 16 | self.batch = in_shape[0] 17 | self.channel = in_shape[1] 18 | self.height = in_shape[2] 19 | self.width = in_shape[3] 20 | self.padding = kernel_size // 2 21 | self.bias = bias 22 | 23 | self.t_cc = nn.Conv2d(num_hidden, self.num_hidden * 4, 24 | kernel_size=self.kernel_size, 25 | stride=1, padding=self.padding, 26 | bias=bias 27 | ) 28 | 29 | self.s_cc = nn.Conv2d(num_hidden, self.num_hidden * 4, 30 | kernel_size=self.kernel_size, 31 | stride=1, padding=self.padding, 32 | bias=bias 33 | ) 34 | 35 | self.x_cc = nn.Conv2d(in_channel, self.num_hidden * 4, 36 | kernel_size=self.kernel_size, 37 | stride=1, padding=self.padding, 38 | bias=bias 39 | ) 40 | 41 | self.last = nn.Conv2d(num_hidden*2, self.num_hidden, 42 | kernel_size=1, stride=1, padding=0, 43 | bias=bias 44 | ) 45 | 46 | 47 | def init_state(self): 48 | return torch.zeros([self.batch, self.num_hidden, self.height, self.width], 49 | dtype=torch.float32) 50 | 51 | 52 | def forward(self, x, h, c, m): 53 | if h is None: 54 | h = self.init_state().to(x.device) 55 | if c is None: 56 | c = self.init_state().to(x.device) 57 | if m is None: 58 | m = self.init_state().to(x.device) 59 | 60 | t_cc = self.t_cc(h) 61 | s_cc = self.s_cc(m) 62 | x_cc = self.x_cc(x) 63 | 64 | i_s, g_s, f_s, o_s = torch.split(s_cc, self.num_hidden, dim=1) 65 | i_t, g_t, f_t, o_t = torch.split(t_cc, self.num_hidden, dim=1) 66 | i_x, g_x, f_x, o_x = torch.split(x_cc, self.num_hidden, dim=1) 67 | 68 | i = torch.sigmoid(i_x + i_t) 69 | i_ = torch.sigmoid(i_x + i_s) 70 | g = torch.tanh(g_x + g_t) 71 | g_ = torch.tanh(g_x + g_s) 72 | f = torch.sigmoid(f_x + f_t) 73 | f_ = torch.sigmoid(f_x + f_s) 74 | o = torch.sigmoid(o_x + o_t + o_s) 75 | new_m = f_ * m + i_ * g_ 76 | new_c = f * c + i * g 77 | 78 | cell = torch.cat([new_m, new_c], dim=1) 79 | cell = self.last(cell) 80 | 81 | new_h = o * torch.tanh(cell) 82 | 83 | return new_h, new_c, new_m 84 | 85 | 86 | class MIMS(nn.Module): 87 | def __init__(self, in_channel, num_hidden, kernel_size, in_shape, stride=1, bias=True): 88 | super(MIMS, self).__init__() 89 | 90 | self.in_channel = in_channel 91 | self.num_hidden = num_hidden 92 | self.kernel_size = kernel_size 93 | self.stride = stride 94 | self.batch = in_shape[0] 95 | self.channel = in_shape[1] 96 | self.height = in_shape[2] 97 | self.width = in_shape[3] 98 | self.padding = kernel_size // 2 99 | self.bias = bias 100 | 101 | self.conv_h = nn.Sequential(nn.Conv2d(self.num_hidden, self.num_hidden * 4, 102 | kernel_size=self.kernel_size, 103 | stride=self.stride, padding=self.padding, 104 | bias=self.bias 105 | ), 106 | #nn.LayerNorm([num_hidden*4, self.height, self.width]) 107 | ) 108 | 109 | self.conv_x = nn.Sequential(nn.Conv2d(self.in_channel, self.num_hidden * 4, 110 | kernel_size=self.kernel_size, 111 | stride=self.stride, padding=self.padding, 112 | bias=self.bias 113 | ), 114 | #nn.LayerNorm([num_hidden*4, self.height, self.width]) 115 | ) 116 | 117 | ct_weight_ = torch.empty((self.num_hidden*2, self.height, self.width)) 118 | self.ct_weight = nn.Parameter(nn.init.kaiming_normal_(ct_weight_)) 119 | oc_weight_ = torch.empty((self.num_hidden, self.height, self.width)) 120 | self.oc_weight = nn.Parameter(nn.init.kaiming_normal_(oc_weight_)) 121 | 122 | def init_state(self): 123 | return torch.zeros([self.batch, self.num_hidden, self.height, self.width], 124 | dtype=torch.float32) 125 | 126 | def forward(self, x, h_t, c_t): 127 | if h_t is None: 128 | h_t = self.init_state().to(x.device) 129 | if c_t is None: 130 | c_t = self.init_state().to(x.device) 131 | 132 | h_concat = self.conv_h(h_t) 133 | g_h, i_h, f_h, o_h = torch.split(h_concat, self.num_hidden, dim=1) 134 | 135 | ct_activation = c_t.repeat([1,2,1,1]) * self.ct_weight 136 | i_c, f_c = torch.split(ct_activation, self.num_hidden, dim=1) 137 | i_ = i_h + i_c 138 | f_ = f_h + f_c 139 | g_ = g_h 140 | o_ = o_h 141 | 142 | if x != None: 143 | x_concat = self.conv_x(x) 144 | i_x, g_x, f_x, o_x = torch.split(x_concat, self.num_hidden, dim=1) 145 | 146 | i_ += i_x 147 | f_ += f_x 148 | g_ += g_x 149 | o_ += o_x 150 | 151 | i_ = torch.sigmoid(i_) 152 | f_ = torch.sigmoid(f_) 153 | c_new = f_ * c_t + i_ * torch.tanh(g_) 154 | 155 | o_c = o_ + c_new * self.oc_weight 156 | 157 | h_new = torch.sigmoid(o_c) * torch.tanh(c_new) 158 | 159 | return h_new, c_new 160 | 161 | 162 | class MIMBlock(nn.Module): 163 | def __init__(self, in_channel, num_hidden, kernel_size, in_shape, 164 | stride=1, bias=True): 165 | super(MIMBlock, self).__init__() 166 | self.in_channel = in_channel 167 | self.num_hidden = num_hidden 168 | self.kernel_size = kernel_size 169 | self.stride = stride 170 | self.batch = in_shape[0] 171 | self.height = in_shape[2] 172 | self.width = in_shape[3] 173 | self.padding = kernel_size // 2 174 | self.bias = bias 175 | 176 | self.t_cc = nn.Sequential(nn.Conv2d(num_hidden, num_hidden*3, 177 | kernel_size=self.kernel_size, 178 | stride=self.stride, padding=self.padding, 179 | bias=self.bias 180 | ), 181 | #nn.LayerNorm([num_hidden*3, self.height, self.width]) 182 | ) 183 | 184 | self.s_cc = nn.Sequential(nn.Conv2d(num_hidden, num_hidden*4, 185 | kernel_size=self.kernel_size, 186 | stride=self.stride, padding=self.padding, 187 | bias=self.bias 188 | ), 189 | #nn.LayerNorm([num_hidden*4, self.height, self.width]) 190 | ) 191 | 192 | self.x_cc = nn.Sequential(nn.Conv2d(in_channel, num_hidden*4, 193 | kernel_size=self.kernel_size, 194 | stride=self.stride, padding=self.padding, 195 | bias=self.bias 196 | ), 197 | #nn.LayerNorm([num_hidden*4, self.height, self.width]) 198 | ) 199 | 200 | self.mims = MIMS(in_channel, num_hidden, kernel_size, in_shape, 201 | stride, bias) 202 | self.last = nn.Conv2d(num_hidden*2, num_hidden, 1, 1, padding=0) 203 | 204 | def init_state(self): 205 | return torch.zeros([self.batch, self.num_hidden, self.height, self.width], 206 | dtype=torch.float32) 207 | 208 | def forward(self, x, diff_h, h, c, m, convlstm_c): 209 | if h is None: 210 | h = self.init_state().to(x.device) 211 | if c is None: 212 | c = self.init_state().to(x.device) 213 | if m is None: 214 | m = self.init_state().to(x.device) 215 | if convlstm_c is None: 216 | convlstm_c = self.init_state().to(x.device) 217 | if diff_h is None: 218 | diff_h = torch.zeros_like(h, device=x.device) 219 | 220 | t_cc = self.t_cc(h) 221 | s_cc = self.s_cc(m) 222 | x_cc = self.x_cc(x) 223 | 224 | i_s, g_s, f_s, o_s = torch.split(s_cc, self.num_hidden, dim=1) 225 | i_t, g_t, o_t = torch.split(t_cc, self.num_hidden, dim=1) 226 | i_x, g_x, f_x, o_x = torch.split(x_cc, self.num_hidden, dim=1) 227 | 228 | i = torch.sigmoid(i_x + i_t) 229 | i_ = torch.sigmoid(i_x + i_s) 230 | g = torch.tanh(g_x + g_t) 231 | g_ = torch.tanh(g_x + g_s) 232 | f_ = torch.sigmoid(f_x + f_s) 233 | o = torch.sigmoid(o_x + o_t + o_s) 234 | new_m = f_ * m + i_ * g_ 235 | 236 | c, convlstm_c = self.mims(diff_h, c, convlstm_c) 237 | 238 | new_c = c + i * g 239 | cell = torch.cat([new_c, new_m], dim=1) 240 | cell = self.last(cell) 241 | new_h = o * torch.tanh(cell) 242 | 243 | return new_h, new_c, new_m, convlstm_c 244 | 245 | 246 | class MIMN(nn.Module): 247 | def __init__(self, in_channel, num_hidden, kernel_size, in_shape, 248 | stride=1, bias=True): 249 | """ 250 | """ 251 | super(MIMN, self).__init__() 252 | self.in_channel = in_channel 253 | self.num_hidden = num_hidden 254 | self.kernel_size = kernel_size 255 | self.stride = stride 256 | self.batch = in_shape[0] 257 | self.height = in_shape[2] 258 | self.width = in_shape[3] 259 | self.padding = kernel_size // 2 260 | self.bias = bias 261 | 262 | self.conv_h = nn.Sequential( 263 | nn.Conv2d(num_hidden, self.num_hidden * 4, 264 | kernel_size=self.kernel_size, 265 | stride=1, padding=self.padding, 266 | bias=self.bias), 267 | #nn.LayerNorm([num_hidden*4, self.height, self.width]) 268 | ) 269 | 270 | self.conv_x = nn.Sequential( 271 | nn.Conv2d(in_channel, self.num_hidden * 4, 272 | kernel_size=self.kernel_size, 273 | stride=1, padding=self.padding, 274 | bias=self.bias 275 | ), 276 | #nn.LayerNorm([num_hidden*4, self.height, self.width]) 277 | ) 278 | 279 | ct_weight_ = torch.empty((self.num_hidden*2, self.height, self.width)) 280 | self.ct_weight = nn.Parameter(nn.init.kaiming_normal_(ct_weight_)) 281 | 282 | oc_weight_ = torch.empty((self.num_hidden, self.height, self.width)) 283 | self.oc_weight = nn.init.kaiming_normal_(oc_weight_) 284 | 285 | 286 | def init_state(self): 287 | shape = [self.batch, self.num_hidden, self.height, self.width] 288 | return torch.zeros(shape, dtype=torch.float32) 289 | 290 | def forward(self, x, h_t, c_t): 291 | if h_t is None: 292 | h_t = self.init_state().to(x.device) 293 | if c_t is None: 294 | c_t = self.init_state().to(x.device) 295 | 296 | h_concat = self.conv_h(h_t) 297 | 298 | i_h, g_h, f_h, o_h = torch.split(h_concat, self.num_hidden, dim=1) 299 | 300 | ct_activation = c_t.repeat([1,2,1,1]) * self.ct_weight 301 | i_c, f_c = torch.split(ct_activation, self.num_hidden, dim=1) 302 | 303 | i_ = i_h + i_c 304 | f_ = f_h + f_c 305 | g_ = g_h 306 | o_ = o_h 307 | 308 | if x != None: 309 | x_concat = self.conv_x(x) 310 | i_x, g_x, f_x, o_x = torch.split(x_concat, self.num_hidden, dim=1) 311 | 312 | i_ += i_x 313 | f_ += f_x 314 | g_ += g_x 315 | o_ += o_x 316 | 317 | g_ = torch.tanh(g_) 318 | i_ = torch.sigmoid(i_) 319 | f_ = torch.sigmoid(f_) 320 | c_new = f_ * c_t + i_ * g_ 321 | 322 | o_c = torch.sigmoid(o_ + c_new * self.oc_weight.to(x.device)) 323 | 324 | h_new = o_c * torch.tanh(c_new) 325 | 326 | return h_new, c_new 327 | 328 | 329 | class MIM(nn.Module): 330 | def __init__(self, input_dims, out_dims, in_shape, hidden_dim=[32, 32], 331 | kernel_size=3, stride=1, total_length=10, input_length=5, 332 | lr=0.001): 333 | """ 334 | :param input_dims(int): input channel 335 | :param out_dims(int): output channel 336 | :param in_shape(list): input shape 337 | :param hidden_dim(list): hidden layers channel 338 | :param kernel_size(int): kernel size 339 | :param stride(int): stride 340 | :param total_length(int): total number of frames 341 | :param input_length(int): number of input frames 342 | :param lr(float): learning rate 343 | """ 344 | super(MIM, self).__init__() 345 | 346 | self.learning_rate = lr 347 | self.input_dims = input_dims 348 | self.out_dims = out_dims 349 | self.hidden_dim = hidden_dim 350 | self.kernel_size = kernel_size 351 | self.stride = stride 352 | self.num_layers = len(hidden_dim) 353 | self.total_length = total_length 354 | self.input_length = input_length 355 | self.in_shape = in_shape 356 | self.batch = in_shape[0] 357 | self.height = in_shape[2] 358 | self.width = in_shape[3] 359 | 360 | # stationarity 361 | self.stlstm_layer = nn.ModuleList([]) 362 | for i in range(self.num_layers): 363 | if i < 1: 364 | self.stlstm_layer.append(SpatioTemporalLSTMCell(self.input_dims, self.hidden_dim[i], 365 | kernel_size=kernel_size, 366 | in_shape=in_shape 367 | ) 368 | ) 369 | else: 370 | self.stlstm_layer.append(MIMBlock(self.hidden_dim[i-1], self.hidden_dim[i], 371 | kernel_size=kernel_size, stride=stride, 372 | in_shape=in_shape 373 | ) 374 | ) 375 | 376 | # non-stationarity 377 | self.stlstm_layer_diff = nn.ModuleList([]) 378 | for i in range(self.num_layers-1): 379 | self.stlstm_layer_diff.append(MIMN(self.hidden_dim[i], self.hidden_dim[i+1], 380 | kernel_size=kernel_size, stride=stride, 381 | in_shape=in_shape 382 | ) 383 | ) 384 | 385 | self.last = nn.Conv2d(self.hidden_dim[-1], self.out_dims, 1, 1, 0) 386 | 387 | def forward(self, frames, ss_bool=None): 388 | st_memory = None 389 | cell_state = [None] * self.num_layers 390 | hidden_state = [None] * self.num_layers 391 | cell_state_diff = [None] * (self.num_layers - 1) 392 | hidden_state_diff = [None] * (self.num_layers - 1) 393 | convlstm_c = [None] * (self.num_layers - 1) 394 | 395 | gen_imgs = [] 396 | for ts in range(self.total_length - 1): 397 | if ts < self.input_length: 398 | x_gen = frames[:, ts] 399 | else: 400 | x_gen = ss_bool[:, ts-self.input_length] * frames[:, ts] + (1 - ss_bool[:, ts-self.input_length]) * x_gen 401 | 402 | preh = hidden_state[0] 403 | # 1st layer(convlstm) 404 | hidden_state[0], cell_state[0], st_memory = self.stlstm_layer[0]( 405 | x_gen, hidden_state[0], cell_state[0], st_memory) 406 | 407 | # higher layer(mim) 408 | for i in range(1, self.num_layers): 409 | if ts > 0: 410 | if i == 1: 411 | hidden_state_diff[i-1], cell_state_diff[i-1] = self.stlstm_layer_diff[i-1](hidden_state[i-1] - preh, 412 | hidden_state_diff[i-1], 413 | cell_state_diff[i-1]) 414 | else: 415 | hidden_state_diff[i-1], cell_state_diff[i-1] = self.stlstm_layer_diff[i-1]( 416 | hidden_state_diff[i-2], hidden_state_diff[i-1], cell_state_diff[i-1]) 417 | else: 418 | self.stlstm_layer_diff[i-1](torch.zeros_like(hidden_state[i-1]), None, None) 419 | 420 | preh = hidden_state[i] 421 | hidden_state[i], cell_state[i], st_memory, convlstm_c[i-1] = self.stlstm_layer[i]( 422 | hidden_state[i-1], hidden_state_diff[i-1], hidden_state[i], cell_state[i], st_memory, convlstm_c[i-1]) 423 | 424 | x_gen = self.last(hidden_state[-1]) 425 | gen_imgs.append(x_gen) 426 | 427 | return torch.stack(gen_imgs, dim=1) 428 | 429 | --------------------------------------------------------------------------------