This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpredictor.py
48 lines (41 loc) · 1.81 KB
/
predictor.py
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
# Copyright 2021 The tfaip authors. All Rights Reserved.
#
# This file is part of tfaip.
#
# tfaip is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# tfaip is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# tfaip. If not, see http://www.gnu.org/licenses/.
# ==============================================================================
from tfaip import DataBaseParams, Sample
from tfaip.imports import MultiModelPredictor, MultiModelVoter
class TutorialVoter(MultiModelVoter):
"""
This MultiModelVoter performs a majority vote of several predictions.
Alternatively, one could sum up all probabilities of the classes and pick the argmax.
"""
def vote(self, sample: Sample) -> Sample:
# sample.outputs is a list of the output of each model
# just do a majority voting
counts = {}
for output in sample.outputs:
p = output["class"]
counts[p] = counts.get(p, 0) + 1
voted = max(counts.items(), key=lambda kv: kv[1])[0]
return sample.new_outputs({"class": voted})
class TutorialMultiModelPredictor(MultiModelPredictor):
"""
Tutorial class for a MultiModelPredictor to show how to implement a voting mechanism to vote the output of
multiple models.
"""
def create_voter(self, data_params: "DataBaseParams") -> MultiModelVoter:
# Create an instance of the voter
return TutorialVoter()