如何按数字顺序对数据路径进行排序?

How can I sort datapath in numerical order?

我有10000119~10000130个病人的3D ct影像 我已将每个患者数据的 3D CT 图像沿 z 轴分成 250 个切片。

我想为每位患者的每个切片按数字顺序对该数据路径进行排序。 我想要做的是:

/mnt/intern/code/dataset/test/original2Densenble/10000119slice0.mat
/mnt/intern/code/dataset/test/original2Densenble/10000119slice1.mat
/mnt/intern/code/dataset/test/original2Densenble/10000119slice2.mat
/mnt/intern/code/dataset/test/original2Densenble/10000119slice3.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000119slice250.mat
/mnt/intern/code/dataset/test/original2Densenble/10000120slice0.mat
/mnt/intern/code/dataset/test/original2Densenble/10000120slice1.mat
/mnt/intern/code/dataset/test/original2Densenble/10000120slice2.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000120slice250.mat
/mnt/intern/code/dataset/test/original2Densenble/10000121slice0.mat
/mnt/intern/code/dataset/test/original2Densenble/10000121slice1.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000121slice250.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000130slice0.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000130slice250.mat

我这样做的原因是因为我想将 2D Unet softmax 输出与 3D Unet softmax 输出集成。 所以我认为我需要为每个患者的每个切片对 2D 切片进行排序,以表示超过 2D Unet softmax 输出和 3D Unet softmax 输出。 为此,我认为我首先需要按数字顺序加载 2D 数据。 我想到了拆分功能,但我认为它最终会因为患者的编号(10000119~10000130)和患者的切片编号(每个患者的slice0.mat~slice250.mat)而混淆。

我找到了自己的答案..!我希望这也能帮助其他深度学习工程师! 希望你有美好的一天;希望你今天过得很开心! 我明确编码以获得手头的直觉。

class SliceData2D(Dataset):

    def __init__(self, root_data, root_label, transform):

        self.transform = transform
        self.examples = []
        self.examples2 = []

        data_files = list(pathlib.Path(root_data).iterdir())
        label_files = list(pathlib.Path(root_label).iterdir())
        data_files = list(map(str, data_files))
        label_files = list(map(str, label_files))
        # print(len(data_files))
        # print(len(label_files))
        data_files = sorted(data_files)
        label_files = sorted(label_files)
        del label_files[0] # /mnt/intern/code/dataset/test/label2Densemble/.mat 이상한 파일 1개 제거
        # print(len(label_files))

        data1 = sorted(data_files[0 : 250], key=lambda x: int(           (x.split('slice')[1]).split('.')[0]    ) )
        data2 = sorted(data_files[250 : 500], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data3 = sorted(data_files[500 : 750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data4 = sorted(data_files[750 : 1000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data5 = sorted(data_files[1000 : 1250], key=lambda x: int(     (x.split('slice')[1]).split('.')[0]    ) )
        data6 = sorted(data_files[1250 : 1500], key=lambda x: int(      (x.split('slice')[1]).split('.')[0]    ) )
        data7 = sorted(data_files[1500 : 1750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data8 = sorted(data_files[1750 : 2000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data9 = sorted(data_files[2000 : 2250], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data10 = sorted(data_files[2250 : 2500], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )
        data11 = sorted(data_files[2500 : 2750], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )
        
        # print(data1, '\n', data2, '\n', data3, '\n', data4, '\n', data5, '\n', data6, '\n', data7, '\n', data8, '\n', data9, '\n', data10, '\n', data11, '\n')
        
        data1 = list(map(pathlib.Path, data1))
        data2 = list(map(pathlib.Path, data2))
        data3 = list(map(pathlib.Path, data3))
        data4 = list(map(pathlib.Path, data4))
        data5 = list(map(pathlib.Path, data5))
        data6 = list(map(pathlib.Path, data6))
        data7 = list(map(pathlib.Path, data7))
        data8 = list(map(pathlib.Path, data8))
        data9 = list(map(pathlib.Path, data9))
        data10 = list(map(pathlib.Path, data10))
        data11 = list(map(pathlib.Path, data11))

        label1 = sorted(label_files[0 : 250], key=lambda x: int(           (x.split('slice')[1]).split('.')[0]    ) )
        label2 = sorted(label_files[250 : 500], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label3 = sorted(label_files[500 : 750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label4 = sorted(label_files[750 : 1000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label5 = sorted(label_files[1000 : 1250], key=lambda x: int(     (x.split('slice')[1]).split('.')[0]    ) )
        label6 = sorted(label_files[1250 : 1500], key=lambda x: int(      (x.split('slice')[1]).split('.')[0]    ) )
        label7 = sorted(label_files[1500 : 1750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label8 = sorted(label_files[1750 : 2000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label9 = sorted(label_files[2000 : 2250], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label10 = sorted(label_files[2250 : 2500], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )
        label11 = sorted(label_files[2500 : 2750], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )

        # print(label1, '\n', label2, '\n', label3, '\n', label4, '\n', label5, '\n', label6, '\n', label7, '\n', label8, '\n', label9, '\n', label10, '\n', label11, '\n')
        
        label1 = list(map(pathlib.Path, label1))
        label2 = list(map(pathlib.Path, label2))
        label3 = list(map(pathlib.Path, label3))
        label4 = list(map(pathlib.Path, label4))
        label5 = list(map(pathlib.Path, label5))
        label6 = list(map(pathlib.Path, label6))
        label7 = list(map(pathlib.Path, label7))
        label8 = list(map(pathlib.Path, label8))
        label9 = list(map(pathlib.Path, label9))
        label10 = list(map(pathlib.Path, label10))
        label11 = list(map(pathlib.Path, label11))


        self.examples = data1 + data2 + data3 + data4 + data5 + data6 + data7 + data8 + data9 + data10 + data11
        self.examples2 = label1 + label2 + label3 + label4 + label5 + label6 + label7 + label8 + label9 + label10 + label11
        print(self.examples)
        
    def __len__(self):
        
        return len(self.examples)

    def __getitem__(self, i):
        
        fname = self.examples[i]
        fname2 = self.examples2[i]
        
        data_name = str(pathlib.Path(fname))
        label_name = str(pathlib.Path(fname2))
        
        d_load = scipy.io.loadmat(data_name); l_load = scipy.io.loadmat(label_name)
        data = d_load['data']; label = l_load['label']
            
        return self.transform(data, label, fname.name)