Modeling and simulating the algorithm for adjusting brightness in Python and Julia languages

Serbu Claudiaa
10 min readJun 12, 2023

--

Python is an extremely popular programming language among people in the field of science, but especially among professionals in the “machine learning” domain, being widely used in the field of artificial intelligence. However, it has been stated that Julia, a relatively new language, has the potential to compete with Python.

This article aims to present and analyze, in terms of execution time and performance, these two very important programming languages in the field of artificial intelligence. In the end, a comparison will be made between them, using an algorithm for adjusting the brightness of an image.

Introduction

Python is the most popular language when it comes to prototyping and developing algorithms for machine learning. Python is an interpreted language that seems to have a significant loss of performance compared to compiled languages. Julia is a relatively new language that attempts to bridge the gap between high-performance yet complex languages like C/C++ and slow and easy-to-use languages like Python. However, the Python community has developed a multitude of solutions to address performance issues.

As we move towards a data-driven society where all devices collect massive amounts of data, machine learning is used to extract meaningful information and make correlations, becoming a predominant field in most applications. As the amount of data grows, the way we process this data becomes increasingly important. This development leads to increasingly complex machine learning models. Emphasis is placed on foundational tools, namely programming languages, so it would be particularly interesting to see how these two languages compare in terms of performance and speed.

This article aims to provide insight into how these two languages compare in terms of performance when it comes to machine learning. It could be a small step towards determining whether Julia is a better tool for machine learning or not, considering various aspects such as libraries, future developments, maintenance, and whether it can perform as well as other high-level languages.

This article aims to offer a perspective on how the two languages compare in terms of performance when it comes to machine learning. It could also be a small step towards finding out whether Julia is a better tool for machine learning or not, and whether Julia is a good enough language, taking into account several aspects: libraries, future developments, maintenance, and whether it can perform as well as other high-level languages.

Python

Python is a dynamic multi-paradigm programming language created in 1989 by the Dutch programmer Guido van Rossum. Van Rossum is still a leader in the community of software developers who work on perfecting the Python language and its core implementation, CPython, which is written in C.

“Computer vision” is a process through which we can understand how images and videos are stored and how we can manipulate and extract data from them, playing a crucial role in the field of artificial intelligence.

  1. OpenCv

OpenCV is an open-source library for computer vision, machine learning, and image processing, and it now plays an important role in real-time operations, which is crucial in today’s systems. By using OpenCV, images and videos can be processed to identify objects, faces, or even handwritten text. The first version of OpenCV was 1.0. OpenCV is released under a BSD license, making it free for both academic and commercial use. It is cross-platform, supporting C++, C, Python, and Java, and it is compatible with Windows, Linux, Mac OS, iOS, and Android. When OpenCV was designed, the main emphasis was on real-time applications for efficient computation.

Example of reading an image using OpenCV:

import cv2

image = cv2.imread('image.png')
h, w = image.shape[:2]
print("Height = {}, Width = {}".format(h,w))

2. NumPy

NumPy, which stands for Numerical Python, is a library composed of objects — multidimensional arrays, and a collection of tools for processing these arrays. It is the fundamental package for scientific computing. In addition to its obvious scientific applications, NumPy can also be used as an efficient multidimensional container for generic data.

Example code using the NumPy library:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr)

The result:

[1 2 3 4 5]
<class 'numpy.ndarray'>

Julia

Founded in 2009 and released in 2012, Julia is a high-performance, open-source programming language that is also considered high-level. As stated by its four creators, Julia was born out of a need to address the shortcomings of other programming languages while integrating the unique and desirable features of those languages. Initially conceived as a general-purpose language, Julia has thrived in the field of numerical and scientific computing. The language supports parallelism at three primary levels: coroutine (green threading), multi-threading, and multi-core/distributed processing.

Features of the Julia programming language:

  • Free and open-source with the MIT license
  • Easy to learn with a friendly syntax
  • Compiled, not interpreted, which makes it faster
  • High-performance language designed for parallel computing
  • Unicode and UTF-8 encoding support
  • Works well with other programming languages such as C and Python

Considering that Julia is exceptionally fast and highly performant, it comes as no surprise that it has gained significant attention from users. Specifically, the Julia language is highly popular among mathematicians and data scientists.

BlackRock uses Julia to analyze time series data, while the British insurer Aviva utilizes it to calculate risks. Additionally, the Federal Reserve Bank of New York previously used Julia in modeling the U.S. economy and found that Julia’s model estimation was nearly ten times faster than MATLAB. Notably, the Celeste project, which is based on Julia, employed the language to catalog telescope data for all visible astronomical objects. The project became the first Julia-based application to achieve a peak performance of 1.54 PF/s (petaflops) in just 14.6 minutes, establishing a new scientific milestone. Other key users of Julia include NVIDIA, CISCO, Climate Modeling Alliance, Cancer Research UK, QuantEcon, and more, with the list continuing to grow. Other applications include but are not limited to fields such as data science, AI, machine learning, and visualization.

Python vs Julia

A. Performance

Julia is a compiled language, which means that programs written in Julia are executed directly as executable code, resulting in better performance. It delivers impressive, efficient, and fast results without requiring many optimizations or techniques. Some optimizations in Julia cannot be utilized in Python. Essentially, projects from other languages can be written once and compiled into Julia, making it ideal for automated machine learning and artificial intelligence. The execution time in Julia for large and complex code is lower than in Python. Python requires more optimization methods and external libraries, which highlights the performance and usefulness of the Julia language.

