'Evaluator' object has no attribute 'loss_value'

61 Views Asked by At

I'm trying to make a simple style copy net, combining two images. I'm a newbie and doing it from the example to get some experience in programming. The main idea is to copy style on the target image. Here's the code I wrote:

def preprocess(image_path):
    img = load_img(image_path, target_size = (img_height, img_width))
    img = img_to_array(img)
    img = np.expand_dims(img, axis = 0)
    img = vgg19.preprocess_input(img)
    return img 

def deprocess(x):
    x[:, :, 0] += 103.939
    x[:, :, 1] += 116.779
    x[:, :, 2] += 123.68 
    x = x[:, :, ::-1]
    x = np.clip(x, 0, 255).astype('unit8')
    return x
target_image = backend.variable(preprocess(target_image_path))
sr_image = backend.variable(preprocess(sr_image_path))

if backend.image_data_format() == 'channels_first':
        combination_image = backend.placeholder((1,3,img_height, img_width))
else:
        combination_image = backend.placeholder((1,img_height, img_width,3))

input_tensor = backend.concatenate([target_image, sr_image, combination_image], axis = 0)

model = vgg19.VGG19(input_tensor = input_tensor, weights = 'imagenet', include_top = False)
print('Model loaded successfully')

def content_loss(base, combination):
    return backend.sum(backend.square(combination - base))

def gram_matrix(x):
    features = backend.batch_flatten(backend.permute_dimensions(x, (2, 0, 1)))
    gram = backend.dot(features, backend.transpose(features))
    return gram

def style_loss(style, combination):
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = img_height * img_width
    return backend.sum(backend.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2))

def total_variation_loss(x):
    a = backend.square(
        x[:, :img_height - 1, :img_width - 1, :] - 
        x[:, 1:, :img_width - 1, :])
        
    b = backend.square(
        x[:, :img_height - 1, :img_width - 1, :] - 
        x[:, :img_height - 1, 1:, :])
        
    return backend.sum(backend.pow(a + b, 1.25))

outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
content_layer = 'block5_conv2'
style_layers = ['block1_conv1',
                'block2_conv1', 
                'block3_conv1', 
                'block4_conv1', 
                'block5_conv1']
total_variation_weight = 1e-4
style_weight = 1.
content_weight = 0.025

loss = backend.variable(0.0)
layer_features = outputs_dict[content_layer]
target_image_features = layer_features[0, :, :, :]
combination_features = layer_features[2, :, :, :]
loss = loss + content_weight * content_loss(target_image_features, combination_features)

for layer_name in style_layers:
    layer_features = outputs_dict[layer_name]
    style_reference_features = layer_features[1, :, :, :]
    combination_features = layer_features[2, :, :, :]
    sl = style_loss(style_reference_features, combination_features)
    loss = loss + (style_weight / len(style_layers)) * sl
    
loss +=  total_variation_weight * total_variation_loss(combination_image)

grads = backend.gradients(loss, combination_image)
    
outputs = [loss]
if isinstance(grads, (list,tuple)):
    outputs += grads
else:
    outputs.append(grads)
f_outputs = backend.function([combination_image], outputs)
    

def eval_loss_and_grads(x):
        if backend.image_data_format() == 'channels_first':
            x = x.reshape((1, 3, img_height, img_width))
        else:
            x = x.reshape((1, img_height, img_width, 3))
        outs = f_outputs([x])
        loss_value = outs[0]
        if len(outs[1:]) == 1:
            grad_values = outs[1].flatten().astype('float64')
        else:
            grad_values = np.array(outs[1:]).flatten().astype('float64')
        return loss_value, grad_values

class Evaluator(object):
    
    def _unit_(self):
        self.loss_value = None
        self.grads_values = None 
    
    def loss(self, x):
        assert self.loss_value is None 
        loss_value, grad_values = eval_loss_and_grads(x)
        self.loss_value = loss_value
        self.grads_values = grad_values
        return self.loss_value
    
    def grads(self, x):
        assert self.loss_value is not None
        grad_values = np.copy(self.grad_values)
        self.loss_value = None
        self.grad_values = None
        return grad_values 

