{"id":11553,"date":"2023-04-03T09:17:01","date_gmt":"2023-04-03T13:17:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/deep-learning\/?p=11553"},"modified":"2024-08-21T08:27:02","modified_gmt":"2024-08-21T12:27:02","slug":"apply-ai-with-new-r2023a-examples","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/deep-learning\/2023\/04\/03\/apply-ai-with-new-r2023a-examples\/","title":{"rendered":"Apply AI with New R2023a Examples"},"content":{"rendered":"There are many new examples in the documentation of the latest MATLAB release (R2023a) that show how to use and apply the newest machine learning and deep learning features. Feel free to take a deep dive into the <a href=\"https:\/\/www.mathworks.com\/help\/stats\/release-notes.html\">machine learning release notes<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/release-notes.html\">deep learning release notes<\/a> to explore all new features and examples.\r\n<h6><\/h6>\r\nIn this blog post, I will show highlights from three new examples that apply deep learning:\r\n<h6><\/h6>\r\n<ul>\r\n \t<li><a href=\"#Visual_Inspection\">Example 1: Visual Inspection<\/a><\/li>\r\n \t<li><a href=\"#EEG_Classification\">Example 2: EEG Classification<\/a><\/li>\r\n \t<li><a href=\"#AI_for_ROM\">Example 3: Reduced Ordering Modeling<\/a><\/li>\r\n<\/ul>\r\n<h6><\/h6>\r\n<p style=\"font-size: 18px;\"><a name=\"Visual_Inspection\"><\/a><strong>Visual Inspection of PCBs<\/strong><\/p>\r\n<em><strong>Note:<\/strong> This section of the blog post describes an example that was updated in R2023b. Check out the updated example <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ug\/detect-pcb-defects-using-yolox-deep-learning.html\">Detect Defects on Printed Circuit Boards Using YOLOX Network<\/a>, which uses a YOLOX instead of a YOLO v4 object detector.<\/em>\r\n<h6><\/h6>\r\n<a href=\"https:\/\/www.mathworks.com\/discovery\/visual-inspection.html\">Visual inspection<\/a> is the image-based inspection of parts where a camera scans the part under test for both failures and quality defects. Visual inspection systems with high-resolution cameras efficiently detect microscale or even nanoscale defects that are difficult for human eyes to pick up.\r\n<h6><\/h6>\r\nHere, I will show you highlights from the documentation example Detect Defects on Printed Circuit Boards (PCBs) Using YOLO v4 Network. PCBs contain individual electronic devices and their connections. Defects in PCBs can result in poor performance or product failures. By detecting these defects, production lines can remove faulty PCBs and ensure that electronic devices are of high quality.\r\n<h6><\/h6>\r\nThis example shows how to detect, localize, and classify defects on PCBs using a YOLOv4 deep neural network.\u00a0 I will mainly highlight the MATLAB tools that allow you to streamline the visual inspection of PCBs and focus on the application, rather than spending too much time on data management and creating a deep learning network.\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>Prepare Training Data<\/strong><\/p>\r\nMATLAB provides functions for preparing data for object detection, some of which are presented in this table:\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"156\"><strong>Functionality<\/strong><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"120\"><strong>MATLAB Function<\/strong><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"348\"><strong>More Details<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" rowspan=\"3\" width=\"156\">Data management<\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"120\"><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.io.datastore.imagedatastore.html\">imageDatastore<\/a><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"348\">Read and manage training images.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"120\"><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.io.datastore.filedatastore.html\">fileDatastore<\/a><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"348\">Read and manage annotation data.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"120\"><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.io.datastore.combine.html\">combine<\/a><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"348\">Combine multiple datastores.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"156\">Transform data<\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"120\"><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.io.datastore.transform.html\">transform<\/a><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"348\">Transform data in datastores to (1) resize images and bounding boxes to the size of the input network and (2) augment (e.g., horizontal flip and scale) input data.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"156\">Estimate anchor boxes<\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"120\"><a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/estimateanchorboxes.html\">estimateAnchorBoxes<\/a><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"348\">Estimate a specified number of anchor boxes based on the size of objects in the preprocessed training data.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>Create Detector<\/strong><\/p>\r\nInstead of creating a deep learning model from scratch, you can get a <a href=\"https:\/\/www.mathworks.com\/solutions\/deep-learning\/models.html\">pretrained model<\/a>, which you can modify and retrain to adapt to your task. You can get the YOLOv4 object detector in just one line of code by using the <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/yolov4objectdetector.html\">yolov4ObjectDetector<\/a> function.\r\n<h6><\/h6>\r\n<pre>detectorToTrain = yolov4ObjectDetector(\"tiny-yolov4-coco\",classNames, ...\r\n    anchorBoxes, InputSize=inputSize);\r\n<\/pre>\r\n<h6><\/h6>\r\nAfter you get the object detector, use the <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/trainyolov4objectdetector.html\">trainYOLOv4ObjectDetector<\/a> function to train it.\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>Inspect Single Image<\/strong><\/p>\r\nThe <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/yolov4objectdetector.detect.html\">detect<\/a>\u00a0function can predict bounding boxes, labels, and class-specific confidence scores for each bounding box. Use detect to inspect a single image and display the results.\r\n<h6><\/h6>\r\n<pre>[bboxes,scores,labels] = detect(detector,sampleImage);\r\n\r\nimshow(sampleImage)\r\nshowShape(\"rectangle\",bboxes,Label=labels);\r\ntitle(\"Predicted Defects\")\r\n<\/pre>\r\n<h6><\/h6>\r\nFor this specific image three missing holes are detected on the PCB, as shown in the figure below. Imagine the implications in the mass production of PCBs\u2026\r\n<h6><\/h6>\r\n<h6><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-11580 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2023\/03\/pcb.png\" alt=\"Image of PCB that has been visually inspected and three missing holes have been detected\" width=\"1644\" height=\"902\" \/><\/h6>\r\n<h6><\/h6>\r\n<strong>Figure:<\/strong> Image of visually inspected PCB that shows missing holes\r\n<h6><\/h6>\r\n&nbsp;\r\n<p style=\"font-size: 14px;\"><strong>Inspect Multiple Images<\/strong><\/p>\r\nAs easily as detecting defects on a single PCB, the detect function can find defects on all the PCB images in the data store. Then, you can evaluate the trained object detector.\r\n<h6><\/h6>\r\nDetect the bounding boxes for all test images.\r\n<h6><\/h6>\r\n<pre>detectionResults = detect(detector,dsTest);<\/pre>\r\n<h6><\/h6>\r\nCalculate the average precision score for each class by using the <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/evaluatedetectionprecision.html\">evaluateDetectionPrecision<\/a>\u00a0function. Precision quantifies the ability of the detector to correctly classify objects. Also calculate the recall and precision values from each detected defect. Recall quantifies the ability of the detector to detect all relevant objects for a class.\r\n<h6><\/h6>\r\n<pre>[averagePrecision,recall,precision] = evaluateDetectionPrecision(detectionResults,dsTest);<\/pre>\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>More resources<\/strong><\/p>\r\n\r\n<ul>\r\n \t<li>Check out more <a href=\"https:\/\/www.mathworks.com\/help\/vision\/examples.html?category=automated-visual-inspection&amp;s_tid=CRUX_topnav\">visual inspection examples<\/a>.<\/li>\r\n \t<li>Check out how C-CORE and Equinor developed automated software with MATLAB to <a href=\"https:\/\/www.mathworks.com\/company\/newsletters\/articles\/analyzing-satellite-radar-imagery-with-deep-learning.html\">analyze satellite radar imagery with deep learning<\/a>.<\/li>\r\n \t<li>Check out the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/116555-computer-vision-toolbox-automated-visual-inspection-library\">Computer Vision Toolbox Automated Visual Inspection Library<\/a>, which enables you to train, calibrate, and evaluate stat-of-the-art anomaly detectors.<\/li>\r\n<\/ul>\r\n<h6><\/h6>\r\n<p style=\"font-size: 18px;\"><a name=\"EEG_Classification\"><\/a><strong>EEG Data Classification<\/strong><\/p>\r\nElectroencephalography (EEG) signals are the most accessible and not surprisingly, the most investigated brain signals. One of the applications of EEG analysis is diagnosing epilepsy and predicting epileptic seizures. Automatic seizure prediction can be used as the onset of a closed-loop stimulator that suppresses epileptic seizures.\r\n<h6><\/h6>\r\nThe documentation example <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/time-frequency-convolutional-network-for-eeg-data-classification.html\">Time-Frequency Convolutional Network for EEG Data Classification<\/a> shows how to classify EEG time series from persons with and without epilepsy using a time-frequency convolutional network. It also shows how to classify pre-seizure and seizure EEG signals. The time-frequency convolutional network achieves high classification accuracy in both cases, which suggests that this type of model can be applied to real-world epilepsy diagnosis and seizure prediction.\r\n<h6><\/h6>\r\nThis example is of particular interest to me, as I spent almost 10 years analyzing and modeling brain signals using MATLAB. I am always very excited to see new MATLAB tools that advance Brain Machine Interfaces (BMIs). See below a few elements in the example that really caught my attention.\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>Training Data Preparation<\/strong><\/p>\r\nIn addition to the MATLAB tools for data management shown in the visual inspection example (and others), MATLAB also provides a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.io.datastore.tabulartextdatastore.html\">datastore for tabular text files<\/a>, which helps manage the EEG time series in this example.\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>Deep Learning Network<\/strong><\/p>\r\nThe convolutional network predicts the class of the EEG data based on the squared magnitude of the continuous wavelet transform (CWT), which you can easily compute by using the <a href=\"https:\/\/www.mathworks.com\/help\/wavelet\/ref\/cwt.html\">cwt<\/a> function. This squared magnitude is also known as a scalogram. The scalogram is an ideal time-frequency transformation for time series data like EEG waveforms, which feature both slowly-oscillating and transient phenomena.\r\n<h6><\/h6>\r\n<h6><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-11592 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2023\/03\/EEGDataTFMapExample.png\" alt=\"Time series and scalogram of seizure EEG, pre-seizure EEG, and normal EGG\" width=\"560\" height=\"420\" \/><\/h6>\r\n<h6><\/h6>\r\n<strong>Figure:<\/strong> Scalogram of EEG time series\r\n<h6><\/h6>\r\nDefine a network that uses a time-frequency transformation of the input signal for classification.\r\n<h6><\/h6>\r\n<pre>netSPN = \r\n    [sequenceInputLayer(1,\"MinLength\",4097,\"Name\",\"input\",\"Normalization\",\"zscore\")\r\n    convolution1dLayer(5,1,\"stride\",2)\r\n    <span style=\"background-color: #ffff00;\">cwtLayer(\"SignalLength\",2047,\"IncludeLowpass\",true,\"Wavelet\",\"amor\")<\/span>\r\n    maxPooling2dLayer([5,10])\r\n    convolution2dLayer([5,10],5,\"Padding\",\"same\")\r\n    maxPooling2dLayer([5,10])  \r\n    batchNormalizationLayer\r\n    reluLayer\r\n    convolution2dLayer([5,10],10,\"Padding\",\"same\")\r\n    maxPooling2dLayer([2,4])   \r\n    batchNormalizationLayer\r\n    reluLayer\r\n    flattenLayer\r\n    globalAveragePooling1dLayer\r\n    dropoutLayer(0.4)\r\n    fullyConnectedLayer(3)\r\n    softmaxLayer\r\n  classificationLayer(\"Classes\",unique(trainLabelsSPN),\"ClassWeights\",classwghts)\r\n    ];\r\n<\/pre>\r\nUnlike deep learning networks that use the magnitude or squared magnitude of the CWT (scalogram) as a preprocessing step, this example uses a differentiable scalogram layer (<a href=\"https:\/\/www.mathworks.com\/help\/wavelet\/ref\/cwtlayer.html\">cwtLayer<\/a>) . With a differentiable scalogram layer inside the network, you can put learnable operations before and after the scalogram. Layers of this type significantly expand the architectural variations that are possible with time-frequency transforms.\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>Classification Results<\/strong><\/p>\r\nVisualize the classification results with a <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/confusionchart.html\">confusion matrix chart<\/a>. The confusion chart shows good performance on the test set. The row summaries in the confusion chart show the model's\u00a0recall, while the column summaries show the\u00a0precision. Both recall and precision generally fall between 95 and 100 percent.\r\n<h6><\/h6>\r\n<h6><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-11601 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2023\/03\/EEGDataTFMapExample_03.png\" alt=\"Confusion matrix for classified EEG data\" width=\"560\" height=\"420\" \/><\/h6>\r\n<h6><\/h6>\r\n<strong>Figure:<\/strong> Confusion matrix chart for EEG classification\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>More Resources<\/strong><\/p>\r\nCheck out the following links, to see how MATLAB users have applied machine learning and deep learning to EEG-based BMIs and epileptic seizure detection:\r\n<ul>\r\n \t<li><a href=\"https:\/\/www.mathworks.com\/company\/newsletters\/articles\/using-machine-learning-to-predict-epileptic-seizures-from-eeg-data.html\">Using Machine Learning to Predict Epileptic Seizures from EEG Data<\/a><\/li>\r\n \t<li><a href=\"https:\/\/www.mathworks.com\/company\/user_stories\/dutch-epilepsy-clinics-foundation-automates-the-detection-and-diagnosis-of-epileptic-seizures.html\">Dutch Epilepsy Clinics Foundation Automates the Detection and Diagnosis of Epileptic Seizures<\/a><\/li>\r\n \t<li><a href=\"https:\/\/www.mathworks.com\/company\/user_stories\/ut-austin-researchers-convert-brain-signals-to-words-and-phrases-using-wavelets-and-deep-learning.html\">UT Austin Researchers Convert Brain Signals to Words and Phrases Using Wavelets and Deep Learning<\/a><\/li>\r\n<\/ul>\r\n<h6><\/h6>\r\n<p style=\"font-size: 18px;\"><a name=\"AI_for_ROM\"><\/a><strong>Reduced Order Modeling with AI<\/strong><\/p>\r\n<a href=\"https:\/\/www.mathworks.com\/discovery\/reduced-order-modeling.html\">Reduced Order Modeling<\/a>\u00a0(ROM) is a technique for reducing the computational complexity or storage requirement of a computer model, while preserving the expected fidelity within a controlled error. High-fidelity non-linear models can take hours or even days to simulate. This causes a significant computational challenge for many engineering teams.\r\n<h6><\/h6>\r\nAI-based reduced-order models can significantly speed up simulations and analysis of higher-order large-scale systems. AI enables to create a model from measured data of your component, so you get the right answer in an accurate, dynamic, and low-cost way. Even if you already have a high-fidelity first-principles model, you can use data-driven models to create a surrogate model that is potentially simpler and simulates faster. A faster but equally accurate model can help you progress as you design, test, and deploy your system.\r\n<h6><\/h6>\r\nTo learn more about ROM using AI, check out <a href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2022\/12\/05\/using-ai-for-reduced-order-modeling\/\">our previous blog post<\/a> and <a href=\"https:\/\/www.mathworks.com\/videos\/ai-with-model-based-design-reduced-order-modeling-1670274257624.html\">this video<\/a>.\r\n<h6><\/h6>\r\n<p style=\"font-size: 14px;\"><strong>Example Overview<\/strong><\/p>\r\nYou can train AI models using various techniques, such as <a href=\"https:\/\/www.mathworks.com\/discovery\/lstm.html\">LSTMs<\/a> and Neural ODEs. <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/reduced-order-modeling-using-ctesn-model.html\">This new example<\/a> shows how to perform reduced order modeling using a continuous-time echo state network (CTESN). This neural network is a type of <a href=\"https:\/\/www.mathworks.com\/discovery\/rnn.html\">recurrent neural network<\/a> (RNN), as is an LSTM, which uses a reservoir computing framework to project signals from higher dimensional spaces.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-11607 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2023\/03\/ReducedOrderModelingUsingCTESNExample_01.png\" alt=\"Replace ODE solver with a continuous-time echo state network (CTESN) model\" width=\"706\" height=\"243\" \/>\r\n<h6><\/h6>\r\n<strong>Figure:<\/strong> Replace ODE solver with a continuous-time echo state network.\r\n<h6><\/h6>\r\nYou can apply the methods shown in the example if you need to solve an ODE in a model multiple times with different parameters. To save computational resources, instead of approximating the solution using an ODE solver within the system, you can train a neural network using a collection of automatically-derived solutions and then, incorporate the neural network in your model. A CTESN model typically evaluates faster than an ODE solver.\r\n<h6><\/h6>\r\n&nbsp;\r\n<p style=\"font-size: 18px;\"><strong>Conclusion<\/strong><\/p>\r\nI hope that the above brief description of the three new examples sparked your curiosity to try them out and explore other new AI features and examples in MATLAB R2023. Comment here to talk about your exciting AI applications and how you are going to take advantage of the latest MATLAB AI features.\r\n<h6><\/h6>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2023\/03\/pcb.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>There are many new examples in the documentation of the latest MATLAB release (R2023a) that show how to use and apply the newest machine learning and deep learning features. Feel free to take a deep... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2023\/04\/03\/apply-ai-with-new-r2023a-examples\/\">read more >><\/a><\/p>","protected":false},"author":194,"featured_media":11580,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[36,9,59],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/11553"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/users\/194"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/comments?post=11553"}],"version-history":[{"count":53,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/11553\/revisions"}],"predecessor-version":[{"id":15919,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/11553\/revisions\/15919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media\/11580"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media?parent=11553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/categories?post=11553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/tags?post=11553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}