Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update big model ram #3062

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion deploy/python/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, func_list, main_indicator="Topk"):
def __call__(self, x, image_file=None):
rtn = None
for func in self.func_list:
tmp = func(x, image_file)
tmp = func(*x, image_file)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改这个的目的是?

if type(func).__name__ in self.main_indicator:
rtn = tmp
return rtn
Expand Down Expand Up @@ -496,3 +496,61 @@ def __call__(self, batch_preds, file_names=None):
).astype(np.int8).tolist()
batch_res.append({"attributes": label_res, "output": pred_res})
return batch_res

class RamOutPut(object):

def __init__(self, language="cn",tag_list="", tag_list_chinese="", threshold=0.68, delete_tag_index=[], ram_class_threshold_path="ppcls/utils/RAM/ram_tag_list_threshold.txt"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文件需要过一下pre-commit,ram_class_threshold_path在yaml中传入

self.language = language
assert tag_list, tag_list_chinese
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要给出报错信息

self.tag_list = self.load_tag_list(tag_list)
self.delete_tag_index = delete_tag_index
self.tag_list_chinese = self.load_tag_list(tag_list_chinese)
self.num_class = len(self.tag_list)
self.class_threshold = paddle.ones([self.num_class]) * threshold
ram_class_threshold_path = ram_class_threshold_path
with open(ram_class_threshold_path, "r", encoding="utf-8") as f:
ram_class_threshold = [float(s.strip()) for s in f]
for key,value in enumerate(ram_class_threshold):
self.class_threshold[key] = value

def load_tag_list(self, tag_list_file):
with open(tag_list_file, "r", encoding="utf-8") as f:
tag_list = f.read().splitlines()
tag_list = np.array(tag_list)
return tag_list

def __call__(self, logits, bs, file_names=None):
batch_res = []
logits = paddle.to_tensor(logits)
if bs is None:
if len(logits.shape) <2:
bs = 1
else:
bs = logits.shape[0]
targets = paddle.where(
F.sigmoid(logits) > self.class_threshold,
paddle.to_tensor([1.0]),
paddle.zeros(self.num_class))
targets = targets.reshape([bs,-1])
res = {}
tag = targets.cpu().numpy()
tag[:,self.delete_tag_index] = 0
tag_output = []
tag_output_chinese = []
for b in range(bs):
index = np.argwhere(tag[b] == 1)
token = self.tag_list[index].squeeze(axis=1)
tag_output.append(" | ".join(token))
token_chinese = self.tag_list_chinese[index].squeeze(axis=1)
tag_output_chinese.append(" | ".join(token_chinese))
res["cn"] = tag_output_chinese
res["en"] = tag_output
res["all"] = f"en : {tag_output}, cn: {tag_output_chinese}"

outputformat = {
"class_ids": targets.nonzero(),
"scores": logits,
"label_names": res[self.language]
}
batch_res.append(outputformat)
return outputformat
52 changes: 52 additions & 0 deletions deploy/python/predict_multimodal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要增加版权信息


import cv2
import numpy as np

from paddleclas.deploy.utils import logger, config
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不需要从paddleclas下到入吧?

from paddleclas.deploy.utils.get_image_list import get_image_list
from .predict_cls import ClsPredictor


def main(config):
cls_predictor = ClsPredictor(config)
image_list = get_image_list(config["Global"]["infer_imgs"])

batch_imgs = []
batch_names = []
cnt = 0
for idx, img_path in enumerate(image_list):
img = cv2.imread(img_path)
if img is None:
logger.warning(
"Image file failed to read and has been skipped. The path: {}".
format(img_path))
else:
img = img[:, :, ::-1]
batch_imgs.append(img)
img_name = os.path.basename(img_path)
batch_names.append(img_name)
cnt += 1

if cnt % config["Global"]["batch_size"] == 0 or (idx + 1
) == len(image_list):
if len(batch_imgs) == 0:
continue
batch_results = cls_predictor.predict(batch_imgs)
for number, result_dict in enumerate(batch_results):
if len(batch_imgs) == 0:
continue
batch_results = cls_predictor.predict(batch_imgs)
for number, result_key in enumerate(batch_results.keys()):
print(f"{img_name}-{result_key}:{batch_results[result_key]}")
batch_imgs = []
batch_names = []
if cls_predictor.benchmark:
cls_predictor.auto_logger.report()
return


if __name__ == "__main__":
args = config.parse_args()
config = config.get_config(args.config, overrides=args.override, show=True)
main(config)
2 changes: 2 additions & 0 deletions ppcls/arch/backbone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
from .model_zoo.adaface_ir_net import AdaFace_IR_18, AdaFace_IR_34, AdaFace_IR_50, AdaFace_IR_101, AdaFace_IR_152, AdaFace_IR_SE_50, AdaFace_IR_SE_101, AdaFace_IR_SE_152, AdaFace_IR_SE_200
from .model_zoo.wideresnet import WideResNet
from .model_zoo.uniformer import UniFormer_small, UniFormer_small_plus, UniFormer_small_plus_dim64, UniFormer_base, UniFormer_base_ls
from .ram.ram import ram
from .ram.ram_plus import ram_plus


# help whl get all the models' api (class type) and components' api (func type)
Expand Down
Empty file.
Loading