Reputation: 431
I am trying to implement the Xception model using Tensorflow 2.0. For reference , I am using the architecture from the screenshot below.
I was trying to implement the first Xception module in the Entry Flow portion of the neural network and I got an error with regards to channel size mis-match. The code is as below.
import tensorflow as tf
from tensorflow import keras
class Xception_entry_flow(keras.layers.Layer):
def __init__(self,filters_1,activation,size_1,**kwargs):
super().__init__(**kwargs)
self.main_layer = [
keras.layers.SeparableConv2D(filters = filters_1,kernel_size = size_1,activation = 'relu',padding='same'),
keras.layers.SeparableConv2D(filters = filters_1,kernel_size = size_1,activation = 'relu',padding='same'),
keras.layers.MaxPool2D(pool_size= 3,strides=2,padding='same')
]
self.skip_layer = [
keras.layers.Conv2D(filters = filters_1,kernel_size=1,strides=2,padding='same')
]
self.activation = keras.activations.get(activation)
def call(self,inputs):
Z = inputs
for layer in self.main_layer:
Z_1 = layer(Z)
print('Shape of Image after main layer')
print(Z_1.shape)
for layer in self.skip_layer:
Z_2 = layer(Z)
print('Shape after skip layer')
print(Z_2.shape)
return self.activation(Z_1+Z_2)
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(32,kernel_size=3,strides = 2,activation='relu',input_shape =[229,229,3]))
model.add(keras.layers.Conv2D(64,kernel_size=3,strides=1,activation='relu'))
model.add(Xception_entry_flow(filters_1=128,size_1=3,activation ='relu'))
After running the above code, I get the following error:
ValueError: Dimensions must be equal, but are 64 and 128 for 'xception_entry_flow_12/add' (op: 'AddV2') with input shapes: [?,56,56,64], [?,56,56,128].
The outputs of the main layer and the skip layer (Conv (1X1) and stride = 2X2) are as follows
Shape of Image after main layer
(None, 56, 56, 64)
Shape after skip layer
(None, 56, 56, 128)
I am not able to understand why the channel size reduces after applying keras.layers.MaxPool2D(pool_size= 3,strides=2,padding='same')
operation in the main layer.
My assumption is that the keras.layers.SeparableConv2D
operation accepts the filter size argument based on what the output channel size should be. Is my assumption about this function wrong?
The Tensorflow version I am using is as follows :
Tensorflow Version : 2.0.0
Thanks in advance!
Upvotes: 1
Views: 110
Reputation: 11333
It's a bug (i think), should be,
Z_1 = inputs
for layer in self.main_layer:
# You currently got layer(Z)
Z_1 = layer(Z_1)
Right now, you have Z
which is a (None, 112, 112, 64)
sized tensor (from last Conv2D
layer in the model
). You are actually applying MaxPool2D
on that instead of the output of the SeparableConv2D
layer of self.main_layers
.
Upvotes: 1