-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_CRC_7.py
184 lines (171 loc) · 8.9 KB
/
dataset_CRC_7.py
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
import random
import torch
from torch.utils.data import DataLoader
import os
import numpy as np
# Trans
class CRC(torch.utils.data.Dataset):
def __init__(self, label_flag=None, train=True, gen_cand=False):
self.train = train
self.label_flag = label_flag
self.gen_cand = gen_cand
# 1. load all features and labels
save_path = "./embeddings/"
if train or gen_cand:
self.all_patches_ori = np.load(os.path.join(save_path, "./training_datasets/embeddings.npy"))
self.labels_ori = np.load(os.path.join(save_path, "./training_datasets/labels.npy"))
self.all_patches = []
self.labels = []
for i in range(len(self.all_patches_ori)):
if self.labels_ori[i] != 1 and self.labels_ori[i] != 2:
self.all_patches.append(self.all_patches_ori[i])
self.labels.append(self.labels_ori[i])
self.all_patches = np.array(self.all_patches)
self.labels = np.array(self.labels)
self.class2index = np.load(os.path.join(save_path, "./training_datasets/class2index.npy"))
else:
# test pick data from unlabelled pool
self.all_patches_ori = np.load(os.path.join(save_path, "./testing_datasets/embeddings.npy"))
self.labels_ori = np.load(os.path.join(save_path, "./testing_datasets/labels.npy"))
self.all_patches = []
self.labels = []
for i in range(len(self.all_patches_ori)):
if self.labels_ori[i] != 1 or self.labels_ori[i] != 2:
self.all_patches.append(self.all_patches_ori[i])
self.labels.append(self.labels_ori[i])
self.all_patches = np.array(self.all_patches)
self.labels = np.array(self.labels)
self.class2index = np.load(os.path.join(save_path, "./testing_datasets/class2index.npy"))
self.all_patches_test = []
self.labels_test = []
for i in range(len(self.labels)):
if self.labels[i] == 3:
self.all_patches_test.append(self.all_patches[i])
self.labels_test.append(0)
elif self.labels[i] == 6:
self.all_patches_test.append(self.all_patches[i])
self.labels_test.append(1)
elif self.labels[i] == 8:
self.all_patches_test.append(self.all_patches[i])
self.labels_test.append(2)
self.num_patches = self.all_patches.shape[0]
self.num_labels = self.labels.shape[0]
print("[DATA INFO] labels is {}, num_patches is {}".format(
self.num_labels, self.num_patches))
# match with all_patches and their labels
self.indexes = np.arange(0, self.num_patches, 1)
self.indexes_tmp = self.indexes.copy()
self.indexes_unlabelled = []
if self.train or self.gen_cand:
if label_flag is None:
# pick 0.01 percent data to initialize and train the model
self.label_flag = np.zeros_like(self.labels)
random.shuffle(self.indexes_tmp)
# print((int)(len(self.indexes_tmp)*0.01)) # 799
label_length = int(len(self.indexes_tmp)*0.01)
for i in range(label_length):
self.label_flag[self.indexes_tmp[i]] = 1 # annotated
# generate unlabelled pool
for i in range(len(self.indexes)):
if self.label_flag[i] == 0:
self.indexes_unlabelled.append(i)
# else:
# # label_flag + len(unlabelled_pool)*0.04
# for i in range(len(self.indexes)):
# if self.label_flag[i] == 0:
# self.indexes_unlabelled.append(self.indexes[i]) # collect indexes
# random.shuffle(self.indexes_unlabelled)
# for i in range((int)(len(self.all_patches) * 0.04)):
# if self.label_flag[self.indexes_unlabelled[i]] == 1:
# print('error!')
# self.label_flag[self.indexes_unlabelled[i]] = 1 # annotated
# self.indexes_unlabelled = []
# for i in range(len(self.indexes)):
# if self.label_flag[i] == 0:
# self.indexes_unlabelled.append(i)
self.class3patches = []
self.class3labels = []
self.class3indexes = []
self.class6indexes = []
for i in range(len(self.all_patches)):
if self.label_flag[i] == 1 and (self.labels[i] == 8 or self.labels[i] == 3 or self.labels[i] == 6):
self.class3patches.append(self.all_patches[i])
self.class3labels.append(self.labels[i])
self.class3indexes.append(self.indexes[i])
if self.label_flag[i] == 1 and self.labels[i] != 3 and self.labels[i] != 6 and self.labels[i] != 8:
self.class6indexes.append(self.indexes[i])
print('first initial model 3class length:',len(self.class3patches))
self.unlabelled_patches = []
self.unlabelled_labels = []
self.unlabelled_indexes = []
num = 0
for i in range(len(self.all_patches)):
if self.label_flag[i] == 0:
self.unlabelled_patches.append(self.all_patches[i])
self.unlabelled_labels.append(self.labels[i])
self.unlabelled_indexes.append(self.indexes[i])
else:
num += 1
print("")
def __getitem__(self, index):
# patch_image = self.all_patches_pos_slides[index]
# patch_image = torch.from_numpy(patch_image)
# each_image_w = torch.nn.functional.dropout(patch_image, p=0.2, training=False)
# each_image_s = torch.nn.functional.dropout(patch_image, p=0.4, training=False)
if self.train:
# pick 3 class which are already annotated(labelled pool)
patch_feat = self.class3patches[index]
patch_label = self.class3labels[index]
patch_index = self.class3indexes[index]
if patch_label == 3:
patch_label = 0
elif patch_label == 6:
patch_label = 1
elif patch_label == 8:
patch_label = 2
return patch_feat, patch_label, patch_index
elif self.gen_cand:
# eval(unlabelled pool)
patch_feat = self.unlabelled_patches[index]
patch_label = self.unlabelled_labels[index] # will not be used
patch_index = self.unlabelled_indexes[index]
return patch_feat, patch_label, patch_index
else:
# test always the same
patch_feat = self.all_patches_test[index]
patch_label = self.labels_test[index] #
return patch_feat, patch_label
def __len__(self):
if self.train:
return len(self.class3labels)
elif self.gen_cand:
return len(self.unlabelled_labels)
else:
return len(self.labels_test)
def load_dataset(batch_size=1, label_flag=None):
# initialize
train_dataset = CRC(label_flag, train=True)
first_label_flag = train_dataset.label_flag
# unlabelled_pool_indexes = train_dataset.unlabelled_indexes
gen_can_dataset = CRC(label_flag=first_label_flag, train=False, gen_cand=True) # according to ssd to gennerate
unlabelled_pool_indexes = gen_can_dataset.unlabelled_indexes
labels = gen_can_dataset.labels
patches = gen_can_dataset.all_patches
class3indexes = gen_can_dataset.class3indexes
class6indexes = gen_can_dataset.class6indexes
test_dataset = CRC(label_flag=first_label_flag, train=False) # stay the same
train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=256,
shuffle=True, num_workers=0, drop_last=False, pin_memory=True)
gen_can_dataloader = torch.utils.data.DataLoader(gen_can_dataset, batch_size=256,
shuffle=False, num_workers=0, drop_last=False, pin_memory=True)
test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=256,
shuffle=False, num_workers=0, drop_last=False, pin_memory=True)
return train_dataloader, gen_can_dataloader, test_dataloader, unlabelled_pool_indexes, labels, patches, class3indexes, class6indexes, first_label_flag
if __name__ == '__main__':
train_dataloader, gen_can_dataloader, test_dataloader = load_dataset()
for i in train_dataloader:
print(i)
break
for i in test_dataloader:
print(i)
break