DelphiPythonPython GUIWindows

Artificial Intelligence Solutions With Keras Library In A Windows Python App

abstract background with blue stains

Are you looking for a simple, flexible, and powerful deep learning library, and build a nice GUI for them? You can deliver enterprise-grade AI solutions easily by combining Keras and Python4Delphi library, inside Delphi and C++Builder.

Keras is a high-level neural networks API for Python. Keras acts as an interface for the TensorFlow library.

Keras is designed for human beings, not machines. Keras follows best practices for reducing cognitive load: It offers consistent and simple APIs, minimizes the number of user actions required for common use cases, and it provides clear and actionable error messages. 

It’s popularity? You don’t need to worry! Keras has extensive documentation and developer guides, and also the most used deep learning framework among top-5 winning teams on Kaggle. Keras was also the 10th most cited tool in the KDnuggets 2018 software poll and registered a 22% usage. Keras is used by CERN, NASA, NIH, and many more scientific organizations around the world (and yes, Keras is used at the LHC).

This post will guide you on how to run the Keras library to train neural networks and use Python for Delphi to display it in the Delphi Windows GUI app.

First, open and run our Python GUI using project Demo01 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute script button, and get the result in the upper Memo. You can find the Demo01 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1 6813476

This post will introduce you to how to perform image classification from scratch, starting from JPEG image files on disk and we will run it in Python GUI. In this tutorial, we will use the famous Kaggle’s Cats vs Dogs binary classification dataset, and we will train neural networks to classify those images.

1. Get the Dataset

First, let’s download the 786M ZIP archive of the raw data, by using this curl command in your Windows cmd:

0 1curldownloadthedatasets 1636105

And unzip the dataset:

0 2extractdatasets 8452308

Now we have a PetImages folder that contains two subfolders, Cat and Dog. Each subfolder contains image files for each category.

 

2. Import Libraries

If you are a new user of Python4Delphi, Delphi, or RAD Studio, importing these libraries may cause some errors, please read this link to help you to fix them.

 

3. Filter out the Corrupted Images

When working with lots of real-world image data, corrupted images are a one of the common problems. Let’s filter out badly-encoded images that do not feature the string “JFIF” in their header, using this code:

 

4. Split the Dataset into the Training and Validation Set

This is the code to split your dataset into 80% for training set, and 20% for validation set:

 

5. Visualize the Data

This is the results of all operations above, performed in Python GUI by Python4Delphi:

2 1 4734745

The “0” label means “Cat”, while the “1” label means “Dog”.

 

6. Perform Image Data Augmentation

When you don’t have a large image dataset, it’s a good practice to artificially introduce sample diversity by applying random yet realistic transformations to the training images, such as random horizontal flipping or small random rotations. This helps expose the model to different aspects of the training data while slowing down overfitting. This is the code for random flip and random rotation operations:

Let’s visualize what the augmented samples look like, by applying data_augmentation repeatedly to the first image in the dataset:

This is the results of the data augmentation, performed in Python GUI by Python4Delphi:

2 2 6225858

 

7. Standardize the Data

Our images are already in a standard size (180x180), as they are being yielded as contiguous float32 batches by our dataset. However, their RGB channel values are in the [0, 255] range. This is not ideal for a neural network; in general, you should seek to make your input values small. Here, we will standardize values to be in the [0, 1] by using a Rescaling layer at the start of our model.

This tutorial is designed to be performed in a CPU. So the preprocessing for the augmented images will happen asynchronously, and will be buffered before going into the model, like this:

 

8. Configure the Dataset for Performance

Let’s make sure to use buffered prefetching so we can yield data from disk without having I/O becoming blocking:

 

9. Build a Deep Learning Model

We’ll build a small version of the Xception network (authored by François Chollet). We haven’t particularly tried to optimize the architecture; if you want to do a systematic search for the best model configuration, consider using Keras Tuner.

Note that:

  • We start the model with the data_augmentation preprocessor, followed by a Rescaling layer.
  • We include a Dropout layer before the final classification layer.

 

10. Train the Model

This is the code to train the deep learning model, with 50 training epochs:

Warning: Training this model on a regular laptop will take around 1 until 2.5 hours for each training epoch.

3 2 9252301

Congratulations, now you have learned how to run the Keras library to train neural networks and use Python for Delphi to display it in the Delphi Windows GUI app! Now you can try lots more sophisticated examples from these documentations using the Keras library and Python4Delphi.


Check out the keras library for Python and use it in your projects: https://pypi.org/project/Keras/ and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi


References & further readings

[1] Chollet, F. (2022).

Image classification from scratch. Keras documentation. keras.io/examples/vision/image_classification_from_scratch

[2] Keras. (2023).

Code examples. Keras documentation. keras.io/examples

[3] Keras. (2023).

Keras: Simple. Flexible. Powerful. Keras Website. keras.io

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

Leave a Reply

Your email address will not be published. Required fields are marked *