<s> from __future__ import division <EOL> from io import BytesIO <EOL> import math <EOL> import numpy <EOL> from PIL import Image <EOL> from six import PY3 <EOL> try : <EOL> from . _image import window_batch_bchw <EOL> window_batch_bchw_available = True <EOL> except ImportError : <EOL> window_batch_bchw_available = False <EOL> from . import ExpectsAxisLabels , SourcewiseTransformer <EOL> from . . import config <EOL> class ImagesFromBytes ( SourcewiseTransformer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data_stream , color_mode = '<STR_LIT>' , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , data_stream . produces_examples ) <EOL> which_sources = kwargs . get ( '<STR_LIT>' , data_stream . sources ) <EOL> axis_labels = self . _make_axis_labels ( data_stream , which_sources , <EOL> kwargs [ '<STR_LIT>' ] ) <EOL> kwargs . setdefault ( '<STR_LIT>' , axis_labels ) <EOL> super ( ImagesFromBytes , self ) . __init__ ( data_stream , ** kwargs ) <EOL> self . color_mode = color_mode <EOL> def transform_source_example ( self , example , source_name ) : <EOL> if PY3 : <EOL> bytes_type = bytes <EOL> else : <EOL> bytes_type = str <EOL> if not isinstance ( example , bytes_type ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( bytes_type . __name__ ) ) <EOL> pil_image = Image . open ( BytesIO ( example ) ) <EOL> if self . color_mode is not None : <EOL> pil_image = pil_image . convert ( self . color_mode ) <EOL> image = numpy . array ( pil_image ) <EOL> if image . ndim == <NUM_LIT:3> : <EOL> return image . transpose ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> elif image . ndim == <NUM_LIT:2> : <EOL> image = image [ numpy . newaxis ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return image <EOL> def transform_source_batch ( self , batch , source_name ) : <EOL> return [ self . transform_source_example ( im , source_name ) for im in batch ] <EOL> def _make_axis_labels ( self , data_stream , which_sources , produces_examples ) : <EOL> labels = { } <EOL> for source in data_stream . sources : <EOL> if source in which_sources : <EOL> if produces_examples : <EOL> labels [ source ] = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) <EOL> else : <EOL> labels [ source ] = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) <EOL> else : <EOL> labels [ source ] = ( data_stream . axis_labels [ source ] <EOL> if source in data_stream . axis_labels <EOL> else None ) <EOL> return labels <EOL> class MinimumImageDimensions ( SourcewiseTransformer , ExpectsAxisLabels ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data_stream , minimum_shape , resample = '<STR_LIT>' , <EOL> ** kwargs ) : <EOL> self . minimum_shape = minimum_shape <EOL> try : <EOL> self . resample = getattr ( Image , resample . upper ( ) ) <EOL> except AttributeError : <EOL> raise ValueError ( "<STR_LIT>" . format ( resample ) ) <EOL> kwargs . setdefault ( '<STR_LIT>' , data_stream . produces_examples ) <EOL> kwargs . setdefault ( '<STR_LIT>' , data_stream . axis_labels ) <EOL> super ( MinimumImageDimensions , self ) . __init__ ( data_stream , ** kwargs ) <EOL> def transform_source_batch ( self , batch , source_name ) : <EOL> self . verify_axis_labels ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) , <EOL> self . data_stream . axis_labels [ source_name ] , <EOL> source_name ) <EOL> return [ self . _example_transform ( im , source_name ) for im in batch ] <EOL> def transform_source_example ( self , example , source_name ) : <EOL> self . verify_axis_labels ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) , <EOL> self . data_stream . axis_labels [ source_name ] , <EOL> source_name ) <EOL> return self . _example_transform ( example , source_name ) <EOL> def _example_transform ( self , example , _ ) : <EOL> if example . ndim > <NUM_LIT:3> or example . ndim < <NUM_LIT:2> : <EOL> raise NotImplementedError <EOL> min_height , min_width = self . minimum_shape <EOL> original_height , original_width = example . shape [ - <NUM_LIT:2> : ] <EOL> if original_height < min_height or original_width < min_width : <EOL> dt = example . dtype <EOL> if example . ndim == <NUM_LIT:3> : <EOL> im = example . transpose ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> else : <EOL> im = example <EOL> im = Image . fromarray ( im ) <EOL> width , height = im . size <EOL> multiplier = max ( <NUM_LIT:1> , min_width / width , min_height / height ) <EOL> width = int ( math . ceil ( width * multiplier ) ) <EOL> height = int ( math . ceil ( height * multiplier ) ) <EOL> im = numpy . array ( im . resize ( ( width , height ) ) ) . astype ( dt ) <EOL> if im . ndim == <NUM_LIT:3> : <EOL> example = im . transpose ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> else : <EOL> example = im <EOL> return example <EOL> class RandomFixedSizeCrop ( SourcewiseTransformer , ExpectsAxisLabels ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data_stream , window_shape , ** kwargs ) : <EOL> if not window_batch_bchw_available : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> self . window_shape = window_shape <EOL> self . rng = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . warned_axis_labels = False <EOL> if self . rng is None : <EOL> self . rng = numpy . random . RandomState ( config . default_seed ) <EOL> kwargs . setdefault ( '<STR_LIT>' , data_stream . produces_examples ) <EOL> kwargs . setdefault ( '<STR_LIT>' , data_stream . axis_labels ) <EOL> super ( RandomFixedSizeCrop , self ) . __init__ ( data_stream , ** kwargs ) <EOL> def transform_source_batch ( self , source , source_name ) : <EOL> self . verify_axis_labels ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) , <EOL> self . data_stream . axis_labels [ source_name ] , <EOL> source_name ) <EOL> windowed_height , windowed_width = self . window_shape <EOL> if isinstance ( source , numpy . ndarray ) and source . ndim == <NUM_LIT:4> : <EOL> out = numpy . empty ( source . shape [ : <NUM_LIT:2> ] + self . window_shape , <EOL> dtype = source . dtype ) <EOL> batch_size = source . shape [ <NUM_LIT:0> ] <EOL> image_height , image_width = source . shape [ <NUM_LIT:2> : ] <EOL> max_h_off = image_height - windowed_height <EOL> max_w_off = image_width - windowed_width <EOL> if max_h_off < <NUM_LIT:0> or max_w_off < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> source . shape [ <NUM_LIT:2> : ] , self . window_shape ) ) <EOL> offsets_w = self . rng . random_integers ( <NUM_LIT:0> , max_w_off , size = batch_size ) <EOL> offsets_h = self . rng . random_integers ( <NUM_LIT:0> , max_h_off , size = batch_size ) <EOL> window_batch_bchw ( source , offsets_h , offsets_w , out ) <EOL> return out <EOL> elif all ( isinstance ( b , numpy . ndarray ) and b . ndim == <NUM_LIT:3> for b in source ) : <EOL> return [ self . transform_source_example ( im , source_name ) <EOL> for im in source ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def transform_source_example ( self , example , source_name ) : <EOL> self . verify_axis_labels ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) , <EOL> self . data_stream . axis_labels [ source_name ] , <EOL> source_name ) <EOL> windowed_height , windowed_width = self . window_shape <EOL> if not isinstance ( example , numpy . ndarray ) or example . ndim != <NUM_LIT:3> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> image_height , image_width = example . shape [ <NUM_LIT:1> : ] <EOL> if image_height < windowed_height or image_width < windowed_width : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> windowed_height , windowed_width , <EOL> image_height , image_width ) ) <EOL> if image_height - windowed_height > <NUM_LIT:0> : <EOL> off_h = self . rng . random_integers ( <NUM_LIT:0> , image_height - windowed_height ) <EOL> else : <EOL> off_h = <NUM_LIT:0> <EOL> if image_width - windowed_width > <NUM_LIT:0> : <EOL> off_w = self . rng . random_integers ( <NUM_LIT:0> , image_width - windowed_width ) <EOL> else : <EOL> off_w = <NUM_LIT:0> <EOL> return example [ : , off_h : off_h + windowed_height , <EOL> off_w : off_w + windowed_width ] <EOL> class Random2DRotation ( SourcewiseTransformer , ExpectsAxisLabels ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data_stream , maximum_rotation = math . pi , <EOL> resample = '<STR_LIT>' , ** kwargs ) : <EOL> if maximum_rotation <= <NUM_LIT:0> or maximum_rotation > math . pi : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( maximum_rotation ) ) <EOL> self . maximum_rotation = numpy . rad2deg ( maximum_rotation ) <EOL> try : <EOL> self . resample = getattr ( Image , resample . upper ( ) ) <EOL> except AttributeError : <EOL> raise ValueError ( "<STR_LIT>" . format ( resample ) ) <EOL> self . rng = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . warned_axis_labels = False <EOL> if self . rng is None : <EOL> self . rng = numpy . random . RandomState ( config . default_seed ) <EOL> kwargs . setdefault ( '<STR_LIT>' , data_stream . produces_examples ) <EOL> kwargs . setdefault ( '<STR_LIT>' , data_stream . axis_labels ) <EOL> super ( Random2DRotation , self ) . __init__ ( data_stream , ** kwargs ) <EOL> def transform_source_batch ( self , source , source_name ) : <EOL> self . verify_axis_labels ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) , <EOL> self . data_stream . axis_labels [ source_name ] , <EOL> source_name ) <EOL> rotation_angles = self . rng . uniform ( - self . maximum_rotation , <EOL> self . maximum_rotation , <EOL> len ( source ) ) <EOL> if isinstance ( source , list ) and all ( isinstance ( b , numpy . ndarray ) and <EOL> b . ndim == <NUM_LIT:3> for b in source ) : <EOL> return [ self . _example_transform ( im , angle ) <EOL> for im , angle in zip ( source , rotation_angles ) ] <EOL> elif isinstance ( source , numpy . ndarray ) and source . dtype == object and all ( isinstance ( b , numpy . ndarray ) and b . ndim == <NUM_LIT:3> for b in <EOL> source ) : <EOL> out = numpy . empty ( len ( source ) , dtype = object ) <EOL> for im_idx , ( im , angle ) in enumerate ( zip ( source , rotation_angles ) ) : <EOL> out [ im_idx ] = self . _example_transform ( im , angle ) <EOL> return out <EOL> elif isinstance ( source , numpy . ndarray ) and source . ndim == <NUM_LIT:4> : <EOL> return numpy . array ( [ self . _example_transform ( im , angle ) <EOL> for im , angle in zip ( source , rotation_angles ) ] , <EOL> dtype = source . dtype ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def transform_source_example ( self , example , source_name ) : <EOL> self . verify_axis_labels ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' ) , <EOL> self . data_stream . axis_labels [ source_name ] , <EOL> source_name ) <EOL> if not isinstance ( example , numpy . ndarray ) or example . ndim != <NUM_LIT:3> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> rotation_angle = self . rng . uniform ( - self . maximum_rotation , <EOL> self . maximum_rotation ) <EOL> return self . _example_transform ( example , rotation_angle ) <EOL> def _example_transform ( self , example , rotation_angle ) : <EOL> dt = example . dtype <EOL> im = Image . fromarray ( example . transpose ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ) <EOL> example = numpy . array ( im . rotate ( rotation_angle , <EOL> resample = self . resample ) ) . astype ( dt ) <EOL> return example . transpose ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) </s>
