Which is better, TensorFlow or Keras?

The author of this article is often asked these questions in email, social media, and even face-to-face conversations with deep learning researchers, practitioners, and engineers: Should I use Keras or TensorFlow in my project? Which is better, TensorFlow or Keras? Should I spend time researching TensorFlow or Keras? Do you have the same question? If so, I believe this article will give you the answer.

In fact, by mid-2017, Keras had been adopted on a large scale and integrated with TensorFlow. This combination of TensorFlow + Keras allows you to:

Use Keras interface to define the model;

If you need specific TensorFlow features or need to implement custom features that Keras does not support but TensorFlow supports, you can go back to TensorFlow.

Simply put, you can insert TensorFlow code directly into Keras's model or training pipeline!

But please don't get me wrong, I am not saying that you don't need to know TensorFlow. I mean, if you:

Just started to get involved in deep learning...

Selecting for the next project...

I want to know which is "better", Keras or TensorFlow...

My suggestion is to start with Keras first, and then dive into TensorFlow, so that you can get some specific features you need.

In this article, I will show you how to use Keras to train a neural network, and how to use the Keras + TensorFlow combination built directly in the TensorFlow library to train a model.

Which one should I learn Keras or TF?

In the rest of the article, I will continue to discuss the controversy between Keras and TensorFlow and why this question is actually a wrong question.

We will use the standard keras module and TensorFlow's tf.keras module to implement a convolutional neural network (CNN).

We will train CNN on a sample data set, and then check the results-you will find that Keras and TensorFlow can work together very harmoniously.

Most importantly, you will understand why the argument between Keras and TensorFlow is actually meaningless.

Although it has been more than a year since TensorFlow announced the integration of Keras into the official TensorFlow version, many deep learning practitioners still don't know that they can access Keras through the tf.keras submodule. I am surprised for this.

More importantly, the integration of Keras + TensorFlow is seamless, and you can put the TensorFlow code directly into the Keras model.

Using Keras in TensorFlow will bring you the best of both worlds:

You can use the simple and intuitive API provided by Keras to create models;

Keras API is very similar to scikit-learn (considered the "gold standard" of machine learning APIs);

Keras API is modular and easy to use;

When you need a custom implementation or a more complex loss function, you can directly enter TensorFlow and let the code automatically integrate with the Keras model.

In the past few years, deep learning researchers, practitioners, and engineers often have to make the following choices:

I chose the Keras library that is easy to use but difficult to customize?

Or choose the hard-to-use TensorFlow API and write more code?

Fortunately, we don't have to struggle anymore.

If you find yourself still asking such a question, then please take a step back-you are asking the wrong question-you can have both frameworks at the same time.

As shown in the figure, import TensorFlow (tf), and then call tf.keras, it can be seen that Keras has actually become a part of TensorFlow.

Including Keras in tf.keras allows you to implement simple feedforward neural networks using standard Keras packages:

Then use the tf.keras submodule to implement the same network:

Does this mean you have to use tf.keras? Is the standard Keras package obsolete? of course not.

As a library, Keras can still be used alone, so the two may part ways in the future. However, because Google officially supports Keras and TensorFlow, it seems unlikely that this will happen.

The key is:

If you are used to writing code in Keras, then please continue to do so.

But if you are mainly using TensorFlow, then you should start to consider the Keras API:

It is built into TensorFlow;

It is easier to use;

When you need to use TensorFlow to implement specific functions, you can directly integrate it into the Keras model.

Our sample data set

The CIFAR-10 data set contains 10 categories, and we will use it in our demonstration.

For simplicity, we will use the following method to train two separate convolutional neural networks on the CIFAR-10 dataset:

TensorFlow + Keras;

Keras submodule of tf.keras.

I will also show how to include custom TensorFlow code in a Keras model.

Our project structure

You can use the tree command to view our project structure in the terminal:

The pyimagesearch module cannot be installed via pip, please click the download link provided at the end of the article. Now let's take a look at two important Python files of this module:

minivggnetkeras.py: The Keras implementation of MiniVGGNet (a deep learning model of Opportunity VGGNet).

minivggnettf.py: TensorFlow + Keras (ie tf.keras) implementation of MiniVGGNet.

The project root directory contains two Python files:

train_network_keras.py: Keras version of the training script.

train_network_tf.py: TensorFlow + Keras version of the training script, almost exactly the same as the previous one.

Each script will generate the corresponding training accuracy and loss:

plot_keras.png

plot_tf.png

Use Keras to train the network

The first step of training is to use Keras to implement the network architecture.

Open the minivggnetkeras.py file and insert the following code:

We first import a series of Keras packages needed to build the model.

Then define our MiniVGGNetKeras class:

We define the build method, inputShape and input.

Then define the main parts of the convolutional neural network:

You will find that we have stacked a series of convolution, ReLU activation and batch normalization layers before applying the pooling layer in order to reduce the spatial dimension of the volume. Dropout is also used to reduce overfitting.

