Hi everybody,
I would like to convert a pre-trained Caffe model to Keras format. If I understood correctly I have to extract the output shape and the weights of the Caffe model, build a model in Keras with the same shape and then upload the weights.
This is the shape and weights of the pretrained model
The output shape of layer data is (3, 400, 400) The output shape of layer conv1 is (64, 98, 98) The output shape of layer norm1 is (64, 98, 98) The output shape of layer pool1 is (64, 49, 49) The output shape of layer conv2 is (256, 49, 49) The output shape of layer norm2 is (256, 49, 49) The output shape of layer pool2 is (256, 24, 24) The output shape of layer conv3 is (256, 24, 24) The output shape of layer conv4 is (256, 24, 24) The output shape of layer conv5 is (256, 24, 24) The output shape of layer pool5 is (256, 12, 12) The output shape of layer conv6 is (4096, 2, 2) The output shape of layer conv7 is (4096, 2, 2) The output shape of layer conv8 is (3, 2, 2) The output shape of layer pool6 is (3, 1, 1) The output shape of layer prob is (3, 1, 1) conv1 (64, 3, 11, 11) (64,) conv2 (256, 64, 5, 5) (256,) conv3 (256, 256, 3, 3) (256,) conv4 (256, 256, 3, 3) (256,) conv5 (256, 256, 3, 3) (256,) conv6 (4096, 256, 6, 6) (4096,) conv7 (4096, 4096, 1, 1) (4096,) conv8 (3, 4096, 1, 1) (3,)
This is the shape I programmed in Keras using as reference this link
data = Input(shape=(3,400,400), name='DATA') conv1 = Convolution2D(64,11,11, subsample=(2,2))(data) conv1 = Activation('relu', name='CONV1')(conv1) norm1= BatchNormalization(name='NORM1')(conv1) pool1 = MaxPooling2D(pool_size=(3,3), strides=(2,2), name= "POOL1")(norm1) conv2 = Convolution2D(256,5,5)(pool1) conv2 = Activation('relu', name='CONV2')(conv2) norm2= BatchNormalization(name='NORM2')(conv2) pool2 = MaxPooling2D(pool_size=(3,3),strides=(2,2), name='POOL2')(norm2) conv3 = Convolution2D(256,3,3)(pool2) conv3 = Activation('relu', name='CONV3')(conv3) conv4 = Convolution2D(256,3,3)(conv3) conv4 = Activation('relu', name='CONV4')(conv4) conv5 = Convolution2D(256,3,3)(conv4) conv5 = Activation('relu', name='CONV5')(conv5) pool5 = MaxPooling2D(pool_size=(3,3),strides=(2,2), name='POOL5')(conv5) conv6 = Convolution2D(4096,6,6)(pool5) conv6 = Activation('relu', name='CONV6')(conv6) conv7 = Convolution2D(4096,1,1)(conv6) conv7 = Activation('relu', name='CONV7')(conv7) conv8 = Convolution2D(3,1,1)(conv7) conv8 = Activation('relu', name='CONV8')(conv8) pool6 = MaxPooling2D(pool_size=(3,3),strides=(2,2), name='POOL6')(conv8) prob = Activation('softmax', name='PROB')(pool6) model = Model(input=[data],output=[prob]) model.compile(optimizer='adam', loss='categorical_crossentropy')
But the model.summary() looks very different from the original I don't understand how to obtain the exact same shape.
The source of information of the pre-trained model is https://github.com/nealjean/predicting-poverty
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= DATA (InputLayer) (None, 3, 400, 400) 0 _________________________________________________________________ conv2d_7 (Conv2D) (None, 64, 195, 195) 23296 _________________________________________________________________ CONV1 (Activation) (None, 64, 195, 195) 0 _________________________________________________________________ NORM1 (BatchNormalization) (None, 64, 195, 195) 780 _________________________________________________________________ POOL1 (MaxPooling2D) (None, 64, 97, 97) 0 _________________________________________________________________ conv2d_8 (Conv2D) (None, 256, 93, 93) 409856 _________________________________________________________________ CONV2 (Activation) (None, 256, 93, 93) 0 _________________________________________________________________ POOL2 (MaxPooling2D) (None, 256, 46, 46) 0 _________________________________________________________________ conv2d_9 (Conv2D) (None, 256, 44, 44) 590080 _________________________________________________________________ CONV3 (Activation) (None, 256, 44, 44) 0 _________________________________________________________________ conv2d_10 (Conv2D) (None, 256, 42, 42) 590080 _________________________________________________________________ CONV4 (Activation) (None, 256, 42, 42) 0 _________________________________________________________________ conv2d_11 (Conv2D) (None, 256, 40, 40) 590080 _________________________________________________________________ CONV5 (Activation) (None, 256, 40, 40) 0 _________________________________________________________________ POOL5 (MaxPooling2D) (None, 256, 13, 13) 0 _________________________________________________________________ conv2d_12 (Conv2D) (None, 4096, 8, 8) 37752832 _________________________________________________________________ CONV6 (Activation) (None, 4096, 8, 8) 0