'how to add 'None' column to nparray

I am trying to use the tf-explain library, but I am running into an error with the validation data for the GradCAM method.

I get the following error:

WARNING:tensorflow:Model was constructed with shape (None, 100, 100, 3) for input Tensor("xception_input:0", shape=(None, 100, 100, 3), dtype=float32), but it was called on an input with incompatible shape (100, 100, 3, 1).
WARNING:tensorflow:Model was constructed with shape (None, 100, 100, 3) for input Tensor("input_1:0", shape=(None, 100, 100, 3), dtype=float32), but it was called on an input with incompatible shape (100, 100, 3, 1).

...

ValueError: number of input channels does not match corresponding dimension of filter, 1 != 3

This is after trying to manually add a dimension with:

img = tf.keras.preprocessing.image.load_img('../data/COVID-19_Radiography_Dataset/archive/COVID_all/COVID-1.png', target_size=(100, 100))
img = tf.keras.preprocessing.image.img_to_array(img)

img2 = img[..., np.newaxis]

explainer = GradCAM()

grid = explainer.explain((img2, None), self.model, class_index=covid_img_index, layer_name='dense_2')

where I would get the following error:

Traceback (most recent call last):
  File "/home/oskar/anaconda3/envs/masterenv/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py", line 930, in merge_with
    self.assert_same_rank(other)
  File "/home/oskar/anaconda3/envs/masterenv/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py", line 984, in assert_same_rank
    raise ValueError("Shapes %s and %s must have the same rank" %
ValueError: Shapes (100, 100, 3) and (None, None, None, None) must have the same rank

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/oskar/anaconda3/envs/masterenv/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py", line 1015, in with_rank
    return self.merge_with(unknown_shape(rank=rank))
  File "/home/oskar/anaconda3/envs/masterenv/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py", line 936, in merge_with
    raise ValueError("Shapes %s and %s are not compatible" % (self, other))
ValueError: Shapes (100, 100, 3) and (None, None, None, None) are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/oskar/anaconda3/envs/masterenv/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py", line 1039, in __init__
    input_shape.with_rank(num_spatial_dims + 2)
  File "/home/oskar/anaconda3/envs/masterenv/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py", line 1017, in with_rank
    raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (100, 100, 3) must have rank 4

...

ValueError: input tensor must have rank 4

So, to me it seems I have to add a null dimension? To adhere to the expected (None, 100, 100, 3) shape? When I add it as described above, it does not work. So is there a way to make a shape (None, 100, 100, 3) out of np.array with shape (100, 100, 3) ?

Please let me know if there is any additional data that would be useful to better explain the problem.



Solution 1:[1]

Please change the

img2 = img[..., np.newaxis]

to

img2 = img[np.newaxis ,...]

So that it will not change the channel size from 3 to 1.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mojtaba Abdi Kh.