<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abby</title>
    <description>The latest articles on DEV Community by Abby (@anmorgan2414).</description>
    <link>https://dev.to/anmorgan2414</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1120290%2Ff3d2d8af-78d3-463c-ba40-ae7149a7d734.jpg</url>
      <title>DEV Community: Abby</title>
      <link>https://dev.to/anmorgan2414</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anmorgan2414"/>
    <language>en</language>
    <item>
      <title>Powering Anomaly Detection for Industry 4.0</title>
      <dc:creator>Abby</dc:creator>
      <pubDate>Mon, 24 Jul 2023 13:00:00 +0000</pubDate>
      <link>https://dev.to/anmorgan2414/powering-anomaly-detection-for-industry-40-anomalib-comet-1e2f</link>
      <guid>https://dev.to/anmorgan2414/powering-anomaly-detection-for-industry-40-anomalib-comet-1e2f</guid>
      <description>&lt;h2&gt;
  
  
  What is Industry 4.0?
&lt;/h2&gt;

&lt;p&gt;You’ve probably heard the buzz: Industry 4.0 is revolutionizing the way companies manufacture, develop and distribute their products. But, what exactly is Industry 4.0?&lt;/p&gt;

&lt;p&gt;To understand the Fourth Industrial Revolution, it helps to remember the first three. The First Industrial Revolution began at the end of the 18th century and focused on mechanizing industrial processes. The Second Industrial Revolution introduced electrification, and the Third Industrial Revolution championed automatization. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nTQSLTkl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx0qgf3ggu4dieckfrfe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nTQSLTkl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx0qgf3ggu4dieckfrfe.png" alt="The Ford assembly line in 1913; source: Wikimedia commons/public domain" width="800" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Ford assembly line in 1913; source: Wikimedia commons/public domain&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each of these periods focused on reducing human intervention in industry, but what’s left once factories are mechanized, electrified, and automated? The Fourth Industrial Revolution is all about harnessing the power of data and leveraging it to simulate cognition in industry.&lt;/p&gt;

&lt;p&gt;More than anything, Industry 4.0 is a paradigm shift in the way we organize and manage industrial processes to make the most of cyber-physical systems. These smart manufacturing processes include artificial intelligence, machine learning, cloud- and edge-computing, Industrial IoT, distributed computing, augmented reality, and much, much more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b19BNMaV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfh7c2twtccxeiycnacy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b19BNMaV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfh7c2twtccxeiycnacy.png" alt="Industrial IoT; image from Connected" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Industrial IoT; image from Connected&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Anomaly Detection?
&lt;/h2&gt;

&lt;p&gt;One of the most popular ways that artificial intelligence is being incorporated into industrial manufacturing is through automated defect detection or anomaly detection. Anomaly detection is the process of identifying anomalous items in a stream of input data and is a critical component of quality assurance in any production line.&lt;/p&gt;

&lt;p&gt;Traditional manual defect detection methods are not only expensive and time-consuming but can also often be ineffective, as not all anomalies are visible to the human eye. However, with the advent of machine learning, computer vision can be leveraged to facilitate human operator work– or even completely automate this process!&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of Anomaly Detection
&lt;/h2&gt;

&lt;p&gt;Anomaly detection faces several unique challenges. It’s often difficult to obtain a large amount of anomalous data, making traditional supervised learning techniques impractical. These class imbalances also mean that popular evaluation metrics like accuracy aren’t relevant.&lt;/p&gt;

&lt;p&gt;Furthermore, the difference between a normal sample and an anomalous one can be microscopic. It isn't always feasible to predefine all types of anomalies at the outset of an experiment. Fortunately, to address each of these challenges and more, a team of artificial intelligence researchers at Intel have developed a cutting-edge, easy-to-implement, open-source package called Anomalib.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Anomalib?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/openvinotoolkit/anomalib"&gt;Anomalib&lt;/a&gt; is an open-source deep learning library developed by Intel that makes it easy to benchmark different anomaly detection algorithms on both public and custom datasets, all by simply modifying a config file. As the largest public collection of anomaly detection algorithms and datasets, it has a strong focus on image-based anomaly detection. It’s a comprehensive, end-to-end solution that includes cutting-edge algorithms, relevant evaluation methods, prediction visualizations, hyperparameter optimization, and inference deployment code with &lt;a href="https://github.com/openvinotoolkit/openvino"&gt;Intel’s OpenVINO Toolkit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3sOzWKj8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2l685cc9kqcva8on0pz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3sOzWKj8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2l685cc9kqcva8on0pz.png" alt="Anomaly detection graphics from a Comet + Anomalib experiment by Sid Mehta; used with permission." width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Anomaly detection graphics from a Comet + Anomalib experiment by Sid Mehta; used with permission.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, how does it work? Anomalib uses unsupervised ML techniques to learn an implicit representation of normality with AutoEncoders, GANs, or a combination of both. During inference, new samples are compared against the embeddings of normal samples to determine whether or not they are anomalous. In this way, Anomalib allows you to save your sparse anomalous data for testing purposes only. &lt;/p&gt;

