def __init__(self, encoder, decoder, measure, renderer): super(ImplicitRotationPredictor, self).__init__() self.show_decoded_image = pr.ShowImage('decoded_image', wait=False) self.show_closest_image = pr.ShowImage('closest_image', wait=False) self.encoder = EncoderPredictor(encoder) self.dictionary = MakeDictionary(self.encoder, renderer)() self.encoder.add(pr.ExpandDims(0)) self.encoder.add(MeasureSimilarity(self.dictionary, measure)) self.decoder = DecoderPredictor(decoder) outputs = ['image', 'latent_vector', 'latent_image', 'decoded_image'] self.wrap = pr.WrapOutput(outputs)
def __init__(self, class_names, prior_boxes, variances=[.1, .2]): super(ShowBoxes, self).__init__() self.deprocess_boxes = SequentialProcessor([ pr.DecodeBoxes(prior_boxes, variances), pr.ToBoxes2D(class_names, True), pr.FilterClassBoxes2D(class_names[1:]) ]) self.denormalize_boxes2D = pr.DenormalizeBoxes2D() self.draw_boxes2D = pr.DrawBoxes2D(class_names) self.show_image = pr.ShowImage()
def __init__(self, model, colors=None): super(PostprocessSegmentation, self).__init__() self.add(PreprocessImage()) self.add(pr.ExpandDims(0)) self.add(pr.Predict(model)) self.add(pr.Squeeze(0)) self.add(Round()) self.add(MasksToColors(model.output_shape[-1], colors)) self.add(pr.DenormalizeImage()) self.add(pr.CastImage('uint8')) self.add(pr.ShowImage())
def __init__(self, model, colors=None): super(PostprocessSegmentation, self).__init__() self.add(pr.UnpackDictionary(['image_path'])) self.add(pr.LoadImage()) self.add(pr.ResizeImage(model.input_shape[1:3])) self.add(pr.ConvertColorSpace(pr.RGB2BGR)) self.add(pr.SubtractMeanImage(pr.BGR_IMAGENET_MEAN)) self.add(pr.ExpandDims(0)) self.add(pr.Predict(model)) self.add(pr.Squeeze(0)) self.add(Round()) self.add(MasksToColors(model.output_shape[-1], colors)) self.add(pr.DenormalizeImage()) self.add(pr.CastImage('uint8')) self.add(pr.ShowImage())
# We can now apply our pipeline as a normal function: for _ in range(5): image = load_image(image_fullpath) # use it as a normal function image = augment(image) show_image(image) # We can add to our sequential pipeline other function anywhere i.e. arg 0: augment.insert(0, pr.LoadImage()) for _ in range(5): # now we don't load the image every time. image = augment(image_fullpath) show_image(image) # Adding new processor at the end to have a single function. augment.add(pr.ShowImage()) for _ in range(5): # everything compressed into a single function image = augment(image_fullpath) # We can also pop the last processor added. augment.pop() # We now create another processor for geometric augmentation. # NOTE: We can instantiate a new SequentialProcessor using a list of processors transform = SequentialProcessor([pr.RandomRotation(), pr.RandomTranslation()]) # We can call both of our processors separately: for _ in range(5): image = transform(augment(image_fullpath)) show_image(image)
def __init__(self, mean=pr.BGR_IMAGENET_MEAN): super(AugmentBoxes, self).__init__() self.add(pr.ToImageBoxCoordinates()) self.add(pr.Expand(mean=mean)) self.add(pr.RandomSampleCrop()) self.add(pr.RandomFlipBoxesLeftRight()) self.add(pr.ToNormalizedBoxCoordinates()) # We now visualize our current box augmentation # For that we build a quick pipeline for drawing our boxes draw_boxes = SequentialProcessor([ pr.ControlMap(pr.ToBoxes2D(class_names, False), [1], [1]), pr.ControlMap(pr.DenormalizeBoxes2D(), [0, 1], [1], {0: 0}), pr.DrawBoxes2D(class_names), pr.ShowImage() ]) # Let's test it our box data augmentation pipeline augment_boxes = AugmentBoxes() print('Box augmentation examples') for _ in range(10): image = P.image.load_image(image_fullpath) image, boxes = augment_boxes(image, box_data.copy()) draw_boxes(P.image.resize_image(image, (300, 300)), boxes) # There is also some box-preprocessing that is required. # Mostly we must match our boxes to a set of default (prior) boxes. # Then we must encode them and expand the class label to a one-hot vector. class PreprocessBoxes(SequentialProcessor):
if __name__ == "__main__": import os from paz.datasets import FER, FERPlus # data generator and augmentations generator = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, zoom_range=.1, horizontal_flip=True) pipeline = ProcessGrayImage(48, 8, generator) dataset = 'FERPlus' data_path = os.path.join(os.path.expanduser('~'), '.keras/paz/datasets/') name_to_manager = {'FER': FER, 'FERPlus': FERPlus} data_managers, datasets = {}, {} data_path = os.path.join(data_path, dataset) kwargs = {'path': data_path} if dataset in ['FERPlus'] else {} data_manager = name_to_manager[dataset](split='train', **kwargs) data = data_manager.load_data() sequence = ProcessingSequence(pipeline, 32, data) batch = sequence.__getitem__(0) show = pr.ShowImage() for arg in range(32): image = batch[0]['image'][arg][..., 0] image = 255 * image image = image.astype('uint8') show(image)
# first we transform our numpy array into our built-in ``Box2D`` messages to_boxes2D = pr.ToBoxes2D(class_names) denormalize = pr.DenormalizeBoxes2D() boxes2D = to_boxes2D(box_data) image = load_image(image_fullpath) boxes2D = denormalize(image, boxes2D) draw_boxes2D = pr.DrawBoxes2D(class_names) show_image(draw_boxes2D(image, boxes2D)) # As you can see, we were not able to put everything as a # ``SequentialProcessor``. This is because we are dealing with 2 inputs: # ``box_data`` and ``image``. We can join them into a single processor # using ``pr.ControlMap`` wrap. ``pr.ControlMap`` allows you to select which # arguments (``intro_indices``) are passed to your processor, and also where # you should put the output of your processor (``outro_indices``). draw_boxes = SequentialProcessor() draw_boxes.add(pr.ControlMap(to_boxes2D, intro_indices=[1], outro_indices=[1])) draw_boxes.add(pr.ControlMap(pr.LoadImage(), [0], [0])) draw_boxes.add(pr.ControlMap(denormalize, [0, 1], [1], keep={0: 0})) draw_boxes.add(pr.DrawBoxes2D(class_names)) draw_boxes.add(pr.ShowImage()) # now you have everything in a single packed function that loads and draws! draw_boxes(image_fullpath, box_data) # Also note if one of your function is ``eating`` away one input that you # wish to keep in your pipeline, you can use the ``keep`` dictionary to # explicitly say which of your inputs you wish to keep and where it should # be located. This is represented respectively by the ``key`` and the # ``value`` of the ``keep`` dictionary.