#NEWLINE# def build_loss( self , a_image , ap_image , b_image ) : #NEWLINE# #INDENT# 'Create an expression for the loss as a function of the image inputs.' #NEWLINE# print ( 'Building loss...' ) #NEWLINE# loss = super ( NNFModel , self ) . build_loss ( a_image , ap_image , b_image ) #NEWLINE# print ( 'Precomputing static features...' ) #NEWLINE# ( all_a_features , all_ap_image_features , all_b_features ) = self . precompute_static_features ( a_image , ap_image , b_image ) #NEWLINE# print ( 'Building and combining losses...' ) #NEWLINE# if self . args . analogy_weight : #NEWLINE# #INDENT# for layer_name in self . args . analogy_layers : #NEWLINE# #INDENT# a_features = all_a_features [ layer_name ] [ 0 ] #NEWLINE# ap_image_features = all_ap_image_features [ layer_name ] [ 0 ] #NEWLINE# b_features = all_b_features [ layer_name ] [ 0 ] #NEWLINE# layer_features = self . get_layer_output ( layer_name ) #NEWLINE# combination_features = layer_features [ 0 , : , : , : ] #NEWLINE# al = nnf_analogy_loss ( a_features , ap_image_features , b_features , combination_features , num_steps = self . args . analogy_nnf_steps , patch_size = self . args . patch_size , patch_stride = self . args . patch_stride , jump_size = 1.0 ) #NEWLINE# loss += ( ( self . args . analogy_weight / len ( self . args . analogy_layers ) ) * al ) #NEWLINE# #UNINDENT# #UNINDENT# existing_feature_nnfs = getattr ( self , 'feature_nnfs' , ( [ None ] * len ( self . args . mrf_layers ) ) ) #NEWLINE# self . feature_nnfs = [ ] #NEWLINE# if self . args . mrf_weight : #NEWLINE# #INDENT# for ( layer_name , existing_nnf ) in zip ( self . args . mrf_layers , existing_feature_nnfs ) : #NEWLINE# #INDENT# ap_image_features = all_ap_image_features [ layer_name ] [ 0 ] #NEWLINE# layer_features = self . get_layer_output ( layer_name ) #NEWLINE# combination_features = layer_features [ 0 , : , : , : ] #NEWLINE# input_shape = self . get_layer_output_shape ( layer_name ) #NEWLINE# if ( existing_nnf and ( not self . args . randomize_mnf_nnf ) ) : #NEWLINE# #INDENT# matcher = existing_nnf . matcher . scale ( ( input_shape [ 3 ] , input_shape [ 2 ] , input_shape [ 1 ] ) , ap_image_features ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# matcher = PatchMatcher ( ( input_shape [ 3 ] , input_shape [ 2 ] , input_shape [ 1 ] ) , ap_image_features , patch_size = self . args . patch_size , jump_size = 1.0 , patch_stride = self . args . patch_stride ) #NEWLINE# #UNINDENT# nnf = NNFState ( matcher , self . get_f_layer ( layer_name ) ) #NEWLINE# self . feature_nnfs . append ( nnf ) #NEWLINE# sl = content_loss ( combination_features , nnf . placeholder ) #NEWLINE# loss += ( ( self . args . mrf_weight / len ( self . args . mrf_layers ) ) * sl ) #NEWLINE# #UNINDENT# #UNINDENT# if self . args . b_bp_content_weight : #NEWLINE# #INDENT# for layer_name in self . args . b_content_layers : #NEWLINE# #INDENT# b_features = K . variable ( all_b_features [ layer_name ] [ 0 ] ) #NEWLINE# bp_features = self . get_layer_output ( layer_name ) #NEWLINE# cl = content_loss ( bp_features , b_features ) #NEWLINE# loss += ( ( self . args . b_bp_content_weight / len ( self . args . b_content_layers ) ) * cl ) #NEWLINE# #UNINDENT# #UNINDENT# if ( self . args . neural_style_weight != 0.0 ) : #NEWLINE# #INDENT# for layer_name in self . args . neural_style_layers : #NEWLINE# #INDENT# ap_image_features = K . variable ( all_ap_image_features [ layer_name ] [ 0 ] ) #NEWLINE# layer_features = self . get_layer_output ( layer_name ) #NEWLINE# layer_shape = self . get_layer_output_shape ( layer_name ) #NEWLINE# combination_features = layer_features [ 0 , : , : , : ] #NEWLINE# nsl = neural_style_loss ( ap_image_features , combination_features , 3 , self . output_shape [ ( - 2 ) ] , self . output_shape [ ( - 1 ) ] ) #NEWLINE# loss += ( ( self . args . neural_style_weight / len ( self . args . neural_style_layers ) ) * nsl ) #NEWLINE# #UNINDENT# #UNINDENT# return loss