예제 #1
0
 def __call__(self, sess, tup, save_dir, model):
     print(tup)
     img = nib.load(tup[0])
     img_data = img.get_data()
     vol_shape = img_data.shape
     data_loader = StandardDataLoader(self.stride, self.segment_size_in)
     vs, vsegs, vmasks = data_loader.vol_s(tup[0:3], crop_by=self.crop_by)
     print(self.stride)
     vs, vsegs = zip(*[(v1, v2) for v1, v2, vm in zip(vs, vsegs, vmasks) if np.any(vm.seg_arr)])
     yr_labels, yr = self.evaluate_case(sess, model, self.batch_size, vs, vsegs)
     vpreds = [VolumeSegment(start_voxel=vol.start_voxel, seg_arr=seg_arr)
               for vol, seg_arr in zip(vsegs, yr) if np.all(vol.start_voxel - vol_shape < 0)]
     for v_pred in  vpreds: v_pred.compute_indices(self.segment_size_out, vol_shape)
     img_handler = ImageHandler()
     pre_arr = img_handler.create_image_from_windows(vpreds, vol_shape)
     img_nii = nib.Nifti1Image(pre_arr.astype(np.float32), affine=img.affine)
     out_name = os.path.join(save_dir, 'pred_' + os.path.basename(tup[0]).split('.')[0] + '.nii.gz')
     nib.nifti1.save(img_nii, out_name)
     if len(tup) == 3:
         seg_img = nib.load(tup[2]).get_data().astype(int)
         seg_img_one_hot = np.eye(self.nlabels,dtype=int)[seg_img.reshape(-1)]
         pred_img_one_hot = np.eye(self.nlabels,dtype=int)[pre_arr.astype(np.uint8).reshape(-1)]
         dice_arr = 2*np.sum(seg_img_one_hot*pred_img_one_hot, axis=0)/(
             np.sum(seg_img_one_hot, axis=0) + np.sum(pred_img_one_hot, axis=0))
         dice = np.sum(dice_arr)
         fpr = np.sum((1-seg_img_one_hot)*pred_img_one_hot)/np.sum(1-seg_img_one_hot)
         tpr = np.sum(seg_img_one_hot*pred_img_one_hot)/np.sum(seg_img_one_hot)
         print("TPR:", tpr, " FPR: ", fpr, " DSC: ", dice_arr)
         return tuple(dice_arr)
     else:
         return -1
