Esempio n. 1
0
    def predict_density_map(self,
                            path_to_spheroid,
                            patch_sizes,
                            strides,
                            border=None,
                            padding='VALID',
                            session=None):

        # Load the data
        spheroid, spheroid_header = nrrd.read(path_to_spheroid)
        spheroid = spheroid.astype(np.float32)

        # Generate image patches
        if session == None:
            session = tf.Session()
        patches_X = impro.gen_patches(session=session,
                                      data=spheroid,
                                      patch_slices=patch_sizes[0],
                                      patch_rows=patch_sizes[1],
                                      patch_cols=patch_sizes[2],
                                      stride_slices=strides[0],
                                      stride_rows=strides[1],
                                      stride_cols=strides[2],
                                      input_dim_order='XYZ',
                                      padding=padding)

        # Make a prediction for each patch and predict the density-patches
        patches_y = np.zeros_like(patches_X, dtype=np.float32)

        # Predict the density-patches
        for zslice in range(patches_X.shape[0]):
            for row in range(patches_X.shape[1]):
                for col in range(patches_X.shape[2]):
                    X = patches_X[zslice, row, col, :]
                    y = self.predict_sample(X)
                    patches_y[zslice, row, col, :] = y

        # Make a 3D image out of the patches
        spheroid_new = impro.restore_volume(patches=patches_X,
                                            border=border,
                                            output_dim_order='XYZ')
        density_map = impro.restore_volume(patches=patches_y,
                                           border=border,
                                           output_dim_order='XYZ')

        # Get the number of cells
        num_of_cells = np.sum(density_map)

        if session == None:
            session.close()
            tf.reset_default_graph()

        return spheroid_new, density_map, num_of_cells
data, header = nrrd.read(path_to_data)
#data = np.transpose(data, axes=(1,0,2))
plt.imshow(data[:,:,20])

# Specify patch size
patch_slices = 4
patch_rows = 16
patch_cols = 16

# Specify strides
stride_slices = 2
stride_rows = 4
stride_cols = 4
patches = impro.gen_patches(data=data, patch_slices=patch_slices, 
                            patch_rows=patch_rows, patch_cols=patch_cols, 
                            stride_slices=stride_slices, 
                            stride_rows=stride_rows, stride_cols=stride_cols, 
                            input_dim_order='XYZ', padding='SAME')

plt.imshow(patches[2,1,1,3,:,:]) # Patch index=ZYX, Patch-dimension order = ZYX


#reconstructed = np.zeros(shape=(patches.shape[0]*patches.shape[3]/stride_dim0, 
#                                patches.shape[1]*patches.shape[4]/stride_dim1, 
#                                patches.shape[2]*patches.shape[5]/stride_dim2))

# Calculate the size of the reconstructed volume (not so clear how TensorFlow
# calculates the Padding) -> savety distance
safety_distance = 4
if patch_slices / stride_slices == 1:
    out_dim0 = data.shape[0]
