/

A typical supervised learning task necessitates sourcing for a great deal of data, to build complicated models and boost predictive power. These desirable outcomes, however, will not be realized without objectively labelling the data available. In this article, I will discuss some intuition and implementation of a supervised learning model coupled with the active learning algorithm to label data. Active learning optimizes the labelling process, by leveraging the advantages of manual and automatic labelling. Arguably, it is a competitive strategy to produce reliable labels with minimal manpower.

1. Manual Labelling
Label tags are attached to data points by a human who is an in-house labeller, crowd sourced or outsourced personnel. However, considering the massive volume of data, manual labelling can be time-consuming, costly, and difficult to coordinate.
2. Automatic Labeling
A separate ML model can be trained to understand raw data and output appropriate label tags. Some relevant learning algorithms include supervised learning, unsupervised learning, and reinforcement learning. Admittedly, some manual work is still required to label a small portion of the data set if a supervised learning model is used. In general, automatic labeling significantly lessens manual workload. But the quality of output labels is heavily dependent on the labeling model’s performance, which can be difficult to improve.
Active learning is an algorithm that prioritizes informative unlabelled training samples. These samples are extracted for labelling by a user (yes, a human). Subsequently, they are added back into the data set to retrain the labelling model. Priority scores of each data point are recalculated accordingly, and the cycle repeats until a satisfactory model performance is achieved. The final model can then be applied to the remaining un-labelled data. Ultimately, we obtain a fully labelled training data set ready for use!

By intelligently selecting useful samples to label, the manual work required drastically reduces. Furthermore, the curious nature of the algorithm helps obtain a robust final classification model which provides labels to new un-labelled data. In this way, active learning helps attain the best of both manual and automatic labelling.
1. Question! How can we quantify the informativeness of a data point and its priority score?
There are many ways! For instance, through calculating confidence of label prediction, margin, entropy, uncertainty, etc. More about these concepts can be found in Learn Faster with Smarter Data Labelling and Active Learning in Machine Learning.
2. Transfer learning applied here can further improve the labelling model’s performance!
The code illustrations in this section are contextualized on a project brought together by the Omdena Singapore Chapter, titled Exploring the Impact of Covid-19 on Mental Health in Singapore using NLP. The project kicked off with scraping textual data from multiple sources such as Reddit, Twitter, and YouTube. Labelling came next. One of the goals for this project was to build a topic classifier, thus our labels should be categories of some sort.
Having > 80K posts, limited time, and manpower, it was unrealistic to manually label every post. Many of us in the team favored automatic labelling, thus we attempted topic modelling using Latent Dirichlet Allocation (LDA). Unfortunately, the outcome of that endeavour was not meaningful.
We wanted some certainty about the label tags, yet complete data labelling efficiently. Research led us to active learning, which we pursued. Keeping our goal in mind, we identified “Social Issue Discussion”, “Personal Stories”, “Advocacy”, “Factual Queries/Sharing” and “Others” as label tags. Ultimately, the labels, together with the textual data from posts scraped from social media, can train a topic classifier to categorize new posts into different groups.
We decided to use Superintendent after coming across the author’s presentation at PyData London 2019. It is a library that can implement active learning on ML data and has many useful features that simplify code implementation. These aside, there are many illustrative codes in its documentation, which we referenced heavily. It made the developing process more beginner friendly.
Apart from Superintendent, other software can be roped in using Docker Compose, to make the labelling task more cohesive. This includes:

