In TensorFlow for 2D convolution we have:
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,
data_format=None, name=None)
and
tf.contrib.layers.convolution2d(*args, **kwargs)
Thank you
Keras Conv2D is a 2D Convolution Layer, this layer creates a convolution kernel that is wind with layers input which helps produce a tensor of outputs.
Class Conv2D. Inherits From: Conv2D , Layer. Defined in tensorflow/python/layers/convolutional.py . 2D convolution layer (e.g. spatial convolution over images). This layer creates a convolution kernel that is convolved (actually cross-correlated) with the layer input to produce a tensor of outputs.
Figure 1: The Keras Conv2D parameter, filters determines the number of kernels to convolve with the input volume. Each of these operations produces a 2D activation map. The first required Conv2D parameter is the number of filters that the convolutional layer will learn.
kernel_size: An integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. strides: An integer or tuple/list of 2 integers, specifying the strides of the convolution along the height and width.
tf.nn.conv2d(...)
is the core, low-level convolution functionality provided by TensorFlow. tf.contrib.layers.conv2d(...)
is part of a higher-level API build around core-TensorFlow.
Note, that in current TensorFlow versions, parts of layers are now in core, too, e.g. tf.layers.conv2d
.
The difference is simply, that tf.nn.conv2d
is an op, that does convolution, nothing else. tf.layers.conv2d
does more, e.g. it also creates variables for the kernel and the biases amongst other things.
Check out the Tensorflow Tutorial on CNNs which uses Tensorflow core (here). With the low-level API the convolutional layers are created like this:
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
Compare that to the TF Layers Tutorial for CNNs (here). With TF Layers convolutional layers are create like this:
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
Without knowing your use case: Most likely you want to use tf.layers.conv2d
.
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