未验证 提交 24727034 编写于 作者: T Tingquan Gao 提交者: GitHub

Update whl (#871)

* Update whl

* Optimize usage of CLI args
上级 27849b7a
include LICENSE.txt include LICENSE.txt
include README.md include README.md
include docs/en/whl_en.md include docs/en/whl_en.md
recursive-include deploy/python predict_cls.py preprocess.py postprocess.py det_preprocess.py
recursive-include deploy/utils get_image_list.py config.py logger.py predictor.py
recursive-include tools/infer utils.py predict.py include ppcls/arch/backbone/__init__.py
recursive-include ppcls/utils logger.py recursive-include ppcls/arch/backbone/ *.py
recursive-include ppcls/utils imagenet1k_label_list.txt \ No newline at end of file
...@@ -128,6 +128,12 @@ class CropImage(object): ...@@ -128,6 +128,12 @@ class CropImage(object):
def __call__(self, img): def __call__(self, img):
w, h = self.size w, h = self.size
img_h, img_w = img.shape[:2] img_h, img_w = img.shape[:2]
if img_h < h or img_w < w:
raise Exception(
f"The size({h}, {w}) of CropImage must be greater than size({img_h}, {img_w}) of image. Please check image original size and size of ResizeImage if used."
)
w_start = (img_w - w) // 2 w_start = (img_w - w) // 2
h_start = (img_h - h) // 2 h_start = (img_h - h) // 2
......
...@@ -170,7 +170,7 @@ def get_config(fname, overrides=None, show=True): ...@@ -170,7 +170,7 @@ def get_config(fname, overrides=None, show=True):
return config return config
def parse_args(): def parser():
parser = argparse.ArgumentParser("generic-image-rec train script") parser = argparse.ArgumentParser("generic-image-rec train script")
parser.add_argument( parser.add_argument(
'-c', '-c',
...@@ -184,5 +184,14 @@ def parse_args(): ...@@ -184,5 +184,14 @@ def parse_args():
action='append', action='append',
default=[], default=[],
help='config options to be overridden') help='config options to be overridden')
args = parser.parse_args() parser.add_argument(
'-v',
'--verbose',
action='store_true',
help='wheather print the config info')
return parser
def parse_args():
args = parser().parse_args()
return args return args
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
...@@ -29,33 +15,43 @@ ...@@ -29,33 +15,43 @@
import os import os
import sys import sys
__dir__ = os.path.dirname(__file__) __dir__ = os.path.dirname(__file__)
sys.path.append(os.path.join(__dir__, '')) sys.path.append(os.path.join(__dir__, ""))
sys.path.append(os.path.join(__dir__, "deploy"))
import argparse import argparse
import shutil import shutil
import textwrap import textwrap
import tarfile
import requests
import warnings
from functools import partial
from difflib import SequenceMatcher from difflib import SequenceMatcher
from prettytable import PrettyTable
import cv2 import cv2
import numpy as np import numpy as np
import tarfile
import requests
from tqdm import tqdm from tqdm import tqdm
from tools.infer.utils import get_image_list, preprocess, save_prelabel_results from prettytable import PrettyTable
from tools.infer.predict import Predictor
from deploy.python.predict_cls import ClsPredictor
from deploy.utils.get_image_list import get_image_list
from deploy.utils import config
from ppcls.arch.backbone import *
__all__ = ["PaddleClas"]
__all__ = ['PaddleClas']
BASE_DIR = os.path.expanduser("~/.paddleclas/") BASE_DIR = os.path.expanduser("~/.paddleclas/")
BASE_INFERENCE_MODEL_DIR = os.path.join(BASE_DIR, 'inference_model') BASE_INFERENCE_MODEL_DIR = os.path.join(BASE_DIR, "inference_model")
BASE_IMAGES_DIR = os.path.join(BASE_DIR, 'images') BASE_IMAGES_DIR = os.path.join(BASE_DIR, "images")
model_series = { BASE_DOWNLOAD_URL = "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/{}_infer.tar"
MODEL_SERIES = {
"AlexNet": ["AlexNet"], "AlexNet": ["AlexNet"],
"DarkNet": ["DarkNet53"], "DarkNet": ["DarkNet53"],
"DeiT": [ "DeiT": [
'DeiT_base_distilled_patch16_224', 'DeiT_base_distilled_patch16_384', "DeiT_base_distilled_patch16_224", "DeiT_base_distilled_patch16_384",
'DeiT_base_patch16_224', 'DeiT_base_patch16_384', "DeiT_base_patch16_224", "DeiT_base_patch16_384",
'DeiT_small_distilled_patch16_224', 'DeiT_small_patch16_224', "DeiT_small_distilled_patch16_224", "DeiT_small_patch16_224",
'DeiT_tiny_distilled_patch16_224', 'DeiT_tiny_patch16_224' "DeiT_tiny_distilled_patch16_224", "DeiT_tiny_patch16_224"
], ],
"DenseNet": [ "DenseNet": [
"DenseNet121", "DenseNet161", "DenseNet169", "DenseNet201", "DenseNet121", "DenseNet161", "DenseNet169", "DenseNet201",
...@@ -150,42 +146,126 @@ model_series = { ...@@ -150,42 +146,126 @@ model_series = {
} }
class ModelNameError(Exception): class ImageTypeError(Exception):
""" ModelNameError """ImageTypeError.
""" """
def __init__(self, message=''): def __init__(self, message=""):
super().__init__(message) super().__init__(message)
class InputModelError(Exception):
"""InputModelError.
"""
def __init__(self, message=""):
super().__init__(message)
def args_cfg():
parser = config.parser()
other_options = [
("infer_imgs", str, None, "The image(s) to be predicted."),
("model_name", str, None, "The model name to be used."),
("inference_model_dir", str, None, "The directory of model files."),
("use_gpu", bool, True, "Whether use GPU. Default by True."), (
"enable_mkldnn", bool, False,
"Whether use MKLDNN. Default by False."),
("batch_size", int, 1, "Batch size. Default by 1.")
]
for name, opt_type, default, description in other_options:
parser.add_argument(
"--" + name, type=opt_type, default=default, help=description)
args = parser.parse_args()
for name, opt_type, default, description in other_options:
val = eval("args." + name)
full_name = "Global." + name
args.override.append(
f"{full_name}={val}") if val is not default else None
cfg = config.get_config(
args.config, overrides=args.override, show=args.verbose)
return cfg
def get_default_confg():
return {
"Global": {
"model_name": "MobileNetV3_small_x0_35",
"use_gpu": False,
"use_fp16": False,
"enable_mkldnn": False,
"cpu_num_threads": 1,
"use_tensorrt": False,
"ir_optim": False,
"enable_profile": False
},
"PreProcess": {
"transform_ops": [{
"ResizeImage": {
"resize_short": 256
}
}, {
"CropImage": {
"size": 224
}
}, {
"NormalizeImage": {
"scale": 0.00392157,
"mean": [0.485, 0.456, 0.406],
"std": [0.229, 0.224, 0.225],
"order": ""
}
}, {
"ToCHWImage": None
}]
},
"PostProcess": {
"name": "Topk",
"topk": 5,
"class_id_map_file": "./ppcls/utils/imagenet1k_label_list.txt"
}
}
def print_info(): def print_info():
table = PrettyTable(['Series', 'Name']) """Print list of supported models in formatted.
"""
table = PrettyTable(["Series", "Name"])
try: try:
sz = os.get_terminal_size() sz = os.get_terminal_size()
width = sz.columns - 30 if sz.columns > 50 else 10 width = sz.columns - 30 if sz.columns > 50 else 10
except OSError: except OSError:
width = 100 width = 100
for series in model_series: for series in MODEL_SERIES:
names = textwrap.fill(" ".join(model_series[series]), width=width) names = textwrap.fill(" ".join(MODEL_SERIES[series]), width=width)
table.add_row([series, names]) table.add_row([series, names])
print('Inference models that PaddleClas provides are listed as follows:') width = len(str(table).split("\n")[0])
print("{}".format("-" * width))
print("Models supported by PaddleClas".center(width))
print(table) print(table)
print("Powered by PaddlePaddle!".rjust(width))
print("{}".format("-" * width))
def get_model_names(): def get_model_names():
"""Get the model names list.
"""
model_names = [] model_names = []
for series in model_series: for series in MODEL_SERIES:
model_names += model_series[series] model_names += (MODEL_SERIES[series])
return model_names return model_names
def similar_architectures(name='', names=[], thresh=0.1, topk=10): def similar_architectures(name="", names=[], thresh=0.1, topk=10):
""" """Find the most similar topk model names.
inferred similar architectures
""" """
scores = [] scores = []
for idx, n in enumerate(names): for idx, n in enumerate(names):
if n.startswith('__'): if n.startswith("__"):
continue continue
score = SequenceMatcher(None, n.lower(), name.lower()).quick_ratio() score = SequenceMatcher(None, n.lower(), name.lower()).quick_ratio()
if score > thresh: if score > thresh:
...@@ -196,35 +276,44 @@ def similar_architectures(name='', names=[], thresh=0.1, topk=10): ...@@ -196,35 +276,44 @@ def similar_architectures(name='', names=[], thresh=0.1, topk=10):
def download_with_progressbar(url, save_path): def download_with_progressbar(url, save_path):
"""Download from url with progressbar.
"""
if os.path.isfile(save_path):
os.remove(save_path)
response = requests.get(url, stream=True) response = requests.get(url, stream=True)
total_size_in_bytes = int(response.headers.get('content-length', 0)) total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte block_size = 1024 # 1 Kibibyte
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True) progress_bar = tqdm(total=total_size_in_bytes, unit="iB", unit_scale=True)
with open(save_path, 'wb') as file: with open(save_path, "wb") as file:
for data in response.iter_content(block_size): for data in response.iter_content(block_size):
progress_bar.update(len(data)) progress_bar.update(len(data))
file.write(data) file.write(data)
progress_bar.close() progress_bar.close()
if total_size_in_bytes == 0 or progress_bar.n != total_size_in_bytes: if total_size_in_bytes == 0 or progress_bar.n != total_size_in_bytes or not os.path.isfile(
save_path):
raise Exception( raise Exception(
"Something went wrong while downloading model/image from {}". f"Something went wrong while downloading model/image from {url}")
format(url))
def maybe_download(model_storage_directory, url): def check_model_file(model_name):
# using custom model """Check the model files exist and download and untar when no exist.
"""
storage_directory = partial(os.path.join, BASE_INFERENCE_MODEL_DIR,
model_name)
url = BASE_DOWNLOAD_URL.format(model_name)
tar_file_name_list = [ tar_file_name_list = [
'inference.pdiparams', 'inference.pdiparams.info', 'inference.pdmodel' "inference.pdiparams", "inference.pdiparams.info", "inference.pdmodel"
] ]
if not os.path.exists( model_file_path = storage_directory("inference.pdmodel")
os.path.join(model_storage_directory, 'inference.pdiparams') params_file_path = storage_directory("inference.pdiparams")
) or not os.path.exists( if not os.path.exists(model_file_path) or not os.path.exists(
os.path.join(model_storage_directory, 'inference.pdmodel')): params_file_path):
tmp_path = os.path.join(model_storage_directory, url.split('/')[-1]) tmp_path = storage_directory(url.split("/")[-1])
print('download {} to {}'.format(url, tmp_path)) print(f"download {url} to {tmp_path}")
os.makedirs(model_storage_directory, exist_ok=True) os.makedirs(storage_directory(), exist_ok=True)
download_with_progressbar(url, tmp_path) download_with_progressbar(url, tmp_path)
with tarfile.open(tmp_path, 'r') as tarObj: with tarfile.open(tmp_path, "r") as tarObj:
for member in tarObj.getmembers(): for member in tarObj.getmembers():
filename = None filename = None
for tar_file_name in tar_file_name_list: for tar_file_name in tar_file_name_list:
...@@ -233,251 +322,192 @@ def maybe_download(model_storage_directory, url): ...@@ -233,251 +322,192 @@ def maybe_download(model_storage_directory, url):
if filename is None: if filename is None:
continue continue
file = tarObj.extractfile(member) file = tarObj.extractfile(member)
with open( with open(storage_directory(filename), "wb") as f:
os.path.join(model_storage_directory, filename),
'wb') as f:
f.write(file.read()) f.write(file.read())
os.remove(tmp_path) os.remove(tmp_path)
if not os.path.exists(model_file_path) or not os.path.exists(
params_file_path):
raise Exception(
f"Something went wrong while praparing the model[{model_name}] files!"
)
return storage_directory()
def load_label_name_dict(path):
if not os.path.exists(path): def save_prelabel_results(class_id, input_file_path, output_dir):
print( """Save the predicted image according to the prediction.
"Warning: If want to use your own label_dict, please input legal path!\nOtherwise label_names will be empty!" """
) output_dir = os.path.join(output_dir, str(class_id))
return None if not os.path.isdir(output_dir):
else: os.makedirs(output_dir)
result = {} shutil.copy(input_file_path, output_dir)
for line in open(path, 'r'):
partition = line.split('\n')[0].partition(' ')
try:
result[int(partition[0])] = str(partition[-1])
except:
result = {}
break
return result
def parse_args(mMain=True, add_help=True):
def str2bool(v):
return v.lower() in ("true", "t", "1")
if mMain == True:
# general params
parser = argparse.ArgumentParser(add_help=add_help)
parser.add_argument("--model_name", type=str)
parser.add_argument("-i", "--image_file", type=str)
parser.add_argument("--use_gpu", type=str2bool, default=False)
# params for preprocess
parser.add_argument("--resize_short", type=int, default=256)
parser.add_argument("--resize", type=int, default=224)
parser.add_argument("--normalize", type=str2bool, default=True)
parser.add_argument("-b", "--batch_size", type=int, default=1)
# params for predict
parser.add_argument(
"--model_file", type=str, default='') ## inference.pdmodel
parser.add_argument(
"--params_file", type=str, default='') ## inference.pdiparams
parser.add_argument("--ir_optim", type=str2bool, default=True)
parser.add_argument("--use_fp16", type=str2bool, default=False)
parser.add_argument("--use_tensorrt", type=str2bool, default=False)
parser.add_argument("--gpu_mem", type=int, default=8000)
parser.add_argument("--enable_profile", type=str2bool, default=False)
parser.add_argument("--top_k", type=int, default=1)
parser.add_argument("--enable_mkldnn", type=str2bool, default=False)
parser.add_argument("--cpu_num_threads", type=int, default=10)
# parameters for pre-label the images
parser.add_argument("--label_name_path", type=str, default='')
parser.add_argument(
"--pre_label_image",
type=str2bool,
default=False,
help="Whether to pre-label the images using the loaded weights")
parser.add_argument("--pre_label_out_idr", type=str, default=None)
return parser.parse_args()
else:
return argparse.Namespace(
model_name='',
image_file='',
use_gpu=False,
use_fp16=False,
use_tensorrt=False,
is_preprocessed=False,
resize_short=256,
resize=224,
normalize=True,
batch_size=1,
model_file='',
params_file='',
ir_optim=True,
gpu_mem=8000,
enable_profile=False,
top_k=1,
enable_mkldnn=False,
cpu_num_threads=10,
label_name_path='',
pre_label_image=False,
pre_label_out_idr=None)
class PaddleClas(object): class PaddleClas(object):
"""PaddleClas.
"""
print_info() print_info()
def __init__(self, **kwargs): def __init__(self,
model_names = get_model_names() config: dict=None,
process_params = parse_args(mMain=False, add_help=False) model_name: str=None,
process_params.__dict__.update(**kwargs) inference_model_dir: str=None,
use_gpu: bool=None,
if not os.path.exists(process_params.model_file): batch_size: int=None):
if process_params.model_name is None: """Init PaddleClas with config.
raise ModelNameError(
'Please input model name that you want to use!')
similar_names = similar_architectures(process_params.model_name,
model_names)
model_list = ', '.join(similar_names)
if process_params.model_name not in similar_names:
err = "{} is not exist! Maybe you want: [{}]" \
"".format(process_params.model_name, model_list)
raise ModelNameError(err)
if process_params.model_name in model_names:
url = 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/{}_infer.tar'.format(
process_params.model_name)
if not os.path.exists(
os.path.join(BASE_INFERENCE_MODEL_DIR,
process_params.model_name)):
os.makedirs(
os.path.join(BASE_INFERENCE_MODEL_DIR,
process_params.model_name))
download_path = os.path.join(BASE_INFERENCE_MODEL_DIR,
process_params.model_name)
maybe_download(model_storage_directory=download_path, url=url)
process_params.model_file = os.path.join(download_path,
'inference.pdmodel')
process_params.params_file = os.path.join(
download_path, 'inference.pdiparams')
process_params.label_name_path = os.path.join(
__dir__, 'ppcls/utils/imagenet1k_label_list.txt')
else:
raise Exception(
'The model inputed is {}, not provided by PaddleClas. If you want to use your own model, please input model_file as model path!'.
format(process_params.model_name))
else:
print('Using user-specified model and params!')
print("process params are as follows: \n{}".format(process_params))
self.label_name_dict = load_label_name_dict(
process_params.label_name_path)
self.args = process_params
self.predictor = Predictor(process_params)
def postprocess(self, output):
output = output.flatten()
classes = np.argpartition(output, -self.args.top_k)[-self.args.top_k:]
class_ids = classes[np.argsort(-output[classes])]
scores = output[class_ids]
label_names = [self.label_name_dict[c]
for c in class_ids] if self.label_name_dict else []
return {
"class_ids": class_ids,
"scores": scores,
"label_names": label_names
}
def predict(self, input_data): Args:
config: The config of PaddleClas's predictor, default by None. If default, the default configuration is used. Please refer doc for more information.
model_name: The model name supported by PaddleClas, default by None. If specified, override config.
inference_model_dir: The directory that contained model file and params file to be used, default by None. If specified, override config.
use_gpu: Wheather use GPU, default by None. If specified, override config.
batch_size: The batch size to pridict, default by None. If specified, override config.
"""
super().__init__()
self._config = config
self._check_config(model_name, inference_model_dir, use_gpu,
batch_size)
self._check_input_model()
self.cls_predictor = ClsPredictor(self._config)
def get_config(self):
"""Get the config.
""" """
predict label of img with paddleclas return self._config
def _check_config(self,
model_name=None,
inference_model_dir=None,
use_gpu=None,
batch_size=None):
if self._config is None:
self._config = get_default_confg()
warnings.warn("config is not provided, use default!")
self._config = config.AttrDict(self._config)
config.create_attr_dict(self._config)
if model_name is not None:
self._config.Global["model_name"] = model_name
if inference_model_dir is not None:
self._config.Global["inference_model_dir"] = inference_model_dir
if use_gpu is not None:
self._config.Global["use_gpu"] = use_gpu
if batch_size is not None:
self._config.Global["batch_size"] = batch_size
def _check_input_model(self):
"""Check input model name or model files.
"""
candidate_model_names = get_model_names()
input_model_name = self._config.Global.get("model_name", None)
inference_model_dir = self._config.Global.get("inference_model_dir",
None)
if input_model_name is not None:
similar_names = similar_architectures(input_model_name,
candidate_model_names)
similar_names_str = ", ".join(similar_names)
if input_model_name not in similar_names_str:
err = f"{input_model_name} is not exist! Maybe you want: [{similar_names_str}]"
raise InputModelError(err)
if input_model_name not in candidate_model_names:
err = f"{input_model_name} is not provided by PaddleClas. If you want to use your own model, please input model_file as model path!"
raise InputModelError(err)
self._config.Global.inference_model_dir = check_model_file(
input_model_name)
return
elif inference_model_dir is not None:
model_file_path = os.path.join(inference_model_dir,
"inference.pdmodel")
params_file_path = os.path.join(inference_model_dir,
"inference.pdiparams")
if not os.path.isfile(model_file_path) or not os.path.isfile(
params_file_path):
err = f"There is no model file or params file in this directory: {inference_model_dir}"
raise InputModelError(err)
return
else:
err = f"Please specify the model name supported by PaddleClas or directory contained model file and params file."
raise InputModelError(err)
return
def predict(self, input_data, print_pred=True):
"""Predict label of img with paddleclas.
Args: Args:
input_data(string, NumPy.ndarray): image to be classified, support: input_data(str, NumPy.ndarray):
string: local path of image file, internet URL, directory containing series of images; image to be classified, support: str(local path of image file, internet URL, directory containing series of images) and NumPy.ndarray(preprocessed image data that has 3 channels and accords with [C, H, W], or raw image data that has 3 channels and accords with [H, W, C]).
NumPy.ndarray: preprocessed image data that has 3 channels and accords with [C, H, W], or raw image data that has 3 channels and accords with [H, W, C]
Returns: Returns:
dict: {image_name: "", class_id: [], scores: [], label_names: []},if label name path == None,label_names will be empty. dict: {image_name: "", class_id: [], scores: [], label_names: []},if label name path == None,label_names will be empty.
""" """
if isinstance(input_data, np.ndarray): if isinstance(input_data, np.ndarray):
if not self.args.is_preprocessed: return self.cls_predictor.predict(input_data)
input_data = input_data[:, :, ::-1]
input_data = preprocess(input_data, self.args)
input_data = np.expand_dims(input_data, axis=0)
batch_outputs = self.predictor.predict(input_data)
result = {"filename": "image"}
result.update(self.postprocess(batch_outputs[0]))
return result
elif isinstance(input_data, str): elif isinstance(input_data, str):
input_path = input_data if input_data.startswith("http"):
# download internet image image_storage_dir = partial(os.path.join, BASE_IMAGES_DIR)
if input_path.startswith('http'): if not os.path.exists(image_storage_dir()):
if not os.path.exists(BASE_IMAGES_DIR): os.makedirs(image_storage_dir())
os.makedirs(BASE_IMAGES_DIR) image_save_path = image_storage_dir("tmp.jpg")
file_path = os.path.join(BASE_IMAGES_DIR, 'tmp.jpg') download_with_progressbar(input_data, image_save_path)
download_with_progressbar(input_path, file_path) input_data = image_save_path
print("Current using image from Internet:{}, renamed as: {}". warnings.warn(
format(input_path, file_path)) f"Image to be predicted from Internet: {input_data}, has been saved to: {image_save_path}"
input_path = file_path )
image_list = get_image_list(input_path) image_list = get_image_list(input_data)
total_result = [] batch_size = self._config.Global.get("batch_size", 1)
batch_input_list = [] pre_label_out_idr = self._config.Global.get("pre_label_out_idr",
False)
img_list = []
img_path_list = [] img_path_list = []
output_list = []
cnt = 0 cnt = 0
for idx, img_path in enumerate(image_list): for idx, img_path in enumerate(image_list):
img = cv2.imread(img_path) img = cv2.imread(img_path)
if img is None: if img is None:
print( warnings.warn(
"Warning: Image file failed to read and has been skipped. The path: {}". f"Image file failed to read and has been skipped. The path: {img_path}"
format(img_path)) )
continue continue
else: img_list.append(img)
img = img[:, :, ::-1] img_path_list.append(img_path)
data = preprocess(img, self.args) cnt += 1
batch_input_list.append(data)
img_path_list.append(img_path) if cnt % batch_size == 0 or (idx + 1) == len(image_list):
cnt += 1 outputs = self.cls_predictor.predict(img_list)
output_list.append(outputs[0])
if cnt % self.args.batch_size == 0 or (idx + 1 preds = self.cls_predictor.postprocess(outputs)
) == len(image_list): for nu, pred in enumerate(preds):
batch_outputs = self.predictor.predict( if pre_label_out_idr:
np.array(batch_input_list)) save_prelabel_results(pred["class_ids"][0],
for number, output in enumerate(batch_outputs): img_path_list[nu],
result = {"filename": img_path_list[number]} pre_label_out_idr)
result.update(self.postprocess(output)) if print_pred:
pred_str_list = [
result_str = "top-{} result: {}".format( f"filename: {img_path_list[nu]}",
self.args.top_k, result) f"top-{self._config.PostProcess.get('topk', 1)}"
print(result_str) ]
for k in pred:
total_result.append(result) pred_str_list.append(f"{k}: {pred[k]}")
if self.args.pre_label_image: print(", ".join(pred_str_list))
save_prelabel_results(result["class_ids"][0], img_list = []
img_path_list[number],
self.args.pre_label_out_idr)
batch_input_list = []
img_path_list = [] img_path_list = []
return total_result return output_list
else: else:
print( err = "Please input legal image! The type of image supported by PaddleClas are: NumPy.ndarray and string of local path or Ineternet URL"
"Error: Please input legal image! The type of image supported by PaddleClas are: NumPy.ndarray and string of local path or Ineternet URL" raise ImageTypeError(err)
) return
return []
# for CLI
def main(): def main():
# for cmd """Function API used for commad line.
args = parse_args(mMain=True) """
clas_engine = PaddleClas(**(args.__dict__)) cfg = args_cfg()
print('{}{}{}'.format('*' * 10, args.image_file, '*' * 10)) clas_engine = PaddleClas(cfg)
total_result = clas_engine.predict(args.image_file) clas_engine.predict(cfg["Global"]["infer_imgs"], print_pred=True)
return
print("Predict complete!")
if __name__ == '__main__': if __name__ == "__main__":
main() main()
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import sys
import inspect
from ppcls.arch.backbone.legendary_models.mobilenet_v1 import MobileNetV1_x0_25, MobileNetV1_x0_5, MobileNetV1_x0_75, MobileNetV1 from ppcls.arch.backbone.legendary_models.mobilenet_v1 import MobileNetV1_x0_25, MobileNetV1_x0_5, MobileNetV1_x0_75, MobileNetV1
from ppcls.arch.backbone.legendary_models.mobilenet_v3 import MobileNetV3_small_x0_35, MobileNetV3_small_x0_5, MobileNetV3_small_x0_75, MobileNetV3_small_x1_0, MobileNetV3_small_x1_25, MobileNetV3_large_x0_35, MobileNetV3_large_x0_5, MobileNetV3_large_x0_75, MobileNetV3_large_x1_0, MobileNetV3_large_x1_25 from ppcls.arch.backbone.legendary_models.mobilenet_v3 import MobileNetV3_small_x0_35, MobileNetV3_small_x0_5, MobileNetV3_small_x0_75, MobileNetV3_small_x1_0, MobileNetV3_small_x1_25, MobileNetV3_large_x0_35, MobileNetV3_large_x0_5, MobileNetV3_large_x0_75, MobileNetV3_large_x1_0, MobileNetV3_large_x1_25
from ppcls.arch.backbone.legendary_models.resnet import ResNet18, ResNet18_vd, ResNet34, ResNet34_vd, ResNet50, ResNet50_vd, ResNet101, ResNet101_vd, ResNet152, ResNet152_vd, ResNet200_vd from ppcls.arch.backbone.legendary_models.resnet import ResNet18, ResNet18_vd, ResNet34, ResNet34_vd, ResNet50, ResNet50_vd, ResNet101, ResNet101_vd, ResNet152, ResNet152_vd, ResNet200_vd
...@@ -51,7 +54,22 @@ from ppcls.arch.backbone.model_zoo.rexnet import ReXNet_1_0, ReXNet_1_3, ReXNet_ ...@@ -51,7 +54,22 @@ from ppcls.arch.backbone.model_zoo.rexnet import ReXNet_1_0, ReXNet_1_3, ReXNet_
from ppcls.arch.backbone.model_zoo.gvt import pcpvt_small, pcpvt_base, pcpvt_large, alt_gvt_small, alt_gvt_base, alt_gvt_large from ppcls.arch.backbone.model_zoo.gvt import pcpvt_small, pcpvt_base, pcpvt_large, alt_gvt_small, alt_gvt_base, alt_gvt_large
from ppcls.arch.backbone.model_zoo.levit import LeViT_128S, LeViT_128, LeViT_192, LeViT_256, LeViT_384 from ppcls.arch.backbone.model_zoo.levit import LeViT_128S, LeViT_128, LeViT_192, LeViT_256, LeViT_384
from ppcls.arch.backbone.model_zoo.dla import DLA34, DLA46_c, DLA46x_c, DLA60, DLA60x, DLA60x_c, DLA102, DLA102x, DLA102x2, DLA169 from ppcls.arch.backbone.model_zoo.dla import DLA34, DLA46_c, DLA46x_c, DLA60, DLA60x, DLA60x_c, DLA102, DLA102x, DLA102x2, DLA169
from ppcls.arch.backbone.model_zoo.rednet import RedNet26, RedNet38, RedNet50, RedNet101, RedNet152 from ppcls.arch.backbone.model_zoo.rednet import RedNet26, RedNet38, RedNet50, RedNet101, RedNet152
from ppcls.arch.backbone.model_zoo.tnt import TNT_small from ppcls.arch.backbone.model_zoo.tnt import TNT_small
from ppcls.arch.backbone.model_zoo.hardnet import HarDNet68, HarDNet85, HarDNet39_ds, HarDNet68_ds from ppcls.arch.backbone.model_zoo.hardnet import HarDNet68, HarDNet85, HarDNet39_ds, HarDNet68_ds
from ppcls.arch.backbone.variant_models.resnet_variant import ResNet50_last_stage_stride1 from ppcls.arch.backbone.variant_models.resnet_variant import ResNet50_last_stage_stride1
def get_apis():
current_func = sys._getframe().f_code.co_name
current_module = sys.modules[__name__]
api = []
for _, obj in inspect.getmembers(current_module,
inspect.isclass) + inspect.getmembers(
current_module, inspect.isfunction):
api.append(obj.__name__)
api.remove(current_func)
return api
__all__ = get_apis()
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
...@@ -11,14 +11,14 @@ ...@@ -11,14 +11,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from setuptools import setup
from io import open from io import open
from setuptools import setup
with open('requirements.txt', encoding="utf-8-sig") as f: with open('requirements.txt', encoding="utf-8-sig") as f:
requirements = f.readlines() requirements = f.readlines()
def readme(): def readme():
with open('docs/en/whl_en.md', encoding="utf-8-sig") as f: with open('docs/en/whl_en.md', encoding="utf-8-sig") as f:
README = f.read() README = f.read()
...@@ -30,7 +30,9 @@ setup( ...@@ -30,7 +30,9 @@ setup(
packages=['paddleclas'], packages=['paddleclas'],
package_dir={'paddleclas': ''}, package_dir={'paddleclas': ''},
include_package_data=True, include_package_data=True,
entry_points={"console_scripts": ["paddleclas= paddleclas.paddleclas:main"]}, entry_points={
"console_scripts": ["paddleclas= paddleclas.paddleclas:main"]
},
version='0.0.0', version='0.0.0',
install_requires=requirements, install_requires=requirements,
license='Apache License 2.0', license='Apache License 2.0',
...@@ -40,10 +42,11 @@ setup( ...@@ -40,10 +42,11 @@ setup(
url='https://github.com/PaddlePaddle/PaddleClas', url='https://github.com/PaddlePaddle/PaddleClas',
download_url='https://github.com/PaddlePaddle/PaddleClas.git', download_url='https://github.com/PaddlePaddle/PaddleClas.git',
keywords=[ keywords=[
'A treasure chest for image classification powered by PaddlePaddle.' 'A treasure chest for image classification powered by PaddlePaddle.'
], ],
classifiers=[ classifiers=[
'Intended Audience :: Developers', 'Operating System :: OS Independent', 'Intended Audience :: Developers',
'Operating System :: OS Independent',
'Natural Language :: Chinese (Simplified)', 'Natural Language :: Chinese (Simplified)',
'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.2',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册