Docker images are files containing all required code needed for a software program (e.g. Nginx, Redis, etc) to run inside a Docker container. They act like templates and are fundamental blocks that people build on top of. In our case, more dependencies were required. Thus, additional installation on top of the original docker images was executed like so:
tensorflow.Dockerfile image, voila.Dockerfile is available on GitHub (source: Omdena)
# create a layer with TensorFlow Docker image and set it as the base image FROM tensorflow/tensorflow:latest-py3# install additional dependencies needed RUN pip install superintendent RUN pip install sqlalchemy==1.3.24 RUN mkdir /app WORKDIR /app# configure container ENTRYPOINT ["python"]# specify default command to run within an executing container CMD ["app.py"]
docker-compose.yml pulls and initializes images specified in a docker container. It also enables further customization, including volumes that are important for data to persist. For example, ./postgres-data:/var/lib/postgresql/data ensures data stored in the Docker container at /var/lib/postgresql/data is similarly present in the local directory ./postgres-data. In our code, they were also used to make input data accessible in the container and to store trained labelling models, as seen below.
# declare the docker-compose version version: "3.1"# specify all services needed services:db:image: postgres restart: always environment: &environmentPOSTGRES_USER: superintendent POSTGRES_PASSWORD: superintendent POSTGRES_DB: labeling POSTGRES_HOST_AUTH_METHOD: trustvolumes:- ./postgres-data:/var/lib/postgresql/dataports:- "5432:5432"orchestrator:build:context: . dockerfile: tensorflow.Dockerfilerestart: always depends_on:- "db"environment: *environment entrypoint: python /app/orchestrate.py volumes:- ./orchestrate.py:/app/orchestrate.py - ./aldata.csv:/app/aldata.csv - ./models:/app/models
Shortened docker-compose.yml, full code is available on GitHub (source: Omdena)
orchestrate.py acts as the brain of the web application, and thus repeatedly runs until docker-compose is terminated. It sets up a connection with the database, reads, and stores the intended input data. When all the required components are running, relevant samples are identified for labelling using active learning strategies. After users provide a suitable label tag, it is updated in the database. Subsequently, these labelled data points are pre-processed before they are fed to the model for training. The model is then evaluated to inform users of its reliability. To adapt the above functions to our task, we added some features to our code, as mentioned below.
???? Code data pre-processor (where relevant)
???? Build the labelling model
???? Establish a method to evaluate model
def evaluate_model(model, x, y):
# calculate cross validation score
score = cross_val_score(model, x, y, scoring = "accuracy", cv = 3)
# create directory /app/models if it is not already present
if not os.path.isdir("/app/models/"):
os.mkdir("/app/models")
# design filename
save_time = time.asctime(time.localtime(time.time()))
filename = str(save_time) + " {}".format(score)
# save trained model at specified path using filename designed above
filepath = "/app/models/{}".format(filename)
pickle.dump(model, open(filepath, "wb"))
return score
???? Connect code to database previously set up in YAML file
???? Create a widget to coordinate the active learning
???? Read in data
???? Configure setting to train in some time interval you specify
The Jupyter notebook is used to style the front-end interface of the web application. For our task, we made a simple interface that displays post contents (the combined title and selftext), label tag options, and a progress bar.
Cool! We managed to run our scripts using docker-compose on a local machine. But really, it was not useful yet, because the application is not accessible to others for distributed labelling. Thus, this section records a step-by-step illustration of how our code was deployed to AWS EC2 using a free-tier account.









On cmd, enter the directory containing the .pem key pair file. Then connect to the instance using the commands below. For Mac:
???? chmod 400 omdena-sg-sample.pem to change access permission
???? ssh -i omdena-sg-sample.pem [email protected]
We will need PuTTY. It works like the SSH command but is developed specifically for Windows. The instructions to set up PuTTY are here.
For Docker and Docker Compose to run in Ubuntu, they have to be installed. The steps to do so are extensively explained and illustrated in the articles on DigitalOcean below.
Docker and Docker Compose are installed correctly if running sudo docker ps and docker-compose –version returns an empty table and the version of docker-compose installed respectively.
Run sudo docker-compose up! This will take some time, give it a few minutes. After which, the web application can be accessed using the public DNS provided by the EC2 instance as follows:
Overall, the experience consists of researching about active learning, then implementing it using Superintendent and Docker-Compose, and finally deploying it. At the end of it all, the team managed to run the active learning program and label some data successfully. However, this was not fully implemented in the project primarily due to tight deadlines. That said, the team also had difficulties agreeing on the label tags which we should use.
The complete source code on GitHub: https://github.com/OmdenaAI/omdena-singapore-covid-health/tree/main/src/tasks/task-3-1-data-labelling
*** *** ***
A big thank you to Omdena for the opportunity to work on the Singapore Local Chapter project. Also, a shout-out to all the collaborators who contributed to the project and to my fellow enthusiastic teammates, Rhey Ann Magcalas and Chan Jiong Jiet, who were a great help in exploring active learning.
*** *** ***
This article is written by Tan Jamie.

AI for Sustainable Farming: Tackling Greenhouse Gas Emissions and Empowering Responsible Finance

AI for Solar Energy Adoption in Sub-Saharan Africa

Deep Learning for Solar Rooftop Mapping and Faster Installation

AI Meets a 96-Year-Old NGO: Transforming Cross-Border Child Protection with Smarter Case Management