
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can Tensorflow be used to create a convolutional base using Python?
A convolutional neural network would generally consist of combination of the following layers: Convolutional layers, Pooling layers and Dense layers.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
Convolutional neural networks have been used to produce great results for a specific kind of problems, such as image recognition. It can be created using the ‘Sequential’ method which is present in the ‘models’ class. Layers can be added to this convolutional network using the ‘add’ method.
We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.
We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.
print("Creating the convolutional base") model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) print("Description of arhcitecture is") model.summary()
Code credit: https://www.tensorflow.org/tutorials/images/cnn
Output
Creating the convolutional base Description of arhcitecture is Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 30, 30, 32) 896 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 13, 13, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 4, 4, 64) 36928 ================================================================= Total params: 56,320 Trainable params: 56,320 Non-trainable params: 0
Explanation
The above lines help define the convolutional base using a common pattern.
This pattern is a stack of Conv2D and MaxPooling2D layers.
As input, which is a CNN takes tensors of shape (image_height, image_width, color_channels), is taken.
CNN is configured to process inputs of shape (32, 32, 3), which is the format of CIFAR images.
This can be done by passing the argument input_shape to our first layer.
The output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels).
The width and height dimensions shrink as the network gets deeper.
The number of output channels for every Conv2D layer is controlled by the first argument (e.g., 32 or 64).
- Related Articles
- How can Tensorflow be used to build a one dimensional convolutional network using Python?
- How can Tensorflow be used to compare the linear model and the Convolutional model using Python?
- How can Tensorflow be used to create a feature extractor using Python?
- How can Tensorflow be used to create a sequential model using Python?
- How can TensorFlow be used to create a tensor and display a message using Python?
- How can Tensorflow be used to compose layers using Python?
- How can Tensorflow be used to attach a classification head using Python?
- How can Tensorflow be used to evaluate a CNN model using Python?
- How can Tensorflow be used to add two matrices using Python?
- How can Tensorflow be used to multiply two matrices using Python?
- How can Tensorflow be used to visualize training results using Python?
- How can Tensorflow be used to instantiate an estimator using Python?
- How can Tensorflow be used to decode the predictions using Python?
- How can Tensorflow be used to build normalization layer using Python?
- How can Tensorflow be used to check the predicrion using Python?
