Exemple #1
0
    def fit(self, directory, subject_index=None):
        """Ffitits internal statistics to some sample data.
        """
        self.mean = np.zeros((1,) + self.image_shape, dtype=K.floatx())
        self.std = np.zeros((1,) + self.image_shape, dtype=K.floatx())

        nb_train_samples = len(subject_index)

        paths_train = []
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(".npz"):
                    paths_train.append(os.path.join(root, file))
        filenames = sort_nicely(paths_train)

        for i in range(nb_train_samples):
            fname = filenames[subject_index[i]]
            img, label = load_img(fname)
            self.mean += np.true_divide(img,nb_train_samples)

        for i in range(nb_train_samples):
            fname = filenames[subject_index[i]]
            img, label = load_img(fname)
            self.std += np.true_divide(np.square(img-self.mean),nb_train_samples)

        self.std = np.sqrt(self.std)
Exemple #2
0
    def __init__(self, directory, image_data_generator,
                 subject_index=None,
                 image_shape=(256, 256, 256),
                 nb_class = None,
                 labels_permuted=None,
                 batch_size=32, shuffle=True, seed=None):
        self.directory = directory
        self.image_data_generator = image_data_generator
        self.nb_class = nb_class
        self.image_shape = image_shape
        self.labels_permuted = labels_permuted

        # first, count the number of samples and classes
        self.nb_sample = len(subject_index)
        self.filenames = []
        self.subject_index = subject_index

        paths_train = []
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(".npz"):
                    paths_train.append(os.path.join(root, file))

        self.filenames = sort_nicely(paths_train)

        print('Found %d neuroimages in the directory.' % (self.nb_sample))

        super(DirectoryIterator, self).__init__(self.nb_sample, batch_size, shuffle, seed)
Exemple #3
0
def create_kernel(config_module):
    paths = config_module.path_files
    input_data_type = config_module.input_data_type
    experiment_name = config_module.experiment_name
    labels_file = paths["labels_file"]
    data_dir = paths["raw_images_dir"]
    mask_file = paths["mask_file"]

    print("Creating precomputed linear kernels for experiment: ",
          experiment_name)

    save_dir = "./results/" + experiment_name + "/SVM/precomputed_kernel/"
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    kernel_file = "precomputed_kernel.npz"

    print("Reading labels from %s" % labels_file)
    labels = np.genfromtxt(labels_file, delimiter=',', dtype='int8')
    print("   # of labels samples: %d " % len(labels))

    if mask_file is not "":
        mask = nib.load(mask_file)
        mask = mask.get_data()
        mask = np.asarray(mask, dtype='float32')
        mask = np.nan_to_num(mask)

    print("Reading images with format {} from: %s".format(
        input_data_type, data_dir))
    img_paths = glob.glob(data_dir + "/*" + input_data_type)
    img_paths = sort_nicely(img_paths)

    img_names = []
    for img_name in img_paths:
        img_names.append(os.path.splitext(os.path.basename(img_name))[0])

    n_samples = len(labels)
    if n_samples != len(img_paths):
        raise ValueError('Different number of labels and images files')

    print("Loading images")
    print("   # of images samples: %d " % len(img_paths))

    n_samples = len(img_paths)

    print(n_samples)

    K = np.float64(np.zeros((n_samples, n_samples)))
    step_size = 30

    # outer loop
    for i in range(int(np.ceil(n_samples / np.float(step_size)))):

        it = i + 1
        max_it = int(np.ceil(n_samples / np.float(step_size)))
        print(" outer loop iteration: %d of %d." % (it, max_it))

        # generate indices and then paths for this block
        start_ind_1 = i * step_size
        stop_ind_1 = min(start_ind_1 + step_size, n_samples)
        block_paths_1 = img_paths[start_ind_1:stop_ind_1]

        # read in the images in this block
        images_1 = []
        for k, path in enumerate(block_paths_1):
            img = nib.load(path)
            img = img.get_data()
            img = np.asarray(img, dtype='float64')
            img = np.nan_to_num(img)
            if mask_file is not "":
                img_vec = img[mask > 0]
            else:
                img_vec = np.reshape(img, np.product(img.shape))
            images_1.append(img_vec)
            del img
        images_1 = np.array(images_1)
        for j in range(i + 1):

            it = j + 1
            max_it = i + 1

            print(" inner loop iteration: %d of %d." % (it, max_it))

            # if i = j, then sets of image data are the same - no need to load
            if i == j:

                start_ind_2 = start_ind_1
                stop_ind_2 = stop_ind_1
                images_2 = images_1

            # if i !=j, read in a different block of images
            else:
                start_ind_2 = j * step_size
                stop_ind_2 = min(start_ind_2 + step_size, n_samples)
                block_paths_2 = img_paths[start_ind_2:stop_ind_2]

                images_2 = []
                for k, path in enumerate(block_paths_2):
                    img = nib.load(path)
                    img = img.get_data()
                    img = np.asarray(img, dtype='float64')
                    img = np.nan_to_num(img)
                    if mask_file is not "":
                        img_vec = img[mask > 0]
                    else:
                        img_vec = np.reshape(img, np.product(img.shape))
                    images_2.append(img_vec)
                    del img
                images_2 = np.array(images_2)

            block_K = np.dot(images_1, np.transpose(images_2))
            K[start_ind_1:stop_ind_1, start_ind_2:stop_ind_2] = block_K
            K[start_ind_2:stop_ind_2,
              start_ind_1:stop_ind_1] = np.transpose(block_K)

    print("")
    print("Saving Dataset")
    print("   Kernel+Labels:" + kernel_file)
    np.savez(save_dir + kernel_file, kernel=K, labels=labels, names=img_names)
    print("Done")
