@ -15,28 +15,66 @@ IMAGENET_DPN_MEAN = (124 / 255, 117 / 255, 104 / 255)
IMAGENET_DPN_STD = tuple ( [ 1 / ( .0167 * 255 ) ] * 3 )
IMAGENET_DPN_STD = tuple ( [ 1 / ( .0167 * 255 ) ] * 3 )
def get_mean_and_std ( model , args , num_chan = 3 ) :
def resolve_data_config ( model , args , default_cfg = { } , verbose = True ) :
if hasattr ( model , ' default_cfg ' ) :
new_config = { }
mean = model . default_cfg [ ' mean ' ]
default_cfg = default_cfg
std = model . default_cfg [ ' std ' ]
if not default_cfg and hasattr ( model , ' default_cfg ' ) :
else :
default_cfg = model . default_cfg
# Resolve input/image size
# FIXME grayscale/chans arg to use different # channels?
in_chans = 3
input_size = ( in_chans , 224 , 224 )
if args . img_size is not None :
# FIXME support passing img_size as tuple, non-square
assert isinstance ( args . img_size , int )
input_size = ( in_chans , args . img_size , args . img_size )
elif ' input_size ' in default_cfg :
input_size = default_cfg [ ' input_size ' ]
new_config [ ' input_size ' ] = input_size
# resolve interpolation method
new_config [ ' interpolation ' ] = ' bilinear '
if args . interpolation :
new_config [ ' interpolation ' ] = args . interpolation
elif ' interpolation ' in default_cfg :
new_config [ ' interpolation ' ] = default_cfg [ ' interpolation ' ]
# resolve dataset + model mean for normalization
new_config [ ' mean ' ] = get_mean_by_model ( args . model )
if args . mean is not None :
if args . mean is not None :
mean = tuple ( args . mean )
mean = tuple ( args . mean )
if len ( mean ) == 1 :
if len ( mean ) == 1 :
mean = tuple ( list ( mean ) * num_chan )
mean = tuple ( list ( mean ) * in_chans )
else :
assert len ( mean ) == num_chan
else :
else :
mean = get_mean_by_model ( args . model )
assert len ( mean ) == in_chans
new_config [ ' mean ' ] = mean
elif ' mean ' in default_cfg :
new_config [ ' mean ' ] = default_cfg [ ' mean ' ]
# resolve dataset + model std deviation for normalization
new_config [ ' std ' ] = get_std_by_model ( args . model )
if args . std is not None :
if args . std is not None :
std = tuple ( args . std )
std = tuple ( args . std )
if len ( std ) == 1 :
if len ( std ) == 1 :
std = tuple ( list ( std ) * num_chan )
std = tuple ( list ( std ) * i n_chans )
else :
else :
assert len ( std ) == num_chan
assert len ( std ) == in_chans
new_config [ ' std ' ] = std
else :
else :
std = get_std_by_model ( args . model )
new_config [ ' std ' ] = default_cfg [ ' std ' ]
return mean , std
# resolve default crop percentage
new_config [ ' crop_pct ' ] = DEFAULT_CROP_PCT
if ' crop_pct ' in default_cfg :
new_config [ ' crop_pct ' ] = default_cfg [ ' crop_pct ' ]
if verbose :
print ( ' Data processing configuration for current model + dataset: ' )
for n , v in new_config . items ( ) :
print ( ' \t %s : %s ' % ( n , str ( v ) ) )
return new_config
def get_mean_by_name ( name ) :
def get_mean_by_name ( name ) :
@ -104,6 +142,7 @@ def transforms_imagenet_train(
img_size = 224 ,
img_size = 224 ,
scale = ( 0.1 , 1.0 ) ,
scale = ( 0.1 , 1.0 ) ,
color_jitter = ( 0.4 , 0.4 , 0.4 ) ,
color_jitter = ( 0.4 , 0.4 , 0.4 ) ,
interpolation = ' bilinear ' ,
random_erasing = 0.4 ,
random_erasing = 0.4 ,
use_prefetcher = False ,
use_prefetcher = False ,
mean = IMAGENET_DEFAULT_MEAN ,
mean = IMAGENET_DEFAULT_MEAN ,
@ -112,7 +151,8 @@ def transforms_imagenet_train(
tfl = [
tfl = [
transforms . RandomResizedCrop (
transforms . RandomResizedCrop (
img_size , scale = scale , interpolation = Image . BICUBIC ) ,
img_size , scale = scale ,
interpolation = Image . BILINEAR if interpolation == ' bilinear ' else Image . BICUBIC ) ,
transforms . RandomHorizontalFlip ( ) ,
transforms . RandomHorizontalFlip ( ) ,
transforms . ColorJitter ( * color_jitter ) ,
transforms . ColorJitter ( * color_jitter ) ,
]
]
@ -135,14 +175,24 @@ def transforms_imagenet_train(
def transforms_imagenet_eval (
def transforms_imagenet_eval (
img_size = 224 ,
img_size = 224 ,
crop_pct = None ,
crop_pct = None ,
interpolation = ' bilinear ' ,
use_prefetcher = False ,
use_prefetcher = False ,
mean = IMAGENET_DEFAULT_MEAN ,
mean = IMAGENET_DEFAULT_MEAN ,
std = IMAGENET_DEFAULT_STD ) :
std = IMAGENET_DEFAULT_STD ) :
crop_pct = crop_pct or DEFAULT_CROP_PCT
crop_pct = crop_pct or DEFAULT_CROP_PCT
if isinstance ( img_size , tuple ) :
assert len ( img_size ) == 2
if img_size [ 0 ] == img_size [ 1 ] :
# fall-back to older behaviour so Resize scales to shortest edge if target is square
scale_size = int ( math . floor ( img_size [ 0 ] / crop_pct ) )
else :
scale_size = tuple ( [ int ( x [ 0 ] / crop_pct ) for x in img_size ] )
else :
scale_size = int ( math . floor ( img_size / crop_pct ) )
scale_size = int ( math . floor ( img_size / crop_pct ) )
tfl = [
tfl = [
transforms . Resize ( scale_size , Image . BICUBIC ) ,
transforms . Resize ( scale_size , Image . BI LINEAR if interpolation == ' bilinear ' else Image . BI CUBIC) ,
transforms . CenterCrop ( img_size ) ,
transforms . CenterCrop ( img_size ) ,
]
]
if use_prefetcher :
if use_prefetcher :