0

# Get the attention scores for the specific image and layer
attention_scores = attention_model.predict(normalized_image[np.newaxis, ...])

# Normalize the attention scores to [0, 1]
normalized_attention_scores = attention_scores / np.max(attention_scores)

# Resize the attention scores to match the original image dimensions
resized_attention_scores = cv2.resize(normalized_attention_scores[0, :, :, 0], (image_array.shape[1], image_array.shape[0]))


the output is

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-0f5e34480f96> in <module>
     64 
     65 # Normalize the attention scores to [0, 1]
---> 66 normalized_attention_scores = attention_scores / np.max(attention_scores)
     67 
     68 # Resize the attention scores to match the original image dimensions

<__array_function__ internals> in amax(*args, **kwargs)

~/.local/lib/python3.7/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial, where)
   2753     """
   2754     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
-> 2755                           keepdims=keepdims, initial=initial, where=where)
   2756 
   2757 

~/.local/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     84                 return reduction(axis=axis, out=out, **passkwargs)
     85 
---> 86     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     87 
     88 

ValueError: could not broadcast input array from shape (64,64) into shape (1,)

I've tried the varies way and searched tons of methods to try to solve it but since I am using the Keras multi-head attention in my VIT model and the model is following this reference: https://keras.io/examples/vision/image_classification_with_vision_transformer/ which is not the VIT-Dino. I'm a little bit confused about how to solve this problem.

Please anyone can help me or give me any direction to solve it, grateful thanks!!!!!

  • What is the attention_scores shape? The error is from np.max(), so would you some printings there to check the passed/returned values in each line. – user2586955 Aug 15 '23 at 12:14

0 Answers0