tensorflow / lucid

A collection of infrastructure and tools for research in neural network interpretability.
Apache License 2.0
4.66k stars 655 forks source link

issue in render own model #253

Open Edw2n opened 4 years ago

Edw2n commented 4 years ago

I'm trying to follow this link [https://github.com/tensorflow/lucid/wiki/Importing-Models-into-Lucid]

And I have error in feature visualization of a layer with lucid render.render_vis(model, "Conv2D_3:0")

What is the proper parameter in my case?

I checked my node name with this code

for node in model.graph_def.node:
      print(node.name)

And results are below.

Const
Variable
Variable_1
Variable_2
Variable_3
Variable_4
Variable_5
Variable_6
Variable_7
Variable_8
Variable_9
Variable_10
Variable_11
Conv2D_3
add_6
Relu_5
MaxPool_3
Conv2D_4
add_7
Relu_6
MaxPool_4
Conv2D_5
add_8
Relu_7
MaxPool_5
Reshape_1/shape
Reshape_1
MatMul_3
add_9
Relu_8
MatMul_4
add_10
Relu_9
MatMul_5
add_11
Softmax
lucid_metadata_json

Could you help me how can I solve this problem? :)

colah commented 4 years ago

What error do you get when you call render?

Edw2n commented 4 years ago

@colah The error message is as follows:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-5878bc10721e> in <module>()
      3 #_ = render.render_vis(model, obj)
      4 
----> 5 _ = render.render_vis(model,"Softmax:0")

6 frames
/usr/local/lib/python3.6/dist-packages/lucid/optvis/render.py in render_vis(model, objective_f, param_f, optimizer, transforms, thresholds, print_objectives, verbose, relu_gradient_override, use_fixed_seed)
     93 
     94     T = make_vis_T(model, objective_f, param_f, optimizer, transforms,
---> 95                    relu_gradient_override)
     96     print_objective_func = make_print_objective_func(print_objectives, T)
     97     loss, vis_op, t_image = T("loss"), T("vis_op"), T("input")

/usr/local/lib/python3.6/dist-packages/lucid/optvis/render.py in make_vis_T(model, objective_f, param_f, optimizer, transforms, relu_gradient_override)
    175     with gradient_override_map({'Relu': redirected_relu_grad,
    176                                 'Relu6': redirected_relu6_grad}):
--> 177       T = import_model(model, transform_f(t_image), t_image)
    178   else:
    179     T = import_model(model, transform_f(t_image), t_image)

/usr/local/lib/python3.6/dist-packages/lucid/optvis/render.py in import_model(model, t_image, t_image_raw, scope, input_map)
    255     t_image_raw = t_image
    256 
--> 257   T_ = model.import_graph(t_image, scope=scope, forget_xy_shape=True, input_map=input_map)
    258 
    259   def T(layer):

/usr/local/lib/python3.6/dist-packages/lucid/modelzoo/vision_base.py in import_graph(self, t_input, scope, forget_xy_shape, input_map)
    199       final_input_map.update(input_map)
    200     tf.import_graph_def(
--> 201         self.graph_def, final_input_map, name=scope)
    202     self.post_import(scope)
    203 

/tensorflow-1.15.2/python3.6/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/importer.py in import_graph_def(graph_def, input_map, return_elements, name, op_dict, producer_op_list)
    403       name=name,
    404       op_dict=op_dict,
--> 405       producer_op_list=producer_op_list)
    406 
    407 

/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/importer.py in _import_graph_def_internal(graph_def, input_map, return_elements, validate_colocation_constraints, name, op_dict, producer_op_list)
    533     raise ValueError(
    534         'Attempted to map inputs that were not found in graph_def: [%s]' %
--> 535         ', '.join(missing_unused_input_keys))
    536 
    537   if return_elements is None:

**ValueError: Attempted to map inputs that were not found in graph_def: [input:0]**
colah commented 4 years ago

