deep_learning_cookbook icon indicating copy to clipboard operation
deep_learning_cookbook copied to clipboard

12.2 Neural Style Import, AttributeError, RuntimeError Issues and Solutions

Open mikechen66 opened this issue 5 years ago • 1 comments

Hi Douwe:

Since any error incurs a bunch of long running hours and might make guys bad sleep, I raise the issues and related solutions.

1. from scipy.misc import imread, imresize, imsave, fromimage, toimage

Issues:

ImportError: cannot import name 'imread' and 'imsave 'from 'scipy.misc' ImportError: cannot import name 'imresize' from 'scipy.misc' ImportError: cannot import name 'fromimage' from 'scipy.misc' ImportError: cannot import name 'toimage' from 'scipy.misc'

Solutions:

change from scipy.misc import imread, imresize, imsave to

from imageio import imread, imsave
from skimage.transform import resize

change from scipy.misc import fromimage, toimage to

fromimage(im) -> np.asarray(im)
toimage() -> Image.fromarray()

2. AttributeError: 'numpy.int64' object has no attribute 'value'

Issues in details


AttributeError Traceback (most recent call last) in 3 loss_style = K.variable(0.) 4 for idx, layer_features in enumerate(feature_outputs): ----> 5 loss_style += style_loss(layer_features[0, :, :, :], layer_features[1, :, :, :]) 6 7 style_evaluator = Evaluator(loss_style, result_image)

in style_loss(layer_1, layer_2) 8 gr1 = gram_matrix(layer_1) 9 gr2 = gram_matrix(layer_2) ---> 10 return K.sum(K.square(gr1 - gr2)) / (np.prod(layer_2.shape).value ** 2)

Solution:

In [7]:

change return K.sum(K.square(gr1 - gr2)) / (np.prod(layer_2.shape).value ** 2) to return K.sum(K.square(gr1 - gr2)) / (np.prod(layer_2.shape) ** 2)

In [8]:

change the lines of the code

for idx, layer_features in enumerate(feature_outputs):
    loss_style +=  style_loss(layer_features[0, :, :, :], layer_features[1, :, :, :])

to the following lines of code

for idx, layer_features in enumerate(feature_outputs):
    loss_style = loss_style + style_loss(layer_features[0, :, :, :], layer_features[1, :, :, :])

3. RuntimeError Issue and Solution

RuntimeError: Variable += value not supported. Use variable.assign_add(value) to modify the variable value and variable = variable + value to get a new Tensor object.

Issues in details


RuntimeError Traceback (most recent call last) in 6 loss_style = K.variable(0.) 7 for idx, layer_features in enumerate(feature_outputs): ----> 8 loss_style += style_loss(layer_features[1, :, :, :], layer_features[2, :, :, :]) * (0.5 ** idx) 9 10 loss_content /= 40