예제 #2
0
    def test_two_pathway(self):
        """Test the TwoPathway dataloader and see if reconstituting the volume
        segments gives the same as a block reduction of the imagei"""
        tup = (self.img_file_path, self.mask_file_path, self.seg_file_path)
        img_handler = ImageHandler()

        stride = np.array([9, 9, 9])
        segment_size = np.array([25, 25, 25])
        segment_size_ss = np.array([19, 19, 19])
        dl = TwoPathwayDataLoader(stride, segment_size, segment_size_ss)
        _, _, vl_ss = dl.vol_s(tup)
        img_recons_arr = img_handler.create_image_from_windows(
            vl_ss,
            np.array(self.image_arr.shape) // 3 + 1)

        #img_nii = nib.Nifti1Image(img_recons_arr, affine=np.eye(4))
        #nib.nifti1.save(img_nii,'blockreduced_recons_test.nii.gz')

        img_reduced_arr = block_reduce(self.image_arr,
                                       block_size=(3, 3, 3),
                                       func=np.median).astype(np.uint8)
        #img_nii = nib.Nifti1Image(img_reduced_arr, affine=np.eye(4))
        #nib.nifti1.save(img_nii,'block_reduced_test.nii.gz')
        img_diff_arr = (img_reduced_arr - img_recons_arr)
        self.assertFalse(np.any(img_diff_arr))
예제 #3
0
    def __call__(self, sess, tup, save_dir, model):
        print(tup)
        img = nib.load(tup[0])
        img_data = img.get_data()
        vol_shape = np.array(img_data.shape)
        print(self.stride)
        data_loader = StandardDataLoader(self.stride, self.segment_size_in)
        vs, vsegs = data_loader.vol_s(tup, crop_by=self.crop_by)
        yr = self.evaluate_case(sess, model, self.batch_size, vs)

        for feat_no in range(0, self.no_feats):
            vpreds = [
                VolumeSegment(start_voxel=vol.start_voxel, seg_arr=seg_arr)
                for vol, seg_arr in zip(vsegs, yr)
                if np.all(vol.start_voxel - vol_shape < 0)
            ]
            for v_pred in vpreds:
                v_pred.compute_indices(self.segment_size_out, vol_shape)
            img_handler = ImageHandler()
            pre_arr = img_handler.create_image_from_windows(vpreds, vol_shape)

            #img_nii = nib.Nifti1Image(pre_arr.astype(np.float32), affine=img.affine)
            #out_name = os.path.join(save_dir,'prob_' + os.path.basename(tup[0]).split('.')[0] + '.nii.gz')
            #nib.nifti1.save(img_nii,out_name)

            #seg_img = nib.load(tup[2]).get_data()
            #pre_arr = pre_arr > 0.5

            img_nii = nib.Nifti1Image(pre_arr.astype(np.float32),
                                      affine=img.affine)
            out_name = os.path.join(
                save_dir, 'feat_' + str(feat_no) + '_' +
                os.path.basename(tup[0]).split('.')[0] + '.nii.gz')
            nib.nifti1.save(img_nii, out_name)
예제 #4
0
 def test_image_handler(self):
     """Check decomposing image to VoluemSegments and back again produces the same image"""
     img_handler = ImageHandler()
     stride = np.array([10] * 3)
     window_shape = np.array([50] * 3)
     vol_list = img_handler.image_to_vols(self.image_arr, stride,
                                          window_shape)
     img_recons_arr = img_handler.create_image_from_windows(
         vol_list, self.image_arr.shape)
     img_diff_arr = self.mask_arr * (self.image_arr - img_recons_arr)
     #img_nii = nib.Nifti1Image(img_recons_arr, affine=np.eye(4))
     #nib.nifti1.save(img_nii,'pred_test.nii.gz') #For testing
     self.assertFalse(np.any(img_diff_arr))
예제 #5
0
 def vol_s(self, tup, crop_by=0):
     img_file_path, mask_file_path, seg_file_path = tup
     img_handler = ImageHandler()
     image_arr = nib.load(img_file_path).get_data()
     mask_arr = nib.load(mask_file_path).get_data()
     seg_arr = nib.load(seg_file_path).get_data().astype(np.uint8)
     image_arr = mask_arr*image_arr + (1-mask_arr)*(image_arr.max())
     vol_list = img_handler.image_to_vols(image_arr, self.stride, self.segment_size,
                                          crop_by=crop_by, rnd_offset=self.rnd_offset,
                                          mask_arr=mask_arr)
     tuples = [(np.array(vol.seg_arr.shape)-2*crop_by, vol.start_voxel+crop_by)
               for vol in vol_list]
     vol_list_segs = img_handler.image_vols_to_vols(seg_arr, tuples)
     return exclude_windows_outside_mask(mask_arr, vol_list, vol_list_segs)
예제 #6
0
    def __call__(self, sess, tup, save_dir, model):
        #batch_size = 400
        #stride = np.array([7,7,7])
        #crop_by = 8
        print(tup)
        img = nib.load(tup[0])
        img_data = img.get_data()
        vol_shape = img_data.shape
        data_loader = TwoPathwayDataLoader(self.stride, self.segment_size_in, self.segment_size_in_ss, self.crop_by)
        vs, vsegs, vs_ss = data_loader.vol_s(tup, crop_by=self.crop_by)
        tup_seg_size_in = tuple(self.segment_size_in.tolist() + [1])
        tup_seg_size_in_ss = tuple(self.segment_size_in_ss.tolist() + [1])
        print(tup_seg_size_in_ss, 'here')
        full_batch = [v.seg_arr.reshape(tup_seg_size_in) for v in vs]
        full_batch_ss = [v.seg_arr.reshape(tup_seg_size_in_ss) for v in vs_ss]
        num_batches = (len(vs)//self.batch_size)
        if len(vs) % self.batch_size: num_batches += 1
        y_out_list = []
        for i in range(0, num_batches):
            batch = full_batch[i*self.batch_size:min(len(vs), (i+1)*self.batch_size)]
            batch_ss = full_batch_ss[i*self.batch_size:min(len(vs), (i+1)*self.batch_size)]
            y_out = sess.run(model.pred, feed_dict={model.X:batch, model.X_ss:batch_ss})
            y_out_list.append(y_out)
            print("Segmenting ", i+1, " of ", num_batches, y_out.shape)
        tup_out = tuple([len(vs)] + self.segment_size_out.tolist())
        yr = np.vstack(y_out_list).reshape([len(vs), 9, 9, 9])
        yr_labels = np.vstack([v.seg_arr for v in vsegs]).reshape([len(vs), 9, 9, 9])
        dice_segments = 2*np.sum(yr*yr_labels)/(np.sum(yr)+np.sum(yr_labels))
        print(dice_segments)
        vpreds = [VolumeSegment(start_voxel=vol.start_voxel, seg_arr=seg_arr)
                  for vol, seg_arr in zip(vsegs, yr) if np.all(vol.start_voxel - vol_shape < 0)]
        for v_pred in  vpreds: v_pred.compute_indices(self.segment_size_out, vol_shape)
        img_handler = ImageHandler()
        pre_arr = img_handler.create_image_from_windows(vpreds, vol_shape)
        mask_arr = nib.load(tup[1]).get_data()
        pre_arr = pre_arr*mask_arr

        img_nii = nib.Nifti1Image(pre_arr, affine=img.affine)
        out_name = os.path.join(save_dir, 'pred_' + os.path.basename(tup[0]).split('.')[0] + '.nii.gz')
        nib.nifti1.save(img_nii, out_name)

        seg_img = nib.load(tup[2]).get_data()
        pre_arr = pre_arr > 0.5
        dice = 2*np.sum(seg_img*pre_arr)/(np.sum(pre_arr)+np.sum(seg_img))
        return dice
예제 #7
0
 def vol_s(self, tup, crop_by=0):
     img_file_path, mask_file_path, seg_file_path = tup
     img_handler = ImageHandler()
     image_arr = nib.load(img_file_path).get_data()
     mask_arr = nib.load(mask_file_path).get_data()
     block_image_arr = block_reduce(image_arr, block_size=(3, 3, 3),
                                    func=np.median).astype(np.uint8)
     vol_list = img_handler.image_to_vols(image_arr, self.stride,
                                          self.segment_size, crop_by=crop_by)
     tuples = [(np.array(vol.seg_arr.shape) - 2*crop_by, vol.start_voxel +
                crop_by) for vol in vol_list]
     seg_arr = nib.load(seg_file_path).get_data().astype(np.uint8)
     vol_list_segs = img_handler.image_vols_to_vols(seg_arr, tuples)
     tuples = [(self.segment_size_ss,
                ((vol.start_voxel +
                  (self.segment_size-self.ss_factor*self.segment_size_ss)//2)//self.ss_factor))
               for vol in vol_list]
     vol_list_subsampled = img_handler.image_vols_to_vols(block_image_arr, tuples)
     return exclude_windows_outside_mask(mask_arr, vol_list, vol_list_segs, vol_list_subsampled)
예제 #8
0
    def test_image_handler_seg_vs(self):
        """Check decomposing segmentation and reconstituting produces the same image"""
        stride = np.array([10] * 3)
        window_shape = np.array([50] * 3)
        img_handler = ImageHandler()
        vol_list = img_handler.image_to_vols(self.image_arr, stride,
                                             window_shape)
        tuples = [(vol.seg_arr.shape, vol.start_voxel) for vol in vol_list]
        vol_list_segs = img_handler.image_vols_to_vols(self.seg_arr, tuples)
        seg_recons_arr = img_handler.create_image_from_windows(
            vol_list_segs, self.image_arr.shape)
        self.assertFalse(
            np.any(self.mask_arr * (self.seg_arr - seg_recons_arr)))

        vol1 = vol_list_segs[0]
        vol2 = VolumeSegment(vol1.start_voxel)
        vol2.read_array(self.seg_arr, vol1.seg_arr.shape)
        self.assertTrue(vol1 == vol2)

        tuples = [(vol.seg_arr.shape, vol.start_voxel)
                  for vol in vol_list_segs]
        vol_list_segs2 = img_handler.image_vols_to_vols(self.seg_arr, tuples)
        for v1, v2 in zip(vol_list_segs, vol_list_segs2):
            self.assertTrue(v1 == v2)