There have been significant advancements in Artificial Intelligence (AI) research. The evolution of AI has led to machine learning applications becoming some of the most widespread technologies right now. Considering how much machine learning has impacted our lives, it’ll continue to be at the forefront. Forecasts predict the global machine learning market to grow from $21.17b in 2022 to $209.91b by 2029. Read on to learn about a few machine learning examples from the growing market of machine learning technologies.
Today we see machine learning examples everywhere. From daily life tasks to professional and industrial procedures, machine learning has a major impact on businesses. Our dependency on machines in routine tasks has fuelled data creation in all major industries across the globe. These vast datasets include unstructured data in the form of texts, images, videos, and audio. These industries leverage machine learning technology to simplify working with large amounts of unstructured data.
Machine Learning in Classification Problems
Let’s consider machine learning classification that separates a given dataset into two or more categories. Classification techniques employ supervised machine learning algorithms that use labeled training data for model training.
Image classification is the most popular machine learning classification problems. It involves the categorisation and labelling of image pixels using particular machine learning models. Let’s go step-by-step through the code of a practical image classification example to understand its workings.
Image Classification Tutorial
Here is a simple tutorial of binary image classification using Convolutional Neural Network (CNN). A convolutional neural network (CNN) is a deep learning modelling technique that is analogous to the working of neurons in the human brain. This technique is mostly applied to image analysis. CNN model takes an input image and assigns weights to image objects that can be differentiated from one another. The image objects act as neurons that are stimulated using mathematical functions called convolutions.
This tutorial has been created in Google Colab using Kaggle’s Cats & Dogs images dataset.
The dataset includes images of cats and dogs divided into three directories:
- Training set including training images.
- Test set including test images.
- Single predictions for single image predictions.
The following python code takes you through building a machine learning model using various cats and dogs images. The model is then plotted to graphically demonstrate training accuracy and loss. After model training, we’ll evaluate its accuracy through test images.
Step 1: Setting up dataset from Kaggle to Colab
We used this guide for loading the dataset from Kaggle to Colab.
!pip install kaggle !mkdir ~/.kaggle from google.colab import drive drive.mount('/content/drive') !cp /content/drive/MyDrive/Colab Notebooks/kaggle.json ~/.kaggle/ !chmod 600 ~/.kaggle/kaggle.json !kaggle datasets download d4rklucif3r/cat-and-dogs !unzip /content/drive/MyDrive/Datasets/cat-and-dogs.zip -d /content/drive/MyDrive/Datasets/ |
Step2: Importing Libraries
Importing required libraries to access modelling and plotting functions.
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import keras from keras.models import Sequential from keras.layers import Conv2D,Dense,MaxPool2D,Flatten,Dropout from keras.preprocessing.image import ImageDataGenerator,img_to_array,load_img
Step 3: Loading Dataset
In this section, we load our training and test images in batches and resize them all to fit the same size. This is also where we define the type of classification (binary in our case) and the color mode of the images (we are using RGB images).
train_dataload = ImageDataGenerator(rescale=1/255) test_dataload = ImageDataGenerator(rescale=1/255) train_datapath = '/content/drive/MyDrive/Datasets/cat-and-dogs/training_set' train_dataset = train_dataload.flow_from_directory(directory=train_datapath,target_size=(128,128),batch_size=32,color_mode="rgb",class_mode = 'binary') test_datapath = '/content/drive/MyDrive/Datasets/cat-and-dogs/test_set' test_dataset = train_dataload.flow_from_directory(directory=train_datapath,target_size=(128,128),batch_size=30,color_mode="rgb",class_mode = 'binary') |
Step 4: Data visualizing
The following code fetched one image each of both cat and dog from their training sets for visualizing the training data.
imgsample=load_img(train_datapath + "/cats/cat.1001.jpg") plt.imshow(imgsample) plt.axis("off") plt.title("Cat Image Sample View") plt.show() plt.figure() imgsample=load_img(train_datapath + "/dogs/dog.1001.jpg") plt.imshow(imgsample) plt.axis("off") plt.title("Dog Image Sample View") plt.show()
Step 5: Building a CNN model
This step involves building a CNN model by adding layers and defining the functions to be performed on those layers.
model = Sequential() model.add(Conv2D(filters = 64, kernel_size = (5,5), activation = "relu", input_shape = (128,128,3))) model.add(MaxPool2D(pool_size = (2,2))) model.add(Conv2D(filters = 32, kernel_size = (5,5), activation = "relu")) model.add(MaxPool2D(pool_size = (2,2))) model.add(Dropout(0.25)) model.add(Conv2D(filters = 16, kernel_size = (5,5), activation = "relu")) model.add(MaxPool2D(pool_size = (2,2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(units = 256, activation = "relu")) model.add(Dropout(0.5)) model.add(Dense(units = 1, activation = "sigmoid"))
The following code shows a display of the layers we created in our model. The result of the convolution functions applied to them are also displayed in the form of their output shape.
model.compile(optimizer=’rmsprop’,loss=’binary_crossentropy’,metrics=[‘accuracy’]) model.summary() |
Step 6: Fitting the model
The model training was done using GPU for faster results. For this tutorial we limited the epochs to 20, which took around 15-17 minutes to execute.
hist = model.fit(train_dataset, validation_dataset = test_dataset, epochs = 20) |
Step 7: Plotting the model
We graphically visualize the accuracy and loss metrics for our training and test sets.
plt.figure(figsize=(6,6)) plt.style.use("ggplot") plt.plot(hist.history['loss'], color='b', label="Training loss") plt.plot(hist.history['val_loss'], color='r', label="Validation loss") plt.legend() plt.show() plt.figure() plt.figure(figsize=(6,6)) plt.style.use("ggplot") plt.plot(hist.history['accuracy'], color='b', label="Training accuracy") plt.plot(hist.history['val_accuracy'], color='r',label="Validation accuracy") plt.legend(loc = "lower right") plt.show()
Step 8: Prediction testing on unknown single prediction set image
The CNN model can be trained to comprehend complex details of an image. The model correctly predicted the test image as that of a dog. Using this model, we were able to achieve 87% accuracy on the test dataset in 20 epochs.
testing_image = load_img('/content/drive/MyDrive/Datasets/cat-and-dogs/single_prediction/cat_or_dog_1.jpg', target_size = (128, 128)) testing_image = img_to_array(testing_image) testing_image = np.expand_dims(testing_image, axis = 0) result = model.predict(testing_image) if result[0][0] == 1: prediction = 'dog' else: prediction = 'cat' print(prediction) print_image=load_img('/content/drive/MyDrive/Datasets/cat-and-dogs/single_prediction/cat_or_dog_1.jpg') plt.imshow(print_image) plt.axis("off") plt.show()
Real-World Machine Learning Examples using Classification
Classification with machine learning involves the use of deep learning algorithms to understand sophisticated details of given data. Deep learning involves using many hidden layers in a model and has significantly improved the performance of classification tasks.
Let us explore a few industries where classification algorithms have opened doors for technological accomplishments:
HealthCare
Machine learning has revolutionized the healthcare sector with its disease prognostics and diagnostics applications. Image classification is the most popular machine learning technique that helps healthcare professionals timely diagnose patients.
Some prominent image classification examples in healthcare include
- Cancer detection through the classification of cancerous and noncancerous scans.
- Identifying disease progression to prioritize critically ill patients.
- Potential heart failure prediction using patient’s cardiovascular history
Social Media
In today’s age, social media is a huge contributor to data generation across the globe. Machine learning solutions have allowed for a more personalized user experience in apps. The social media industry uses machine learning classification in the following ways:
- Tagging on social media platforms is done by assigning labels to images.
- Categorizing ‘People You May Know’ based on friend list, profile visits, workplace, etc.
- Image sentimental analysis to classify positive and negative emotions based on image and suggesting appropriate emoticons.
E-commerce
E-commerce industries use machine learning solutions for better customer needs satisfaction and inventory management. The rapidly changing customer demands can only be met with advanced machine learning techniques.
E-commerce industries commonly use classification models for:
- Providing product recommendations based on customers’ previous purchases, browsing patterns, cart history, etc.
- Categorising customers based on their browsing and buying patterns.
- Product categorisation in catalog-based websites that combine products from different retailers.
- Customer behaviour prediction to promotional advertisements.
Finance
Industries like banking and finance deal with critical data and transactions that cannot be compromised. Machine learning has become very popular in these sectors for its applications in:
- Using historical transactions for predicting potential threats like credit card fraud and money laundering.
- Determining creditworthiness of a customer based on transactional history.
- Identifying trading strategies using analysis of economic variables.
Cybersecurity
The cyberspace witnesses large volumes of data movement. The security of this space is crucial to preserving data integrity and stopping polluted data from entering computer systems. These security practices are put in place using machine learning algorithms for:
- Malware categorisation by using existing malware to predict and prevent potential malware.
- Spam filtering by classifying email as spam and non-spam using spam features.
What does the future hold for machine learning?
To avoid explicitly programming processes for growing amounts of data globally, we will continue to depend on machine learning algorithms. The machine learning examples we listed in this article touch on just a few industries affected by machine learning. The list of machine learning breakthroughs will continue to evolve and will be challenging to summarise in a single article.
Machine learning is advancing by leaps and bounds, making room for even more possibilities for modernising our lifestyles. The coming years will witness how far this self-improving technology takes us.
If machine learning and AI intrigue you and you want to learn more, check out our collection of resources on the topic. Also, make sure to check out our courses and our special program for aspiring data scientists Beyond Machine.