~/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in iadd(self, unused_other) 1220 1221 def iadd(self, unused_other): -> 1222 raise RuntimeError("Variable += value not supported. Use " 1223 "variable.assign_add(value) to modify the variable " 1224 "value and variable = variable + value to get a new "

RuntimeError: Variable += value not supported. Use variable.assign_add(value) to modify the variable value and variable = variable + value to get a new Tensor object.

Solutions

In [17]:

Change the lines of code:

for idx, layer_features in enumerate(feature_outputs):
    loss_style += style_loss(layer_features[1, :, :, :], layer_features[2, :, :, :]) * (0.5 ** idx)

to the following line of code:

for idx, layer_features in enumerate(feature_outputs):
    loss_style = loss + style_loss(layer_features[1, :, :, :], layer_features[2, :, :, :]) * (0.5 ** idx)

4. RuntimeError Issue and solution

In [22]:

Issue

RuntimeError Traceback (most recent call last) in 8 loss_style_winter = K.variable(0.) 9 for idx, layer_features in enumerate(feature_outputs): ---> 10 loss_style_summer += style_loss(layer_features[1, :, :, :], layer_features[-1, :, :, :]) * (0.5 ** idx) 11 loss_style_winter += style_loss(layer_features[2, :, :, :], layer_features[-1, :, :, :]) * (0.5 ** idx) 12

~/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in iadd(self, unused_other) 1220 1221 def iadd(self, unused_other): -> 1222 raise RuntimeError("Variable += value not supported. Use " 1223 "variable.assign_add(value) to modify the variable " 1224 "value and variable = variable + value to get a new "

RuntimeError: Variable += value not supported. Use variable.assign_add(value) to modify the variable value and variable = variable + value to get a new Tensor object.

Solution

Change the lines of code

for idx, layer_features in enumerate(feature_outputs):
    loss_style_summer += style_loss(layer_features[1, :, :, :], layer_features[-1, :, :, :]) * (0.5 ** idx)
    loss_style_winter += style_loss(layer_features[2, :, :, :], layer_features[-1, :, :, :]) * (0.5 ** idx)

to the following lines of code

for idx, layer_features in enumerate(feature_outputs):

    loss_style_summer = loss_style_summer + style_loss(layer_features[1, :, :, :], layer_features[-1, 
         :, :, :]) * (0.5 ** idx)
    loss_style_winter =  loss_style_winter + style_loss(layer_features[2, :, :, :], layer_features[-1, :, :,
         :]) * (0.5 ** idx)

mikechen66 avatar May 09 '20 00:05 mikechen66

With regard to In [30] and In [31], there is the following ValueError. I can not correct the ValueErrors at present.

ValueError: Cannot create an execution function which is comprised of elements from multiple graphs.

Code

In [30]

combined_evaluator = Evaluator(loss_total, combination_image, loss_content=loss_content, 
                               loss_variation=loss_variation, loss_style_summer=loss_style_summer,
                               loss_style_winter=loss_style_winter)
iterate = K.function([combination_image, summerness], 
                     combined_evaluator.iterate.outputs)

combined_evaluator.iterate = lambda inputs: iterate(inputs + [1.0]) 
res = run(combined_evaluator, preprocess_image(base_image_path), num_iter=50)

ValueError


ValueError Traceback (most recent call last) in 3 loss_style_winter=loss_style_winter) 4 iterate = K.function([combination_image, summerness], ----> 5 combined_evaluator.iterate.outputs) 6 7 combined_evaluator.iterate = lambda inputs: iterate(inputs + [1.0])

~/miniconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in function(inputs, outputs, updates, **kwargs) 3028 return tf_keras_backend.function(inputs, outputs, 3029 updates=updates, -> 3030 **kwargs) 3031 3032

~/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in function(inputs, outputs, updates, name, **kwargs) 3758 raise ValueError('Session keyword arguments are not support during ' 3759 'eager execution. You passed: %s' % (kwargs,)) -> 3760 return EagerExecutionFunction(inputs, outputs, updates=updates, name=name) 3761 3762 if kwargs:

~/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in init(self, inputs, outputs, updates, name) 3613 } 3614 if len(graphs) > 1: -> 3615 raise ValueError('Cannot create an execution function which is comprised ' 3616 'of elements from multiple graphs.') 3617

ValueError: Cannot create an execution function which is comprised of elements from multiple graphs.

Code

In [31]

path = 'style_transfer/summer_winter_%d.jpg'
def save(res, step):
    img = deprocess_image(res.copy(), h, w)
    imsave(path % step, img)

for step in range(1, 21):
    combined_evaluator = Evaluator(loss_total, combination_image, loss_content=loss_content, 
                                   loss_variation=loss_variation, loss_style_summer=loss_style_summer,
                                   loss_style_winter=loss_style_winter)
    iterate = K.function([combination_image, summerness], 
                         combined_evaluator.iterate.outputs)

    combined_evaluator.iterate = lambda inputs: iterate(inputs + [1.0 - step / 20.])
    res = run(combined_evaluator, preprocess_image(base_image_path), num_iter=50)
    save(res, step)

ValueError

ValueError Traceback (most recent call last) in 9 loss_style_winter=loss_style_winter) 10 iterate = K.function([combination_image, summerness], ---> 11 combined_evaluator.iterate.outputs) 12 13 combined_evaluator.iterate = lambda inputs: iterate(inputs + [1.0 - step / 20.])

~/miniconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in function(inputs, outputs, updates, **kwargs) 3028 return tf_keras_backend.function(inputs, outputs, 3029 updates=updates, -> 3030 **kwargs) 3031 3032

~/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in function(inputs, outputs, updates, name, **kwargs) 3758 raise ValueError('Session keyword arguments are not support during ' 3759 'eager execution. You passed: %s' % (kwargs,)) -> 3760 return EagerExecutionFunction(inputs, outputs, updates=updates, name=name) 3761 3762 if kwargs:

~/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in init(self, inputs, outputs, updates, name) 3613 } 3614 if len(graphs) > 1: -> 3615 raise ValueError('Cannot create an execution function which is comprised ' 3616 'of elements from multiple graphs.') 3617

ValueError: Cannot create an execution function which is comprised of elements from multiple graphs.

mikechen66 avatar May 09 '20 03:05 mikechen66