September 1, 2022

Unsupervised learning in Image Classification - Everything To Know

An AI model is trained in several ways. With this article, we are exploring unsupervised learning for image classification. Read ahead to learn everything you need to know to get started.

Introduction

Machine learning and deep learning are frequently employed for classification and regression problems in practice. Both these techniques use different algorithms to train the models for prediction tasks in businesses and organizations. Image classification is a very common task in healthcare computer vision problems, fraud detection in financing institutes, customer behavior analysis, natural language processing, and many more. The two well-known techniques for machine learning are supervised and unsupervised learning. A good number of different algorithms have been employed depending on the data and the nature of the problem. Supervised learning is relatively simple for the application, while unsupervised learning to train the model is relatively complex and less accurate, although reliable. Unsupervised learning uses a totally different approach due to the non-availability of labels in the data. Yet it has its own advantages in image classification.

In this article, we are going to compare the two options of learning for the model when being applied to image classification but will focus more on the latter covering the different algorithms briefly. We will also cover in detail the concept and process of one algorithm viz clustering for unsupervised learning that is popular for image classification.

Concept of Supervised and Unsupervised Learning

Supervised learning is similar to a classroom scenario where the students learn with the help of a teacher who guides them. Here the model learns with the help of labels of the output made available to it and, with the observed features, easily predicts the output on similar objects not shown to it earlier. Thus it is not complex, and the more details provided, the more easily and quickly it can be trained. Whereas in unsupervised learning, the scenario is different as the students have to learn without the guidance of a teacher and on their own. Likewise, the model here learns on its own by grouping the objects by observing similar patterns into one class and different ones in the second or other class. Naturally, the process is slightly complex and relatively less accurate.

Supervised and Unsupervised Machine Learning Approaches

The following table shows a comparison of the two techniques.

Feature Supervised Learning Unsupervised Learning
Type of Data required Labeled Data Unlabeled data
Computation High Complexity Low Complexity
Applicability Offline Real-time
Model Accuracy Highly reliable and accurate Moderately accurate and reliable
Algorithm types Classification and Regression Clustering and Association Rule mining

Why do we need Unsupervised Learning?

Although unsupervised learning generates moderate accuracy results, there are many reasons for which unsupervised learning could be a better choice -  

  1. It can find patterns in all types of datasets where humans cannot easily spot a trend.
  2. It can quickly help to identify features helpful for categorizing the input data into logical groups.
  3. It performs well in real-time compared to supervised learning and can be implemented in AI systems demanding real-time responses for input recognition and classification. This is one of the main features of unsupervised learning.

Benefits and Limitations of Unsupervised Learning 

A few advantages offered by unsupervised learning are -

  • Unsupervised learning can classify unlabeled data. Since data annotation, i.e., Labeling is a manual, tedious, and expensive process, the labels can be added later after grouping, which saves time and money.
  •  It can quickly find the underlying patterns in data that are difficult to identify with conventional methods.
  • It assists in reducing the dimensionality of the input dataset.

Now, let's us understand some challenges in unsupervised learning -

  • It provides moderate accuracy since it learns slowly on unlabeled data (raw data) by slowly identifying the patterns on its own. 
  • It is a time-consuming process as the algorithm's learning phase might require a lot of time for analysis and computations. With increased complexity in the data, the training phase might be even longer.

Algorithms in Unsupervised Learning 

The algorithms in unsupervised learning fall into two categories viz classification and association. We will show them in schematic form as below and then cover brief information about each except clustering, which will be covered in detail and with an example.

Clustering is employed when we want to discover the groups inherently existing in the data. For example, we can form customer groups based on their purchasing behavior, like the type of particular products or particular brands. An association algorithm becomes applicable when we can establish a rule of relationship, like when a customer who buys a product x is also likely to buy product y. For example, when a customer buys milk, he is most likely to buy bread or toast with it. Different algorithms have been developed for both options, a few of which are shown below.

   

Types of Algorithms in Unsupervised Learning

                                                        

Classification in Unsupervised Machine Learning

  1. Clustering

