Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between convolution2d and conv2d in tensorflow in terms of ussage

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)
  • I am not sure about differences?
  • I know that I should use the first one if I want to use a special filter, right? But what else? Especially about outputs?

Thank you

like image 399
Sesoin Avatar asked Apr 24 '17 11:04

Sesoin


People also ask

What is Conv2D used for?

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.

What is Tensorflow Conv2D?

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.

How does keras Conv2D work?

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.

What is kernel size in Conv2D?

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.


1 Answers

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.

like image 117
thertweck Avatar answered Sep 21 '22 07:09

thertweck