Skip to main content

CNN Input shape for Deep Learning Frameworks

In Deep learning use cases images are represented as 3-D tensors (for colored images) and 2-D tensors (for gray scale images). Images mainly have three attributes: height, width, channels. Different deep learning frameworks expect these attributes to be specified in different order as per respective frameworks. In this post we are going to discuss the formats in which popular deep learning frameworks expect these attributes to be specified.

Currently there are many deep learning frameworks in the market like: 

1. Keras (for Python): Keras is a deep learning framework for Python. Keras is a wrapper around numerical computing libraries to provide user an easy interface to code Deep Learning networks. As a backend Keras could use:
    a. Tensorflow
    b. Theano

2. DL4J (for Java/Scala): DL4J is a deep learning framework written in Java. It could be used with Java as well scala programming languages.

Image Structure in CNN:

CNN (Convolutional Neural Networks) are neural network architectures mainly used for image based tasks. In a CNN a color image is represented as:
Fig 1. Image structure in CNN.




Above figure represents structure of an image. A colored image is represented as having three attributes:
    a. Height of image (in pixels)
    b. Width of image (in pixels)
    c. Channels
Here height and width are self explanatory. Channels represent the number of channels for image. Channels could have following values:
    a. 3 : If image is colored then there are 3 channels in the image (Red, Green, Blue).
    b. 1 : If image is gray scale then number of channels is 1.

Input shape for CNN layer in Deep Learning Frameworks: 

In different frameworks the 3 attributes of an image could appear in different ordering thus creating a confusing situation for developer. This post summarizes the attribute ordering for different frameworks:

1. Keras (with Tensorflow backend): If we are using Keras and Tensorflow is our backend then shape of input image is:
(height, width, channels)
#(Python code)
# Create the model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(height, width,  
      channels), padding='same', activation='relu', kernel_constraint=maxnorm(3)))

Highlighted section demonstrates the order in which attributes of image are to be listed in Keras (with Tensorflow backend).

2. Keras (with Theano backend): If we are using Keras and Theano is our backend then the shape of input image is:
(channels, height, width)
#(Python code)
# Create the model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(channels, height,  
      width), padding='same', activation='relu', kernel_constraint=maxnorm(3)))

Highlighted section demonstrates the order in which attributes of image are to be listed in Keras (with Theano backend).

3. DL4J: In DL4J while creating MultiLayerConfiguration the input image is represented as:
(height, width, channels) 
// (Java code)
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(seed)
            .iterations(iterations)
            ........
            ........
            .backprop(true).pretrain(false)
            .setInputType(InputType.convolutional(height, width, channels))

Highlighted section demonstrates the order in which attributes of image are to be listed in DL4J.

Comments

Popular posts from this blog

Uploading a binary file to RESTful web service in java using Jersey

Hi, Recently I had a situation where I was supposed to upload a binary file to a server side RESTful web service created in Jersey. I faced a lot of trouble in doing so because always one or the other part of the hood was not working properly. The following post gives a step by step process as how to create a RESTful service that accepts uploaded binary files. Pre-requisites: 1. A running RESTful service based on Jersey. 2. An HTML page with file upload component. 3. Tomcat 7 as web server. 4. Eclipse indigo. Note: Let us suppose that tomcat is running on same machine on which the HTML client is present. So we will be using the base domain URL as ' http://localhost:8080/ '. If your service and client are located on different instances of tomcat then replace the localhost part of the URL with the IP address of the machine on which the REST service is running. Step 1 (Eclipse setup): a. Create a 'Dynamic Web Project' in eclipse indigo. Let us say that thi

Using R Programming Language inside Java

Hi, This tutorial walks you through using R programming language inside a java program. R is a powerful programming language for Machine Learning and Data Mining. It contains implementation of various Machine Learning algorithms. Recently i had a situation where i was supposed to use these machine learning algorithms inside my java program. I searched the web but the stuff i found was not of much help. Thus i thought of putting the same in the form of this tutorial. This tutorial is not intended to teach you R language. It is only to help you integrate R to Java and then to use R functions inside a Java program. Pre-requisite: Following things are needed to be pre-configured on your system to use R in Java program: 1. R workbench:  R has got a console known as RGui where R commands/programs could be executed. To install RGui simply go to  http://cran.r-project.org/bin/windows/base/  and download the ' Download R 3.0.2 for Windows   (52 megabytes, 32/64 bit) '. Simply

Creating a JAX-WS Web Service and Client using Eclipse Indigo

Creating a JAX-WS Web Service and corresponding client is a very trivial task. However I faced various problems while doing the same mainly while creating a client for the web service. This was mainly because the contents I found on the Internet were in a distributed manner. So I thought of creating a tutorial which binds all the concepts at one place. So this tutorial does not teaches indepth concepts of web services. Instead it guides as how to create a Web Service and a seperate client. Software Requirements: 1. Eclipse Indigo. Note: To download Eclipse Indigo follow the link http://www.eclipse.org/downloads/. Then select 'Eclipse IDE for Java EE Developers'. Process: We will proceed with the creation of web service and client in following two steps: 1. Creating Web Service 2. Creating client to consume web service. 1. Creating Web Service: Creating of a JAX-WS web service is not a very tough task. We simply create a class(normal Java class) and define certai