&lt;p&gt;Anomalib currently supports &lt;a href="https://openvinotoolkit.github.io/anomalib/reference_guide/algorithms/index.html"&gt;ten cutting-edge anomaly detection models&lt;/a&gt;, including &lt;a href="https://openvinotoolkit.github.io/anomalib/reference_guide/algorithms/fastflow.html"&gt;FastFlow&lt;/a&gt;, &lt;a href="https://openvinotoolkit.github.io/anomalib/reference_guide/algorithms/padim.html"&gt;PaDiM&lt;/a&gt;, &lt;a href="https://openvinotoolkit.github.io/anomalib/reference_guide/algorithms/patchcore.html"&gt;PatchCore&lt;/a&gt;, and &lt;a href="https://openvinotoolkit.github.io/anomalib/reference_guide/algorithms/cflow.html"&gt;CFlow models&lt;/a&gt;, but is also continuously updated with the latest state-of-the-art algorithms. You can also &lt;a href="https://openvinotoolkit.github.io/anomalib/how_to_guides/train_custom_data.html"&gt;train models with custom data&lt;/a&gt; or access public datasets like the MVTec or BeanTech datasets through the API. What’s best, Anomalib makes end-to-end anomaly detection possible straight out-of-the-box, and without additional GPUs or super long training times.&lt;/p&gt;

&lt;p&gt;Anomalib can do more than just support POC projects, however. In the real world, machine learning is a highly iterative process, and the details of all these iterations can get pretty confusing, pretty fast. To optimize your model and get it production-ready, you’ll need to log, manage, and version these details in an experiment tracking tool. Anomalib recently announced a full integration with Comet, and in this article, we'll explore how to use the two together to power production-grade anomaly detection for Industry 4.0!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kTN5GA06--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iku4b4cs1gfhspjlc5ds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kTN5GA06--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iku4b4cs1gfhspjlc5ds.png" alt="Anomaly detection of hazelnuts from the MVTec dataset; image from Anomalib." width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Anomaly detection of hazelnuts from the MVTec dataset; image from Anomalib.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Anomalib + Comet for Anomaly Detection
&lt;/h2&gt;

&lt;p&gt;Comet is a powerful tool that allows you to &lt;a href="https://www.comet.com/site/products/artifacts-dataset-management/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;manage and version your training data&lt;/a&gt;, &lt;a href="https://www.comet.com/site/products/ml-experiment-tracking/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;track and compare training runs&lt;/a&gt;, and &lt;a href="https://www.comet.com/site/products/model-production-monitoring/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;monitor your models in production&lt;/a&gt; — all in one platform. And now Comet is fully integrated with Anomalib for experiment management, benchmarking, and hyperparameter optimization!&lt;/p&gt;

&lt;p&gt;The Comet + Anomalib integration offers the following features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Autologging and custom logging of experiment- and project-level metrics and features, including system metrics, hyperparameters, graph definition, evaluation metrics, and more.&lt;/li&gt;
&lt;li&gt;Organize your project-level dashboard with &lt;a href="https://www.comet.com/docs/v2/guides/comet-dashboard/code-panels/about-panels/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;Comet’s custom panels&lt;/a&gt; for an overview that tailors to your team’s specific needs.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.comet.com/site/blog/introducing-comets-new-image-panel/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;Image Panels&lt;/a&gt; allow you to compare your images across different experiments and throughout different steps. Search for individual images and showcase selected images across individual experiment runs.&lt;/li&gt;
&lt;li&gt;Log benchmarked results to Comet as a means to track model drift.&lt;/li&gt;
&lt;li&gt;Isolate the best hyperparameters with HPO powered by the &lt;a href="https://www.comet.com/docs/v2/api-and-sdk/python-sdk/introduction-optimizer/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;Comet Optimizer&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ORYfI9PC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctt6brh5kcqray7zwvqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ORYfI9PC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctt6brh5kcqray7zwvqw.png" alt="Auto-logged experiment-level charts in Comet; image by author." width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Auto-logged experiment-level charts in Comet; image by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Logging
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Experiment-level
&lt;/h4&gt;

