<s> from . base import ImageAugmentor <EOL> from ... utils import logger <EOL> import numpy as np <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class GaussianMap ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , image_shape , sigma = <NUM_LIT:0.5> ) : <EOL> assert len ( image_shape ) == <NUM_LIT:2> <EOL> self . shape = image_shape <EOL> self . sigma = sigma <EOL> def get_gaussian_weight ( self , anchor ) : <EOL> ret = np . zeros ( self . shape , dtype = '<STR_LIT>' ) <EOL> y , x = np . mgrid [ : self . shape [ <NUM_LIT:0> ] , : self . shape [ <NUM_LIT:1> ] ] <EOL> y = y . astype ( '<STR_LIT>' ) / ret . shape [ <NUM_LIT:0> ] - anchor [ <NUM_LIT:0> ] <EOL> x = x . astype ( '<STR_LIT>' ) / ret . shape [ <NUM_LIT:1> ] - anchor [ <NUM_LIT:1> ] <EOL> g = np . exp ( - ( x ** <NUM_LIT:2> + y ** <NUM_LIT:2> ) / self . sigma ) <EOL> return g <EOL> def np_sample ( img , coords ) : <EOL> coords = np . maximum ( coords , <NUM_LIT:0> ) <EOL> coords = np . minimum ( coords , np . array ( [ img . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> , img . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> ] ) ) <EOL> lcoor = np . floor ( coords ) . astype ( '<STR_LIT>' ) <EOL> ucoor = lcoor + <NUM_LIT:1> <EOL> ucoor = np . minimum ( ucoor , np . array ( [ img . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> , img . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> ] ) ) <EOL> diff = coords - lcoor <EOL> neg_diff = <NUM_LIT:1.0> - diff <EOL> lcoory , lcoorx = np . split ( lcoor , <NUM_LIT:2> , axis = <NUM_LIT:2> ) <EOL> ucoory , ucoorx = np . split ( ucoor , <NUM_LIT:2> , axis = <NUM_LIT:2> ) <EOL> diff = np . repeat ( diff , <NUM_LIT:3> , <NUM_LIT:2> ) . reshape ( ( diff . shape [ <NUM_LIT:0> ] , diff . shape [ <NUM_LIT:1> ] , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> neg_diff = np . repeat ( neg_diff , <NUM_LIT:3> , <NUM_LIT:2> ) . reshape ( ( diff . shape [ <NUM_LIT:0> ] , diff . shape [ <NUM_LIT:1> ] , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> diffy , diffx = np . split ( diff , <NUM_LIT:2> , axis = <NUM_LIT:2> ) <EOL> ndiffy , ndiffx = np . split ( neg_diff , <NUM_LIT:2> , axis = <NUM_LIT:2> ) <EOL> ret = img [ lcoory , lcoorx , : ] * ndiffx * ndiffy + img [ ucoory , ucoorx , : ] * diffx * diffy + img [ lcoory , ucoorx , : ] * ndiffy * diffx + img [ ucoory , lcoorx , : ] * diffy * ndiffx <EOL> return ret [ : , : , <NUM_LIT:0> , : ] <EOL> class GaussianDeform ( ImageAugmentor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , anchors , shape , sigma = <NUM_LIT:0.5> , randrange = None ) : <EOL> """<STR_LIT>""" <EOL> logger . warn ( "<STR_LIT>" ) <EOL> super ( GaussianDeform , self ) . __init__ ( ) <EOL> self . anchors = anchors <EOL> self . K = len ( self . anchors ) <EOL> self . shape = shape <EOL> self . grid = np . mgrid [ <NUM_LIT:0> : self . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> : self . shape [ <NUM_LIT:1> ] ] . transpose ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> self . grid = self . grid . astype ( '<STR_LIT>' ) <EOL> gm = GaussianMap ( self . shape , sigma = sigma ) <EOL> self . gws = np . array ( [ gm . get_gaussian_weight ( ank ) <EOL> for ank in self . anchors ] , dtype = '<STR_LIT>' ) <EOL> self . gws = self . gws . transpose ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> if randrange is None : <EOL> self . randrange = self . shape [ <NUM_LIT:0> ] / <NUM_LIT:8> <EOL> else : <EOL> self . randrange = randrange <EOL> def _augment ( self , img ) : <EOL> if img . coords : <EOL> raise NotImplementedError ( ) <EOL> v = self . rng . rand ( self . K , <NUM_LIT:2> ) . astype ( '<STR_LIT>' ) - <NUM_LIT:0.5> <EOL> v = v * <NUM_LIT:2> * self . randrange <EOL> grid = self . grid + np . dot ( self . gws , v ) <EOL> img . arr = np_sample ( img . arr , grid ) </s>