B. Speed

Speed was one of the primary objectives in creating and developing the Julia language. Interestingly, Julia is a member of the Petaflop club, which comprises computing languages that surpass the maximum performance of one petaflop per second. Julia is not interpreted; instead, it utilizes just-in-time (JIT) compilation, which involves compiling during runtime. Julia excels in numerical and complex computing functions since it is designed to execute code quickly. However, with the continuous improvements of the Python interpreter, Python can excel in execution speed through external libraries, optimization tools, and third-party JIT compilers.

C. Libraries

Considering its early stage, Julia has a limited number of libraries. Additionally, the libraries are not well-maintained, requiring more time to establish and execute data, as they are still in active development. On the other hand, Python boasts a vast number and rich set of libraries, primarily due to its long-standing existence and popularity. Moreover, these libraries are well-maintained, making it easier to perform various additional tasks. Python is also supported by a significant number of third-party libraries, making it a favorite among developers and programmers. With a supportive and active programming community, Python offers excellent support for community-built tools, systems, and interfaces. However, Julia lacks substantial support and many excellent resources, debugging tools, or problem-solving capabilities at the level of Python’s performance.

Python vs Julia

Modeling and simulating the algorithm for adjusting brightness

The benchmark was based on a basic image processing task, which involved adjusting the brightness of a set of images. The reason behind choosing this task was to verify if Julia can perform equally well compared to image processing libraries like OpenCV (widely used for computer vision tasks). Both NumPy and OpenCV are highly optimized libraries that provide high-level abstractions for various intensive computational tasks involving vectors, images/videos, etc.

The image manipulation task in Julia was performed using built-in methods in the “Images” library, and the same was done using NumPy and OpenCV in Python. After running the benchmark on 4 images with different resolutions, it was observed that OpenCV and NumPy performed better than Julia. This does not mean that Julia’s performance is not as good as Python because both OpenCV and NumPy are highly optimized libraries, and it is expected for them to be faster. Additionally, Julia was able to perform as well as NumPy in some calculation tasks, such as matrix addition.

In the first case, the image manipulation task for both Julia and Python was performed using only for loops. After running the benchmark on 4 images with different resolutions, it was observed that Julia performed much better than Python. The used images had the following characteristics:

  • 001.jpg (425x310, 35.1 KB)
  • 002.jpg (360x220, 18 KB)
  • 003.jpg (2400x1152, 517 KB)
  • 004.jpg (1920x1200, 337 KB)

For comparative evaluation, the BenchmarkTools library in Julia was used. Since Julia supports interfacing with foreign functions in Python, it was possible to incorporate the Python script into the Julia script. The PyCall library in Julia was used to interface Python with Julia. For this comparison, 2 Julia tests were performed: julia builtin.jl, which uses color broadcasting to balance the colors of an image, and julia loop.jl, which uses basic for loops to perform RGB color balancing.

Furthermore, 3 tests were conducted in Python: py cv.py, which uses the OpenCV image processing library to balance the colors of an image, py loop.py, which uses basic loops to perform the same task, and py np.py, which uses specific vectorized operations from the NumPy library to perform RGB color balancing.

In all tests, color balancing occurs within the color balance function, except for py cv.py, where the OpenCV cv2.addWeighted() function is used for color balancing. The benchmark involves modifying the brightness of 4 images with different resolutions by a factor of 50.

Besides its high performance and good library support, an important aspect was Julia’s ability to integrate with foreign functions from C/Fortran, C++, Python, R, Java, and many other languages.

Julia vs Python in the case of using loops:

Julia vs Python in the case of using special libraries, Numpy and OpenCv:

The performance of Julia was 10 times better than Python, and some of its numerical calculations even outperformed NumPy. However, due to the lack of good libraries like OpenCv, executing tasks in computer vision, NLP, etc. was not as flexible compared to Python. While Julia does have some excellent libraries for image processing, they were not fully stable and feature-rich like Python. Nevertheless, development is still underway, and Julia may be able to surpass Python in terms of library support in the very near future.

Conclusions

In general, choosing the right language for scientific applications is a very difficult task and involves multiple compromises. Such a language should be easy to use, but we also desire high performance, high portability, and high productivity (HP3).

While Julia is gaining attention and making a name for itself, Python remains a true rival. Regardless of the language we choose, we must consider several factors because each language has its strengths and weaknesses. Both Julia and Python have a bright future in the fields of big data, data science, AI, and machine learning. However, Julia has the potential to grow and establish itself as a programming language, eventually achieving widespread adoption in the industry.

Knowing the capabilities of Julia, Python will continue to improve and address its weaknesses. Additionally, Python will remain an important player in all technological domains, possessing sought-after skills regardless of Julia’s rise. In conclusion, both languages show promise, and competition can only lead to further improvement, benefiting the future.

==========================================================

About me:

My name is Claudia Serbu, and I work as a software engineer at Luxoft Romania. I am a Master Student at Faculty of Electronics, Telecommunications and Information Technology, and I am studying Computer and Information Systems. At Luxoft, I had the opportunity to participate in various programming courses, so that’s how this article was inspired. Thank you! Other references that helped me to write this article:

Viktor Axillus, 2020–01–28, Comparing Julia and Python, An investiga-
tion of the performance on image processing with deep neural networks
and classification

Dogaru, Ioana; Dogaru, Radu (2015) Using Python and Julia for efficient
implementation of natural computing and complexity related algorithms

--

--