I want to create a conv2d layer by explicitly defining the weight matrix of the layer (Conv2D has use_bias parameter set to False). I have been trying to do this using layer.set_weights([K]) where K is (?, 7, 7, 512, 512) Tensor.
In simple Tensorflow API, it could probably be done by passing the tensor the filter parameter in tf.nn.conv2d(input, filter,..)
Moreover, I have more problem and that i show should I address the batch dimension in the K Tensor because it has been generated by a network
Basically I want to implement a hypernetwork in which i have generated the weights for the Conv2D layer specified above, from another network in the Tensor K. The weight Tensor K has shape [height, width, Filters, Channels]
template= Input(shape=(448,684,3))
hyper_net= VGG16(weights='imagenet', include_top=False,
input_tensor=None, input_shape=(448,684, 3))
k1= hyper_net(template)
kconv1= hyper_net.get_layer(name='block5_conv1')
config_conv1= kconv1.get_config()
k1conv1 = Conv2D.from_config(config_conv1)(k1)
kconv2= hyper_net.get_layer(name='block5_conv2')
config_conv2= kconv2.get_config()
k1conv2 = Conv2D.from_config(config_conv2)(k1conv1)
k1pool1= MaxPooling2D(pool_size=(2,3))(k1conv2)
k1pool1= Reshape((7,7,512,1))(k1pool1)
print(k1pool1.shape)
K= Conv3D(512, (1,1,1), strides=(1, 1, 1), padding='valid',
activation=None, use_bias=True, kernel_initializer='he_normal', bias_initializer='zeros', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01))(k1pool1)
ortho= Input(tensor=tf.convert_to_tensor(O))
base_model = VGG16(weights='imagenet', include_top=False,
input_tensor=None, input_shape=(1760, 1760, 3))
o1= base_model(ortho)
Oconv1= Conv2D(512, (7, 7), activation='relu', padding='valid',use_bias=False)
Oconv1.set_weights([K])
It gives error as:
ValueError: You called `set_weights(weights)` on layer "conv2d_4" with a weight list of length 1, but the layer was expecting 0 weights. Provided weights: [<tf.Tensor 'conv3d_9/add:0' shape=(?, 7, 7, 512, ...
In Tensorflow 2.0 with eager execution, you might be able to do 1 of the below:
1) You can call the build
method on Oconv1
before using the set_weights
method. You are getting the ValueError
as the weights Variables in the layer are not yet initialized, hence the layer cannot take in any weights via set_weights
before building.
Oconv1= Conv2D(512, (7, 7), activation='relu', padding='valid',use_bias=False)
input_shape = tf.TensorShape([None, h, w, c]) # to define h, w, c based on shape of layer input
Oconv1.build(input_shape)
Oconv1.set_weights([K])
2) You can also pass in a weights
kwarg into the Conv2D
constructor.
Oconv1= Conv2D(512, (7, 7), activation='relu', padding='valid',use_bias=False,weights=[K])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With