赞
踩
every blog every motto: There’s only one corner of the universe you can be sure of improving, and that’s your own self.
https://blog.csdn.net/weixin_39190382?spm=1010.2135.3001.5343
异常检测库anomalib的使用
说明: 分类时不需要mask
数据: https://github.com/openvinotoolkit/anomalib/releases/download/hazelnut_toy_dataset/hazelnut_toy.zip
最新版的anomalib使用Python3.10
conda create -n anomalib_env python=3.10
conda activate anomalib_env
pip install anomalib
在异常检测过程中,可能存在异常图片无法获取的情况,所以分两种情况:
如下代码,使用Folder类,我们需要指定
至此,我们就生成了datamodule。
# Import the datamodule
from anomalib.data import Folder
# Create the datamodule
datamodule = Folder(
name="hazelnut_toy",
root="datasets/hazelnut_toy",
normal_dir="good",
abnormal_dir="crack",
task="classification",
)
# Setup the datamodule
datamodule.setup()
我们可以查看train,valid,test
i, train_data = next(enumerate(datamodule.train_dataloader()))
print(train_data.keys())
# dict_keys(['image_path', 'label', 'image'])
i, val_data = next(enumerate(datamodule.val_dataloader()))
print(val_data.keys())
# dict_keys(['image_path', 'label', 'image'])
i, test_data = next(enumerate(datamodule.test_dataloader()))
print(test_data.keys())
# dict_keys(['image_path', 'label', 'image'])
我们可以指定验证集和测试集的划分比例可划分模式
默认情况下正常图片为训练集,异常图片为测试集。
默认的验证集是从测试集进行划分的,默认划分比例为0.5
说明: 训练完会进行测试
# Import the model and engine
from anomalib.models import Patchcore
from anomalib.engine import Engine
# Create the model and engine
model = Patchcore()
engine = Engine(task="classification")
# Train a Patchcore model on the given datamodule
engine.train(datamodule=datamodule, model=model)
当只有正常图片时,有两种处理方式:
我们这里展示第二种方式
# Import the datamodule
from anomalib.data import Folder
from anomalib.data.utils import TestSplitMode
# Create the datamodule
datamodule = Folder(
name="hazelnut_toy",
root="datasets/hazelnut_toy",
normal_dir="good",
test_split_mode=TestSplitMode.SYNTHETIC,
task="classification",
)
# Setup the datamodule
datamodule.setup()
这里训练和上述类似,
# Import the model and engine
from anomalib.models import Patchcore
from anomalib.engine import Engine
# Create the model and engine
model = Patchcore()
engine = Engine(task="classification")
# Train a Patchcore model on the given datamodule
engine.train(datamodule=datamodule, model=model)
假设我们有一个数据集,其中训练集只包含正常图像,测试集同时包含正常和异常图像。我们还对测试集中的异常图像进行了掩码处理。我们想要训练一个能够检测出测试集中异常区域的异常分割模型
# Import the datamodule
from anomalib.data import Folder
# Create the datamodule
datamodule = Folder(
name="hazelnut_toy",
root="datasets/hazelnut_toy",
normal_dir="good",
abnormal_dir="crack",
mask_dir="mask/crack",
normal_split_ratio=0.2,
)
# Setup the datamodule
datamodule.setup()
其中datasets和dataloader访问方式如下:
# Access the datasets
train_dataset = datamodule.train_data
val_dataset = datamodule.val_data
test_dataset = datamodule.test_data
# Access the dataloaders
train_dataloader = datamodule.train_dataloader()
val_dataloader = datamodule.val_dataloader()
test_dataloader = datamodule.test_dataloader()
# Import the model and engine
from anomalib.models import Patchcore
from anomalib.engine import Engine
# Create the model and engine
model = Patchcore()
engine = Engine()
# Train a Patchcore model on the given datamodule
engine.train(datamodule=datamodule, model=model)
和上述分类一样,当只有正常图片时,有两种处理方式:
我们依然展示的是第二种情况
# Import the datamodule
from anomalib.data import Folder
from anomalib.data.utils import TestSplitMode
# Create the datamodule
datamodule = Folder(
name="hazelnut_toy",
root="datasets/hazelnut_toy",
normal_dir="good",
test_split_mode=TestSplitMode.SYNTHETIC,
)
# Setup the datamodule
datamodule.setup()
训练和上述类似
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。