&lt;p&gt;In single-experiment view, Comet logs appropriate evaluation metrics in both tabular and chart form. By definition, anomaly detection is a problem of class imbalances, and this makes for poor performance with traditional ML metrics like accuracy, which are designed around an assumption of balanced class distribution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K5BieSCl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlwds1q2zqp1shr8w7f3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K5BieSCl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlwds1q2zqp1shr8w7f3.png" alt="Experiment-level graphics of a broken bottle from the MVTec dataset in Comet; image by author." width="800" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Experiment-level graphics of a broken bottle from the MVTec dataset in Comet; image by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead, Anomalib + Comet calculates the F1 score and AUROC at both image and pixel levels. The F1 score combines precision and recall into a single metric by taking their harmonic mean, while still accounting for the precision-recall tradeoff. The AUROC curve describes how well a model can distinguish between classes by plotting a probability curve at various thresholds, or degrees of separability, and is another very important metric for evaluating classification problems with class imbalances.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c8YRkkBH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fevznorog4dmmk5r6ba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c8YRkkBH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fevznorog4dmmk5r6ba.png" alt="Formula for F1 score, where TP are true positives, FP are false positives, and FN are false negatives" width="800" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Formula for F1 score, where TP are true positives, FP are false positives, and FN are false negatives&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kABTICvO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rumjo4pele8np4w6evew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kABTICvO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rumjo4pele8np4w6evew.png" alt="Pixel-level AUROC and F1 Scores, as auto-logged in Comet; image by author." width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pixel-level AUROC and F1 Scores, as auto-logged in Comet; image by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But that’s not all! Scrolling through the left-hand sidebar of the single-experiment view, you’ll find that quite a few metrics and features are automatically logged for you! Basic contextual information like source code, system metrics, installed packages, and output are all logged. Experiment-specific hyperparameters, metrics, and graph definitions are also auto-logged, and with a simple edit to the Anomalib config file, you can also log images and other graphics to Comet. Keeping track of all these metrics is essential for producing production-ready models and monitoring them for concept and data drift. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iBi8HUHq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/155oeuq7tvpivpkukdie.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iBi8HUHq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/155oeuq7tvpivpkukdie.gif" alt="Auto-logged experiment-level hyperparameter tracking in Comet; GIF by author." width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Auto-logged experiment-level hyperparameter tracking in Comet; GIF by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Project-level
&lt;/h4&gt;

&lt;p&gt;In the panel-level view, you’ll see charts automatically populated with performance metrics for a bird’s eye view of your project across experiment runs. You can also add Comet’s new Image Panels to this view to visualize specific prediction images across different experiments, as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hCw__SAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c6v6kd7vbi7l8ns3ccr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hCw__SAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c6v6kd7vbi7l8ns3ccr.png" alt="Project-level line charts in Comet; image by author." width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Project-level line charts in Comet; image by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T2nPoqaw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y61icnnmzdvkygnku7uc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T2nPoqaw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y61icnnmzdvkygnku7uc.png" alt="Project-level image panel in Comet; image by author." width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Project-level image panel in Comet; image by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lastly, it can be really important to see how things are shaping up between two specific experiment runs. Comet allows you to diff selected runs for a more cross-sectional view of your project. This also allows you to compare specific metrics and parameters:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dU5FRG1X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9n92ta4gjlj47dvazl0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dU5FRG1X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9n92ta4gjlj47dvazl0.gif" alt="Diffing two selected experiment runs in Comet; GIF by author." width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Diffing two selected experiment runs in Comet; GIF by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Benchmarking
&lt;/h3&gt;

&lt;p&gt;Anomalib also includes a benchmarking script for relating results across different combinations of models, their parameters, and dataset categories. Log the model performance and throughputs to Comet as a means to track model drift, or export them to a CSV file. You can check out the &lt;a href="https://openvinotoolkit.github.io/anomalib/tutorials/benchmarking.html"&gt;full documentation here&lt;/a&gt;, and once your configuration is decided, perform your benchmarking with one simple command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python tools/benchmarking/benchmark.py \
--config &amp;lt;relative-or-absolute-path&amp;gt;/&amp;lt;paramfile&amp;gt;.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hyperparameter Optimization
&lt;/h3&gt;

&lt;p&gt;Anomalib also supports hyperparameter optimization with the Comet Optimizer, making it easier to isolate the right combination of hyperparameters. See &lt;a href="https://openvinotoolkit.github.io/anomalib/tutorials/hyperparameter_optimization.html"&gt;here&lt;/a&gt; for Anomalib’s HPO docs, or see &lt;a href="https://www.comet.com/docs/v2/api-and-sdk/python-sdk/introduction-optimizer/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;here&lt;/a&gt; for details on other possible configurations with Comet's Optimizer.&lt;/p&gt;

