PaddleClas/ppcls/utils/check.py

137 lines
3.9 KiB
Python
Raw Normal View History

2020-04-09 02:16:30 +08:00
# Copyright (c) 2020 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.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
2020-04-25 15:05:33 +08:00
import os
2020-04-09 02:16:30 +08:00
import sys
import paddle.fluid as fluid
2020-04-13 19:36:02 +08:00
from ppcls.modeling import get_architectures
2020-04-09 02:16:30 +08:00
from ppcls.modeling import similar_architectures
from ppcls.utils import logger
def check_version():
"""
Log error and exit when the installed version of paddlepaddle is
not satisfied.
"""
2020-08-30 14:23:40 +08:00
err = "PaddlePaddle version 2.0.0 or higher is required, " \
2020-04-09 02:16:30 +08:00
"or a suitable develop version is satisfied as well. \n" \
"Please make sure the version is good with your code." \
try:
2020-08-30 14:23:40 +08:00
fluid.require_version('2.0.0')
2020-04-25 15:05:33 +08:00
except Exception:
2020-04-09 02:16:30 +08:00
logger.error(err)
sys.exit(1)
def check_gpu():
"""
Log error and exit when using paddlepaddle cpu version.
"""
err = "You are using paddlepaddle cpu version! Please try to " \
"install paddlepaddle-gpu to run model on GPU."
try:
assert fluid.is_compiled_with_cuda()
except AssertionError:
logger.error(err)
sys.exit(1)
def check_architecture(architecture):
"""
check architecture and recommend similar architectures
"""
2020-04-16 14:13:48 +08:00
assert isinstance(architecture, dict), \
2020-04-25 15:05:33 +08:00
("the type of architecture({}) should be dict". format(architecture))
2020-04-16 14:13:48 +08:00
assert "name" in architecture, \
2020-04-25 15:05:33 +08:00
("name must be in the architecture keys, just contains: {}". format(
architecture.keys()))
2020-04-16 14:13:48 +08:00
similar_names = similar_architectures(architecture["name"],
get_architectures())
2020-04-09 02:16:30 +08:00
model_list = ', '.join(similar_names)
2020-04-20 19:01:08 +08:00
err = "Architecture [{}] is not exist! Maybe you want: [{}]" \
2020-04-16 14:13:48 +08:00
"".format(architecture["name"], model_list)
2020-04-09 02:16:30 +08:00
try:
2020-04-16 14:13:48 +08:00
assert architecture["name"] in similar_names
2020-04-09 02:16:30 +08:00
except AssertionError:
logger.error(err)
sys.exit(1)
def check_mix(architecture, use_mix=False):
"""
check mix parameter
"""
err = "Cannot use mix processing in GoogLeNet, " \
"please set use_mix = False."
try:
2020-04-25 15:05:33 +08:00
if architecture["name"] == "GoogLeNet":
assert use_mix is not True
2020-04-09 02:16:30 +08:00
except AssertionError:
logger.error(err)
sys.exit(1)
def check_classes_num(classes_num):
"""
check classes_num
"""
err = "classes_num({}) should be a positive integer" \
2020-04-25 15:05:33 +08:00
"and larger than 1".format(classes_num)
2020-04-09 02:16:30 +08:00
try:
assert isinstance(classes_num, int)
assert classes_num > 1
except AssertionError:
logger.error(err)
sys.exit(1)
def check_data_dir(path):
"""
check cata_dir
"""
err = "Data path is not exist, please given a right path" \
"".format(path)
try:
assert os.isdir(path)
except AssertionError:
logger.error(err)
sys.exit(1)
def check_function_params(config, key):
"""
check specify config
"""
k_config = config.get(key)
assert k_config is not None, \
2020-04-25 15:05:33 +08:00
('{} is required in config'.format(key))
2020-04-09 02:16:30 +08:00
assert k_config.get('function'), \
2020-04-25 15:05:33 +08:00
('function is required {} config'.format(key))
2020-04-09 02:16:30 +08:00
params = k_config.get('params')
assert params is not None, \
2020-04-25 15:05:33 +08:00
('params is required in {} config'.format(key))
2020-04-09 02:16:30 +08:00
assert isinstance(params, dict), \
2020-04-25 15:05:33 +08:00
('the params in {} config should be a dict'.format(key))