Michael Lempart
Michael Lempart

Reputation: 51

Setting filter weights of a convolutional layer

Im working on a semantic segmentation project which involves dynamic filters in order to learn multiscale representations.

To create these filters I use a Unet backbone and extract the feature maps from the bottleneck layer. The feature maps are of size H x W X 512, where H is the height of the feature map, W the width and 512 is the number of channels (maps).

These features are passed to a 1x1 convolution to reduce the amount of filters to H X W X 128 and the features are also passed to an adaptive pooling layer to reduce H X W X 512 to k x k x 512, where k is the size of the filter (i.ex. 5). The filter is then also fed through a 1 x 1 convolution to reduce it to 128.

This gives me a feature map f = H x W x 128 and a filter kernel g of size k x k x 128.

Now I want to convolve f with g and tried the following in keras:

conv = Conv2D(128, kernel_size = 5, kernel_initializer = g, trainable = False)(f)

Unfortunately this does not work and I just get an error saying:

"Could not interpret initializer identifier: Tensor("strided_slice:0", shape = (5,5,128), dtype = float32)"

Now Iam wondering what Iam doing wrong?

In addition I have to mention that the shape of the output tnesor after average pooling /1x1 conv is (? , 5, 5, 128), where ? is the batch size. The get the kernel I tried something like:

g = g[0,:,:,:]

Thanks for any advice,

cheers,

Michael

Upvotes: 0

Views: 432

Answers (1)

Lescurel
Lescurel

Reputation: 11631

The kernel_initializer argument of the constructor of Conv2D does not expect a kernel, but a function that would initialize a kernel. You can read more in the documentation

If you just want to perform a convolution without trainable weights, you are better off using the tensorflow native function tf.nn.conv2d :

conv = tf.nn.conv2d(f,g,strides=[1,1,1,1],padding='VALID')

Upvotes: 1

Related Questions