top of page

Codes

   Sometimes the interviewers request me to send them a sample code to know more about my programming skills. Since most of the codes that I have implemented are corporative, I decided to write some sample codes in different languages and share them in my website. In continue, I share two sample codes in C++ and Python. Meanwhile, I have a good experience in Matlab and maybe I add a sample code written in this academic programming language later.

​

C++

   Suppose you want to implement a program in which you can find the most similar images to a specific image (download project). In this case, you need a dataset of images, their features and labels. One more point, you should define a mechanism for similarity detection and finally retrieve the most similar images. To this issue, despite the basic classes for graphical GUI, I have defined a class named "ImageAnnotation," where all the computational processes are calculated and ultimately the output is retrieved to the upper controller class. Therefore, I show the GUI and ignore describing it (because it is not my main issue), main function, and the widget class, and describe the "ImageAnnotation" class by details.

The header of this class includes a "struct" which contains the information attained by this class and conveys the most similar images and related tags toward the controller class. Two "enum"s are defined for two purposes. While "nearesrNeighborMethod" determines how many similar images should be retrieved (one or three), "distanceComputingMethod" offers this option that different metrics could be implied in similarity calculation (euclidean or manhattan distance). In this project, for legibility, functions and variables follow the camel code naming, and all the variables passed into the functions are followed by an underline. Meanwhile, since duplicating the variables (especially images and vectors) are time and memory consuming, in many cases the calls in functions are by reference and through making these variables as "constant," we make a limitation in data manipulation. Public and private categorization are the essence of encapsulation and abstraction and for readability, I have defined more than one private section. The names of the variables and functions clarifies their application and since their definition is in ".cpp" file, we describe their details there. Below is the implementation of the header.

Now, in the source file we implement any function. After the necessary includes and constant variables we define the constructor, where we call two functions of this class for default value setting. This class has no pointer, so the destructor function is not necessary.

Two setter functions are used for sending parameters for similarity computing methods.

The first step is feeding the variables. As I mentioned first, images, features and labels should be read from saved files. So, the address of the folders containing any set of these data is passed to the corresponding function to be set in the related variables. Four functions below read images and text files containing the information.

The main function of this class is the function "computeTagsForImage". This function first checks that data are provided. If the condition checked, it calls three consecutive functions. The first one finds the most similar images. The second is responsible for removing repetitive tags, while the last function provides tags from tag list. In the final stage, this function puts the most similar images and their corresponding tags in a variable of the type of predefined structure "DataPackage" and returns it for the controller connected to the GUI.

The functions called in the previous function are describes as below. Function "checkAllDataExistency" controls the variables to prevent system crash.

Function "findMostSimilarImages", utilizes the features uploaded in "features" variable to find one or three most similar images based on a variable that we have set. This function calls another function for distance computing, which works base on manhattan or euclidean distance depicted by the user. These two functions are shown as below.

After finding the most similar images we should extract their labels and remove their redundancy. This procedure is done by "refineSelectedLabels" function.

By finding the indices of the tags we should extract the text of tags.

A pointer of this class is made in the controller class of GUI to be an intermediate between the user commands and the defined class.

Python

   I have recently focused on two important libraries in machine learning issues, especially neural networks, in Python. Tensorflow and PyTorch are the most interesting and applicable libraries, where you can easily make your convolutional and deep neural networks. As a simple sample code, the following represents a code where a dynamic convolutional neural networks is provided to predict the facial expressions. In this code, three expressions, including 'happy', 'angry', and 'sad' are considered. It is noteworthy that running such a project in the real world requires myriad face images, while in this code I used only 22 samples for training and 6 for test. For such a program, we need to detect face in any image, create dataset, create model. So, I wrote different classes for any independent task and used a control class to utilizes these classes for our task. The figure below is the output of this program for a sample image. This program is developed in colab and is reachable through this link.

Generally, many libraries have been developed to facilitate our programming. Initially, I called some of them that was needed for my program.

As I mentioned, we need a class for face detection. Fortunately, many algorithms has been uploaded in the internet and opencv has methods for calling them. I found an xml file in the internet that is the implementation of haar face detection algorithm and fed it into opencv and get the region of face.

Another class, called ModelGenerator, prepares a model, and set the stage for learning and testing a new sample. The number of convolutional layers, as well as the number of neurons in the hidden layer, could be managed by the controller class.

The next class provides the train and test datasets using the samples that are stored in a specific root and folder. In the dataset folder, for any class there is a folder containing some images that are the samples of that class.

It is necessary to write a controller class that utilizes the aforementioned classes and provides the understandable and easy-to-use functions for the developer. This class is an intermediate between the classes and the upper-level software developer. 

By the existence of the controller class, easily we call some of its methods to set up the data and model. Then we can train our model and test it with a sample outside our dataset.

Photo7.png

Mohammad Mehdi
Hosseini

(MoMeHo)

bottom of page