Now add the fully connected layer to the network:

We have implemented a CNN using Keras, and now create a driving script that will be used for training.

Open train_network_keras.py and insert the following code:

We first import the required packages.

Set matplotlib to "Agg" so that you can save the training results as image files.

Then import the MiniVGGNetKeras class.

We use scikit-learn's LabelBinarizer for "one-hot" encoding, and use classification_report to print the classification accuracy.

Then import the data set.

We pass in the command line parameters through --plot, which is the path where the image is saved.

Now let's load the CIFAR-10 data set and encode the tags:

We first load and extract training and test splits, convert them to floating point numbers and perform data scaling.

Then we encode the labels and initialize labelNames.

Next, let's start training the model:

We first set the training parameters and optimization methods.

Then we use the MiniVGGNetKeras.build method to initialize and compile the model.

Subsequently, we started the training program.

Now let's evaluate the network and generate the resulting graph:

We evaluate the network based on the test segmentation of the data, generate a classification_report, and finally export the results.

Note: I usually serialize and export the model so that I can use it in image or video processing scripts, but I don’t plan to do this here because it is beyond the scope of this article.

Open a terminal and execute the following command:

It takes more than 5 minutes for my CPU to complete an epoch.

We got a 75% accuracy rate-certainly not the most advanced, but it is much better than random guessing (1/10).

For small networks, our accuracy rate is very good, and there is no overfitting.

Use TensorFlow and tf.keras to train the network

The MiniVGGNet CNN built using tf.keras is the same as the model we built directly using Keras, except for the activation function modified for demonstration purposes.

Now that we have implemented and trained a simple CNN using the Keras library, we need to:

Use TensorFlow's tf.keras to achieve the same network;

A TensorFlow activation function is included in the Keras model. This function is not implemented using Keras.

First, open the minivggnettf.py file, we will implement the TensorFlow version of MiniVGGNet:

Note that there is only one line in the import section. The tf.keras submodule contains all the Keras functions that we can call directly.

I want to emphasize the Lambda layer-they are used to insert a custom activation function CRELU (Concatenated ReLU).

Keras does not implement CRELU, but TensorFlow does it-by using TensorFlow and tf.keras, we can add CRELU to the Keras model with one line of code.

The next step is to write a TensorFlow + Keras driver script to train MiniVGGNetTF.

Open train_network_tf.py and insert the following code:

Then the command line parameters are parsed.

Then load the data set as before.

The rest of the lines are the same-extract training/test splits and encode labels.

Now let's start training the model:

The training process is almost the same. We have implemented the exact same training process, but this time using tf.keras.

Open a terminal and execute the following command:

After the training is complete, you will get results similar to the following:

By replacing the RELU activation function with CRELU, we obtained 76% accuracy. However, this 1% increase may be due to the random initialization of network weights, and further cross-validation experiments are needed to prove that this increase in accuracy is indeed due to CRELU.

In any case, the original accuracy rate is not the focus of this section. What we need to pay attention to is how to replace the standard Keras activation function with the TensorFlow activation function inside the Keras model!

You can also use your own custom activation function, loss/cost function or layer.

Summarize

In this article, we discussed Keras and TensorFlow related issues, including:

Should I use Keras or TensorFlow in my project?

Which is better, TensorFlow or Keras?

Should I spend time researching TensorFlow or Keras?

In the end we found that choosing between Keras and TensorFlow became less important.

Because the Keras library has been directly integrated into TensorFlow through the tf.keras module.

Yoga Laptop

What`s the main features appear into your mind when you look yoga laptop? Lightweight, super thinner, touch screen, 360 rotating, smaller size, like as notebook? You are right, that`s the main reasons why some people also called it as laptop yoga slim or yoga notebook. To OS, just same as Education Laptop-windows operating system, so you can see many windows yoga laptop at the market around the world. In fact, this intel yoga laptop usually is designed for normal jobs, like basic WPS, Photoshop, video or music editing, online learning, shopping, presentation when on a business trip, etc. At our store, you can see 11.6 inch n5100 360 Laptop in metal, 13.3 inch 360 flip laptop, and 14 inch 360 degree rotating laptop, etc. The people who take business trips often or prefer fashion design should like this type device.

There are education laptop also, like 14.1 inch Student Laptop for Hope or government Projects, 15.6 inch competitive celeron business laptop for normal business works, 15 inch 10th or 11th Gaming Laptop for heavier tasks, like teachers, high or university students, etc, 16.1 inch i7 16gb ram 4gb graphics laptop for engineering student, etc

Any other question or other requirements, just contact us freely.

Yoga Laptop,Laptop Yoga Slim,Yoga Notebook,Intel Yoga Laptop,Windows Yoga Laptop

Henan Shuyi Electronics Co., Ltd. , https://www.shuyilaptop.com

This entry was posted in on