'Error:Input to reshape is a tensor with 409600 values, but the requested shape requires a multiple of 25088 [[{{node pam_3/Reshape_1}}]]
I am trying to apply channel attention and position attention layer on last convolutional layer of VGG16. But stuck badly at the above error. I am new to coding in python and deep learning. I am confused about shape values in Class CAM and class PAM. Here is my code:
from keras import initializers
from keras import regularizers
from keras import constraints
class PAM(Layer):**strong text**
def __init__(self,
gamma_initializer=tf.zeros_initializer(),
gamma_regularizer=None,
gamma_constraint=None,
**kwargs):
super(PAM, self).__init__(**kwargs)
self.gamma_initializer = gamma_initializer
self.gamma_regularizer = gamma_regularizer
self.gamma_constraint = gamma_constraint
def build(self, input_shape):
self.gamma = self.add_weight(shape=(512,),
initializer=self.gamma_initializer,
name='gamma',
regularizer=self.gamma_regularizer,
constraint=self.gamma_constraint)
self.built = True
def compute_output_shape(self, input_shape):
return input_shape
def call(self, input):
input_shape = input.get_shape().as_list()
_, h, w, filters = input_shape
b = Conv2D(512, 3, use_bias=False, kernel_initializer='he_normal')(att_input)
c = Conv2D(512, 3, use_bias=False, kernel_initializer='he_normal')(att_input)
d = Conv2D(512, 3, use_bias=False, kernel_initializer='he_normal')(att_input)
vec_b = K.reshape(b, (-1, h * w, 512))
vec_cT = tf.transpose(K.reshape(c, (-1, h * w,512)), (0, 2, 1))
bcT = K.batch_dot(vec_b, vec_cT)
softmax_bcT = Activation('softmax')(bcT)
vec_d = K.reshape(d, (-1, h * w, 512))
bcTd = K.batch_dot(softmax_bcT, vec_d)
bcTd = K.reshape(bcTd, (-1, h, w, 512))
out = self.gamma*bcTd + att_input
return out
class CAM(Layer):
def __init__(self,
gamma_initializer=tf.zeros_initializer(),
gamma_regularizer=None,
gamma_constraint=None,
**kwargs):
super(CAM, self).__init__(**kwargs)
self.gamma_initializer = gamma_initializer
self.gamma_regularizer = gamma_regularizer
self.gamma_constraint = gamma_constraint
def build(self, input_shape):
self.gamma = self.add_weight(shape=(512,),
initializer=self.gamma_initializer,
name='gamma',
regularizer=self.gamma_regularizer,
constraint=self.gamma_constraint)
self.built = True
def compute_output_shape(self, input_shape):
return input_shape
def call(self, input):
input_shape = input.get_shape().as_list()
_, h, w, filters = input_shape
vec_a = K.reshape(input, (-1, h * w, 512))
vec_aT = tf.transpose(vec_a, (0, 2, 1))
aTa = K.batch_dot(vec_aT, vec_a)
softmax_aTa = Activation('softmax')(aTa)
aaTa = K.batch_dot(vec_a, softmax_aTa)
aaTa = K.reshape(aaTa, (-1, h, w, 512))
out = self.gamma*aaTa + att_input
return out
pam = PAM()(att_input)
pam = Conv2D(512, 3, padding='same', use_bias=False, kernel_initializer='he_normal')(pam)
pam = BatchNormalization(axis=3)(pam)
pam = Activation('relu')(pam)
pam = Dropout(0.5)(pam)
pam = Conv2D(512, 3, padding='same', use_bias=False, kernel_initializer='he_normal')(pam)
cam = CAM()(att_input)
cam = Conv2D(512, 3, padding='same', use_bias=False, kernel_initializer='he_normal')(cam)
cam = BatchNormalization(axis=3)(cam)
cam = Activation('relu')(cam)
cam = Dropout(0.5)(cam)
cam = Conv2D(512, 3, padding='same', use_bias=False, kernel_initializer='he_normal')(cam)
Solution 1:[1]
Make sure you have the input dimensions correct. Can't say exactly where the error is without looking at your code but whenever I've had an error like that it has almost always been a case where I overlooked one of the dimensions. Think carefully about how your input changes through the layers. Printing out the model summary might help. Good luck
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 | pickplatespiral |
