<s> import os <EOL> import time <EOL> import numpy as np <EOL> import scipy . ndimage <EOL> from scipy . misc import imsave <EOL> from image_analogy import img_utils , vgg16 <EOL> from image_analogy . optimizer import Optimizer <EOL> def main ( args , model_class ) : <EOL> '''<STR_LIT>''' <EOL> if args . num_scales > <NUM_LIT:1> : <EOL> step_scale_factor = ( <NUM_LIT:1> - args . min_scale ) / ( args . num_scales - <NUM_LIT:1> ) <EOL> else : <EOL> step_scale_factor = <NUM_LIT:0.0> <EOL> args . min_scale = <NUM_LIT:1.0> <EOL> full_ap_image = img_utils . load_image ( args . ap_image_path ) <EOL> full_a_image = img_utils . load_image ( args . a_image_path ) <EOL> full_b_image = img_utils . load_image ( args . b_image_path ) <EOL> full_img_width , full_img_height = calculate_image_dims ( args , full_b_image ) <EOL> img_num_channels = <NUM_LIT:3> <EOL> b_scale_ratio_width = float ( full_b_image . shape [ <NUM_LIT:1> ] ) / full_img_width <EOL> b_scale_ratio_height = float ( full_b_image . shape [ <NUM_LIT:0> ] ) / full_img_height <EOL> output_dir = os . path . dirname ( args . result_prefix ) <EOL> if not os . path . exists ( output_dir ) : <EOL> os . makedirs ( output_dir ) <EOL> x = None <EOL> optimizer = Optimizer ( ) <EOL> for scale_i in range ( args . num_scales ) : <EOL> scale_factor = ( scale_i * step_scale_factor ) + args . min_scale <EOL> img_width = int ( round ( full_img_width * scale_factor ) ) <EOL> img_height = int ( round ( full_img_height * scale_factor ) ) <EOL> if x is None : <EOL> x = np . random . uniform ( <NUM_LIT:0> , <NUM_LIT:255> , ( img_height , img_width , <NUM_LIT:3> ) ) <EOL> x = vgg16 . img_to_vgg ( x ) <EOL> else : <EOL> zoom_ratio = img_width / float ( x . shape [ - <NUM_LIT:1> ] ) <EOL> x = scipy . ndimage . zoom ( x , ( <NUM_LIT:1> , zoom_ratio , zoom_ratio ) , order = <NUM_LIT:1> ) <EOL> img_height , img_width = x . shape [ - <NUM_LIT:2> : ] <EOL> if args . a_scale_mode == '<STR_LIT>' : <EOL> a_img_width = img_width <EOL> a_img_height = img_height <EOL> elif args . a_scale_mode == '<STR_LIT:none>' : <EOL> a_img_width = full_a_image . shape [ <NUM_LIT:1> ] * scale_factor <EOL> a_img_height = full_a_image . shape [ <NUM_LIT:0> ] * scale_factor <EOL> else : <EOL> a_img_width = full_a_image . shape [ <NUM_LIT:1> ] * scale_factor * b_scale_ratio_width <EOL> a_img_height = full_a_image . shape [ <NUM_LIT:0> ] * scale_factor * b_scale_ratio_height <EOL> a_img_width = int ( round ( args . a_scale * a_img_width ) ) <EOL> a_img_height = int ( round ( args . a_scale * a_img_height ) ) <EOL> a_image = img_utils . preprocess_image ( full_a_image , a_img_width , a_img_height ) <EOL> ap_image = img_utils . preprocess_image ( full_ap_image , a_img_width , a_img_height ) <EOL> b_image = img_utils . preprocess_image ( full_b_image , img_width , img_height ) <EOL> print ( '<STR_LIT>' . format ( scale_factor , a_image . shape , b_image . shape ) ) <EOL> net = vgg16 . get_model ( img_width , img_height , weights_path = args . vgg_weights , pool_mode = args . pool_mode ) <EOL> model = model_class ( net , args ) <EOL> model . build ( a_image , ap_image , b_image , ( <NUM_LIT:1> , img_num_channels , img_height , img_width ) ) <EOL> for i in range ( args . num_iterations_per_scale ) : <EOL> print ( '<STR_LIT>' . format ( scale_i , i ) ) <EOL> start_time = time . time ( ) <EOL> if args . color_jitter : <EOL> color_jitter = ( args . color_jitter * <NUM_LIT:2> ) * ( np . random . random ( ( <NUM_LIT:3> , img_height , img_width ) ) - <NUM_LIT:0.5> ) <EOL> x += color_jitter <EOL> if args . jitter : <EOL> jitter = args . jitter * scale_factor <EOL> ox , oy = np . random . randint ( - jitter , jitter + <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> x = np . roll ( np . roll ( x , ox , - <NUM_LIT:1> ) , oy , - <NUM_LIT:2> ) <EOL> x , min_val , info = optimizer . optimize ( x , model ) <EOL> print ( '<STR_LIT>' . format ( min_val ) ) <EOL> x = x . reshape ( ( <NUM_LIT:3> , img_height , img_width ) ) <EOL> if args . jitter : <EOL> x = np . roll ( np . roll ( x , - ox , - <NUM_LIT:1> ) , - oy , - <NUM_LIT:2> ) <EOL> if args . color_jitter : <EOL> x -= color_jitter <EOL> if args . output_full_size : <EOL> out_resize_shape = ( full_img_height , full_img_width ) <EOL> else : <EOL> out_resize_shape = None <EOL> img = img_utils . deprocess_image ( np . copy ( x ) , contrast_percent = args . contrast_percent , resize = out_resize_shape ) <EOL> fname = args . result_prefix + '<STR_LIT>' . format ( scale_i , i ) <EOL> imsave ( fname , img ) <EOL> end_time = time . time ( ) <EOL> print ( '<STR_LIT>' . format ( fname ) ) <EOL> print ( '<STR_LIT>' . format ( end_time - start_time , ) ) <EOL> def calculate_image_dims ( args , full_b_image ) : <EOL> '''<STR_LIT>''' <EOL> full_img_width = full_b_image . shape [ <NUM_LIT:1> ] <EOL> full_img_height = full_b_image . shape [ <NUM_LIT:0> ] <EOL> if args . out_width or args . out_height : <EOL> if args . out_width and args . out_height : <EOL> full_img_width = args . out_width <EOL> full_img_height = args . out_height <EOL> else : <EOL> if args . out_width : <EOL> full_img_height = int ( round ( args . out_width / float ( full_img_width ) * full_img_height ) ) <EOL> full_img_width = args . out_width <EOL> else : <EOL> full_img_width = int ( round ( args . out_height / float ( full_img_height ) * full_img_width ) ) <EOL> full_img_height = args . out_height <EOL> return full_img_width , full_img_height </s>
