a b/utils/dataloader.py
1
import pickle
2
import numpy as np
3
import os
4
from utils.equalizer import *
5
from config import *
6
7
#dataset_path: path to *.np dataset that has been preprocessed
8
#normdata_path: path to directory where norm and equalization data is stored ('normalization.np','equalization.np')
9
#img_res: the shape of the orthotopres (cubes) being processed
10
class DataLoader():
11
    def __init__(self, dataset_path, normdata_path, img_res=None):
12
        self.normdata_path = normdata_path
13
        if img_res is not None:
14
            self.img_res = img_res
15
        else:
16
            self.img_res = config['cube_size']
17
18
        self.m_xlims = config['mask_xlims']
19
        self.m_ylims = config['mask_ylims']
20
        self.m_zlims = config['mask_zlims']
21
        print("loading preprocessed dataset...")
22
        self.data_train = np.load(dataset_path)
23
        # format for nerual net
24
        self.data_train = self.data_train.reshape((len(self.data_train), self.img_res[0], self.img_res[1], self.img_res[2], 1))
25
        # shuffle
26
        np.random.shuffle(self.data_train)
27
28
    def load_data(self, batch_size=1, is_testing=False):
29
        if is_testing == False:
30
            idx = np.random.permutation(len(self.data_train))
31
            batch_images = self.data_train[idx[:batch_size]]
32
        else:
33
            idx = np.random.permutation(len(self.data_train))
34
            batch_images = self.data_train[idx[:batch_size]]
35
        imgs_A = []
36
        imgs_B = []
37
        for i, img in enumerate(batch_images):
38
            imgs_A.append(img)
39
            img_out = np.copy(img)
40
            img_out[self.m_zlims[0]:self.m_zlims[1], self.m_xlims[0]:self.m_xlims[1], self.m_ylims[0]:self.m_ylims[1]] = 0
41
            imgs_B.append(img_out)
42
43
        return np.array(imgs_A), np.array(imgs_B)
44
45
    def load_batch(self, batch_size=1, is_testing=False):
46
        if is_testing == False:
47
            self.n_batches = int(len(self.data_train) / batch_size)
48
        else:
49
            self.n_batches = int(len(self.data_train) / batch_size)
50
51
        for i in range(self.n_batches - 1):
52
            if is_testing == False:
53
                batch = self.data_train[i * batch_size:(i + 1) * batch_size]
54
            else:
55
                batch = self.data_train[i * batch_size:(i + 1) * batch_size]
56
            imgs_A = []
57
            imgs_B = []
58
            for i, img in enumerate(batch):
59
                imgs_A.append(img)
60
                img_out = np.copy(img)
61
                img_out[self.m_zlims[0]:self.m_zlims[1], self.m_xlims[0]:self.m_xlims[1], self.m_ylims[0]:self.m_ylims[1]] = 0
62
                imgs_B.append(img_out)
63
            imgs_A = np.array(imgs_A)
64
            imgs_B = np.array(imgs_B)
65
            yield imgs_A, imgs_B
66