This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def resample_img(itk_image, out_spacing=[2.0, 2.0, 2.0], is_label=False): | |
# Resample images to 2mm spacing with SimpleITK | |
original_spacing = itk_image.GetSpacing() | |
original_size = itk_image.GetSize() | |
out_size = [ | |
int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))), | |
int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))), | |
int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2])))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generator function | |
def f(): | |
fn = read_fn(file_references=all_filenames, | |
mode=tf.estimator.ModeKeys.TRAIN, | |
params=reader_params) | |
ex = next(fn) | |
# Yield the next image | |
yield ex | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_fn(file_references, mode, params=None): | |
# We define a `read_fn` and iterate through the `file_references`, which | |
# can contain information about the data to be read (e.g. a file path): | |
for meta_data in file_references: | |
# Here, we parse the `subject_id` to construct a file path to read | |
# an image from. | |
subject_id = meta_data[0] | |
data_path = '../../data/IXI_HH/1mm' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decode(serialized_example): | |
# Decode examples stored in TFRecord | |
# NOTE: make sure to specify the correct dimensions for the images | |
features = tf.parse_single_example( | |
serialized_example, | |
features={'train/image': tf.FixedLenFeature([128, 224, 224, 1], tf.float32), | |
'train/label': tf.FixedLenFeature([], tf.int64)}) | |
# NOTE: No need to cast these features, as they are already `tf.float32` values. | |
return features['train/image'], features['train/label'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _int64_feature(value): | |
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) | |
def _float_feature(value): | |
return tf.train.Feature(float_list=tf.train.FloatList(value=value)) | |
# path to save the TFRecords file | |
train_filename = 'train.tfrecords' | |
# open the file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleITK as sitk | |
import numpy as np | |
# A path to a T1-weighted brain .nii image: | |
t1_fn = './brain_t1_0001.nii' | |
# Read the .nii image containing the volume with SimpleITK: | |
sitk_t1 = sitk.ReadImage(t1_fn) | |
# and access the numpy array: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load all data into memory | |
data = load_data(all_filenames, tf.estimator.ModeKeys.TRAIN, reader_params) | |
# Create placeholder variables and define their shapes (here, | |
# we input a volume image of size [128, 224, 244] and a single | |
# channel (i.e. greyscale): | |
x = tf.placeholder(reader_example_dtypes['features']['x'], | |
[None, 128, 224, 224, 1]) | |
y = tf.placeholder(reader_example_dtypes['labels']['y'], | |
[None, 1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(): | |
print('bar') |