-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
82 lines (69 loc) · 2.45 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
'''
加载数据的原图,局部的图形。
'''
import PIL.Image as Image
import os
import torch.utils.data as data
import matplotlib.pyplot as plt
import numpy as np
import torch
import random
def make_dataset(path):
imgs = []
img_types = ['adenoma','cancer','normal','polyp']
for i in range(4):
img_dir_path = os.path.join(path,img_types[i],'image')
img_name = os.listdir(img_dir_path)
for name in img_name:
img_path = os.path.join(img_dir_path,name)
imgs.append((img_path, i))
return imgs
def swap(img, crop, p):
def crop_image(image, cropnum):
width, high = image.shape[1], image.shape[2]
crop_x = [int((width / cropnum[0]) * i) for i in range(cropnum[0] + 1)]
crop_y = [int((high / cropnum[1]) * i) for i in range(cropnum[1] + 1)]
im_list = []
for j in range(len(crop_y) - 1):
for i in range(len(crop_x) - 1):
img = image[:, crop_y[j]:min(crop_y[j + 1], high), crop_x[i]:min(crop_x[i + 1], width)]
img = np.fft.fftshift(np.fft.fftn(img))
# img = np.abs(img)
im_list.append(img)
return im_list
images = crop_image(img, crop)
if p > random.random():
random.shuffle(images)
width, high = img.shape[0],img.shape[1]
iw = int(width / crop[0])
ih = int(high / crop[1])
img1 = []
for i in range(crop[0]):
img1.append(np.concatenate(images[i*crop[0]:(i+1)*crop[0]],1))
toImage = np.concatenate(img1,2)
return toImage
class BowelDataset(data.Dataset):
def __init__(self, root, transform=None):
imgs = make_dataset(root)
self.imgs = imgs
self.transform = transform
def __getitem__(self, index):
crop_path, label = self.imgs[index]
img = Image.open(crop_path)
if self.transform is not None:
img = self.transform(img)
img = np.array(img)
img = img.astype(np.float32)
img = img/255.0
img = img.transpose((2, 0, 1))
img_F = swap(img, (40, 40), p=0.1)
F_real = np.real(img_F)
F_imag = np.imag(img_F)
F_complex = np.concatenate((F_real, F_imag), axis=0)
F_complex = torch.tensor(F_complex).float()
label = torch.tensor(label)
return F_complex, label
def __len__(self):
return len(self.imgs)
def getPath(self):
return self.imgs