Caffe Convolution "Group" parameter conversion to Keras Conv2D

663 views
Skip to first unread message

pmpa...@gmail.com

unread,
Sep 6, 2018, 7:22:53 AM9/6/18
to Keras-users
I am trying to convert a Caffe-model (in prototxt) to its equivalent in Keras.

One convolutional layer has the following attributes :

layer {
  name: "conv2"
  type: "Convolution"
  bottom: "norm1"
  top: "conv2"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  convolution_param {
    num_output: 384
    kernel_size: 5
   group: 2
    stride: 2
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0.1
    }
  }
}

My question is how can I implement this parameter in Keras syntax ( using Conv2D layer ) ?

Sergey O.

unread,
Sep 6, 2018, 8:35:03 AM9/6/18
to pmpa...@gmail.com, Keras-users
group (g) [default 1]: If g > 1, we restrict the connectivity of each filter to a subset of the input. Specifically, the input and output channels are separated into g groups, and the iith output group channels will be only connected to the iith input group channels.

And from what I can tell we don't have an option in keras (nor tensorflow) to do this automatically:
https://github.com/tensorflow/tensorflow/pull/10482

But, given you are only splitting into 2 groups, it shouldn't be too hard to implement in keras.

You have two options:
1) split tensors into groups, do convolution on each and concatenate back together...
(this will likely be slower than option (2), but if this is your first layer (after input layer), you don't have much of a choice)...
A = Input(shape=(10,10))
B1 = Lambda(lambda x: x[...,0:5])(A)
B2 = Lambda(lambda x: x[...,5:10])(A)
C1 = Conv1D(5,2)(B1)
C2 = Conv1D(5,2)(B2)
C = Concatenate()([C1,C2])
model = Model(A,C)


2) if you know you'll be splitting the channels into groups, for your previous layer, generate parallel layers, apply conv to each and then concate.

For example, instead of doing:
A = Input(shape=(10,10))
B = Conv1D(10,2)(A)
C = Conv1D(10,2)(B)
model = Model(A,C)

Do:
A = Input(shape=(10,10))
B1 = Conv1D(5,2)(A)
B2 = Conv1D(5,2)(A)
C1 = Conv1D(5,2)(B1)
C2 = Conv1D(5,2)(B2)
C = Concatenate()([C1,C2])
model = Model(A,C)


--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/d2a08285-e964-4fb7-99a7-d56fa6ecdc50%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages