def create_image_mb_source(map_file, mean_file, train, total_number_of_samples): if not os.path.exists(map_file) or not os.path.exists(mean_file): raise RuntimeError("File '%s' or '%s' does not exist." % (map_file, mean_file)) # transformation pipeline for the features has jitter/crop only when training transforms = [] if train: transforms += [ xforms.crop(crop_type='randomarea', area_ratio=(0.08, 1.0), aspect_ratio=(0.75, 1.3333), jitter_type='uniratio'), # train uses jitter xforms.color(brightness_radius=0.4, contrast_radius=0.4, saturation_radius=0.4) ] else: transforms += [ C.io.transforms.crop(crop_type='center', side_ratio=0.875) # test has no jitter ] transforms += [ xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='cubic'), xforms.mean(mean_file) ] # deserializer return C.io.MinibatchSource( C.io.ImageDeserializer(map_file, C.io.StreamDefs( features=C.io.StreamDef(field='image', transforms=transforms), # 1st col in mapfile referred to as 'image' labels=C.io.StreamDef(field='label', shape=num_classes))), # and second as 'label' randomize=train, max_samples=total_number_of_samples, multithreaded_deserializer=True)
def create_reader(map_file, train, dimensions, classes, total_number_of_samples): print( f"Reading map file: {map_file} with number of samples {total_number_of_samples}" ) # transformation pipeline for the features has jitter/crop only when training transforms = [] # finalize_network uses data augmentation (translation only) if train: transforms += [ xforms.crop(crop_type='randomside', area_ratio=(0.08, 1.0), aspect_ratio=(0.75, 1.3333), jitter_type='uniratio'), xforms.color(brightness_radius=0.4, contrast_radius=0.4, saturation_radius=0.4) ] transforms += [ xforms.scale(width=dimensions['width'], height=dimensions['height'], channels=dimensions['depth'], interpolations='linear') ] source = MinibatchSource(ImageDeserializer( map_file, StreamDefs(features=StreamDef(field='image', transforms=transforms), labels=StreamDef(field='label', shape=len(classes)))), randomize=train, max_samples=total_number_of_samples, multithreaded_deserializer=True) return source
def create_image_mb_source(map_file, train, total_number_of_samples): print('Creating source for {}.'.format(map_file)) transforms = [] if train: transforms += [ xforms.crop(crop_type='randomarea', area_ratio=(0.08, 1.0), aspect_ratio=(0.75, 1), jitter_type='uniratio'), # train uses jitter xforms.color(brightness_radius=0.4, contrast_radius=0.4, saturation_radius=0.4) ] transforms += [ xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='cubic') ] # deserializer return C.io.MinibatchSource( C.io.ImageDeserializer( map_file, C.io.StreamDefs( features=C.io.StreamDef( field='image', transforms=transforms ), # 1st col in mapfile referred to as 'image' labels=C.io.StreamDef( field='label', shape=num_classes))), # and second as 'label' randomize=train, max_samples=total_number_of_samples, multithreaded_deserializer=True)
def create_image_mb_source(map_file, train, total_number_of_samples): ''' Input: map_file, train:bool, total_num_of_samples Function: - checks if it is training or testing phase - for training: It applies Image Augmentation techniques like cropping, width_shift, height_shift, horizontal_flip, color_contrast to prevent overfitting of the model. Return: MinibatchSource to be fed into the CNTK Model ''' print('Creating source for {}.'.format(map_file)) transforms = [] if train: # Apply translational and color transformations only for the Image set transforms += [ xforms.crop(crop_type='randomarea', area_ratio=(0.08, 1.0), aspect_ratio=(0.75, 1), jitter_type='uniratio'), # train uses jitter xforms.color(brightness_radius=0.4, contrast_radius=0.4, saturation_radius=0.4) ] # Scale the images to a specified size (224 x 224) as expected by the model transforms += [ xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='cubic') ] return C.io.MinibatchSource( C.io.ImageDeserializer( map_file, C.io.StreamDefs(features=C.io.StreamDef(field='image', transforms=transforms), # 1st col in mapfile referred to as 'image' labels=C.io.StreamDef(field='label', shape=num_classes))), # and second as 'label' randomize=train, max_samples=total_number_of_samples, multithreaded_deserializer=False)
def create_image_mb_source(map_file, mean_file, train, total_number_of_samples): if not os.path.exists(map_file) or not os.path.exists(mean_file): raise RuntimeError("File '%s' or '%s' does not exist." % (map_file, mean_file)) # transformation pipeline for the features has jitter/crop only when training transforms = [] if train: transforms += [ xforms.crop(crop_type='randomarea', area_ratio=(0.08, 1.0), aspect_ratio=(0.75, 1.3333), jitter_type='uniratio'), # train uses jitter xforms.color(brightness_radius=0.4, contrast_radius=0.4, saturation_radius=0.4) ] else: transforms += [ C.io.transforms.crop(crop_type='center', side_ratio=0.875) # test has no jitter ] transforms += [ xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='cubic'), xforms.mean(mean_file) ] # deserializer return C.io.MinibatchSource( C.io.ImageDeserializer(map_file, C.io.StreamDefs( features=C.io.StreamDef(field='image', transforms=transforms), # 1st col in mapfile referred to as 'image' labels=C.io.StreamDef(field='label', shape=num_classes))), # and second as 'label' randomize=train, max_samples=total_number_of_samples, multithreaded_deserializer=True)
def create_mb(map_file, params, training_set): transforms = [] image_dimensions = params['image_dimensions'] num_classes = params['num_classes'] if training_set: # Scale to square-sized image. without this the cropping transform would chop the larger dimension of an # image to make it squared, and then take 0.9 crops from within the squared image. transforms += [ xforms.scale(width=2 * image_dimensions[0], height=2 * image_dimensions[1], channels=image_dimensions[2], scale_mode='pad', pad_value=114) ] transforms += [ xforms.crop(crop_type='randomside', side_ratio=0.9, jitter_type='uniratio') ] # Randomly crop square area #randomside enables Horizontal flipping #new_dim = side_ratio * min(old_w,old_h) , 0.9 * 224 = 201.6 #transforms += [xforms.crop(crop_type='center')] transforms += [ xforms.color(brightness_radius=0.2, contrast_radius=0.2, saturation_radius=0.2) ] transforms += [xforms.crop(crop_type='center', side_ratio=0.875)] # test has no jitter] # Scale down and pad transforms += [ xforms.scale(width=image_dimensions[0], height=image_dimensions[1], channels=image_dimensions[2], scale_mode='pad', pad_value=114) ] return MinibatchSource(ImageDeserializer( map_file, StreamDefs(features=StreamDef(field='image', transforms=transforms), labels=StreamDef(field='label', shape=num_classes))), randomize=training_set, multithreaded_deserializer=True)
def create_minibatch_source(map_filename, num_classes): transforms = [xforms.crop(crop_type='randomside', side_ratio=0.85, jitter_type='uniratio'), xforms.scale(width=224, height=224, channels=3, interpolations='linear'), xforms.color(brightness_radius=0.2, contrast_radius=0.2, saturation_radius=0.2)] return(cntk.io.MinibatchSource(cntk.io.ImageDeserializer( map_filename, cntk.io.StreamDefs( features=cntk.io.StreamDef( field='image', transforms=transforms, is_sparse=False), labels=cntk.io.StreamDef( field='label', shape=num_classes, is_sparse=False)))))
def create_mb_source(map_file, image_width, image_height, num_channels, num_classes, boTrain): transforms = [] if boTrain: # Scale to square-sized image. without this the cropping transform would chop the larger dimension of an # image to make it squared, and then take 0.9 crops from within the squared image. transforms += [xforms.scale(width=2*image_width, height=2*image_height, channels=num_channels, interpolations='linear', scale_mode='pad', pad_value=114)] transforms += [xforms.crop(crop_type='randomside', side_ratio=0.9, jitter_type='uniratio')] # Randomly crop square area transforms += [xforms.scale(width=image_width, height=image_height, channels=num_channels, # Scale down and pad interpolations='linear', scale_mode='pad', pad_value=114)] if boTrain: transforms += [xforms.color(brightness_radius=0.2, contrast_radius=0.2, saturation_radius=0.2)] return MinibatchSource(ImageDeserializer(map_file, StreamDefs( features = StreamDef(field='image', transforms=transforms), labels = StreamDef(field='label', shape=num_classes))), randomize = boTrain, multithreaded_deserializer=True)
def create_reader(map_file, is_train): transforms = [ xforms.color(brightness_radius=0.2, contrast_radius=0.2, saturation_radius=0.2), xforms.scale(width=img_width, height=img_height, channels=img_channel, interpolations="linear") ] return C.io.MinibatchSource( C.io.ImageDeserializer( map_file, C.io.StreamDefs(image=C.io.StreamDef(field="image", transforms=transforms), dummy=C.io.StreamDef(field="label", shape=num_classes))), randomize=is_train, max_sweeps=C.io.INFINITELY_REPEAT if is_train else 1)
def create_reader(map_file): transforms = [ xforms.crop(crop_type='randomside', side_ratio=0.85, jitter_type='uniratio'), xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='linear'), xforms.color(brightness_radius=0.2, contrast_radius=0.2, saturation_radius=0.2) ] return (MinibatchSource( ImageDeserializer( map_file, StreamDefs(features=StreamDef(field='image', transforms=transforms, is_sparse=False), labels=StreamDef(field='label', shape=num_classes, is_sparse=False)))))