def test_get_image_sizes(self): temp_dir = self.get_temp_dir() _write_image(os.path.join(temp_dir, 'image1.png'), 300, 300) _write_image(os.path.join(temp_dir, 'image2.png'), 300, 300) # test with single channel name size = io_utils.get_image_sizes(temp_dir, ['image1']) self.assertEqual(size, (300, 300)) # test with multiple channel names sizes = io_utils.get_image_sizes(temp_dir, ['image1', 'image2']) self.assertEqual(sizes, (300, 300))
def make_training_data_3d(direc_name, file_name_save, channel_names, training_direcs=None, annotation_name='corrected', raw_image_direc='raw', annotation_direc='annotated', reshape_size=None, num_frames=50, montage_mode=True): """ Read all images in training directories and save as npz file 3D image sets are "stacks" of images. For annotation purposes, these images have been sliced into "montages", where a section of each stack has been sliced for efficient annotated by humans. The raw_image_direc should be a specific montage (e.g. montage_0_0) and the annotation is the corresponding annotated montage. # Arguments direc_name: directory containing folders of training data file_name_save: full filepath for npz file where the data will be saved training_direcs: directories of images located inside direc_name If None, all directories in direc_name are used. channel_names: Loads all raw images with a channel_name in the filename raw_image_direc: directory name inside each training dir with raw images annotation_direc: directory name inside each training dir with masks reshape_size: If provided, will reshape the images to the given size. num_frames: number of frames to load from each training directory montage_mode: load masks from "montaged" subdirs inside annotation_direc """ # Load one file to get image sizes rand_train_dir = os.path.join(direc_name, random.choice(training_direcs), raw_image_direc) if montage_mode: rand_train_dir = os.path.join(rand_train_dir, random.choice(os.listdir(rand_train_dir))) image_size = get_image_sizes(rand_train_dir, channel_names) X = load_training_images_3d(direc_name, training_direcs, raw_image_direc=raw_image_direc, channel_names=channel_names, image_size=image_size, num_frames=num_frames, montage_mode=montage_mode) y = load_annotated_images_3d(direc_name, training_direcs, annotation_direc=annotation_direc, annotation_name=annotation_name, image_size=image_size, num_frames=num_frames, montage_mode=montage_mode) # Reshape X and y if reshape_size is not None: X, y = reshape_movie(X, y, reshape_size=reshape_size) np.savez(file_name_save, X=X, y=y) return None
def make_training_data_2d(direc_name, file_name_save, channel_names, raw_image_direc='raw', annotation_direc='annotated', annotation_name='feature', training_direcs=None, reshape_size=None): """Read all images in training directories and save as npz file. Args: direc_name (str): directory containing folders of training data file_name_save (str): full filepath for npz file where the data will be saved training_direcs (str[]): directories of images located inside direc_name. If None, all directories in direc_name are used. raw_image_direc (str): directory name inside each training dir with raw images annotation_direc (str): directory name inside each training dir with masks channel_names (str[]): Loads all raw images with a channel_name in the filename annotation_name (str): Loads all masks with annotation_name in the filename reshape_size (int): If provided, reshapes the images to the given size """ # Load one file to get image sizes (assumes all images same size) image_path = os.path.join(direc_name, random.choice(training_direcs), raw_image_direc) image_size = get_image_sizes(image_path, channel_names) X = load_training_images_2d(direc_name, training_direcs, raw_image_direc=raw_image_direc, channel_names=channel_names, image_size=image_size) y = load_annotated_images_2d(direc_name, training_direcs, annotation_direc=annotation_direc, annotation_name=annotation_name, image_size=image_size) if reshape_size is not None: X, y = reshape_matrix(X, y, reshape_size=reshape_size) # Save training data in npz format np.savez(file_name_save, X=X, y=y)
def make_training_data_3d(direc_name, file_name_save, channel_names, training_direcs=None, annotation_name='corrected', raw_image_direc='raw', annotation_direc='annotated', reshape_size=None, num_frames=None, montage_mode=True): """Read all images in training directories and save as npz file. 3D image sets are "stacks" of images. For annotation purposes, these images have been sliced into "montages", where a section of each stack has been sliced for efficient annotated by humans. The raw_image_direc should be a specific montage (e.g. montage_0_0) and the annotation is the corresponding annotated montage. Args: direc_name (str): directory containing folders of training data file_name_save (str): full filepath for npz file where the data will be saved training_direcs (str[]): directories of images located inside direc_name. If None, all directories in direc_name are used. annotation_name (str): Loads all masks with annotation_name in the filename channel_names (str[]): Loads all raw images with a channel_name in the filename raw_image_direc (str): directory name inside each training dir with raw images annotation_direc (str): directory name inside each training dir with masks reshape_size (int): If provided, reshapes the images to the given size. num_frames (int): number of frames to load from each training directory montage_mode (bool): load masks from "montaged" subdirs inside annotation_direc """ # Load one file to get image sizes rand_train_dir = os.path.join(direc_name, random.choice(training_direcs), raw_image_direc) if montage_mode: rand_train_dir = os.path.join( rand_train_dir, random.choice(os.listdir(rand_train_dir))) image_size = get_image_sizes(rand_train_dir, channel_names) if num_frames is None: raw = [ os.path.join(direc_name, t, raw_image_direc) for t in training_direcs ] ann = [ os.path.join(direc_name, t, annotation_direc) for t in training_direcs ] if montage_mode: raw = [os.path.join(r, d) for r in raw for d in os.listdir(r)] ann = [os.path.join(a, d) for a in ann for d in os.listdir(a)] # use all images if not set # will select the first N images where N is the smallest # number of images in each subdir of all training_direcs num_frames_raw = min([count_image_files(f) for f in raw]) num_frames_ann = min([count_image_files(f) for f in ann]) num_frames = min(num_frames_raw, num_frames_ann) X = load_training_images_3d(direc_name, training_direcs, raw_image_direc=raw_image_direc, channel_names=channel_names, image_size=image_size, num_frames=num_frames, montage_mode=montage_mode) y = load_annotated_images_3d(direc_name, training_direcs, annotation_direc=annotation_direc, annotation_name=annotation_name, image_size=image_size, num_frames=num_frames, montage_mode=montage_mode) # Reshape X and y if reshape_size is not None: X, y = reshape_movie(X, y, reshape_size=reshape_size) np.savez(file_name_save, X=X, y=y)