print(' Directory {0} does not contain an image with file ' 'name {1}. Skip.'.format(data_dir, os.path.basename(image_name))) continue # Read the image print(' Reading {} ...'.format(image_name)) nim = nib.load(image_name) image = nim.get_data() X, Y, Z, T = image.shape orig_image = image print(' Segmenting full sequence ...') start_seg_time = time.time() # Intensity rescaling image = rescale_intensity(image, (1, 99)) # Prediction (segmentation) pred = np.zeros(image.shape) # Pad the image size to be a factor of 16 so that the # downsample and upsample procedures in the network will # result in the same image size at each resolution level. X2, Y2 = int(math.ceil(X / 16.0)) * 16, int(math.ceil(Y / 16.0)) * 16 x_pre, y_pre = int((X2 - X) / 2), int((Y2 - Y) / 2) x_post, y_post = (X2 - X) - x_pre, (Y2 - Y) - y_pre image = np.pad(image, ((x_pre, x_post), (y_pre, y_post), (0, 0), (0, 0)), 'constant') # Process each time frame for t in range(T): # Transpose the shape to NXYC
def get_random_batch(filename_list, batch_size, image_size=192, data_augmentation=False, shift=0.0, rotate=0.0, scale=0.0, intensity=0.0, flip=False): # Randomly select batch_size images from filename_list n_file = len(filename_list) print(n_file) n_selected = 0 images = [] labels = [] nims = [] while n_selected < batch_size: rand_index = random.randrange(n_file) image_name, label_name, frame = filename_list[rand_index] if os.path.exists(image_name) and os.path.exists(label_name): print(' Select {0} {1} {2}'.format(image_name, label_name, frame)) # Read image and label nim = nib.load(image_name) image = nim.get_fdata() nil = nib.load(label_name) label = nil.get_fdata() fr = get_frames(label, 'sa') label = label[:, :, :, fr[frame]] image = image[:, :, :, fr[frame]] # Handle exceptions if image.shape != label.shape: print('Error: mismatched size, image.shape = {0}, ' 'label.shape = {1}'.format(image.shape, label.shape)) print('Skip {0}, {1}'.format(image_name, label_name)) continue if image.max() < 1e-6: print('Error: blank image, image.max = {0}'.format( image.max())) print('Skip {0} {1}'.format(image_name, label_name)) continue # Normalise the image size X, Y, Z = image.shape cx, cy = int(X / 2), int(Y / 2) image = crop_image(image, cx, cy, image_size) label = crop_image(label, cx, cy, image_size) # Intensity rescaling image = rescale_intensity(image, (1.0, 99.0)) print(image.shape) # Append the image slices to the batch # Use list for appending, which is much faster than numpy array for z in range(Z): images += [image[:, :, z]] labels += [label[:, :, z]] nims.append(nim) # Increase the counter n_selected += 1 # Convert to a numpy array images = np.array(images, dtype=np.float32) labels = np.array(labels, dtype=np.int32) print(images.shape) # Add the channel dimension # tensorflow by default assumes NHWC format images = np.expand_dims(images, axis=3) # Perform data augmentation if data_augmentation: images, labels = data_augmenter(images, labels, shift=shift, rotate=rotate, scale=scale, intensity=intensity, flip=flip) print(images.shape) return images, labels, nims