-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
152 lines (145 loc) · 7.81 KB
/
Copy pathdataset.py
File metadata and controls
152 lines (145 loc) · 7.81 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
from torch.utils.data import Dataset
import torch
import numpy as np
import os
import glob
import pandas as pd
import random
class InstanceDataset(Dataset):
def __init__(self, ood_dataset, args) -> None:
super(InstanceDataset).__init__()
self.ood_dataset = ood_dataset
self.args = args
self.in_dataframe = pd.read_csv('datasets_csv/Camelyon/binary_Camelyon_testval.csv')
self.out_datalist = glob.glob(f'Feats/{ood_dataset}/*/*/features.pt')
def get_instance_feats(self, csv_file_df):
feats = csv_file_df.iloc[1].split('\n')[0]
if self.args.extractor == 'Kimia':
feats = feats.replace('/data1/WSI/Patches/Features/COAD', 'Feats/COAD')
feats = feats.replace('/data1/WSI/Patches/Features/PRAD', 'Feats/PRAD')
feats = feats.replace('/data1/WSI/Patches/Features/Camelyon16', 'Feats/Camelyon')
feats = feats.replace('simclr_files_256_v0', 'Camelyon16_Tissue_Kimia_20x')
feats_csv_path = feats + '/features.pt'
feats = torch.load(feats_csv_path).cuda()
feats = feats[np.random.permutation(len(feats))]
feats, labels = self.construct_ood_feats(feats)
permutation = np.random.permutation(len(feats))
feats = feats[permutation]
labels = labels[permutation]
return labels, feats
def construct_ood_feats(self, feats):
ood_instances = random.choices(self.out_datalist, k=10)
ood_feats = torch.load(ood_instances[0])
for i in range(1, len(ood_instances)):
ood_feats = torch.cat((ood_feats, torch.load(ood_instances[i])), dim=0)
num_ood_instances = round(feats.shape[0]*self.args.ood_ratio)
ood_feats = ood_feats[np.random.permutation(len(ood_feats))][:num_ood_instances]
feats = torch.cat((feats[num_ood_instances:],ood_feats),dim=0)
labels = torch.cat((torch.zeros(feats.shape[0]-num_ood_instances),torch.ones(num_ood_instances)),dim=0)
return feats, labels
def __len__(self):
return len(self.in_dataframe)
def __getitem__(self, idx):
labels, feats = self.get_instance_feats(self.in_dataframe.iloc[idx])
return labels, feats
class BagDataset(Dataset):
def __init__(self, train_path, args) -> None:
super(BagDataset).__init__()
self.train_path = train_path
self.args = args
# self.database = redis.Redis(host='localhost', port=6379)
def get_bag_feats(self, csv_file_df, args):
# if args.dataset == 'TCGA-lung-default':
# feats_csv_path = 'datasets/tcga-dataset/tcga_lung_data_feats/' + csv_file_df.iloc[0].split('/')[1] + '.csv'
if args.extractor == 'Kimia':
if args.dataset == 'NSCLC':
if 'LUAD' in csv_file_df.iloc[1]:
pre_path = 'Feats'
slide_name = csv_file_df.iloc[1].split('/')[-1].split('\n')[0]
slide_name = slide_name.split('.')[0]
feats_csv_path = pre_path + '/LUAD/LUAD_Diagnostic_Kimia_20x/' + slide_name + '/features.pt'
else:
pre_path = 'Feats'
slide_name = csv_file_df.iloc[1].split('/')[-1].split('\n')[0]
slide_name = slide_name.split('.')[0]
feats_csv_path = pre_path + '/LUSC/LUSC_Diagnostic_Kimia_20x/' + slide_name + '/features.pt'
elif args.dataset == 'Camelyon':
feats = csv_file_df.iloc[1].split('\n')[0]
feats = feats.replace('/data1/WSI/Patches/Features/COAD', 'Feats/COAD')
feats = feats.replace('/data1/WSI/Patches/Features/BRCA', 'Feats/BRCA')
feats = feats.replace('/data1/WSI/Patches/Features/PRAD', 'Feats/PRAD')
feats = feats.replace('/data1/WSI/Patches/Features/Camelyon16', 'Feats/Camelyon')
feats = feats.replace('simclr_files_256_v0', 'Camelyon16_Tissue_Kimia_20x')
feats_csv_path = feats + '/features.pt'
elif args.dataset == 'BRACS_WSI':
feats = csv_file_df.iloc[1].split('\n')[0]
feats_csv_path = 'Feats/BRACS_WSI/BRACS_WSI_Kimia_20x/' + feats + '/features.pt'
else:
feats = csv_file_df.iloc[1].split('\n')[0]
feats = feats.replace('/data1/WSI/Patches/Features/COAD', 'Feats/COAD')
feats = feats.replace('/data1/WSI/Patches/Features/BRCA', 'Feats/BRCA')
feats_csv_path = feats + '/features.pt'
elif args.extractor == 'Resnet':
if args.dataset == 'NSCLC':
if 'LUAD' in csv_file_df.iloc[1]:
pre_path = 'Feats'
slide_name = csv_file_df.iloc[1].split('/')[-1].split('\n')[0]
slide_name = slide_name.split('.')[0]
feats_csv_path = pre_path + '/LUAD/LUAD_Diagnostic_ResNet_20x/' + slide_name + '/features.pt'
else:
pre_path = 'Feats'
slide_name = csv_file_df.iloc[1].split('/')[-1].split('\n')[0]
slide_name = slide_name.split('.')[0]
feats_csv_path = pre_path + '/LUSC/LUSC_Diagnostic_ResNet_20x/' + slide_name + '/features.pt'
elif args.dataset == 'Camelyon':
feats = csv_file_df.iloc[1].split('\n')[0]
feats = feats.replace('/data1/WSI/Patches/Features/Camelyon16', 'Feats/Camelyon')
# feats = feats.replace('simclr_files_256_v0', 'Camelyon16_Tissue_ResNet_20x')
# feats_csv_path = feats + '/features.pt'
feats = feats.replace('simclr_files_256_v0', 'Camelyon16_ImageNet')
feats = feats.replace('training', 'train')
feats = feats.replace('testing', 'test')
feats_csv_path = feats + '.pt'
elif args.dataset == 'BRACS_WSI':
feats = csv_file_df.iloc[1].split('\n')[0]
feats_csv_path = 'Feats/BRACS_WSI/BRACS_WSI_Kimia_20x/' + feats + '/features.pt'
else:
feats_csv_path = csv_file_df.iloc[1]
feats_csv_path = feats_csv_path.split('\n')[0] + '/features.pt'
elif args.extractor == 'dsmil':
if args.dataset == 'Camelyon':
feats = csv_file_df.iloc[1].split('\n')[0]
feats = feats.replace('/data1/WSI/Patches/Features/Camelyon16', 'Feats/Camelyon')
feats = feats.replace('simclr_files_256_v0', 'simclr_files_256_v2')
feats_csv_path = feats + '/features.pt'
else:
raise NotImplementedError
else:
raise NotImplementedError
try:
feats = torch.load(feats_csv_path).cuda()
except:
print('i=1')
feats = feats[np.random.permutation(len(feats))]
label = np.zeros(args.num_classes)
if args.num_classes == 1:
label[0] = csv_file_df.iloc[0]
else:
if int(csv_file_df.iloc[0]-1) <= (len(label) - 1):
label[int(csv_file_df.iloc[0])-1] = 1
# label = csv_file_df.iloc[0]
# label = csv_file_df.iloc[0]
label = torch.tensor(np.array(label))
return label, feats
def dropout_patches(self, feats, p):
idx = np.random.choice(np.arange(feats.shape[0]), int(feats.shape[0] * (1 - p)), replace=False)
sampled_feats = np.take(feats, idx, axis=0)
pad_idx = np.random.choice(np.arange(sampled_feats.shape[0]), int(feats.shape[0] * p), replace=False)
pad_feats = np.take(sampled_feats, pad_idx, axis=0)
sampled_feats = np.concatenate((sampled_feats, pad_feats), axis=0)
return sampled_feats
def __getitem__(self, idx):
label, feats = self.get_bag_feats(self.train_path.iloc[idx], self.args)
return label, feats, self.train_path.iloc[idx].iloc[1]
def __len__(self):
return len(self.train_path)