Call to conv2d failing in model subclassing

1,116 views
Skip to first unread message

Aakash Kumar Nain

unread,
May 16, 2019, 8:28:01 AM5/16/19
to TensorFlow Community Testing, TensorFlow Developers
I am trying to build a model by `subclassing` tge tf.keras.Model. Here is what my code looks like:
class CustomModel(tf.keras.Model):
  def __init__(self):
    super(CustomModel, self).__init__()
  
    self.conv1   = Conv2D(32, (3, 3), padding='same')
    self.conv2   = Conv2D(64, (3, 3), padding='same')
    self.pool    = MaxPooling2D(pool_size=(2, 2))
    self.bn      = BatchNormalization()
    self.relu    = Activation("relu")
    self.softmax = Activation("softmax")
    self.drop1   = Dropout(0.25)
    self.drop2   = Dropout(0.5)
    self.dense1  = Dense(512)
    self.dense2  = Dense(10)
    self.flat    = Flatten()
    
  
  
  def call(self, inputs, train):
    z = self.conv1(inputs)
    z = self.bn(z, training=train)
    z = self.relu(z)
    
    z = self.conv1(z)
    z = self.bn(z, training=train)
    z = self.relu(z)
    z = self.pool(z)
    z = self.drop1(z, training=train)
    
    z = self.conv2(z)
    z = self.bn(z, training=train)
    z = self.relu(z)
    
    z = self.conv2(z)
    z = self.bn(z, training=train)
    z = self.relu(z)
    z = self.pool(z)
    z = self.drop1(z, training=train)
    
    z = self.flat(z)
    z = self.dense1(z)
    z = self.relu(z)
    z = self.drop2(z, training=train)
    z = self.dense2(z)
    z = self.softmax(z)
    
    return z

When I am calling my model on some random input to check if it is working fine, it is failing on the first conv layer. Here is what I am doing: 
random_input = np.random.rand(32,32, 3).astype(np.float32)
random_input = np.expand_dims(random_input, axis=0)
preds = model(random_input, train=False)
print(preds)

I am getting this error:
InvalidArgumentError: input depth must be evenly divisible by filter depth: 32 vs 3 [Op:Conv2D]

Can someone please tell me what is going on here?

Regards,
Aakash Nain 
 
 

Aakash Kumar Nain

unread,
May 16, 2019, 8:54:29 AM5/16/19
to Abdechafi Neji, TensorFlow Community Testing, TensorFlow Developers
I don't agree. Number of filters in a conv layer has no relation with the input depth. Also, the same model when defined using `Sequential` or `Functional` api works fine. I suspect this is something related to the call to `build` method.

On Thu 16 May, 2019, 6:15 PM Abdechafi Neji, <neji.abdec...@gmail.com> wrote:
The error is very clear , you shoul have input depth that divisible by filter 32 , this error i get when i use yolo darknet to train my dataset , you should have ( filters mod 32 ==0)

--
You received this message because you are subscribed to the Google Groups "TensorFlow Community Testing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testing+u...@tensorflow.org.


--
-- 
Cordialement
________________________________________________________
Neji Abdechafi
Développeur informatique

Adresse: Route  Ellaba Km 4  Medenine - 4100 - TUNISIE.
Tél: 00 216 54 60 46 97
Mail: neji.abdec...@gmail.com

Francesco Tonolini

unread,
Mar 31, 2020, 9:38:17 AM3/31/20
to TensorFlow Community Testing, neji.abdec...@gmail.com, devel...@tensorflow.org
I have the same problem with tf.nn.conv2D. Has this issue been resolved?


Il giorno giovedì 16 maggio 2019 13:54:29 UTC+1, Aakash Kumar Nain ha scritto:
I don't agree. Number of filters in a conv layer has no relation with the input depth. Also, the same model when defined using `Sequential` or `Functional` api works fine. I suspect this is something related to the call to `build` method.

To unsubscribe from this group and stop receiving emails from it, send an email to tes...@tensorflow.org.


--
-- 
Cordialement
________________________________________________________
Neji Abdechafi
Développeur informatique

Adresse: Route  Ellaba Km 4  Medenine - 4100 - TUNISIE.
Tél: 00 216 54 60 46 97
Reply all
Reply to author
Forward
0 new messages