The process of grouping similar entities together is known as 'clustering' in unsupervised machine learning. This technique aims to identify patterns in the input data and then group similar data points together. Grouping similar entities together helps to profile the attributes of different groups. Using this technique, we can gain insights into underlying patterns of different groups in the input dataset. There are many algorithms developed to implement this technique, but for this article, we will discuss 'K-Means clustering,' one popular and frequently used clustering algorithm in unsupervised machine learning.

  1. K-Means clustering 

This unsupervised learning algorithm is used to form groups of unlabelled data into a random but logical group called clusters denoted as 'k.' The value of k is predetermined before forming actual clusters. Simply put, if k = 3 or 5, the number of clusters will be 3 and 5, respectively. Each dataset with similar properties is included in one group, and those with different properties are grouped into more groups depending on their particular but similar properties. Let us say we have data of different species; then they will be grouped into clusters like a group of birds, fish, and dogs where k will be equal to 3.

Thus this method helps the data to be divided into different groups of images, although it was not labeled. It achieves this on the basis of different but similar characteristics of different species on the basis of size, shape, and other features.

Process of K-Means Clustering

The sequence is as follows - 

  1. Get the data.
  2. Decide the value of k (random number or by knee graph) for clustering.
  3. Select a centroid of each cluster, i.e., for the first group, c1 as centroid, c2 for the second, and c3 for the third, if k=3
  4. Calculate the distance of other points from each centroid
  5. Shift or allocate the data points as per their minimum distance from each centroid.
  6. Find out the data points which shift from one cluster centroid to another. If so, shift it to that centroid due to its minimum distance from that centroid.
  7. Update the centroid and repeat the process until data points allotted to newly formed clusters do not shift their positions. In other words, convergence has been achieved for all data points.

We will understand this clustering process of unsupervised learning in detail with an example in the later part of this article.

b. Hierarchical Clustering

There are also two types of Hierarchical clustering - Agglomerative and Divisive.

i) Agglomerative: This follows a bottom-to-top approach for data separation. To begin with, every data point is a cluster by itself. Hence, a number of clusters are formed, which are then progressively added together, reducing the number of clusters. This process of merging continues till one single cluster is formed.

ii) Divisive: This is the opposite of the Agglomerative approach as it follows a top-to-bottom pattern. The complete dataset starts as a single cluster and is then gradually broken down into multiple clusters until no further division is possible.

c.  Principal Component Analysis (PCA)

This is a technique used in unsupervised learning when large datasets with a number of features are to be analyzed. As the data involves a large number of features, it takes more computational power and significant time. The idea is, therefore, to reduce the number of features, referred to as 'dimensionality reduction.' The dimensions are reduced to a smaller number but maintain the originality of this data structure and prevent loss of information. It is used in movie recommendations and purchases of stocks, etc.

2. Association 

This technique is used to find the relation, association, and/or dependencies between the data items. Various rules are used to find useful relations between the datasets in the database. The Association learning rule benefits product-based business organizations because mapping relations between different products can improve their sales and earn more profits. Similarly, web usage mining, market-based analysis, and continuous production analysis are some important applications of association rule mining.

  1. Apriori Algorithm

This algorithm utilizes the principle of frequent itemset mining in transactions which means it tries to find out the group of items that go with each other most frequently. It does this by a two-stage process wherein firstly; the items are grouped that go together and then pruning or removing those which don't frequently go with others. With repetitions of the above steps, finally, a group that has threshold and confidence values according to preset parameters is formed with the association principle. The preset parameters are 'support' which is the frequency of occurrence of an item, and 'confidence' which is a conditional probability.

  1. Frequent Pattern (FP) Growth Algorithm

It is an acronym for 'Frequent Pattern.' It is similar to the Apriori algorithm, but it represents all the data points like a tree structure and is hence known as the frequent pattern tree. The algorithm finds the most frequent pattern from the given database. This approach is faster than Apriori as it reduces the search time to find frequent patterns of an itemset in data.

  1. Eclat Algorithm

