File size: 21,585 Bytes
0f34fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# A modified version of "Fully Convolutional Mesh Autoencoder using Efficient Spatially Varying Kernels"
# https://arxiv.org/abs/2006.04325
# and thanks to this more modern implementation as well
# https://github.com/g-fiche/Mesh-VQ-VAE
# https://arxiv.org/abs/2312.08291
import torch
import torch.nn as nn
import numpy as np
import os

#################################################################################
#                                       AE                                      #
#################################################################################
class AE(nn.Module):
    def __init__(self, model, bs=16, num_vertices=6890):
        super().__init__()
        # currently only set up is for SMPL-H
        self.num_vertices = num_vertices
        self.bs=bs
        self.encoder = Encoder(model)
        self.decoder = Decoder(model)

    def encode(self, x):
        B, L = x.shape[0], x.shape[1]
        x = x.view(B * L, self.num_vertices, 3)
        x_encoder = self.encoder(x)
        return x_encoder

    def forward(self, x):
        B, L = x.shape[0], x.shape[1]
        x = x.view(B * L, self.num_vertices, 3)
        x_encoder = self.encoder(x)
        x_out = self.decoder(x_encoder)
        x_out = x_out.view(B, L, self.num_vertices, 3)
        return x_out


    def decode(self, x):
        T = x.shape[1]
        if x.shape[1] % self.bs != 0:
            x = torch.cat([x, torch.zeros_like(x[:, :self.bs-x.shape[1] % self.bs])], dim=1)
        outputs = []
        for i in range(x.shape[0]):
            outputss = []
            for j in range(0, x.shape[1], self.bs):
                chunk = x[i, j:j + self.bs]
                out = self.decoder(chunk)
                outputss.append(out)
            outputs.append(torch.cat(outputss, dim=0)[:T])
        x_out = torch.stack(outputs, dim=0)

        return x_out

#################################################################################
#                                     AE Zoos                                   #
#################################################################################
def ae(**kwargs):
    config_model = {"batch": 16,
                    "connection_folder": "body_models/ConnectionMatrices/",
                    "initial_connection_fn": "body_models/ConnectionMatrices/_pool0.npy",
                    "connection_layer_lst": ["pool0", "pool1", "pool2", "pool3", "pool4", "pool5", "pool6", "pool7_28",
                                             "unpool7_28", "unpool6", "unpool5", "unpool4", "unpool3", "unpool2",
                                             "unpool1", "unpool0"],
                    "channel_lst": [64, 64, 128, 128, 256, 256, 512, 12, 512, 256, 256, 128, 128, 64, 64, 3],
                    "weight_num_lst": [9, 0, 9, 0, 9, 0, 9, 0, 0, 9, 0, 9, 0, 9, 0, 9],
                    "residual_rate_lst": [0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0],
                    }
    return AE(FullyConvAE(config_model, **kwargs), bs=config_model["batch"])
AE_models = {
    'AE_Model': ae
}


