Should I be using Keras vs. TensorFlow for my project? Is TensorFlow or Keras better? Should I invest my time studying TensorFlow? Or Keras?
The above are all examples of questions I hear echoed throughout my inbox, social media, and even in-person conversations with deep learning researchers, practitioners, and engineers.
I even receive questions related to my book, Deep Learning for Computer Vision with Python where readers are asking why I’m covering “just” Keras — what about TensorFlow?
It’s unfortunate.
Because it’s the wrong question to be asking.
As of mid-2017, Keras was actually fully adopted and integrated into TensorFlow. This TensorFlow + Keras integration means that you can:
Define your model using the easy to use interface of Keras
And then drop down into TensorFlow if you need (1) specific TensorFlow functionality or (2) need to implement a custom feature that Keras does not support but TensorFlow does.
In short:
You can insert TensorFlow code directly into your Keras model or training pipeline!
Don’t get me wrong. I’m not saying that you don’t need to understand a bit of TensorFlow for certain applications — this is especially true if you’re performing novel research and need custom implementations. I’m just saying that if you’re spinning your wheels:
Just getting started studying deep learning…
Trying to decide on which library to use for your next project…
Wondering if Keras or TensorFlow is “better”…
…then it’s time those wheels got some traction.
Stop worrying and just get started. My suggestion would be to use Keras to start and then drop down into TensorFlow for any specific functionality you may need.
In today’s post, I’ll show you how you can train both (1) a neural network using strict Keras and (2) a model using the Keras + TensorFlow integration (with custom features) built directly into the TensorFlow library.
To learn more about Keras vs. Tensorflow, just keep reading!
Looking for the source code to this post?
Jump right to the downloads section.
In the remainder of today’s tutorial, I’ll continue to discuss the Keras vs. TensorFlow argument and how it’s the wrong question to be asking.
From there we’ll implement a Convolutional Neural Network (CNN) using both the standard keras module along with the tf.keras module baked right into TensorFlow.
We’ll train these CNNs on an example dataset and then examine the results — as you’ll find out, Keras and TensorFlow live together in harmony.
And perhaps most importantly, you’ll learn why the Keras vs. TensorFlow argument doesn’t make much sense anymore.
Asking whether you should be using Keras or TensorFlow is the wrong question — and in fact, the question doesn’t even make sense anymore. Even though it’s been over a year since TensorFlow announced that Keras will be integrated into official TensorFlow releases, I’m still surprised by the number of deep learning practitioners who are unaware that they can access Keras via the tf.keras sub-module.
And more to the point — that the Keras + TensorFlow integration is seamless, allowing you to drop raw TensorFlow code directly into your Keras model.
Using Keras inside of TensorFlow gives you the best of both worlds:
You can use the simple, intuitive API provided by Keras to create your models.
The Keras API itself is similar to scikit-learn’s, arguably the “gold standard” of machine learning APIs.
The Keras API is modular, Pythonic, and super easy to use.
And when you need a custom layer implementation, a more complex loss function, etc., you can drop down into TensorFlow and have the code integrate with your Keras model automatically.
In prior years, deep learning researchers, practitioners, and engineers often had to choose:
Do I go with the easy to use, but perhaps harder to customize Keras library?
Or do I utilize the significantly harder TensorFlow API, write an order of magnitude more code, and not to mention, work with a less than easy to follow API?
Luckily, we don’t have to choose anymore.
If you find yourself in a situation asking “Should I use Keras vs. TensorFlow?”, take a step back — you’re asking the wrong question — you can have both.
Figure 3: As you can see, by importing TensorFlow (as tf
) and subsequently calling tf.keras
, I’ve demonstrated in a Python shell that Keras is actually part of TensorFlow.
Including Keras inside tf.keras allows you to to take the following simple feedforward neural network using the standard Keras package:
And then implement the same network using the tf.keras submodule which is part of TensorFlow:
Does this mean that you have to use tf.keras ? Is the standard Keras package now obsolete? No, of course not.
Keras as a library will still operate independently and separately from TensorFlow so there is a possibility that the two will diverge in the future; however, given that Google officially supports both Keras and TensorFlow, that divergence seems extremely unlikely.
The point is this:
If you’re comfortable writing code using pure Keras, go for it, and keep doing it.
But if you find yourself working in TensorFlow, you should start leveraging the Keras API:
It’s built right into TensorFlow
It’s easier to use
And when you need pure TensorFlow to implement a specific feature or functionality, it can be dropped right into your Keras model.
There is no more Keras vs. TensorFlow argument — you get to have both and you get the best of both worlds.
Figure 4: The CIFAR-10 dataset has 10 classes and is used for today’s demonstration (image credit).
For the sake of simplicity, we are going to be training two separate Convolutional Neural Networks (CNNs) on the CIFAR-10 dataset using:
Keras with a TensorFlow backend
The Keras submodule inside tf.keras
I’ll also be showing how to include custom TensorFlow code within your actual Keras model.
The CIFAR-10 dataset itself consists of 10 separate classes with 50,000 training images and 10,000 testing images. A sample is shown in Figure 4.
Our project structure today can be viewed in the terminal with the tree command:
The pyimagesearch module is included with the downloads associated with this blog post. It is not pip-installable, but it is included in the “Downloads”. Let’s review the two important Python files part of the module:
minivggnetkeras.py : This is our strict Keras implementation of MiniVGGNet , a deep learning model based on VGGNet .
minivggnettf.py : This is our TensorFlow + Keras (i.e., tf.keras ) implementation of MiniVGGNet .
The root of the project folder contains two Python files:
train_network_keras.py : This is the first training script we’ll implement using strict Keras.
train_network_tf.py : The TensorFlow + Keras version of the training script is nearly identical; we’ll walk through it, highlighting differences, as well.
Each of the scripts will generate a respective training accuracy/loss plot as well:
plot_keras.png
plot_tf.png
As you can see from the directory structure, we’re going to be demonstrating the implementation + training of MiniVGGNet for both Keras and TensorFlow (with the tf.keras module) today.
The first step in training our network is to implement the network architecture itself in Keras.
I’ll assume you are already familiar with the fundamentals of training a neural network with Keras — if you are not, please refer to this introductory post.
Open up the minivggnetkeras.py file and insert the following code:
We begin with a bunch of Keras imports required to build our model.
From there, we define our MiniVGGNetKeras class:
We define the build method on Line 12, and define our inputShape and input . We’ll assume “channels last” ordering which is why depth is the last value in the inputShape tuple.
Let’s start defining the body of the Convolutional Neural Network:
Examining the code block, you’ll notice we are stacking a series of convolutional, ReLU activation, and batch normalization layers prior to applying a pooling layer to reduce the spatial dimensions of the volume. Dropout is also applied to reduce overfitting.
For a brief review of the layer types and terminology, be sure to check out my previous Keras tutorial where they are explained. And for in-depth study, you should pick up a copy of my deep learning book, Deep Learning for Computer Vision with Python.
Let’s add the fully-connected (FC) layers to the network:
Our FC and Softmax classifier are appended onto the network. We then define the neural network model and return it to the calling function.
Now that we’ve implemented our CNN in Keras, let’s create the driver script that will be used to train it.
Open up train_network_keras.py and insert the following code:
We import our required packages on Lines 2-13.
Notice the following:
On Line 3 the backend for Matplotlib is set to "Agg" so that we can save our training plots as image files.
On Line 6 we import the MiniVGGNetKeras class.
We’re using the scikit-learn’s LabelBinarizer for “one-hot” encoding and its classification_report to print classification accuracy statistics (Lines 7 and 8).
Our dataset is conveniently imported on Line 10. If you want to learn how to use custom datasets, I suggest you refer to this previous Keras tutorial or this post which shows how to a real-world of example with Keras.
Our only command line argument (our output --plot path) is parsed on Lines 16-19.
Let’s load CIFAR-10 and encode the labels:
We load and extract our training and testing splits on Lines 24 and 25) as well as convert to floating point + scale the data on Lines 26 and 27.
We encode our labels and initialize the actual labelNames on Lines 30-36.
Next, let’s train the model:
The training parameters and optimization method are set (Lines 40-46).
Then we use our MiniVGGNetKeras.build method to initialize our model and compile it (Lines 47-50).
And subsequently, we kick off the training procedure (Lines 54 and 55).
Let’s evaluate the network and generate a plot:
Here we evaluate the network on our testing split of the data and generate a classification_report . Finally, we assemble and export our plot.
Note: Usually, I would serialize and export our model here so that it can be put to use in an image or video processing script, but we aren’t going to do that today as that is outside the scope of the tutorial.
To run our script, make sure you use the “Downloads” section of the blog post to download the source code.
From there, open up a terminal and execute the following command:
Each epoch is taking a little over 5 minutes to complete on my CPU.
Figure 6: The accuracy/loss training curves are plotted with Matplotlib. This network was trained with Keras.
As we can see from the terminal output, we are obtaining 75% accuracy on our testing set — certainly not state-of-the-art; however, it’s far better than random guessing (1/10).
For a small network, our accuracy is actually quite good!
And as our output plot demonstrates in Figure 6, there is no overfitting occurring.
tf.keras
Figure 7: The MiniVGGNet CNN architecture built with tf.keras
(a module which is built into TensorFlow) is identical to the model that we built with Keras directly. They are one and the same with the exception of the activation function which I have changed for demonstration purposes.
Now that we’ve implemented and trained a simple CNN using the Keras library, let’s learn how we can:
Implement the same network architecture using TensorFlow’s tf.keras
Include a TensorFlow activation function inside our Keras model that is not implemented in Keras itself.
To get started, open up the minivggnettf.py file and we’ll implement our TensorFlow version of MiniVGGNet :
In this file, notice that the imports are replaced by a single line (Line 2). The tf.keras sub-module contains all of our Keras functionality which we can call directly.
I’d like to call attention to the Lambda layers — they are used to insert a custom activation function, CRELU (Concatenated ReLUs), based on the paper Understanding and Improving Convolutional Neural Networks via Concatenated Rectified Linear Units by Shang et al. These lines are highlighted in yellow.
CRELUs are not implemented in Keras but are in TensorFlow — by using TensorFlow and tf.keras we can add CRELUs into our Keras model with just a single line of code.
Note: The CRELU has two outputs, one positiveReLU and one negative ReLU concatenated together. For positive x values the CRELU will return [x, 0] while for negative x values CRELU will return [0, x]. For more information, please refer to the Shang et al. publication.
The next step is to implement our TensorFlow + Keras driver script to train MiniVGGNetTF .
Open up train_network_tf.py and insert the following code:
Our imports are handled on Lines 2-12. The only changes in comparison to our Keras training script include importing the MiniVGGNetTF class and importing tensorflow as tf rather than Keras.
Our command line argument is parsed on Lines 15-18.
Then we load our data on Line 23 much like before.
The rest of the lines are the same — extracting training/testing splits and encoding our labels.
Let’s train our model:
The training process, taking place on Lines 39-54, is the same with the exception of those highlighted in yellow, where only minor changes are to be noted.
From there we evaluate and plot our data (Lines 58-73).
As you can see, we’ve implemented the exact same training process, only now we’re using tf.keras .
To run this script, make sure you use “Downloads” section of the blog post to grab the code.
From there, open up a terminal and execute the following command:
After training completes you have a training plot that look similar to the following:
Figure 8: The deep learning training plot shows our accuracy and loss curves. The CNN was trained with the Keras module which is built into TensorFlow.
By swapping in the CRELU for the RELU activation function we obtain 76% accuracy; however, that 1% increase may be due to the random initialization of the weights in the network — further experiments with cross-validation would be required to demonstrate that CRELU was indeed responsible for this increase of accuracy.
However, the raw accuracy is not the important aspect of this section.
Instead, focus on how we were able to swap in a TensorFlow activation function in-place of a standard Keras activation function inside of a Keras model!
You could do the same with your own custom activation functions, loss/cost functions, or layer implementations as well.
In today’s blog post we discussed questions surrounding Keras vs. TensorFlow, including:
Should I be using Keras vs. TensorFlow for my project?
Is TensorFlow or Keras better?
Should I invest my time studying TensorFlow? Or Keras?
Ultimately, we found that trying to decide between Keras and TensorFlow is starting to become more and more irrelevant.
The Keras library has been integrated directly into TensorFlow via the tf.keras module.
Essentially, you can code your model and training procedures using the easy to use Keras API and then custom implementations into the model or training process using pure TensorFlow!
If you’re spinning your wheels trying to just get started with deep learning, trying to decide between Keras or TensorFlow for your next project, or simply wondering if Keras or TensorFlow is “better”…then it’s time you seek some traction.
My advice to you is simple:
Just get started.
Type either import keras or import tensorflow as tf (so you have access to tf.keras ) into your Python project and get to work.
TensorFlow can be directly integrated into your model or training process so there’s no need to compare features, functionality, or ease of use — all of TensorFlow and Keras are available for you to use in your projects.
I hope you enjoyed today’s blog post!
If you are interested in getting started with computer vision and deep learning, I would suggest you take a look at my book, Deep Learning for Computer Vision with Python. Inside the book, I utilize Keras and TensorFlow to teach you deep learning applied to computer vision applications.
And if you would like to download the source code today’s tutorial (and be notified when future blog posts are published here on PyImageSearch), just enter your email address in the form below!
聯(lián)系客服