&lt;p&gt;At the top of your Comet optimizer report, you’ll find each of your hyperparameters ranked by the evaluation method of your choice, in order of the largest magnitude of &lt;a href="https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient#:~:text=%2C%20is%20a%20nonparametric%20measure%20of,described%20using%20a%20monotonic%20function."&gt;Spearman correlation coefficient&lt;/a&gt;, to the smallest. In the snapshot below, the learning rate had the largest correlation coefficient with the model’s F1 score.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k76BLaAm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f6wfz9qi5wsbm1v88use.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k76BLaAm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f6wfz9qi5wsbm1v88use.png" alt="Comet Optimizer report; image by author." width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Comet Optimizer report; image by author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You’ll also find all of your experiment runs ranked by your evaluation metric, with the additional option to toggle the ranking based on any of the model’s parameters. Lastly, Comet plots your evaluation metrics across all experiment runs in line plots, and feel free to add any of Comet’s publicly available custom panels!&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;To start tracking your Anomalib projects with Comet, just follow the four quick steps below. Feel free to follow along with &lt;strong&gt;&lt;a href="https://colab.research.google.com/drive/10KHnJqXefh7Wbfdx5w6ht7LMDPhxEhKH#scrollTo=3Dr3VHzafL-z"&gt;this Colab tutorial&lt;/a&gt;&lt;/strong&gt;, and be sure to check out this &lt;a href="https://www.comet.com/sherpan/mvtec/view/NHQvFHshwKmTfdOdmtixHmv6G/panels?utm_source=Comet&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;completed Anomalib project here&lt;/a&gt;, courtesy of Comet’s own ML Growth Engineer, Sid Mehta.&lt;/p&gt;

&lt;h3&gt;
  
  
  0. Setup and Installation
&lt;/h3&gt;

&lt;p&gt;Clone the Anomalib repo into your environment and install the necessary dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/openvinotoolkit/anomalib.git
cd anomalib
pip install . --q
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1. Configure Comet Credentials
&lt;/h3&gt;

&lt;p&gt;If you don’t already have a Comet account, you can sign up for free &lt;a href="https://www.comet.com/signup?utm_source=colab&amp;amp;utm_medium=data_panels_blog&amp;amp;utm_campaign=DevTo"&gt;here&lt;/a&gt;. Make sure to grab your API key from your account settings so you can configure your Comet credentials in &lt;a href="https://www.comet.com/docs/v2/guides/tracking-ml-training/configuring-comet/?utm_source=Medium&amp;amp;utm_medium=referral&amp;amp;utm_content=Comet+Anomalib"&gt;any of several ways&lt;/a&gt;. For the sake of simplicity, we'll set them directly through environment variables here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export COMET_API_KEY = &amp;lt;Your-Comet-API-Key&amp;gt;
export COMET_PROJECT_NAME = &amp;lt;Your-Comet-Project-Name&amp;gt; # this will default to the name of your dataset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Modify the Anomalib config File
&lt;/h3&gt;

&lt;p&gt;Next, we’ll need to modify our Anomalib config file to enable logging. The easiest way to do this is to open the existing configuration file at &lt;code&gt;anomalib/anomalib/models/&amp;lt;model-of-your-choice&amp;gt;/config.yaml&lt;/code&gt; and adjust the following parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;visualization:
    show_images: true
    save_images: true
    log_images: true
    mode: full # options: ["full", "simple"]
logging:
    logger: comet
    log_graph: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can copy your particular model's default config template into a new YAML file and adjust the parameters as needed. In &lt;a href="https://www.comet.com/signup?utm_source=colab&amp;amp;utm_medium=referral&amp;amp;utm_campaign=AMS_US_EN_AWA_Online_Anomalib_Comet_Integration"&gt;this Colab notebook&lt;/a&gt;, we’ve also demonstrated how to use &lt;code&gt;pyyaml&lt;/code&gt; to write a config file in an interactive environment. &lt;strong&gt;&lt;em&gt;Note&lt;/em&gt;&lt;/strong&gt; that each model supported by Anomalib has a different config file structure.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Training
&lt;/h3&gt;

&lt;p&gt;By default, &lt;code&gt;!python tools/train.py&lt;/code&gt; runs the PaDiM model on the bottle category from the MVTec AD (CC BY-NC SA 4.0) dataset.&lt;/p&gt;

&lt;p&gt;To use a different algorithm, just switch out the model name in the config file path to another supported algorithm. To use a custom dataset, just update the relevant Anomalib config file accordingly with the path to your dataset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python tools/train.py \
--config anomalib/models/&amp;lt;specific-model-name&amp;gt;/&amp;lt;config-file&amp;gt;.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now just head over to the Comet UI to check out your results!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In the real world, machine learning is a highly iterative process and you’ll likely have many more training runs to visualize and organize. Comet makes it easy to track these runs, share them with members of your team, and collaborate within your organization. By pairing Anomalib with Comet, you can take advantage of all the cutting-edge algorithms of Anomalib, and create production-worthy, maintainable models for your next Industry 4.0 project.&lt;/p&gt;