Exemple #4
0
def create_npy(config_module):
    paths = config_module.path_files
    input_data_type = config_module.input_data_type
    experiment_name = config_module.experiment_name
    labels_file = paths["labels_file"]
    data_dir = paths["raw_images_dir"]
    mask_file = paths["mask_file"]

    print("Saving 3D images using .npz format for experiment: ",
          experiment_name)

    save_dir = "./results/" + experiment_name + "/CNN/img_npz/"
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    print("Reading labels from %s" % labels_file)
    labels = np.genfromtxt(labels_file, delimiter=',', dtype='int8')
    print("   # of labels samples: %d " % len(labels))

    print("Reading images with format {} from: %s".format(
        input_data_type, data_dir))
    img_paths = glob.glob(data_dir + "/*" + input_data_type)
    img_paths = sort_nicely(img_paths)

    n_samples = len(labels)
    if n_samples != len(img_paths):
        raise ValueError('Different number of labels and images files')

    # IF YOU WANT TO REMOVE BLANK VOXELS, UNCOMMENT THIS SECTION
    print(
        "Calculating 3d dimensions to remove blank voxels and reduce the 3d volume."
    )

    if mask_file:
        mask = nib.load(mask_file)
        mask = mask.get_data()
        mask = np.asarray(mask, dtype='float32')
        mask = np.nan_to_num(mask)

        max_x, max_y, max_z, min_x, min_y, min_z = find_image_boundary(
            mask_file)

        print(max_x, max_y, max_z)
        print(min_x, min_y, min_z)

    else:
        min_x = 1000
        max_x = 0
        min_y = 1000
        max_y = 0
        min_z = 1000
        max_z = 0

        for path in img_paths:
            max_x_img, max_y_img, max_z_img, min_x_img, min_y_img, min_z_img = find_image_boundary(
                path)

            # X axis
            if min_x > min_x_img:
                min_x = min_x_img

            if max_x < max_x_img:
                max_x = max_x_img

            # Y axis
            if min_y > min_y_img:
                min_y = min_y_img

            if max_y < max_y_img:
                max_y = max_y_img

            # Z axis
            if min_z > min_z_img:
                min_z = min_z_img

            if max_z < max_z_img:
                max_z = max_z_img

        print(max_x, max_y, max_z)
        print(min_z, min_y, min_x)

    print("Loading images")
    print("   # of images samples: %d " % len(img_paths))
    print("")
    print("{:<5}  {:100s} {:15s}\tCLASS\tMIN    - MAX VALUES".format(
        '#', 'FILENAME', 'DIMENSIONS'))
    for k, path in enumerate(img_paths):
        label = labels[k]
        img = nib.load(path)
        img = img.get_data()
        img = np.asarray(img, dtype='float32')
        img = np.nan_to_num(img)
        if mask_file:
            img = np.multiply(img, mask)
        img = img[min_x:max_x, min_y:max_y, min_z:max_z]
        print("{:<5}  {:100s} ({:3}, {:3}, {:3})\t{:}\t{:6.4} - {:6.4}".format(
            (k + 1), os.path.basename(os.path.normpath(path)), img.shape[0],
            img.shape[1], img.shape[2], labels[k], np.min(img), np.max(img)))
        img = np.true_divide(img, np.max(img))
        img = np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2]))
        f_name = os.path.splitext(os.path.basename(path))[0]
        np.savez(save_dir + f_name, image=img, label=label)
        del img
    print("Done")