File size: 3,936 Bytes
25cae60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright 2022 - Valeo Comfort and Driving Assistance - Oriane Siméoni @ valeo.ai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
from model import FoundModel
from misc import load_config
from datasets.datasets import build_dataset
from evaluation.saliency import evaluate_saliency
from evaluation.uod import evaluation_unsupervised_object_discovery

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description = 'Evaluation of FOUND',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument(
        "--eval-type",
        type=str,
        choices=["saliency", "uod"],
        help="Evaluation type."
    )
    parser.add_argument(
        "--dataset-eval",
        type=str,
        choices=["ECSSD", "DUT-OMRON", "DUTS-TEST", "VOC07", "VOC12", "COCO20k"],
        help="Name of evaluation dataset."
    )
    parser.add_argument(
        "--dataset-set-eval",
        type=str,
        default=None,
        help="Set of the dataset."
    )
    parser.add_argument(
        "--apply-bilateral",
        action="store_true", 
        help="use bilateral solver."
    )
    parser.add_argument(
        "--evaluation-mode",
        type=str,
        default="multi",
        choices=["single", "multi"],
        help="Type of evaluation."
    )
    parser.add_argument(
        "--model-weights",
        type=str,
        default="data/weights/decoder_weights.pt",
    )
    parser.add_argument(
        "--dataset-dir",
        type=str,
        default="/datasets_local",
    )
    parser.add_argument(
        "--config",
        type=str,
        default="configs/found_DUTS-TR.yaml",
    )
    args = parser.parse_args()
    print(args.__dict__)

    # Configuration
    config = load_config(args.config)

    # ------------------------------------
    # Load the model
    model = FoundModel(vit_model=config.model["pre_training"],
                        vit_arch=config.model["arch"],
                        vit_patch_size=config.model["patch_size"],
                        enc_type_feats=config.found["feats"],
                        bkg_type_feats=config.found["feats"],
                        bkg_th=config.found["bkg_th"])
    # Load weights
    model.decoder_load_weights(args.model_weights)
    model.eval()
    print(f"Model {args.model_weights} loaded correctly.")

    # ------------------------------------
    # Build the validation set
    val_dataset = build_dataset(
        root_dir=args.dataset_dir,
        dataset_name=args.dataset_eval,
        dataset_set=args.dataset_set_eval,
        for_eval=True,
        evaluation_type=args.eval_type,
    )
    print(f"\nBuilding dataset {val_dataset.name} (#{len(val_dataset)} images)")
    
    # ------------------------------------
    # Training
    print(f"\nStarted evaluation on {val_dataset.name}")
    if args.eval_type == "saliency":
        evaluate_saliency(
            val_dataset,
            model=model,
            evaluation_mode=args.evaluation_mode,
            apply_bilateral=args.apply_bilateral,
        )
    elif args.eval_type == "uod":
        if args.apply_bilateral:
            raise ValueError("Not implemented.")

        evaluation_unsupervised_object_discovery(
            val_dataset,
            model=model,
            evaluation_mode=args.evaluation_mode,
        )
    else:
        raise ValueError("Other evaluation method to come.")