&lt;p&gt;Thanks for making it all the way to the end of this article, and we hope you found this tutorial useful! Feel free to drop any questions or feedback in the comments below, and stay tuned for more content!&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>ai</category>
      <category>machinelearning</category>
      <category>python</category>
    </item>
    <item>
      <title>Credit Card Fraud Detection With Autoencoders</title>
      <dc:creator>Abby</dc:creator>
      <pubDate>Sat, 15 Jul 2023 15:37:45 +0000</pubDate>
      <link>https://dev.to/anmorgan2414/credit-card-fraud-detection-with-autoencoders-448o</link>
      <guid>https://dev.to/anmorgan2414/credit-card-fraud-detection-with-autoencoders-448o</guid>
      <description>&lt;p&gt;In this article, we'll leverage the power of autoencoders to address a key issue for banks and their customers: credit card fraud. Feel free to follow along with the full code tutorial in this &lt;a href="https://colab.research.google.com/drive/1Jy3IiJenxeEO6TtfctlnZSaoNJo3sIZv?usp=sharing#scrollTo=NptUFQUBY9f9"&gt;&lt;strong&gt;Colab&lt;/strong&gt;&lt;/a&gt; and get the &lt;a href="https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud"&gt;Kaggle&lt;/a&gt; dataset here.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;If you can't wait, check out the final &lt;a href="https://www.comet.com/examples/demo-autoencoder-pytorch-fraud/view/lfaaPYvJBLFEJpgDzUPli3hHZ/panels?utm_source=DevTo&amp;amp;utm_medium=referral&amp;amp;utm_content=data_panels_blog"&gt;public project here!&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Credit Card Fraud
&lt;/h1&gt;

&lt;p&gt;Credit card fraud represents an important, yet complex challenge for banks. In 2021 alone, &lt;a href="https://nilsonreport.com/mention/1515/1link/"&gt;Nilson&lt;/a&gt; estimated that credit card fraud had surpassed $28.5 billion dollars worldwide. And while effective fraud detection efforts, like the use of machine learning and deep learning algorithms, have helped bring about a steady decline in fraud since its peak in 2016, fraud still represents about 6.8% of international credit card transactions.&lt;/p&gt;

&lt;p&gt;But fraudulent financial data comes with its own set of unique challenges. Fraudulent transactions represent anomalous data that make up a very small percentage of total transactions. This results in significant class imbalances that make many traditional evaluation metrics, like &lt;a href="https://machinelearningmastery.com/failure-of-accuracy-for-imbalanced-class-distributions/"&gt;accuracy&lt;/a&gt;, irrelevant.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;If you aren't already, you can follow along with the full code in this &lt;a href="https://colab.research.google.com/drive/1Jy3IiJenxeEO6TtfctlnZSaoNJo3sIZv?usp=sharing#scrollTo=NptUFQUBY9f9"&gt;Colab&lt;/a&gt;.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  The Data
&lt;/h1&gt;

&lt;p&gt;Our data consists of two days' worth of real bank transactions made by European cardholders in 2013. In this dataset, fraud makes up just 0.172% of all transactions, meaning a model that predicted "no fraud" for every single observation would still achieve an accuracy of over 98%! In the pie chart below, fraudulent transactions are represented by the tiny orange sliver.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QPq7Zoua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/480wxfcvbkbgfyvgyv3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QPq7Zoua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/480wxfcvbkbgfyvgyv3a.png" alt="1. Pie chart by author; note the tiny proportion of fraudulent samples" width="412" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, in order to protect all personal identifiable information (PII) in this dataset, all features (besides &lt;code&gt;Time&lt;/code&gt; and &lt;code&gt;Amount&lt;/code&gt;) have undergone a PCA transformation. This means that we cannot use any domain knowledge for the purposes of feature engineering or selection.&lt;/p&gt;

&lt;h1&gt;
  
  
  Data Visualization and Pre-processing
&lt;/h1&gt;

&lt;p&gt;Let's start out by visualizing our data. It would be impossible to plot 30 dimensions, so first we'll apply &lt;a href="https://scikit-learn.org/stable/modules/generated/sklearn.manifold.TSNE.html"&gt;Scikit-Learn's implementation of t-SNE&lt;/a&gt; to the data. &lt;a href="https://en.wikipedia.org/wiki/T-distributed_stochastic_neighbor_embedding"&gt;t-SNE&lt;/a&gt; is a dataset decomposition technique. Here we only plot the first two components with maximum information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--27YEHl3m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0108qtugfsag8q5q0u7j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--27YEHl3m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0108qtugfsag8q5q0u7j.png" alt="2. TSNE plot by author; the two classes are effectively indistinguishable" width="728" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can observe in this graph that there is little difference between fraudulent and non-fraudulent transactions. Most machine learning models would struggle to classify this data as-is.&lt;/p&gt;

