-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_3D.py
More file actions
1452 lines (1075 loc) · 63.2 KB
/
train_3D.py
File metadata and controls
1452 lines (1075 loc) · 63.2 KB
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function, division
import os
import re
from os import path
import numpy as np
from PIL import Image
import glob
from torch import optim
import torch.utils.data
import torch
import torch.nn.functional as F
import torch.nn
import torchvision
from torch.utils.data.sampler import SubsetRandomSampler
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
# import torchsummary
# from torchstat import stat
from torchsummary import summary
# import pytorch_lightning as pl
import matplotlib.pyplot as plt
# import natsort
from tensorboardX import SummaryWriter
from thop import profile, clever_format
from rtree import index
import shutil
import random
import pickle
import dill
from models.Models_3D import CSFL_Net_3D
from tools.tracing.tracing_tools_3D import binary_image_skeletonize, get_pos_image_3d, get_network_predict_3d, tracing_strategy_fast_3d, tracing_strategy_lstm_3d
from tools.Data_Loader_3d import Images_Dataset_folder
from tools.Losses import dice_loss, MSE_loss, L1_loss, dice_score, bce_loss_w
# from tools.generate_seed import generate_skl_seed
from skimage import morphology, transform
# from models.seq2seq_ConvLSTM import EncoderDecoderConvLSTM
import time
from configs import config_3d
from lib.klib.baseio import *
from lib.swclib.swc_io import swc_save_metric, swc_save, read_swc_tree
from lib.swclib.swc_tree import SwcTree
from lib.swclib.swc_node import SwcNode
from lib.swclib.euclidean_point import EuclideanPoint
from lib.swclib.edge_match_utils import get_bounds
from lib.swclib import edge_match_utils
from lib.swclib.re_sample import up_sample_swc_tree
import copy
args = config_3d.args
resize_radio = args.resize_radio
r_resize = args.r_resize
def train(args, model_name, device_ids, device):
#######################################################
# Setting the basic paramters of the model
#######################################################
batch_size = args.batch_size
valid_size = args.valid_rate
epoch = args.epochs
initial_lr = args.lr
num_workers = args.n_threads
data_shape = args.data_shape
to_restore = args.to_restore
vector_bins = args.vector_bins
train_seg = args.train_seg
if train_seg:
datasets_plag = 'train'
freeze_plag = False
else:
datasets_plag = 'train_r'
freeze_plag = True
if train_seg:
lambda_seg = 1
lambda_centerline = 1
lambda_r = 0
lambda_d_class = 0
lambda_d_reg = 0
else:
lambda_seg = 0
lambda_centerline = 0
lambda_r = 100
lambda_d_class = 1
lambda_d_reg = 0.001
random_seed = random.randint(1, 100)
shuffle = True
lossT = []
lossL = []
lossL.append(np.inf)
lossT.append(np.inf)
epoch_valid = epoch-2
i_valid = 0
train_on_gpu = torch.cuda.is_available()
pin_memory = False
if train_on_gpu:
pin_memory = True
#######################################################
# load the data
#######################################################
print('loading the train data')
train_data_dir = args.dataset_img_path
train_label_dir = args.dataset_img_path
train_data_dir_list = []
train_label_dir_list = []
train_data_imagename_dir_list = os.listdir(train_data_dir)
ssss = 0
for imagename in train_data_imagename_dir_list:
train_data_imagename_num_dir_list = os.listdir(train_data_dir + imagename)
for imagename_num in train_data_imagename_num_dir_list:
train_data_dir_list.append(imagename + '/' + imagename_num)
train_label_dir_list.append(imagename + '/' + imagename_num)
ssss += 1
Training_Data = Images_Dataset_folder(train_data_dir, train_label_dir, train_data_dir_list, train_label_dir_list, mode = datasets_plag)
num_train = len(Training_Data)
indices = list(range(num_train))
np.random.shuffle(indices)
split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
train_loader = torch.utils.data.DataLoader(Training_Data, batch_size=batch_size, sampler=train_sampler,num_workers=num_workers, pin_memory=pin_memory,)
valid_loader = torch.utils.data.DataLoader(Training_Data, batch_size=batch_size, sampler=valid_sampler,num_workers=num_workers, pin_memory=pin_memory,)
#######################################################
# load the test data
#######################################################
print('loading the test data')
train_data_dir = args.dataset_img_test_path
train_label_dir = args.dataset_img_test_path
train_data_dir_list = []
train_label_dir_list = []
train_data_imagename_dir_list = os.listdir(train_data_dir)
for imagename in train_data_imagename_dir_list:
train_data_imagename_num_dir_list = os.listdir(train_data_dir + imagename)
for imagename_num in train_data_imagename_num_dir_list:
train_data_dir_list.append(imagename + '/' + imagename_num)
train_label_dir_list.append(imagename + '/' + imagename_num)
Training_Data = Images_Dataset_folder(train_data_dir, train_label_dir, train_data_dir_list, train_label_dir_list, mode = 'test')
num_train = len(Training_Data)
indices = list(range(num_train))
np.random.shuffle(indices)
split = int(np.floor(0.5 * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
test_loader = torch.utils.data.DataLoader(Training_Data, batch_size=batch_size, sampler=train_sampler,num_workers=num_workers, pin_memory=pin_memory,)
#######################################################
# build the model
#######################################################
def model_unet_LSTM(model_input, in_channel, out_channel, freeze_net):
model = model_input(in_channel, out_channel, freeze_net)
return model
model_train = model_unet_LSTM(model_name, 1, 1, freeze_plag)
model_train = torch.nn.DataParallel(model_train, device_ids=device_ids)
model_train.to(device)
softmax = torch.nn.Softmax()#.cuda(gpu)
criterion = torch.nn.CrossEntropyLoss()#.cuda(gpu)
reg_criterion = torch.nn.MSELoss()#.cuda(gpu)
# bce_criterion = torch.nn.BCELoss()
bce_criterion = torch.nn.BCEWithLogitsLoss()
sigmoid_layer = torch.nn.Sigmoid()
opt = torch.optim.Adam(model_train.parameters(), lr=initial_lr) # try SGD
MAX_STEP = args.epochs
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(opt, MAX_STEP, eta_min=0)
#######################################################
# model and log dir
#######################################################
LOG_DIR = str(args.log_save_dir) + str(args.gpu_id) + '/'
MODEL_DIR = str(args.model_save_dir) + str(args.gpu_id) + '/'
try:
shutil.rmtree(LOG_DIR)
print('Model folder there, so deleted for newer one')
os.mkdir(LOG_DIR)
except OSError:
print("Creation of the log directory '%s' failed " % LOG_DIR)
else:
print("Successfully created the log directory '%s' " % LOG_DIR)
writer = SummaryWriter(LOG_DIR)
try:
os.mkdir(MODEL_DIR)
except OSError:
print("Creation of the model directory '%s' failed " % MODEL_DIR)
else:
print("Successfully created the model directory '%s' " % MODEL_DIR)
if to_restore:
print("loading model")
model_train.load_state_dict(torch.load(MODEL_DIR + str(epoch) + '_' + str(batch_size) + '/epoch_' + str(epoch) + '_batchsize_' + str(batch_size) + '.pth'))
else:
read_model_path = MODEL_DIR + str(epoch) + '_' + str(batch_size)
print(read_model_path)
if os.path.exists(read_model_path) and os.path.isdir(read_model_path):
shutil.rmtree(read_model_path)
print('Model folder there, so deleted for newer one')
try:
os.mkdir(read_model_path)
except OSError:
print("Creation of the model directory '%s' failed" % read_model_path)
else:
print("Successfully created the model directory '%s' " % read_model_path)
######################DATA NROM########################
total_step = train_loader.__len__()
# pause
idx_tensor = [(idx) for idx in range(vector_bins)]
idx_tensor = torch.autograd.Variable(torch.FloatTensor(idx_tensor)).to(device)
#=============================================================================
print("begin training")
valid_loss_min = np.Inf
r_loss_min = np.Inf
n_iter = 1
global_step_ = 0
for i in range(epoch):
train_loss = 0.0
valid_loss = 0.0
valid_class_loss = 0.0
valid_reg_loss = 0.0
valid_rad_loss = 0.0
since = time.time()
scheduler.step(i)
print('learning rate: %f' % (opt.param_groups[0]['lr']))
train_step_temp = 0
valid_step_temp = 0
#######################################################
# Training Data
#######################################################
model_train.train()
for x_img, y_lab, y_dis, y_r, y_exist, y1, y2, y3, y1_bin, y2_bin, y3_bin in train_loader:
train_begin_time = time.time()
y_exist_rnn = y_exist.view(-1)
y_r_rnn = y_r.view(-1)
y1_rnn = y1.view(-1)
y2_rnn = y2.view(-1)
y3_rnn = y3.view(-1)
y1_bin_rnn = y1_bin.view(-1)
y2_bin_rnn = y2_bin.view(-1)
y3_bin_rnn = y3_bin.view(-1)
exist_id_rnn = np.where(y_exist_rnn==1)
exist_no_id_rnn = np.where(y_exist_rnn==0)
x_img, y_lab, y_dis = x_img.to(device), y_lab.to(device), y_dis.to(device)
y_exist_rnn, y1_rnn, y2_rnn, y3_rnn, y1_bin_rnn, y2_bin_rnn, y3_bin_rnn = y_exist_rnn.to(device), y1_rnn.to(device), y2_rnn.to(device), y3_rnn.to(device), y1_bin_rnn.to(device), y2_bin_rnn.to(device), y3_bin_rnn.to(device)
opt.zero_grad()
y_lab_pred, y_dis_pred, y_d_pred_1, y_d_pred_2, y_d_pred_3, y_r_pred, y_d_pred_1_rnn, y_d_pred_2_rnn, y_d_pred_3_rnn, y_r_pred_rnn = model_train(x_img)
# ======================================== 单点预测 =============================================
y_r_pred_rnn = y_r_pred_rnn.view(-1)
y_d_pred_1_rnn = y_d_pred_1_rnn.view(-1,vector_bins)
y_d_pred_2_rnn = y_d_pred_2_rnn.view(-1,vector_bins)
y_d_pred_3_rnn = y_d_pred_3_rnn.view(-1,vector_bins)
vector1_predicted_softmax_rnn = softmax(y_d_pred_1_rnn)
vector2_predicted_softmax_rnn = softmax(y_d_pred_2_rnn)
vector3_predicted_softmax_rnn = softmax(y_d_pred_3_rnn)
vector_predicted_rnn = torch.zeros([y_d_pred_1_rnn.shape[0],3], dtype=torch.float32)
vector_predicted_rnn[:,0] = torch.sum(vector1_predicted_softmax_rnn * idx_tensor, 1) * (1/vector_bins) * 2 - 1
vector_predicted_rnn[:,1] = torch.sum(vector2_predicted_softmax_rnn * idx_tensor, 1) * (1/vector_bins) #* 2 - 1
vector_predicted_rnn[:,2] = torch.sum(vector3_predicted_softmax_rnn * idx_tensor, 1) * (1/vector_bins) * 2 - 1
y_d_class_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3, vector_bins], dtype=torch.float32)
y_d_class_pred_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3, vector_bins], dtype=torch.float32)
y_d_reg_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3], dtype=torch.float32)
y_d_reg_pred_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3], dtype=torch.float32)
y_r_exist_rnn = torch.zeros([len(exist_id_rnn[0])], dtype=torch.float32)
y_r_pred_exist_rnn = torch.zeros([len(exist_id_rnn[0])], dtype=torch.float32)
exist_num = 0
for exist_id_num in exist_id_rnn[0]:
onehot_0_0 = y1_bin_rnn[exist_id_num]
onehot_1_0 = y2_bin_rnn[exist_id_num]
onehot_2_0 = y3_bin_rnn[exist_id_num]
y_d_class_exist_rnn[exist_num][0][onehot_0_0] = 1
y_d_class_exist_rnn[exist_num][1][onehot_1_0] = 1
y_d_class_exist_rnn[exist_num][2][onehot_2_0] = 1
y_d_class_pred_exist_rnn[exist_num][0] = sigmoid_layer(y_d_pred_1_rnn[exist_id_num])
y_d_class_pred_exist_rnn[exist_num][1] = sigmoid_layer(y_d_pred_2_rnn[exist_id_num])
y_d_class_pred_exist_rnn[exist_num][2] = sigmoid_layer(y_d_pred_3_rnn[exist_id_num])
y_d_reg_exist_rnn[exist_num][0] = y1_rnn[exist_id_num]
y_d_reg_exist_rnn[exist_num][1] = y2_rnn[exist_id_num]
y_d_reg_exist_rnn[exist_num][2] = y3_rnn[exist_id_num]
vector_predicted_norm = torch.sqrt(vector_predicted_rnn[exist_id_num][0]**2 + vector_predicted_rnn[exist_id_num][1]**2 + vector_predicted_rnn[exist_id_num][2]**2 + 1e-9)
y_d_reg_pred_exist_rnn[exist_num][0] = vector_predicted_rnn[exist_id_num][0] / vector_predicted_norm
y_d_reg_pred_exist_rnn[exist_num][1] = vector_predicted_rnn[exist_id_num][1] / vector_predicted_norm
y_d_reg_pred_exist_rnn[exist_num][2] = vector_predicted_rnn[exist_id_num][2] / vector_predicted_norm
y_r_exist_rnn[exist_num] = y_r_rnn[exist_id_num]
y_r_pred_exist_rnn[exist_num] = y_r_pred_rnn[exist_id_num]
exist_num += 1
vector_up = torch.sum(torch.multiply(y_d_reg_pred_exist_rnn,y_d_reg_exist_rnn), axis=1)
vector_down_test = torch.sqrt(torch.multiply(y_d_reg_pred_exist_rnn[:,0],y_d_reg_pred_exist_rnn[:,0]) + torch.multiply(y_d_reg_pred_exist_rnn[:,1],y_d_reg_pred_exist_rnn[:,1]) + torch.multiply(y_d_reg_pred_exist_rnn[:,2],y_d_reg_pred_exist_rnn[:,2]) + 1e-9)
vector_down_gold = torch.sqrt(torch.multiply(y_d_reg_exist_rnn[:,0],y_d_reg_exist_rnn[:,0]) + torch.multiply(y_d_reg_exist_rnn[:,1],y_d_reg_exist_rnn[:,1]) + torch.multiply(y_d_reg_exist_rnn[:,2],y_d_reg_exist_rnn[:,2]) + 1e-9)
angle_result = vector_up/(vector_down_test*vector_down_gold + 1e-9)
angle_result_norm_0 = torch.minimum(angle_result, torch.ones_like(angle_result))
angle_result_norm_1 = torch.maximum(angle_result_norm_0, -torch.ones_like(angle_result))
angle_arccos_rnn = torch.arccos(angle_result_norm_1)*180/np.pi
angle_arccos_gt_rnn = torch.zeros_like(angle_arccos_rnn)
for ss in range(angle_arccos_rnn.shape[0]):
if angle_arccos_rnn[ss] > 90:
angle_arccos_rnn[ss] = 180 - angle_arccos_rnn[ss]
# Multi-head Loss
loss_img_seg = lambda_seg * bce_loss_w(y_lab_pred, y_lab, 0.95)
loss_img_centerline = lambda_centerline * bce_loss_w(y_dis_pred, y_dis, 0.95)
# print(torch.mean(y_d_class_pred_exist_rnn[:,0,:]))
# print(torch.mean(y_d_class_pred_exist_rnn[:,1,:]))
# print(torch.mean(y_d_class_pred_exist_rnn[:,2,:]))
loss_img_direction_rnn_class1 = bce_criterion(y_d_class_pred_exist_rnn[:,0,:], y_d_class_exist_rnn[:,0,:])
loss_img_direction_rnn_class2 = bce_criterion(y_d_class_pred_exist_rnn[:,1,:], y_d_class_exist_rnn[:,1,:])
loss_img_direction_rnn_class3 = bce_criterion(y_d_class_pred_exist_rnn[:,2,:], y_d_class_exist_rnn[:,2,:])
loss_img_direction_rnn_class = lambda_d_class * (loss_img_direction_rnn_class1 + loss_img_direction_rnn_class2 + loss_img_direction_rnn_class3)/3
loss_img_direction_rnn_reg = lambda_d_reg * MSE_loss(angle_arccos_rnn, angle_arccos_gt_rnn)
loss_img_radius_rnn = lambda_r * MSE_loss(y_r_pred_exist_rnn, y_r_exist_rnn)
lossT = loss_img_seg + loss_img_centerline + loss_img_direction_rnn_class + loss_img_direction_rnn_reg + loss_img_radius_rnn
lossdice_seg = dice_score(y_lab_pred, y_lab)
lossdice_centerline = dice_score(y_dis_pred, y_dis)
# test
lossT.requires_grad_(True)
train_loss += lossT.item()
lossT.backward()
opt.step()
# pause
train_end_time = time.time()
if (train_step_temp+1) % 20 == 0:
print("===================================================================================")
print('Epoch: {}/{} \t Step: {}/{} \t Total Loss: {:.5f} \t Time: {:.5f}/step'.format(i + 1, epoch, train_step_temp, total_step, lossT.item(), train_end_time-train_begin_time))
if train_seg == False:
print('Vector Angle LSTM: {:.5f} \t'.format(torch.mean(angle_arccos_rnn)))
print('Radius LSTM Loss: {:.5f} \t Class LSTM Loss: {:.5f} \t Reg LSTM Loss: {:.5f} \t'.format(loss_img_radius_rnn.item(), loss_img_direction_rnn_class.item(), loss_img_direction_rnn_reg.item()))
else:
print('Segmentation Loss: {:.5f} \t F1: {:.5f} \t | Centerline Loss: {:.5f} \t F1: {:.5f} \t'.format(loss_img_seg.item(),lossdice_seg.item(), loss_img_centerline.item(), lossdice_centerline.item()))
train_step_temp += 1
writer.add_scalar('Training Segmentation Loss', loss_img_seg.item(), global_step=global_step_)
writer.add_scalar('Training Centerline Loss', loss_img_centerline.item(), global_step=global_step_)
writer.add_scalar('Training Radius Reg LSTM Loss', loss_img_radius_rnn.item(), global_step=global_step_)
writer.add_scalar('Training Vector Reg LSTM Loss', loss_img_direction_rnn_reg.item(), global_step=global_step_)
writer.add_scalar('Training Vector Class LSTM Loss', loss_img_direction_rnn_class.item(), global_step=global_step_)
writer.add_scalar('Training Total Loss', lossT.item(), global_step=global_step_)
global_step_ += 1
#######################################################
#Validation Step
#######################################################
model_train.eval()
torch.no_grad() #to increase the validation process uses less memory
neg_num = 0
neg_loss = 0
pos_num = 0
pos_loss = 0
seg_acc_list = []
centerline_acc_list = []
class_acc_list0 = []
class_acc_list1 = []
class_acc_list2 = []
class_acc_list3 = []
angle_single_list = []
angle_rnn_list = []
for x_img, y_lab, y_dis, y_r, y_exist, y1, y2, y3, y1_bin, y2_bin, y3_bin in test_loader:
train_begin_time = time.time()
y_exist_rnn = y_exist.view(-1)
y_r_rnn = y_r.view(-1)
y1_rnn = y1.view(-1)
y2_rnn = y2.view(-1)
y3_rnn = y3.view(-1)
y1_bin_rnn = y1_bin.view(-1)
y2_bin_rnn = y2_bin.view(-1)
y3_bin_rnn = y3_bin.view(-1)
exist_id_rnn = np.where(y_exist_rnn==1)
exist_no_id_rnn = np.where(y_exist_rnn==0)
x_img, y_lab, y_dis = x_img.to(device), y_lab.to(device), y_dis.to(device)
y_exist_rnn, y1_rnn, y2_rnn, y3_rnn, y1_bin_rnn, y2_bin_rnn, y3_bin_rnn = y_exist_rnn.to(device), y1_rnn.to(device), y2_rnn.to(device), y3_rnn.to(device), y1_bin_rnn.to(device), y2_bin_rnn.to(device), y3_bin_rnn.to(device)
opt.zero_grad()
y_lab_pred, y_dis_pred, y_d_pred_1, y_d_pred_2, y_d_pred_3, y_r_pred, y_d_pred_1_rnn, y_d_pred_2_rnn, y_d_pred_3_rnn, y_r_pred_rnn = model_train(x_img)
# ======================================== 单点预测 =============================================
y_r_pred_rnn = y_r_pred_rnn.view(-1)
y_d_pred_1_rnn = y_d_pred_1_rnn.view(-1,vector_bins)
y_d_pred_2_rnn = y_d_pred_2_rnn.view(-1,vector_bins)
y_d_pred_3_rnn = y_d_pred_3_rnn.view(-1,vector_bins)
vector1_predicted_softmax_rnn = softmax(y_d_pred_1_rnn)
vector2_predicted_softmax_rnn = softmax(y_d_pred_2_rnn)
vector3_predicted_softmax_rnn = softmax(y_d_pred_3_rnn)
vector_predicted_rnn = torch.zeros([y_d_pred_1_rnn.shape[0],3], dtype=torch.float32)
vector_predicted_rnn[:,0] = torch.sum(vector1_predicted_softmax_rnn * idx_tensor, 1) * (1/vector_bins) * 2 - 1
vector_predicted_rnn[:,1] = torch.sum(vector2_predicted_softmax_rnn * idx_tensor, 1) * (1/vector_bins) #* 2 - 1
vector_predicted_rnn[:,2] = torch.sum(vector3_predicted_softmax_rnn * idx_tensor, 1) * (1/vector_bins) * 2 - 1
y_d_class_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3, vector_bins], dtype=torch.float32)
y_d_class_pred_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3, vector_bins], dtype=torch.float32)
y_d_reg_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3], dtype=torch.float32)
y_d_reg_pred_exist_rnn = torch.zeros([len(exist_id_rnn[0]), 3], dtype=torch.float32)
y_r_exist_rnn = torch.zeros([len(exist_id_rnn[0])], dtype=torch.float32)
y_r_pred_exist_rnn = torch.zeros([len(exist_id_rnn[0])], dtype=torch.float32)
exist_num = 0
for exist_id_num in exist_id_rnn[0]:
onehot_0_0 = y1_bin_rnn[exist_id_num]
onehot_1_0 = y2_bin_rnn[exist_id_num]
onehot_2_0 = y3_bin_rnn[exist_id_num]
y_d_class_exist_rnn[exist_num][0][onehot_0_0] = 1
y_d_class_exist_rnn[exist_num][1][onehot_1_0] = 1
y_d_class_exist_rnn[exist_num][2][onehot_2_0] = 1
y_d_class_pred_exist_rnn[exist_num][0] = sigmoid_layer(y_d_pred_1_rnn[exist_id_num])
y_d_class_pred_exist_rnn[exist_num][1] = sigmoid_layer(y_d_pred_2_rnn[exist_id_num])
y_d_class_pred_exist_rnn[exist_num][2] = sigmoid_layer(y_d_pred_3_rnn[exist_id_num])
y_d_reg_exist_rnn[exist_num][0] = y1_rnn[exist_id_num]
y_d_reg_exist_rnn[exist_num][1] = y2_rnn[exist_id_num]
y_d_reg_exist_rnn[exist_num][2] = y3_rnn[exist_id_num]
vector_predicted_norm = torch.sqrt(vector_predicted_rnn[exist_id_num][0]**2 + vector_predicted_rnn[exist_id_num][1]**2 + vector_predicted_rnn[exist_id_num][2]**2 + 1e-9)
y_d_reg_pred_exist_rnn[exist_num][0] = vector_predicted_rnn[exist_id_num][0] / vector_predicted_norm
y_d_reg_pred_exist_rnn[exist_num][1] = vector_predicted_rnn[exist_id_num][1] / vector_predicted_norm
y_d_reg_pred_exist_rnn[exist_num][2] = vector_predicted_rnn[exist_id_num][2] / vector_predicted_norm
y_r_exist_rnn[exist_num] = y_r_rnn[exist_id_num]
y_r_pred_exist_rnn[exist_num] = y_r_pred_rnn[exist_id_num]
exist_num += 1
vector_up = torch.sum(torch.multiply(y_d_reg_pred_exist_rnn,y_d_reg_exist_rnn), axis=1)
vector_down_test = torch.sqrt(torch.multiply(y_d_reg_pred_exist_rnn[:,0],y_d_reg_pred_exist_rnn[:,0]) + torch.multiply(y_d_reg_pred_exist_rnn[:,1],y_d_reg_pred_exist_rnn[:,1]) + torch.multiply(y_d_reg_pred_exist_rnn[:,2],y_d_reg_pred_exist_rnn[:,2]) + 1e-9)
vector_down_gold = torch.sqrt(torch.multiply(y_d_reg_exist_rnn[:,0],y_d_reg_exist_rnn[:,0]) + torch.multiply(y_d_reg_exist_rnn[:,1],y_d_reg_exist_rnn[:,1]) + torch.multiply(y_d_reg_exist_rnn[:,2],y_d_reg_exist_rnn[:,2]) + 1e-9)
angle_result = vector_up/(vector_down_test*vector_down_gold + 1e-9)
angle_result_norm_0 = torch.minimum(angle_result, torch.ones_like(angle_result))
angle_result_norm_1 = torch.maximum(angle_result_norm_0, -torch.ones_like(angle_result))
angle_arccos_rnn = torch.arccos(angle_result_norm_1)*180/np.pi
angle_arccos_gt_rnn = torch.zeros_like(angle_arccos_rnn)
for ss in range(angle_arccos_rnn.shape[0]):
if angle_arccos_rnn[ss] > 90:
angle_arccos_rnn[ss] = 180 - angle_arccos_rnn[ss]
angle_rnn_list.append(torch.mean(angle_arccos_rnn).cpu().detach().numpy())
# Loss 计算
loss_img_seg = lambda_seg * bce_loss_w(y_lab_pred, y_lab, 0.95)
loss_img_centerline = lambda_centerline * bce_loss_w(y_dis_pred, y_dis, 0.95)
loss_img_direction_rnn_class1 = bce_criterion(y_d_class_pred_exist_rnn[:,0,:], y_d_class_exist_rnn[:,0,:])
loss_img_direction_rnn_class2 = bce_criterion(y_d_class_pred_exist_rnn[:,1,:], y_d_class_exist_rnn[:,1,:])
loss_img_direction_rnn_class3 = bce_criterion(y_d_class_pred_exist_rnn[:,2,:], y_d_class_exist_rnn[:,2,:])
loss_img_direction_rnn_class = lambda_d_class * (loss_img_direction_rnn_class1 + loss_img_direction_rnn_class2 + loss_img_direction_rnn_class3)/3
loss_img_direction_rnn_reg = lambda_d_reg * MSE_loss(angle_arccos_rnn, angle_arccos_gt_rnn)
loss_img_radius_rnn = lambda_r * MSE_loss(y_r_pred_exist_rnn, y_r_exist_rnn)
lossL = 1 * loss_img_seg + 1 * loss_img_centerline + 1 * loss_img_direction_rnn_class + 1 * loss_img_direction_rnn_reg + 1 * loss_img_radius_rnn
valid_loss += lossL.item()
valid_class_loss += loss_img_direction_rnn_class.item()
valid_reg_loss += loss_img_direction_rnn_reg.item()
valid_step_temp += 1
# =========================================================================================
# centerline 计算误差
lossdice_seg = dice_score(y_lab_pred, y_lab)
seg_acc_list.append(lossdice_seg.item())
lossdice_centerline = dice_score(y_dis_pred, y_dis)
centerline_acc_list.append(lossdice_centerline.item())
# R的计算误差
y_r1_cpu = y_r_exist_rnn.cpu().detach().numpy()
y_pred_r_cpu = y_r_pred_exist_rnn.cpu().detach().numpy()
# print(y_r1_cpu,y_pred_r_cpu)
for s in range(y_pred_r_cpu.shape[0]):
s_temp = y_r1_cpu[s] - y_pred_r_cpu[s]
if s_temp < 0:
neg_num += 1
neg_loss += abs(s_temp)*r_resize
else:
pos_num += 1
pos_loss += abs(s_temp)*r_resize
# 分类和角度的计算误差
# # 分类误差
y_pred1_cpu = y_d_class_pred_exist_rnn[:,0,:].cpu().detach().numpy()
y_pred2_cpu = y_d_class_pred_exist_rnn[:,1,:].cpu().detach().numpy()
y_pred3_cpu = y_d_class_pred_exist_rnn[:,2,:].cpu().detach().numpy()
y1_bin1_cpu = y_d_class_exist_rnn[:,0,:].cpu().detach().numpy()
y2_bin1_cpu = y_d_class_exist_rnn[:,1,:].cpu().detach().numpy()
y3_bin1_cpu = y_d_class_exist_rnn[:,2,:].cpu().detach().numpy()
pred1 = y_pred1_cpu.argsort(axis=1)[:,-1:]#[-1]#.astype(np.int16) # 获取最大值位置
pred2 = y_pred2_cpu.argsort(axis=1)[:,-1:]#[-1]#.astype(np.int16) # 获取最大值位置
pred3 = y_pred3_cpu.argsort(axis=1)[:,-1:]#[-1]#.astype(np.int16) # 获取最大值位置
y1_bin1_cpu_min = np.min(y1_bin1_cpu, axis=1)
y2_bin1_cpu_min = np.min(y2_bin1_cpu, axis=1)
y3_bin1_cpu_min = np.min(y3_bin1_cpu, axis=1)
for ss in range(pred1.shape[0]):
if pred1[ss][0]>=vector_bins//2:
pred1[ss][0] = vector_bins - 1 - pred1[ss][0]
if pred2[ss][0]>=vector_bins//2:
pred2[ss][0] = vector_bins - 1 - pred2[ss][0]
if pred3[ss][0]>=vector_bins//2:
pred3[ss][0] = vector_bins - 1 - pred3[ss][0]
cur_acc1 = np.mean(np.equal(pred1, y1_bin1_cpu_min)*np.ones_like(pred1))
cur_acc2 = np.mean(np.equal(pred2, y2_bin1_cpu_min)*np.ones_like(pred2))
cur_acc3 = np.mean(np.equal(pred3, y3_bin1_cpu_min)*np.ones_like(pred3))
class_acc_list1.append(cur_acc1)
class_acc_list2.append(cur_acc2)
class_acc_list3.append(cur_acc3)
print("============================================")
print('total num: %d' % (split))
print("--------------------------------------------")
print('分割平均准确率:', np.mean(seg_acc_list))
print('中线平均准确率:', np.mean(centerline_acc_list))
# R的计算误差
print("--------------------------------------------")
print('neg_num: %d neg_loss:%f' % (neg_num, neg_loss/(neg_num+1e-7)))
print('pos_num: %d pos_loss:%f' % (pos_num, pos_loss/(pos_num+1e-7)))
print('average_loss:%f' % ((neg_loss+pos_loss)/(neg_num+pos_num+1e-7)))
# 分类的计算误差
# print("--------------------------------------------")
# print('越界平均准确率:', np.mean(class_acc_list0))
# 角度的计算误差
print("--------------------------------------------")
print('acc1平均准确率:', np.mean(class_acc_list1))
print('acc2平均准确率:', np.mean(class_acc_list2))
print('acc3平均准确率:', np.mean(class_acc_list3))
print('平均偏差角度:', np.mean(angle_rnn_list))
print("============================================")
writer.add_scalar('Test Loss', lossL.item(), global_step=i)
#######################################################
#To write in Tensorboard
#######################################################
train_loss = train_loss / train_step_temp
valid_loss = valid_loss / valid_step_temp
valid_class_loss = valid_class_loss / valid_step_temp
valid_reg_loss = valid_reg_loss / valid_step_temp
if (i+1) % 1 == 0:
print('Epoch: {}/{} \t Training Loss: {:.6f} \t Validation Loss: {:.6f} \t Class Loss: {:.6f} \t Reg Loss: {:.6f}'.format(i + 1, epoch, train_loss, valid_loss, valid_class_loss, valid_reg_loss))
#######################################################
#Early Stopping
#######################################################
if train_seg:
r_loss = 2 - np.mean(seg_acc_list) - np.mean(centerline_acc_list)
else:
r_loss = valid_loss
torch.save(model_train.state_dict(), MODEL_DIR + str(epoch) + '_' + str(batch_size) + '/epoch_' + str(epoch) + '_batchsize_' + str(batch_size) + '.pth')
if r_loss <= r_loss_min:
print('R loss decreased (%6f --> %6f). Saving model ' % (r_loss_min, r_loss))
torch.save(model_train.state_dict(), MODEL_DIR + str(epoch) + '_' + str(batch_size) + '/epoch_' + str(epoch) + '_batchsize_' + str(batch_size) + '_best.pth')
r_loss_min = r_loss
time_elapsed = time.time() - since
print('this epoch time: {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
n_iter += 1
def inference_segmentation(args, model_name, device_ids, device):
predict_centerline_path = args.predict_centerline_path
if not os.path.exists(predict_centerline_path):
os.makedirs(predict_centerline_path)
#######################################################
# Setting the basic paramters of the model
#######################################################
batch_size = args.batch_size
epoch = args.epochs
num_workers = args.n_threads
vector_bins = args.vector_bins
data_shape = args.data_shape
dataset_name = args.dataset_name
resize_radio = args.resize_radio
test_patch_height = args.test_patch_height
test_patch_width = args.test_patch_width
test_patch_depth = args.test_patch_depth
stride_height = args.stride_height
stride_width = args.stride_width
stride_depth = args.stride_depth
#######################################################
# build the model
#######################################################
def model_unet_LSTM(model_input, in_channel=1, out_channel=64):
model = model_input(in_channel, out_channel)
return model
model_test = model_unet_LSTM(model_name, 1, 1)
model_test = torch.nn.DataParallel(model_test, device_ids=device_ids)
model_test.to(device)
#######################################################
# load the checkpoint
#######################################################
MODEL_DIR = str(args.model_save_dir) + str(args.gpu_id) + '/'
checkpoint = torch.load(MODEL_DIR + str(epoch) + '_' + str(batch_size) + '/epoch_' + str(epoch) + '_batchsize_' + str(batch_size) + '.pth')
model_test.load_state_dict(checkpoint)
model_test.eval()
#######################################################
# tracing the image
#######################################################
data_transform = torchvision.transforms.Compose([])
TEST_DIR = args.test_data_path
test_image = glob.glob(TEST_DIR + '*.tif')
for test_img_dir in test_image:
image_name = test_img_dir.split('/')[-1].split('.')[0]
# if image_name != '25800_12940_7244':
# continue
print(test_img_dir)
stack_org = open_tif(test_img_dir).astype(np.float32)
stack_org = np.sqrt(copy.deepcopy(stack_org)) / 255 # * 2 - 1
d, h, w = stack_org.shape
d_new = ((d-(test_patch_depth-stride_depth))//(stride_depth)+1)*(stride_depth) + (test_patch_depth-stride_depth)
h_new = ((h-(test_patch_height-stride_height))//(stride_height)+1)*(stride_height) + (test_patch_height-stride_height)
w_new = ((w-(test_patch_width-stride_width))//(stride_width)+1)*(stride_width) + (test_patch_width-stride_width)
# 重调大小
stack_org_new = np.ones([d_new, h_new, w_new],dtype=np.float32)
stack_org_new[0:d,0:h,0:w] = copy.deepcopy(stack_org)
shape_lab_image = np.zeros([d_new, h_new, w_new],dtype=np.float32)
shape_dis_image = np.zeros([d_new, h_new, w_new],dtype=np.float32)
count_image = np.zeros([d_new, h_new, w_new],dtype=np.float32)
shape_list = []
SHAPE = [data_shape[0],data_shape[1],data_shape[2]]
batch_list = []
batch_lab_list = []
batch_dis_list = []
cnt = 0
for k in range(0, d_new-(test_patch_depth-stride_depth), stride_depth):
for j in range(0, h_new-(test_patch_height-stride_height), stride_height):
for i in range(0, w_new-(test_patch_width-stride_width), stride_width):
patch = stack_org_new[k:k + SHAPE[0], j:j + SHAPE[1], i:i + SHAPE[2]]
r_d, r_h, r_w = patch.shape
batch_list.append(patch.astype(np.float32))
batch_matrix = np.zeros(dtype=np.float32, shape=[len(batch_list), 1, 1, *SHAPE])
for i in range(len(batch_list)):
batch_matrix[i,:,:,:,:,:] = copy.deepcopy(batch_list[i])
batch_input = data_transform(batch_matrix)
train_loader = torch.utils.data.DataLoader(batch_input, batch_size=batch_size, num_workers=num_workers)
num=0
for x_batch in train_loader:
num += 1
num_temp = 0
for x_batch in train_loader:
if num_temp % 5 == 0:
print('num:%d / %d '% (num_temp,num))
num_temp+=1
batch_input = x_batch.to(device)
y_lab_pred, y_dis_pred = model_test(batch_input, 'test_dis')
pred_lab = y_lab_pred.cpu().detach().numpy()
pred_dis = y_dis_pred.cpu().detach().numpy()
for i in range(pred_lab.shape[0]):
batch_lab_list.append(pred_lab[i,0,:,:,:])
batch_dis_list.append(pred_dis[i,0,:,:,:])
num = 0
for k in range(0, d_new-(test_patch_depth-stride_depth), stride_depth):
for j in range(0, h_new-(test_patch_height-stride_height), stride_height):
for i in range(0, w_new-(test_patch_width-stride_width), stride_width):
shape_lab_image[k:k + SHAPE[0],j:j + SHAPE[1], i:i + SHAPE[2]] += batch_lab_list[num]
shape_dis_image[k:k + SHAPE[0],j:j + SHAPE[1], i:i + SHAPE[2]] += batch_dis_list[num]
count_image[k:k + SHAPE[0],j:j + SHAPE[1], i:i + SHAPE[2]] += 1
num += 1
shape_lab_image = shape_lab_image / count_image
shape_dis_image = shape_dis_image / count_image
# pause
shape_lab_image_new = (shape_lab_image[0:d,0:h,0:w])*255
shape_dis_image_new = (shape_dis_image[0:d,0:h,0:w])*255
file_newname_lab = predict_centerline_path + image_name + '.pro.lab.tif' # for DRIVE
file_newname_skl = predict_centerline_path + image_name + '.pro.skl.tif'
save_tif(shape_lab_image_new, file_newname_lab, np.uint8)
save_tif(shape_dis_image_new, file_newname_skl, np.uint8)
def inference_fastdeepbranchtracer(args, model_name, device_ids, device):
predict_swc_path = args.predict_swc_path
if not os.path.exists(predict_swc_path):
os.makedirs(predict_swc_path)
predict_centerline_path = args.predict_centerline_path
predict_seed_path = args.predict_seed_path
#######################################################
# Setting the basic paramters of the model
#######################################################
batch_size = args.batch_size
valid_size = args.valid_rate
epoch = args.epochs
initial_lr = args.lr
num_workers = args.n_threads
vector_bins = args.vector_bins
data_shape = args.data_shape
dataset_name = args.dataset_name
test_patch_height = args.test_patch_height
test_patch_width = args.test_patch_width
test_patch_depth = args.test_patch_depth
stride_height = args.stride_height
stride_width = args.stride_width
stride_depth = args.stride_depth
# 设置追踪模式
tracing_strategy_flag = args.tracing_strategy_mode
SHAPE = [data_shape[0],data_shape[1],data_shape[2]]
#######################################################
# build the model
#######################################################
model_test = model_name(1, 1)
model_test = torch.nn.DataParallel(model_test, device_ids=device_ids)
model_test.to(device)
#######################################################
# load the checkpoint
#######################################################
MODEL_DIR = str(args.model_save_dir) + str(args.gpu_id) + '/'
checkpoint = torch.load(MODEL_DIR + str(epoch) + '_' + str(batch_size) + '/epoch_' + str(epoch) + '_batchsize_' + str(batch_size) + '.pth')
model_test.load_state_dict(checkpoint)
model_test.eval()
#######################################################
# load the data
#######################################################
TEST_DIR = args.test_data_path
test_image = glob.glob(TEST_DIR + '*.tif')
#######################################################
# tracing the image
#######################################################
torch.multiprocessing.set_start_method('spawn')
device_info = [model_test, device]
data_info = [SHAPE, vector_bins]
for test_img_dir in test_image:
image_name = test_img_dir.split('/')[-1].split('.')[0]
# if image_name != '25800_13964_7244':
# continue
begin_time = time.time()
print("loading:", test_img_dir)
stack_img = open_tif(test_img_dir).astype(np.float32)
# 输入预测的 中心线
stack_lab_dir = predict_centerline_path + image_name + '.pro.lab.tif' # for chasedb and road
stack_skl_dir = predict_centerline_path + image_name + '.pro.skl.tif' # for chasedb and road
stack_lab = open_tif(stack_lab_dir).astype(np.float32)
stack_skl = open_tif(stack_skl_dir).astype(np.float32)
th = 128 # normal 100 merge 60
stack_skl[stack_skl<th]=0
stack_skl[stack_skl>=th]=1
# version 1 # 输入swc形式的 seed point
seed_list_temp = [[],[],[]]
stack_seed_swc_dir = predict_seed_path + image_name + '.swc' # .swc
seed_tree = read_swc_tree(stack_seed_swc_dir)
for tn in seed_tree.get_node_list():
seed_list_temp[0].append(round(tn.get_z()*resize_radio)+SHAPE[0])
seed_list_temp[1].append(round(tn.get_y()*resize_radio)+SHAPE[1])
seed_list_temp[2].append(round(tn.get_x()*resize_radio)+SHAPE[2])
# version 2 # 输入tif形式的 seed point
# stack_img_ = stack_img.copy()
# th = 128
# stack_img_[stack_img>th] = th
# stack_img_ = (stack_img_/th*255).astype(np.uint8)
# seed_img, seed_list = generate_skl_seed(stack_img_.astype(np.uint8), SHAPE)
indices = list(range(len(seed_list_temp[0])))
np.random.shuffle(indices)
seed_list = [[],[],[]]
for i in range(len(seed_list_temp[0])):
num = indices[i]
seed_list[0].append(seed_list_temp[0][num])
seed_list[1].append(seed_list_temp[1][num])
seed_list[2].append(seed_list_temp[2][num])
seed_list_flag = np.ones(len(seed_list[0]))
print('共有 %d 个种子点' % (seed_list_flag.shape[0]))
# 设置追踪模式
# tracing_strategy_flag = 'centerline'
# tracing_strategy_flag = 'angle'
tracing_strategy_flag = 'anglecenterline'
# ===============resize the image=====================
d, h, w = stack_img.shape
d_new = ((d-(test_patch_depth-stride_depth))//(stride_depth)+1)*(stride_depth) + (test_patch_depth-stride_depth)
h_new = ((h-(test_patch_height-stride_height))//(stride_height)+1)*(stride_height) + (test_patch_height-stride_height)
w_new = ((w-(test_patch_width-stride_width))//(stride_width)+1)*(stride_width) + (test_patch_width-stride_width)
# 重调大小
stack_img_new = np.zeros([d_new, h_new, w_new],dtype=np.float32)
stack_img_new[0:d,0:h,0:w] = copy.deepcopy(stack_img)
stack_lab_new = np.zeros([d_new, h_new, w_new],dtype=np.uint16)
stack_skl_new = np.zeros([d_new, h_new, w_new],dtype=np.uint16)
stack_lab_new[0:d,0:h,0:w] = copy.deepcopy(stack_lab)
stack_skl_new[0:d,0:h,0:w] = copy.deepcopy(stack_skl)
org_img_shape = stack_img_new.shape
org_img_temp = np.zeros([org_img_shape[0]+2*SHAPE[0],org_img_shape[1]+2*SHAPE[1],org_img_shape[2]+2*SHAPE[2]])
org_lab_temp = np.zeros([org_img_shape[0]+2*SHAPE[0],org_img_shape[1]+2*SHAPE[1],org_img_shape[2]+2*SHAPE[2]])
org_skl_temp = np.zeros([org_img_shape[0]+2*SHAPE[0],org_img_shape[1]+2*SHAPE[1],org_img_shape[2]+2*SHAPE[2]])
org_img_temp[1*SHAPE[0]:1*SHAPE[0] + org_img_shape[0], 1*SHAPE[1]:1*SHAPE[1] + org_img_shape[1], 1*SHAPE[2]:1*SHAPE[2] + org_img_shape[2]] = copy.deepcopy(stack_img_new)
org_lab_temp[1*SHAPE[0]:1*SHAPE[0] + org_img_shape[0], 1*SHAPE[1]:1*SHAPE[1] + org_img_shape[1], 1*SHAPE[2]:1*SHAPE[2] + org_img_shape[2]] = copy.deepcopy(stack_lab_new)
org_skl_temp[1*SHAPE[0]:1*SHAPE[0] + org_img_shape[0], 1*SHAPE[1]:1*SHAPE[1] + org_img_shape[1], 1*SHAPE[2]:1*SHAPE[2] + org_img_shape[2]] = copy.deepcopy(stack_skl_new)
shape_image = np.zeros([d_new, h_new, w_new],dtype=np.float32)
# =====================================================
pos_list = []