Attempted to map inputs that were not found in graph_def: [input:0]

When you exported your model, you had to declare what the name of the input node to your model was. It appears that you may have made an error when doing so, and that the name you gave doens't exist in the model.

Edw2n commented 4 years ago

@colah , Thanks to your answer, I solved the problem. However, I have one more problem in rendering my model.

Here is my error message

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/importer.py in _import_graph_def_internal(graph_def, input_map, return_elements, validate_colocation_constraints, name, op_dict, producer_op_list)
    500         results = c_api.TF_GraphImportGraphDefWithResults(
--> 501             graph._c_graph, serialized, options)  # pylint: disable=protected-access
    502         results = c_api_util.ScopedTFImportGraphDefResults(results)

InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'import/max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [1,32,?,1].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
7 frames
/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/importer.py in _import_graph_def_internal(graph_def, input_map, return_elements, validate_colocation_constraints, name, op_dict, producer_op_list)
    503       except errors.InvalidArgumentError as e:
    504         # Convert to ValueError for backwards compatibility.
--> 505         raise ValueError(str(e))
    506 
    507     # Create _DefinedFunctions for any imported functions.

ValueError: Negative dimension size caused by subtracting 2 from 1 for 'import/max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [1,32,?,1].

The link below is the colab notebook where the problem occurred. https://colab.research.google.com/drive/1UPtYvjM_qurK1SwkfNa-4cekb07Nz93-?usp=sharing

And also, I attached another colab notebook about building own model, so please refer to it if necessary. https://colab.research.google.com/drive/1CSRftnEDQiyXIlMwWrfWNgHQcdvUPCtH?usp=sharing

How can I solve this part?

colah commented 4 years ago

When you run feature visualization, you optimize an image of size set by the argument param_f. Some models are incompatible with certain input sizes. Try doing something like:

render.render_vis(model, "conv2d_2/convolution:3", 
                               param_f=lambda: param.image(MODEL_INPUT_WIDTH))
YangLiji commented 4 years ago

@colah , Thanks to your answer, I solved the problem. However, I have one more problem in rendering my model.

Here is my error message

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/importer.py in _import_graph_def_internal(graph_def, input_map, return_elements, validate_colocation_constraints, name, op_dict, producer_op_list)
    500         results = c_api.TF_GraphImportGraphDefWithResults(
--> 501             graph._c_graph, serialized, options)  # pylint: disable=protected-access
    502         results = c_api_util.ScopedTFImportGraphDefResults(results)

InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'import/max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [1,32,?,1].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
7 frames
/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/importer.py in _import_graph_def_internal(graph_def, input_map, return_elements, validate_colocation_constraints, name, op_dict, producer_op_list)
    503       except errors.InvalidArgumentError as e:
    504         # Convert to ValueError for backwards compatibility.
--> 505         raise ValueError(str(e))
    506 
    507     # Create _DefinedFunctions for any imported functions.

ValueError: Negative dimension size caused by subtracting 2 from 1 for 'import/max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [1,32,?,1].

The link below is the colab notebook where the problem occurred. https://colab.research.google.com/drive/1UPtYvjM_qurK1SwkfNa-4cekb07Nz93-?usp=sharing

And also, I attached another colab notebook about building own model, so please refer to it if necessary. https://colab.research.google.com/drive/1CSRftnEDQiyXIlMwWrfWNgHQcdvUPCtH?usp=sharing

How can I solve this part?

hello, have you solved this problem?

YangLiji commented 4 years ago

When you run feature visualization, you optimize an image of size set by the argument param_f. Some models are incompatible with certain input sizes. Try doing something like:

render.render_vis(model, "conv2d_2/convolution:3", 
                               param_f=lambda: param.image(MODEL_INPUT_WIDTH))

hello, I try this, but it doesn't work.

paramf = lambda: param.image(128) = render.render_vis(model, "73/BiasAdd:2", param_f)