PaddleClas/ppcls/data/postprocess/threshoutput.py

63 lines
2.2 KiB
Python
Raw Normal View History

2022-05-23 09:11:03 +08:00
# copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
#
# 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.
2022-08-22 14:40:26 +08:00
import numpy as np
2022-05-23 09:11:03 +08:00
import paddle.nn.functional as F
class ThreshOutput(object):
def __init__(self, threshold, label_0="0", label_1="1"):
self.threshold = threshold
self.label_0 = label_0
self.label_1 = label_1
def __call__(self, x, file_names=None):
y = []
x = F.softmax(x, axis=-1).numpy()
for idx, probs in enumerate(x):
score = probs[1]
if score < self.threshold:
result = {"class_ids": [0], "scores": [1 - score], "label_names": [self.label_0]}
else:
result = {"class_ids": [1], "scores": [score], "label_names": [self.label_1]}
if file_names is not None:
result["file_name"] = file_names[idx]
y.append(result)
return y
2022-08-22 14:40:26 +08:00
class MultiLabelThreshOutput(object):
def __init__(self, threshold=0.5):
self.threshold = threshold
def __call__(self, x, file_names=None):
y = []
x = F.sigmoid(x).numpy()
for idx, probs in enumerate(x):
index = np.where(probs >= self.threshold)[0].astype("int32")
clas_id_list = []
score_list = []
for i in index:
clas_id_list.append(i.item())
score_list.append(probs[i].item())
result = {
"class_ids": clas_id_list,
"scores": np.around(
score_list, decimals=5).tolist(),
}
if file_names is not None:
result["file_name"] = file_names[idx]
y.append(result)
return y