Eclat stands for 'Equivalent Class Transformation.' It is an improved version of the Apriori Algorithm and follows the mining association rule. Accordingly, a rule is found that will predict the occurrence of an item based on how many times other items occur in that transformation.

With this overview of unsupervised algorithms, let us now look at an example of unsupervised image classification using K-Means clustering in the next section.

Unsupervised Image Classification

The image classification task is usually performed with supervised learning, where the model gets trained on known labels. However, when an unsupervised learning approach is used in image classification, it requires the following steps -

  1. Using a pre-trained model for transfer learning as a 'feature extractor' for input data.
  2. Apply an unsupervised learning algorithm to the input data to classify.

Let us understand what feature extraction means and why it is necessary for image classification in unsupervised learning. For describing an image, we can use parameters like the presence of objects in the image, the color, the brightness or the sharpness, etc. But for a machine, the features for an image will be the presence of edges and ridges, any corners or regions of interest, color intensity, etc., as these can be used by the algorithm to segment the image and assess the similarity for grouping or classification.

Thus, feature extraction is a technique of converting the raw data into numerical features for applying the algorithm while maintaining a true representation of the input. A machine learning algorithm cannot work on an image directly, and the image needs to be converted into an array of numbers for further processing. A well-known technique in this regard is called 'transfer learning,' where we can re-use an already trained model for extracting features from unseen images. These pre-trained models for transfer learning are models that have been trained on millions of images for computer vision. Some popular high-performing transfer learning models for image classification are VGG, Inception, and ResNet. It is possible to access these pre-trained models using the Keras framework.

Unsupervised Image Classification using K-Means Clustering 

Here, we will use the flower image dataset containing 3670 images belonging to five categories - daisy, dandelions, roses, sunflowers and tulips. Since it is a large dataset, for demonstration, we will randomly take only 15 images of two categories - daisy and dandelion from the dataset and add them to a single folder. These are some sample images from the dataset - 

Using one of the pre-trained image classification models for transfer learning mentioned earlier, we can extract the features of these images. Once the features are extracted, we will apply the K-Means algorithm using the following code -

#Creating Clusters

kmeans = KMeans(n_clusters=2, init='k-means++', random_state=0)

 

# predict a label for each image based on clusters

Y = kmeans.fit_predict(img_features)

print(Y)

This is the output of the model which indicates the category of all the input images.

Since we have only two categories of flowers, we are using the number of clusters =2. However, for larger datasets with unknown labels, it is essential to experiment with the number of cluster values for acceptable results.

Here are a few images from each cluster -

#cluster 1

#cluster 2

It is clear that the model did a good job at grouping the images into two categories. All images except one image were correctly clustered. This indicates that we can perform image classification using an unsupervised learning approach with transfer learning.

Conclusion

In this article, we discussed unsupervised learning for image classification and different algorithms for the unsupervised learning approach. In summary-

  • Unsupervised Learning is a learning approach that uses different algorithms to analyze and cluster unlabeled raw data without human intervention for Machine Learning models. 
  • Unsupervised learning generally produces moderately accurate results on raw datasets but continues to learn and improve slowly from the newer data.
  • Due to its capability to uncover patterns in an unlabeled dataset, unsupervised machine learning can be applied to image segmentation and classification problems. For example, it can quickly assign labels to uncomplicated, broad land cover classes such as water, vegetation/non-vegetation, forested/non-forested, etc.).

About us:  VisionERAis an Intelligent Document Processing (IDP) platform capable of handling various types of documents and images for classification. It can extract and validate data for bulk volumes with minimal intervention. Also, the platform can be molded as per requirements for any industry and use case because of its custom DIY workflow feature. It is a scalable and flexible platform providing end-to-end document automation for any organization.

Looking for a document processing solution that uses deep learning enhanced image classification capabilities? Set up a demo today by clicking the CTA below or simply send us a query through the contact us  page!

Get Started with your Document Automation Journey

$0 Implementation cost | $0 monthly payments -> No Risk, No Headaches

Pay only for Satisfactory Results!

Sign up for Free Trial