evaluator = Evaluator()
result_prefix = 'result'
iterations = 20 

x = preprocess(target_image_path)
x = x.flatten()
for i in range(iterations):
    print('Start of iterations', i)
    start_time = time.time()
    x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x, 
                                     fprime = evaluator.grads, maxfun = 20)
    print('Current loss value:', min_val)
    img = x.copy().reshape((img_height, img_width, 3))
    img = deprocess(img)
    fname = result_prefix + '_at_iteration_%d.png' %  i
    save_img('D:\study\Stylecopy\fmane', img)
    print('image saved as:', fname)
    end_time = time.time()
    print(' Iteration %d completed in %ds' % (i , end_time - start_time))
    

Here's the error I got:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12264/556996678.py in <module>
      7     print('Start of iterations', i)
      8     start_time = time.time()
----> 9     x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x, 
     10                                      fprime = evaluator.grads, maxfun = 20)
     11     print('Current loss value:', min_val)

~\anaconda3\envs\CNN_base\lib\site-packages\scipy\optimize\lbfgsb.py in fmin_l_bfgs_b(func, x0, fprime, args, approx_grad, bounds, m, factr, pgtol, epsilon, iprint, maxfun, maxiter, disp, callback, maxls)
    195             'maxls': maxls}
    196 
--> 197     res = _minimize_lbfgsb(fun, x0, args=args, jac=jac, bounds=bounds,
    198                            **opts)
    199     d = {'grad': res['jac'],

~\anaconda3\envs\CNN_base\lib\site-packages\scipy\optimize\lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, finite_diff_rel_step, **unknown_options)
    304             iprint = disp
    305 
--> 306     sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
    307                                   bounds=new_bounds,
    308                                   finite_diff_rel_step=finite_diff_rel_step)

~\anaconda3\envs\CNN_base\lib\site-packages\scipy\optimize\optimize.py in _prepare_scalar_function(fun, x0, jac, args, bounds, epsilon, finite_diff_rel_step, hess)
    259     # ScalarFunction caches. Reuse of fun(x) during grad
    260     # calculation reduces overall function evaluations.
--> 261     sf = ScalarFunction(fun, x0, args, grad, hess,
    262                         finite_diff_rel_step, bounds, epsilon=epsilon)
    263 

~\anaconda3\envs\CNN_base\lib\site-packages\scipy\optimize\_differentiable_functions.py in __init__(self, fun, x0, args, grad, hess, finite_diff_rel_step, finite_diff_bounds, epsilon)
    138 
    139         self._update_fun_impl = update_fun
--> 140         self._update_fun()
    141 
    142         # Gradient evaluation

~\anaconda3\envs\CNN_base\lib\site-packages\scipy\optimize\_differentiable_functions.py in _update_fun(self)
    231     def _update_fun(self):
    232         if not self.f_updated:
--> 233             self._update_fun_impl()
    234             self.f_updated = True
    235 

~\anaconda3\envs\CNN_base\lib\site-packages\scipy\optimize\_differentiable_functions.py in update_fun()
    135 
    136         def update_fun():
--> 137             self.f = fun_wrapped(self.x)
    138 
    139         self._update_fun_impl = update_fun

~\anaconda3\envs\CNN_base\lib\site-packages\scipy\optimize\_differentiable_functions.py in fun_wrapped(x)
    132             # Overwriting results in undefined behaviour because
    133             # fun(self.x) will change self.x, with the two no longer linked.
--> 134             return fun(np.copy(x), *args)
    135 
    136         def update_fun():

~\AppData\Local\Temp/ipykernel_12264/3220866978.py in loss(self, x)
     29 
     30     def loss(self, x):
---> 31         assert self.loss_value is None
     32         loss_value, grad_values = eval_loss_and_grads(x)
     33         self.loss_value = loss_value

AttributeError: 'Evaluator' object has no attribute 'loss_value'

I'm dealing with this problem and don't know how to solve it. I've double checked the code with the example (https://www.kaggle.com/code/gabrieltangzy/p2p-gan-newver-slice-cezanne), but haven't found any mistakes. I suppose it may occur because of difference in python versions. I use 3.9.7

0

There are 0 best solutions below