mirror of
https://github.com/hanzoai/anomalib.git
synced 2026-10-30 18:48:11 +00:00
The previous commit was meant to remove .github/workflows and removed every tracked path instead — 892 files, the whole repo. This puts back the 874 that were never in scope; the eighteen workflow files stay gone, for the reasons that commit gives. Content is byte-identical to 9d2aec9 outside .github/workflows.
49 lines
1.5 KiB
Plaintext
49 lines
1.5 KiB
Plaintext
import torch
|
|
|
|
from anomalib.deploy import ExportType, TorchInferencer
|
|
|
|
engine = Engine()
|
|
model = Patchcore()
|
|
|
|
train_transform = Compose(
|
|
[
|
|
RandomHorizontalFlip(p=0.5),
|
|
Resize((256, 256)),
|
|
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
|
],
|
|
)
|
|
|
|
eval_transform = Compose(
|
|
[
|
|
Resize((256, 256)),
|
|
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
|
],
|
|
)
|
|
|
|
datamodule = MVTecAD(train_transform=train_transform, eval_transform=eval_transform)
|
|
|
|
engine.fit(model, datamodule=datamodule)
|
|
|
|
# after running fit, the used eval_transform will be stored in the model
|
|
model.transform
|
|
# Compose(
|
|
# Resize(size=[256, 256], interpolation=InterpolationMode.BILINEAR, antialias=True)
|
|
# Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], inplace=False)
|
|
# )
|
|
|
|
# when exporting the trained model, the transforms are added in the export
|
|
engine.export(model, export_type=ExportType.TORCH, export_root="./export_folder")
|
|
|
|
inferencer = TorchInferencer("export_folder/weights/torch/model.pt")
|
|
inferencer.model.transform
|
|
# Compose(
|
|
# Resize(size=[256, 256], interpolation=InterpolationMode.BILINEAR, antialias=True)
|
|
# Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], inplace=False)
|
|
# )
|
|
|
|
# since the transforms are included in the forward pass, they are applied automatically
|
|
image = torch.rand((3, 900, 900))
|
|
prediction = inferencer.predict(image)
|
|
prediction.pred_label
|
|
# <LabelName.ABNORMAL: 1>
|