&lt;p&gt;First, we'll perform some very basic transformations to the data before feeding it to our model. The original &lt;code&gt;Time&lt;/code&gt; feature represents the number of seconds elapsed between each transaction and the first transaction in the data. We'll convert this relative time measure to hour-of-the-day.&lt;/p&gt;

&lt;p&gt;We also scale the &lt;code&gt;Time&lt;/code&gt; and &lt;code&gt;Amount&lt;/code&gt; features, as all other features (&lt;code&gt;V1&lt;/code&gt;, &lt;code&gt;V2&lt;/code&gt;, … &lt;code&gt;V28&lt;/code&gt;) were previously scaled during the PCA transformation. Finally, because we are training an autoencoder to "learn" the embeddings of a normal transaction, we'll subset just normal samples as our training dataset, and use a 50:50 split of normal and fraudulent samples for our validation set. We'll also set aside 250 samples for a test dataset of completely unseen data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Convert relative 'Time' measure to hour of day
data["Time"] = data["Time"].apply(lambda x : x / 3600 % 24)

# Scale 'Time' and 'Amount'
data['Amount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
data['Time'] = StandardScaler().fit_transform(data['Time'].values.reshape(-1, 1))

# Separate fraudulent transactions from normal transactions
norm_data, fraud_data = data[data['Class']==0], data[data['Class']==1]

# Training data will be 2000 normal samples
# Validation data will be 1000 samples, approximately half fraud and half normal samples, shuffled and with indices reset
# Leave out 200 normal samples and 50 fraud samples for a final test dataset of 250 unseen samples; we'll choose a random subsample of these later
train_data = norm_data.iloc[:2000, :]
val_data = pd.concat([norm_data.iloc[2000:2558,:], fraud_data.iloc[:442, :]], axis =0).sample(frac=1).reset_index(drop=True)
test_samples = pd.concat([norm_data.iloc[2558:2608,:], fraud_data.iloc[442:,:]], axis=0).sample(frac=1).reset_index(drop=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Attacking Data Imbalance With Autoencoders
&lt;/h1&gt;

&lt;p&gt;Due to the stark class imbalances in this dataset, for this challenge we will use an Autoencoder. Autoencoders learn an implicit representation of normality from the "normal" samples, allowing us to reserve our sparse fraudulent data samples for testing. During inference, new samples are compared against the embeddings of normal samples to determine whether or not they are fraudulent.&lt;/p&gt;

&lt;h1&gt;
  
  
  Model Training and Inference
&lt;/h1&gt;

&lt;p&gt;We define a simple Autoencoder model with two fully connected linear layers and the Tanh activation function. We'll be using mean squared error as our loss function and an Adam optimizer. We'll log all of this information to &lt;a href="https://www.comet.com/site/?utm_source=DevTo&amp;amp;utm_medium=referral&amp;amp;utm_content=data_panels_blog"&gt;Comet&lt;/a&gt;, an experiment tracking tool, so we can compare how different experiment runs performed later on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Autoencoder(nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder = nn.Sequential(
            nn.Linear(30,14),
            nn.Tanh(),
            nn.Linear(14,7),
            nn.Tanh(),
            )
        self.decoder = nn.Sequential(
            nn.Linear(7, 14),
            nn.Tanh(),
            nn.Linear(14,30),
            nn.Tanh()
            )
    def forward(self,x):
        x = self.encoder(x)
        x = self.decoder(x)
        return x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can quickly visualize this model architecture with the following diagram. Note that we start with 30 input nodes, then encode this information down to seven nodes, and then decode back to 30 output nodes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vE2pZCJ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48tdw6r8ehutbfiskpv9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vE2pZCJ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48tdw6r8ehutbfiskpv9.png" alt="3. A diagram of our simple autoencoder; Image by author; Neural network diagram created using NN-SVG and labeled with Canva" width="800" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we define our training and validation loops. Note that we create our &lt;code&gt;val_predictions&lt;/code&gt; table by defining a pandas DataFrame within our validation loop and appending the relevant columns. We'll use this DataFrame to examine sample-level metrics in Comet after inference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for epoch in range(hyper_params['epochs']):
    losses = []
    for (data, _) in train_loader:
        # ===================forward=====================
        output = model(data)
        loss = criterion(output, data)     
        # ===================backward====================
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        losses.append(loss.detach().cpu().numpy())
    # ===================log========================
    experiment.log_metric('epoch_loss', np.mean(losses), epoch = epoch+1)

    losses = []
    labels = []
    preds = []
    embeds = []

    for data, label in val_loader:
        # ===================forward=====================
        reconstructed = model(data)
        loss = torch.sigmoid(criterion(reconstructed, data)).item()
        losses.append(round((loss),6))
        labels.append(label.item())
        preds.append(reconstructed[0][-1].item())
        embeds.append(reconstructed[0][:-1].detach().cpu().numpy())
    # ===================log========================  
    experiment.log_metric('val_loss', np.mean(losses), epoch = epoch +1)
    if epoch == (hyper_params["epochs"] - 1):
      print("logging table")
      df = pd.DataFrame()
      df['Reconstruction_Loss'] = losses
      df['Labels_gt'] = labels
      df['Labels_preds']= [int(x &amp;gt;= hyper_params['threshold']) for x in losses]
      experiment.log_table('val_predictions.csv', df)
      print('complete')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Precision-recall Curve vs Threshold Values
&lt;/h1&gt;

&lt;p&gt;Now that we've run training and validation loops, let's examine the precision-recall tradeoff of our model at various threshold levels and plot the reconstruction error against our chosen threshold. To see how we defined our custom plotting functions, see the &lt;a href="https://colab.research.google.com/drive/1Jy3IiJenxeEO6TtfctlnZSaoNJo3sIZv?usp=sharing#scrollTo=NptUFQUBY9f9"&gt;&lt;strong&gt;Colab here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DgcC2FUI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5chdri2406zvis91w1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DgcC2FUI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5chdri2406zvis91w1s.png" alt="4. Precision-recall curve plot by author; the ideal threshold is around 0.75" width="800" height="579"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the plot above, we can see the optimal threshold value for fraudulent transactions is around 0.75. Now let's plot the precision-recall tradeoff of the model with the threshold set to this value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RWhPSlVm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzqqul2am2eyfmibk9sm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RWhPSlVm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzqqul2am2eyfmibk9sm.png" alt="5. Precision-recall curve plot by author; Precision-recall curve of our autoencoder’s performance on the dataset" width="800" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Visualizing Reconstruction Error
&lt;/h1&gt;

&lt;p&gt;Finally, let's visualize the reconstruction error with the threshold level applied to see how well it separates fraudulent transactions from non-fraudulent transactions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--upF8-fO7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d99esyy3p65bn3fp0cf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--upF8-fO7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d99esyy3p65bn3fp0cf4.png" alt="6. Scatter plot by author; reconstructed labels with threshold of 0.75" width="800" height="562"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although some outliers are still misclassified, the threshold separates the vast majority of the reconstructed labels. Note that we used a 50:50 split of fraudulent and non-fraudulent data for the validation split.&lt;/p&gt;

&lt;p&gt;To get a better understanding of how our autoencoder has learned to distinguish between the two classes, let's visualize plots of the raw data (before encoding) and the transformed data (after reconstruction). Here again, we'll apply t-SNE to each dataset and plot the first two components with maximum information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4VhEt1ao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjhah081vzrmx5zbsgoo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4VhEt1ao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjhah081vzrmx5zbsgoo.png" alt="7. Scatter plots by author; t-SNE plots of raw data and reconstructed data" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clearly the data on the right is much easier to separate (and therefore classify), than the data plotted on the left. It would be very difficult for a machine learning algorithm to classify the data on the left at all, so our autoencoder has proven very helpful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MjyWTEEC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptcmrw0pb1gj46t92nvo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MjyWTEEC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptcmrw0pb1gj46t92nvo.png" alt="8. Scatter plot by author; t-SNE plot of our reconstructed data&amp;lt;br&amp;gt;
" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Classification Report and Evaluation
&lt;/h1&gt;

&lt;p&gt;Let's also take a look at the &lt;a href="https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html"&gt;classification report&lt;/a&gt; of our validation data. Note that although the original dataset was highly imbalanced, we made a validation set that was roughly equally split between normal and fraudulent transactions. This gives our model a better opportunity to predict both classes, and also makes the evaluation metrics much more relevant.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_dhfmKwa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z992ucmk138ir9yex9cd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_dhfmKwa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z992ucmk138ir9yex9cd.png" alt="9. Classification report" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With a &lt;a href="https://en.wikipedia.org/wiki/Precision_and_recall"&gt;precision&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Precision_and_recall"&gt;recall&lt;/a&gt;, and &lt;a href="https://en.wikipedia.org/wiki/F-score"&gt;F1-score&lt;/a&gt; of 0.91, our model has done well! But can we do any better? Let's take a look at the data we logged to Comet to identify where our model might be improved. We can also check out how to monitor testing data statistics as our model gets a chance to look at new, unseen data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Zu64JTX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3aofcblho7pqflafp6wi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Zu64JTX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3aofcblho7pqflafp6wi.png" alt="Confusion matrix of autoencoder predictions in Comet ML" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Debugging Our Model With Data Panels
&lt;/h1&gt;

&lt;p&gt;Once we've run training and inference, we can head over the &lt;a href="https://www.comet.com/examples/demo-autoencoder-pytorch-fraud/view/lfaaPYvJBLFEJpgDzUPli3hHZ/panels?utm_source=DevTo&amp;amp;utm_medium=referral&amp;amp;utm_content=data_panels_blog"&gt;Comet UI&lt;/a&gt; to check out our Data Panels. Follow along with &lt;a href="https://www.comet.com/examples/demo-autoencoder-pytorch-fraud/view/lfaaPYvJBLFEJpgDzUPli3hHZ/panels?utm_source=DevTo&amp;amp;utm_medium=referral&amp;amp;utm_content=data_panels_blog"&gt;my public project here&lt;/a&gt; and feel free to experiment with customization options like join types, column order, and more. We'll also take a peek at how to filter and sort the Data Panel to identify misclassified samples for retraining.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SFnkIxye--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdrf8o96ypcrbtkpqxat.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SFnkIxye--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdrf8o96ypcrbtkpqxat.gif" alt="10. confusion matrix of autoencoder predictions" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Data Panels are interactive so we can filter samples based on the ground truth labels and compare them to the model's predictions. Since not detecting fraudulent cases when they are actually occurring could potentially cost financial institutions a lot of money, we specifically want to look at instances where there was fraud, but the model predicted there wasn't (&lt;a href="https://en.wikipedia.org/wiki/False_positives_and_false_negatives"&gt;false negatives&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;We can also gather all instances where the model predicted there was fraud but there wasn't (&lt;a href="https://en.wikipedia.org/wiki/False_positives_and_false_negatives"&gt;false positives&lt;/a&gt;). By re-training the autoencoder on these problematic samples, we can improve the model's representation of "normality" for future runs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TKguFRnv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrjrzze99e2rgrn90mqr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TKguFRnv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrjrzze99e2rgrn90mqr.gif" alt="11. Adding the Comet ML Data Panel to our default view" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also track how prediction columns are changing for the same samples across experiments. By concatenating the tables via columns, we can see side-by-side how our predictions compare to previous runs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6VIhegHd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3r0qk1yyk1mygs6ysl1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6VIhegHd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3r0qk1yyk1mygs6ysl1.gif" alt="12. Isolating false negative samples in Comet ML" width="800" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we can also compare distribution shifts in the input data. If we were using different versions of data across experiments or running experiments over time, it would be a good idea to log some basic summary statistics (like those calculated with &lt;a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.describe.html"&gt;pd.df.describe&lt;/a&gt;) of the input data. This will allow us to quantify the statistical differences between data and whether this is having a substantial impact on model performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# We take 10 random samples from the unseen test set we defined earlier
test_data = test_samples.sample(10).reset_index(drop=True)

# Log summary statistics of test data (only 5 columns, round to 4 decimal places)
experiment.log_table('input_statistics.csv', test_data.describe().iloc[:,[0, 1, 2, 29, 30]].round(4))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nOfyFEcf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh4lewy6xsur01xk03vf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nOfyFEcf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh4lewy6xsur01xk03vf.png" alt="13. Tracking sample predictions across experiment runs in Comet ML" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Thanks for making it all the way to the end and we hope you found this tutorial useful! To recap everything we've accomplished, we:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loaded a notoriously imbalanced dataset&lt;/li&gt;
&lt;li&gt;Constructed a simple Autoencoder model&lt;/li&gt;
&lt;li&gt;Plotted reconstruction labels and defined a classification threshold&lt;/li&gt;
&lt;li&gt;Calculated validation metrics&lt;/li&gt;
&lt;li&gt;Logged sample and project-level data to Comet, an experiment tracking tool&lt;/li&gt;
&lt;li&gt;Examined specific instances where the model struggled and identified areas for improvement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For questions, comments, or feedback, feel free to drop a note in the comments below. Happy coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  References and Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud"&gt;Kaggle's Credit Card Fraud Detection Dataset&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.comet.com/examples/demo-autoencoder-pytorch-fraud/view/lfaaPYvJBLFEJpgDzUPli3hHZ/panels?utm_source=DevTo&amp;amp;utm_medium=referral&amp;amp;utm_content=data_panels_blog"&gt;Our Comet public project with Data Panels&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cometml.slack.com/?redir=%2Fssb%2Fredirect#/shared-invite/email?utm_source=DevTo&amp;amp;utm_medium=referral&amp;amp;utm_content=data_panels_blog"&gt;Our Comet Community Slack channel&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>tutorial</category>
      <category>opensource</category>
      <category>python</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
