-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
145 lines (126 loc) · 6.01 KB
/
dataset.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
import torch
from torch.utils.data import Dataset, DataLoader
import torchvision
from PIL import Image
import numpy as np
class remove_channel(torch.nn.Module):
def __init__(self, p=0.2):
super().__init__()
self.p = p
def forward(self, img):
if np.random.rand() <= self.p:
img[0] = torch.zeros(1, *img.shape[1:])
if np.random.rand() <= self.p:
img[1] = torch.zeros(1, *img.shape[1:])
if np.random.rand() <= self.p:
img[2] = torch.zeros(1, *img.shape[1:])
return img
class DiffusionDatasetSuperRes(Dataset):
def __init__(self, df):
self.transforms = torchvision.transforms.Compose([torchvision.transforms.RandomCrop(256)])
self.additional_transforms = torchvision.transforms.Compose([torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor(), remove_channel(0.2)])
self.dino_transforms = torchvision.transforms.Compose([ torchvision.transforms.Pad(50)])
self.df = df
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
image = Image.open(self.df.iloc[idx, 0])
counter = 0
img = self.transforms(image)
while(torch.count_nonzero(torchvision.transforms.ToTensor()(img)) / (256 * 256 * 3) < 0.3 and counter < 50):
img = self.transforms(image)
counter += 1
img = self.additional_transforms(img)
return img, self.dino_transforms(img)
class DiffusionDataset(Dataset):
def __init__(self, df):
self.transforms = torchvision.transforms.Compose([torchvision.transforms.RandomCrop(256)])
self.additional_transforms = torchvision.transforms.Compose([torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor(), remove_channel(0.2)])
self.img_transforms = torchvision.transforms.Compose([torchvision.transforms.Resize(64)])
self.dino_transforms = torchvision.transforms.Compose([ torchvision.transforms.Pad(50)])
self.df = df
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
image = Image.open(self.df.iloc[idx, 0])
counter = 0
img = self.transforms(image)
while(torch.count_nonzero(torchvision.transforms.ToTensor()(img)) / (256 * 256 * 3) < 0.3 and counter < 50):
img = self.transforms(image)
counter += 1
img = self.additional_transforms(img)
return self.img_transforms(img), self.dino_transforms(img)
class DiffusionDataset1D(Dataset):
def __init__(self, df, smile_emb):
self.df = df
self.smiles = smile_emb
self.transforms = torchvision.transforms.Compose([torchvision.transforms.RandomCrop(256), torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor()])
self.pad = torchvision.transforms.Compose([torchvision.transforms.Pad(50)])
self.control = df
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
counter = 0
counter1 = 0
image = Image.open(self.df.iloc[idx, 0])
img = self.transforms(image)
smile = self.df.iloc[idx, 2]
smile_vec = self.smiles[self.smiles["smiles"] == smile].iloc[0, :256]
smile_vec = torch.tensor([smile_vec], dtype = torch.float32)
cell_type = self.df.iloc[idx, 3]
subset = self.control[self.control["CellLine"] == cell_type]
subset = subset[subset["smile"] == "CS(=O)C"]
control = subset.sample(n = 1)
control_img = Image.open(control.iloc[0, 0])
control_img = self.transforms(control_img)
while(torch.count_nonzero(control_img)/ (256 * 256 * 3) < 0.3 and counter1 < 50):
control = subset.sample(n = 1)
control_img = Image.open(control.iloc[0, 0])
control_img = self.transforms(control_img)
counter1 += 1
while(torch.count_nonzero(img) / (256 * 256 * 3) < 0.3 and counter < 50):
img = self.transforms(image)
counter += 1
img = self.pad(img)
control_img = self.pad(img)
return img, smile_vec, control_img
class DiffusionDataset1DSample(Dataset):
def __init__(self, df, smile_emb):
self.df = df
self.smiles = smile_emb
self.transforms = torchvision.transforms.Compose([torchvision.transforms.RandomCrop(256), torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor()])
self.pad = torchvision.transforms.Compose([torchvision.transforms.Pad(50)])
self.control = df
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
counter = 0
counter1 = 0
image = Image.open(self.df.iloc[idx, 0])
img = self.transforms(image)
smile = self.df.iloc[idx, 2]
smile_vec = self.smiles[self.smiles["smiles"] == smile].iloc[0, :256]
smile_vec = torch.tensor([smile_vec], dtype = torch.float32)
cell_type = self.df.iloc[idx, 3]
subset = self.control[self.control["CellLine"] == cell_type]
subset = subset[subset["smile"] == "CS(=O)C"]
control = subset.sample(n = 1)
control_img = Image.open(control.iloc[0, 0])
control_img = self.transforms(control_img)
while(torch.count_nonzero(control_img)/ (256 * 256 * 3) < 0.3 and counter1 < 50):
control = subset.sample(n = 1)
control_img = Image.open(control.iloc[0, 0])
control_img = self.transforms(control_img)
counter1 += 1
while(torch.count_nonzero(img) / (256 * 256 * 3) < 0.3 and counter < 50):
img = self.transforms(image)
counter += 1
unpadded_img = img
small_img = torchvision.transforms.Resize(64)(img)
img = self.pad(img)
control_img = self.pad(img)
return img, smile_vec, control_img, unpadded_img, small_img