在我初步尝试使用Anomalib训练了MVTecAD数据集和我自己的一个数据集后,我产生了这样一个困惑:每次训练我都要输入一些参数,例如:
anomalib train --data classification-depth.yaml --model anomalib.models.Patchcore --task CLASSIFICATION
这样每次输入一长串指令有些不方便。并且随着使用的深入和对模型了解的加深,我想要调节模型的一些参数,这时我就不得不翻阅模型的文档查看参数说明,并在指令中通过输入参数来进行设置。
怎么能够快速的对模型的所有参数完成配置呢?
其实在Anomalib的主页中已经给出了答案,只不过我当时没有弄懂。当我第一次学习使用Anomalib进行训练时,在它的主页上是这样提示的:
# Get help about the training arguments, run:
anomalib train -h
# Train by using the default values.
anomalib train --model Patchcore --data anomalib.data.MVTec
# Train by overriding arguments.
anomalib train --model Patchcore --data anomalib.data.MVTec --data.category transistor
# Train by using a config file.
anomalib train --config <path/to/config>
第一种当然很容易的运行起来了,会自动的下载模型和数据集;
第二种也很好理解,当默认参数不能满足你的需求的时候,你可以输入一些参数,来覆盖掉默认的参数;
但是当我最开始使用Anomalib时,我不知道第三种的config文件在哪里。其实在每次进行训练时,Anomalib都会自动的保存测试集的推理结果、模型和config文件,就在root/results/<model name>/<dataset name>/<version>/config.yaml。
配置文件的内容包括:
# anomalib==1.1.0
seed_everything: true
trainer:
accelerator: auto
strategy: auto
devices: 1
num_nodes: 1
precision: null
logger: null
callbacks: null
fast_dev_run: false
max_epochs: null
min_epochs: null
max_steps: -1
min_steps: null
max_time: null
limit_train_batches: null
limit_val_batches: null
limit_test_batches: null
limit_predict_batches: null
overfit_batches: 0.0
val_check_interval: null
check_val_every_n_epoch: 1
num_sanity_val_steps: null
log_every_n_steps: null
enable_checkpointing: null
enable_progress_bar: null
enable_model_summary: null
accumulate_grad_batches: 1
gradient_clip_val: null
gradient_clip_algorithm: null
deterministic: null
benchmark: null
inference_mode: true
use_distributed_sampler: true
profiler: null
detect_anomaly: false
barebones: false
plugins: null
sync_batchnorm: false
reload_dataloaders_every_n_epochs: 0
normalization:
normalization_method: MIN_MAX
task: CLASSIFICATION
metrics:
image:
- F1Score
- AUROC
pixel: null
threshold:
class_path: anomalib.metrics.F1AdaptiveThreshold
init_args:
default_value: 0.5
thresholds: null
ignore_index: null
validate_args: true
compute_on_cpu: false
dist_sync_on_step: false
sync_on_compute: true
compute_with_cache: true
logging:
log_graph: false
default_root_dir: results
ckpt_path: null
data:
class_path: anomalib.data.Folder
init_args:
name: JY-Depth-Fusion
normal_dir: train/OK
root: datasets/dataset3-depth-rectify/
abnormal_dir: '[''test/DK'', ''test/JY'']'
normal_test_dir: test/OK
mask_dir: null
normal_split_ratio: 0.0
extensions:
- .png
train_batch_size: 16
eval_batch_size: 16
num_workers: 8
image_size:
- 224
- 224
transform: null
train_transform: null
eval_transform: null
test_split_mode: from_dir
test_split_ratio: 0
val_split_mode: same_as_test
val_split_ratio: 0
seed: null
model:
class_path: anomalib.models.Patchcore
init_args:
backbone: wide_resnet50_2
layers:
- layer2
- layer3
pre_trained: true
coreset_sampling_ratio: 0.02
num_neighbors: 9
可以看到,主要有trainer,normalization,task,metrics,logging,data,model几大部分,这样一来,我们就不必去写输入参数了,还可以一目了然的看到所有参数的配置情况。那么我们只需要把配置文件中的参数设置好,然后简单的一行指令即可完成训练啦!
anomalib train --config <path/to/config>
使用这种配置文件的形式去训练自己的数据集,只需要按照我这篇博客Win11+docker+vscode配置anomalib并训练自己的数据(3)_anomalib训练自己的数据集-CSDN博客中的方法去修改data部分的参数即可。我前文所贴出来的config文件就是我修改好的。