Esempio n. 3
0
                            print('Loading the colocalization file: ',
                                  colocalization_file)
                            colocalization, colocalization_header = nrrd.read(
                                colocalization_file)

                            # Generate image patches from the colocalization file and restore the patches to the volume, so it has the same dimension as the predicted segmentation
                            print(
                                'Patching and restoring the colocalization file: ',
                                colocalization_file)
                            session = tf.Session()
                            patches_colocalization = impro.gen_patches(
                                session=session,
                                data=colocalization,
                                patch_slices=patch_sizes[0],
                                patch_rows=patch_sizes[1],
                                patch_cols=patch_sizes[2],
                                stride_slices=strides[0],
                                stride_rows=strides[1],
                                stride_cols=strides[2],
                                input_dim_order='XYZ',
                                padding=padding)
                            colocalization_restored = impro.restore_volume(
                                patches=patches_colocalization,
                                border=cut_border,
                                output_dim_order='XYZ')

                            # Predict the segmentation of the spheroid
                            print('Predicting the segmentation for: ',
                                  spheroid_file)
                            spheroid_new, segmentation, segmentation_thresholded = cnn.predict_segmentation(
                                path_to_spheroid=spheroid_file,
# Generate the patches
###############################################################################
patch_slices = patch_rows = patch_cols = 32
stride_slices = stride_rows = stride_cols = 16

# WORKAROUND because tensorflow moves the X and y volumes relative to each 
# other if X are int values and Y are float values -> Normalize and Scale the 
# target data with datatype int
Y_s, max_val, min_val = impro.normalize_data(Y_s)
Y_s = impro.scale_data(Y_s, 65535)

# Extract the image patches
session = tf.Session()
p_X = impro.gen_patches(session=session, data=X_s, patch_slices=patch_slices, 
                   patch_rows=patch_rows, patch_cols=patch_cols, 
                   stride_slices=stride_slices, stride_rows=stride_rows, 
                   stride_cols=stride_cols, input_dim_order='XYZ', 
                   padding='SAME')
p_Y = impro.gen_patches(session=session, data=Y_s, patch_slices=patch_slices, 
                   patch_rows=patch_rows, patch_cols=patch_cols, 
                   stride_slices=stride_slices, stride_rows=stride_rows, 
                   stride_cols=stride_cols, input_dim_order='XYZ', 
                   padding='SAME')

# WORKAROUND undo the normalization and scaling
p_Y = impro.unscale_data(p_Y, 65535)
p_Y = impro.unnormalize_data(p_Y, max_val, min_val)

# Plot a slice of an example patch
plt.imshow(p_X[4,5,6,8,:,:])
plt.imshow(p_Y[4,5,6,8,:,:])
Esempio n. 5
0
import_path = os.path.join(os.getcwd(), 'model_export', '2019-07-19_18-50-39')
cnn.load_model_json(import_path, 'model_json', 'model_weights')

# Generate image patches
size_z = patch_slices = 32
size_y = patch_rows = 32
size_x = patch_cols = 32
stride_z = stride_slices = 16
stride_y = stride_rows = 16
stride_x = stride_cols = 16
session = tf.Session()
patches = impro.gen_patches(session=session,
                            data=data,
                            patch_slices=size_z,
                            patch_rows=size_y,
                            patch_cols=size_x,
                            stride_slices=stride_z,
                            stride_rows=stride_y,
                            stride_cols=stride_x,
                            input_dim_order='XYZ',
                            padding='VALID')

#p = patches[2,15,6,15,:,:]
#plt.imshow(p)
#predictions = np.zeros_like(patches, dtype=np.float32)
#predictions = np.zeros((patches.shape[0], patches.shape[1], patches.shape[2], stride_z, stride_y, stride_x), dtype=np.float32)
predictions = np.zeros((patches.shape[0], patches.shape[1], patches.shape[2],
                        size_z, size_y, size_x),
                       dtype=np.float32)

# Predict the density-patches
for zslice in range(patches.shape[0]):
Esempio n. 6
0
                                        y = impro.scale_data(y, 65535)

                                    print(
                                        'Generating image patches for input data...'
                                    )
                                    # Generate the image patches of dimension-order ZYX.
                                    # With the parameter 'input_dim_order='XYZ' the
                                    # input data 'X' is transposed to 'ZYX' before generating
                                    # the patches. So the patches are in dimension-order 'ZYX'
                                    session = tf.Session()
                                    patches_X = impro.gen_patches(
                                        session=session,
                                        data=X,
                                        patch_slices=size_z,
                                        patch_rows=size_y,
                                        patch_cols=size_x,
                                        stride_slices=stride_z,
                                        stride_rows=stride_y,
                                        stride_cols=stride_x,
                                        input_dim_order='XYZ',
                                        padding=padding)  #ZYX

                                    print(
                                        'Generating image patches for target data...'
                                    )
                                    patches_y = impro.gen_patches(
                                        session=session,
                                        data=y,
                                        patch_slices=size_z,
                                        patch_rows=size_y,
                                        patch_cols=size_x,
Esempio n. 7
0
X_path = os.path.join('..', '..', '..', 'Daten2', 'Fibroblasten', '1_draq5.nrrd')
y_path = os.path.join('..', '..', '..', 'Daten2', 'Fibroblasten', '20190221_1547_Segmentation', '1_draq5-gauss_centroids.nrrd')

X, X_header = nrrd.read(X_path)
y, y_header = nrrd.read(y_path)

pz = py = px = 32
sz = sy = sx = 32

padding = 'SAME'


session = tf.Session()
p_X = impro.gen_patches(session=session, data=X, patch_slices=pz, patch_rows=py,
                        patch_cols=px, stride_slices=sz, stride_rows=sy,
                        stride_cols=sx, input_dim_order='XYZ', padding=padding)

p_y = impro.gen_patches(session=session, data=y, patch_slices=pz, patch_rows=py,
                        patch_cols=px, stride_slices=sz, stride_rows=sy,
                        stride_cols=sx, input_dim_order='XYZ', padding=padding)

border = None#(8,8,8)
X_r = impro.restore_volume(patches=p_X, border=border, output_dim_order='XYZ')
y_r = impro.restore_volume(patches=p_y, border=border, output_dim_order='XYZ')

plt.imshow(X_r[150,])
plt.imshow(y_r[150,])

nrrd.write('X_r.nrrd', X_r)
nrrd.write('y_r.nrrd', y_r)
Esempio n. 8
0
    def predict_segmentation(self,
                             path_to_spheroid,
                             patch_sizes,
                             strides,
                             border=None,
                             padding='VALID',
                             threshold=None,
                             label=False,
                             session=None):

        # Load the data
        spheroid, spheroid_header = nrrd.read(path_to_spheroid)
        spheroid = spheroid.astype(np.float32)

        # Generate image patches
        if session == None:
            session = tf.Session()
        patches_X = impro.gen_patches(session=session,
                                      data=spheroid,
                                      patch_slices=patch_sizes[0],
                                      patch_rows=patch_sizes[1],
                                      patch_cols=patch_sizes[2],
                                      stride_slices=strides[0],
                                      stride_rows=strides[1],
                                      stride_cols=strides[2],
                                      input_dim_order='XYZ',
                                      padding=padding)

        # Make a prediction for each patch and predict the density-patches
        patches_y = np.zeros_like(patches_X, dtype=np.float32)

        # Predict the density-patches
        for zslice in range(patches_X.shape[0]):
            for row in range(patches_X.shape[1]):
                for col in range(patches_X.shape[2]):
                    X = patches_X[zslice, row, col, :]
                    y = self.predict_sample(X)
                    patches_y[zslice, row, col, :] = y

        # Make a 3D image out of the patches
        spheroid_new = impro.restore_volume(patches=patches_X,
                                            border=border,
                                            output_dim_order='XYZ')
        segmentation = impro.restore_volume(patches=patches_y,
                                            border=border,
                                            output_dim_order='XYZ')

        if session == None:
            session.close()
            tf.reset_default_graph()

        segmentation_thresholded = None

        if threshold != None:
            if threshold <= 1 and threshold >= 0:
                segmentation_thresholded = self.threshold_filter(
                    segmentation, threshold)
            else:
                print('ERROR: Threshold must be in range 0 to 1.')
                return
            if label == True:
                segmentation_thresholded = np.transpose(
                    segmentation_thresholded, axes=(2, 1, 0))  #ZYX
                segmentation_thresholded = cc3d.connected_components(
                    segmentation_thresholded, connectivity=6)
                segmentation_thresholded = np.transpose(
                    segmentation_thresholded, axes=(2, 1, 0))  #XYZ

        if threshold == None and label == True:
            print(
                'ERROR: Only a binary image can be labelled, so threshold it first.'
            )
            return

        return spheroid_new, segmentation, segmentation_thresholded