sicara / tf-explain

Interpretability Methods for tf.keras models with Tensorflow 2.x
https://tf-explain.readthedocs.io
MIT License
1.02k stars 111 forks source link

Challenge: Working with tfrecords #132

Closed johnny-mueller closed 4 years ago

johnny-mueller commented 4 years ago

I have a problem with the input. I have built a model based on the Yolov3 structure that uses tfrecords as input. Currently I have the requirement that I cannot pass a numpy array. Naive I tried to use tfrecords as input but did not get any useful results.

I get the following error message:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_7:0", shape=(None, 26, 26, 256), dtype=float32) at layer "input_7". The following previous layers 
were accessed without issue: []

Here is the corresponding code I used for this. The loading and preprocessing is based on the preprocessing before training the model

train_dataset = dataset.load_fake_dataset()
train_dataset = dataset.load_tfrecord_dataset(dataset_path, class_names_path, image_size)
train_dataset = train_dataset.batch(batch_size)
train_dataset = train_dataset.map(lambda x, y: (dataset.transform_images(x, image_size), dataset.transform_targets(y, anchors, anchor_masks, image_size)))

explainer = tf_explain.core.GradCAM()
grid = explainer.explain((train_dataset, None), model, class_index=20)
explainer.save(grid, ".", "grad_cam.png")

Is there a workaround or approach how I can approach this?

RaphaelMeudec commented 4 years ago

@johnny-mueller tf-explain does not support (yet) tf.data inputs, only numpy array. So a workaround is to use .take on your dataset and pass it to the explainer as numpy arrays. Supporting tf.data.Dataset is on the roadmap, will comment on this issue once it's done!

johnny-mueller commented 4 years ago

@RaphaelMeudec Thanks for the workaround. I just implemented it and it seems to work. Nevertheless I currently have problems with the input layer.


ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [1, 1, 416, 416, 3]