[Question]: How to work with winCLIP on custom dataset? (also a call for better examples) #2446
-
Describe the bugFollowing the recent advance in Anomaly classification/segmentation, I wanted to try the new winCLIP model, of which the anomalib library also has an implementation. As this is a zero-shot model, i would assume that this would easily work out of the box. Anyone have any experience of how to test zero-shot or to give a couple "normal/healthy" images for few-shot? I can't get it working. I would assume that (as it's zero-shot) i would be able to just input some custom defect images? I also can't figure out the few-shot using some normal/healthy images from my own dataset. Here is my current code: from anomalib.models.image import WinClip
from anomalib.engine import Engine
# Import the datamodule
from anomalib.data import Folder
from anomalib.data.utils import TestSplitMode
# Create the datamodule
datamodule = Folder(
name="lasercut_plank",
root="./DATA_0shot",
normal_dir="normal",
test_split_mode=TestSplitMode.NONE
)
# Setup the datamodule
datamodule.setup()
# Access the datasets
train_dataset = datamodule.train_data
# Access the dataloaders
train_dataloader = datamodule.train_dataloader()
# Create the model and engine
model = WinClip(class_name="lasercut_plank")
engine = Engine(task="segmentation")
# Train a Patchcore model on the given datamodule
engine.train(datamodule=datamodule, model=model) DatasetOther (please specify in the text field below) ModelOther (please specify in the field below) Steps to reproduce the behaviorNone OS informationOS information: Name: anomalib Expected behaviornone ScreenshotsNo response Pip/GitHubpip What version/branch did you use?No response Configuration YAMLnone Logsnone Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi, you are right, WinClip is a zero-shot model, so in theory, you can use it out-of-the box to generate some predictions on local images. However, the full answer is a bit more nuanced. WinClip, like many other anomaly detection models, does not produce binary normal-vs-anomalous classification labels or segmentation masks, but instead produces real-valued anomaly scores (image-level) and anomaly maps (pixel-level). To convert the anomaly scores to labels, we need to compare them to some known threshold value (and assign a normal label to all scores below the threshold, and an anomalous label to all scores above the threshold). When using WinClip out-of-the-box to generate predictions on a new dataset, we don't know which threshold value we should use. This is why we usually run a validation sequence on a small subset of our data which contains some normal and anomalous samples. During this validation sequence, we adaptively compute the optimal threshold value by maximizing the F1 score over the validation set. The validation sequence also collects some statistics used for normalizing the raw anomaly scores and maps to the [0, 1] range. Now let's get back to your use-case. If retrieving just the raw anomaly scores is sufficient for your use-case, you could use WinClip to generate predictions by directly calling the engine's from anomalib.models import WinClip
from anomalib.engine import Engine
engine = Engine(task="classification")
model = WinClip(class_name="lasercut_plank")
predictions = engine.predict(model, data_path="./DATA_0shot/normal") You can access the raw predictions by inspecting the When running To improve this, you will have to provide Anomalib with some normal and anomalous validation images that can be used for normalization and thresholding. Here's some example code illustrating how this could be achieved. from anomalib.models import WinClip
from anomalib.engine import Engine
from anomalib.data import Folder
engine = Engine(task="classification")
model = WinClip(class_name="lasercut_plank")
datamodule = Folder(
name="lasercut_plank",
root="./DATA_0shot",
normal_dir="normal",
abnormal_dir="abnormal", # or wherever your abnormal images are located
task="classification", # we don't have ground-truth masks, so we can't use the segmentation task type
)
engine.validate(model, datamodule=datamodule)
engine.predict(model, data_path="path/to/your/query/images") Few-shot modeTo run WinClip in few-shot mode, simply set the model = WinClip(
class_name="lasercut_plank",
k_shot=2,
few_shot_source="path/to/normal/reference/images",
) |
Beta Was this translation helpful? Give feedback.
-
Thanks @djdameln for the nice explanation. Would it be an idea to rename |
Beta Was this translation helpful? Give feedback.
Hi, you are right, WinClip is a zero-shot model, so in theory, you can use it out-of-the box to generate some predictions on local images.
However, the full answer is a bit more nuanced. WinClip, like many other anomaly detection models, does not produce binary normal-vs-anomalous classification labels or segmentation masks, but instead produces real-valued anomaly scores (image-level) and anomaly maps (pixel-level). To convert the anomaly scores to labels, we need to compare them to some known threshold value (and assign a normal label to all scores below the threshold, and an anomalous label to all scores above the threshold).
When using WinClip out-of-the-box to generate predictions on…