Skip to content

Commit

Permalink
efficient ad tutorial fixes (#1187)
Browse files Browse the repository at this point in the history
tutorial fixes
add anomaly det to readme
  • Loading branch information
samuel-wj-chapman authored Sep 2, 2024
1 parent 598caeb commit 39334f1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tutorials/mct_model_garden/evaluation_metrics/anomaly_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def benchmark(unified_model, name, q_st_start=None, q_st_end=None, q_ae_start=No
auc = test(test_set=test_set, unified_model=unified_model, test_output_dir=test_output_dir, q_st_start=None, q_st_end=None, q_ae_start=None, q_ae_end=None, desc='Final inference')
print('Final image auc: {:.4f}'.format(auc))

def test(test_set, unified_model, test_output_dir=None, desc='Running inference'):
def test(test_set, unified_model, test_output_dir=None, q_st_start=None, q_st_end=None, q_ae_start=None, q_ae_end=None, desc='Running inference'):
"""Test the model and calculate the AUC score."""
y_true, y_score = [], []
for image, target, path in tqdm(test_set, desc=desc):
orig_width, orig_height = image.size
image = DEFAULT_TRANSFORM(image)[None] # Add batch dimension
if torch.cuda.is_available():
image = image.cuda()
map_combined = predict_combined(image, unified_model, q_st_start=None, q_st_end=None, q_ae_start=None, q_ae_end=None)
map_combined , _, _ = predict_combined(image, unified_model, q_st_start=None, q_st_end=None, q_ae_start=None, q_ae_end=None)
map_combined = torch.nn.functional.interpolate(map_combined, (orig_height, orig_width), mode='bilinear')
map_combined = map_combined[0, 0].detach().cpu().numpy()
defect_class = os.path.basename(os.path.dirname(path))
Expand Down
10 changes: 10 additions & 0 deletions tutorials/notebooks/imx500_notebooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ deployment performance.
<td>50.4</td>
<td>47.1</td>
</tr>
<tr>
<td>Anomaly Detection</td>
<td>Efficient AD</td>
<td> <a href="pytorch/pytorch_efficient_anomaly_detection.ipynb">PyTorch</a></td>
<td><a href="https://arxiv.org/pdf/2303.08730v3">Ultralytics</a></td>
<td><a href="https://huggingface.co/SSI-DNN/Efficient_Anomaly_Detection">mct-model-garden</a></td>
<td>MvTech</td>
<td>98.56</td>
<td>97.85</td>
</tr>

</table>

Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@
"outputs": [],
"source": [
"from huggingface_hub import hf_hub_download\n",
"import tutorials.mct_model_garden.models_pytorch.Efficient_Anomaly_Det as efficient_ad\n",
"import tutorials.mct_model_garden.models_pytorch.Efficient_Anomaly_Det.efficient_ad import get_pdn_small, get_autoencoder, UnifiedAnomalyDetectionModel\n",
"\n",
"out_channels = 384\n",
"\n",
"model_path = hf_hub_download(repo_id=\"SSI-DNN/Efficient_Anomaly_Detection\", filename=\"efficientAD_bottle.pth\")\n",
"\n",
"teacher = efficient_ad.get_pdn_small(out_channels)\n",
"student = efficient_ad.get_pdn_small(2 * out_channels)\n",
"autoencoder = efficient_ad.get_autoencoder(out_channels)\n",
"teacher = get_pdn_small(out_channels)\n",
"student = get_pdn_small(2 * out_channels)\n",
"autoencoder = get_autoencoder(out_channels)\n",
"\n",
"model = efficient_ad.UnifiedAnomalyDetectionModel.load_model(model_path, teacher, student, autoencoder)"
]
Expand Down Expand Up @@ -322,7 +322,9 @@
"outputs": [],
"source": [
"#We first need to calculate the normalisation values\n",
"tutorials.resources.utils.efficient_ad_utils import map_normalization, teacher_normalization\n",
"\n",
"teacher_mean, teacher_std = teacher_normalization(teacher, train_loader)\n",
"q_st_start, q_st_end, q_ae_start, q_ae_end = map_normalization(\n",
" validation_loader=train_loader, teacher=teacher, student=student,\n",
" autoencoder=autoencoder, teacher_mean=teacher_mean,\n",
Expand All @@ -341,7 +343,7 @@
},
"outputs": [],
"source": [
"from tutorials.mct_model_garden.anomaly_eval import benchmark\n",
"from tutorials.mct_model_garden.evaluation_metrics.anomaly_eval import benchmark\n",
"benchmark(model, 'mvtec_ad', q_st_start, q_st_end, q_ae_start, q_ae_end)"
]
},
Expand Down Expand Up @@ -392,8 +394,8 @@
},
"outputs": [],
"source": [
"from tutorials.mct_model_garden.anomaly_eval import benchmark\n",
"benchmark(quant_model, 'mvtec_ad_quant')"
"from tutorials.mct_model_garden.evaluation_metrics.anomaly_eval import benchmark\n",
"benchmark(quant_model, 'mvtec_ad_quant', q_st_start, q_st_end, q_ae_start, q_ae_end)"
]
},
{
Expand Down Expand Up @@ -442,7 +444,7 @@
"test_output_dir = os.path.join('output', 'anomaly_maps',\n",
" name, 'bottle', 'test')\n",
"model.eval()\n",
"visualize_anomalies(model, dataset_path, test_output_dir)"
"visualize_anomalies(model, dataset_path, test_output_dir, q_st_start, q_st_end, q_ae_start, q_ae_end)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions tutorials/resources/utils/efficient_ad_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def train_transform(image):
"""Apply transformations to the training images."""
return DEFAULT_TRANSFORM(image), DEFAULT_TRANSFORM(TRANSFORM_AE(image))

def visualize_anomalies(unified_model, dataset_path, test_output_dir=None, desc='Running inference'):
def visualize_anomalies(unified_model, dataset_path, test_output_dir=None, q_st_start=None, q_st_end=None, q_ae_start=None, q_ae_end=None, desc='Running inference'):
"""Visualize anomalies by overlaying heatmaps on the original images."""
test_set = ImageFolderWithPath(os.path.join(dataset_path, 'bottle', 'test'))
images_to_display = random.sample(list(test_set), 10) # Randomly select 10 images to display
Expand All @@ -78,7 +78,7 @@ def visualize_anomalies(unified_model, dataset_path, test_output_dir=None, desc=
for image, target, path in tqdm(images_to_display, desc=desc):
orig_width, orig_height = image.size
image_tensor = DEFAULT_TRANSFORM(image)[None] # Add batch dimension
map_combined = unified_model(image_tensor)
map_combined , _, _ = predict_combined(image_tensor, unified_model, q_st_start=None, q_st_end=None, q_ae_start=None, q_ae_end=None)
map_combined = torch.nn.functional.interpolate(map_combined, (orig_height, orig_width), mode='bilinear')
map_combined = map_combined[0, 0].detach().cpu().numpy()

Expand Down

0 comments on commit 39334f1

Please sign in to comment.