class Encoder(nn.Module):
    def __init__(self, model):
        super(Encoder, self).__init__()
        self.model = model

    def forward(self, x):
        out = self.model.forward_till_layer_n(x, len(self.model.channel_lst) // 2)
        return out


class Decoder(nn.Module):
    def __init__(self, model):
        super(Decoder, self).__init__()
        self.model = model

    def forward(self, x):
        out = self.model.forward_from_layer_n(x, len(self.model.channel_lst) // 2)
        return out

class FullyConvAE(nn.Module):
    def __init__(
        self, config_model=None, test_mode=False
    ):  # layer_info_lst= [(point_num, feature_dim)]
        super(FullyConvAE, self).__init__()

        self.test_mode = test_mode

        self.channel_lst = config_model["channel_lst"]

        self.residual_rate_lst = config_model["residual_rate_lst"]

        self.weight_num_lst = config_model["weight_num_lst"]

        self.initial_connection_fn = config_model["initial_connection_fn"]

        data = np.load(self.initial_connection_fn)
        neighbor_id_dist_lstlst = data[:, 1:]  # point_num*(1+2*neighbor_num)
        self.point_num = data.shape[0]
        self.neighbor_id_lstlst = neighbor_id_dist_lstlst.reshape(
            (self.point_num, -1, 2)
        )[
            :, :, 0
        ]  # point_num*neighbor_num
        self.neighbor_num_lst = np.array(data[:, 0])  # point_num

        self.relu = nn.ELU()

        self.batch = config_model["batch"]

        #####For Laplace computation######
        self.initial_neighbor_id_lstlst = torch.LongTensor(
            self.neighbor_id_lstlst
        ).cuda()  # point_num*max_neighbor_num
        self.initial_neighbor_num_lst = torch.FloatTensor(
            self.neighbor_num_lst
        ).cuda()  # point_num

        self.connection_folder = config_model["connection_folder"]
        self.connection_layer_fn_lst = []
        fn_lst = os.listdir(self.connection_folder)
        self.connection_layer_lst = config_model["connection_layer_lst"]
        for layer_name in self.connection_layer_lst:
            layer_name = "_" + layer_name + "."

            find_fn = False
            for fn in fn_lst:
                if (layer_name in fn) and ((".npy" in fn) or (".npz" in fn)):
                    self.connection_layer_fn_lst += [self.connection_folder + fn]
                    find_fn = True
                    break
            if find_fn == False:
                print("!!!ERROR: cannot find the connection layer fn")

        self.init_layers(self.batch)

        self.initial_max_neighbor_num = self.initial_neighbor_id_lstlst.shape[1]

    def init_layers(self, batch):
        self.layer_lst = (
            []
        )  ##[in_channel, out_channel, in_pn, out_pn, max_neighbor_num, neighbor_num_lst,neighbor_id_lstlst,conv_layer, residual_layer]

        self.layer_num = len(self.channel_lst)

        in_point_num = self.point_num
        in_channel = 3

        for l in range(self.layer_num):
            out_channel = self.channel_lst[l]
            weight_num = self.weight_num_lst[l]
            residual_rate = self.residual_rate_lst[l]

            connection_info = np.load(self.connection_layer_fn_lst[l])
            out_point_num = connection_info.shape[0]
            neighbor_num_lst = torch.FloatTensor(
                connection_info[:, 0].astype(float)
            ).cuda()  # out_point_num*1
            neighbor_id_dist_lstlst = connection_info[
                :, 1:
            ]  # out_point_num*(max_neighbor_num*2)
            print(self.connection_layer_fn_lst[l])
            print()
            neighbor_id_lstlst = neighbor_id_dist_lstlst.reshape(
                (out_point_num, -1, 2)
            )[
                :, :, 0
            ]  # out_point_num*max_neighbor_num
            neighbor_id_lstlst = torch.LongTensor(neighbor_id_lstlst).cuda()
            max_neighbor_num = neighbor_id_lstlst.shape[1]
            avg_neighbor_num = round(neighbor_num_lst.mean().item())
            effective_w_weights_rate = neighbor_num_lst.sum() / float(
                max_neighbor_num * out_point_num
            )
            effective_w_weights_rate = round(effective_w_weights_rate.item(), 3)

            pc_mask = torch.ones(in_point_num + 1).cuda()
            pc_mask[in_point_num] = 0
            neighbor_mask_lst = pc_mask[
                neighbor_id_lstlst
            ].contiguous()  # out_pn*max_neighbor_num neighbor is 1 otherwise 0

            zeros_batch_outpn_outchannel = torch.zeros(
                (batch, out_point_num, out_channel)
            ).cuda()

            if (residual_rate < 0) or (residual_rate > 1):
                print("Invalid residual rate", residual_rate)
            ####parameters for conv###############
            conv_layer = ""

            if residual_rate < 1:
                weights = torch.randn(weight_num, out_channel * in_channel).cuda()

                weights = nn.Parameter(weights).cuda()

                self.register_parameter("weights" + str(l), weights)

                bias = nn.Parameter(torch.zeros(out_channel).cuda())
                self.register_parameter("bias" + str(l), bias)

                w_weights = torch.randn(out_point_num, max_neighbor_num, weight_num) / (
                    avg_neighbor_num * weight_num
                )

                w_weights = nn.Parameter(w_weights.cuda())
                self.register_parameter("w_weights" + str(l), w_weights)

                conv_layer = (weights, bias, w_weights)

            ####parameters for residual###############

            ## a residual layer with out_point_num==in_point_num and residual_rate==1 is a pooling or unpooling layer

            residual_layer = ""

            if residual_rate > 0:
                p_neighbors = ""
                weight_res = ""

                if out_point_num != in_point_num:
                    p_neighbors = nn.Parameter(
                        (
                            torch.randn(out_point_num, max_neighbor_num)
                            / (avg_neighbor_num)
                        ).cuda()
                    )
                    self.register_parameter("p_neighbors" + str(l), p_neighbors)

                if out_channel != in_channel:
                    weight_res = torch.randn(out_channel, in_channel)
                    # self.normalize_weights(weight_res)
                    weight_res = weight_res / out_channel
                    weight_res = nn.Parameter(weight_res.cuda())
                    self.register_parameter("weight_res" + str(l), weight_res)

                residual_layer = (weight_res, p_neighbors)

            #####put everythin together

            layer = (
                in_channel,
                out_channel,
                in_point_num,
                out_point_num,
                weight_num,
                max_neighbor_num,
                neighbor_num_lst,
                neighbor_id_lstlst,
                conv_layer,
                residual_layer,
                residual_rate,
                neighbor_mask_lst,
                zeros_batch_outpn_outchannel,
            )

            self.layer_lst += [layer]

            in_point_num = out_point_num
            in_channel = out_channel

    # precompute the parameters so as to accelerate forwarding in testing mode
    def init_test_mode(self):
        for l in range(len(self.layer_lst)):
            layer_info = self.layer_lst[l]

            (
                in_channel,
                out_channel,
                in_pn,
                out_pn,
                weight_num,
                max_neighbor_num,
                neighbor_num_lst,
                neighbor_id_lstlst,
                conv_layer,
                residual_layer,
                residual_rate,
                neighbor_mask_lst,
                zeros_batch_outpn_outchannel,
            ) = layer_info

            if len(conv_layer) != 0:
                (
                    weights,
                    bias,
                    raw_w_weights,
                ) = conv_layer  # weight_num*(out_channel*in_channel)   out_point_num* max_neighbor_num* weight_num

                w_weights = ""

                w_weights = raw_w_weights * neighbor_mask_lst.view(
                    out_pn, max_neighbor_num, 1
                ).repeat(
                    1, 1, weight_num
                )  # out_pn*max_neighbor_num*weight_num

                weights = torch.einsum(
                    "pmw,wc->pmc", [w_weights, weights]
                )  # out_pn*max_neighbor_num*(out_channel*in_channel)
                weights = weights.view(
                    out_pn, max_neighbor_num, out_channel, in_channel
                )

                conv_layer = weights, bias

            ####compute output of residual layer####

            if len(residual_layer) != 0:
                (
                    weight_res,
                    p_neighbors_raw,
                ) = residual_layer  # out_channel*in_channel  out_pn*max_neighbor_num
                if in_pn != out_pn:
                    p_neighbors = torch.abs(p_neighbors_raw) * neighbor_mask_lst
                    p_neighbors_sum = p_neighbors.sum(1) + 1e-8  # out_pn
                    p_neighbors = p_neighbors / p_neighbors_sum.view(out_pn, 1).repeat(
                        1, max_neighbor_num
                    )

                    residual_layer = weight_res, p_neighbors

            self.layer_lst[l] = (
                in_channel,
                out_channel,
                in_pn,
                out_pn,
                weight_num,
                max_neighbor_num,
                neighbor_num_lst,
                neighbor_id_lstlst,
                conv_layer,
                residual_layer,
                residual_rate,
                neighbor_mask_lst,
                zeros_batch_outpn_outchannel,
            )

    # a faster mode for testing
    # input_pc batch*in_pn*in_channel
    # out_pc batch*out_pn*out_channel
    def forward_one_conv_layer_batch_during_test(
        self, in_pc, layer_info, is_final_layer=False
    ):
        batch = in_pc.shape[0]

        (
            in_channel,
            out_channel,
            in_pn,
            out_pn,
            weight_num,
            max_neighbor_num,
            neighbor_num_lst,
            neighbor_id_lstlst,
            conv_layer,
            residual_layer,
            residual_rate,
            neighbor_mask_lst,
            zeros_batch_outpn_outchannel,
        ) = layer_info

        device = in_pc.get_device()
        if device < 0:
            device = "cpu"

        in_pc_pad = torch.cat(
            (in_pc, torch.zeros(batch, 1, in_channel).to(device)), 1
        )  # batch*(in_pn+1)*in_channel

        in_neighbors = in_pc_pad[
            :, neighbor_id_lstlst.to(device)
        ]  # batch*out_pn*max_neighbor_num*in_channel

        ####compute output of convolution layer####
        out_pc_conv = zeros_batch_outpn_outchannel.clone()

        if len(conv_layer) != 0:
            (
                weights,
                bias,
            ) = conv_layer  # weight_num*(out_channel*in_channel)   out_point_num* max_neighbor_num* weight_num

            out_neighbors = torch.einsum(
                "pmoi,bpmi->bpmo", [weights.to(device), in_neighbors]
            )  # batch*out_pn*max_neighbor_num*out_channel

            out_pc_conv = out_neighbors.sum(2)

            out_pc_conv = out_pc_conv + bias

            if is_final_layer == False:
                out_pc_conv = self.relu(
                    out_pc_conv
                )  ##self.relu is defined in the init function

        # if(self.residual_rate==0):
        #    return out_pc
        ####compute output of residual layer####
        out_pc_res = zeros_batch_outpn_outchannel.clone()

        if len(residual_layer) != 0:
            (
                weight_res,
                p_neighbors,
            ) = residual_layer  # out_channel*in_channel  out_pn*max_neighbor_num

            if in_channel != out_channel:
                in_pc_pad = torch.einsum("oi,bpi->bpo", [weight_res, in_pc_pad])

            out_pc_res = []
            if in_pn == out_pn:
                out_pc_res = in_pc_pad[:, 0:in_pn].clone()
            else:
                in_neighbors = in_pc_pad[
                    :, neighbor_id_lstlst.to(device)
                ]  # batch*out_pn*max_neighbor_num*out_channel
                out_pc_res = torch.einsum(
                    "pm,bpmo->bpo", [p_neighbors.to(device), in_neighbors]
                )

        out_pc = out_pc_conv.to(device) * np.sqrt(1 - residual_rate) + out_pc_res.to(
            device
        ) * np.sqrt(residual_rate)

        return out_pc

    # use in train mode. Slower than test mode
    # input_pc batch*in_pn*in_channel
    # out_pc batch*out_pn*out_channel
    def forward_one_conv_layer_batch(self, in_pc, layer_info, is_final_layer=False):
        batch = in_pc.shape[0]

        (
            in_channel,
            out_channel,
            in_pn,
            out_pn,
            weight_num,
            max_neighbor_num,
            neighbor_num_lst,
            neighbor_id_lstlst,
            conv_layer,
            residual_layer,
            residual_rate,
            neighbor_mask_lst,
            zeros_batch_outpn_outchannel,
        ) = layer_info

        in_pc_pad = torch.cat(
            (in_pc, torch.zeros(batch, 1, in_channel).cuda()), 1
        )  # batch*(in_pn+1)*in_channel

        in_neighbors = in_pc_pad[
            :, neighbor_id_lstlst
        ]  # batch*out_pn*max_neighbor_num*in_channel

        ####compute output of convolution layer####
        out_pc_conv = zeros_batch_outpn_outchannel.clone()

        if len(conv_layer) != 0:
            (
                weights,
                bias,
                raw_w_weights,
            ) = conv_layer  # weight_num*(out_channel*in_channel)   out_point_num* max_neighbor_num* weight_num

            w_weights = raw_w_weights * neighbor_mask_lst.view(
                out_pn, max_neighbor_num, 1
            ).repeat(
                1, 1, weight_num
            )  # out_pn*max_neighbor_num*weight_num

            weights = torch.einsum(
                "pmw,wc->pmc", [w_weights, weights]
            )  # out_pn*max_neighbor_num*(out_channel*in_channel)
            weights = weights.view(out_pn, max_neighbor_num, out_channel, in_channel)

            out_neighbors = torch.einsum(
                "pmoi,bpmi->bpmo", [weights, in_neighbors]
            )  # batch*out_pn*max_neighbor_num*out_channel

            out_pc_conv = out_neighbors.sum(2)

            out_pc_conv = out_pc_conv + bias

            if is_final_layer == False:
                out_pc_conv = self.relu(
                    out_pc_conv
                )  ##self.relu is defined in the init function

        ####compute output of residual layer####
        out_pc_res = zeros_batch_outpn_outchannel.clone()

        if len(residual_layer) != 0:
            (
                weight_res,
                p_neighbors_raw,
            ) = residual_layer  # out_channel*in_channel  out_pn*max_neighbor_num

            if in_channel != out_channel:
                in_pc_pad = torch.einsum("oi,bpi->bpo", [weight_res, in_pc_pad])

            out_pc_res = []
            if in_pn == out_pn:
                out_pc_res = in_pc_pad[:, 0:in_pn].clone()
            else:
                in_neighbors = in_pc_pad[
                    :, neighbor_id_lstlst
                ]  # batch*out_pn*max_neighbor_num*out_channel

                p_neighbors = torch.abs(p_neighbors_raw) * neighbor_mask_lst
                p_neighbors_sum = p_neighbors.sum(1) + 1e-8  # out_pn
                p_neighbors = p_neighbors / p_neighbors_sum.view(out_pn, 1).repeat(
                    1, max_neighbor_num
                )

                out_pc_res = torch.einsum("pm,bpmo->bpo", [p_neighbors, in_neighbors])

        # print(out_pc_conv.shape, out_pc_res.shape)
        out_pc = out_pc_conv * np.sqrt(1 - residual_rate) + out_pc_res * np.sqrt(
            residual_rate
        )

        return out_pc

    def forward_till_layer_n(self, in_pc, layer_n):
        out_pc = in_pc.clone()

        for i in range(layer_n):
            if self.test_mode == False:
                out_pc = self.forward_one_conv_layer_batch(out_pc, self.layer_lst[i])
            else:
                out_pc = self.forward_one_conv_layer_batch_during_test(
                    out_pc, self.layer_lst[i]
                )

        # out_pc = self.final_linear(out_pc.transpose(1,2)).transpose(1,2) #batch*3*point_num

        return out_pc

    def forward_from_layer_n(self, in_pc, layer_n):
        out_pc = in_pc.clone()

        for i in range(layer_n, self.layer_num):
            if i < (self.layer_num - 1):
                if self.test_mode == False:
                    out_pc = self.forward_one_conv_layer_batch(
                        out_pc, self.layer_lst[i]
                    )
                else:
                    out_pc = self.forward_one_conv_layer_batch_during_test(
                        out_pc, self.layer_lst[i]
                    )
            else:
                if self.test_mode == False:
                    out_pc = self.forward_one_conv_layer_batch(
                        out_pc, self.layer_lst[i], is_final_layer=True
                    )
                else:
                    out_pc = self.forward_one_conv_layer_batch_during_test(
                        out_pc, self.layer_lst[i], is_final_layer=True
                    )

        return out_pc

    def forward_layer_n(self, in_pc, layer_n):
        out_pc = in_pc.clone()

        if layer_n < (self.layer_num - 1):
            if self.test_mode == False:
                out_pc = self.forward_one_conv_layer_batch(
                    out_pc, self.layer_lst[layer_n]
                )
            else:
                out_pc = self.forward_one_conv_layer_batch_during_test(
                    out_pc, self.layer_lst[layer_n]
                )
        else:
            if self.test_mode == False:
                out_pc = self.forward_one_conv_layer_batch(
                    out_pc, self.layer_lst[layer_n], is_final_layer=True
                )
            else:
                out_pc = self.forward_one_conv_layer_batch_during_test(
                    out_pc, self.layer_lst[layer_n], is_final_layer=True
                )

        return out_pc