提交 d6b6c6ff 编写于 作者: W wuzewu

Fix travis-ci issue.

上级 b100dc5d
...@@ -27,6 +27,7 @@ install: ...@@ -27,6 +27,7 @@ install:
else else
pip install --upgrade paddlepaddle; pip install --upgrade paddlepaddle;
pip install -r requirements.txt; pip install -r requirements.txt;
pip install yapf==0.26.0
fi fi
notifications: notifications:
......
...@@ -26,16 +26,15 @@ parser.add_argument("--wav", type=str, required=True, help="Audio file to infer. ...@@ -26,16 +26,15 @@ parser.add_argument("--wav", type=str, required=True, help="Audio file to infer.
parser.add_argument("--sr", type=int, default=44100, help="Sample rate of inference audio.") parser.add_argument("--sr", type=int, default=44100, help="Sample rate of inference audio.")
parser.add_argument("--model_type", type=str, default='panns_cnn14', help="Select model to to inference.") parser.add_argument("--model_type", type=str, default='panns_cnn14', help="Select model to to inference.")
parser.add_argument("--topk", type=int, default=1, help="Show top k results of prediction labels.") parser.add_argument("--topk", type=int, default=1, help="Show top k results of prediction labels.")
parser.add_argument("--checkpoint", parser.add_argument(
type=str, "--checkpoint", type=str, default='./checkpoint/best_model/model.pdparams', help="Checkpoint of model.")
default='./checkpoint/best_model/model.pdparams',
help="Checkpoint of model.")
args = parser.parse_args() args = parser.parse_args()
if __name__ == '__main__': if __name__ == '__main__':
label_map = {idx: label for idx, label in enumerate(ESC50.label_list)} label_map = {idx: label for idx, label in enumerate(ESC50.label_list)}
model = hub.Module(name=args.model_type, model = hub.Module(
name=args.model_type,
version='1.0.0', version='1.0.0',
task='sound-cls', task='sound-cls',
num_class=ESC50.num_class, num_class=ESC50.num_class,
......
...@@ -22,7 +22,8 @@ from paddlehub.datasets import ESC50 ...@@ -22,7 +22,8 @@ from paddlehub.datasets import ESC50
parser = argparse.ArgumentParser(__doc__) parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--num_epoch", type=int, default=50, help="Number of epoches for fine-tuning.") parser.add_argument("--num_epoch", type=int, default=50, help="Number of epoches for fine-tuning.")
parser.add_argument("--use_gpu", parser.add_argument(
"--use_gpu",
type=ast.literal_eval, type=ast.literal_eval,
default=True, default=True,
help="Whether use GPU for fine-tuning, input should be True or False") help="Whether use GPU for fine-tuning, input should be True or False")
......
...@@ -173,8 +173,3 @@ bash search.sh ...@@ -173,8 +173,3 @@ bash search.sh
cd PaddleHub/demo/autaug/ cd PaddleHub/demo/autaug/
bash train.sh bash train.sh
``` ```
...@@ -23,6 +23,7 @@ logger = log.get_logger(level=logging.INFO) ...@@ -23,6 +23,7 @@ logger = log.get_logger(level=logging.INFO)
import auto_augment import auto_augment
auto_augment_path = auto_augment.__file__ auto_augment_path = auto_augment.__file__
class HubFitterClassifer(object): class HubFitterClassifer(object):
"""Trains an instance of the Model class.""" """Trains an instance of the Model class."""
...@@ -43,8 +44,7 @@ class HubFitterClassifer(object): ...@@ -43,8 +44,7 @@ class HubFitterClassifer(object):
# `import paddle`. Otherwise, it would not take any effect. # `import paddle`. Otherwise, it would not take any effect.
set_paddle_flags( set_paddle_flags(
# enable GC to save memory # enable GC to save memory
FLAGS_fraction_of_gpu_memory_to_use=hparams.resource_config.gpu, FLAGS_fraction_of_gpu_memory_to_use=hparams.resource_config.gpu, )
)
import paddle import paddle
import paddlehub as hub import paddlehub as hub
from paddlehub_utils.trainer import CustomTrainer from paddlehub_utils.trainer import CustomTrainer
...@@ -63,23 +63,18 @@ class HubFitterClassifer(object): ...@@ -63,23 +63,18 @@ class HubFitterClassifer(object):
paddle.disable_static(paddle.CUDAPlace(paddle.distributed.get_rank())) paddle.disable_static(paddle.CUDAPlace(paddle.distributed.get_rank()))
train_dataset, eval_dataset = _init_loader(self.hparams) train_dataset, eval_dataset = _init_loader(self.hparams)
model = hub.Module(name=hparams["task_config"]["classifier"]["model_name"], label_list=self.class_to_id_dict.keys(), load_checkpoint=None) model = hub.Module(
name=hparams["task_config"]["classifier"]["model_name"],
optimizer = paddle.optimizer.Adam( label_list=self.class_to_id_dict.keys(),
learning_rate=0.001, parameters=model.parameters()) load_checkpoint=None)
trainer = CustomTrainer(
model=model, optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
optimizer=optimizer, trainer = CustomTrainer(model=model, optimizer=optimizer, checkpoint_dir='img_classification_ckpt')
checkpoint_dir='img_classification_ckpt')
self.model = model self.model = model
self.optimizer = optimizer self.optimizer = optimizer
trainer.init_train_and_eval( trainer.init_train_and_eval(
train_dataset, train_dataset, epochs=100, batch_size=32, eval_dataset=eval_dataset, save_interval=1)
epochs=100,
batch_size=32,
eval_dataset=eval_dataset,
save_interval=1)
self.trainer = trainer self.trainer = trainer
def _fit_param(self, show: bool = False) -> None: def _fit_param(self, show: bool = False) -> None:
...@@ -123,8 +118,7 @@ class HubFitterClassifer(object): ...@@ -123,8 +118,7 @@ class HubFitterClassifer(object):
""" """
self.hparams = new_hparams self.hparams = new_hparams
self.trainer.train_loader.dataset.reset_policy( self.trainer.train_loader.dataset.reset_policy(new_hparams.search_space)
new_hparams.search_space)
return None return None
def save_model(self, checkpoint_dir: str, step: Optional[str] = None) -> str: def save_model(self, checkpoint_dir: str, step: Optional[str] = None) -> str:
...@@ -134,8 +128,7 @@ class HubFitterClassifer(object): ...@@ -134,8 +128,7 @@ class HubFitterClassifer(object):
step: If provided, creates a checkpoint with the given step step: If provided, creates a checkpoint with the given step
number, instead of overwriting the existing checkpoints. number, instead of overwriting the existing checkpoints.
""" """
checkpoint_path = os.path.join(checkpoint_dir, checkpoint_path = os.path.join(checkpoint_dir, 'epoch') + '-' + str(step)
'epoch') + '-' + str(step)
logger.info('Saving model checkpoint to {}'.format(checkpoint_path)) logger.info('Saving model checkpoint to {}'.format(checkpoint_path))
self.trainer.save_model(os.path.join(checkpoint_path, "checkpoint")) self.trainer.save_model(os.path.join(checkpoint_path, "checkpoint"))
...@@ -145,8 +138,7 @@ class HubFitterClassifer(object): ...@@ -145,8 +138,7 @@ class HubFitterClassifer(object):
"""Loads a checkpoint with the architecture structure stored in the name.""" """Loads a checkpoint with the architecture structure stored in the name."""
ckpt_path = os.path.join(checkpoint_path, "checkpoint") ckpt_path = os.path.join(checkpoint_path, "checkpoint")
self.trainer.load_model(ckpt_path) self.trainer.load_model(ckpt_path)
logger.info( logger.info('Loaded child model checkpoint from {}'.format(checkpoint_path))
'Loaded child model checkpoint from {}'.format(checkpoint_path))
def eval_child_model(self, mode: str, pass_id: int = 0) -> dict: def eval_child_model(self, mode: str, pass_id: int = 0) -> dict:
"""Evaluate the child model. """Evaluate the child model.
...@@ -204,10 +196,7 @@ class HubFitterClassifer(object): ...@@ -204,10 +196,7 @@ class HubFitterClassifer(object):
"""Trains the model `m` for one epoch.""" """Trains the model `m` for one epoch."""
start_time = time.time() start_time = time.time()
train_acc = self.train_one_epoch(curr_epoch) train_acc = self.train_one_epoch(curr_epoch)
logger.info( logger.info('Epoch:{} time(min): {}'.format(curr_epoch, (time.time() - start_time) / 60.0))
'Epoch:{} time(min): {}'.format(
curr_epoch,
(time.time() - start_time) / 60.0))
return train_acc return train_acc
def _compute_final_accuracies(self, iteration: int) -> dict: def _compute_final_accuracies(self, iteration: int) -> dict:
...@@ -228,8 +217,7 @@ class HubFitterClassifer(object): ...@@ -228,8 +217,7 @@ class HubFitterClassifer(object):
self._fit_param() self._fit_param()
train_acc = self._run_training_loop(epoch) train_acc = self._run_training_loop(epoch)
valid_acc = self.eval_child_model(mode="val", pass_id=epoch) valid_acc = self.eval_child_model(mode="val", pass_id=epoch)
logger.info('valid acc: {}'.format( logger.info('valid acc: {}'.format(valid_acc))
valid_acc))
all_metric = {} all_metric = {}
all_metric.update(train_acc) all_metric.update(train_acc)
all_metric.update(valid_acc) all_metric.update(valid_acc)
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
# 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.
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# ******************************************************************************* # *******************************************************************************
# #
...@@ -36,8 +35,7 @@ import paddlehub.vision.transforms as transforms ...@@ -36,8 +35,7 @@ import paddlehub.vision.transforms as transforms
from PIL import ImageFile from PIL import ImageFile
from auto_augment.autoaug.transform.autoaug_transform import AutoAugTransform from auto_augment.autoaug.transform.autoaug_transform import AutoAugTransform
ImageFile.LOAD_TRUNCATED_IMAGES = True ImageFile.LOAD_TRUNCATED_IMAGES = True
__imagenet_stats = {'mean': [0.485, 0.456, 0.406], __imagenet_stats = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
'std': [0.229, 0.224, 0.225]}
class PbaAugment(object): class PbaAugment(object):
...@@ -45,8 +43,7 @@ class PbaAugment(object): ...@@ -45,8 +43,7 @@ class PbaAugment(object):
pytorch 分类 PbaAugment transform pytorch 分类 PbaAugment transform
""" """
def __init__( def __init__(self,
self,
input_size: int = 224, input_size: int = 224,
scale_size: int = 256, scale_size: int = 256,
normalize: Optional[list] = None, normalize: Optional[list] = None,
...@@ -64,27 +61,20 @@ class PbaAugment(object): ...@@ -64,27 +61,20 @@ class PbaAugment(object):
""" """
if normalize is None: if normalize is None:
normalize = { normalize = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
'mean': [
0.485, 0.456, 0.406], 'std': [
0.229, 0.224, 0.225]}
policy = kwargs["policy"] policy = kwargs["policy"]
assert stage in ["search", "train"] assert stage in ["search", "train"]
train_epochs = kwargs["hp_policy_epochs"] train_epochs = kwargs["hp_policy_epochs"]
self.auto_aug_transform = AutoAugTransform.create( self.auto_aug_transform = AutoAugTransform.create(policy, stage=stage, train_epochs=train_epochs)
policy, stage=stage, train_epochs=train_epochs)
#self.auto_aug_transform = PbtAutoAugmentClassiferTransform(conf) #self.auto_aug_transform = PbtAutoAugmentClassiferTransform(conf)
if pre_transform: if pre_transform:
self.pre_transform = transforms.Resize(input_size) self.pre_transform = transforms.Resize(input_size)
self.post_transform = transforms.Compose( self.post_transform = transforms.Compose(
transforms=[ transforms=[transforms.Permute(),
transforms.Permute(), transforms.Normalize(**normalize, channel_first=True)],
transforms.Normalize(**normalize, channel_first=True) channel_first=False)
],
channel_first = False
)
self.cur_epoch = 0 self.cur_epoch = 0
def set_epoch(self, indx: int) -> None: def set_epoch(self, indx: int) -> None:
...@@ -164,8 +154,7 @@ class PicReader(paddle.io.Dataset): ...@@ -164,8 +154,7 @@ class PicReader(paddle.io.Dataset):
PicReader PicReader
""" """
def __init__( def __init__(self,
self,
root_path: str, root_path: str,
list_file: str, list_file: str,
meta: bool = False, meta: bool = False,
...@@ -222,8 +211,7 @@ class PicReader(paddle.io.Dataset): ...@@ -222,8 +211,7 @@ class PicReader(paddle.io.Dataset):
img = cv2.resize(img, (scale_size, scale_size)) img = cv2.resize(img, (scale_size, scale_size))
self.cache_img_buff[image_path] = img self.cache_img_buff[image_path] = img
except BaseException: except BaseException:
print("img_path:{} can not by cv2".format( print("img_path:{} can not by cv2".format(image_path).format(image_path))
image_path).format(image_path))
pass pass
...@@ -265,9 +253,7 @@ class PicReader(paddle.io.Dataset): ...@@ -265,9 +253,7 @@ class PicReader(paddle.io.Dataset):
with open(self.list_file) as f: with open(self.list_file) as f:
lines = f.read().splitlines() lines = f.read().splitlines()
print( print("PicReader:: found {} picture in `{}'".format(len(lines), self.list_file))
"PicReader:: found {} picture in `{}'".format(
len(lines), self.list_file))
for i, line in enumerate(lines): for i, line in enumerate(lines):
record = re.split(delimiter, line) record = re.split(delimiter, line)
# record = line.split() # record = line.split()
...@@ -401,20 +387,12 @@ def _read_classes(csv_file: str) -> dict: ...@@ -401,20 +387,12 @@ def _read_classes(csv_file: str) -> dict:
class_name = row.strip() class_name = row.strip()
# print(class_id, class_name) # print(class_id, class_name)
except ValueError: except ValueError:
six.raise_from( six.raise_from(ValueError('line {}: format should be \'class_name\''.format(line)), None)
ValueError(
'line {}: format should be \'class_name\''.format(line)),
None)
class_id = _parse( class_id = _parse(line, int, 'line {}: malformed class ID: {{}}'.format(line))
line,
int,
'line {}: malformed class ID: {{}}'.format(line))
if class_name in result: if class_name in result:
raise ValueError( raise ValueError('line {}: duplicate class name: \'{}\''.format(line, class_name))
'line {}: duplicate class name: \'{}\''.format(
line, class_name))
result[class_name] = class_id result[class_name] = class_id
return result return result
...@@ -439,10 +417,7 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple: ...@@ -439,10 +417,7 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple:
epochs = hparams.task_config.classifier.epochs epochs = hparams.task_config.classifier.epochs
no_cache_img = hparams.task_config.classifier.get("no_cache_img", False) no_cache_img = hparams.task_config.classifier.get("no_cache_img", False)
normalize = { normalize = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
'mean': [
0.485, 0.456, 0.406], 'std': [
0.229, 0.224, 0.225]}
if TrainTransform is None: if TrainTransform is None:
TrainTransform = PbaAugment( TrainTransform = PbaAugment(
...@@ -453,10 +428,7 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple: ...@@ -453,10 +428,7 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple:
hp_policy_epochs=epochs, hp_policy_epochs=epochs,
) )
delimiter = hparams.data_config.delimiter delimiter = hparams.data_config.delimiter
kwargs = dict( kwargs = dict(conf=hparams, delimiter=delimiter)
conf=hparams,
delimiter=delimiter
)
if hparams.task_config.classifier.use_class_map: if hparams.task_config.classifier.use_class_map:
class_to_id_dict = _read_classes(label_list=hparams.data_config.label_list) class_to_id_dict = _read_classes(label_list=hparams.data_config.label_list)
...@@ -475,13 +447,11 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple: ...@@ -475,13 +447,11 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple:
list_file=val_list, list_file=val_list,
transform=transforms.Compose( transform=transforms.Compose(
transforms=[ transforms=[
transforms.Resize( transforms.Resize((224, 224)),
(224,
224)),
transforms.Permute(), transforms.Permute(),
transforms.Normalize( transforms.Normalize(**normalize, channel_first=True)
**normalize, channel_first=True)], ],
channel_first = False), channel_first=False),
class_to_id_dict=class_to_id_dict, class_to_id_dict=class_to_id_dict,
cache_img=not no_cache_img, cache_img=not no_cache_img,
**kwargs) **kwargs)
......
...@@ -18,6 +18,7 @@ from paddle.distributed import ParallelEnv ...@@ -18,6 +18,7 @@ from paddle.distributed import ParallelEnv
from paddlehub.utils.log import logger from paddlehub.utils.log import logger
from paddlehub.utils.utils import Timer from paddlehub.utils.utils import Timer
class CustomTrainer(Trainer): class CustomTrainer(Trainer):
def __init__(self, **kwargs) -> None: def __init__(self, **kwargs) -> None:
super(CustomTrainer, self).__init__(**kwargs) super(CustomTrainer, self).__init__(**kwargs)
...@@ -33,10 +34,7 @@ class CustomTrainer(Trainer): ...@@ -33,10 +34,7 @@ class CustomTrainer(Trainer):
self.batch_sampler, self.train_loader = self.init_train(train_dataset, batch_size, num_workers) self.batch_sampler, self.train_loader = self.init_train(train_dataset, batch_size, num_workers)
self.eval_loader = self.init_evaluate(eval_dataset, batch_size, num_workers) self.eval_loader = self.init_evaluate(eval_dataset, batch_size, num_workers)
def init_train(self, def init_train(self, train_dataset: paddle.io.Dataset, batch_size: int = 1, num_workers: int = 0) -> tuple:
train_dataset: paddle.io.Dataset,
batch_size: int = 1,
num_workers: int = 0) -> tuple:
use_gpu = True use_gpu = True
place = paddle.CUDAPlace(ParallelEnv().dev_id) if use_gpu else paddle.CPUPlace() place = paddle.CUDAPlace(ParallelEnv().dev_id) if use_gpu else paddle.CPUPlace()
paddle.disable_static(place) paddle.disable_static(place)
...@@ -47,7 +45,8 @@ class CustomTrainer(Trainer): ...@@ -47,7 +45,8 @@ class CustomTrainer(Trainer):
train_dataset, batch_sampler=batch_sampler, places=place, num_workers=num_workers, return_list=True) train_dataset, batch_sampler=batch_sampler, places=place, num_workers=num_workers, return_list=True)
return batch_sampler, loader return batch_sampler, loader
def train_one_epoch(self, loader: paddle.io.DataLoader, timer: Timer, current_epoch: int, epochs: int, log_interval: int, steps_per_epoch: int) -> None: def train_one_epoch(self, loader: paddle.io.DataLoader, timer: Timer, current_epoch: int, epochs: int,
log_interval: int, steps_per_epoch: int) -> None:
avg_loss = 0 avg_loss = 0
avg_metrics = defaultdict(int) avg_metrics = defaultdict(int)
self.model.train() self.model.train()
...@@ -70,15 +69,13 @@ class CustomTrainer(Trainer): ...@@ -70,15 +69,13 @@ class CustomTrainer(Trainer):
if self.use_vdl: if self.use_vdl:
self.log_writer.add_scalar(tag='TRAIN/loss', step=timer.current_step, value=avg_loss) self.log_writer.add_scalar(tag='TRAIN/loss', step=timer.current_step, value=avg_loss)
print_msg = 'Epoch={}/{}, Step={}/{}'.format(current_epoch, epochs, batch_idx + 1, print_msg = 'Epoch={}/{}, Step={}/{}'.format(current_epoch, epochs, batch_idx + 1, steps_per_epoch)
steps_per_epoch)
print_msg += ' loss={:.4f}'.format(avg_loss) print_msg += ' loss={:.4f}'.format(avg_loss)
for metric, value in avg_metrics.items(): for metric, value in avg_metrics.items():
value /= log_interval value /= log_interval
if self.use_vdl: if self.use_vdl:
self.log_writer.add_scalar( self.log_writer.add_scalar(tag='TRAIN/{}'.format(metric), step=timer.current_step, value=value)
tag='TRAIN/{}'.format(metric), step=timer.current_step, value=value)
print_msg += ' {}={:.4f}'.format(metric, value) print_msg += ' {}={:.4f}'.format(metric, value)
print_msg += ' lr={:.6f} step/sec={:.2f} | ETA {}'.format(lr, timer.timing, timer.eta) print_msg += ' lr={:.6f} step/sec={:.2f} | ETA {}'.format(lr, timer.timing, timer.eta)
...@@ -139,9 +136,7 @@ class CustomTrainer(Trainer): ...@@ -139,9 +136,7 @@ class CustomTrainer(Trainer):
self.save_model(best_model_path) self.save_model(best_model_path)
self._save_metrics() self._save_metrics()
metric_msg = [ metric_msg = ['{}={:.4f}'.format(metric, value) for metric, value in self.best_metrics.items()]
'{}={:.4f}'.format(metric, value) for metric, value in self.best_metrics.items()
]
metric_msg = ' '.join(metric_msg) metric_msg = ' '.join(metric_msg)
logger.eval('Saving best model to {} [best {}]'.format(best_model_path, metric_msg)) logger.eval('Saving best model to {} [best {}]'.format(best_model_path, metric_msg))
......
from auto_augment.autoaug.experiment.experiment import AutoAugExperiment from auto_augment.autoaug.experiment.experiment import AutoAugExperiment
from auto_augment.autoaug.utils.yaml_config import get_config from auto_augment.autoaug.utils.yaml_config import get_config
from hub_fitter import HubFitterClassifer from hub_fitter import HubFitterClassifer
...@@ -9,14 +8,21 @@ logging.basicConfig(level=logging.INFO) ...@@ -9,14 +8,21 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--config",help="config file",) parser.add_argument(
parser.add_argument("--workspace",default=None, help="work_space",) "--config",
help="config file",
)
parser.add_argument(
"--workspace",
default=None,
help="work_space",
)
def main(): def main():
search_test() search_test()
def search_test(): def search_test():
args = parser.parse_args() args = parser.parse_args()
config = args.config config = args.config
...@@ -38,14 +44,13 @@ def search_test(): ...@@ -38,14 +44,13 @@ def search_test():
resource_config=resource_config, resource_config=resource_config,
data_config=data_config, data_config=data_config,
search_space=search_space, search_space=search_space,
fitter=HubFitterClassifer fitter=HubFitterClassifer)
)
result = exper.search() # 开始搜索任务 result = exper.search() # 开始搜索任务
policy = result.get_best_policy() # 最佳策略获取, policy格式见 搜索结果应用格式 policy = result.get_best_policy() # 最佳策略获取, policy格式见 搜索结果应用格式
print("policy is:{}".format(policy)) print("policy is:{}".format(policy))
dump_path = os.path.join(workspace, "auto_aug_config.json") dump_path = os.path.join(workspace, "auto_aug_config.json")
result.dump_best_policy( result.dump_best_policy(path=dump_path)
path=dump_path)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -24,9 +24,20 @@ logging.basicConfig(level=logging.INFO) ...@@ -24,9 +24,20 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--config",help="config file",) parser.add_argument(
parser.add_argument("--workspace",default=None, help="work_space",) "--config",
parser.add_argument("--policy",default=None, help="data aug policy",) help="config file",
)
parser.add_argument(
"--workspace",
default=None,
help="work_space",
)
parser.add_argument(
"--policy",
default=None,
help="data aug policy",
)
if __name__ == '__main__': if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
...@@ -39,10 +50,7 @@ if __name__ == '__main__': ...@@ -39,10 +50,7 @@ if __name__ == '__main__':
input_size = task_config.classifier.input_size input_size = task_config.classifier.input_size
scale_size = task_config.classifier.scale_size scale_size = task_config.classifier.scale_size
normalize = { normalize = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
'mean': [
0.485, 0.456, 0.406], 'std': [
0.229, 0.224, 0.225]}
epochs = task_config.classifier.epochs epochs = task_config.classifier.epochs
policy = args.policy policy = args.policy
...@@ -50,13 +58,11 @@ if __name__ == '__main__': ...@@ -50,13 +58,11 @@ if __name__ == '__main__':
print("use normal train transform") print("use normal train transform")
TrainTransform = transforms.Compose( TrainTransform = transforms.Compose(
transforms=[ transforms=[
transforms.Resize( transforms.Resize((input_size, input_size)),
(input_size,
input_size)),
transforms.Permute(), transforms.Permute(),
transforms.Normalize( transforms.Normalize(**normalize, channel_first=True)
**normalize, channel_first=True)], ],
channel_first = False) channel_first=False)
else: else:
TrainTransform = PbaAugment( TrainTransform = PbaAugment(
input_size=input_size, input_size=input_size,
...@@ -64,17 +70,12 @@ if __name__ == '__main__': ...@@ -64,17 +70,12 @@ if __name__ == '__main__':
normalize=normalize, normalize=normalize,
policy=policy, policy=policy,
hp_policy_epochs=epochs, hp_policy_epochs=epochs,
stage="train" stage="train")
)
train_dataset, eval_dataset = _init_loader(config, TrainTransform=TrainTransform) train_dataset, eval_dataset = _init_loader(config, TrainTransform=TrainTransform)
class_to_id_dict = _read_classes(config.data_config.label_list) class_to_id_dict = _read_classes(config.data_config.label_list)
model = hub.Module(name=config.task_config.classifier.model_name, label_list=class_to_id_dict.keys(), load_checkpoint=None) model = hub.Module(
name=config.task_config.classifier.model_name, label_list=class_to_id_dict.keys(), load_checkpoint=None)
optimizer = paddle.optimizer.Adam( optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
learning_rate=0.001, parameters=model.parameters()) trainer = CustomTrainer(model=model, optimizer=optimizer, checkpoint_dir='img_classification_ckpt')
trainer = CustomTrainer(
model=model,
optimizer=optimizer,
checkpoint_dir='img_classification_ckpt')
trainer.train(train_dataset, epochs=epochs, batch_size=32, eval_dataset=eval_dataset, save_interval=10) trainer.train(train_dataset, epochs=epochs, batch_size=32, eval_dataset=eval_dataset, save_interval=10)
...@@ -6,7 +6,8 @@ from paddlehub.datasets import Canvas ...@@ -6,7 +6,8 @@ from paddlehub.datasets import Canvas
if __name__ == '__main__': if __name__ == '__main__':
transform = T.Compose([T.Resize((256, 256), interpolation='NEAREST'), transform = T.Compose(
[T.Resize((256, 256), interpolation='NEAREST'),
T.RandomPaddingCrop(crop_size=176), T.RandomPaddingCrop(crop_size=176),
T.RGB2LAB()], to_rgb=True) T.RGB2LAB()], to_rgb=True)
......
...@@ -159,5 +159,3 @@ https://github.com/PaddlePaddle/PaddleSeg ...@@ -159,5 +159,3 @@ https://github.com/PaddlePaddle/PaddleSeg
paddlepaddle >= 2.0.0 paddlepaddle >= 2.0.0
paddlehub >= 2.0.0 paddlehub >= 2.0.0
...@@ -24,9 +24,7 @@ if __name__ == '__main__': ...@@ -24,9 +24,7 @@ if __name__ == '__main__':
'华裔作家韩素音女士曾三次到大足,称“大足石窟是一座未被开发的金矿”。', '华裔作家韩素音女士曾三次到大足,称“大足石窟是一座未被开发的金矿”。',
] ]
data = [[split_char.join(text)] for text in text_a] data = [[split_char.join(text)] for text in text_a]
label_map = { label_map = {idx: label for idx, label in enumerate(label_list)}
idx: label for idx, label in enumerate(label_list)
}
model = hub.Module( model = hub.Module(
name='ernie_tiny', name='ernie_tiny',
...@@ -39,4 +37,3 @@ if __name__ == '__main__': ...@@ -39,4 +37,3 @@ if __name__ == '__main__':
results = model.predict(data=data, max_seq_len=128, batch_size=1, use_gpu=True) results = model.predict(data=data, max_seq_len=128, batch_size=1, use_gpu=True)
for idx, text in enumerate(text_a): for idx, text in enumerate(text_a):
print(f'Text:\n{text} \nLable: \n{", ".join(results[idx][1:len(text)+1])} \n') print(f'Text:\n{text} \nLable: \n{", ".join(results[idx][1:len(text)+1])} \n')
...@@ -21,7 +21,11 @@ import argparse ...@@ -21,7 +21,11 @@ import argparse
parser = argparse.ArgumentParser(__doc__) parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--num_epoch", type=int, default=3, help="Number of epoches for fine-tuning.") parser.add_argument("--num_epoch", type=int, default=3, help="Number of epoches for fine-tuning.")
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="Whether use GPU for fine-tuning, input should be True or False") parser.add_argument(
"--use_gpu",
type=ast.literal_eval,
default=True,
help="Whether use GPU for fine-tuning, input should be True or False")
parser.add_argument("--learning_rate", type=float, default=5e-5, help="Learning rate used to train with warmup.") parser.add_argument("--learning_rate", type=float, default=5e-5, help="Learning rate used to train with warmup.")
parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.") parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.")
parser.add_argument("--batch_size", type=int, default=32, help="Total examples' number in batch for training.") parser.add_argument("--batch_size", type=int, default=32, help="Total examples' number in batch for training.")
...@@ -30,12 +34,9 @@ parser.add_argument("--save_interval", type=int, default=1, help="Save checkpoin ...@@ -30,12 +34,9 @@ parser.add_argument("--save_interval", type=int, default=1, help="Save checkpoin
args = parser.parse_args() args = parser.parse_args()
if __name__ == '__main__': if __name__ == '__main__':
label_list = MSRA_NER.label_list label_list = MSRA_NER.label_list
label_map = { label_map = {idx: label for idx, label in enumerate(label_list)}
idx: label for idx, label in enumerate(label_list)
}
model = hub.Module( model = hub.Module(
name='ernie_tiny', name='ernie_tiny',
...@@ -45,21 +46,9 @@ if __name__ == '__main__': ...@@ -45,21 +46,9 @@ if __name__ == '__main__':
) )
tokenizer = model.get_tokenizer() tokenizer = model.get_tokenizer()
train_dataset = MSRA_NER( train_dataset = MSRA_NER(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='train')
tokenizer=tokenizer, dev_dataset = MSRA_NER(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='dev')
max_seq_len=args.max_seq_len, test_dataset = MSRA_NER(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='test')
mode='train'
)
dev_dataset = MSRA_NER(
tokenizer=tokenizer,
max_seq_len=args.max_seq_len,
mode='dev'
)
test_dataset = MSRA_NER(
tokenizer=tokenizer,
max_seq_len=args.max_seq_len,
mode='test'
)
optimizer = paddle.optimizer.AdamW(learning_rate=args.learning_rate, parameters=model.parameters()) optimizer = paddle.optimizer.AdamW(learning_rate=args.learning_rate, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu) trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu)
......
...@@ -3,4 +3,4 @@ import paddlehub as hub ...@@ -3,4 +3,4 @@ import paddlehub as hub
if __name__ == '__main__': if __name__ == '__main__':
model = hub.Module(name='msgnet', load_checkpoint="/PATH/TO/CHECKPOINT") model = hub.Module(name='msgnet', load_checkpoint="/PATH/TO/CHECKPOINT")
result = model.predict(origin=["venice-boat.jpg"], style="candy.jpg", visualization=True, save_path ='style_tranfer') result = model.predict(origin=["venice-boat.jpg"], style="candy.jpg", visualization=True, save_path='style_tranfer')
\ No newline at end of file
...@@ -166,4 +166,3 @@ for idx, text in enumerate(data): ...@@ -166,4 +166,3 @@ for idx, text in enumerate(data):
``` ```
参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load) 参数配置正确后,请执行脚本`python predict.py`, 加载模型具体可参见[加载](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load)
...@@ -19,17 +19,19 @@ from model import BoWModel ...@@ -19,17 +19,19 @@ from model import BoWModel
import ast import ast
import argparse import argparse
parser = argparse.ArgumentParser(__doc__) parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--hub_embedding_name", type=str, default='w2v_baidu_encyclopedia_target_word-word_dim300', help="") parser.add_argument("--hub_embedding_name", type=str, default='w2v_baidu_encyclopedia_target_word-word_dim300', help="")
parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.") parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.")
parser.add_argument("--batch_size", type=int, default=64, help="Total examples' number in batch for training.") parser.add_argument("--batch_size", type=int, default=64, help="Total examples' number in batch for training.")
parser.add_argument("--checkpoint", type=str, default='./checkpoint/best_model/model.pdparams', help="Model checkpoint") parser.add_argument("--checkpoint", type=str, default='./checkpoint/best_model/model.pdparams', help="Model checkpoint")
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="Whether use GPU for fine-tuning, input should be True or False") parser.add_argument(
"--use_gpu",
type=ast.literal_eval,
default=True,
help="Whether use GPU for fine-tuning, input should be True or False")
args = parser.parse_args() args = parser.parse_args()
if __name__ == '__main__': if __name__ == '__main__':
# Data to be prdicted # Data to be prdicted
data = [ data = [
...@@ -44,12 +46,9 @@ if __name__ == '__main__': ...@@ -44,12 +46,9 @@ if __name__ == '__main__':
embedder = hub.Module(name=args.hub_embedding_name) embedder = hub.Module(name=args.hub_embedding_name)
tokenizer = embedder.get_tokenizer() tokenizer = embedder.get_tokenizer()
model = BoWModel( model = BoWModel(embedder=embedder, tokenizer=tokenizer, load_checkpoint=args.checkpoint, label_map=label_map)
embedder=embedder,
tokenizer=tokenizer,
load_checkpoint=args.checkpoint,
label_map=label_map)
results = model.predict(data, max_seq_len=args.max_seq_len, batch_size=args.batch_size, use_gpu=args.use_gpu, return_result=False) results = model.predict(
data, max_seq_len=args.max_seq_len, batch_size=args.batch_size, use_gpu=args.use_gpu, return_result=False)
for idx, text in enumerate(data): for idx, text in enumerate(data):
print('Data: {} \t Lable: {}'.format(text[0], results[idx])) print('Data: {} \t Lable: {}'.format(text[0], results[idx]))
...@@ -21,7 +21,6 @@ from model import BoWModel ...@@ -21,7 +21,6 @@ from model import BoWModel
import ast import ast
import argparse import argparse
parser = argparse.ArgumentParser(__doc__) parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--hub_embedding_name", type=str, default='w2v_baidu_encyclopedia_target_word-word_dim300', help="") parser.add_argument("--hub_embedding_name", type=str, default='w2v_baidu_encyclopedia_target_word-word_dim300', help="")
parser.add_argument("--num_epoch", type=int, default=10, help="Number of epoches for fine-tuning.") parser.add_argument("--num_epoch", type=int, default=10, help="Number of epoches for fine-tuning.")
...@@ -30,11 +29,14 @@ parser.add_argument("--max_seq_len", type=int, default=128, help="Number of word ...@@ -30,11 +29,14 @@ parser.add_argument("--max_seq_len", type=int, default=128, help="Number of word
parser.add_argument("--batch_size", type=int, default=64, help="Total examples' number in batch for training.") parser.add_argument("--batch_size", type=int, default=64, help="Total examples' number in batch for training.")
parser.add_argument("--checkpoint_dir", type=str, default='./checkpoint', help="Directory to model checkpoint") parser.add_argument("--checkpoint_dir", type=str, default='./checkpoint', help="Directory to model checkpoint")
parser.add_argument("--save_interval", type=int, default=5, help="Save checkpoint every n epoch.") parser.add_argument("--save_interval", type=int, default=5, help="Save checkpoint every n epoch.")
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="Whether use GPU for fine-tuning, input should be True or False") parser.add_argument(
"--use_gpu",
type=ast.literal_eval,
default=True,
help="Whether use GPU for fine-tuning, input should be True or False")
args = parser.parse_args() args = parser.parse_args()
if __name__ == '__main__': if __name__ == '__main__':
embedder = hub.Module(name=args.hub_embedding_name) embedder = hub.Module(name=args.hub_embedding_name)
tokenizer = embedder.get_tokenizer() tokenizer = embedder.get_tokenizer()
...@@ -44,8 +46,7 @@ if __name__ == '__main__': ...@@ -44,8 +46,7 @@ if __name__ == '__main__':
test_dataset = ChnSentiCorp(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='test') test_dataset = ChnSentiCorp(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='test')
model = BoWModel(embedder=embedder) model = BoWModel(embedder=embedder)
optimizer = paddle.optimizer.AdamW( optimizer = paddle.optimizer.AdamW(learning_rate=args.learning_rate, parameters=model.parameters())
learning_rate=args.learning_rate, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu) trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu)
trainer.train( trainer.train(
train_dataset, train_dataset,
......
...@@ -21,7 +21,11 @@ import argparse ...@@ -21,7 +21,11 @@ import argparse
parser = argparse.ArgumentParser(__doc__) parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--num_epoch", type=int, default=3, help="Number of epoches for fine-tuning.") parser.add_argument("--num_epoch", type=int, default=3, help="Number of epoches for fine-tuning.")
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="Whether use GPU for fine-tuning, input should be True or False") parser.add_argument(
"--use_gpu",
type=ast.literal_eval,
default=True,
help="Whether use GPU for fine-tuning, input should be True or False")
parser.add_argument("--learning_rate", type=float, default=5e-5, help="Learning rate used to train with warmup.") parser.add_argument("--learning_rate", type=float, default=5e-5, help="Learning rate used to train with warmup.")
parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.") parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.")
parser.add_argument("--batch_size", type=int, default=32, help="Total examples' number in batch for training.") parser.add_argument("--batch_size", type=int, default=32, help="Total examples' number in batch for training.")
...@@ -33,15 +37,11 @@ args = parser.parse_args() ...@@ -33,15 +37,11 @@ args = parser.parse_args()
if __name__ == '__main__': if __name__ == '__main__':
model = hub.Module(name='ernie_tiny', version='2.0.1', task='seq-cls') model = hub.Module(name='ernie_tiny', version='2.0.1', task='seq-cls')
train_dataset = ChnSentiCorp( train_dataset = ChnSentiCorp(tokenizer=model.get_tokenizer(), max_seq_len=args.max_seq_len, mode='train')
tokenizer=model.get_tokenizer(), max_seq_len=args.max_seq_len, mode='train') dev_dataset = ChnSentiCorp(tokenizer=model.get_tokenizer(), max_seq_len=args.max_seq_len, mode='dev')
dev_dataset = ChnSentiCorp( test_dataset = ChnSentiCorp(tokenizer=model.get_tokenizer(), max_seq_len=args.max_seq_len, mode='test')
tokenizer=model.get_tokenizer(), max_seq_len=args.max_seq_len, mode='dev')
test_dataset = ChnSentiCorp(
tokenizer=model.get_tokenizer(), max_seq_len=args.max_seq_len, mode='test')
optimizer = paddle.optimizer.AdamW( optimizer = paddle.optimizer.AdamW(learning_rate=args.learning_rate, parameters=model.parameters())
learning_rate=args.learning_rate, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu) trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu)
trainer.train( trainer.train(
train_dataset, train_dataset,
......
...@@ -21,7 +21,11 @@ import argparse ...@@ -21,7 +21,11 @@ import argparse
parser = argparse.ArgumentParser(__doc__) parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--num_epoch", type=int, default=10, help="Number of epoches for fine-tuning.") parser.add_argument("--num_epoch", type=int, default=10, help="Number of epoches for fine-tuning.")
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="Whether use GPU for fine-tuning, input should be True or False") parser.add_argument(
"--use_gpu",
type=ast.literal_eval,
default=True,
help="Whether use GPU for fine-tuning, input should be True or False")
parser.add_argument("--learning_rate", type=float, default=5e-5, help="Learning rate used to train with warmup.") parser.add_argument("--learning_rate", type=float, default=5e-5, help="Learning rate used to train with warmup.")
parser.add_argument("--max_seq_len", type=int, default=64, help="Number of words of the longest seqence.") parser.add_argument("--max_seq_len", type=int, default=64, help="Number of words of the longest seqence.")
parser.add_argument("--batch_size", type=int, default=128, help="Total examples' number in batch for training.") parser.add_argument("--batch_size", type=int, default=128, help="Total examples' number in batch for training.")
...@@ -38,8 +42,7 @@ if __name__ == '__main__': ...@@ -38,8 +42,7 @@ if __name__ == '__main__':
dev_dataset = LCQMC(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='dev') dev_dataset = LCQMC(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='dev')
test_dataset = LCQMC(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='test') test_dataset = LCQMC(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='test')
optimizer = paddle.optimizer.AdamW( optimizer = paddle.optimizer.AdamW(learning_rate=args.learning_rate, parameters=model.parameters())
learning_rate=args.learning_rate, parameters=model.parameters())
trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu) trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu)
trainer.train( trainer.train(
train_dataset, train_dataset,
......
...@@ -93,6 +93,3 @@ $ hub serving start -m chinese_ocr_db_crnn_mobile ...@@ -93,6 +93,3 @@ $ hub serving start -m chinese_ocr_db_crnn_mobile
</p> </p>
* 如需在线快速体验,请点击[PaddleHub教程合集](https://aistudio.baidu.com/aistudio/projectdetail/231146),可使用AI Studio平台提供的GPU算力进行快速尝试。 * 如需在线快速体验,请点击[PaddleHub教程合集](https://aistudio.baidu.com/aistudio/projectdetail/231146),可使用AI Studio平台提供的GPU算力进行快速尝试。
...@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo ...@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn10", @moduleinfo(
name="panns_cnn10",
version="1.0.0", version="1.0.0",
summary="", summary="",
author="Baidu", author="Baidu",
...@@ -52,15 +53,15 @@ class PANN(nn.Layer): ...@@ -52,15 +53,15 @@ class PANN(nn.Layer):
self.num_class = num_class self.num_class = num_class
if task == 'sound-cls': if task == 'sound-cls':
self.cnn10 = CNN10(extract_embedding=True, self.cnn10 = CNN10(
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams')) extract_embedding=True, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams'))
self.dropout = nn.Dropout(0.1) self.dropout = nn.Dropout(0.1)
self.fc = nn.Linear(self.cnn10.emb_size, num_class) self.fc = nn.Linear(self.cnn10.emb_size, num_class)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
else: else:
self.cnn10 = CNN10(extract_embedding=False, self.cnn10 = CNN10(
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams')) extract_embedding=False, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams'))
self.task = task self.task = task
if load_checkpoint is not None and os.path.isfile(load_checkpoint): if load_checkpoint is not None and os.path.isfile(load_checkpoint):
......
...@@ -25,13 +25,15 @@ class ConvBlock(nn.Layer): ...@@ -25,13 +25,15 @@ class ConvBlock(nn.Layer):
def __init__(self, in_channels, out_channels): def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__() super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2D(in_channels=in_channels, self.conv1 = nn.Conv2D(
in_channels=in_channels,
out_channels=out_channels, out_channels=out_channels,
kernel_size=(3, 3), kernel_size=(3, 3),
stride=(1, 1), stride=(1, 1),
padding=(1, 1), padding=(1, 1),
bias_attr=False) bias_attr=False)
self.conv2 = nn.Conv2D(in_channels=out_channels, self.conv2 = nn.Conv2D(
in_channels=out_channels,
out_channels=out_channels, out_channels=out_channels,
kernel_size=(3, 3), kernel_size=(3, 3),
stride=(1, 1), stride=(1, 1),
......
...@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo ...@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn14", @moduleinfo(
name="panns_cnn14",
version="1.0.0", version="1.0.0",
summary="", summary="",
author="Baidu", author="Baidu",
...@@ -52,15 +53,15 @@ class PANN(nn.Layer): ...@@ -52,15 +53,15 @@ class PANN(nn.Layer):
self.num_class = num_class self.num_class = num_class
if task == 'sound-cls': if task == 'sound-cls':
self.cnn14 = CNN14(extract_embedding=True, self.cnn14 = CNN14(
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams')) extract_embedding=True, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams'))
self.dropout = nn.Dropout(0.1) self.dropout = nn.Dropout(0.1)
self.fc = nn.Linear(self.cnn14.emb_size, num_class) self.fc = nn.Linear(self.cnn14.emb_size, num_class)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
else: else:
self.cnn14 = CNN14(extract_embedding=False, self.cnn14 = CNN14(
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams')) extract_embedding=False, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams'))
self.task = task self.task = task
if load_checkpoint is not None and os.path.isfile(load_checkpoint): if load_checkpoint is not None and os.path.isfile(load_checkpoint):
......
...@@ -25,13 +25,15 @@ class ConvBlock(nn.Layer): ...@@ -25,13 +25,15 @@ class ConvBlock(nn.Layer):
def __init__(self, in_channels, out_channels): def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__() super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2D(in_channels=in_channels, self.conv1 = nn.Conv2D(
in_channels=in_channels,
out_channels=out_channels, out_channels=out_channels,
kernel_size=(3, 3), kernel_size=(3, 3),
stride=(1, 1), stride=(1, 1),
padding=(1, 1), padding=(1, 1),
bias_attr=False) bias_attr=False)
self.conv2 = nn.Conv2D(in_channels=out_channels, self.conv2 = nn.Conv2D(
in_channels=out_channels,
out_channels=out_channels, out_channels=out_channels,
kernel_size=(3, 3), kernel_size=(3, 3),
stride=(1, 1), stride=(1, 1),
......
...@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo ...@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn6", @moduleinfo(
name="panns_cnn6",
version="1.0.0", version="1.0.0",
summary="", summary="",
author="Baidu", author="Baidu",
...@@ -52,15 +53,15 @@ class PANN(nn.Layer): ...@@ -52,15 +53,15 @@ class PANN(nn.Layer):
self.num_class = num_class self.num_class = num_class
if task == 'sound-cls': if task == 'sound-cls':
self.cnn6 = CNN6(extract_embedding=True, self.cnn6 = CNN6(
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams')) extract_embedding=True, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams'))
self.dropout = nn.Dropout(0.1) self.dropout = nn.Dropout(0.1)
self.fc = nn.Linear(self.cnn6.emb_size, num_class) self.fc = nn.Linear(self.cnn6.emb_size, num_class)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
else: else:
self.cnn6 = CNN6(extract_embedding=False, self.cnn6 = CNN6(
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams')) extract_embedding=False, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams'))
self.task = task self.task = task
if load_checkpoint is not None and os.path.isfile(load_checkpoint): if load_checkpoint is not None and os.path.isfile(load_checkpoint):
......
...@@ -25,7 +25,8 @@ class ConvBlock5x5(nn.Layer): ...@@ -25,7 +25,8 @@ class ConvBlock5x5(nn.Layer):
def __init__(self, in_channels, out_channels): def __init__(self, in_channels, out_channels):
super(ConvBlock5x5, self).__init__() super(ConvBlock5x5, self).__init__()
self.conv1 = nn.Conv2D(in_channels=in_channels, self.conv1 = nn.Conv2D(
in_channels=in_channels,
out_channels=out_channels, out_channels=out_channels,
kernel_size=(5, 5), kernel_size=(5, 5),
stride=(1, 1), stride=(1, 1),
......
...@@ -18,6 +18,7 @@ class ColorizeHint: ...@@ -18,6 +18,7 @@ class ColorizeHint:
hint(np.ndarray): hint images hint(np.ndarray): hint images
mask(np.ndarray): mask images mask(np.ndarray): mask images
""" """
def __init__(self, percent: float, num_points: int = None, samp: str = 'normal', use_avg: bool = True): def __init__(self, percent: float, num_points: int = None, samp: str = 'normal', use_avg: bool = True):
self.percent = percent self.percent = percent
self.num_points = num_points self.num_points = num_points
...@@ -52,11 +53,9 @@ class ColorizeHint: ...@@ -52,11 +53,9 @@ class ColorizeHint:
# add color point # add color point
if self.use_avg: if self.use_avg:
# embed() # embed()
hint[nn, :, h:h + P, w:w + P] = np.mean(np.mean(data[nn, :, h:h + P, w:w + P], hint[nn, :, h:h + P, w:w + P] = np.mean(
axis=2, np.mean(data[nn, :, h:h + P, w:w + P], axis=2, keepdims=True), axis=1, keepdims=True).reshape(
keepdims=True), 1, C, 1, 1)
axis=1,
keepdims=True).reshape(1, C, 1, 1)
else: else:
hint[nn, :, h:h + P, w:w + P] = data[nn, :, h:h + P, w:w + P] hint[nn, :, h:h + P, w:w + P] = data[nn, :, h:h + P, w:w + P]
mask[nn, :, h:h + P, w:w + P] = 1 mask[nn, :, h:h + P, w:w + P] = 1
...@@ -82,6 +81,7 @@ class ColorizePreprocess: ...@@ -82,6 +81,7 @@ class ColorizePreprocess:
data(dict):The preprocessed data for colorization. data(dict):The preprocessed data for colorization.
""" """
def __init__(self, def __init__(self,
ab_thresh: float = 0., ab_thresh: float = 0.,
p: float = 0., p: float = 0.,
...@@ -113,8 +113,8 @@ class ColorizePreprocess: ...@@ -113,8 +113,8 @@ class ColorizePreprocess:
data['B'] = data_lab[:, 1:, :, :] data['B'] = data_lab[:, 1:, :, :]
if self.ab_thresh > 0: # mask out grayscale images if self.ab_thresh > 0: # mask out grayscale images
thresh = 1. * self.ab_thresh / 110 thresh = 1. * self.ab_thresh / 110
mask = np.sum(np.abs(np.max(np.max(data['B'], axis=3), axis=2) - np.min(np.min(data['B'], axis=3), axis=2)), mask = np.sum(
axis=1) np.abs(np.max(np.max(data['B'], axis=3), axis=2) - np.min(np.min(data['B'], axis=3), axis=2)), axis=1)
mask = (mask >= thresh) mask = (mask >= thresh)
data['A'] = data['A'][mask, :, :, :] data['A'] = data['A'][mask, :, :, :]
data['B'] = data['B'][mask, :, :, :] data['B'] = data['B'][mask, :, :, :]
......
...@@ -40,6 +40,7 @@ class UserGuidedColorization(nn.Layer): ...@@ -40,6 +40,7 @@ class UserGuidedColorization(nn.Layer):
load_checkpoint (str): Pretrained checkpoint path. load_checkpoint (str): Pretrained checkpoint path.
""" """
def __init__(self, use_tanh: bool = True, load_checkpoint: str = None): def __init__(self, use_tanh: bool = True, load_checkpoint: str = None):
super(UserGuidedColorization, self).__init__() super(UserGuidedColorization, self).__init__()
self.input_nc = 4 self.input_nc = 4
......
...@@ -14,6 +14,7 @@ from paddlehub.module.cv_module import StyleTransferModule ...@@ -14,6 +14,7 @@ from paddlehub.module.cv_module import StyleTransferModule
class GramMatrix(nn.Layer): class GramMatrix(nn.Layer):
"""Calculate gram matrix""" """Calculate gram matrix"""
def forward(self, y): def forward(self, y):
(b, ch, h, w) = y.shape (b, ch, h, w) = y.shape
features = y.reshape((b, ch, w * h)) features = y.reshape((b, ch, w * h))
...@@ -24,6 +25,7 @@ class GramMatrix(nn.Layer): ...@@ -24,6 +25,7 @@ class GramMatrix(nn.Layer):
class ConvLayer(nn.Layer): class ConvLayer(nn.Layer):
"""Basic conv layer with reflection padding layer""" """Basic conv layer with reflection padding layer"""
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int): def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int):
super(ConvLayer, self).__init__() super(ConvLayer, self).__init__()
pad = int(np.floor(kernel_size / 2)) pad = int(np.floor(kernel_size / 2))
...@@ -51,6 +53,7 @@ class UpsampleConvLayer(nn.Layer): ...@@ -51,6 +53,7 @@ class UpsampleConvLayer(nn.Layer):
Return: Return:
img(paddle.Tensor): UpsampleConvLayer output. img(paddle.Tensor): UpsampleConvLayer output.
""" """
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int, upsample=None): def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int, upsample=None):
super(UpsampleConvLayer, self).__init__() super(UpsampleConvLayer, self).__init__()
self.upsample = upsample self.upsample = upsample
...@@ -85,6 +88,7 @@ class Bottleneck(nn.Layer): ...@@ -85,6 +88,7 @@ class Bottleneck(nn.Layer):
Return: Return:
img(paddle.Tensor): Bottleneck output. img(paddle.Tensor): Bottleneck output.
""" """
def __init__(self, def __init__(self,
inplanes: int, inplanes: int,
planes: int, planes: int,
...@@ -98,8 +102,8 @@ class Bottleneck(nn.Layer): ...@@ -98,8 +102,8 @@ class Bottleneck(nn.Layer):
self.residual_layer = nn.Conv2D(inplanes, planes * self.expansion, kernel_size=1, stride=stride) self.residual_layer = nn.Conv2D(inplanes, planes * self.expansion, kernel_size=1, stride=stride)
conv_block = (norm_layer(inplanes), nn.ReLU(), nn.Conv2D(inplanes, planes, kernel_size=1, stride=1), conv_block = (norm_layer(inplanes), nn.ReLU(), nn.Conv2D(inplanes, planes, kernel_size=1, stride=1),
norm_layer(planes), nn.ReLU(), ConvLayer(planes, planes, kernel_size=3, stride=stride), norm_layer(planes), nn.ReLU(), ConvLayer(planes, planes, kernel_size=3, stride=stride),
norm_layer(planes), nn.ReLU(), nn.Conv2D(planes, planes * self.expansion, kernel_size=1, norm_layer(planes), nn.ReLU(), nn.Conv2D(
stride=1)) planes, planes * self.expansion, kernel_size=1, stride=1))
self.conv_block = nn.Sequential(*conv_block) self.conv_block = nn.Sequential(*conv_block)
def forward(self, x: paddle.Tensor): def forward(self, x: paddle.Tensor):
...@@ -125,14 +129,12 @@ class UpBottleneck(nn.Layer): ...@@ -125,14 +129,12 @@ class UpBottleneck(nn.Layer):
Return: Return:
img(paddle.Tensor): UpBottleneck output. img(paddle.Tensor): UpBottleneck output.
""" """
def __init__(self, inplanes: int, planes: int, stride: int = 2, norm_layer: nn.Layer = nn.BatchNorm2D): def __init__(self, inplanes: int, planes: int, stride: int = 2, norm_layer: nn.Layer = nn.BatchNorm2D):
super(UpBottleneck, self).__init__() super(UpBottleneck, self).__init__()
self.expansion = 4 self.expansion = 4
self.residual_layer = UpsampleConvLayer(inplanes, self.residual_layer = UpsampleConvLayer(
planes * self.expansion, inplanes, planes * self.expansion, kernel_size=1, stride=1, upsample=stride)
kernel_size=1,
stride=1,
upsample=stride)
conv_block = [] conv_block = []
conv_block += [norm_layer(inplanes), nn.ReLU(), nn.Conv2D(inplanes, planes, kernel_size=1, stride=1)] conv_block += [norm_layer(inplanes), nn.ReLU(), nn.Conv2D(inplanes, planes, kernel_size=1, stride=1)]
conv_block += [ conv_block += [
...@@ -163,6 +165,7 @@ class Inspiration(nn.Layer): ...@@ -163,6 +165,7 @@ class Inspiration(nn.Layer):
Return: Return:
img(paddle.Tensor): UpBottleneck output. img(paddle.Tensor): UpBottleneck output.
""" """
def __init__(self, C: int, B: int = 1): def __init__(self, C: int, B: int = 1):
super(Inspiration, self).__init__() super(Inspiration, self).__init__()
...@@ -179,8 +182,8 @@ class Inspiration(nn.Layer): ...@@ -179,8 +182,8 @@ class Inspiration(nn.Layer):
self.P = paddle.bmm(self.weight.expand_as(self.G), self.G) self.P = paddle.bmm(self.weight.expand_as(self.G), self.G)
x = paddle.bmm( x = paddle.bmm(
self.P.transpose((0, 2, 1)).expand((X.shape[0], self.C, self.C)), X.reshape( self.P.transpose((0, 2, 1)).expand((X.shape[0], self.C, self.C)), X.reshape((X.shape[0], X.shape[1],
(X.shape[0], X.shape[1], -1))).reshape(X.shape) -1))).reshape(X.shape)
return x return x
def __repr__(self): def __repr__(self):
...@@ -190,6 +193,7 @@ class Inspiration(nn.Layer): ...@@ -190,6 +193,7 @@ class Inspiration(nn.Layer):
class Vgg16(nn.Layer): class Vgg16(nn.Layer):
""" First four layers from Vgg16.""" """ First four layers from Vgg16."""
def __init__(self): def __init__(self):
super(Vgg16, self).__init__() super(Vgg16, self).__init__()
self.conv1_1 = nn.Conv2D(3, 64, kernel_size=3, stride=1, padding=1) self.conv1_1 = nn.Conv2D(3, 64, kernel_size=3, stride=1, padding=1)
...@@ -264,12 +268,8 @@ class MSGNet(nn.Layer): ...@@ -264,12 +268,8 @@ class MSGNet(nn.Layer):
Return: Return:
img(paddle.Tensor): MSGNet output. img(paddle.Tensor): MSGNet output.
""" """
def __init__(self,
input_nc=3, def __init__(self, input_nc=3, output_nc=3, ngf=128, n_blocks=6, norm_layer=nn.InstanceNorm2D,
output_nc=3,
ngf=128,
n_blocks=6,
norm_layer=nn.InstanceNorm2D,
load_checkpoint=None): load_checkpoint=None):
super(MSGNet, self).__init__() super(MSGNet, self).__init__()
self.gram = GramMatrix() self.gram = GramMatrix()
......
...@@ -194,7 +194,7 @@ class ResNet50_vd(nn.Layer): ...@@ -194,7 +194,7 @@ class ResNet50_vd(nn.Layer):
label_file = os.path.join(self.directory, 'label_list.txt') label_file = os.path.join(self.directory, 'label_list.txt')
files = open(label_file) files = open(label_file)
for line in files.readlines(): for line in files.readlines():
line=line.strip('\n') line = line.strip('\n')
label_list.append(line) label_list.append(line)
self.labels = label_list self.labels = label_list
class_dim = len(self.labels) class_dim = len(self.labels)
...@@ -248,7 +248,12 @@ class ResNet50_vd(nn.Layer): ...@@ -248,7 +248,12 @@ class ResNet50_vd(nn.Layer):
print("load pretrained checkpoint success") print("load pretrained checkpoint success")
def transforms(self, images: Union[str, np.ndarray]): def transforms(self, images: Union[str, np.ndarray]):
transforms = T.Compose([T.Resize((256, 256)), T.CenterCrop(224), T.Normalize(mean=[0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])], to_rgb=True) transforms = T.Compose([
T.Resize((256, 256)),
T.CenterCrop(224),
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
],
to_rgb=True)
return transforms(images) return transforms(images)
def forward(self, inputs: paddle.Tensor): def forward(self, inputs: paddle.Tensor):
......
...@@ -111,5 +111,3 @@ paddlepaddle >= 2.0.0 ...@@ -111,5 +111,3 @@ paddlepaddle >= 2.0.0
paddlehub >= 2.0.0 paddlehub >= 2.0.0
paddlex >= 1.3.0 paddlex >= 1.3.0
...@@ -12,7 +12,6 @@ import paddle.nn as nn ...@@ -12,7 +12,6 @@ import paddle.nn as nn
from paddlex.seg import transforms as T from paddlex.seg import transforms as T
from paddlehub.module.module import moduleinfo, runnable, serving from paddlehub.module.module import moduleinfo, runnable, serving
METER_SHAPE = 512 METER_SHAPE = 512
CIRCLE_CENTER = [256, 256] CIRCLE_CENTER = [256, 256]
CIRCLE_RADIUS = 250 CIRCLE_RADIUS = 250
...@@ -44,11 +43,13 @@ def cv2_to_base64(image: np.ndarray): ...@@ -44,11 +43,13 @@ def cv2_to_base64(image: np.ndarray):
return base64.b64encode(data.tostring()).decode('utf8') return base64.b64encode(data.tostring()).decode('utf8')
@moduleinfo(name="barometer_reader", @moduleinfo(
name="barometer_reader",
type="CV/image_editing", type="CV/image_editing",
author="paddlepaddle", author="paddlepaddle",
author_email="", author_email="",
summary="meter_reader implements the detection and automatic reading of traditional mechanical pointer meters based on Meter detection and pointer segmentation.", summary=
"meter_reader implements the detection and automatic reading of traditional mechanical pointer meters based on Meter detection and pointer segmentation.",
version="1.0.0") version="1.0.0")
class BarometerReader(nn.Layer): class BarometerReader(nn.Layer):
def __init__(self): def __init__(self):
...@@ -121,8 +122,7 @@ class BarometerReader(nn.Layer): ...@@ -121,8 +122,7 @@ class BarometerReader(nn.Layer):
if pointer_flag: if pointer_flag:
if pointer_data[i] == 0 and pointer_data[i + 1] == 0: if pointer_data[i] == 0 and pointer_data[i + 1] == 0:
one_pointer_end = i - 1 one_pointer_end = i - 1
pointer_location = ( pointer_location = (one_pointer_start + one_pointer_end) / 2
one_pointer_start + one_pointer_end) / 2
one_pointer_start = 0 one_pointer_start = 0
one_pointer_end = 0 one_pointer_end = 0
pointer_flag = False pointer_flag = False
...@@ -135,8 +135,7 @@ class BarometerReader(nn.Layer): ...@@ -135,8 +135,7 @@ class BarometerReader(nn.Layer):
if scale_location[i] <= pointer_location and pointer_location < scale_location[i + 1]: if scale_location[i] <= pointer_location and pointer_location < scale_location[i + 1]:
scales = i + (pointer_location - scale_location[i]) / ( scales = i + (pointer_location - scale_location[i]) / (
scale_location[i + 1] - scale_location[i] + 1e-05) + 1 scale_location[i + 1] - scale_location[i] + 1e-05) + 1
ratio = (pointer_location - scale_location[0]) / ( ratio = (pointer_location - scale_location[0]) / (scale_location[scale_num - 1] - scale_location[0] + 1e-05)
scale_location[scale_num - 1] - scale_location[0] + 1e-05)
result = {'scale_num': scale_num, 'scales': scales, 'ratio': ratio} result = {'scale_num': scale_num, 'scales': scales, 'ratio': ratio}
return result return result
...@@ -147,7 +146,7 @@ class BarometerReader(nn.Layer): ...@@ -147,7 +146,7 @@ class BarometerReader(nn.Layer):
erode_kernel: int = 4, erode_kernel: int = 4,
use_erode: bool = True, use_erode: bool = True,
visualization: bool = False, visualization: bool = False,
save_dir: str ='output'): save_dir: str = 'output'):
if isinstance(im_file, str): if isinstance(im_file, str):
im = cv2.imread(im_file).astype('float32') im = cv2.imread(im_file).astype('float32')
...@@ -173,13 +172,7 @@ class BarometerReader(nn.Layer): ...@@ -173,13 +172,7 @@ class BarometerReader(nn.Layer):
meter_shape = sub_image.shape meter_shape = sub_image.shape
scale_x = float(METER_SHAPE) / float(meter_shape[1]) scale_x = float(METER_SHAPE) / float(meter_shape[1])
scale_y = float(METER_SHAPE) / float(meter_shape[0]) scale_y = float(METER_SHAPE) / float(meter_shape[0])
meter_meter = cv2.resize( meter_meter = cv2.resize(sub_image, None, None, fx=scale_x, fy=scale_y, interpolation=cv2.INTER_LINEAR)
sub_image,
None,
None,
fx=scale_x,
fy=scale_y,
interpolation=cv2.INTER_LINEAR)
meter_meter = meter_meter.astype('float32') meter_meter = meter_meter.astype('float32')
resized_meters.append(meter_meter) resized_meters.append(meter_meter)
...@@ -191,9 +184,7 @@ class BarometerReader(nn.Layer): ...@@ -191,9 +184,7 @@ class BarometerReader(nn.Layer):
for j in range(i, im_size): for j in range(i, im_size):
meter_images.append(resized_meters[j - i]) meter_images.append(resized_meters[j - i])
result = self.segmenter.batch_predict( result = self.segmenter.batch_predict(transforms=self.seg_transform, img_file_list=meter_images)
transforms=self.seg_transform,
img_file_list=meter_images)
if use_erode: if use_erode:
kernel = np.ones((erode_kernel, erode_kernel), np.uint8) kernel = np.ones((erode_kernel, erode_kernel), np.uint8)
...@@ -250,21 +241,16 @@ class BarometerReader(nn.Layer): ...@@ -250,21 +241,16 @@ class BarometerReader(nn.Layer):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict(im_file=args.input_path) results = self.predict(im_file=args.input_path)
return results return results
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
...@@ -116,4 +116,3 @@ https://github.com/PaddlePaddle/PaddleDetection ...@@ -116,4 +116,3 @@ https://github.com/PaddlePaddle/PaddleDetection
paddlepaddle >= 2.0.0 paddlepaddle >= 2.0.0
paddlehub >= 2.0.0 paddlehub >= 2.0.0
...@@ -42,38 +42,28 @@ def visualize_box_mask(im, results, labels=None, mask_resolution=14, threshold=0 ...@@ -42,38 +42,28 @@ def visualize_box_mask(im, results, labels=None, mask_resolution=14, threshold=0
im (PIL.Image.Image): visualized image im (PIL.Image.Image): visualized image
""" """
if not labels: if not labels:
labels = ['background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', labels = [
'train', 'truck', 'boat', 'traffic light', 'fire', 'hydrant', 'stop sign', 'parking meter', 'background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat',
'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'traffic light', 'fire', 'hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'hair drier', 'toothbrush'] 'teddy bear', 'hair drier', 'toothbrush'
]
if isinstance(im, str): if isinstance(im, str):
im = Image.open(im).convert('RGB') im = Image.open(im).convert('RGB')
else: else:
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
im = Image.fromarray(im) im = Image.fromarray(im)
if 'masks' in results and 'boxes' in results: if 'masks' in results and 'boxes' in results:
im = draw_mask( im = draw_mask(im, results['boxes'], results['masks'], labels, resolution=mask_resolution)
im,
results['boxes'],
results['masks'],
labels,
resolution=mask_resolution)
if 'boxes' in results: if 'boxes' in results:
im = draw_box(im, results['boxes'], labels) im = draw_box(im, results['boxes'], labels)
if 'segm' in results: if 'segm' in results:
im = draw_segm( im = draw_segm(im, results['segm'], results['label'], results['score'], labels, threshold=threshold)
im,
results['segm'],
results['label'],
results['score'],
labels,
threshold=threshold)
return im return im
...@@ -200,28 +190,19 @@ def draw_box(im, np_boxes, labels): ...@@ -200,28 +190,19 @@ def draw_box(im, np_boxes, labels):
color = tuple(clsid2color[clsid]) color = tuple(clsid2color[clsid])
# draw bbox # draw bbox
draw.line( draw.line([(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)],
[(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
(xmin, ymin)],
width=draw_thickness, width=draw_thickness,
fill=color) fill=color)
# draw label # draw label
text = "{} {:.4f}".format(labels[clsid], score) text = "{} {:.4f}".format(labels[clsid], score)
tw, th = draw.textsize(text) tw, th = draw.textsize(text)
draw.rectangle( draw.rectangle([(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
return im return im
def draw_segm(im, def draw_segm(im, np_segms, np_label, np_score, labels, threshold=0.5, alpha=0.7):
np_segms,
np_label,
np_score,
labels,
threshold=0.5,
alpha=0.7):
""" """
Draw segmentation on image. Draw segmentation on image.
""" """
...@@ -250,28 +231,17 @@ def draw_segm(im, ...@@ -250,28 +231,17 @@ def draw_segm(im,
sum_y = np.sum(mask, axis=1) sum_y = np.sum(mask, axis=1)
y = np.where(sum_y > 0.5)[0] y = np.where(sum_y > 0.5)[0]
x0, x1, y0, y1 = x[0], x[-1], y[0], y[-1] x0, x1, y0, y1 = x[0], x[-1], y[0], y[-1]
cv2.rectangle(im, (x0, y0), (x1, y1), cv2.rectangle(im, (x0, y0), (x1, y1), tuple(color_mask.astype('int32').tolist()), 1)
tuple(color_mask.astype('int32').tolist()), 1)
bbox_text = '%s %.2f' % (labels[clsid], score) bbox_text = '%s %.2f' % (labels[clsid], score)
t_size = cv2.getTextSize(bbox_text, 0, 0.3, thickness=1)[0] t_size = cv2.getTextSize(bbox_text, 0, 0.3, thickness=1)[0]
cv2.rectangle(im, (x0, y0), (x0 + t_size[0], y0 - t_size[1] - 3), cv2.rectangle(im, (x0, y0), (x0 + t_size[0], y0 - t_size[1] - 3), tuple(color_mask.astype('int32').tolist()),
tuple(color_mask.astype('int32').tolist()), -1) -1)
cv2.putText( cv2.putText(im, bbox_text, (x0, y0 - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), 1, lineType=cv2.LINE_AA)
im,
bbox_text, (x0, y0 - 2),
cv2.FONT_HERSHEY_SIMPLEX,
0.3, (0, 0, 0),
1,
lineType=cv2.LINE_AA)
return Image.fromarray(im.astype('uint8')) return Image.fromarray(im.astype('uint8'))
def load_predictor(model_dir, def load_predictor(model_dir, run_mode='fluid', batch_size=1, use_gpu=False, min_subgraph_size=3):
run_mode='fluid',
batch_size=1,
use_gpu=False,
min_subgraph_size=3):
"""set AnalysisConfig, generate AnalysisPredictor """set AnalysisConfig, generate AnalysisPredictor
Args: Args:
model_dir (str): root path of __model__ and __params__ model_dir (str): root path of __model__ and __params__
...@@ -282,20 +252,16 @@ def load_predictor(model_dir, ...@@ -282,20 +252,16 @@ def load_predictor(model_dir,
ValueError: predict by TensorRT need use_gpu == True. ValueError: predict by TensorRT need use_gpu == True.
""" """
if not use_gpu and not run_mode == 'fluid': if not use_gpu and not run_mode == 'fluid':
raise ValueError( raise ValueError("Predict by TensorRT mode: {}, expect use_gpu==True, but use_gpu == {}".format(
"Predict by TensorRT mode: {}, expect use_gpu==True, but use_gpu == {}" run_mode, use_gpu))
.format(run_mode, use_gpu))
if run_mode == 'trt_int8': if run_mode == 'trt_int8':
raise ValueError("TensorRT int8 mode is not supported now, " raise ValueError("TensorRT int8 mode is not supported now, " "please use trt_fp32 or trt_fp16 instead.")
"please use trt_fp32 or trt_fp16 instead.")
precision_map = { precision_map = {
'trt_int8': fluid.core.AnalysisConfig.Precision.Int8, 'trt_int8': fluid.core.AnalysisConfig.Precision.Int8,
'trt_fp32': fluid.core.AnalysisConfig.Precision.Float32, 'trt_fp32': fluid.core.AnalysisConfig.Precision.Float32,
'trt_fp16': fluid.core.AnalysisConfig.Precision.Half 'trt_fp16': fluid.core.AnalysisConfig.Precision.Half
} }
config = fluid.core.AnalysisConfig( config = fluid.core.AnalysisConfig(os.path.join(model_dir, '__model__'), os.path.join(model_dir, '__params__'))
os.path.join(model_dir, '__model__'),
os.path.join(model_dir, '__params__'))
if use_gpu: if use_gpu:
# initial GPU memory(M), device ID # initial GPU memory(M), device ID
config.enable_use_gpu(100, 0) config.enable_use_gpu(100, 0)
......
...@@ -33,20 +33,16 @@ class Detector(object): ...@@ -33,20 +33,16 @@ class Detector(object):
threshold (float): threshold to reserve the result for output. threshold (float): threshold to reserve the result for output.
""" """
def __init__(self, def __init__(self, min_subgraph_size: int = 60, use_gpu=False, threshold: float = 0.5):
min_subgraph_size: int = 60,
use_gpu=False,
threshold: float = 0.5):
model_dir = os.path.join(self.directory, 'solov2_r50_fpn_1x') model_dir = os.path.join(self.directory, 'solov2_r50_fpn_1x')
self.predictor = D.load_predictor( self.predictor = D.load_predictor(model_dir, min_subgraph_size=min_subgraph_size, use_gpu=use_gpu)
model_dir, self.compose = [
min_subgraph_size=min_subgraph_size, P.Resize(max_size=1333),
use_gpu=use_gpu)
self.compose = [P.Resize(max_size=1333),
P.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), P.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
P.Permute(), P.Permute(),
P.PadStride(stride=32)] P.PadStride(stride=32)
]
def transform(self, im: Union[str, np.ndarray]): def transform(self, im: Union[str, np.ndarray]):
im, im_info = P.preprocess(im, self.compose) im, im_info = P.preprocess(im, self.compose)
...@@ -61,17 +57,14 @@ class Detector(object): ...@@ -61,17 +57,14 @@ class Detector(object):
for box in np_boxes: for box in np_boxes:
print('class_id:{:d}, confidence:{:.4f},' print('class_id:{:d}, confidence:{:.4f},'
'left_top:[{:.2f},{:.2f}],' 'left_top:[{:.2f},{:.2f}],'
' right_bottom:[{:.2f},{:.2f}]'.format( ' right_bottom:[{:.2f},{:.2f}]'.format(int(box[0]), box[1], box[2], box[3], box[4], box[5]))
int(box[0]), box[1], box[2], box[3], box[4], box[5]))
results['boxes'] = np_boxes results['boxes'] = np_boxes
if np_masks is not None: if np_masks is not None:
np_masks = np_masks[expect_boxes, :, :, :] np_masks = np_masks[expect_boxes, :, :, :]
results['masks'] = np_masks results['masks'] = np_masks
return results return results
def predict(self, def predict(self, image: Union[str, np.ndarray], threshold: float = 0.5):
image: Union[str, np.ndarray],
threshold: float = 0.5):
''' '''
Args: Args:
image (str/np.ndarray): path of image/ np.ndarray read by cv2 image (str/np.ndarray): path of image/ np.ndarray read by cv2
...@@ -117,12 +110,9 @@ class DetectorSOLOv2(Detector): ...@@ -117,12 +110,9 @@ class DetectorSOLOv2(Detector):
use_gpu (bool): whether use gpu use_gpu (bool): whether use gpu
threshold (float): threshold to reserve the result for output. threshold (float): threshold to reserve the result for output.
""" """
def __init__(self,
use_gpu: bool = False, def __init__(self, use_gpu: bool = False, threshold: float = 0.5):
threshold: float = 0.5): super(DetectorSOLOv2, self).__init__(use_gpu=use_gpu, threshold=threshold)
super(DetectorSOLOv2, self).__init__(
use_gpu=use_gpu,
threshold=threshold)
def predict(self, def predict(self,
image: Union[str, np.ndarray], image: Union[str, np.ndarray],
...@@ -148,12 +138,9 @@ class DetectorSOLOv2(Detector): ...@@ -148,12 +138,9 @@ class DetectorSOLOv2(Detector):
self.predictor.zero_copy_run() self.predictor.zero_copy_run()
output_names = self.predictor.get_output_names() output_names = self.predictor.get_output_names()
np_label = self.predictor.get_output_tensor(output_names[ np_label = self.predictor.get_output_tensor(output_names[0]).copy_to_cpu()
0]).copy_to_cpu() np_score = self.predictor.get_output_tensor(output_names[1]).copy_to_cpu()
np_score = self.predictor.get_output_tensor(output_names[ np_segms = self.predictor.get_output_tensor(output_names[2]).copy_to_cpu()
1]).copy_to_cpu()
np_segms = self.predictor.get_output_tensor(output_names[
2]).copy_to_cpu()
output = dict(segm=np_segms, label=np_label, score=np_score) output = dict(segm=np_segms, label=np_label, score=np_score)
if visualization: if visualization:
......
...@@ -78,19 +78,12 @@ class Resize(object): ...@@ -78,19 +78,12 @@ class Resize(object):
im_channel = im.shape[2] im_channel = im.shape[2]
im_scale_x, im_scale_y = self.generate_scale(im) im_scale_x, im_scale_y = self.generate_scale(im)
if self.use_cv2: if self.use_cv2:
im = cv2.resize( im = cv2.resize(im, None, None, fx=im_scale_x, fy=im_scale_y, interpolation=self.interp)
im,
None,
None,
fx=im_scale_x,
fy=im_scale_y,
interpolation=self.interp)
else: else:
resize_w = int(im_scale_x * float(im.shape[1])) resize_w = int(im_scale_x * float(im.shape[1]))
resize_h = int(im_scale_y * float(im.shape[0])) resize_h = int(im_scale_y * float(im.shape[0]))
if self.max_size != 0: if self.max_size != 0:
raise TypeError( raise TypeError('If you set max_size to cap the maximum size of image,'
'If you set max_size to cap the maximum size of image,'
'please set use_cv2 to True to resize the image.') 'please set use_cv2 to True to resize the image.')
im = im.astype('uint8') im = im.astype('uint8')
im = Image.fromarray(im) im = Image.fromarray(im)
...@@ -99,8 +92,7 @@ class Resize(object): ...@@ -99,8 +92,7 @@ class Resize(object):
# padding im when image_shape fixed by infer_cfg.yml # padding im when image_shape fixed by infer_cfg.yml
if self.max_size != 0 and self.image_shape is not None: if self.max_size != 0 and self.image_shape is not None:
padding_im = np.zeros( padding_im = np.zeros((self.max_size, self.max_size, im_channel), dtype=np.float32)
(self.max_size, self.max_size, im_channel), dtype=np.float32)
im_h, im_w = im.shape[:2] im_h, im_w = im.shape[:2]
padding_im[:im_h, :im_w, :] = im padding_im[:im_h, :im_w, :] = im
im = padding_im im = padding_im
......
...@@ -207,10 +207,7 @@ class BodyPoseModel(nn.Layer): ...@@ -207,10 +207,7 @@ class BodyPoseModel(nn.Layer):
save_path = os.path.join(save_path, img_name) save_path = os.path.join(save_path, img_name)
cv2.imwrite(save_path, canvas) cv2.imwrite(save_path, canvas)
results = { results = {'candidate': candidate, 'subset': subset, 'data': canvas}
'candidate': candidate,
'subset': subset,
'data': canvas}
return results return results
...@@ -221,7 +218,7 @@ class BodyPoseModel(nn.Layer): ...@@ -221,7 +218,7 @@ class BodyPoseModel(nn.Layer):
""" """
images_decode = [P.base64_to_cv2(image) for image in images] images_decode = [P.base64_to_cv2(image) for image in images]
results = self.predict(img=images_decode[0], **kwargs) results = self.predict(img=images_decode[0], **kwargs)
final={} final = {}
final['candidate'] = P.cv2_to_base64(results['candidate']) final['candidate'] = P.cv2_to_base64(results['candidate'])
final['subset'] = P.cv2_to_base64(results['subset']) final['subset'] = P.cv2_to_base64(results['subset'])
final['data'] = P.cv2_to_base64(results['data']) final['data'] = P.cv2_to_base64(results['data'])
...@@ -238,19 +235,13 @@ class BodyPoseModel(nn.Layer): ...@@ -238,19 +235,13 @@ class BodyPoseModel(nn.Layer):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(img=args.input_path, save_path=args.output_dir, visualization=args.visualization)
img=args.input_path,
save_path=args.output_dir,
visualization=args.visualization)
return results return results
...@@ -260,19 +251,12 @@ class BodyPoseModel(nn.Layer): ...@@ -260,19 +251,12 @@ class BodyPoseModel(nn.Layer):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--output_dir', '--output_dir', type=str, default='openpose_body', help="The directory to save output images.")
type=str,
default='openpose_body',
help="The directory to save output images.")
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--visualization', '--visualization', type=bool, default=True, help="whether to save output as images.")
type=bool,
default=True,
help="whether to save output as images.")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
\ No newline at end of file
...@@ -170,7 +170,11 @@ class HandPoseModel(nn.Layer): ...@@ -170,7 +170,11 @@ class HandPoseModel(nn.Layer):
return np.array(all_peaks) return np.array(all_peaks)
def predict(self, img: Union[str, np.ndarray], save_path: str = 'openpose_hand', scale: list = [0.5, 1.0, 1.5, 2.0], visualization: bool = True): def predict(self,
img: Union[str, np.ndarray],
save_path: str = 'openpose_hand',
scale: list = [0.5, 1.0, 1.5, 2.0],
visualization: bool = True):
self.eval() self.eval()
self.visualization = visualization self.visualization = visualization
if isinstance(img, str): if isinstance(img, str):
...@@ -193,7 +197,11 @@ class HandPoseModel(nn.Layer): ...@@ -193,7 +197,11 @@ class HandPoseModel(nn.Layer):
peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y) peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y)
all_hand_peaks.append(peaks) all_hand_peaks.append(peaks)
canvas = copy.deepcopy(org_img) canvas = copy.deepcopy(org_img)
canvas = self.draw_pose(canvas, body_result['candidate'], body_result['subset'],) canvas = self.draw_pose(
canvas,
body_result['candidate'],
body_result['subset'],
)
canvas = self.draw_hand(canvas, all_hand_peaks) canvas = self.draw_hand(canvas, all_hand_peaks)
if self.visualization: if self.visualization:
if not os.path.exists(save_path): if not os.path.exists(save_path):
...@@ -202,9 +210,7 @@ class HandPoseModel(nn.Layer): ...@@ -202,9 +210,7 @@ class HandPoseModel(nn.Layer):
save_path = os.path.join(save_path, img_name) save_path = os.path.join(save_path, img_name)
cv2.imwrite(save_path, canvas) cv2.imwrite(save_path, canvas)
results = { results = {'all_hand_peaks': all_hand_peaks, 'data': canvas}
'all_hand_peaks': all_hand_peaks,
'data': canvas}
return results return results
...@@ -215,8 +221,8 @@ class HandPoseModel(nn.Layer): ...@@ -215,8 +221,8 @@ class HandPoseModel(nn.Layer):
""" """
images_decode = [P.base64_to_cv2(image) for image in images] images_decode = [P.base64_to_cv2(image) for image in images]
results = self.predict(img=images_decode[0], **kwargs) results = self.predict(img=images_decode[0], **kwargs)
final={} final = {}
final['all_hand_peaks']=[peak.tolist() for peak in results['all_hand_peaks']] final['all_hand_peaks'] = [peak.tolist() for peak in results['all_hand_peaks']]
final['data'] = P.cv2_to_base64(results['data']) final['data'] = P.cv2_to_base64(results['data'])
return final return final
...@@ -230,20 +236,14 @@ class HandPoseModel(nn.Layer): ...@@ -230,20 +236,14 @@ class HandPoseModel(nn.Layer):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(
img=args.input_path, img=args.input_path, save_path=args.output_dir, scale=args.scale, visualization=args.visualization)
save_path=args.output_dir,
scale=args.scale,
visualization=args.visualization)
return results return results
...@@ -253,24 +253,14 @@ class HandPoseModel(nn.Layer): ...@@ -253,24 +253,14 @@ class HandPoseModel(nn.Layer):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--output_dir', '--output_dir', type=str, default='openpose_hand', help="The directory to save output images.")
type=str,
default='openpose_hand',
help="The directory to save output images.")
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--scale', '--scale', type=list, default=[0.5, 1.0, 1.5, 2.0], help="The search scale for openpose hands model.")
type=list,
default=[0.5, 1.0, 1.5, 2.0],
help="The search scale for openpose hands model.")
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--visualization', '--visualization', type=bool, default=True, help="whether to save output as images.")
type=bool,
default=True,
help="whether to save output as images.")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
\ No newline at end of file
...@@ -232,6 +232,7 @@ class ResizeScaling: ...@@ -232,6 +232,7 @@ class ResizeScaling:
resize_img = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=self.interpolation) resize_img = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=self.interpolation)
return resize_img return resize_img
def npmax(array: np.ndarray): def npmax(array: np.ndarray):
"""Get max value and index.""" """Get max value and index."""
arrayindex = array.argmax(1) arrayindex = array.argmax(1)
......
...@@ -6,6 +6,7 @@ import numpy as np ...@@ -6,6 +6,7 @@ import numpy as np
from paddle.nn import Layer from paddle.nn import Layer
from paddlehub.module.module import moduleinfo from paddlehub.module.module import moduleinfo
@moduleinfo( @moduleinfo(
name="ExtremeC3_Portrait_Segmentation", # 模型名称 name="ExtremeC3_Portrait_Segmentation", # 模型名称
type="CV/semantic_segmentation", # 模型类型 type="CV/semantic_segmentation", # 模型类型
...@@ -26,7 +27,7 @@ class ExtremeC3_Portrait_Segmentation(Layer): ...@@ -26,7 +27,7 @@ class ExtremeC3_Portrait_Segmentation(Layer):
self.model.eval() self.model.eval()
# 均值方差 # 均值方差
self.mean = [107.304565, 115.69884, 132.35703 ] self.mean = [107.304565, 115.69884, 132.35703]
self.std = [63.97182, 65.1337, 68.29726] self.std = [63.97182, 65.1337, 68.29726]
# 读取数据函数 # 读取数据函数
...@@ -107,20 +108,12 @@ class ExtremeC3_Portrait_Segmentation(Layer): ...@@ -107,20 +108,12 @@ class ExtremeC3_Portrait_Segmentation(Layer):
cv2.imwrite(os.path.join(output_dir, 'result_mask_%d.png' % i), mask) cv2.imwrite(os.path.join(output_dir, 'result_mask_%d.png' % i), mask)
cv2.imwrite(os.path.join(output_dir, 'result_%d.png' % i), result) cv2.imwrite(os.path.join(output_dir, 'result_%d.png' % i), result)
results.append({ results.append({'mask': mask, 'result': result})
'mask': mask,
'result': result
})
return results return results
# 关键点检测函数 # 关键点检测函数
def Segmentation(self, def Segmentation(self, images=None, paths=None, batch_size=1, output_dir='output', visualization=False):
images=None,
paths=None,
batch_size=1,
output_dir='output',
visualization=False):
# 加载数据处理器 # 加载数据处理器
datas = self.load_datas(paths, images) datas = self.load_datas(paths, images)
......
...@@ -6,6 +6,7 @@ import numpy as np ...@@ -6,6 +6,7 @@ import numpy as np
from paddle.nn import Layer from paddle.nn import Layer
from paddlehub.module.module import moduleinfo from paddlehub.module.module import moduleinfo
@moduleinfo( @moduleinfo(
name="SINet_Portrait_Segmentation", # 模型名称 name="SINet_Portrait_Segmentation", # 模型名称
type="CV/semantic_segmentation", # 模型类型 type="CV/semantic_segmentation", # 模型类型
...@@ -26,7 +27,7 @@ class SINet_Portrait_Segmentation(Layer): ...@@ -26,7 +27,7 @@ class SINet_Portrait_Segmentation(Layer):
self.model.eval() self.model.eval()
# 均值方差 # 均值方差
self.mean = [107.304565, 115.69884, 132.35703 ] self.mean = [107.304565, 115.69884, 132.35703]
self.std = [63.97182, 65.1337, 68.29726] self.std = [63.97182, 65.1337, 68.29726]
# 读取数据函数 # 读取数据函数
...@@ -107,20 +108,12 @@ class SINet_Portrait_Segmentation(Layer): ...@@ -107,20 +108,12 @@ class SINet_Portrait_Segmentation(Layer):
cv2.imwrite(os.path.join(output_dir, 'result_mask_%d.png' % i), mask) cv2.imwrite(os.path.join(output_dir, 'result_mask_%d.png' % i), mask)
cv2.imwrite(os.path.join(output_dir, 'result_%d.png' % i), result) cv2.imwrite(os.path.join(output_dir, 'result_%d.png' % i), result)
results.append({ results.append({'mask': mask, 'result': result})
'mask': mask,
'result': result
})
return results return results
# 关键点检测函数 # 关键点检测函数
def Segmentation(self, def Segmentation(self, images=None, paths=None, batch_size=1, output_dir='output', visualization=False):
images=None,
paths=None,
batch_size=1,
output_dir='output',
visualization=False):
# 加载数据处理器 # 加载数据处理器
datas = self.load_datas(paths, images) datas = self.load_datas(paths, images)
......
...@@ -30,8 +30,7 @@ def SyncBatchNorm(*args, **kwargs): ...@@ -30,8 +30,7 @@ def SyncBatchNorm(*args, **kwargs):
class ConvBNLayer(nn.Layer): class ConvBNLayer(nn.Layer):
"""Basic conv bn relu layer.""" """Basic conv bn relu layer."""
def __init__( def __init__(self,
self,
in_channels: int, in_channels: int,
out_channels: int, out_channels: int,
kernel_size: int, kernel_size: int,
...@@ -44,8 +43,7 @@ class ConvBNLayer(nn.Layer): ...@@ -44,8 +43,7 @@ class ConvBNLayer(nn.Layer):
super(ConvBNLayer, self).__init__() super(ConvBNLayer, self).__init__()
self.is_vd_mode = is_vd_mode self.is_vd_mode = is_vd_mode
self._pool2d_avg = AvgPool2D( self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True)
kernel_size=2, stride=2, padding=0, ceil_mode=True)
self._conv = Conv2D( self._conv = Conv2D(
in_channels=in_channels, in_channels=in_channels,
out_channels=out_channels, out_channels=out_channels,
...@@ -83,11 +81,7 @@ class BottleneckBlock(nn.Layer): ...@@ -83,11 +81,7 @@ class BottleneckBlock(nn.Layer):
super(BottleneckBlock, self).__init__() super(BottleneckBlock, self).__init__()
self.conv0 = ConvBNLayer( self.conv0 = ConvBNLayer(
in_channels=in_channels, in_channels=in_channels, out_channels=out_channels, kernel_size=1, act='relu', name=name + "_branch2a")
out_channels=out_channels,
kernel_size=1,
act='relu',
name=name + "_branch2a")
self.dilation = dilation self.dilation = dilation
...@@ -100,11 +94,7 @@ class BottleneckBlock(nn.Layer): ...@@ -100,11 +94,7 @@ class BottleneckBlock(nn.Layer):
dilation=dilation, dilation=dilation,
name=name + "_branch2b") name=name + "_branch2b")
self.conv2 = ConvBNLayer( self.conv2 = ConvBNLayer(
in_channels=out_channels, in_channels=out_channels, out_channels=out_channels * 4, kernel_size=1, act=None, name=name + "_branch2c")
out_channels=out_channels * 4,
kernel_size=1,
act=None,
name=name + "_branch2c")
if not shortcut: if not shortcut:
self.short = ConvBNLayer( self.short = ConvBNLayer(
...@@ -139,12 +129,7 @@ class BottleneckBlock(nn.Layer): ...@@ -139,12 +129,7 @@ class BottleneckBlock(nn.Layer):
class SeparableConvBNReLU(nn.Layer): class SeparableConvBNReLU(nn.Layer):
"""Depthwise Separable Convolution.""" """Depthwise Separable Convolution."""
def __init__(self, def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
super(SeparableConvBNReLU, self).__init__() super(SeparableConvBNReLU, self).__init__()
self.depthwise_conv = ConvBN( self.depthwise_conv = ConvBN(
in_channels, in_channels,
...@@ -153,8 +138,7 @@ class SeparableConvBNReLU(nn.Layer): ...@@ -153,8 +138,7 @@ class SeparableConvBNReLU(nn.Layer):
padding=padding, padding=padding,
groups=in_channels, groups=in_channels,
**kwargs) **kwargs)
self.piontwise_conv = ConvBNReLU( self.piontwise_conv = ConvBNReLU(in_channels, out_channels, kernel_size=1, groups=1)
in_channels, out_channels, kernel_size=1, groups=1)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
x = self.depthwise_conv(x) x = self.depthwise_conv(x)
...@@ -165,15 +149,9 @@ class SeparableConvBNReLU(nn.Layer): ...@@ -165,15 +149,9 @@ class SeparableConvBNReLU(nn.Layer):
class ConvBN(nn.Layer): class ConvBN(nn.Layer):
"""Basic conv bn layer""" """Basic conv bn layer"""
def __init__(self, def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
super(ConvBN, self).__init__() super(ConvBN, self).__init__()
self._conv = Conv2D( self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels) self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
...@@ -185,16 +163,10 @@ class ConvBN(nn.Layer): ...@@ -185,16 +163,10 @@ class ConvBN(nn.Layer):
class ConvBNReLU(nn.Layer): class ConvBNReLU(nn.Layer):
"""Basic conv bn relu layer.""" """Basic conv bn relu layer."""
def __init__(self, def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
super(ConvBNReLU, self).__init__() super(ConvBNReLU, self).__init__()
self._conv = Conv2D( self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels) self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
...@@ -251,8 +223,7 @@ class Activation(nn.Layer): ...@@ -251,8 +223,7 @@ class Activation(nn.Layer):
act_name = act_dict[act] act_name = act_dict[act]
self.act_func = eval("activation.{}()".format(act_name)) self.act_func = eval("activation.{}()".format(act_name))
else: else:
raise KeyError("{} does not exist in the current {}".format( raise KeyError("{} does not exist in the current {}".format(act, act_dict.keys()))
act, act_dict.keys()))
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
...@@ -281,7 +252,7 @@ class ASPPModule(nn.Layer): ...@@ -281,7 +252,7 @@ class ASPPModule(nn.Layer):
in_channels: int, in_channels: int,
out_channels: int, out_channels: int,
align_corners: bool, align_corners: bool,
use_sep_conv: bool= False, use_sep_conv: bool = False,
image_pooling: bool = False): image_pooling: bool = False):
super().__init__() super().__init__()
...@@ -311,10 +282,7 @@ class ASPPModule(nn.Layer): ...@@ -311,10 +282,7 @@ class ASPPModule(nn.Layer):
out_size += 1 out_size += 1
self.image_pooling = image_pooling self.image_pooling = image_pooling
self.conv_bn_relu = ConvBNReLU( self.conv_bn_relu = ConvBNReLU(in_channels=out_channels * out_size, out_channels=out_channels, kernel_size=1)
in_channels=out_channels * out_size,
out_channels=out_channels,
kernel_size=1)
self.dropout = nn.Dropout(p=0.1) # drop rate self.dropout = nn.Dropout(p=0.1) # drop rate
...@@ -322,20 +290,12 @@ class ASPPModule(nn.Layer): ...@@ -322,20 +290,12 @@ class ASPPModule(nn.Layer):
outputs = [] outputs = []
for block in self.aspp_blocks: for block in self.aspp_blocks:
y = block(x) y = block(x)
y = F.interpolate( y = F.interpolate(y, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
y,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
outputs.append(y) outputs.append(y)
if self.image_pooling: if self.image_pooling:
img_avg = self.global_avg_pool(x) img_avg = self.global_avg_pool(x)
img_avg = F.interpolate( img_avg = F.interpolate(img_avg, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
img_avg,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
outputs.append(img_avg) outputs.append(img_avg)
x = paddle.concat(outputs, axis=1) x = paddle.concat(outputs, axis=1)
......
...@@ -27,7 +27,6 @@ from deeplabv3p_resnet50_voc.resnet import ResNet50_vd ...@@ -27,7 +27,6 @@ from deeplabv3p_resnet50_voc.resnet import ResNet50_vd
import deeplabv3p_resnet50_voc.layers as L import deeplabv3p_resnet50_voc.layers as L
@moduleinfo( @moduleinfo(
name="deeplabv3p_resnet50_voc", name="deeplabv3p_resnet50_voc",
type="CV/semantic_segmentation", type="CV/semantic_segmentation",
...@@ -72,9 +71,8 @@ class DeepLabV3PResnet50(nn.Layer): ...@@ -72,9 +71,8 @@ class DeepLabV3PResnet50(nn.Layer):
super(DeepLabV3PResnet50, self).__init__() super(DeepLabV3PResnet50, self).__init__()
self.backbone = ResNet50_vd() self.backbone = ResNet50_vd()
backbone_channels = [self.backbone.feat_channels[i] for i in backbone_indices] backbone_channels = [self.backbone.feat_channels[i] for i in backbone_indices]
self.head = DeepLabV3PHead(num_classes, backbone_indices, self.head = DeepLabV3PHead(num_classes, backbone_indices, backbone_channels, aspp_ratios, aspp_out_channels,
backbone_channels, aspp_ratios, align_corners)
aspp_out_channels, align_corners)
self.align_corners = align_corners self.align_corners = align_corners
self.transforms = T.Compose([T.Normalize()]) self.transforms = T.Compose([T.Normalize()])
...@@ -96,11 +94,8 @@ class DeepLabV3PResnet50(nn.Layer): ...@@ -96,11 +94,8 @@ class DeepLabV3PResnet50(nn.Layer):
feat_list = self.backbone(x) feat_list = self.backbone(x)
logit_list = self.head(feat_list) logit_list = self.head(feat_list)
return [ return [
F.interpolate( F.interpolate(logit, x.shape[2:], mode='bilinear', align_corners=self.align_corners) for logit in logit_list
logit, ]
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners) for logit in logit_list]
class DeepLabV3PHead(nn.Layer): class DeepLabV3PHead(nn.Layer):
...@@ -123,17 +118,13 @@ class DeepLabV3PHead(nn.Layer): ...@@ -123,17 +118,13 @@ class DeepLabV3PHead(nn.Layer):
is even, e.g. 1024x512, otherwise it is True, e.g. 769x769. is even, e.g. 1024x512, otherwise it is True, e.g. 769x769.
""" """
def __init__(self, num_classes: int, backbone_indices: Tuple[paddle.Tensor], backbone_channels: Tuple[paddle.Tensor], def __init__(self, num_classes: int, backbone_indices: Tuple[paddle.Tensor],
aspp_ratios: Tuple[float], aspp_out_channels: int, align_corners: bool): backbone_channels: Tuple[paddle.Tensor], aspp_ratios: Tuple[float], aspp_out_channels: int,
align_corners: bool):
super().__init__() super().__init__()
self.aspp = L.ASPPModule( self.aspp = L.ASPPModule(
aspp_ratios, aspp_ratios, backbone_channels[1], aspp_out_channels, align_corners, use_sep_conv=True, image_pooling=True)
backbone_channels[1],
aspp_out_channels,
align_corners,
use_sep_conv=True,
image_pooling=True)
self.decoder = Decoder(num_classes, backbone_channels[0], align_corners) self.decoder = Decoder(num_classes, backbone_channels[0], align_corners)
self.backbone_indices = backbone_indices self.backbone_indices = backbone_indices
...@@ -160,25 +151,17 @@ class Decoder(nn.Layer): ...@@ -160,25 +151,17 @@ class Decoder(nn.Layer):
def __init__(self, num_classes: int, in_channels: int, align_corners: bool): def __init__(self, num_classes: int, in_channels: int, align_corners: bool):
super(Decoder, self).__init__() super(Decoder, self).__init__()
self.conv_bn_relu1 = L.ConvBNReLU( self.conv_bn_relu1 = L.ConvBNReLU(in_channels=in_channels, out_channels=48, kernel_size=1)
in_channels=in_channels, out_channels=48, kernel_size=1)
self.conv_bn_relu2 = L.SeparableConvBNReLU( self.conv_bn_relu2 = L.SeparableConvBNReLU(in_channels=304, out_channels=256, kernel_size=3, padding=1)
in_channels=304, out_channels=256, kernel_size=3, padding=1) self.conv_bn_relu3 = L.SeparableConvBNReLU(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.conv_bn_relu3 = L.SeparableConvBNReLU( self.conv = nn.Conv2D(in_channels=256, out_channels=num_classes, kernel_size=1)
in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.conv = nn.Conv2D(
in_channels=256, out_channels=num_classes, kernel_size=1)
self.align_corners = align_corners self.align_corners = align_corners
def forward(self, x: paddle.Tensor, low_level_feat: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor, low_level_feat: paddle.Tensor) -> paddle.Tensor:
low_level_feat = self.conv_bn_relu1(low_level_feat) low_level_feat = self.conv_bn_relu1(low_level_feat)
x = F.interpolate( x = F.interpolate(x, low_level_feat.shape[2:], mode='bilinear', align_corners=self.align_corners)
x,
low_level_feat.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
x = paddle.concat([x, low_level_feat], axis=1) x = paddle.concat([x, low_level_feat], axis=1)
x = self.conv_bn_relu2(x) x = self.conv_bn_relu2(x)
x = self.conv_bn_relu3(x) x = self.conv_bn_relu3(x)
......
...@@ -36,11 +36,7 @@ class BasicBlock(nn.Layer): ...@@ -36,11 +36,7 @@ class BasicBlock(nn.Layer):
act='relu', act='relu',
name=name + "_branch2a") name=name + "_branch2a")
self.conv1 = L.ConvBNLayer( self.conv1 = L.ConvBNLayer(
in_channels=out_channels, in_channels=out_channels, out_channels=out_channels, kernel_size=3, act=None, name=name + "_branch2b")
out_channels=out_channels,
kernel_size=3,
act=None,
name=name + "_branch2b")
if not shortcut: if not shortcut:
self.short = L.ConvBNLayer( self.short = L.ConvBNLayer(
...@@ -67,8 +63,7 @@ class BasicBlock(nn.Layer): ...@@ -67,8 +63,7 @@ class BasicBlock(nn.Layer):
class ResNet50_vd(nn.Layer): class ResNet50_vd(nn.Layer):
def __init__(self, def __init__(self, multi_grid: tuple = (1, 2, 4)):
multi_grid: tuple = (1, 2, 4)):
super(ResNet50_vd, self).__init__() super(ResNet50_vd, self).__init__()
depth = [3, 4, 6, 3] depth = [3, 4, 6, 3]
num_channels = [64, 256, 512, 1024] num_channels = [64, 256, 512, 1024]
...@@ -76,26 +71,11 @@ class ResNet50_vd(nn.Layer): ...@@ -76,26 +71,11 @@ class ResNet50_vd(nn.Layer):
self.feat_channels = [c * 4 for c in num_filters] self.feat_channels = [c * 4 for c in num_filters]
dilation_dict = {2: 2, 3: 4} dilation_dict = {2: 2, 3: 4}
self.conv1_1 = L.ConvBNLayer( self.conv1_1 = L.ConvBNLayer(
in_channels=3, in_channels=3, out_channels=32, kernel_size=3, stride=2, act='relu', name="conv1_1")
out_channels=32,
kernel_size=3,
stride=2,
act='relu',
name="conv1_1")
self.conv1_2 = L.ConvBNLayer( self.conv1_2 = L.ConvBNLayer(
in_channels=32, in_channels=32, out_channels=32, kernel_size=3, stride=1, act='relu', name="conv1_2")
out_channels=32,
kernel_size=3,
stride=1,
act='relu',
name="conv1_2")
self.conv1_3 = L.ConvBNLayer( self.conv1_3 = L.ConvBNLayer(
in_channels=32, in_channels=32, out_channels=64, kernel_size=3, stride=1, act='relu', name="conv1_3")
out_channels=64,
kernel_size=3,
stride=1,
act='relu',
name="conv1_3")
self.pool2d_max = nn.MaxPool2D(kernel_size=3, stride=2, padding=1) self.pool2d_max = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
self.stage_list = [] self.stage_list = []
...@@ -104,18 +84,15 @@ class ResNet50_vd(nn.Layer): ...@@ -104,18 +84,15 @@ class ResNet50_vd(nn.Layer):
block_list = [] block_list = []
for i in range(depth[block]): for i in range(depth[block]):
conv_name = "res" + str(block + 2) + chr(97 + i) conv_name = "res" + str(block + 2) + chr(97 + i)
dilation_rate = dilation_dict[ dilation_rate = dilation_dict[block] if dilation_dict and block in dilation_dict else 1
block] if dilation_dict and block in dilation_dict else 1
if block == 3: if block == 3:
dilation_rate = dilation_rate * multi_grid[i] dilation_rate = dilation_rate * multi_grid[i]
bottleneck_block = self.add_sublayer( bottleneck_block = self.add_sublayer(
'bb_%d_%d' % (block, i), 'bb_%d_%d' % (block, i),
L.BottleneckBlock( L.BottleneckBlock(
in_channels=num_channels[block] in_channels=num_channels[block] if i == 0 else num_filters[block] * 4,
if i == 0 else num_filters[block] * 4,
out_channels=num_filters[block], out_channels=num_filters[block],
stride=2 if i == 0 and block != 0 stride=2 if i == 0 and block != 0 and dilation_rate == 1 else 1,
and dilation_rate == 1 else 1,
shortcut=shortcut, shortcut=shortcut,
if_first=block == i == 0, if_first=block == i == 0,
name=conv_name, name=conv_name,
......
...@@ -51,8 +51,8 @@ class HRNet_W18(nn.Layer): ...@@ -51,8 +51,8 @@ class HRNet_W18(nn.Layer):
def __init__(self, def __init__(self,
pretrained: str = None, pretrained: str = None,
stage1_num_modules: int = 1, stage1_num_modules: int = 1,
stage1_num_blocks: tuple = (4,), stage1_num_blocks: tuple = (4, ),
stage1_num_channels: tuple = (64,), stage1_num_channels: tuple = (64, ),
stage2_num_modules: int = 1, stage2_num_modules: int = 1,
stage2_num_blocks: tuple = (4, 4), stage2_num_blocks: tuple = (4, 4),
stage2_num_channels: tuple = (18, 36), stage2_num_channels: tuple = (18, 36),
...@@ -83,20 +83,10 @@ class HRNet_W18(nn.Layer): ...@@ -83,20 +83,10 @@ class HRNet_W18(nn.Layer):
self.feat_channels = [sum(stage4_num_channels)] self.feat_channels = [sum(stage4_num_channels)]
self.conv_layer1_1 = L.ConvBNReLU( self.conv_layer1_1 = L.ConvBNReLU(
in_channels=3, in_channels=3, out_channels=64, kernel_size=3, stride=2, padding='same', bias_attr=False)
out_channels=64,
kernel_size=3,
stride=2,
padding='same',
bias_attr=False)
self.conv_layer1_2 = L.ConvBNReLU( self.conv_layer1_2 = L.ConvBNReLU(
in_channels=64, in_channels=64, out_channels=64, kernel_size=3, stride=2, padding='same', bias_attr=False)
out_channels=64,
kernel_size=3,
stride=2,
padding='same',
bias_attr=False)
self.la1 = Layer1( self.la1 = Layer1(
num_channels=64, num_channels=64,
...@@ -106,9 +96,7 @@ class HRNet_W18(nn.Layer): ...@@ -106,9 +96,7 @@ class HRNet_W18(nn.Layer):
name="layer2") name="layer2")
self.tr1 = TransitionLayer( self.tr1 = TransitionLayer(
in_channels=[self.stage1_num_channels[0] * 4], in_channels=[self.stage1_num_channels[0] * 4], out_channels=self.stage2_num_channels, name="tr1")
out_channels=self.stage2_num_channels,
name="tr1")
self.st2 = Stage( self.st2 = Stage(
num_channels=self.stage2_num_channels, num_channels=self.stage2_num_channels,
...@@ -120,9 +108,7 @@ class HRNet_W18(nn.Layer): ...@@ -120,9 +108,7 @@ class HRNet_W18(nn.Layer):
align_corners=align_corners) align_corners=align_corners)
self.tr2 = TransitionLayer( self.tr2 = TransitionLayer(
in_channels=self.stage2_num_channels, in_channels=self.stage2_num_channels, out_channels=self.stage3_num_channels, name="tr2")
out_channels=self.stage3_num_channels,
name="tr2")
self.st3 = Stage( self.st3 = Stage(
num_channels=self.stage3_num_channels, num_channels=self.stage3_num_channels,
num_modules=self.stage3_num_modules, num_modules=self.stage3_num_modules,
...@@ -133,9 +119,7 @@ class HRNet_W18(nn.Layer): ...@@ -133,9 +119,7 @@ class HRNet_W18(nn.Layer):
align_corners=align_corners) align_corners=align_corners)
self.tr3 = TransitionLayer( self.tr3 = TransitionLayer(
in_channels=self.stage3_num_channels, in_channels=self.stage3_num_channels, out_channels=self.stage4_num_channels, name="tr3")
out_channels=self.stage4_num_channels,
name="tr3")
self.st4 = Stage( self.st4 = Stage(
num_channels=self.stage4_num_channels, num_channels=self.stage4_num_channels,
num_modules=self.stage4_num_modules, num_modules=self.stage4_num_modules,
...@@ -161,30 +145,16 @@ class HRNet_W18(nn.Layer): ...@@ -161,30 +145,16 @@ class HRNet_W18(nn.Layer):
st4 = self.st4(tr3) st4 = self.st4(tr3)
x0_h, x0_w = st4[0].shape[2:] x0_h, x0_w = st4[0].shape[2:]
x1 = F.interpolate( x1 = F.interpolate(st4[1], (x0_h, x0_w), mode='bilinear', align_corners=self.align_corners)
st4[1], (x0_h, x0_w), x2 = F.interpolate(st4[2], (x0_h, x0_w), mode='bilinear', align_corners=self.align_corners)
mode='bilinear', x3 = F.interpolate(st4[3], (x0_h, x0_w), mode='bilinear', align_corners=self.align_corners)
align_corners=self.align_corners)
x2 = F.interpolate(
st4[2], (x0_h, x0_w),
mode='bilinear',
align_corners=self.align_corners)
x3 = F.interpolate(
st4[3], (x0_h, x0_w),
mode='bilinear',
align_corners=self.align_corners)
x = paddle.concat([st4[0], x1, x2, x3], axis=1) x = paddle.concat([st4[0], x1, x2, x3], axis=1)
return [x] return [x]
class Layer1(nn.Layer): class Layer1(nn.Layer):
def __init__(self, def __init__(self, num_channels: int, num_filters: int, num_blocks: int, has_se: bool = False, name: str = None):
num_channels: int,
num_filters: int,
num_blocks: int,
has_se: bool = False,
name: str = None):
super(Layer1, self).__init__() super(Layer1, self).__init__()
self.bottleneck_block_list = [] self.bottleneck_block_list = []
...@@ -253,12 +223,7 @@ class TransitionLayer(nn.Layer): ...@@ -253,12 +223,7 @@ class TransitionLayer(nn.Layer):
class Branches(nn.Layer): class Branches(nn.Layer):
def __init__(self, def __init__(self, num_blocks: int, in_channels: int, out_channels: int, has_se: bool = False, name: str = None):
num_blocks: int,
in_channels: int,
out_channels: int,
has_se: bool = False,
name: str = None):
super(Branches, self).__init__() super(Branches, self).__init__()
self.basic_block_list = [] self.basic_block_list = []
...@@ -273,8 +238,7 @@ class Branches(nn.Layer): ...@@ -273,8 +238,7 @@ class Branches(nn.Layer):
num_channels=in_ch, num_channels=in_ch,
num_filters=out_channels[i], num_filters=out_channels[i],
has_se=has_se, has_se=has_se,
name=name + '_branch_layer_' + str(i + 1) + '_' + name=name + '_branch_layer_' + str(i + 1) + '_' + str(j + 1)))
str(j + 1)))
self.basic_block_list[i].append(basic_block_func) self.basic_block_list[i].append(basic_block_func)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
...@@ -301,11 +265,7 @@ class BottleneckBlock(nn.Layer): ...@@ -301,11 +265,7 @@ class BottleneckBlock(nn.Layer):
self.downsample = downsample self.downsample = downsample
self.conv1 = L.ConvBNReLU( self.conv1 = L.ConvBNReLU(
in_channels=num_channels, in_channels=num_channels, out_channels=num_filters, kernel_size=1, padding='same', bias_attr=False)
out_channels=num_filters,
kernel_size=1,
padding='same',
bias_attr=False)
self.conv2 = L.ConvBNReLU( self.conv2 = L.ConvBNReLU(
in_channels=num_filters, in_channels=num_filters,
...@@ -316,26 +276,15 @@ class BottleneckBlock(nn.Layer): ...@@ -316,26 +276,15 @@ class BottleneckBlock(nn.Layer):
bias_attr=False) bias_attr=False)
self.conv3 = L.ConvBN( self.conv3 = L.ConvBN(
in_channels=num_filters, in_channels=num_filters, out_channels=num_filters * 4, kernel_size=1, padding='same', bias_attr=False)
out_channels=num_filters * 4,
kernel_size=1,
padding='same',
bias_attr=False)
if self.downsample: if self.downsample:
self.conv_down = L.ConvBN( self.conv_down = L.ConvBN(
in_channels=num_channels, in_channels=num_channels, out_channels=num_filters * 4, kernel_size=1, padding='same', bias_attr=False)
out_channels=num_filters * 4,
kernel_size=1,
padding='same',
bias_attr=False)
if self.has_se: if self.has_se:
self.se = SELayer( self.se = SELayer(
num_channels=num_filters * 4, num_channels=num_filters * 4, num_filters=num_filters * 4, reduction_ratio=16, name=name + '_fc')
num_filters=num_filters * 4,
reduction_ratio=16,
name=name + '_fc')
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
residual = x residual = x
...@@ -375,26 +324,14 @@ class BasicBlock(nn.Layer): ...@@ -375,26 +324,14 @@ class BasicBlock(nn.Layer):
padding='same', padding='same',
bias_attr=False) bias_attr=False)
self.conv2 = L.ConvBN( self.conv2 = L.ConvBN(
in_channels=num_filters, in_channels=num_filters, out_channels=num_filters, kernel_size=3, padding='same', bias_attr=False)
out_channels=num_filters,
kernel_size=3,
padding='same',
bias_attr=False)
if self.downsample: if self.downsample:
self.conv_down = L.ConvBNReLU( self.conv_down = L.ConvBNReLU(
in_channels=num_channels, in_channels=num_channels, out_channels=num_filters, kernel_size=1, padding='same', bias_attr=False)
out_channels=num_filters,
kernel_size=1,
padding='same',
bias_attr=False)
if self.has_se: if self.has_se:
self.se = SELayer( self.se = SELayer(num_channels=num_filters, num_filters=num_filters, reduction_ratio=16, name=name + '_fc')
num_channels=num_filters,
num_filters=num_filters,
reduction_ratio=16,
name=name + '_fc')
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
residual = x residual = x
...@@ -423,17 +360,11 @@ class SELayer(nn.Layer): ...@@ -423,17 +360,11 @@ class SELayer(nn.Layer):
med_ch = int(num_channels / reduction_ratio) med_ch = int(num_channels / reduction_ratio)
stdv = 1.0 / math.sqrt(num_channels * 1.0) stdv = 1.0 / math.sqrt(num_channels * 1.0)
self.squeeze = nn.Linear( self.squeeze = nn.Linear(
num_channels, num_channels, med_ch, weight_attr=paddle.ParamAttr(initializer=nn.initializer.Uniform(-stdv, stdv)))
med_ch,
weight_attr=paddle.ParamAttr(
initializer=nn.initializer.Uniform(-stdv, stdv)))
stdv = 1.0 / math.sqrt(med_ch * 1.0) stdv = 1.0 / math.sqrt(med_ch * 1.0)
self.excitation = nn.Linear( self.excitation = nn.Linear(
med_ch, med_ch, num_filters, weight_attr=paddle.ParamAttr(initializer=nn.initializer.Uniform(-stdv, stdv)))
num_filters,
weight_attr=paddle.ParamAttr(
initializer=nn.initializer.Uniform(-stdv, stdv)))
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
pool = self.pool2d_gap(x) pool = self.pool2d_gap(x)
...@@ -442,8 +373,7 @@ class SELayer(nn.Layer): ...@@ -442,8 +373,7 @@ class SELayer(nn.Layer):
squeeze = F.relu(squeeze) squeeze = F.relu(squeeze)
excitation = self.excitation(squeeze) excitation = self.excitation(squeeze)
excitation = F.sigmoid(excitation) excitation = F.sigmoid(excitation)
excitation = paddle.reshape( excitation = paddle.reshape(excitation, shape=[-1, self._num_channels, 1, 1])
excitation, shape=[-1, self._num_channels, 1, 1])
out = x * excitation out = x * excitation
return out return out
...@@ -507,11 +437,7 @@ class HighResolutionModule(nn.Layer): ...@@ -507,11 +437,7 @@ class HighResolutionModule(nn.Layer):
super(HighResolutionModule, self).__init__() super(HighResolutionModule, self).__init__()
self.branches_func = Branches( self.branches_func = Branches(
num_blocks=num_blocks, num_blocks=num_blocks, in_channels=num_channels, out_channels=num_filters, has_se=has_se, name=name)
in_channels=num_channels,
out_channels=num_filters,
has_se=has_se,
name=name)
self.fuse_func = FuseLayers( self.fuse_func = FuseLayers(
in_channels=num_filters, in_channels=num_filters,
...@@ -557,8 +483,7 @@ class FuseLayers(nn.Layer): ...@@ -557,8 +483,7 @@ class FuseLayers(nn.Layer):
for k in range(i - j): for k in range(i - j):
if k == i - j - 1: if k == i - j - 1:
residual_func = self.add_sublayer( residual_func = self.add_sublayer(
"residual_{}_layer_{}_{}_{}".format( "residual_{}_layer_{}_{}_{}".format(name, i + 1, j + 1, k + 1),
name, i + 1, j + 1, k + 1),
L.ConvBN( L.ConvBN(
in_channels=pre_num_filters, in_channels=pre_num_filters,
out_channels=out_channels[i], out_channels=out_channels[i],
...@@ -569,8 +494,7 @@ class FuseLayers(nn.Layer): ...@@ -569,8 +494,7 @@ class FuseLayers(nn.Layer):
pre_num_filters = out_channels[i] pre_num_filters = out_channels[i]
else: else:
residual_func = self.add_sublayer( residual_func = self.add_sublayer(
"residual_{}_layer_{}_{}_{}".format( "residual_{}_layer_{}_{}_{}".format(name, i + 1, j + 1, k + 1),
name, i + 1, j + 1, k + 1),
L.ConvBNReLU( L.ConvBNReLU(
in_channels=pre_num_filters, in_channels=pre_num_filters,
out_channels=out_channels[j], out_channels=out_channels[j],
...@@ -592,11 +516,7 @@ class FuseLayers(nn.Layer): ...@@ -592,11 +516,7 @@ class FuseLayers(nn.Layer):
y = self.residual_func_list[residual_func_idx](x[j]) y = self.residual_func_list[residual_func_idx](x[j])
residual_func_idx += 1 residual_func_idx += 1
y = F.interpolate( y = F.interpolate(y, residual_shape, mode='bilinear', align_corners=self.align_corners)
y,
residual_shape,
mode='bilinear',
align_corners=self.align_corners)
residual = residual + y residual = residual + y
elif j < i: elif j < i:
y = x[j] y = x[j]
......
...@@ -30,8 +30,7 @@ def SyncBatchNorm(*args, **kwargs): ...@@ -30,8 +30,7 @@ def SyncBatchNorm(*args, **kwargs):
class ConvBNLayer(nn.Layer): class ConvBNLayer(nn.Layer):
"""Basic conv bn relu layer.""" """Basic conv bn relu layer."""
def __init__( def __init__(self,
self,
in_channels: int, in_channels: int,
out_channels: int, out_channels: int,
kernel_size: int, kernel_size: int,
...@@ -44,8 +43,7 @@ class ConvBNLayer(nn.Layer): ...@@ -44,8 +43,7 @@ class ConvBNLayer(nn.Layer):
super(ConvBNLayer, self).__init__() super(ConvBNLayer, self).__init__()
self.is_vd_mode = is_vd_mode self.is_vd_mode = is_vd_mode
self._pool2d_avg = AvgPool2D( self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True)
kernel_size=2, stride=2, padding=0, ceil_mode=True)
self._conv = Conv2D( self._conv = Conv2D(
in_channels=in_channels, in_channels=in_channels,
out_channels=out_channels, out_channels=out_channels,
...@@ -83,11 +81,7 @@ class BottleneckBlock(nn.Layer): ...@@ -83,11 +81,7 @@ class BottleneckBlock(nn.Layer):
super(BottleneckBlock, self).__init__() super(BottleneckBlock, self).__init__()
self.conv0 = ConvBNLayer( self.conv0 = ConvBNLayer(
in_channels=in_channels, in_channels=in_channels, out_channels=out_channels, kernel_size=1, act='relu', name=name + "_branch2a")
out_channels=out_channels,
kernel_size=1,
act='relu',
name=name + "_branch2a")
self.dilation = dilation self.dilation = dilation
...@@ -100,11 +94,7 @@ class BottleneckBlock(nn.Layer): ...@@ -100,11 +94,7 @@ class BottleneckBlock(nn.Layer):
dilation=dilation, dilation=dilation,
name=name + "_branch2b") name=name + "_branch2b")
self.conv2 = ConvBNLayer( self.conv2 = ConvBNLayer(
in_channels=out_channels, in_channels=out_channels, out_channels=out_channels * 4, kernel_size=1, act=None, name=name + "_branch2c")
out_channels=out_channels * 4,
kernel_size=1,
act=None,
name=name + "_branch2c")
if not shortcut: if not shortcut:
self.short = ConvBNLayer( self.short = ConvBNLayer(
...@@ -139,12 +129,7 @@ class BottleneckBlock(nn.Layer): ...@@ -139,12 +129,7 @@ class BottleneckBlock(nn.Layer):
class SeparableConvBNReLU(nn.Layer): class SeparableConvBNReLU(nn.Layer):
"""Depthwise Separable Convolution.""" """Depthwise Separable Convolution."""
def __init__(self, def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
super(SeparableConvBNReLU, self).__init__() super(SeparableConvBNReLU, self).__init__()
self.depthwise_conv = ConvBN( self.depthwise_conv = ConvBN(
in_channels, in_channels,
...@@ -153,8 +138,7 @@ class SeparableConvBNReLU(nn.Layer): ...@@ -153,8 +138,7 @@ class SeparableConvBNReLU(nn.Layer):
padding=padding, padding=padding,
groups=in_channels, groups=in_channels,
**kwargs) **kwargs)
self.piontwise_conv = ConvBNReLU( self.piontwise_conv = ConvBNReLU(in_channels, out_channels, kernel_size=1, groups=1)
in_channels, out_channels, kernel_size=1, groups=1)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
x = self.depthwise_conv(x) x = self.depthwise_conv(x)
...@@ -165,15 +149,9 @@ class SeparableConvBNReLU(nn.Layer): ...@@ -165,15 +149,9 @@ class SeparableConvBNReLU(nn.Layer):
class ConvBN(nn.Layer): class ConvBN(nn.Layer):
"""Basic conv bn layer""" """Basic conv bn layer"""
def __init__(self, def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
super(ConvBN, self).__init__() super(ConvBN, self).__init__()
self._conv = Conv2D( self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels) self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
...@@ -185,16 +163,10 @@ class ConvBN(nn.Layer): ...@@ -185,16 +163,10 @@ class ConvBN(nn.Layer):
class ConvBNReLU(nn.Layer): class ConvBNReLU(nn.Layer):
"""Basic conv bn relu layer.""" """Basic conv bn relu layer."""
def __init__(self, def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
super(ConvBNReLU, self).__init__() super(ConvBNReLU, self).__init__()
self._conv = Conv2D( self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels) self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
...@@ -251,8 +223,7 @@ class Activation(nn.Layer): ...@@ -251,8 +223,7 @@ class Activation(nn.Layer):
act_name = act_dict[act] act_name = act_dict[act]
self.act_func = eval("activation.{}()".format(act_name)) self.act_func = eval("activation.{}()".format(act_name))
else: else:
raise KeyError("{} does not exist in the current {}".format( raise KeyError("{} does not exist in the current {}".format(act, act_dict.keys()))
act, act_dict.keys()))
def forward(self, x: paddle.Tensor) -> paddle.Tensor: def forward(self, x: paddle.Tensor) -> paddle.Tensor:
...@@ -276,13 +247,7 @@ class ASPPModule(nn.Layer): ...@@ -276,13 +247,7 @@ class ASPPModule(nn.Layer):
image_pooling (bool, optional): If augmented with image-level features. Default: False image_pooling (bool, optional): If augmented with image-level features. Default: False
""" """
def __init__(self, def __init__(self, aspp_ratios, in_channels, out_channels, align_corners, use_sep_conv=False, image_pooling=False):
aspp_ratios,
in_channels,
out_channels,
align_corners,
use_sep_conv=False,
image_pooling=False):
super().__init__() super().__init__()
self.align_corners = align_corners self.align_corners = align_corners
...@@ -311,10 +276,7 @@ class ASPPModule(nn.Layer): ...@@ -311,10 +276,7 @@ class ASPPModule(nn.Layer):
out_size += 1 out_size += 1
self.image_pooling = image_pooling self.image_pooling = image_pooling
self.conv_bn_relu = ConvBNReLU( self.conv_bn_relu = ConvBNReLU(in_channels=out_channels * out_size, out_channels=out_channels, kernel_size=1)
in_channels=out_channels * out_size,
out_channels=out_channels,
kernel_size=1)
self.dropout = nn.Dropout(p=0.1) # drop rate self.dropout = nn.Dropout(p=0.1) # drop rate
...@@ -322,20 +284,12 @@ class ASPPModule(nn.Layer): ...@@ -322,20 +284,12 @@ class ASPPModule(nn.Layer):
outputs = [] outputs = []
for block in self.aspp_blocks: for block in self.aspp_blocks:
y = block(x) y = block(x)
y = F.interpolate( y = F.interpolate(y, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
y,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
outputs.append(y) outputs.append(y)
if self.image_pooling: if self.image_pooling:
img_avg = self.global_avg_pool(x) img_avg = self.global_avg_pool(x)
img_avg = F.interpolate( img_avg = F.interpolate(img_avg, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
img_avg,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
outputs.append(img_avg) outputs.append(img_avg)
x = paddle.concat(outputs, axis=1) x = paddle.concat(outputs, axis=1)
......
...@@ -25,6 +25,7 @@ from paddlehub.module.cv_module import ImageSegmentationModule ...@@ -25,6 +25,7 @@ from paddlehub.module.cv_module import ImageSegmentationModule
import ocrnet_hrnetw18_voc.layers as L import ocrnet_hrnetw18_voc.layers as L
from ocrnet_hrnetw18_voc.hrnet import HRNet_W18 from ocrnet_hrnetw18_voc.hrnet import HRNet_W18
@moduleinfo( @moduleinfo(
name="ocrnet_hrnetw18_voc", name="ocrnet_hrnetw18_voc",
type="CV/semantic_segmentation", type="CV/semantic_segmentation",
...@@ -90,11 +91,8 @@ class OCRNetHRNetW18(nn.Layer): ...@@ -90,11 +91,8 @@ class OCRNetHRNetW18(nn.Layer):
feats = [feats[i] for i in self.backbone_indices] feats = [feats[i] for i in self.backbone_indices]
logit_list = self.head(feats) logit_list = self.head(feats)
logit_list = [ logit_list = [
F.interpolate( F.interpolate(logit, x.shape[2:], mode='bilinear', align_corners=self.align_corners) for logit in logit_list
logit, ]
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners) for logit in logit_list]
return logit_list return logit_list
...@@ -108,32 +106,23 @@ class OCRHead(nn.Layer): ...@@ -108,32 +106,23 @@ class OCRHead(nn.Layer):
ocr_key_channels(int, optional): The number of key channels in ObjectAttentionBlock. Default: 256. ocr_key_channels(int, optional): The number of key channels in ObjectAttentionBlock. Default: 256.
""" """
def __init__(self, def __init__(self, num_classes: int, in_channels: int, ocr_mid_channels: int = 512, ocr_key_channels: int = 256):
num_classes: int,
in_channels: int,
ocr_mid_channels: int = 512,
ocr_key_channels: int = 256):
super().__init__() super().__init__()
self.num_classes = num_classes self.num_classes = num_classes
self.spatial_gather = SpatialGatherBlock() self.spatial_gather = SpatialGatherBlock()
self.spatial_ocr = SpatialOCRModule(ocr_mid_channels, ocr_key_channels, self.spatial_ocr = SpatialOCRModule(ocr_mid_channels, ocr_key_channels, ocr_mid_channels)
ocr_mid_channels)
self.indices = [-2, -1] if len(in_channels) > 1 else [-1, -1] self.indices = [-2, -1] if len(in_channels) > 1 else [-1, -1]
self.conv3x3_ocr = L.ConvBNReLU( self.conv3x3_ocr = L.ConvBNReLU(in_channels[self.indices[1]], ocr_mid_channels, 3, padding=1)
in_channels[self.indices[1]], ocr_mid_channels, 3, padding=1)
self.cls_head = nn.Conv2D(ocr_mid_channels, self.num_classes, 1) self.cls_head = nn.Conv2D(ocr_mid_channels, self.num_classes, 1)
self.aux_head = nn.Sequential( self.aux_head = nn.Sequential(
L.ConvBNReLU(in_channels[self.indices[0]], L.ConvBNReLU(in_channels[self.indices[0]], in_channels[self.indices[0]], 1),
in_channels[self.indices[0]], 1),
nn.Conv2D(in_channels[self.indices[0]], self.num_classes, 1)) nn.Conv2D(in_channels[self.indices[0]], self.num_classes, 1))
def forward(self, feat_list: List[paddle.Tensor]) -> paddle.Tensor: def forward(self, feat_list: List[paddle.Tensor]) -> paddle.Tensor:
feat_shallow, feat_deep = feat_list[self.indices[0]], feat_list[ feat_shallow, feat_deep = feat_list[self.indices[0]], feat_list[self.indices[1]]
self.indices[1]]
soft_regions = self.aux_head(feat_shallow) soft_regions = self.aux_head(feat_shallow)
pixels = self.conv3x3_ocr(feat_deep) pixels = self.conv3x3_ocr(feat_deep)
...@@ -171,17 +160,11 @@ class SpatialGatherBlock(nn.Layer): ...@@ -171,17 +160,11 @@ class SpatialGatherBlock(nn.Layer):
class SpatialOCRModule(nn.Layer): class SpatialOCRModule(nn.Layer):
"""Aggregate the global object representation to update the representation for each pixel.""" """Aggregate the global object representation to update the representation for each pixel."""
def __init__(self, def __init__(self, in_channels: int, key_channels: int, out_channels: int, dropout_rate: float = 0.1):
in_channels: int,
key_channels: int,
out_channels: int,
dropout_rate: float = 0.1):
super().__init__() super().__init__()
self.attention_block = ObjectAttentionBlock(in_channels, key_channels) self.attention_block = ObjectAttentionBlock(in_channels, key_channels)
self.conv1x1 = nn.Sequential( self.conv1x1 = nn.Sequential(L.ConvBNReLU(2 * in_channels, out_channels, 1), nn.Dropout2D(dropout_rate))
L.ConvBNReLU(2 * in_channels, out_channels, 1),
nn.Dropout2D(dropout_rate))
def forward(self, pixels: paddle.Tensor, regions: paddle.Tensor) -> paddle.Tensor: def forward(self, pixels: paddle.Tensor, regions: paddle.Tensor) -> paddle.Tensor:
context = self.attention_block(pixels, regions) context = self.attention_block(pixels, regions)
...@@ -201,12 +184,10 @@ class ObjectAttentionBlock(nn.Layer): ...@@ -201,12 +184,10 @@ class ObjectAttentionBlock(nn.Layer):
self.key_channels = key_channels self.key_channels = key_channels
self.f_pixel = nn.Sequential( self.f_pixel = nn.Sequential(
L.ConvBNReLU(in_channels, key_channels, 1), L.ConvBNReLU(in_channels, key_channels, 1), L.ConvBNReLU(key_channels, key_channels, 1))
L.ConvBNReLU(key_channels, key_channels, 1))
self.f_object = nn.Sequential( self.f_object = nn.Sequential(
L.ConvBNReLU(in_channels, key_channels, 1), L.ConvBNReLU(in_channels, key_channels, 1), L.ConvBNReLU(key_channels, key_channels, 1))
L.ConvBNReLU(key_channels, key_channels, 1))
self.f_down = L.ConvBNReLU(in_channels, key_channels, 1) self.f_down = L.ConvBNReLU(in_channels, key_channels, 1)
......
...@@ -60,29 +60,27 @@ class Bert(nn.Layer): ...@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained(pretrained_model_name_or_path='bert-base-cased', num_classes=self.num_classes, **kwargs) self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-cased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(pretrained_model_name_or_path='bert-base-cased', num_classes=self.num_classes, **kwargs) self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-cased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-cased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-cased', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-cased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-cased', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -130,8 +128,7 @@ class Bert(nn.Layer): ...@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -173,5 +170,4 @@ class Bert(nn.Layer): ...@@ -173,5 +170,4 @@ class Bert(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return BertTokenizer.from_pretrained( return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-cased', *args, **kwargs)
pretrained_model_name_or_path='bert-base-cased', *args, **kwargs)
...@@ -60,37 +60,27 @@ class Bert(nn.Layer): ...@@ -60,37 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained( self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-chinese', pretrained_model_name_or_path='bert-base-chinese', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained( self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-chinese', pretrained_model_name_or_path='bert-base-chinese', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +128,7 @@ class Bert(nn.Layer): ...@@ -138,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +170,4 @@ class Bert(nn.Layer): ...@@ -181,5 +170,4 @@ class Bert(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return BertTokenizer.from_pretrained( return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', *args, **kwargs)
pretrained_model_name_or_path='bert-base-chinese', *args, **kwargs)
...@@ -60,37 +60,29 @@ class Bert(nn.Layer): ...@@ -60,37 +60,29 @@ class Bert(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained( self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-cased', pretrained_model_name_or_path='bert-base-multilingual-cased', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained( self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-cased', pretrained_model_name_or_path='bert-base-multilingual-cased', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-multilingual-cased', **kwargs) self.model = BertModel.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-cased', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-multilingual-cased', **kwargs) self.model = BertModel.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-cased', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +130,7 @@ class Bert(nn.Layer): ...@@ -138,8 +130,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
......
...@@ -60,37 +60,29 @@ class Bert(nn.Layer): ...@@ -60,37 +60,29 @@ class Bert(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained( self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-uncased', pretrained_model_name_or_path='bert-base-multilingual-uncased', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained( self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-uncased', pretrained_model_name_or_path='bert-base-multilingual-uncased', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-multilingual-uncased', **kwargs) self.model = BertModel.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-uncased', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-multilingual-uncased', **kwargs) self.model = BertModel.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-uncased', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +130,7 @@ class Bert(nn.Layer): ...@@ -138,8 +130,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
......
...@@ -60,29 +60,27 @@ class Bert(nn.Layer): ...@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', num_classes=self.num_classes, **kwargs) self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-uncased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', num_classes=self.num_classes, **kwargs) self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-uncased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -130,8 +128,7 @@ class Bert(nn.Layer): ...@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -173,5 +170,4 @@ class Bert(nn.Layer): ...@@ -173,5 +170,4 @@ class Bert(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return BertTokenizer.from_pretrained( return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', *args, **kwargs)
pretrained_model_name_or_path='bert-base-uncased', *args, **kwargs)
...@@ -60,29 +60,27 @@ class Bert(nn.Layer): ...@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained(pretrained_model_name_or_path='bert-large-cased', num_classes=self.num_classes, **kwargs) self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-large-cased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(pretrained_model_name_or_path='bert-large-cased', num_classes=self.num_classes, **kwargs) self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-large-cased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-cased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-cased', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-cased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-cased', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -130,8 +128,7 @@ class Bert(nn.Layer): ...@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -173,5 +170,4 @@ class Bert(nn.Layer): ...@@ -173,5 +170,4 @@ class Bert(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return BertTokenizer.from_pretrained( return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-large-cased', *args, **kwargs)
pretrained_model_name_or_path='bert-large-cased', *args, **kwargs)
...@@ -60,29 +60,27 @@ class Bert(nn.Layer): ...@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', num_classes=self.num_classes, **kwargs) self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-large-uncased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', num_classes=self.num_classes, **kwargs) self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-large-uncased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -130,8 +128,7 @@ class Bert(nn.Layer): ...@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -173,5 +170,4 @@ class Bert(nn.Layer): ...@@ -173,5 +170,4 @@ class Bert(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return BertTokenizer.from_pretrained( return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', *args, **kwargs)
pretrained_model_name_or_path='bert-large-uncased', *args, **kwargs)
...@@ -35,8 +35,7 @@ from paddlehub.utils.log import logger ...@@ -35,8 +35,7 @@ from paddlehub.utils.log import logger
author="ymcui", author="ymcui",
author_email="ymcui@ir.hit.edu.cn", author_email="ymcui@ir.hit.edu.cn",
type="nlp/semantic_model", type="nlp/semantic_model",
meta=TransformerModule meta=TransformerModule)
)
class BertWwm(nn.Layer): class BertWwm(nn.Layer):
""" """
BertWwm model BertWwm model
...@@ -61,37 +60,27 @@ class BertWwm(nn.Layer): ...@@ -61,37 +60,27 @@ class BertWwm(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained( self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-chinese', pretrained_model_name_or_path='bert-wwm-chinese', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained( self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-chinese', pretrained_model_name_or_path='bert-wwm-chinese', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -139,8 +128,7 @@ class BertWwm(nn.Layer): ...@@ -139,8 +128,7 @@ class BertWwm(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -182,5 +170,4 @@ class BertWwm(nn.Layer): ...@@ -182,5 +170,4 @@ class BertWwm(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return BertTokenizer.from_pretrained( return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', *args, **kwargs)
pretrained_model_name_or_path='bert-wwm-chinese', *args, **kwargs)
...@@ -35,8 +35,7 @@ from paddlehub.utils.log import logger ...@@ -35,8 +35,7 @@ from paddlehub.utils.log import logger
author="ymcui", author="ymcui",
author_email="ymcui@ir.hit.edu.cn", author_email="ymcui@ir.hit.edu.cn",
type="nlp/semantic_model", type="nlp/semantic_model",
meta=TransformerModule meta=TransformerModule)
)
class BertWwm(nn.Layer): class BertWwm(nn.Layer):
""" """
BertWwm model BertWwm model
...@@ -61,37 +60,27 @@ class BertWwm(nn.Layer): ...@@ -61,37 +60,27 @@ class BertWwm(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = BertForSequenceClassification.from_pretrained( self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-ext-chinese', pretrained_model_name_or_path='bert-wwm-ext-chinese', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained( self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-ext-chinese', pretrained_model_name_or_path='bert-wwm-ext-chinese', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', **kwargs) self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -139,8 +128,7 @@ class BertWwm(nn.Layer): ...@@ -139,8 +128,7 @@ class BertWwm(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -182,5 +170,4 @@ class BertWwm(nn.Layer): ...@@ -182,5 +170,4 @@ class BertWwm(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return BertTokenizer.from_pretrained( return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', *args, **kwargs)
pretrained_model_name_or_path='bert-wwm-ext-chinese', *args, **kwargs)
...@@ -60,37 +60,27 @@ class Electra(nn.Layer): ...@@ -60,37 +60,27 @@ class Electra(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ElectraForSequenceClassification.from_pretrained( self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-base', pretrained_model_name_or_path='chinese-electra-base', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained( self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-base', pretrained_model_name_or_path='chinese-electra-base', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +128,7 @@ class Electra(nn.Layer): ...@@ -138,8 +128,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +170,4 @@ class Electra(nn.Layer): ...@@ -181,5 +170,4 @@ class Electra(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ElectraTokenizer.from_pretrained( return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', *args, **kwargs)
pretrained_model_name_or_path='chinese-electra-base', *args, **kwargs)
...@@ -60,37 +60,27 @@ class Electra(nn.Layer): ...@@ -60,37 +60,27 @@ class Electra(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ElectraForSequenceClassification.from_pretrained( self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-small', pretrained_model_name_or_path='chinese-electra-small', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained( self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-small', pretrained_model_name_or_path='chinese-electra-small', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +128,7 @@ class Electra(nn.Layer): ...@@ -138,8 +128,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +170,4 @@ class Electra(nn.Layer): ...@@ -181,5 +170,4 @@ class Electra(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ElectraTokenizer.from_pretrained( return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', *args, **kwargs)
pretrained_model_name_or_path='chinese-electra-small', *args, **kwargs)
...@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger ...@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger
@moduleinfo( @moduleinfo(
name="electra-base", name="electra-base",
version="1.0.1", version="1.0.1",
summary= summary="electra-base, 12-layer, 768-hidden, 12-heads, 110M parameters. The module is executed as paddle.dygraph.",
"electra-base, 12-layer, 768-hidden, 12-heads, 110M parameters. The module is executed as paddle.dygraph.",
author="paddlepaddle", author="paddlepaddle",
author_email="", author_email="",
type="nlp/semantic_model", type="nlp/semantic_model",
...@@ -60,37 +59,27 @@ class Electra(nn.Layer): ...@@ -60,37 +59,27 @@ class Electra(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ElectraForSequenceClassification.from_pretrained( self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='electra-base', pretrained_model_name_or_path='electra-base', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained( self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='electra-base', pretrained_model_name_or_path='electra-base', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-base', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-base', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-base', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-base', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +127,7 @@ class Electra(nn.Layer): ...@@ -138,8 +127,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +169,4 @@ class Electra(nn.Layer): ...@@ -181,5 +169,4 @@ class Electra(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ElectraTokenizer.from_pretrained( return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='electra-base', *args, **kwargs)
pretrained_model_name_or_path='electra-base', *args, **kwargs)
...@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger ...@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger
@moduleinfo( @moduleinfo(
name="electra-large", name="electra-large",
version="1.0.1", version="1.0.1",
summary= summary="electra-large, 24-layer, 1024-hidden, 16-heads, 335M parameters. The module is executed as paddle.dygraph.",
"electra-large, 24-layer, 1024-hidden, 16-heads, 335M parameters. The module is executed as paddle.dygraph.",
author="paddlepaddle", author="paddlepaddle",
author_email="", author_email="",
type="nlp/semantic_model", type="nlp/semantic_model",
...@@ -60,37 +59,27 @@ class Electra(nn.Layer): ...@@ -60,37 +59,27 @@ class Electra(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ElectraForSequenceClassification.from_pretrained( self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='electra-large', pretrained_model_name_or_path='electra-large', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained( self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='electra-large', pretrained_model_name_or_path='electra-large', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-large', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-large', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-large', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-large', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +127,7 @@ class Electra(nn.Layer): ...@@ -138,8 +127,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +169,4 @@ class Electra(nn.Layer): ...@@ -181,5 +169,4 @@ class Electra(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ElectraTokenizer.from_pretrained( return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='electra-large', *args, **kwargs)
pretrained_model_name_or_path='electra-large', *args, **kwargs)
...@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger ...@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger
@moduleinfo( @moduleinfo(
name="electra-small", name="electra-small",
version="1.0.1", version="1.0.1",
summary= summary="electra-small, 12-layer, 256-hidden, 4-heads, 14M parameters. The module is executed as paddle.dygraph.",
"electra-small, 12-layer, 256-hidden, 4-heads, 14M parameters. The module is executed as paddle.dygraph.",
author="paddlepaddle", author="paddlepaddle",
author_email="", author_email="",
type="nlp/semantic_model", type="nlp/semantic_model",
...@@ -60,37 +59,27 @@ class Electra(nn.Layer): ...@@ -60,37 +59,27 @@ class Electra(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ElectraForSequenceClassification.from_pretrained( self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='electra-small', pretrained_model_name_or_path='electra-small', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained( self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='electra-small', pretrained_model_name_or_path='electra-small', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-small', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-small', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-small', **kwargs) self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-small', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +127,7 @@ class Electra(nn.Layer): ...@@ -138,8 +127,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +169,4 @@ class Electra(nn.Layer): ...@@ -181,5 +169,4 @@ class Electra(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ElectraTokenizer.from_pretrained( return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='electra-small', *args, **kwargs)
pretrained_model_name_or_path='electra-small', *args, **kwargs)
...@@ -60,29 +60,27 @@ class Ernie(nn.Layer): ...@@ -60,29 +60,27 @@ class Ernie(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ErnieForSequenceClassification.from_pretrained(pretrained_model_name_or_path='ernie-1.0', num_classes=self.num_classes, **kwargs) self.model = ErnieForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='ernie-1.0', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ErnieForTokenClassification.from_pretrained(pretrained_model_name_or_path='ernie-1.0', num_classes=self.num_classes, **kwargs) self.model = ErnieForTokenClassification.from_pretrained(
pretrained_model_name_or_path='ernie-1.0', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-1.0', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-1.0', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-1.0', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-1.0', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -130,8 +128,7 @@ class Ernie(nn.Layer): ...@@ -130,8 +128,7 @@ class Ernie(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -173,5 +170,4 @@ class Ernie(nn.Layer): ...@@ -173,5 +170,4 @@ class Ernie(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ErnieTokenizer.from_pretrained( return ErnieTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-1.0', *args, **kwargs)
pretrained_model_name_or_path='ernie-1.0', *args, **kwargs)
...@@ -59,29 +59,27 @@ class ErnieTiny(nn.Layer): ...@@ -59,29 +59,27 @@ class ErnieTiny(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ErnieForSequenceClassification.from_pretrained(pretrained_model_name_or_path='ernie-tiny', num_classes=self.num_classes, **kwargs) self.model = ErnieForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='ernie-tiny', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ErnieForTokenClassification.from_pretrained(pretrained_model_name_or_path='ernie-tiny', num_classes=self.num_classes, **kwargs) self.model = ErnieForTokenClassification.from_pretrained(
pretrained_model_name_or_path='ernie-tiny', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-tiny', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-tiny', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-tiny', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-tiny', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -129,8 +127,7 @@ class ErnieTiny(nn.Layer): ...@@ -129,8 +127,7 @@ class ErnieTiny(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -172,5 +169,4 @@ class ErnieTiny(nn.Layer): ...@@ -172,5 +169,4 @@ class ErnieTiny(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ErnieTinyTokenizer.from_pretrained( return ErnieTinyTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-tiny', *args, **kwargs)
pretrained_model_name_or_path='ernie-tiny', *args, **kwargs)
...@@ -60,37 +60,27 @@ class ErnieV2(nn.Layer): ...@@ -60,37 +60,27 @@ class ErnieV2(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ErnieForSequenceClassification.from_pretrained( self.model = ErnieForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-en', pretrained_model_name_or_path='ernie-2.0-en', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ErnieForTokenClassification.from_pretrained( self.model = ErnieForTokenClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-en', pretrained_model_name_or_path='ernie-2.0-en', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +128,7 @@ class ErnieV2(nn.Layer): ...@@ -138,8 +128,7 @@ class ErnieV2(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +170,4 @@ class ErnieV2(nn.Layer): ...@@ -181,5 +170,4 @@ class ErnieV2(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ErnieTokenizer.from_pretrained( return ErnieTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', *args, **kwargs)
pretrained_model_name_or_path='ernie-2.0-en', *args, **kwargs)
\ No newline at end of file
...@@ -60,37 +60,27 @@ class ErnieV2(nn.Layer): ...@@ -60,37 +60,27 @@ class ErnieV2(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = ErnieForSequenceClassification.from_pretrained( self.model = ErnieForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-large-en', pretrained_model_name_or_path='ernie-2.0-large-en', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = ErnieForTokenClassification.from_pretrained( self.model = ErnieForTokenClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-large-en', pretrained_model_name_or_path='ernie-2.0-large-en', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', **kwargs) self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +128,7 @@ class ErnieV2(nn.Layer): ...@@ -138,8 +128,7 @@ class ErnieV2(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +170,4 @@ class ErnieV2(nn.Layer): ...@@ -181,5 +170,4 @@ class ErnieV2(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return ErnieTokenizer.from_pretrained( return ErnieTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', *args, **kwargs)
pretrained_model_name_or_path='ernie-2.0-large-en', *args, **kwargs)
...@@ -60,37 +60,27 @@ class Roberta(nn.Layer): ...@@ -60,37 +60,27 @@ class Roberta(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = RobertaForSequenceClassification.from_pretrained( self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='rbt3', pretrained_model_name_or_path='rbt3', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained( self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='rbt3', pretrained_model_name_or_path='rbt3', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbt3', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbt3', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbt3', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbt3', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +128,7 @@ class Roberta(nn.Layer): ...@@ -138,8 +128,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +170,4 @@ class Roberta(nn.Layer): ...@@ -181,5 +170,4 @@ class Roberta(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return RobertaTokenizer.from_pretrained( return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='rbt3', *args, **kwargs)
pretrained_model_name_or_path='rbt3', *args, **kwargs)
...@@ -60,37 +60,27 @@ class Roberta(nn.Layer): ...@@ -60,37 +60,27 @@ class Roberta(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = RobertaForSequenceClassification.from_pretrained( self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='rbtl3', pretrained_model_name_or_path='rbtl3', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained( self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='rbtl3', pretrained_model_name_or_path='rbtl3', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbtl3', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbtl3', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbtl3', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbtl3', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -138,8 +128,7 @@ class Roberta(nn.Layer): ...@@ -138,8 +128,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -181,5 +170,4 @@ class Roberta(nn.Layer): ...@@ -181,5 +170,4 @@ class Roberta(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return RobertaTokenizer.from_pretrained( return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='rbtl3', *args, **kwargs)
pretrained_model_name_or_path='rbtl3', *args, **kwargs)
...@@ -61,37 +61,27 @@ class Roberta(nn.Layer): ...@@ -61,37 +61,27 @@ class Roberta(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = RobertaForSequenceClassification.from_pretrained( self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext-large', pretrained_model_name_or_path='roberta-wwm-ext-large', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained( self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext-large', pretrained_model_name_or_path='roberta-wwm-ext-large', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -139,8 +129,7 @@ class Roberta(nn.Layer): ...@@ -139,8 +129,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -182,5 +171,4 @@ class Roberta(nn.Layer): ...@@ -182,5 +171,4 @@ class Roberta(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return RobertaTokenizer.from_pretrained( return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', *args, **kwargs)
pretrained_model_name_or_path='roberta-wwm-ext-large', *args, **kwargs)
...@@ -61,37 +61,27 @@ class Roberta(nn.Layer): ...@@ -61,37 +61,27 @@ class Roberta(nn.Layer):
task = 'seq-cls' task = 'seq-cls'
logger.warning( logger.warning(
"current task name 'sequence_classification' was renamed to 'seq-cls', " "current task name 'sequence_classification' was renamed to 'seq-cls', "
"'sequence_classification' has been deprecated and will be removed in the future.", "'sequence_classification' has been deprecated and will be removed in the future.", )
)
if task == 'seq-cls': if task == 'seq-cls':
self.model = RobertaForSequenceClassification.from_pretrained( self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext', pretrained_model_name_or_path='roberta-wwm-ext', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task == 'token-cls': elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained( self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext', pretrained_model_name_or_path='roberta-wwm-ext', num_classes=self.num_classes, **kwargs)
num_classes=self.num_classes,
**kwargs
)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator( self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
elif task == 'text-matching': elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', **kwargs)
self.dropout = paddle.nn.Dropout(0.1) self.dropout = paddle.nn.Dropout(0.1)
self.classifier = paddle.nn.Linear(self.model.config['hidden_size']*3, 2) self.classifier = paddle.nn.Linear(self.model.config['hidden_size'] * 3, 2)
self.criterion = paddle.nn.loss.CrossEntropyLoss() self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy() self.metric = paddle.metric.Accuracy()
elif task is None: elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', **kwargs) self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', **kwargs)
else: else:
raise RuntimeError("Unknown task {}, task should be one in {}".format( raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
task, self._tasks_supported))
self.task = task self.task = task
...@@ -139,8 +129,7 @@ class Roberta(nn.Layer): ...@@ -139,8 +129,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1)) loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \ num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels) self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update( self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate()) _, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score} return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs return token_level_probs
...@@ -182,5 +171,4 @@ class Roberta(nn.Layer): ...@@ -182,5 +171,4 @@ class Roberta(nn.Layer):
""" """
Gets the tokenizer that is customized for this module. Gets the tokenizer that is customized for this module.
""" """
return RobertaTokenizer.from_pretrained( return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', *args, **kwargs)
pretrained_model_name_or_path='roberta-wwm-ext', *args, **kwargs)
...@@ -29,7 +29,8 @@ class DataFormatError(Exception): ...@@ -29,7 +29,8 @@ class DataFormatError(Exception):
self.args = args self.args = args
@moduleinfo(name="simnet_bow", @moduleinfo(
name="simnet_bow",
version="1.2.0", version="1.2.0",
summary="Baidu's open-source similarity network model based on bow_pairwise.", summary="Baidu's open-source similarity network model based on bow_pairwise.",
author="baidu-nlp", author="baidu-nlp",
...@@ -106,11 +107,11 @@ class SimnetBow(hub.Module): ...@@ -106,11 +107,11 @@ class SimnetBow(hub.Module):
seq_len_used = fluid.layers.squeeze(seq_len, axes=[1]) seq_len_used = fluid.layers.squeeze(seq_len, axes=[1])
# Add embedding layer. # Add embedding layer.
w_param_attrs = fluid.ParamAttr(name="emb", w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.TruncatedNormal(scale=0.02), name="emb", initializer=fluid.initializer.TruncatedNormal(scale=0.02), trainable=trainable)
trainable=trainable)
dict_dim = 500002 dict_dim = 500002
emb_1 = fluid.layers.embedding(input=text_1, emb_1 = fluid.layers.embedding(
input=text_1,
size=[dict_dim, 128], size=[dict_dim, 128],
is_sparse=True, is_sparse=True,
padding_idx=dict_dim - 1, padding_idx=dict_dim - 1,
...@@ -122,7 +123,8 @@ class SimnetBow(hub.Module): ...@@ -122,7 +123,8 @@ class SimnetBow(hub.Module):
if num_slots > 1: if num_slots > 1:
text_2 = fluid.data(name='text_2', shape=[-1, max_seq_len], dtype='int64', lod_level=0) text_2 = fluid.data(name='text_2', shape=[-1, max_seq_len], dtype='int64', lod_level=0)
emb_2 = fluid.embedding(input=text_2, emb_2 = fluid.embedding(
input=text_2,
size=[dict_dim, 128], size=[dict_dim, 128],
is_sparse=True, is_sparse=True,
padding_idx=dict_dim - 1, padding_idx=dict_dim - 1,
...@@ -134,7 +136,8 @@ class SimnetBow(hub.Module): ...@@ -134,7 +136,8 @@ class SimnetBow(hub.Module):
if num_slots > 2: if num_slots > 2:
text_3 = fluid.data(name='text_3', shape=[-1, max_seq_len], dtype='int64', lod_level=0) text_3 = fluid.data(name='text_3', shape=[-1, max_seq_len], dtype='int64', lod_level=0)
emb_3 = fluid.embedding(input=text_3, emb_3 = fluid.embedding(
input=text_3,
size=[dict_dim, 128], size=[dict_dim, 128],
is_sparse=True, is_sparse=True,
padding_idx=dict_dim - 1, padding_idx=dict_dim - 1,
...@@ -295,10 +298,8 @@ class SimnetBow(hub.Module): ...@@ -295,10 +298,8 @@ class SimnetBow(hub.Module):
""" """
Run as a command Run as a command
""" """
self.parser = argparse.ArgumentParser(description="Run the simnet_bow module.", self.parser = argparse.ArgumentParser(
prog='hub run simnet_bow', description="Run the simnet_bow module.", prog='hub run simnet_bow', usage='%(prog)s', add_help=True)
usage='%(prog)s',
add_help=True)
self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required") self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
...@@ -323,10 +324,8 @@ class SimnetBow(hub.Module): ...@@ -323,10 +324,8 @@ class SimnetBow(hub.Module):
""" """
Add the command config options Add the command config options
""" """
self.arg_config_group.add_argument('--use_gpu', self.arg_config_group.add_argument(
type=ast.literal_eval, '--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction")
default=False,
help="whether use GPU for prediction")
self.arg_config_group.add_argument('--batch_size', type=int, default=1, help="batch size for prediction") self.arg_config_group.add_argument('--batch_size', type=int, default=1, help="batch size for prediction")
......
...@@ -27,12 +27,12 @@ import numpy as np ...@@ -27,12 +27,12 @@ import numpy as np
import paddle import paddle
import paddlehub as hub import paddlehub as hub
@moduleinfo( @moduleinfo(
name="Rumor_prediction", name="Rumor_prediction",
version="1.0.0", version="1.0.0",
type="nlp/semantic_model", type="nlp/semantic_model",
summary= summary="Is the input text prediction a rumor",
"Is the input text prediction a rumor",
author="彭兆帅,郑博培", author="彭兆帅,郑博培",
author_email="1084667371@qq.com,2733821739@qq.com") author_email="1084667371@qq.com,2733821739@qq.com")
class Rumorprediction(hub.Module): class Rumorprediction(hub.Module):
...@@ -51,6 +51,7 @@ class Rumorprediction(hub.Module): ...@@ -51,6 +51,7 @@ class Rumorprediction(hub.Module):
image (list(numpy.ndarray)): images data, shape of each is [H, W, C], the color space is BGR. image (list(numpy.ndarray)): images data, shape of each is [H, W, C], the color space is BGR.
use_gpu(bool): Weather to use gpu use_gpu(bool): Weather to use gpu
""" """
# 获取数据 # 获取数据
def get_data(sentence): def get_data(sentence):
# 读取数据字典 # 读取数据字典
...@@ -66,6 +67,7 @@ class Rumorprediction(hub.Module): ...@@ -66,6 +67,7 @@ class Rumorprediction(hub.Module):
s = '<unk>' s = '<unk>'
data.append(int(dict_txt[s])) data.append(int(dict_txt[s]))
return data return data
data = [] data = []
for text in texts: for text in texts:
text = get_data(text) text = get_data(text)
...@@ -75,16 +77,14 @@ class Rumorprediction(hub.Module): ...@@ -75,16 +77,14 @@ class Rumorprediction(hub.Module):
place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace() place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place) exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program()) exe.run(paddle.static.default_startup_program())
[infer_program, feeded_var_names, target_var] = paddle.fluid.io.load_inference_model(dirname=self.default_pretrained_model_path, executor=exe) [infer_program, feeded_var_names, target_var] = paddle.fluid.io.load_inference_model(
dirname=self.default_pretrained_model_path, executor=exe)
# 生成预测数据 # 生成预测数据
tensor_words = paddle.fluid.create_lod_tensor(data, base_shape, place) tensor_words = paddle.fluid.create_lod_tensor(data, base_shape, place)
# 执行预测 # 执行预测
result = exe.run(program=infer_program, result = exe.run(program=infer_program, feed={feeded_var_names[0]: tensor_words}, fetch_list=target_var)
feed={feeded_var_names[0]: tensor_words},
fetch_list=target_var)
# 分类名称 # 分类名称
names = [ '谣言', '非谣言'] names = ['谣言', '非谣言']
results = [] results = []
...@@ -93,36 +93,25 @@ class Rumorprediction(hub.Module): ...@@ -93,36 +93,25 @@ class Rumorprediction(hub.Module):
content = texts[i] content = texts[i]
lab = np.argsort(result)[0][i][-1] lab = np.argsort(result)[0][i][-1]
alltext = { alltext = {'content': content, 'prediction': names[lab], 'probability': result[0][i][lab]}
'content': content,
'prediction': names[lab],
'probability': result[0][i][lab]
}
alltext = [alltext] alltext = [alltext]
results = results + alltext results = results + alltext
return results return results
def add_module_config_arg(self): def add_module_config_arg(self):
""" """
Add the command config options Add the command config options
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--use_gpu', '--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction")
type=ast.literal_eval,
default=False,
help="whether use GPU for prediction")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options Add the command input options
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_text', type=str, default=None, help="input_text is str")
'--input_text',
type=str,
default=None,
help="input_text is str")
@runnable @runnable
def run_cmd(self, argvs): def run_cmd(self, argvs):
""" """
...@@ -134,19 +123,15 @@ class Rumorprediction(hub.Module): ...@@ -134,19 +123,15 @@ class Rumorprediction(hub.Module):
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, optional.")
description=
"Run configuration for controlling module behavior, optional.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
input_text = [args.input_text] input_text = [args.input_text]
results = self.Rumor( results = self.Rumor(texts=input_text, use_gpu=args.use_gpu)
texts=input_text, use_gpu=args.use_gpu)
return results return results
...@@ -23,23 +23,14 @@ import paddle.fluid.dygraph as D ...@@ -23,23 +23,14 @@ import paddle.fluid.dygraph as D
def gen_bias(encoder_inputs, decoder_inputs, step): def gen_bias(encoder_inputs, decoder_inputs, step):
decoder_bsz, decoder_seqlen = decoder_inputs.shape[:2] decoder_bsz, decoder_seqlen = decoder_inputs.shape[:2]
attn_bias = L.reshape( attn_bias = L.reshape(L.range(0, decoder_seqlen, 1, dtype='float32') + 1, [1, -1, 1])
L.range(0, decoder_seqlen, 1, dtype='float32') + 1, [1, -1, 1]) decoder_bias = L.cast((L.matmul(attn_bias, 1. / attn_bias, transpose_y=True) >= 1.),
decoder_bias = L.cast(
(L.matmul(attn_bias, 1. / attn_bias, transpose_y=True) >= 1.),
'float32') #[1, 1, decoderlen, decoderlen] 'float32') #[1, 1, decoderlen, decoderlen]
encoder_bias = L.unsqueeze( encoder_bias = L.unsqueeze(L.cast(L.ones_like(encoder_inputs), 'float32'), [1]) #[bsz, 1, encoderlen]
L.cast(L.ones_like(encoder_inputs), 'float32'), encoder_bias = L.expand(encoder_bias, [1, decoder_seqlen, 1]) #[bsz,decoderlen, encoderlen]
[1]) #[bsz, 1, encoderlen] decoder_bias = L.expand(decoder_bias, [decoder_bsz, 1, 1]) #[bsz, decoderlen, decoderlen]
encoder_bias = L.expand(
encoder_bias, [1, decoder_seqlen, 1]) #[bsz,decoderlen, encoderlen]
decoder_bias = L.expand(decoder_bias,
[decoder_bsz, 1, 1]) #[bsz, decoderlen, decoderlen]
if step > 0: if step > 0:
bias = L.concat([ bias = L.concat([encoder_bias, L.ones([decoder_bsz, decoder_seqlen, step], 'float32'), decoder_bias], -1)
encoder_bias,
L.ones([decoder_bsz, decoder_seqlen, step], 'float32'), decoder_bias
], -1)
else: else:
bias = L.concat([encoder_bias, decoder_bias], -1) bias = L.concat([encoder_bias, decoder_bias], -1)
return bias return bias
...@@ -71,27 +62,16 @@ def greedy_search_infilling(model, ...@@ -71,27 +62,16 @@ def greedy_search_infilling(model,
ids = L.stack([cls_ids, attn_ids], -1) ids = L.stack([cls_ids, attn_ids], -1)
for step in range(max_decode_len): for step in range(max_decode_len):
bias = gen_bias(q_ids, ids, step) bias = gen_bias(q_ids, ids, step)
pos_ids = D.to_variable( pos_ids = D.to_variable(np.tile(np.array([[step, step + 1]], dtype=np.int64), [d_batch, 1]))
np.tile(np.array([[step, step + 1]], dtype=np.int64), [d_batch, 1]))
pos_ids += seqlen pos_ids += seqlen
_, logits, info = model( _, logits, info = model(
ids, ids, L.ones_like(ids) * tgt_type_id, pos_ids=pos_ids, attn_bias=bias, past_cache=past_cache)
L.ones_like(ids) * tgt_type_id,
pos_ids=pos_ids,
attn_bias=bias,
past_cache=past_cache)
gen_ids = L.argmax(logits, -1) gen_ids = L.argmax(logits, -1)
past_cached_k, past_cached_v = past_cache past_cached_k, past_cached_v = past_cache
cached_k, cached_v = info['caches'] cached_k, cached_v = info['caches']
cached_k = [ cached_k = [L.concat([pk, k[:, :1, :]], 1) for pk, k in zip(past_cached_k, cached_k)] # concat cached
L.concat([pk, k[:, :1, :]], 1) cached_v = [L.concat([pv, v[:, :1, :]], 1) for pv, v in zip(past_cached_v, cached_v)]
for pk, k in zip(past_cached_k, cached_k)
] # concat cached
cached_v = [
L.concat([pv, v[:, :1, :]], 1)
for pv, v in zip(past_cached_v, cached_v)
]
past_cache = (cached_k, cached_v) past_cache = (cached_k, cached_v)
gen_ids = gen_ids[:, 1] gen_ids = gen_ids[:, 1]
...@@ -107,10 +87,8 @@ def greedy_search_infilling(model, ...@@ -107,10 +87,8 @@ def greedy_search_infilling(model,
return output_ids return output_ids
BeamSearchState = namedtuple('BeamSearchState', BeamSearchState = namedtuple('BeamSearchState', ['log_probs', 'lengths', 'finished'])
['log_probs', 'lengths', 'finished']) BeamSearchOutput = namedtuple('BeamSearchOutput', ['scores', 'predicted_ids', 'beam_parent_ids'])
BeamSearchOutput = namedtuple('BeamSearchOutput',
['scores', 'predicted_ids', 'beam_parent_ids'])
def log_softmax(x): def log_softmax(x):
...@@ -120,8 +98,7 @@ def log_softmax(x): ...@@ -120,8 +98,7 @@ def log_softmax(x):
def mask_prob(p, onehot_eos, finished): def mask_prob(p, onehot_eos, finished):
is_finished = L.cast(L.reshape(finished, [-1, 1]) != 0, 'float32') is_finished = L.cast(L.reshape(finished, [-1, 1]) != 0, 'float32')
p = is_finished * (1. - L.cast(onehot_eos, 'float32')) * -9999. + ( p = is_finished * (1. - L.cast(onehot_eos, 'float32')) * -9999. + (1. - is_finished) * p
1. - is_finished) * p
return p return p
...@@ -130,8 +107,7 @@ def hyp_score(log_probs, length, length_penalty): ...@@ -130,8 +107,7 @@ def hyp_score(log_probs, length, length_penalty):
return log_probs / lp return log_probs / lp
def beam_search_step(state, logits, eos_id, beam_width, is_first_step, def beam_search_step(state, logits, eos_id, beam_width, is_first_step, length_penalty):
length_penalty):
"""logits.shape == [B*W, V]""" """logits.shape == [B*W, V]"""
beam_size, vocab_size = logits.shape # as batch size=1 in this hub module. the first dim means bsz * beam_size equals beam_size beam_size, vocab_size = logits.shape # as batch size=1 in this hub module. the first dim means bsz * beam_size equals beam_size
logits_np = logits.numpy() logits_np = logits.numpy()
...@@ -140,8 +116,7 @@ def beam_search_step(state, logits, eos_id, beam_width, is_first_step, ...@@ -140,8 +116,7 @@ def beam_search_step(state, logits, eos_id, beam_width, is_first_step,
logits = D.to_variable(logits_np) logits = D.to_variable(logits_np)
bsz, beam_width = state.log_probs.shape bsz, beam_width = state.log_probs.shape
onehot_eos = L.cast( onehot_eos = L.cast(F.one_hot(L.ones([1], 'int64') * eos_id, vocab_size), 'int64') #[1, V]
F.one_hot(L.ones([1], 'int64') * eos_id, vocab_size), 'int64') #[1, V]
probs = L.log(L.softmax(logits)) #[B*W, V] probs = L.log(L.softmax(logits)) #[B*W, V]
probs = mask_prob(probs, onehot_eos, state.finished) #[B*W, V] probs = mask_prob(probs, onehot_eos, state.finished) #[B*W, V]
...@@ -156,32 +131,24 @@ def beam_search_step(state, logits, eos_id, beam_width, is_first_step, ...@@ -156,32 +131,24 @@ def beam_search_step(state, logits, eos_id, beam_width, is_first_step,
alllen = L.reshape(alllen, [-1, beam_width * vocab_size]) alllen = L.reshape(alllen, [-1, beam_width * vocab_size])
allscore = hyp_score(allprobs, alllen, length_penalty) allscore = hyp_score(allprobs, alllen, length_penalty)
if is_first_step: if is_first_step:
allscore = L.reshape( allscore = L.reshape(allscore, [bsz, beam_width, -1])[:, 0, :] # first step only consiter beam 0
allscore,
[bsz, beam_width, -1])[:, 0, :] # first step only consiter beam 0
scores, idx = L.topk(allscore, k=beam_width) #[B, W] scores, idx = L.topk(allscore, k=beam_width) #[B, W]
next_beam_id = idx // vocab_size #[B, W] next_beam_id = idx // vocab_size #[B, W]
next_word_id = idx % vocab_size next_word_id = idx % vocab_size
gather_idx = L.concat([L.where(idx != -1)[:, :1], gather_idx = L.concat([L.where(idx != -1)[:, :1], L.reshape(idx, [-1, 1])], 1)
L.reshape(idx, [-1, 1])], 1)
next_probs = L.reshape(L.gather_nd(allprobs, gather_idx), idx.shape) next_probs = L.reshape(L.gather_nd(allprobs, gather_idx), idx.shape)
next_len = L.reshape(L.gather_nd(alllen, gather_idx), idx.shape) next_len = L.reshape(L.gather_nd(alllen, gather_idx), idx.shape)
gather_idx = L.concat( gather_idx = L.concat([L.where(next_beam_id != -1)[:, :1], L.reshape(next_beam_id, [-1, 1])], 1)
[L.where(next_beam_id != -1)[:, :1], next_finished = L.reshape(L.gather_nd(state.finished, gather_idx),
L.reshape(next_beam_id, [-1, 1])], 1)
next_finished = L.reshape(
L.gather_nd(state.finished, gather_idx),
state.finished.shape) #[gather new beam state according to new beam id] state.finished.shape) #[gather new beam state according to new beam id]
next_finished += L.cast(next_word_id == eos_id, 'int64') next_finished += L.cast(next_word_id == eos_id, 'int64')
next_finished = L.cast(next_finished > 0, 'int64') next_finished = L.cast(next_finished > 0, 'int64')
next_state = BeamSearchState( next_state = BeamSearchState(log_probs=next_probs, lengths=next_len, finished=next_finished)
log_probs=next_probs, lengths=next_len, finished=next_finished) output = BeamSearchOutput(scores=scores, predicted_ids=next_word_id, beam_parent_ids=next_beam_id)
output = BeamSearchOutput(
scores=scores, predicted_ids=next_word_id, beam_parent_ids=next_beam_id)
return output, next_state return output, next_state
...@@ -210,15 +177,13 @@ def beam_search_infilling(model, ...@@ -210,15 +177,13 @@ def beam_search_infilling(model,
def reorder_(t, parent_id): def reorder_(t, parent_id):
"""reorder cache according to parent beam id""" """reorder cache according to parent beam id"""
gather_idx = L.where(parent_id != -1)[:, 0] * beam_width + L.reshape( gather_idx = L.where(parent_id != -1)[:, 0] * beam_width + L.reshape(parent_id, [-1])
parent_id, [-1])
t = L.gather(t, gather_idx) t = L.gather(t, gather_idx)
return t return t
def tile_(t, times): def tile_(t, times):
_shapes = list(t.shape[1:]) _shapes = list(t.shape[1:])
ret = L.reshape( ret = L.reshape(L.expand(L.unsqueeze(t, [1]), [
L.expand(L.unsqueeze(t, [1]), [
1, 1,
times, times,
] + [ ] + [
...@@ -241,18 +206,11 @@ def beam_search_infilling(model, ...@@ -241,18 +206,11 @@ def beam_search_infilling(model,
ids = L.stack([cls_ids, attn_ids], -1) ids = L.stack([cls_ids, attn_ids], -1)
for step in range(max_decode_len): for step in range(max_decode_len):
bias = gen_bias(q_ids, ids, step) bias = gen_bias(q_ids, ids, step)
pos_ids = D.to_variable( pos_ids = D.to_variable(np.tile(np.array([[step, step + 1]], dtype=np.int64), [d_batch * beam_width, 1]))
np.tile(
np.array([[step, step + 1]], dtype=np.int64),
[d_batch * beam_width, 1]))
pos_ids += seqlen pos_ids += seqlen
_, logits, info = model( _, logits, info = model(
ids, ids, L.ones_like(ids) * tgt_type_id, pos_ids=pos_ids, attn_bias=bias, past_cache=past_cache)
L.ones_like(ids) * tgt_type_id,
pos_ids=pos_ids,
attn_bias=bias,
past_cache=past_cache)
output, state = beam_search_step( output, state = beam_search_step(
state, state,
...@@ -266,17 +224,14 @@ def beam_search_infilling(model, ...@@ -266,17 +224,14 @@ def beam_search_infilling(model,
past_cached_k, past_cached_v = past_cache past_cached_k, past_cached_v = past_cache
cached_k, cached_v = info['caches'] cached_k, cached_v = info['caches']
cached_k = [ cached_k = [
reorder_(L.concat([pk, k[:, :1, :]], 1), output.beam_parent_ids) reorder_(L.concat([pk, k[:, :1, :]], 1), output.beam_parent_ids) for pk, k in zip(past_cached_k, cached_k)
for pk, k in zip(past_cached_k, cached_k)
] # concat cached ] # concat cached
cached_v = [ cached_v = [
reorder_(L.concat([pv, v[:, :1, :]], 1), output.beam_parent_ids) reorder_(L.concat([pv, v[:, :1, :]], 1), output.beam_parent_ids) for pv, v in zip(past_cached_v, cached_v)
for pv, v in zip(past_cached_v, cached_v)
] ]
past_cache = (cached_k, cached_v) past_cache = (cached_k, cached_v)
pred_ids_flatten = L.reshape(output.predicted_ids, pred_ids_flatten = L.reshape(output.predicted_ids, [d_batch * beam_width])
[d_batch * beam_width])
ids = L.stack([pred_ids_flatten, attn_ids], 1) ids = L.stack([pred_ids_flatten, attn_ids], 1)
if state.finished.numpy().all(): if state.finished.numpy().all():
...@@ -286,8 +241,7 @@ def beam_search_infilling(model, ...@@ -286,8 +241,7 @@ def beam_search_infilling(model,
final_parent_ids = L.stack([o.beam_parent_ids for o in outputs], 0) final_parent_ids = L.stack([o.beam_parent_ids for o in outputs], 0)
final_ids = L.gather_tree(final_ids, final_parent_ids) #[:, :, final_ids = L.gather_tree(final_ids, final_parent_ids) #[:, :,
#0] #pick best beam #0] #pick best beam
final_ids = L.transpose( final_ids = L.transpose(L.reshape(final_ids, [-1, d_batch * 1, beam_width]), [1, 2, 0])
L.reshape(final_ids, [-1, d_batch * 1, beam_width]), [1, 2, 0])
return final_ids return final_ids
......
...@@ -27,10 +27,7 @@ def _fetch_from_remote(url, force_download=False): ...@@ -27,10 +27,7 @@ def _fetch_from_remote(url, force_download=False):
r = requests.get(url, stream=True) r = requests.get(url, stream=True)
total_len = int(r.headers.get('content-length')) total_len = int(r.headers.get('content-length'))
for chunk in tqdm( for chunk in tqdm(
r.iter_content(chunk_size=1024), r.iter_content(chunk_size=1024), total=total_len // 1024, desc='downloading %s' % url, unit='KB'):
total=total_len // 1024,
desc='downloading %s' % url,
unit='KB'):
if chunk: if chunk:
f.write(chunk) f.write(chunk)
f.flush() f.flush()
......
...@@ -30,9 +30,7 @@ def _build_linear(n_in, n_out, name, init, act=None): ...@@ -30,9 +30,7 @@ def _build_linear(n_in, n_out, name, init, act=None):
return D.Linear( return D.Linear(
n_in, n_in,
n_out, n_out,
param_attr=F.ParamAttr( param_attr=F.ParamAttr(name='%s.w_0' % name if name is not None else None, initializer=init),
name='%s.w_0' % name if name is not None else None,
initializer=init),
bias_attr='%s.b_0' % name if name is not None else None, bias_attr='%s.b_0' % name if name is not None else None,
act=act) act=act)
...@@ -41,11 +39,9 @@ def _build_ln(n_in, name): ...@@ -41,11 +39,9 @@ def _build_ln(n_in, name):
return D.LayerNorm( return D.LayerNorm(
normalized_shape=n_in, normalized_shape=n_in,
param_attr=F.ParamAttr( param_attr=F.ParamAttr(
name='%s_layer_norm_scale' % name if name is not None else None, name='%s_layer_norm_scale' % name if name is not None else None, initializer=F.initializer.Constant(1.)),
initializer=F.initializer.Constant(1.)),
bias_attr=F.ParamAttr( bias_attr=F.ParamAttr(
name='%s_layer_norm_bias' % name if name is not None else None, name='%s_layer_norm_bias' % name if name is not None else None, initializer=F.initializer.Constant(1.)),
initializer=F.initializer.Constant(1.)),
) )
...@@ -61,25 +57,18 @@ def append_name(name, postfix): ...@@ -61,25 +57,18 @@ def append_name(name, postfix):
class AttentionLayer(D.Layer): class AttentionLayer(D.Layer):
def __init__(self, cfg, name=None): def __init__(self, cfg, name=None):
super(AttentionLayer, self).__init__() super(AttentionLayer, self).__init__()
initializer = F.initializer.TruncatedNormal( initializer = F.initializer.TruncatedNormal(scale=cfg['initializer_range'])
scale=cfg['initializer_range'])
d_model = cfg['hidden_size'] d_model = cfg['hidden_size']
n_head = cfg['num_attention_heads'] n_head = cfg['num_attention_heads']
assert d_model % n_head == 0 assert d_model % n_head == 0
d_model_q = cfg.get('query_hidden_size_per_head', d_model_q = cfg.get('query_hidden_size_per_head', d_model // n_head) * n_head
d_model // n_head) * n_head d_model_v = cfg.get('value_hidden_size_per_head', d_model // n_head) * n_head
d_model_v = cfg.get('value_hidden_size_per_head',
d_model // n_head) * n_head
self.n_head = n_head self.n_head = n_head
self.d_key = d_model_q // n_head self.d_key = d_model_q // n_head
self.q = _build_linear(d_model, d_model_q, append_name( self.q = _build_linear(d_model, d_model_q, append_name(name, 'query_fc'), initializer)
name, 'query_fc'), initializer) self.k = _build_linear(d_model, d_model_q, append_name(name, 'key_fc'), initializer)
self.k = _build_linear(d_model, d_model_q, append_name(name, 'key_fc'), self.v = _build_linear(d_model, d_model_v, append_name(name, 'value_fc'), initializer)
initializer) self.o = _build_linear(d_model_v, d_model, append_name(name, 'output_fc'), initializer)
self.v = _build_linear(d_model, d_model_v, append_name(
name, 'value_fc'), initializer)
self.o = _build_linear(d_model_v, d_model, append_name(
name, 'output_fc'), initializer)
self.dropout = lambda i: L.dropout( self.dropout = lambda i: L.dropout(
i, i,
dropout_prob=cfg['attention_probs_dropout_prob'], dropout_prob=cfg['attention_probs_dropout_prob'],
...@@ -99,14 +88,11 @@ class AttentionLayer(D.Layer): ...@@ -99,14 +88,11 @@ class AttentionLayer(D.Layer):
k = L.concat([cached_k, k], 1) k = L.concat([cached_k, k], 1)
v = L.concat([cached_v, v], 1) v = L.concat([cached_v, v], 1)
q = L.transpose( q = L.transpose(L.reshape(q, [0, 0, self.n_head, q.shape[-1] // self.n_head]),
L.reshape(q, [0, 0, self.n_head, q.shape[-1] // self.n_head]),
[0, 2, 1, 3]) #[batch, head, seq, dim] [0, 2, 1, 3]) #[batch, head, seq, dim]
k = L.transpose( k = L.transpose(L.reshape(k, [0, 0, self.n_head, k.shape[-1] // self.n_head]),
L.reshape(k, [0, 0, self.n_head, k.shape[-1] // self.n_head]),
[0, 2, 1, 3]) #[batch, head, seq, dim] [0, 2, 1, 3]) #[batch, head, seq, dim]
v = L.transpose( v = L.transpose(L.reshape(v, [0, 0, self.n_head, v.shape[-1] // self.n_head]),
L.reshape(v, [0, 0, self.n_head, v.shape[-1] // self.n_head]),
[0, 2, 1, 3]) #[batch, head, seq, dim] [0, 2, 1, 3]) #[batch, head, seq, dim]
q = L.scale(q, scale=self.d_key**-0.5) q = L.scale(q, scale=self.d_key**-0.5)
...@@ -127,19 +113,12 @@ class AttentionLayer(D.Layer): ...@@ -127,19 +113,12 @@ class AttentionLayer(D.Layer):
class PositionwiseFeedForwardLayer(D.Layer): class PositionwiseFeedForwardLayer(D.Layer):
def __init__(self, cfg, name=None): def __init__(self, cfg, name=None):
super(PositionwiseFeedForwardLayer, self).__init__() super(PositionwiseFeedForwardLayer, self).__init__()
initializer = F.initializer.TruncatedNormal( initializer = F.initializer.TruncatedNormal(scale=cfg['initializer_range'])
scale=cfg['initializer_range'])
d_model = cfg['hidden_size'] d_model = cfg['hidden_size']
d_ffn = cfg.get('intermediate_size', 4 * d_model) d_ffn = cfg.get('intermediate_size', 4 * d_model)
assert cfg['hidden_act'] in ['relu', 'gelu'] assert cfg['hidden_act'] in ['relu', 'gelu']
self.i = _build_linear( self.i = _build_linear(d_model, d_ffn, append_name(name, 'fc_0'), initializer, act=cfg['hidden_act'])
d_model, self.o = _build_linear(d_ffn, d_model, append_name(name, 'fc_1'), initializer)
d_ffn,
append_name(name, 'fc_0'),
initializer,
act=cfg['hidden_act'])
self.o = _build_linear(d_ffn, d_model, append_name(name, 'fc_1'),
initializer)
prob = cfg.get('intermediate_dropout_prob', 0.) prob = cfg.get('intermediate_dropout_prob', 0.)
self.dropout = lambda i: L.dropout( self.dropout = lambda i: L.dropout(
i, i,
...@@ -158,14 +137,11 @@ class ErnieBlock(D.Layer): ...@@ -158,14 +137,11 @@ class ErnieBlock(D.Layer):
def __init__(self, cfg, name=None): def __init__(self, cfg, name=None):
super(ErnieBlock, self).__init__() super(ErnieBlock, self).__init__()
d_model = cfg['hidden_size'] d_model = cfg['hidden_size']
initializer = F.initializer.TruncatedNormal( initializer = F.initializer.TruncatedNormal(scale=cfg['initializer_range'])
scale=cfg['initializer_range'])
self.attn = AttentionLayer( self.attn = AttentionLayer(cfg, name=append_name(name, 'multi_head_att'))
cfg, name=append_name(name, 'multi_head_att'))
self.ln1 = _build_ln(d_model, name=append_name(name, 'post_att')) self.ln1 = _build_ln(d_model, name=append_name(name, 'post_att'))
self.ffn = PositionwiseFeedForwardLayer( self.ffn = PositionwiseFeedForwardLayer(cfg, name=append_name(name, 'ffn'))
cfg, name=append_name(name, 'ffn'))
self.ln2 = _build_ln(d_model, name=append_name(name, 'post_ffn')) self.ln2 = _build_ln(d_model, name=append_name(name, 'post_ffn'))
prob = cfg.get('intermediate_dropout_prob', cfg['hidden_dropout_prob']) prob = cfg.get('intermediate_dropout_prob', cfg['hidden_dropout_prob'])
self.dropout = lambda i: L.dropout( self.dropout = lambda i: L.dropout(
...@@ -175,9 +151,7 @@ class ErnieBlock(D.Layer): ...@@ -175,9 +151,7 @@ class ErnieBlock(D.Layer):
) if self.training else i ) if self.training else i
def forward(self, inputs, attn_bias=None, past_cache=None): def forward(self, inputs, attn_bias=None, past_cache=None):
attn_out, cache = self.attn( attn_out, cache = self.attn(inputs, inputs, inputs, attn_bias, past_cache=past_cache) #self attn
inputs, inputs, inputs, attn_bias,
past_cache=past_cache) #self attn
attn_out = self.dropout(attn_out) attn_out = self.dropout(attn_out)
hidden = attn_out + inputs hidden = attn_out + inputs
hidden = self.ln1(hidden) # dropout/ add/ norm hidden = self.ln1(hidden) # dropout/ add/ norm
...@@ -193,17 +167,13 @@ class ErnieEncoderStack(D.Layer): ...@@ -193,17 +167,13 @@ class ErnieEncoderStack(D.Layer):
def __init__(self, cfg, name=None): def __init__(self, cfg, name=None):
super(ErnieEncoderStack, self).__init__() super(ErnieEncoderStack, self).__init__()
n_layers = cfg['num_hidden_layers'] n_layers = cfg['num_hidden_layers']
self.block = D.LayerList([ self.block = D.LayerList([ErnieBlock(cfg, append_name(name, 'layer_%d' % i)) for i in range(n_layers)])
ErnieBlock(cfg, append_name(name, 'layer_%d' % i))
for i in range(n_layers)
])
def forward(self, inputs, attn_bias=None, past_cache=None): def forward(self, inputs, attn_bias=None, past_cache=None):
if past_cache is not None: if past_cache is not None:
assert isinstance( assert isinstance(
past_cache, tuple past_cache,
), 'unknown type of `past_cache`, expect tuple or list. got %s' % repr( tuple), 'unknown type of `past_cache`, expect tuple or list. got %s' % repr(type(past_cache))
type(past_cache))
past_cache = list(zip(*past_cache)) past_cache = list(zip(*past_cache))
else: else:
past_cache = [None] * len(self.block) past_cache = [None] * len(self.block)
...@@ -233,24 +203,18 @@ class ErnieModel(D.Layer): ...@@ -233,24 +203,18 @@ class ErnieModel(D.Layer):
d_sent = cfg.get("sent_type_vocab_size") or cfg['type_vocab_size'] d_sent = cfg.get("sent_type_vocab_size") or cfg['type_vocab_size']
self.n_head = cfg['num_attention_heads'] self.n_head = cfg['num_attention_heads']
self.return_additional_info = cfg.get('return_additional_info', False) self.return_additional_info = cfg.get('return_additional_info', False)
initializer = F.initializer.TruncatedNormal( initializer = F.initializer.TruncatedNormal(scale=cfg['initializer_range'])
scale=cfg['initializer_range'])
self.ln = _build_ln(d_model, name=append_name(name, 'pre_encoder')) self.ln = _build_ln(d_model, name=append_name(name, 'pre_encoder'))
self.word_emb = D.Embedding([d_vocab, d_emb], self.word_emb = D.Embedding([d_vocab, d_emb],
param_attr=F.ParamAttr( param_attr=F.ParamAttr(
name=append_name( name=append_name(name, 'word_embedding'), initializer=initializer))
name, 'word_embedding'),
initializer=initializer))
self.pos_emb = D.Embedding([d_pos, d_emb], self.pos_emb = D.Embedding([d_pos, d_emb],
param_attr=F.ParamAttr( param_attr=F.ParamAttr(
name=append_name(name, 'pos_embedding'), name=append_name(name, 'pos_embedding'), initializer=initializer))
initializer=initializer))
self.sent_emb = D.Embedding([d_sent, d_emb], self.sent_emb = D.Embedding([d_sent, d_emb],
param_attr=F.ParamAttr( param_attr=F.ParamAttr(
name=append_name( name=append_name(name, 'sent_embedding'), initializer=initializer))
name, 'sent_embedding'),
initializer=initializer))
prob = cfg['hidden_dropout_prob'] prob = cfg['hidden_dropout_prob']
self.dropout = lambda i: L.dropout( self.dropout = lambda i: L.dropout(
i, i,
...@@ -258,15 +222,10 @@ class ErnieModel(D.Layer): ...@@ -258,15 +222,10 @@ class ErnieModel(D.Layer):
dropout_implementation="upscale_in_train", dropout_implementation="upscale_in_train",
) if self.training else i ) if self.training else i
self.encoder_stack = ErnieEncoderStack(cfg, append_name( self.encoder_stack = ErnieEncoderStack(cfg, append_name(name, 'encoder'))
name, 'encoder'))
if cfg.get('has_pooler', True): if cfg.get('has_pooler', True):
self.pooler = _build_linear( self.pooler = _build_linear(
cfg['hidden_size'], cfg['hidden_size'], cfg['hidden_size'], append_name(name, 'pooled_fc'), initializer, act='tanh')
cfg['hidden_size'],
append_name(name, 'pooled_fc'),
initializer,
act='tanh')
else: else:
self.pooler = None self.pooler = None
self.train() self.train()
...@@ -317,10 +276,7 @@ class ErnieModel(D.Layer): ...@@ -317,10 +276,7 @@ class ErnieModel(D.Layer):
encoded(`Variable` of shape `[batch_size, seq_len, hidden_size]`): encoded(`Variable` of shape `[batch_size, seq_len, hidden_size]`):
output logits of transformer stack output logits of transformer stack
""" """
assert len( assert len(src_ids.shape) == 2, 'expect src_ids.shape = [batch, sequecen], got %s' % (repr(src_ids.shape))
src_ids.shape
) == 2, 'expect src_ids.shape = [batch, sequecen], got %s' % (repr(
src_ids.shape))
assert attn_bias is not None if past_cache else True, 'if `past_cache` is specified; attn_bias should not be None' assert attn_bias is not None if past_cache else True, 'if `past_cache` is specified; attn_bias should not be None'
d_batch = L.shape(src_ids)[0] d_batch = L.shape(src_ids)[0]
d_seqlen = L.shape(src_ids)[1] d_seqlen = L.shape(src_ids)[1]
...@@ -334,21 +290,14 @@ class ErnieModel(D.Layer): ...@@ -334,21 +290,14 @@ class ErnieModel(D.Layer):
input_mask = L.unsqueeze(input_mask, axes=[-1]) input_mask = L.unsqueeze(input_mask, axes=[-1])
attn_bias = L.matmul(input_mask, input_mask, transpose_y=True) attn_bias = L.matmul(input_mask, input_mask, transpose_y=True)
if use_causal_mask: if use_causal_mask:
sequence = L.reshape( sequence = L.reshape(L.range(0, d_seqlen, 1, dtype='float32') + 1., [1, 1, -1, 1])
L.range(0, d_seqlen, 1, dtype='float32') + 1., causal_mask = L.cast((L.matmul(sequence, 1. / sequence, transpose_y=True) >= 1.), 'float32')
[1, 1, -1, 1])
causal_mask = L.cast(
(L.matmul(sequence, 1. / sequence, transpose_y=True) >= 1.),
'float32')
attn_bias *= causal_mask attn_bias *= causal_mask
else: else:
assert len( assert len(attn_bias.shape) == 3, 'expect attn_bias tobe rank 3, got %r' % attn_bias.shape
attn_bias.shape
) == 3, 'expect attn_bias tobe rank 3, got %r' % attn_bias.shape
attn_bias = (1. - attn_bias) * -10000.0 attn_bias = (1. - attn_bias) * -10000.0
attn_bias = L.unsqueeze(attn_bias, [1]) attn_bias = L.unsqueeze(attn_bias, [1])
attn_bias = L.expand(attn_bias, attn_bias = L.expand(attn_bias, [1, self.n_head, 1, 1]) # avoid broadcast =_=
[1, self.n_head, 1, 1]) # avoid broadcast =_=
attn_bias.stop_gradient = True attn_bias.stop_gradient = True
if sent_ids is None: if sent_ids is None:
...@@ -361,8 +310,7 @@ class ErnieModel(D.Layer): ...@@ -361,8 +310,7 @@ class ErnieModel(D.Layer):
embedded = self.dropout(self.ln(embedded)) embedded = self.dropout(self.ln(embedded))
encoded, hidden_list, cache_list = self.encoder_stack( encoded, hidden_list, cache_list = self.encoder_stack(embedded, attn_bias, past_cache=past_cache)
embedded, attn_bias, past_cache=past_cache)
if self.pooler is not None: if self.pooler is not None:
pooled = self.pooler(encoded[:, 0, :]) pooled = self.pooler(encoded[:, 0, :])
else: else:
......
...@@ -24,25 +24,18 @@ class ErnieModelForGeneration(ErnieModel): ...@@ -24,25 +24,18 @@ class ErnieModelForGeneration(ErnieModel):
cfg['return_additional_info'] = True cfg['return_additional_info'] = True
cfg['has_pooler'] = False cfg['has_pooler'] = False
super(ErnieModelForGeneration, self).__init__(cfg, name=name) super(ErnieModelForGeneration, self).__init__(cfg, name=name)
initializer = F.initializer.TruncatedNormal( initializer = F.initializer.TruncatedNormal(scale=cfg['initializer_range'])
scale=cfg['initializer_range'])
d_model = cfg['hidden_size'] d_model = cfg['hidden_size']
d_vocab = cfg['vocab_size'] d_vocab = cfg['vocab_size']
self.mlm = _build_linear( self.mlm = _build_linear(
d_model, d_model, d_model, append_name(name, 'mask_lm_trans_fc'), initializer, act=cfg['hidden_act'])
d_model, self.mlm_ln = _build_ln(d_model, name=append_name(name, 'mask_lm_trans'))
append_name(name, 'mask_lm_trans_fc'),
initializer,
act=cfg['hidden_act'])
self.mlm_ln = _build_ln(
d_model, name=append_name(name, 'mask_lm_trans'))
self.mlm_bias = L.create_parameter( self.mlm_bias = L.create_parameter(
dtype='float32', dtype='float32',
shape=[d_vocab], shape=[d_vocab],
attr=F.ParamAttr( attr=F.ParamAttr(
name=append_name(name, 'mask_lm_out_fc.b_0'), name=append_name(name, 'mask_lm_out_fc.b_0'), initializer=F.initializer.Constant(value=0.0)),
initializer=F.initializer.Constant(value=0.0)),
is_bias=True, is_bias=True,
) )
...@@ -56,23 +49,17 @@ class ErnieModelForGeneration(ErnieModel): ...@@ -56,23 +49,17 @@ class ErnieModelForGeneration(ErnieModel):
elif tgt_labels is None: elif tgt_labels is None:
encoded = self.mlm(encoded) encoded = self.mlm(encoded)
encoded = self.mlm_ln(encoded) encoded = self.mlm_ln(encoded)
logits = L.matmul( logits = L.matmul(encoded, self.word_emb.weight, transpose_y=True) + self.mlm_bias
encoded, self.word_emb.weight, transpose_y=True) + self.mlm_bias
output_ids = L.argmax(logits, -1) output_ids = L.argmax(logits, -1)
return output_ids, logits, info return output_ids, logits, info
else: else:
encoded_2d = L.gather_nd(encoded, tgt_pos) encoded_2d = L.gather_nd(encoded, tgt_pos)
encoded_2d = self.mlm(encoded_2d) encoded_2d = self.mlm(encoded_2d)
encoded_2d = self.mlm_ln(encoded_2d) encoded_2d = self.mlm_ln(encoded_2d)
logits_2d = L.matmul( logits_2d = L.matmul(encoded_2d, self.word_emb.weight, transpose_y=True) + self.mlm_bias
encoded_2d, self.word_emb.weight,
transpose_y=True) + self.mlm_bias
if len(tgt_labels.shape) == 1: if len(tgt_labels.shape) == 1:
tgt_labels = L.reshape(tgt_labels, [-1, 1]) tgt_labels = L.reshape(tgt_labels, [-1, 1])
loss = L.reduce_mean( loss = L.reduce_mean(
L.softmax_with_cross_entropy( L.softmax_with_cross_entropy(logits_2d, tgt_labels, soft_label=(tgt_labels.shape[-1] != 1)))
logits_2d,
tgt_labels,
soft_label=(tgt_labels.shape[-1] != 1)))
return loss, logits_2d, info return loss, logits_2d, info
...@@ -77,8 +77,7 @@ class ErnieTokenizer(object): ...@@ -77,8 +77,7 @@ class ErnieTokenizer(object):
encoding='utf8', encoding='utf8',
special_token_list=[]): special_token_list=[]):
if not isinstance(vocab, dict): if not isinstance(vocab, dict):
raise ValueError( raise ValueError('expect `vocab` to be instance of dict, got %s' % type(vocab))
'expect `vocab` to be instance of dict, got %s' % type(vocab))
self.vocab = vocab self.vocab = vocab
self.lower = lower self.lower = lower
self.prefix = wordpiece_prefix self.prefix = wordpiece_prefix
...@@ -89,9 +88,7 @@ class ErnieTokenizer(object): ...@@ -89,9 +88,7 @@ class ErnieTokenizer(object):
self.unk_id = unk_token and self.vocab[unk_token] self.unk_id = unk_token and self.vocab[unk_token]
self.mask_id = mask_token and self.vocab[mask_token] self.mask_id = mask_token and self.vocab[mask_token]
self.unk_token = unk_token self.unk_token = unk_token
special_tokens = { special_tokens = {pad_token, cls_token, sep_token, unk_token, mask_token} | set(special_token_list)
pad_token, cls_token, sep_token, unk_token, mask_token
} | set(special_token_list)
pat_str = '' pat_str = ''
for t in special_tokens: for t in special_tokens:
if t is None: if t is None:
...@@ -135,11 +132,9 @@ class ErnieTokenizer(object): ...@@ -135,11 +132,9 @@ class ErnieTokenizer(object):
len2 = len(id2) len2 = len(id2)
half = seqlen // 2 half = seqlen // 2
if len1 > len2: if len1 > len2:
len1_truncated, len2_truncated = max(half, seqlen - len2), min( len1_truncated, len2_truncated = max(half, seqlen - len2), min(half, len2)
half, len2)
else: else:
len1_truncated, len2_truncated = min(half, seqlen - len1), max( len1_truncated, len2_truncated = min(half, seqlen - len1), max(half, seqlen - len1)
half, seqlen - len1)
return id1[:len1_truncated], id2[:len2_truncated] return id1[:len1_truncated], id2[:len2_truncated]
def build_for_ernie(self, text_id, pair_id=[]): def build_for_ernie(self, text_id, pair_id=[]):
...@@ -155,17 +150,14 @@ class ErnieTokenizer(object): ...@@ -155,17 +150,14 @@ class ErnieTokenizer(object):
return ret_id, ret_id_type return ret_id, ret_id_type
def encode(self, text, pair=None, truncate_to=None): def encode(self, text, pair=None, truncate_to=None):
text_id = np.array( text_id = np.array(self.convert_tokens_to_ids(self.tokenize(text)), dtype=np.int64)
self.convert_tokens_to_ids(self.tokenize(text)), dtype=np.int64)
text_id_type = np.zeros_like(text_id, dtype=np.int64) text_id_type = np.zeros_like(text_id, dtype=np.int64)
if pair is not None: if pair is not None:
pair_id = np.array( pair_id = np.array(self.convert_tokens_to_ids(self.tokenize(pair)), dtype=np.int64)
self.convert_tokens_to_ids(self.tokenize(pair)), dtype=np.int64)
else: else:
pair_id = [] pair_id = []
if truncate_to is not None: if truncate_to is not None:
text_id, pair_id = self.truncate( text_id, pair_id = self.truncate(text_id, [] if pair_id is None else pair_id, truncate_to)
text_id, [] if pair_id is None else pair_id, truncate_to)
ret_id, ret_id_type = self.build_for_ernie(text_id, pair_id) ret_id, ret_id_type = self.build_for_ernie(text_id, pair_id)
return ret_id, ret_id_type return ret_id, ret_id_type
...@@ -35,8 +35,7 @@ from .model.modeling_ernie_gen import ErnieModelForGeneration ...@@ -35,8 +35,7 @@ from .model.modeling_ernie_gen import ErnieModelForGeneration
@moduleinfo( @moduleinfo(
name="ernie_gen_leave", name="ernie_gen_leave",
version="1.0.0", version="1.0.0",
summary= summary="",
"",
author="彭兆帅,郑博培", author="彭兆帅,郑博培",
author_email="1084667371@qq.com,2733821739@qq.com", author_email="1084667371@qq.com,2733821739@qq.com",
type="nlp/text_generation", type="nlp/text_generation",
...@@ -53,10 +52,7 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -53,10 +52,7 @@ class ErnieGen(hub.NLPPredictionModule):
ernie_cfg = dict(json.loads(ernie_cfg_file.read())) ernie_cfg = dict(json.loads(ernie_cfg_file.read()))
ernie_vocab_path = os.path.join(assets_path, 'vocab.txt') ernie_vocab_path = os.path.join(assets_path, 'vocab.txt')
with open(ernie_vocab_path, encoding='utf8') as ernie_vocab_file: with open(ernie_vocab_path, encoding='utf8') as ernie_vocab_file:
ernie_vocab = { ernie_vocab = {j.strip().split('\t')[0]: i for i, j in enumerate(ernie_vocab_file.readlines())}
j.strip().split('\t')[0]: i
for i, j in enumerate(ernie_vocab_file.readlines())
}
with fluid.dygraph.guard(fluid.CPUPlace()): with fluid.dygraph.guard(fluid.CPUPlace()):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
...@@ -83,13 +79,10 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -83,13 +79,10 @@ class ErnieGen(hub.NLPPredictionModule):
Returns: Returns:
results(list): the predict result. results(list): the predict result.
""" """
if texts and isinstance(texts, list) and all(texts) and all( if texts and isinstance(texts, list) and all(texts) and all([isinstance(text, str) for text in texts]):
[isinstance(text, str) for text in texts]):
predicted_data = texts predicted_data = texts
else: else:
raise ValueError( raise ValueError("The input texts should be a list with nonempty string elements.")
"The input texts should be a list with nonempty string elements."
)
if use_gpu and "CUDA_VISIBLE_DEVICES" not in os.environ: if use_gpu and "CUDA_VISIBLE_DEVICES" not in os.environ:
use_gpu = False use_gpu = False
...@@ -134,13 +127,9 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -134,13 +127,9 @@ class ErnieGen(hub.NLPPredictionModule):
Add the command config options Add the command config options
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--use_gpu', '--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction")
type=ast.literal_eval,
default=False,
help="whether use GPU for prediction")
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width")
'--beam_width', type=int, default=5, help="the beam search width")
@runnable @runnable
def run_cmd(self, argvs): def run_cmd(self, argvs):
...@@ -153,12 +142,9 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -153,12 +142,9 @@ class ErnieGen(hub.NLPPredictionModule):
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, optional.")
description=
"Run configuration for controlling module behavior, optional.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
...@@ -171,7 +157,6 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -171,7 +157,6 @@ class ErnieGen(hub.NLPPredictionModule):
self.parser.print_help() self.parser.print_help()
return None return None
results = self.generate( results = self.generate(texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width)
texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width)
return results return results
...@@ -46,10 +46,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -46,10 +46,8 @@ class UnifiedTransformer(nn.Layer):
""" """
Convert input strings to tokens. Convert input strings to tokens.
""" """
return self.tokenizer.dialogue_encode(texts, return self.tokenizer.dialogue_encode(
max_seq_len=max_seq_len, texts, max_seq_len=max_seq_len, add_start_token_as_response=True, is_split_into_words=False)
add_start_token_as_response=True,
is_split_into_words=False)
def _batchify(self, data: List[List[str]], max_seq_len: int, batch_size: int): def _batchify(self, data: List[List[str]], max_seq_len: int, batch_size: int):
""" """
...@@ -127,7 +125,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -127,7 +125,8 @@ class UnifiedTransformer(nn.Layer):
early_stopping=False, early_stopping=False,
num_return_sequences=1): num_return_sequences=1):
ids, scores = self.model.generate(input_ids=input_ids, ids, scores = self.model.generate(
input_ids=input_ids,
token_type_ids=token_type_ids, token_type_ids=token_type_ids,
position_ids=position_ids, position_ids=position_ids,
attention_mask=attention_mask, attention_mask=attention_mask,
...@@ -173,11 +172,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -173,11 +172,8 @@ class UnifiedTransformer(nn.Layer):
num_return_sequences = 1 if 'num_return_sequences' not in kwargs\ num_return_sequences = 1 if 'num_return_sequences' not in kwargs\
else kwargs['num_return_sequences'] else kwargs['num_return_sequences']
results.extend( results.extend(
select_response(ids, select_response(
scores, ids, scores, self.tokenizer, num_return_sequences=num_return_sequences, keep_space=False))
self.tokenizer,
num_return_sequences=num_return_sequences,
keep_space=False))
if self._interactive_mode: if self._interactive_mode:
self.context.append(results[0].strip()) self.context.append(results[0].strip())
......
...@@ -46,10 +46,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -46,10 +46,8 @@ class UnifiedTransformer(nn.Layer):
""" """
Convert input strings to tokens. Convert input strings to tokens.
""" """
return self.tokenizer.dialogue_encode(texts, return self.tokenizer.dialogue_encode(
max_seq_len=max_seq_len, texts, max_seq_len=max_seq_len, add_start_token_as_response=True, is_split_into_words=False)
add_start_token_as_response=True,
is_split_into_words=False)
def _batchify(self, data: List[List[str]], max_seq_len: int, batch_size: int): def _batchify(self, data: List[List[str]], max_seq_len: int, batch_size: int):
""" """
...@@ -127,7 +125,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -127,7 +125,8 @@ class UnifiedTransformer(nn.Layer):
early_stopping=False, early_stopping=False,
num_return_sequences=1): num_return_sequences=1):
ids, scores = self.model.generate(input_ids=input_ids, ids, scores = self.model.generate(
input_ids=input_ids,
token_type_ids=token_type_ids, token_type_ids=token_type_ids,
position_ids=position_ids, position_ids=position_ids,
attention_mask=attention_mask, attention_mask=attention_mask,
...@@ -173,11 +172,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -173,11 +172,8 @@ class UnifiedTransformer(nn.Layer):
num_return_sequences = 1 if 'num_return_sequences' not in kwargs\ num_return_sequences = 1 if 'num_return_sequences' not in kwargs\
else kwargs['num_return_sequences'] else kwargs['num_return_sequences']
results.extend( results.extend(
select_response(ids, select_response(
scores, ids, scores, self.tokenizer, num_return_sequences=num_return_sequences, keep_space=False))
self.tokenizer,
num_return_sequences=num_return_sequences,
keep_space=False))
if self._interactive_mode: if self._interactive_mode:
self.context.append(results[0].strip()) self.context.append(results[0].strip())
......
...@@ -46,10 +46,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -46,10 +46,8 @@ class UnifiedTransformer(nn.Layer):
""" """
Convert input strings to tokens. Convert input strings to tokens.
""" """
return self.tokenizer.dialogue_encode(texts, return self.tokenizer.dialogue_encode(
max_seq_len=max_seq_len, texts, max_seq_len=max_seq_len, add_start_token_as_response=True, is_split_into_words=False)
add_start_token_as_response=True,
is_split_into_words=False)
def _batchify(self, data: List[List[str]], max_seq_len: int, batch_size: int): def _batchify(self, data: List[List[str]], max_seq_len: int, batch_size: int):
""" """
...@@ -127,7 +125,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -127,7 +125,8 @@ class UnifiedTransformer(nn.Layer):
early_stopping=False, early_stopping=False,
num_return_sequences=1): num_return_sequences=1):
ids, scores = self.model.generate(input_ids=input_ids, ids, scores = self.model.generate(
input_ids=input_ids,
token_type_ids=token_type_ids, token_type_ids=token_type_ids,
position_ids=position_ids, position_ids=position_ids,
attention_mask=attention_mask, attention_mask=attention_mask,
...@@ -173,11 +172,8 @@ class UnifiedTransformer(nn.Layer): ...@@ -173,11 +172,8 @@ class UnifiedTransformer(nn.Layer):
num_return_sequences = 1 if 'num_return_sequences' not in kwargs\ num_return_sequences = 1 if 'num_return_sequences' not in kwargs\
else kwargs['num_return_sequences'] else kwargs['num_return_sequences']
results.extend( results.extend(
select_response(ids, select_response(
scores, ids, scores, self.tokenizer, num_return_sequences=num_return_sequences, keep_space=False))
self.tokenizer,
num_return_sequences=num_return_sequences,
keep_space=False))
if self._interactive_mode: if self._interactive_mode:
self.context.append(results[0].strip()) self.context.append(results[0].strip())
......
...@@ -41,18 +41,10 @@ def read_images(paths): ...@@ -41,18 +41,10 @@ def read_images(paths):
version='1.0.0') version='1.0.0')
class MODULE(hub.Module): class MODULE(hub.Module):
def _initialize(self, **kwargs): def _initialize(self, **kwargs):
self.default_pretrained_model_path = os.path.join( self.default_pretrained_model_path = os.path.join(self.directory, 'assets')
self.directory, 'assets') self.model = pdx.deploy.Predictor(self.default_pretrained_model_path, **kwargs)
self.model = pdx.deploy.Predictor(self.default_pretrained_model_path,
**kwargs) def predict(self, images=None, paths=None, data=None, batch_size=1, use_gpu=False, **kwargs):
def predict(self,
images=None,
paths=None,
data=None,
batch_size=1,
use_gpu=False,
**kwargs):
all_data = images if images is not None else read_images(paths) all_data = images if images is not None else read_images(paths)
total_num = len(all_data) total_num = len(all_data)
...@@ -110,36 +102,27 @@ class MODULE(hub.Module): ...@@ -110,36 +102,27 @@ class MODULE(hub.Module):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(paths=[args.input_path], use_gpu=args.use_gpu)
paths=[args.input_path],
use_gpu=args.use_gpu)
return results return results
def add_module_config_arg(self): def add_module_config_arg(self):
""" """
Add the command config options. Add the command config options.
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--use_gpu', type=bool, default=False, help="whether use GPU or not")
'--use_gpu',
type=bool,
default=False,
help="whether use GPU or not")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
if __name__ == '__main__': if __name__ == '__main__':
module = MODULE(directory='./new_model') module = MODULE(directory='./new_model')
......
...@@ -41,18 +41,10 @@ def read_images(paths): ...@@ -41,18 +41,10 @@ def read_images(paths):
version='1.0.0') version='1.0.0')
class MODULE(hub.Module): class MODULE(hub.Module):
def _initialize(self, **kwargs): def _initialize(self, **kwargs):
self.default_pretrained_model_path = os.path.join( self.default_pretrained_model_path = os.path.join(self.directory, 'assets')
self.directory, 'assets') self.model = pdx.deploy.Predictor(self.default_pretrained_model_path, **kwargs)
self.model = pdx.deploy.Predictor(self.default_pretrained_model_path,
**kwargs) def predict(self, images=None, paths=None, data=None, batch_size=1, use_gpu=False, **kwargs):
def predict(self,
images=None,
paths=None,
data=None,
batch_size=1,
use_gpu=False,
**kwargs):
all_data = images if images is not None else read_images(paths) all_data = images if images is not None else read_images(paths)
total_num = len(all_data) total_num = len(all_data)
...@@ -110,36 +102,27 @@ class MODULE(hub.Module): ...@@ -110,36 +102,27 @@ class MODULE(hub.Module):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(paths=[args.input_path], use_gpu=args.use_gpu)
paths=[args.input_path],
use_gpu=args.use_gpu)
return results return results
def add_module_config_arg(self): def add_module_config_arg(self):
""" """
Add the command config options. Add the command config options.
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--use_gpu', type=bool, default=False, help="whether use GPU or not")
'--use_gpu',
type=bool,
default=False,
help="whether use GPU or not")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
if __name__ == '__main__': if __name__ == '__main__':
module = MODULE(directory='./new_model') module = MODULE(directory='./new_model')
......
...@@ -3,11 +3,13 @@ import numpy as np ...@@ -3,11 +3,13 @@ import numpy as np
from typing import Callable from typing import Callable
from code.config import config_parameters from code.config import config_parameters
class GemStones(paddle.io.Dataset): class GemStones(paddle.io.Dataset):
""" """
step 1:paddle.io.Dataset step 1:paddle.io.Dataset
""" """
def __init__(self, transforms: Callable, mode: str ='train'):
def __init__(self, transforms: Callable, mode: str = 'train'):
""" """
step 2:create reader step 2:create reader
""" """
...@@ -33,7 +35,6 @@ class GemStones(paddle.io.Dataset): ...@@ -33,7 +35,6 @@ class GemStones(paddle.io.Dataset):
elif self.mode == 'test': elif self.mode == 'test':
self.data = test_data_folder self.data = test_data_folder
def __getitem__(self, index): def __getitem__(self, index):
""" """
step 3:implement __getitem__ step 3:implement __getitem__
......
...@@ -43,20 +43,11 @@ class BottleneckBlock(nn.Layer): ...@@ -43,20 +43,11 @@ class BottleneckBlock(nn.Layer):
self.conv1 = nn.Conv2D(inplanes, width, 1, bias_attr=False) self.conv1 = nn.Conv2D(inplanes, width, 1, bias_attr=False)
self.bn1 = norm_layer(width) self.bn1 = norm_layer(width)
self.conv2 = nn.Conv2D(width, self.conv2 = nn.Conv2D(
width, width, width, 3, padding=dilation, stride=stride, groups=groups, dilation=dilation, bias_attr=False)
3,
padding=dilation,
stride=stride,
groups=groups,
dilation=dilation,
bias_attr=False)
self.bn2 = norm_layer(width) self.bn2 = norm_layer(width)
self.conv3 = nn.Conv2D(width, self.conv3 = nn.Conv2D(width, planes * self.expansion, 1, bias_attr=False)
planes * self.expansion,
1,
bias_attr=False)
self.bn3 = norm_layer(planes * self.expansion) self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU() self.relu = nn.ReLU()
self.downsample = downsample self.downsample = downsample
...@@ -88,13 +79,7 @@ class BottleneckBlock(nn.Layer): ...@@ -88,13 +79,7 @@ class BottleneckBlock(nn.Layer):
class ResNet(nn.Layer): class ResNet(nn.Layer):
def __init__(self, block=BottleneckBlock, depth=101, with_pool=True): def __init__(self, block=BottleneckBlock, depth=101, with_pool=True):
super(ResNet, self).__init__() super(ResNet, self).__init__()
layer_cfg = { layer_cfg = {18: [2, 2, 2, 2], 34: [3, 4, 6, 3], 50: [3, 4, 6, 3], 101: [3, 4, 23, 3], 152: [3, 8, 36, 3]}
18: [2, 2, 2, 2],
34: [3, 4, 6, 3],
50: [3, 4, 6, 3],
101: [3, 4, 23, 3],
152: [3, 8, 36, 3]
}
layers = layer_cfg[depth] layers = layer_cfg[depth]
self.with_pool = with_pool self.with_pool = with_pool
self._norm_layer = nn.BatchNorm2D self._norm_layer = nn.BatchNorm2D
...@@ -102,12 +87,7 @@ class ResNet(nn.Layer): ...@@ -102,12 +87,7 @@ class ResNet(nn.Layer):
self.inplanes = 64 self.inplanes = 64
self.dilation = 1 self.dilation = 1
self.conv1 = nn.Conv2D(3, self.conv1 = nn.Conv2D(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias_attr=False)
self.inplanes,
kernel_size=7,
stride=2,
padding=3,
bias_attr=False)
self.bn1 = self._norm_layer(self.inplanes) self.bn1 = self._norm_layer(self.inplanes)
self.relu = nn.ReLU() self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2D(kernel_size=3, stride=2, padding=1) self.maxpool = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
...@@ -127,18 +107,12 @@ class ResNet(nn.Layer): ...@@ -127,18 +107,12 @@ class ResNet(nn.Layer):
stride = 1 stride = 1
if stride != 1 or self.inplanes != planes * block.expansion: if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential( downsample = nn.Sequential(
nn.Conv2D(self.inplanes, nn.Conv2D(self.inplanes, planes * block.expansion, 1, stride=stride, bias_attr=False),
planes * block.expansion,
1,
stride=stride,
bias_attr=False),
norm_layer(planes * block.expansion), norm_layer(planes * block.expansion),
) )
layers = [] layers = []
layers.append( layers.append(block(self.inplanes, planes, stride, downsample, 1, 64, previous_dilation, norm_layer))
block(self.inplanes, planes, stride, downsample, 1, 64,
previous_dilation, norm_layer))
self.inplanes = planes * block.expansion self.inplanes = planes * block.expansion
for _ in range(1, blocks): for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, norm_layer=norm_layer)) layers.append(block(self.inplanes, planes, norm_layer=norm_layer))
...@@ -161,7 +135,8 @@ class ResNet(nn.Layer): ...@@ -161,7 +135,8 @@ class ResNet(nn.Layer):
return x return x
@moduleinfo(name="spinalnet_res101_gemstone", @moduleinfo(
name="spinalnet_res101_gemstone",
type="CV/classification", type="CV/classification",
author="nanting03", author="nanting03",
author_email="975348977@qq.com", author_email="975348977@qq.com",
...@@ -194,20 +169,16 @@ class SpinalNet_ResNet101(nn.Layer): ...@@ -194,20 +169,16 @@ class SpinalNet_ResNet101(nn.Layer):
self.half_in_size = half_in_size self.half_in_size = half_in_size
self.fc_spinal_layer1 = nn.Sequential( self.fc_spinal_layer1 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size, layer_width), nn.Dropout(p=0.5), nn.Linear(half_in_size, layer_width), nn.BatchNorm1D(layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_spinal_layer2 = nn.Sequential( self.fc_spinal_layer2 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, layer_width), nn.BatchNorm1D(layer_width),
layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_spinal_layer3 = nn.Sequential( self.fc_spinal_layer3 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, layer_width), nn.BatchNorm1D(layer_width),
layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_spinal_layer4 = nn.Sequential( self.fc_spinal_layer4 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, layer_width), nn.BatchNorm1D(layer_width),
layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_out = nn.Sequential( self.fc_out = nn.Sequential(
nn.Dropout(p=0.5), nn.Dropout(p=0.5),
nn.Linear(layer_width * 4, class_dim), nn.Linear(layer_width * 4, class_dim),
...@@ -219,8 +190,7 @@ class SpinalNet_ResNet101(nn.Layer): ...@@ -219,8 +190,7 @@ class SpinalNet_ResNet101(nn.Layer):
print("load custom checkpoint success") print("load custom checkpoint success")
else: else:
checkpoint = os.path.join(self.directory, checkpoint = os.path.join(self.directory, 'spinalnet_res101.pdparams')
'spinalnet_res101.pdparams')
self.model_dict = paddle.load(checkpoint) self.model_dict = paddle.load(checkpoint)
self.set_dict(self.model_dict) self.set_dict(self.model_dict)
print("load pretrained checkpoint success") print("load pretrained checkpoint success")
...@@ -240,14 +210,9 @@ class SpinalNet_ResNet101(nn.Layer): ...@@ -240,14 +210,9 @@ class SpinalNet_ResNet101(nn.Layer):
y = paddle.flatten(y, 1) y = paddle.flatten(y, 1)
y1 = self.fc_spinal_layer1(y[:, 0:self.half_in_size]) y1 = self.fc_spinal_layer1(y[:, 0:self.half_in_size])
y2 = self.fc_spinal_layer2( y2 = self.fc_spinal_layer2(paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y1], axis=1))
paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y1], y3 = self.fc_spinal_layer3(paddle.concat([y[:, 0:self.half_in_size], y2], axis=1))
axis=1)) y4 = self.fc_spinal_layer4(paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y3], axis=1))
y3 = self.fc_spinal_layer3(
paddle.concat([y[:, 0:self.half_in_size], y2], axis=1))
y4 = self.fc_spinal_layer4(
paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y3],
axis=1))
y = paddle.concat([y1, y2, y3, y4], axis=1) y = paddle.concat([y1, y2, y3, y4], axis=1)
......
...@@ -43,20 +43,11 @@ class BottleneckBlock(nn.Layer): ...@@ -43,20 +43,11 @@ class BottleneckBlock(nn.Layer):
self.conv1 = nn.Conv2D(inplanes, width, 1, bias_attr=False) self.conv1 = nn.Conv2D(inplanes, width, 1, bias_attr=False)
self.bn1 = norm_layer(width) self.bn1 = norm_layer(width)
self.conv2 = nn.Conv2D(width, self.conv2 = nn.Conv2D(
width, width, width, 3, padding=dilation, stride=stride, groups=groups, dilation=dilation, bias_attr=False)
3,
padding=dilation,
stride=stride,
groups=groups,
dilation=dilation,
bias_attr=False)
self.bn2 = norm_layer(width) self.bn2 = norm_layer(width)
self.conv3 = nn.Conv2D(width, self.conv3 = nn.Conv2D(width, planes * self.expansion, 1, bias_attr=False)
planes * self.expansion,
1,
bias_attr=False)
self.bn3 = norm_layer(planes * self.expansion) self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU() self.relu = nn.ReLU()
self.downsample = downsample self.downsample = downsample
...@@ -88,13 +79,7 @@ class BottleneckBlock(nn.Layer): ...@@ -88,13 +79,7 @@ class BottleneckBlock(nn.Layer):
class ResNet(nn.Layer): class ResNet(nn.Layer):
def __init__(self, block=BottleneckBlock, depth=50, with_pool=True): def __init__(self, block=BottleneckBlock, depth=50, with_pool=True):
super(ResNet, self).__init__() super(ResNet, self).__init__()
layer_cfg = { layer_cfg = {18: [2, 2, 2, 2], 34: [3, 4, 6, 3], 50: [3, 4, 6, 3], 101: [3, 4, 23, 3], 152: [3, 8, 36, 3]}
18: [2, 2, 2, 2],
34: [3, 4, 6, 3],
50: [3, 4, 6, 3],
101: [3, 4, 23, 3],
152: [3, 8, 36, 3]
}
layers = layer_cfg[depth] layers = layer_cfg[depth]
self.with_pool = with_pool self.with_pool = with_pool
self._norm_layer = nn.BatchNorm2D self._norm_layer = nn.BatchNorm2D
...@@ -102,12 +87,7 @@ class ResNet(nn.Layer): ...@@ -102,12 +87,7 @@ class ResNet(nn.Layer):
self.inplanes = 64 self.inplanes = 64
self.dilation = 1 self.dilation = 1
self.conv1 = nn.Conv2D(3, self.conv1 = nn.Conv2D(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias_attr=False)
self.inplanes,
kernel_size=7,
stride=2,
padding=3,
bias_attr=False)
self.bn1 = self._norm_layer(self.inplanes) self.bn1 = self._norm_layer(self.inplanes)
self.relu = nn.ReLU() self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2D(kernel_size=3, stride=2, padding=1) self.maxpool = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
...@@ -127,18 +107,12 @@ class ResNet(nn.Layer): ...@@ -127,18 +107,12 @@ class ResNet(nn.Layer):
stride = 1 stride = 1
if stride != 1 or self.inplanes != planes * block.expansion: if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential( downsample = nn.Sequential(
nn.Conv2D(self.inplanes, nn.Conv2D(self.inplanes, planes * block.expansion, 1, stride=stride, bias_attr=False),
planes * block.expansion,
1,
stride=stride,
bias_attr=False),
norm_layer(planes * block.expansion), norm_layer(planes * block.expansion),
) )
layers = [] layers = []
layers.append( layers.append(block(self.inplanes, planes, stride, downsample, 1, 64, previous_dilation, norm_layer))
block(self.inplanes, planes, stride, downsample, 1, 64,
previous_dilation, norm_layer))
self.inplanes = planes * block.expansion self.inplanes = planes * block.expansion
for _ in range(1, blocks): for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, norm_layer=norm_layer)) layers.append(block(self.inplanes, planes, norm_layer=norm_layer))
...@@ -161,7 +135,8 @@ class ResNet(nn.Layer): ...@@ -161,7 +135,8 @@ class ResNet(nn.Layer):
return x return x
@moduleinfo(name="spinalnet_res50_gemstone", @moduleinfo(
name="spinalnet_res50_gemstone",
type="CV/classification", type="CV/classification",
author="nanting03", author="nanting03",
author_email="975348977@qq.com", author_email="975348977@qq.com",
...@@ -194,20 +169,16 @@ class SpinalNet_ResNet50(nn.Layer): ...@@ -194,20 +169,16 @@ class SpinalNet_ResNet50(nn.Layer):
self.half_in_size = half_in_size self.half_in_size = half_in_size
self.fc_spinal_layer1 = nn.Sequential( self.fc_spinal_layer1 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size, layer_width), nn.Dropout(p=0.5), nn.Linear(half_in_size, layer_width), nn.BatchNorm1D(layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_spinal_layer2 = nn.Sequential( self.fc_spinal_layer2 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, layer_width), nn.BatchNorm1D(layer_width),
layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_spinal_layer3 = nn.Sequential( self.fc_spinal_layer3 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, layer_width), nn.BatchNorm1D(layer_width),
layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_spinal_layer4 = nn.Sequential( self.fc_spinal_layer4 = nn.Sequential(
nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, nn.Dropout(p=0.5), nn.Linear(half_in_size + layer_width, layer_width), nn.BatchNorm1D(layer_width),
layer_width), nn.ReLU())
nn.BatchNorm1D(layer_width), nn.ReLU())
self.fc_out = nn.Sequential( self.fc_out = nn.Sequential(
nn.Dropout(p=0.5), nn.Dropout(p=0.5),
nn.Linear(layer_width * 4, class_dim), nn.Linear(layer_width * 4, class_dim),
...@@ -219,8 +190,7 @@ class SpinalNet_ResNet50(nn.Layer): ...@@ -219,8 +190,7 @@ class SpinalNet_ResNet50(nn.Layer):
print("load custom checkpoint success") print("load custom checkpoint success")
else: else:
checkpoint = os.path.join(self.directory, checkpoint = os.path.join(self.directory, 'spinalnet_res50.pdparams')
'spinalnet_res50.pdparams')
self.model_dict = paddle.load(checkpoint) self.model_dict = paddle.load(checkpoint)
self.set_dict(self.model_dict) self.set_dict(self.model_dict)
print("load pretrained checkpoint success") print("load pretrained checkpoint success")
...@@ -240,14 +210,9 @@ class SpinalNet_ResNet50(nn.Layer): ...@@ -240,14 +210,9 @@ class SpinalNet_ResNet50(nn.Layer):
y = paddle.flatten(y, 1) y = paddle.flatten(y, 1)
y1 = self.fc_spinal_layer1(y[:, 0:self.half_in_size]) y1 = self.fc_spinal_layer1(y[:, 0:self.half_in_size])
y2 = self.fc_spinal_layer2( y2 = self.fc_spinal_layer2(paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y1], axis=1))
paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y1], y3 = self.fc_spinal_layer3(paddle.concat([y[:, 0:self.half_in_size], y2], axis=1))
axis=1)) y4 = self.fc_spinal_layer4(paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y3], axis=1))
y3 = self.fc_spinal_layer3(
paddle.concat([y[:, 0:self.half_in_size], y2], axis=1))
y4 = self.fc_spinal_layer4(
paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y3],
axis=1))
y = paddle.concat([y1, y2, y3, y4], axis=1) y = paddle.concat([y1, y2, y3, y4], axis=1)
......
...@@ -61,16 +61,9 @@ def make_layers(cfg, batch_norm=False): ...@@ -61,16 +61,9 @@ def make_layers(cfg, batch_norm=False):
cfgs = { cfgs = {
'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'B': 'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
[64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'D': [ 'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512,
512, 512, 'M'
],
'E': [
64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512,
'M', 512, 512, 512, 512, 'M'
],
} }
...@@ -86,7 +79,8 @@ def vgg16(batch_norm=False, **kwargs): ...@@ -86,7 +79,8 @@ def vgg16(batch_norm=False, **kwargs):
return _vgg(model_name, 'D', batch_norm, **kwargs) return _vgg(model_name, 'D', batch_norm, **kwargs)
@moduleinfo(name="spinalnet_vgg16_gemstone", @moduleinfo(
name="spinalnet_vgg16_gemstone",
type="CV/classification", type="CV/classification",
author="nanting03", author="nanting03",
author_email="975348977@qq.com", author_email="975348977@qq.com",
...@@ -142,8 +136,7 @@ class SpinalNet_VGG16(nn.Layer): ...@@ -142,8 +136,7 @@ class SpinalNet_VGG16(nn.Layer):
nn.BatchNorm1D(layer_width), nn.BatchNorm1D(layer_width),
nn.ReLU(), nn.ReLU(),
) )
self.fc_out = nn.Sequential(nn.Dropout(p=0.5), self.fc_out = nn.Sequential(nn.Dropout(p=0.5), nn.Linear(layer_width * 4, class_dim))
nn.Linear(layer_width * 4, class_dim))
if load_checkpoint is not None: if load_checkpoint is not None:
self.model_dict = paddle.load(load_checkpoint)[0] self.model_dict = paddle.load(load_checkpoint)[0]
...@@ -151,8 +144,7 @@ class SpinalNet_VGG16(nn.Layer): ...@@ -151,8 +144,7 @@ class SpinalNet_VGG16(nn.Layer):
print("load custom checkpoint success") print("load custom checkpoint success")
else: else:
checkpoint = os.path.join(self.directory, checkpoint = os.path.join(self.directory, 'spinalnet_vgg16.pdparams')
'spinalnet_vgg16.pdparams')
self.model_dict = paddle.load(checkpoint) self.model_dict = paddle.load(checkpoint)
self.set_dict(self.model_dict) self.set_dict(self.model_dict)
print("load pretrained checkpoint success") print("load pretrained checkpoint success")
...@@ -173,14 +165,9 @@ class SpinalNet_VGG16(nn.Layer): ...@@ -173,14 +165,9 @@ class SpinalNet_VGG16(nn.Layer):
y = paddle.flatten(y, 1) y = paddle.flatten(y, 1)
y1 = self.fc_spinal_layer1(y[:, 0:self.half_in_size]) y1 = self.fc_spinal_layer1(y[:, 0:self.half_in_size])
y2 = self.fc_spinal_layer2( y2 = self.fc_spinal_layer2(paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y1], axis=1))
paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y1], y3 = self.fc_spinal_layer3(paddle.concat([y[:, 0:self.half_in_size], y2], axis=1))
axis=1)) y4 = self.fc_spinal_layer4(paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y3], axis=1))
y3 = self.fc_spinal_layer3(
paddle.concat([y[:, 0:self.half_in_size], y2], axis=1))
y4 = self.fc_spinal_layer4(
paddle.concat([y[:, self.half_in_size:2 * self.half_in_size], y3],
axis=1))
y = paddle.concat([y1, y2, y3, y4], axis=1) y = paddle.concat([y1, y2, y3, y4], axis=1)
......
...@@ -41,18 +41,10 @@ def read_images(paths): ...@@ -41,18 +41,10 @@ def read_images(paths):
version='1.0.0') version='1.0.0')
class MODULE(hub.Module): class MODULE(hub.Module):
def _initialize(self, **kwargs): def _initialize(self, **kwargs):
self.default_pretrained_model_path = os.path.join( self.default_pretrained_model_path = os.path.join(self.directory, 'assets')
self.directory, 'assets') self.model = pdx.deploy.Predictor(self.default_pretrained_model_path, **kwargs)
self.model = pdx.deploy.Predictor(self.default_pretrained_model_path,
**kwargs) def predict(self, images=None, paths=None, data=None, batch_size=1, use_gpu=False, **kwargs):
def predict(self,
images=None,
paths=None,
data=None,
batch_size=1,
use_gpu=False,
**kwargs):
all_data = images if images is not None else read_images(paths) all_data = images if images is not None else read_images(paths)
total_num = len(all_data) total_num = len(all_data)
...@@ -110,36 +102,27 @@ class MODULE(hub.Module): ...@@ -110,36 +102,27 @@ class MODULE(hub.Module):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(paths=[args.input_path], use_gpu=args.use_gpu)
paths=[args.input_path],
use_gpu=args.use_gpu)
return results return results
def add_module_config_arg(self): def add_module_config_arg(self):
""" """
Add the command config options. Add the command config options.
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--use_gpu', type=bool, default=False, help="whether use GPU or not")
'--use_gpu',
type=bool,
default=False,
help="whether use GPU or not")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
if __name__ == '__main__': if __name__ == '__main__':
module = MODULE(directory='./new_model') module = MODULE(directory='./new_model')
......
...@@ -37,22 +37,15 @@ def read_images(paths): ...@@ -37,22 +37,15 @@ def read_images(paths):
type='cv/classification', type='cv/classification',
author='郑博培、彭兆帅', author='郑博培、彭兆帅',
author_email='2733821739@qq.com, 1084667371@qq.com', author_email='2733821739@qq.com, 1084667371@qq.com',
summary='The model uses convolution neural network to tell you the key to identify marine fish, so that anyone can call out the names of the creatures.', summary=
'The model uses convolution neural network to tell you the key to identify marine fish, so that anyone can call out the names of the creatures.',
version='1.0.0') version='1.0.0')
class MODULE(hub.Module): class MODULE(hub.Module):
def _initialize(self, **kwargs): def _initialize(self, **kwargs):
self.default_pretrained_model_path = os.path.join( self.default_pretrained_model_path = os.path.join(self.directory, 'assets')
self.directory, 'assets') self.model = pdx.deploy.Predictor(self.default_pretrained_model_path, **kwargs)
self.model = pdx.deploy.Predictor(self.default_pretrained_model_path,
**kwargs) def predict(self, images=None, paths=None, data=None, batch_size=1, use_gpu=False, **kwargs):
def predict(self,
images=None,
paths=None,
data=None,
batch_size=1,
use_gpu=False,
**kwargs):
all_data = images if images is not None else read_images(paths) all_data = images if images is not None else read_images(paths)
total_num = len(all_data) total_num = len(all_data)
...@@ -110,36 +103,27 @@ class MODULE(hub.Module): ...@@ -110,36 +103,27 @@ class MODULE(hub.Module):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(paths=[args.input_path], use_gpu=args.use_gpu)
paths=[args.input_path],
use_gpu=args.use_gpu)
return results return results
def add_module_config_arg(self): def add_module_config_arg(self):
""" """
Add the command config options. Add the command config options.
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--use_gpu', type=bool, default=False, help="whether use GPU or not")
'--use_gpu',
type=bool,
default=False,
help="whether use GPU or not")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
if __name__ == '__main__': if __name__ == '__main__':
module = MODULE(directory='./new_model') module = MODULE(directory='./new_model')
......
...@@ -34,29 +34,35 @@ def read_images(paths): ...@@ -34,29 +34,35 @@ def read_images(paths):
images.append(cv2.imread(path)) images.append(cv2.imread(path))
return images return images
'''旋转图像并剪裁''' '''旋转图像并剪裁'''
def rotate( def rotate(
img, # 图片 img, # 图片
pt1, pt2, pt3, pt4, pt1,
pt2,
pt3,
pt4,
imgOutSrc): imgOutSrc):
# print(pt1,pt2,pt3,pt4) # print(pt1,pt2,pt3,pt4)
withRect = math.sqrt((pt4[0] - pt1[0]) ** 2 + (pt4[1] - pt1[1]) ** 2) # 矩形框的宽度 withRect = math.sqrt((pt4[0] - pt1[0])**2 + (pt4[1] - pt1[1])**2) # 矩形框的宽度
heightRect = math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) **2) heightRect = math.sqrt((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2)
# print("矩形的宽度",withRect, "矩形的高度", heightRect) # print("矩形的宽度",withRect, "矩形的高度", heightRect)
angle = acos((pt4[0] - pt1[0]) / withRect) * (180 / math.pi) # 矩形框旋转角度 angle = acos((pt4[0] - pt1[0]) / withRect) * (180 / math.pi) # 矩形框旋转角度
# print("矩形框旋转角度", angle) # print("矩形框旋转角度", angle)
if withRect > heightRect: if withRect > heightRect:
if pt4[1]>pt1[1]: if pt4[1] > pt1[1]:
pass pass
# print("顺时针旋转") # print("顺时针旋转")
else: else:
# print("逆时针旋转") # print("逆时针旋转")
angle=-angle angle = -angle
else: else:
# print("逆时针旋转") # print("逆时针旋转")
angle=90 - angle angle = 90 - angle
height = img.shape[0] # 原始图像高度 height = img.shape[0] # 原始图像高度
width = img.shape[1] # 原始图像宽度 width = img.shape[1] # 原始图像宽度
...@@ -76,14 +82,15 @@ def rotate( ...@@ -76,14 +82,15 @@ def rotate(
[[pt4[0]], [pt4[1]]] = np.dot(rotateMat, np.array([[pt4[0]], [pt4[1]], [1]])) [[pt4[0]], [pt4[1]]] = np.dot(rotateMat, np.array([[pt4[0]], [pt4[1]], [1]]))
# 处理反转的情况 # 处理反转的情况
if pt2[1]>pt4[1]: if pt2[1] > pt4[1]:
pt2[1],pt4[1]=pt4[1],pt2[1] pt2[1], pt4[1] = pt4[1], pt2[1]
if pt1[0]>pt3[0]: if pt1[0] > pt3[0]:
pt1[0],pt3[0]=pt3[0],pt1[0] pt1[0], pt3[0] = pt3[0], pt1[0]
imgOut = imgRotation[int(pt2[1]):int(pt4[1]), int(pt1[0]):int(pt3[0])] imgOut = imgRotation[int(pt2[1]):int(pt4[1]), int(pt1[0]):int(pt3[0])]
cv2.imwrite(imgOutSrc, imgOut) # 裁减得到的旋转矩形框 cv2.imwrite(imgOutSrc, imgOut) # 裁减得到的旋转矩形框
@moduleinfo( @moduleinfo(
name='WatermeterSegmentation', name='WatermeterSegmentation',
type='CV/semantic_segmentatio', type='CV/semantic_segmentatio',
...@@ -93,18 +100,10 @@ def rotate( ...@@ -93,18 +100,10 @@ def rotate(
version='1.0.0') version='1.0.0')
class MODULE(hub.Module): class MODULE(hub.Module):
def _initialize(self, **kwargs): def _initialize(self, **kwargs):
self.default_pretrained_model_path = os.path.join( self.default_pretrained_model_path = os.path.join(self.directory, 'assets')
self.directory, 'assets') self.model = pdx.deploy.Predictor(self.default_pretrained_model_path, **kwargs)
self.model = pdx.deploy.Predictor(self.default_pretrained_model_path,
**kwargs) def predict(self, images=None, paths=None, data=None, batch_size=1, use_gpu=False, **kwargs):
def predict(self,
images=None,
paths=None,
data=None,
batch_size=1,
use_gpu=False,
**kwargs):
all_data = images if images is not None else read_images(paths) all_data = images if images is not None else read_images(paths)
total_num = len(all_data) total_num = len(all_data)
...@@ -134,7 +133,7 @@ class MODULE(hub.Module): ...@@ -134,7 +133,7 @@ class MODULE(hub.Module):
print(type(contours[0])) print(type(contours[0]))
n = 0 n = 0
m = 0 m = 0
for index,contour in enumerate(contours): for index, contour in enumerate(contours):
if len(contour) > n: if len(contour) > n:
n = len(contour) n = len(contour)
m = index m = index
...@@ -181,7 +180,6 @@ class MODULE(hub.Module): ...@@ -181,7 +180,6 @@ class MODULE(hub.Module):
res.append(result) res.append(result)
return res return res
@runnable @runnable
def run_cmd(self, argvs): def run_cmd(self, argvs):
""" """
...@@ -192,36 +190,27 @@ class MODULE(hub.Module): ...@@ -192,36 +190,27 @@ class MODULE(hub.Module):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(paths=[args.input_path], use_gpu=args.use_gpu)
paths=[args.input_path],
use_gpu=args.use_gpu)
return results return results
def add_module_config_arg(self): def add_module_config_arg(self):
""" """
Add the command config options. Add the command config options.
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--use_gpu', type=bool, default=False, help="whether use GPU or not")
'--use_gpu',
type=bool,
default=False,
help="whether use GPU or not")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
if __name__ == '__main__': if __name__ == '__main__':
module = MODULE(directory='./new_model') module = MODULE(directory='./new_model')
......
...@@ -13,16 +13,13 @@ from paddlehub.module.module import moduleinfo, serving ...@@ -13,16 +13,13 @@ from paddlehub.module.module import moduleinfo, serving
author="jm12138", author="jm12138",
author_email="", author_email="",
summary="Vehicle_License_Plate_Recognition", summary="Vehicle_License_Plate_Recognition",
version="1.0.0" version="1.0.0")
)
class Vehicle_License_Plate_Recognition(nn.Layer): class Vehicle_License_Plate_Recognition(nn.Layer):
def __init__(self): def __init__(self):
super(Vehicle_License_Plate_Recognition, self).__init__() super(Vehicle_License_Plate_Recognition, self).__init__()
self.vlpr = PaddleOCR( self.vlpr = PaddleOCR(
det_model_dir=os.path.join(self.directory, 'det_vlpr'), det_model_dir=os.path.join(self.directory, 'det_vlpr'),
rec_model_dir=os.path.join( rec_model_dir=os.path.join(self.directory, 'ch_ppocr_server_v2.0_rec_infer'))
self.directory, 'ch_ppocr_server_v2.0_rec_infer')
)
@staticmethod @staticmethod
def base64_to_cv2(b64str): def base64_to_cv2(b64str):
...@@ -38,17 +35,11 @@ class Vehicle_License_Plate_Recognition(nn.Layer): ...@@ -38,17 +35,11 @@ class Vehicle_License_Plate_Recognition(nn.Layer):
if isinstance(images, list): if isinstance(images, list):
for item in images: for item in images:
for bbox, text in self.vlpr.ocr(item): for bbox, text in self.vlpr.ocr(item):
results.append({ results.append({'license': text[0], 'bbox': bbox})
'license': text[0],
'bbox': bbox
})
elif isinstance(images, (str, np.ndarray)): elif isinstance(images, (str, np.ndarray)):
for bbox, text in self.vlpr.ocr(images): for bbox, text in self.vlpr.ocr(images):
results.append({ results.append({'license': text[0], 'bbox': bbox})
'license': text[0],
'bbox': bbox
})
return results return results
......
...@@ -38,18 +38,15 @@ class GPT2_Base_CN(nn.Layer): ...@@ -38,18 +38,15 @@ class GPT2_Base_CN(nn.Layer):
# 初始预测 # 初始预测
ids = self.tokenizer.encode(text) ids = self.tokenizer.encode(text)
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array(ids).reshape(1, -1).astype('int64'))
np.array(ids).reshape(1, -1).astype('int64'))
output, cached_kvs = self.model(input_id, use_cache=True) output, cached_kvs = self.model(input_id, use_cache=True)
next_token = int(np.argmax(output[0, -1].numpy())) next_token = int(np.argmax(output[0, -1].numpy()))
ids.append(next_token) ids.append(next_token)
# 使用缓存进行继续预测 # 使用缓存进行继续预测
for i in range(max_len-1): for i in range(max_len - 1):
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array([next_token]).reshape(1, -1).astype('int64'))
np.array([next_token]).reshape(1, -1).astype('int64')) output, cached_kvs = self.model(input_id, use_cache=True, cache=cached_kvs)
output, cached_kvs = self.model(
input_id, use_cache=True, cache=cached_kvs)
next_token = int(np.argmax(output[0, -1].numpy())) next_token = int(np.argmax(output[0, -1].numpy()))
ids.append(next_token) ids.append(next_token)
...@@ -79,14 +76,12 @@ class GPT2_Base_CN(nn.Layer): ...@@ -79,14 +76,12 @@ class GPT2_Base_CN(nn.Layer):
if top_p < 1.0: if top_p < 1.0:
sorted_logits = paddle.sort(logits, descending=True) sorted_logits = paddle.sort(logits, descending=True)
sorted_indices = paddle.argsort(logits, descending=True).numpy() sorted_indices = paddle.argsort(logits, descending=True).numpy()
cumulative_probs = paddle.cumsum(paddle.nn.functional.softmax( cumulative_probs = paddle.cumsum(paddle.nn.functional.softmax(sorted_logits, axis=-1), axis=-1).numpy()
sorted_logits, axis=-1), axis=-1).numpy()
# Remove tokens with cumulative probability above the threshold # Remove tokens with cumulative probability above the threshold
sorted_indices_to_remove = cumulative_probs > top_p sorted_indices_to_remove = cumulative_probs > top_p
# Shift the indices to the right to keep also the first token above the threshold # Shift the indices to the right to keep also the first token above the threshold
sorted_indices_to_remove[..., sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1]
1:] = sorted_indices_to_remove[..., :-1]
sorted_indices_to_remove[..., 0] = 0 sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices[sorted_indices_to_remove] indices_to_remove = sorted_indices[sorted_indices_to_remove]
...@@ -94,7 +89,14 @@ class GPT2_Base_CN(nn.Layer): ...@@ -94,7 +89,14 @@ class GPT2_Base_CN(nn.Layer):
return paddle.to_tensor(logits_np) return paddle.to_tensor(logits_np)
def nucleus_sample(self, text, max_len=32, end_word=None, repitition_penalty=1.0, temperature=1.0, top_k=0, top_p=1.0): def nucleus_sample(self,
text,
max_len=32,
end_word=None,
repitition_penalty=1.0,
temperature=1.0,
top_k=0,
top_p=1.0):
with paddle.no_grad(): with paddle.no_grad():
# 终止标志 # 终止标志
if end_word is not None: if end_word is not None:
...@@ -103,33 +105,28 @@ class GPT2_Base_CN(nn.Layer): ...@@ -103,33 +105,28 @@ class GPT2_Base_CN(nn.Layer):
# 初始预测 # 初始预测
ids = self.tokenizer.encode(text) ids = self.tokenizer.encode(text)
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array(ids).reshape(1, -1).astype('int64'))
np.array(ids).reshape(1, -1).astype('int64'))
output, cached_kvs = self.model(input_id, use_cache=True) output, cached_kvs = self.model(input_id, use_cache=True)
next_token_logits = output[0, -1, :] next_token_logits = output[0, -1, :]
for id in set(ids): for id in set(ids):
next_token_logits[id] /= repitition_penalty next_token_logits[id] /= repitition_penalty
next_token_logits = next_token_logits / temperature next_token_logits = next_token_logits / temperature
filtered_logits = self.top_k_top_p_filtering( filtered_logits = self.top_k_top_p_filtering(next_token_logits, top_k=top_k, top_p=top_p)
next_token_logits, top_k=top_k, top_p=top_p) next_token = paddle.multinomial(
next_token = paddle.multinomial(paddle.nn.functional.softmax( paddle.nn.functional.softmax(filtered_logits, axis=-1), num_samples=1).numpy()
filtered_logits, axis=-1), num_samples=1).numpy()
ids += [int(next_token)] ids += [int(next_token)]
# 使用缓存进行继续预测 # 使用缓存进行继续预测
for i in range(max_len-1): for i in range(max_len - 1):
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array([next_token]).reshape(1, -1).astype('int64'))
np.array([next_token]).reshape(1, -1).astype('int64')) output, cached_kvs = self.model(input_id, use_cache=True, cache=cached_kvs)
output, cached_kvs = self.model(
input_id, use_cache=True, cache=cached_kvs)
next_token_logits = output[0, -1, :] next_token_logits = output[0, -1, :]
for id in set(ids): for id in set(ids):
next_token_logits[id] /= repitition_penalty next_token_logits[id] /= repitition_penalty
next_token_logits = next_token_logits / temperature next_token_logits = next_token_logits / temperature
filtered_logits = self.top_k_top_p_filtering( filtered_logits = self.top_k_top_p_filtering(next_token_logits, top_k=top_k, top_p=top_p)
next_token_logits, top_k=top_k, top_p=top_p) next_token = paddle.multinomial(
next_token = paddle.multinomial(paddle.nn.functional.softmax( paddle.nn.functional.softmax(filtered_logits, axis=-1), num_samples=1).numpy()
filtered_logits, axis=-1), num_samples=1).numpy()
ids += [int(next_token)] ids += [int(next_token)]
# 根据终止标志停止预测 # 根据终止标志停止预测
......
...@@ -30,13 +30,11 @@ class GPT2_CPM_LM(nn.Layer): ...@@ -30,13 +30,11 @@ class GPT2_CPM_LM(nn.Layer):
max_position_embeddings=1024, max_position_embeddings=1024,
type_vocab_size=1, type_vocab_size=1,
initializer_range=0.02, initializer_range=0.02,
pad_token_id=0 pad_token_id=0)
)
self.model = GPT2ForPretraining(gpt2) self.model = GPT2ForPretraining(gpt2)
# 读取CPM-LM模型参数(FP16) # 读取CPM-LM模型参数(FP16)
state_dict = paddle.load(os.path.join( state_dict = paddle.load(os.path.join(self.directory, 'CPM-LM.pdparams'))
self.directory, 'CPM-LM.pdparams'))
# FP16 -> FP32 # FP16 -> FP32
for param in state_dict: for param in state_dict:
...@@ -51,8 +49,7 @@ class GPT2_CPM_LM(nn.Layer): ...@@ -51,8 +49,7 @@ class GPT2_CPM_LM(nn.Layer):
# 加载编解码器 # 加载编解码器
self.tokenizer = GPT2ChineseTokenizer( self.tokenizer = GPT2ChineseTokenizer(
vocab_file=os.path.join(self.directory, 'vocab.json'), vocab_file=os.path.join(self.directory, 'vocab.json'),
model_file=os.path.join(self.directory, 'chinese_vocab.model') model_file=os.path.join(self.directory, 'chinese_vocab.model'))
)
# 初始化编码器 # 初始化编码器
_ = self.tokenizer.encode('_') _ = self.tokenizer.encode('_')
...@@ -67,18 +64,15 @@ class GPT2_CPM_LM(nn.Layer): ...@@ -67,18 +64,15 @@ class GPT2_CPM_LM(nn.Layer):
# 初始预测 # 初始预测
ids = self.tokenizer.encode(text) ids = self.tokenizer.encode(text)
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array(ids).reshape(1, -1).astype('int64'))
np.array(ids).reshape(1, -1).astype('int64'))
output, cached_kvs = self.model(input_id, use_cache=True) output, cached_kvs = self.model(input_id, use_cache=True)
next_token = int(np.argmax(output[0, -1].numpy())) next_token = int(np.argmax(output[0, -1].numpy()))
ids.append(next_token) ids.append(next_token)
# 使用缓存进行继续预测 # 使用缓存进行继续预测
for i in range(max_len-1): for i in range(max_len - 1):
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array([next_token]).reshape(1, -1).astype('int64'))
np.array([next_token]).reshape(1, -1).astype('int64')) output, cached_kvs = self.model(input_id, use_cache=True, cache=cached_kvs)
output, cached_kvs = self.model(
input_id, use_cache=True, cache=cached_kvs)
next_token = int(np.argmax(output[0, -1].numpy())) next_token = int(np.argmax(output[0, -1].numpy()))
ids.append(next_token) ids.append(next_token)
...@@ -108,14 +102,12 @@ class GPT2_CPM_LM(nn.Layer): ...@@ -108,14 +102,12 @@ class GPT2_CPM_LM(nn.Layer):
if top_p < 1.0: if top_p < 1.0:
sorted_logits = paddle.sort(logits, descending=True) sorted_logits = paddle.sort(logits, descending=True)
sorted_indices = paddle.argsort(logits, descending=True).numpy() sorted_indices = paddle.argsort(logits, descending=True).numpy()
cumulative_probs = paddle.cumsum(paddle.nn.functional.softmax( cumulative_probs = paddle.cumsum(paddle.nn.functional.softmax(sorted_logits, axis=-1), axis=-1).numpy()
sorted_logits, axis=-1), axis=-1).numpy()
# Remove tokens with cumulative probability above the threshold # Remove tokens with cumulative probability above the threshold
sorted_indices_to_remove = cumulative_probs > top_p sorted_indices_to_remove = cumulative_probs > top_p
# Shift the indices to the right to keep also the first token above the threshold # Shift the indices to the right to keep also the first token above the threshold
sorted_indices_to_remove[..., sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1]
1:] = sorted_indices_to_remove[..., :-1]
sorted_indices_to_remove[..., 0] = 0 sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices[sorted_indices_to_remove] indices_to_remove = sorted_indices[sorted_indices_to_remove]
...@@ -123,7 +115,14 @@ class GPT2_CPM_LM(nn.Layer): ...@@ -123,7 +115,14 @@ class GPT2_CPM_LM(nn.Layer):
return paddle.to_tensor(logits_np) return paddle.to_tensor(logits_np)
def nucleus_sample(self, text, max_len=32, end_word=None, repitition_penalty=1.0, temperature=1.0, top_k=0, top_p=1.0): def nucleus_sample(self,
text,
max_len=32,
end_word=None,
repitition_penalty=1.0,
temperature=1.0,
top_k=0,
top_p=1.0):
with paddle.no_grad(): with paddle.no_grad():
# 终止标志 # 终止标志
if end_word is not None: if end_word is not None:
...@@ -132,35 +131,30 @@ class GPT2_CPM_LM(nn.Layer): ...@@ -132,35 +131,30 @@ class GPT2_CPM_LM(nn.Layer):
# 初始预测 # 初始预测
ids = self.tokenizer.encode(text) ids = self.tokenizer.encode(text)
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array(ids).reshape(1, -1).astype('int64'))
np.array(ids).reshape(1, -1).astype('int64'))
output, cached_kvs = self.model(input_id, use_cache=True) output, cached_kvs = self.model(input_id, use_cache=True)
next_token_logits = output[0, -1, :] next_token_logits = output[0, -1, :]
for id in set(ids): for id in set(ids):
next_token_logits[id] /= repitition_penalty next_token_logits[id] /= repitition_penalty
next_token_logits = next_token_logits / temperature next_token_logits = next_token_logits / temperature
next_token_logits[self.tokenizer.encoder['<unk>']] = -float('Inf') next_token_logits[self.tokenizer.encoder['<unk>']] = -float('Inf')
filtered_logits = self.top_k_top_p_filtering( filtered_logits = self.top_k_top_p_filtering(next_token_logits, top_k=top_k, top_p=top_p)
next_token_logits, top_k=top_k, top_p=top_p) next_token = paddle.multinomial(
next_token = paddle.multinomial(paddle.nn.functional.softmax( paddle.nn.functional.softmax(filtered_logits, axis=-1), num_samples=1).numpy()
filtered_logits, axis=-1), num_samples=1).numpy()
ids += [int(next_token)] ids += [int(next_token)]
# 使用缓存进行继续预测 # 使用缓存进行继续预测
for i in range(max_len-1): for i in range(max_len - 1):
input_id = paddle.to_tensor( input_id = paddle.to_tensor(np.array([next_token]).reshape(1, -1).astype('int64'))
np.array([next_token]).reshape(1, -1).astype('int64')) output, cached_kvs = self.model(input_id, use_cache=True, cache=cached_kvs)
output, cached_kvs = self.model(
input_id, use_cache=True, cache=cached_kvs)
next_token_logits = output[0, -1, :] next_token_logits = output[0, -1, :]
for id in set(ids): for id in set(ids):
next_token_logits[id] /= repitition_penalty next_token_logits[id] /= repitition_penalty
next_token_logits = next_token_logits / temperature next_token_logits = next_token_logits / temperature
next_token_logits[self.tokenizer.encoder['<unk>']] = -float('Inf') next_token_logits[self.tokenizer.encoder['<unk>']] = -float('Inf')
filtered_logits = self.top_k_top_p_filtering( filtered_logits = self.top_k_top_p_filtering(next_token_logits, top_k=top_k, top_p=top_p)
next_token_logits, top_k=top_k, top_p=top_p) next_token = paddle.multinomial(
next_token = paddle.multinomial(paddle.nn.functional.softmax( paddle.nn.functional.softmax(filtered_logits, axis=-1), num_samples=1).numpy()
filtered_logits, axis=-1), num_samples=1).numpy()
ids += [int(next_token)] ids += [int(next_token)]
# 根据终止标志停止预测 # 根据终止标志停止预测
......
...@@ -4,41 +4,42 @@ from .skyfilter import SkyFilter ...@@ -4,41 +4,42 @@ from .skyfilter import SkyFilter
from paddlehub.module.module import moduleinfo from paddlehub.module.module import moduleinfo
@moduleinfo( @moduleinfo(name="SkyAR", type="CV/Video_editing", author="jm12138", author_email="", summary="SkyAR", version="1.0.0")
name="SkyAR",
type="CV/Video_editing",
author="jm12138",
author_email="",
summary="SkyAR",
version="1.0.0"
)
class SkyAR(nn.Layer): class SkyAR(nn.Layer):
def __init__(self, model_path=None): def __init__(self, model_path=None):
super(SkyAR, self).__init__() super(SkyAR, self).__init__()
self.imgs = ['cloudy', 'district9ship', 'floatingcastle', self.imgs = [
'galaxy', 'jupiter', 'rainy', 'sunny', 'sunset', 'supermoon'] 'cloudy', 'district9ship', 'floatingcastle', 'galaxy', 'jupiter', 'rainy', 'sunny', 'sunset', 'supermoon'
]
self.videos = ['thunderstorm'] self.videos = ['thunderstorm']
if model_path: if model_path:
self.model_path = model_path self.model_path = model_path
else: else:
self.model_path = os.path.join(self.directory, './ResNet50FCN') self.model_path = os.path.join(self.directory, './ResNet50FCN')
def MagicSky( def MagicSky(self,
self, video_path, save_path, config='jupiter', video_path,
is_rainy=False, preview_frames_num=0, is_video_sky=False, is_show=False, save_path,
skybox_img=None, skybox_video=None, rain_cap_path=None, config='jupiter',
halo_effect=True, auto_light_matching=False, is_rainy=False,
relighting_factor=0.8, recoloring_factor=0.5, skybox_center_crop=0.5 preview_frames_num=0,
): is_video_sky=False,
is_show=False,
skybox_img=None,
skybox_video=None,
rain_cap_path=None,
halo_effect=True,
auto_light_matching=False,
relighting_factor=0.8,
recoloring_factor=0.5,
skybox_center_crop=0.5):
if config in self.imgs: if config in self.imgs:
skybox_img = os.path.join( skybox_img = os.path.join(self.directory, 'skybox', '%s.jpg' % config)
self.directory, 'skybox', '%s.jpg' % config)
skybox_video = None skybox_video = None
is_video_sky = False is_video_sky = False
elif config in self.videos: elif config in self.videos:
skybox_img = None skybox_img = None
skybox_video = os.path.join( skybox_video = os.path.join(self.directory, 'skybox', '%s.mp4' % config)
self.directory, 'skybox', '%s.mp4' % config)
is_video_sky = True is_video_sky = True
elif skybox_img: elif skybox_img:
is_video_sky = False is_video_sky = False
...@@ -49,8 +50,7 @@ class SkyAR(nn.Layer): ...@@ -49,8 +50,7 @@ class SkyAR(nn.Layer):
raise 'please check your configs' raise 'please check your configs'
if not rain_cap_path: if not rain_cap_path:
rain_cap_path = os.path.join( rain_cap_path = os.path.join(self.directory, 'rain_streaks', 'videoplayback.mp4')
self.directory, 'rain_streaks', 'videoplayback.mp4')
skyfilter = SkyFilter( skyfilter = SkyFilter(
model_path=self.model_path, model_path=self.model_path,
...@@ -67,7 +67,6 @@ class SkyAR(nn.Layer): ...@@ -67,7 +67,6 @@ class SkyAR(nn.Layer):
skybox_video=skybox_video, skybox_video=skybox_video,
is_video=is_video_sky, is_video=is_video_sky,
is_rainy=is_rainy, is_rainy=is_rainy,
is_show=is_show is_show=is_show)
)
skyfilter.run(preview_frames_num) skyfilter.run(preview_frames_num)
...@@ -29,7 +29,7 @@ class Rain(): ...@@ -29,7 +29,7 @@ class Rain():
return rain_layer return rain_layer
def _create_haze_layer(self, rain_layer): def _create_haze_layer(self, rain_layer):
return 0.1*np.ones_like(rain_layer) return 0.1 * np.ones_like(rain_layer)
def forward(self, img): def forward(self, img):
# get input image size # get input image size
...@@ -54,7 +54,7 @@ class Rain(): ...@@ -54,7 +54,7 @@ class Rain():
img_out = 1 - (1 - rain_layer) * (1 - img) img_out = 1 - (1 - rain_layer) * (1 - img)
# gamma and light correction # gamma and light correction
img_out = self.light_correction*(img_out**self.gamma) img_out = self.light_correction * (img_out**self.gamma)
# check boundary # check boundary
img_out = np.clip(img_out, a_min=0, a_max=1.) img_out = np.clip(img_out, a_min=0, a_max=1.)
......
...@@ -6,10 +6,8 @@ from .utils import build_transformation_matrix, update_transformation_matrix, es ...@@ -6,10 +6,8 @@ from .utils import build_transformation_matrix, update_transformation_matrix, es
class SkyBox(): class SkyBox():
def __init__( def __init__(self, out_size, skybox_img, skybox_video, halo_effect, auto_light_matching, relighting_factor,
self, out_size, skybox_img, skybox_video, halo_effect, recoloring_factor, skybox_center_crop, rain_cap_path, is_video, is_rainy):
auto_light_matching, relighting_factor, recoloring_factor,
skybox_center_crop, rain_cap_path, is_video, is_rainy):
self.out_size_w, self.out_size_h = out_size self.out_size_w, self.out_size_h = out_size
...@@ -28,12 +26,7 @@ class SkyBox(): ...@@ -28,12 +26,7 @@ class SkyBox():
self.skybox_center_crop = skybox_center_crop self.skybox_center_crop = skybox_center_crop
self.load_skybox() self.load_skybox()
self.rainmodel = Rain( self.rainmodel = Rain(
rain_cap_path=rain_cap_path, rain_cap_path=rain_cap_path, rain_intensity=0.8, haze_intensity=0.0, gamma=1.0, light_correction=1.0)
rain_intensity=0.8,
haze_intensity=0.0,
gamma=1.0,
light_correction=1.0
)
# motion parameters # motion parameters
self.M = np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32) self.M = np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32)
...@@ -43,10 +36,8 @@ class SkyBox(): ...@@ -43,10 +36,8 @@ class SkyBox():
def tile_skybox_img(self, imgtile): def tile_skybox_img(self, imgtile):
screen_y1 = int(imgtile.shape[0] / 2 - self.out_size_h / 2) screen_y1 = int(imgtile.shape[0] / 2 - self.out_size_h / 2)
screen_x1 = int(imgtile.shape[1] / 2 - self.out_size_w / 2) screen_x1 = int(imgtile.shape[1] / 2 - self.out_size_w / 2)
imgtile = np.concatenate( imgtile = np.concatenate([imgtile[screen_y1:, :, :], imgtile[0:screen_y1, :, :]], axis=0)
[imgtile[screen_y1:, :, :], imgtile[0:screen_y1, :, :]], axis=0) imgtile = np.concatenate([imgtile[:, screen_x1:, :], imgtile[:, 0:screen_x1, :]], axis=1)
imgtile = np.concatenate(
[imgtile[:, screen_x1:, :], imgtile[:, 0:screen_x1, :]], axis=1)
return imgtile return imgtile
...@@ -57,12 +48,9 @@ class SkyBox(): ...@@ -57,12 +48,9 @@ class SkyBox():
skybox_img = cv2.imread(self.skybox_img, cv2.IMREAD_COLOR) skybox_img = cv2.imread(self.skybox_img, cv2.IMREAD_COLOR)
skybox_img = cv2.cvtColor(skybox_img, cv2.COLOR_BGR2RGB) skybox_img = cv2.cvtColor(skybox_img, cv2.COLOR_BGR2RGB)
self.skybox_img = cv2.resize( self.skybox_img = cv2.resize(skybox_img, (self.out_size_w, self.out_size_h))
skybox_img, (self.out_size_w, self.out_size_h))
cc = 1. / self.skybox_center_crop cc = 1. / self.skybox_center_crop
imgtile = cv2.resize( imgtile = cv2.resize(skybox_img, (int(cc * self.out_size_w), int(cc * self.out_size_h)))
skybox_img, (int(cc * self.out_size_w),
int(cc*self.out_size_h)))
self.skybox_imgx2 = self.tile_skybox_img(imgtile) self.skybox_imgx2 = self.tile_skybox_img(imgtile)
self.skybox_imgx2 = np.expand_dims(self.skybox_imgx2, axis=0) self.skybox_imgx2 = np.expand_dims(self.skybox_imgx2, axis=0)
...@@ -71,15 +59,11 @@ class SkyBox(): ...@@ -71,15 +59,11 @@ class SkyBox():
cap = cv2.VideoCapture(self.skybox_video) cap = cv2.VideoCapture(self.skybox_video)
m_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) m_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cc = 1. / self.skybox_center_crop cc = 1. / self.skybox_center_crop
self.skybox_imgx2 = np.zeros( self.skybox_imgx2 = np.zeros([m_frames, int(cc * self.out_size_h), int(cc * self.out_size_w), 3], np.uint8)
[m_frames, int(cc*self.out_size_h),
int(cc*self.out_size_w), 3], np.uint8)
for i in range(m_frames): for i in range(m_frames):
_, skybox_img = cap.read() _, skybox_img = cap.read()
skybox_img = cv2.cvtColor(skybox_img, cv2.COLOR_BGR2RGB) skybox_img = cv2.cvtColor(skybox_img, cv2.COLOR_BGR2RGB)
imgtile = cv2.resize( imgtile = cv2.resize(skybox_img, (int(cc * self.out_size_w), int(cc * self.out_size_h)))
skybox_img, (int(cc * self.out_size_w),
int(cc * self.out_size_h)))
skybox_imgx2 = self.tile_skybox_img(imgtile) skybox_imgx2 = self.tile_skybox_img(imgtile)
self.skybox_imgx2[i, :] = skybox_imgx2 self.skybox_imgx2[i, :] = skybox_imgx2
...@@ -87,8 +71,7 @@ class SkyBox(): ...@@ -87,8 +71,7 @@ class SkyBox():
r, eps = 20, 0.01 r, eps = 20, 0.01
refined_skymask = guidedfilter(img[:, :, 2], G_pred[:, :, 0], r, eps) refined_skymask = guidedfilter(img[:, :, 2], G_pred[:, :, 0], r, eps)
refined_skymask = np.stack( refined_skymask = np.stack([refined_skymask, refined_skymask, refined_skymask], axis=-1)
[refined_skymask, refined_skymask, refined_skymask], axis=-1)
return np.clip(refined_skymask, a_min=0, a_max=1) return np.clip(refined_skymask, a_min=0, a_max=1)
...@@ -98,14 +81,13 @@ class SkyBox(): ...@@ -98,14 +81,13 @@ class SkyBox():
nbgs, bgh, bgw, c = self.skybox_imgx2.shape nbgs, bgh, bgw, c = self.skybox_imgx2.shape
fetch_id = self.frame_id % nbgs fetch_id = self.frame_id % nbgs
skybg_warp = cv2.warpAffine( skybg_warp = cv2.warpAffine(
self.skybox_imgx2[fetch_id, :, :, :], self.M, self.skybox_imgx2[fetch_id, :, :, :], self.M, (bgw, bgh), borderMode=cv2.BORDER_WRAP)
(bgw, bgh), borderMode=cv2.BORDER_WRAP)
skybg = skybg_warp[0:self.out_size_h, 0:self.out_size_w, :] skybg = skybg_warp[0:self.out_size_h, 0:self.out_size_w, :]
self.frame_id += 1 self.frame_id += 1
return np.array(skybg, np.float32)/255. return np.array(skybg, np.float32) / 255.
def skybox_tracking(self, frame, frame_prev, skymask): def skybox_tracking(self, frame, frame_prev, skymask):
if np.mean(skymask) < 0.05: if np.mean(skymask) < 0.05:
...@@ -113,27 +95,25 @@ class SkyBox(): ...@@ -113,27 +95,25 @@ class SkyBox():
return np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32) return np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32)
prev_gray = cv2.cvtColor(frame_prev, cv2.COLOR_RGB2GRAY) prev_gray = cv2.cvtColor(frame_prev, cv2.COLOR_RGB2GRAY)
prev_gray = np.array(255*prev_gray, dtype=np.uint8) prev_gray = np.array(255 * prev_gray, dtype=np.uint8)
curr_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) curr_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
curr_gray = np.array(255*curr_gray, dtype=np.uint8) curr_gray = np.array(255 * curr_gray, dtype=np.uint8)
mask = np.array(skymask[:, :, 0] > 0.99, dtype=np.uint8) mask = np.array(skymask[:, :, 0] > 0.99, dtype=np.uint8)
template_size = int(0.05*mask.shape[0]) template_size = int(0.05 * mask.shape[0])
mask = cv2.erode(mask, np.ones([template_size, template_size])) mask = cv2.erode(mask, np.ones([template_size, template_size]))
# ShiTomasi corner detection # ShiTomasi corner detection
prev_pts = cv2.goodFeaturesToTrack( prev_pts = cv2.goodFeaturesToTrack(
prev_gray, mask=mask, maxCorners=200, prev_gray, mask=mask, maxCorners=200, qualityLevel=0.01, minDistance=30, blockSize=3)
qualityLevel=0.01, minDistance=30, blockSize=3)
if prev_pts is None: if prev_pts is None:
print('no feature point detected') print('no feature point detected')
return np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32) return np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32)
# Calculate optical flow (i.e. track feature points) # Calculate optical flow (i.e. track feature points)
curr_pts, status, err = cv2.calcOpticalFlowPyrLK( curr_pts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray, curr_gray, prev_pts, None)
prev_gray, curr_gray, prev_pts, None)
# Filter only valid points # Filter only valid points
idx = np.where(status == 1)[0] idx = np.where(status == 1)[0]
if idx.size == 0: if idx.size == 0:
...@@ -147,15 +127,14 @@ class SkyBox(): ...@@ -147,15 +127,14 @@ class SkyBox():
return np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32) return np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32)
# limit the motion to translation + rotation # limit the motion to translation + rotation
dxdyda = estimate_partial_transform(( dxdyda = estimate_partial_transform((np.array(prev_pts), np.array(curr_pts)))
np.array(prev_pts), np.array(curr_pts)))
m = build_transformation_matrix(dxdyda) m = build_transformation_matrix(dxdyda)
return m return m
def relighting(self, img, skybg, skymask): def relighting(self, img, skybg, skymask):
# color matching, reference: skybox_img # color matching, reference: skybox_img
step = int(img.shape[0]/20) step = int(img.shape[0] / 20)
skybg_thumb = skybg[::step, ::step, :] skybg_thumb = skybg[::step, ::step, :]
img_thumb = img[::step, ::step, :] img_thumb = img[::step, ::step, :]
skymask_thumb = skymask[::step, ::step, :] skymask_thumb = skymask[::step, ::step, :]
...@@ -163,7 +142,7 @@ class SkyBox(): ...@@ -163,7 +142,7 @@ class SkyBox():
img_mean = np.sum(img_thumb * (1-skymask_thumb), axis=(0, 1), keepdims=True) \ img_mean = np.sum(img_thumb * (1-skymask_thumb), axis=(0, 1), keepdims=True) \
/ ((1-skymask_thumb).sum(axis=(0, 1), keepdims=True) + 1e-9) / ((1-skymask_thumb).sum(axis=(0, 1), keepdims=True) + 1e-9)
diff = skybg_mean - img_mean diff = skybg_mean - img_mean
img_colortune = img + self.recoloring_factor*diff img_colortune = img + self.recoloring_factor * diff
if self.auto_light_matching: if self.auto_light_matching:
img = img_colortune img = img_colortune
...@@ -176,11 +155,9 @@ class SkyBox(): ...@@ -176,11 +155,9 @@ class SkyBox():
def halo(self, syneth, skybg, skymask): def halo(self, syneth, skybg, skymask):
# reflection # reflection
halo = 0.5*cv2.blur( halo = 0.5 * cv2.blur(skybg * skymask, (int(self.out_size_w / 5), int(self.out_size_w / 5)))
skybg*skymask, (int(self.out_size_w/5),
int(self.out_size_w/5)))
# screen blend 1 - (1-a)(1-b) # screen blend 1 - (1-a)(1-b)
syneth_with_halo = 1 - (1-syneth) * (1-halo) syneth_with_halo = 1 - (1 - syneth) * (1 - halo)
return syneth_with_halo return syneth_with_halo
......
...@@ -8,37 +8,37 @@ __all__ = ['SkyFilter'] ...@@ -8,37 +8,37 @@ __all__ = ['SkyFilter']
class SkyFilter(): class SkyFilter():
def __init__(self, model_path, video_path, save_path, in_size, def __init__(self, model_path, video_path, save_path, in_size, halo_effect, auto_light_matching, relighting_factor,
halo_effect, auto_light_matching, relighting_factor, recoloring_factor, skybox_center_crop, rain_cap_path, skybox_img, skybox_video, is_video, is_rainy,
recoloring_factor, skybox_center_crop, rain_cap_path, is_show):
skybox_img, skybox_video, is_video, is_rainy, is_show
):
self.in_size = in_size self.in_size = in_size
self.is_show = is_show self.is_show = is_show
self.cap = cv2.VideoCapture(video_path) self.cap = cv2.VideoCapture(video_path)
self.m_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT)) self.m_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.fps = self.cap.get(cv2.CAP_PROP_FPS) self.fps = self.cap.get(cv2.CAP_PROP_FPS)
self.out_size = int(self.cap.get( self.out_size = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cv2.CAP_PROP_FRAME_WIDTH)), int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.model = paddle.jit.load( self.model = paddle.jit.load(model_path, model_filename='__model__', params_filename='__params__')
model_path, model_filename='__model__', params_filename='__params__')
self.model.eval() self.model.eval()
self.skyboxengine = SkyBox( self.skyboxengine = SkyBox(
out_size=self.out_size, skybox_img=skybox_img, skybox_video=skybox_video, out_size=self.out_size,
halo_effect=halo_effect, auto_light_matching=auto_light_matching, skybox_img=skybox_img,
relighting_factor=relighting_factor, recoloring_factor=recoloring_factor, skybox_video=skybox_video,
skybox_center_crop=skybox_center_crop, rain_cap_path=rain_cap_path, halo_effect=halo_effect,
is_video=is_video, is_rainy=is_rainy auto_light_matching=auto_light_matching,
) relighting_factor=relighting_factor,
recoloring_factor=recoloring_factor,
skybox_center_crop=skybox_center_crop,
rain_cap_path=rain_cap_path,
is_video=is_video,
is_rainy=is_rainy)
path, _ = os.path.split(save_path) path, _ = os.path.split(save_path)
if path == '': if path == '':
path = '.' path = '.'
if not os.path.exists(path): if not os.path.exists(path):
os.mkdir(path) os.mkdir(path)
self.video_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'MP4V'), self.video_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'MP4V'), self.fps, self.out_size)
self.fps, self.out_size)
def synthesize(self, img_HD, img_HD_prev): def synthesize(self, img_HD, img_HD_prev):
h, w, _ = img_HD.shape h, w, _ = img_HD.shape
...@@ -50,8 +50,7 @@ class SkyFilter(): ...@@ -50,8 +50,7 @@ class SkyFilter():
img = paddle.to_tensor(img) img = paddle.to_tensor(img)
G_pred = self.model(img) G_pred = self.model(img)
G_pred = paddle.nn.functional.interpolate( G_pred = paddle.nn.functional.interpolate(G_pred, (h, w), mode='bicubic', align_corners=False)
G_pred, (h, w), mode='bicubic', align_corners=False)
G_pred = G_pred[0, :].transpose([1, 2, 0]) G_pred = G_pred[0, :].transpose([1, 2, 0])
G_pred = paddle.concat([G_pred, G_pred, G_pred], axis=-1) G_pred = paddle.concat([G_pred, G_pred, G_pred], axis=-1)
G_pred = G_pred.detach().numpy() G_pred = G_pred.detach().numpy()
...@@ -68,7 +67,7 @@ class SkyFilter(): ...@@ -68,7 +67,7 @@ class SkyFilter():
frames_num = preview_frames_num if 0 < preview_frames_num < self.m_frames else self.m_frames frames_num = preview_frames_num if 0 < preview_frames_num < self.m_frames else self.m_frames
print('frames_num: %d, running evaluation...' % frames_num) print('frames_num: %d, running evaluation...' % frames_num)
for idx in range(1, frames_num+1): for idx in range(1, frames_num + 1):
ret, frame = self.cap.read() ret, frame = self.cap.read()
if ret: if ret:
frame = cv2.resize(frame, self.out_size) frame = cv2.resize(frame, self.out_size)
...@@ -84,7 +83,7 @@ class SkyFilter(): ...@@ -84,7 +83,7 @@ class SkyFilter():
if self.is_show: if self.is_show:
show_img = np.concatenate([frame, result], 1) show_img = np.concatenate([frame, result], 1)
h, w = show_img.shape[:2] h, w = show_img.shape[:2]
show_img = cv2.resize(show_img, (720, int(720/w*h))) show_img = cv2.resize(show_img, (720, int(720 / w * h)))
cv2.imshow('preview', show_img) cv2.imshow('preview', show_img)
k = cv2.waitKey(1) k = cv2.waitKey(1)
if (k == 27) or (cv2.getWindowProperty('preview', 0) == -1): if (k == 27) or (cv2.getWindowProperty('preview', 0) == -1):
......
...@@ -3,10 +3,7 @@ import numpy as np ...@@ -3,10 +3,7 @@ import numpy as np
from sklearn.neighbors import KernelDensity from sklearn.neighbors import KernelDensity
__all__ = [ __all__ = [
'build_transformation_matrix', 'build_transformation_matrix', 'update_transformation_matrix', 'estimate_partial_transform', 'removeOutliers',
'update_transformation_matrix',
'estimate_partial_transform',
'removeOutliers',
'guidedfilter' 'guidedfilter'
] ]
...@@ -48,8 +45,7 @@ def estimate_partial_transform(matched_keypoints): ...@@ -48,8 +45,7 @@ def estimate_partial_transform(matched_keypoints):
:return: transform as list of [dx, dy, da] :return: transform as list of [dx, dy, da]
""" """
prev_matched_kp, cur_matched_kp = matched_keypoints prev_matched_kp, cur_matched_kp = matched_keypoints
transform = cv2.estimateAffinePartial2D(np.array(prev_matched_kp), transform = cv2.estimateAffinePartial2D(np.array(prev_matched_kp), np.array(cur_matched_kp))[0]
np.array(cur_matched_kp))[0]
if transform is not None: if transform is not None:
# translation x # translation x
...@@ -83,16 +79,14 @@ def boxfilter(img, r): ...@@ -83,16 +79,14 @@ def boxfilter(img, r):
imDst = np.zeros_like(img) imDst = np.zeros_like(img)
imCum = np.cumsum(img, 0) imCum = np.cumsum(img, 0)
imDst[0: r+1, :] = imCum[r: 2*r+1, :] imDst[0:r + 1, :] = imCum[r:2 * r + 1, :]
imDst[r+1: rows-r, :] = imCum[2*r+1: rows, :] - imCum[0: rows-2*r-1, :] imDst[r + 1:rows - r, :] = imCum[2 * r + 1:rows, :] - imCum[0:rows - 2 * r - 1, :]
imDst[rows-r: rows, :] = np.tile(imCum[rows-1, :], imDst[rows - r:rows, :] = np.tile(imCum[rows - 1, :], [r, 1]) - imCum[rows - 2 * r - 1:rows - r - 1, :]
[r, 1]) - imCum[rows-2*r-1: rows-r-1, :]
imCum = np.cumsum(imDst, 1) imCum = np.cumsum(imDst, 1)
imDst[:, 0: r+1] = imCum[:, r: 2*r+1] imDst[:, 0:r + 1] = imCum[:, r:2 * r + 1]
imDst[:, r+1: cols-r] = imCum[:, 2*r+1: cols] - imCum[:, 0: cols-2*r-1] imDst[:, r + 1:cols - r] = imCum[:, 2 * r + 1:cols] - imCum[:, 0:cols - 2 * r - 1]
imDst[:, cols-r: cols] = np.tile(imCum[:, cols-1], imDst[:, cols - r:cols] = np.tile(imCum[:, cols - 1], [r, 1]).T - imCum[:, cols - 2 * r - 1:cols - r - 1]
[r, 1]).T - imCum[:, cols-2*r-1: cols-r-1]
return imDst return imDst
......
...@@ -64,7 +64,8 @@ _KVDATA_KEYTYPEENTRY = _descriptor.Descriptor( ...@@ -64,7 +64,8 @@ _KVDATA_KEYTYPEENTRY = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='key', _descriptor.FieldDescriptor(
name='key',
full_name='paddlehub.module.desc.KVData.KeyTypeEntry.key', full_name='paddlehub.module.desc.KVData.KeyTypeEntry.key',
index=0, index=0,
number=1, number=1,
...@@ -79,7 +80,8 @@ _KVDATA_KEYTYPEENTRY = _descriptor.Descriptor( ...@@ -79,7 +80,8 @@ _KVDATA_KEYTYPEENTRY = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='value', _descriptor.FieldDescriptor(
name='value',
full_name='paddlehub.module.desc.KVData.KeyTypeEntry.value', full_name='paddlehub.module.desc.KVData.KeyTypeEntry.value',
index=1, index=1,
number=2, number=2,
...@@ -114,7 +116,8 @@ _KVDATA_DATAENTRY = _descriptor.Descriptor( ...@@ -114,7 +116,8 @@ _KVDATA_DATAENTRY = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='key', _descriptor.FieldDescriptor(
name='key',
full_name='paddlehub.module.desc.KVData.DataEntry.key', full_name='paddlehub.module.desc.KVData.DataEntry.key',
index=0, index=0,
number=1, number=1,
...@@ -129,7 +132,8 @@ _KVDATA_DATAENTRY = _descriptor.Descriptor( ...@@ -129,7 +132,8 @@ _KVDATA_DATAENTRY = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='value', _descriptor.FieldDescriptor(
name='value',
full_name='paddlehub.module.desc.KVData.DataEntry.value', full_name='paddlehub.module.desc.KVData.DataEntry.value',
index=1, index=1,
number=2, number=2,
...@@ -164,7 +168,8 @@ _KVDATA = _descriptor.Descriptor( ...@@ -164,7 +168,8 @@ _KVDATA = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='key_type', _descriptor.FieldDescriptor(
name='key_type',
full_name='paddlehub.module.desc.KVData.key_type', full_name='paddlehub.module.desc.KVData.key_type',
index=0, index=0,
number=1, number=1,
...@@ -179,7 +184,8 @@ _KVDATA = _descriptor.Descriptor( ...@@ -179,7 +184,8 @@ _KVDATA = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='data', _descriptor.FieldDescriptor(
name='data',
full_name='paddlehub.module.desc.KVData.data', full_name='paddlehub.module.desc.KVData.data',
index=1, index=1,
number=2, number=2,
...@@ -217,7 +223,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -217,7 +223,8 @@ _MODULEATTR = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='type', _descriptor.FieldDescriptor(
name='type',
full_name='paddlehub.module.desc.ModuleAttr.type', full_name='paddlehub.module.desc.ModuleAttr.type',
index=0, index=0,
number=1, number=1,
...@@ -232,7 +239,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -232,7 +239,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='i', _descriptor.FieldDescriptor(
name='i',
full_name='paddlehub.module.desc.ModuleAttr.i', full_name='paddlehub.module.desc.ModuleAttr.i',
index=1, index=1,
number=2, number=2,
...@@ -247,7 +255,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -247,7 +255,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='f', _descriptor.FieldDescriptor(
name='f',
full_name='paddlehub.module.desc.ModuleAttr.f', full_name='paddlehub.module.desc.ModuleAttr.f',
index=2, index=2,
number=3, number=3,
...@@ -262,7 +271,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -262,7 +271,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='b', _descriptor.FieldDescriptor(
name='b',
full_name='paddlehub.module.desc.ModuleAttr.b', full_name='paddlehub.module.desc.ModuleAttr.b',
index=3, index=3,
number=4, number=4,
...@@ -277,7 +287,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -277,7 +287,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='s', _descriptor.FieldDescriptor(
name='s',
full_name='paddlehub.module.desc.ModuleAttr.s', full_name='paddlehub.module.desc.ModuleAttr.s',
index=4, index=4,
number=5, number=5,
...@@ -292,7 +303,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -292,7 +303,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='map', _descriptor.FieldDescriptor(
name='map',
full_name='paddlehub.module.desc.ModuleAttr.map', full_name='paddlehub.module.desc.ModuleAttr.map',
index=5, index=5,
number=6, number=6,
...@@ -307,7 +319,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -307,7 +319,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='list', _descriptor.FieldDescriptor(
name='list',
full_name='paddlehub.module.desc.ModuleAttr.list', full_name='paddlehub.module.desc.ModuleAttr.list',
index=6, index=6,
number=7, number=7,
...@@ -322,7 +335,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -322,7 +335,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='set', _descriptor.FieldDescriptor(
name='set',
full_name='paddlehub.module.desc.ModuleAttr.set', full_name='paddlehub.module.desc.ModuleAttr.set',
index=7, index=7,
number=8, number=8,
...@@ -337,7 +351,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -337,7 +351,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='object', _descriptor.FieldDescriptor(
name='object',
full_name='paddlehub.module.desc.ModuleAttr.object', full_name='paddlehub.module.desc.ModuleAttr.object',
index=8, index=8,
number=9, number=9,
...@@ -352,7 +367,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -352,7 +367,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='name', _descriptor.FieldDescriptor(
name='name',
full_name='paddlehub.module.desc.ModuleAttr.name', full_name='paddlehub.module.desc.ModuleAttr.name',
index=9, index=9,
number=10, number=10,
...@@ -367,7 +383,8 @@ _MODULEATTR = _descriptor.Descriptor( ...@@ -367,7 +383,8 @@ _MODULEATTR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='info', _descriptor.FieldDescriptor(
name='info',
full_name='paddlehub.module.desc.ModuleAttr.info', full_name='paddlehub.module.desc.ModuleAttr.info',
index=10, index=10,
number=11, number=11,
...@@ -402,7 +419,8 @@ _FEEDDESC = _descriptor.Descriptor( ...@@ -402,7 +419,8 @@ _FEEDDESC = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='var_name', _descriptor.FieldDescriptor(
name='var_name',
full_name='paddlehub.module.desc.FeedDesc.var_name', full_name='paddlehub.module.desc.FeedDesc.var_name',
index=0, index=0,
number=1, number=1,
...@@ -417,7 +435,8 @@ _FEEDDESC = _descriptor.Descriptor( ...@@ -417,7 +435,8 @@ _FEEDDESC = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='alias', _descriptor.FieldDescriptor(
name='alias',
full_name='paddlehub.module.desc.FeedDesc.alias', full_name='paddlehub.module.desc.FeedDesc.alias',
index=1, index=1,
number=2, number=2,
...@@ -452,7 +471,8 @@ _FETCHDESC = _descriptor.Descriptor( ...@@ -452,7 +471,8 @@ _FETCHDESC = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='var_name', _descriptor.FieldDescriptor(
name='var_name',
full_name='paddlehub.module.desc.FetchDesc.var_name', full_name='paddlehub.module.desc.FetchDesc.var_name',
index=0, index=0,
number=1, number=1,
...@@ -467,7 +487,8 @@ _FETCHDESC = _descriptor.Descriptor( ...@@ -467,7 +487,8 @@ _FETCHDESC = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='alias', _descriptor.FieldDescriptor(
name='alias',
full_name='paddlehub.module.desc.FetchDesc.alias', full_name='paddlehub.module.desc.FetchDesc.alias',
index=1, index=1,
number=2, number=2,
...@@ -502,7 +523,8 @@ _MODULEVAR = _descriptor.Descriptor( ...@@ -502,7 +523,8 @@ _MODULEVAR = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='fetch_desc', _descriptor.FieldDescriptor(
name='fetch_desc',
full_name='paddlehub.module.desc.ModuleVar.fetch_desc', full_name='paddlehub.module.desc.ModuleVar.fetch_desc',
index=0, index=0,
number=1, number=1,
...@@ -517,7 +539,8 @@ _MODULEVAR = _descriptor.Descriptor( ...@@ -517,7 +539,8 @@ _MODULEVAR = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='feed_desc', _descriptor.FieldDescriptor(
name='feed_desc',
full_name='paddlehub.module.desc.ModuleVar.feed_desc', full_name='paddlehub.module.desc.ModuleVar.feed_desc',
index=1, index=1,
number=2, number=2,
...@@ -552,7 +575,8 @@ _MODULEDESC_SIGN2VARENTRY = _descriptor.Descriptor( ...@@ -552,7 +575,8 @@ _MODULEDESC_SIGN2VARENTRY = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='key', _descriptor.FieldDescriptor(
name='key',
full_name='paddlehub.module.desc.ModuleDesc.Sign2varEntry.key', full_name='paddlehub.module.desc.ModuleDesc.Sign2varEntry.key',
index=0, index=0,
number=1, number=1,
...@@ -567,7 +591,8 @@ _MODULEDESC_SIGN2VARENTRY = _descriptor.Descriptor( ...@@ -567,7 +591,8 @@ _MODULEDESC_SIGN2VARENTRY = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='value', _descriptor.FieldDescriptor(
name='value',
full_name='paddlehub.module.desc.ModuleDesc.Sign2varEntry.value', full_name='paddlehub.module.desc.ModuleDesc.Sign2varEntry.value',
index=1, index=1,
number=2, number=2,
...@@ -602,7 +627,8 @@ _MODULEDESC = _descriptor.Descriptor( ...@@ -602,7 +627,8 @@ _MODULEDESC = _descriptor.Descriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor(name='sign2var', _descriptor.FieldDescriptor(
name='sign2var',
full_name='paddlehub.module.desc.ModuleDesc.sign2var', full_name='paddlehub.module.desc.ModuleDesc.sign2var',
index=0, index=0,
number=2, number=2,
...@@ -617,7 +643,8 @@ _MODULEDESC = _descriptor.Descriptor( ...@@ -617,7 +643,8 @@ _MODULEDESC = _descriptor.Descriptor(
is_extension=False, is_extension=False,
extension_scope=None, extension_scope=None,
options=None), options=None),
_descriptor.FieldDescriptor(name='attr', _descriptor.FieldDescriptor(
name='attr',
full_name='paddlehub.module.desc.ModuleDesc.attr', full_name='paddlehub.module.desc.ModuleDesc.attr',
index=1, index=1,
number=3, number=3,
...@@ -679,14 +706,16 @@ KVData = _reflection.GeneratedProtocolMessageType( ...@@ -679,14 +706,16 @@ KVData = _reflection.GeneratedProtocolMessageType(
KeyTypeEntry=_reflection.GeneratedProtocolMessageType( KeyTypeEntry=_reflection.GeneratedProtocolMessageType(
'KeyTypeEntry', 'KeyTypeEntry',
(_message.Message, ), (_message.Message, ),
dict(DESCRIPTOR=_KVDATA_KEYTYPEENTRY, dict(
DESCRIPTOR=_KVDATA_KEYTYPEENTRY,
__module__='module_desc_pb2' __module__='module_desc_pb2'
# @@protoc_insertion_point(class_scope:paddlehub.module.desc.KVData.KeyTypeEntry) # @@protoc_insertion_point(class_scope:paddlehub.module.desc.KVData.KeyTypeEntry)
)), )),
DataEntry=_reflection.GeneratedProtocolMessageType( DataEntry=_reflection.GeneratedProtocolMessageType(
'DataEntry', 'DataEntry',
(_message.Message, ), (_message.Message, ),
dict(DESCRIPTOR=_KVDATA_DATAENTRY, dict(
DESCRIPTOR=_KVDATA_DATAENTRY,
__module__='module_desc_pb2' __module__='module_desc_pb2'
# @@protoc_insertion_point(class_scope:paddlehub.module.desc.KVData.DataEntry) # @@protoc_insertion_point(class_scope:paddlehub.module.desc.KVData.DataEntry)
)), )),
...@@ -701,7 +730,8 @@ _sym_db.RegisterMessage(KVData.DataEntry) ...@@ -701,7 +730,8 @@ _sym_db.RegisterMessage(KVData.DataEntry)
ModuleAttr = _reflection.GeneratedProtocolMessageType( ModuleAttr = _reflection.GeneratedProtocolMessageType(
'ModuleAttr', 'ModuleAttr',
(_message.Message, ), (_message.Message, ),
dict(DESCRIPTOR=_MODULEATTR, dict(
DESCRIPTOR=_MODULEATTR,
__module__='module_desc_pb2' __module__='module_desc_pb2'
# @@protoc_insertion_point(class_scope:paddlehub.module.desc.ModuleAttr) # @@protoc_insertion_point(class_scope:paddlehub.module.desc.ModuleAttr)
)) ))
...@@ -710,7 +740,8 @@ _sym_db.RegisterMessage(ModuleAttr) ...@@ -710,7 +740,8 @@ _sym_db.RegisterMessage(ModuleAttr)
FeedDesc = _reflection.GeneratedProtocolMessageType( FeedDesc = _reflection.GeneratedProtocolMessageType(
'FeedDesc', 'FeedDesc',
(_message.Message, ), (_message.Message, ),
dict(DESCRIPTOR=_FEEDDESC, dict(
DESCRIPTOR=_FEEDDESC,
__module__='module_desc_pb2' __module__='module_desc_pb2'
# @@protoc_insertion_point(class_scope:paddlehub.module.desc.FeedDesc) # @@protoc_insertion_point(class_scope:paddlehub.module.desc.FeedDesc)
)) ))
...@@ -719,7 +750,8 @@ _sym_db.RegisterMessage(FeedDesc) ...@@ -719,7 +750,8 @@ _sym_db.RegisterMessage(FeedDesc)
FetchDesc = _reflection.GeneratedProtocolMessageType( FetchDesc = _reflection.GeneratedProtocolMessageType(
'FetchDesc', 'FetchDesc',
(_message.Message, ), (_message.Message, ),
dict(DESCRIPTOR=_FETCHDESC, dict(
DESCRIPTOR=_FETCHDESC,
__module__='module_desc_pb2' __module__='module_desc_pb2'
# @@protoc_insertion_point(class_scope:paddlehub.module.desc.FetchDesc) # @@protoc_insertion_point(class_scope:paddlehub.module.desc.FetchDesc)
)) ))
...@@ -728,7 +760,8 @@ _sym_db.RegisterMessage(FetchDesc) ...@@ -728,7 +760,8 @@ _sym_db.RegisterMessage(FetchDesc)
ModuleVar = _reflection.GeneratedProtocolMessageType( ModuleVar = _reflection.GeneratedProtocolMessageType(
'ModuleVar', 'ModuleVar',
(_message.Message, ), (_message.Message, ),
dict(DESCRIPTOR=_MODULEVAR, dict(
DESCRIPTOR=_MODULEVAR,
__module__='module_desc_pb2' __module__='module_desc_pb2'
# @@protoc_insertion_point(class_scope:paddlehub.module.desc.ModuleVar) # @@protoc_insertion_point(class_scope:paddlehub.module.desc.ModuleVar)
)) ))
...@@ -741,7 +774,8 @@ ModuleDesc = _reflection.GeneratedProtocolMessageType( ...@@ -741,7 +774,8 @@ ModuleDesc = _reflection.GeneratedProtocolMessageType(
Sign2varEntry=_reflection.GeneratedProtocolMessageType( Sign2varEntry=_reflection.GeneratedProtocolMessageType(
'Sign2varEntry', 'Sign2varEntry',
(_message.Message, ), (_message.Message, ),
dict(DESCRIPTOR=_MODULEDESC_SIGN2VARENTRY, dict(
DESCRIPTOR=_MODULEDESC_SIGN2VARENTRY,
__module__='module_desc_pb2' __module__='module_desc_pb2'
# @@protoc_insertion_point(class_scope:paddlehub.module.desc.ModuleDesc.Sign2varEntry) # @@protoc_insertion_point(class_scope:paddlehub.module.desc.ModuleDesc.Sign2varEntry)
)), )),
......
...@@ -27,6 +27,7 @@ class InputExample(object): ...@@ -27,6 +27,7 @@ class InputExample(object):
""" """
Input example of one audio sample. Input example of one audio sample.
""" """
def __init__(self, guid: int, source: Union[list, str], label: Optional[str] = None): def __init__(self, guid: int, source: Union[list, str], label: Optional[str] = None):
self.guid = guid self.guid = guid
self.source = source self.source = source
...@@ -37,6 +38,7 @@ class BaseAudioDataset(object): ...@@ -37,6 +38,7 @@ class BaseAudioDataset(object):
""" """
Base class of speech dataset. Base class of speech dataset.
""" """
def __init__(self, base_path: str, data_file: str, mode: Optional[str] = "train"): def __init__(self, base_path: str, data_file: str, mode: Optional[str] = "train"):
self.data_file = os.path.join(base_path, data_file) self.data_file = os.path.join(base_path, data_file)
self.mode = mode self.mode = mode
...@@ -92,7 +94,8 @@ class AudioClassificationDataset(BaseAudioDataset, paddle.io.Dataset): ...@@ -92,7 +94,8 @@ class AudioClassificationDataset(BaseAudioDataset, paddle.io.Dataset):
if self.feat_type == 'raw': if self.feat_type == 'raw':
record['feat'] = example.source record['feat'] = example.source
elif self.feat_type == 'mel': elif self.feat_type == 'mel':
record['feat'] = extract_melspectrogram(example.source, record['feat'] = extract_melspectrogram(
example.source,
sample_rate=self.feat_cfg['sample_rate'], sample_rate=self.feat_cfg['sample_rate'],
window_size=self.feat_cfg['window_size'], window_size=self.feat_cfg['window_size'],
hop_size=self.feat_cfg['hop_size'], hop_size=self.feat_cfg['hop_size'],
......
...@@ -557,7 +557,9 @@ class TextMatchingDataset(BaseNLPDataset, paddle.io.Dataset): ...@@ -557,7 +557,9 @@ class TextMatchingDataset(BaseNLPDataset, paddle.io.Dataset):
pad_to_max_seq_len=True, return_length=True) pad_to_max_seq_len=True, return_length=True)
record = {'text_a': record_a, 'text_b': record_b} record = {'text_a': record_a, 'text_b': record_b}
else: else:
raise RuntimeError("Unknown type of self.tokenizer: {}, it must be an instance of PretrainedTokenizer".format(type(self.tokenizer))) raise RuntimeError(
"Unknown type of self.tokenizer: {}, it must be an instance of PretrainedTokenizer".format(
type(self.tokenizer)))
if not record: if not record:
logger.info( logger.info(
...@@ -582,7 +584,9 @@ class TextMatchingDataset(BaseNLPDataset, paddle.io.Dataset): ...@@ -582,7 +584,9 @@ class TextMatchingDataset(BaseNLPDataset, paddle.io.Dataset):
else: else:
return query_input_ids, query_token_type_ids, title_input_ids, title_token_type_ids return query_input_ids, query_token_type_ids, title_input_ids, title_token_type_ids
else: else:
raise RuntimeError("Unknown type of self.tokenizer: {}, it must be an instance of PretrainedTokenizer".format(type(self.tokenizer))) raise RuntimeError(
"Unknown type of self.tokenizer: {}, it must be an instance of PretrainedTokenizer".format(
type(self.tokenizer)))
def __len__(self): def __len__(self):
return len(self.records) return len(self.records)
...@@ -63,46 +63,34 @@ class SegDataset(paddle.io.Dataset): ...@@ -63,46 +63,34 @@ class SegDataset(paddle.io.Dataset):
self.edge = edge self.edge = edge
if mode.lower() not in ['train', 'val', 'test']: if mode.lower() not in ['train', 'val', 'test']:
raise ValueError( raise ValueError("mode should be 'train', 'val' or 'test', but got {}.".format(mode))
"mode should be 'train', 'val' or 'test', but got {}.".format(
mode))
if self.transforms is None: if self.transforms is None:
raise ValueError("`transforms` is necessary, but it is None.") raise ValueError("`transforms` is necessary, but it is None.")
self.dataset_root = dataset_root self.dataset_root = dataset_root
if not os.path.exists(self.dataset_root): if not os.path.exists(self.dataset_root):
raise FileNotFoundError('there is not `dataset_root`: {}.'.format( raise FileNotFoundError('there is not `dataset_root`: {}.'.format(self.dataset_root))
self.dataset_root))
if mode == 'train': if mode == 'train':
if train_path is None: if train_path is None:
raise ValueError( raise ValueError('When `mode` is "train", `train_path` is necessary, but it is None.')
'When `mode` is "train", `train_path` is necessary, but it is None.'
)
elif not os.path.exists(train_path): elif not os.path.exists(train_path):
raise FileNotFoundError( raise FileNotFoundError('`train_path` is not found: {}'.format(train_path))
'`train_path` is not found: {}'.format(train_path))
else: else:
file_path = train_path file_path = train_path
elif mode == 'val': elif mode == 'val':
if val_path is None: if val_path is None:
raise ValueError( raise ValueError('When `mode` is "val", `val_path` is necessary, but it is None.')
'When `mode` is "val", `val_path` is necessary, but it is None.'
)
elif not os.path.exists(val_path): elif not os.path.exists(val_path):
raise FileNotFoundError( raise FileNotFoundError('`val_path` is not found: {}'.format(val_path))
'`val_path` is not found: {}'.format(val_path))
else: else:
file_path = val_path file_path = val_path
else: else:
if test_path is None: if test_path is None:
raise ValueError( raise ValueError('When `mode` is "test", `test_path` is necessary, but it is None.')
'When `mode` is "test", `test_path` is necessary, but it is None.'
)
elif not os.path.exists(test_path): elif not os.path.exists(test_path):
raise FileNotFoundError( raise FileNotFoundError('`test_path` is not found: {}'.format(test_path))
'`test_path` is not found: {}'.format(test_path))
else: else:
file_path = test_path file_path = test_path
...@@ -111,8 +99,7 @@ class SegDataset(paddle.io.Dataset): ...@@ -111,8 +99,7 @@ class SegDataset(paddle.io.Dataset):
items = line.strip().split(separator) items = line.strip().split(separator)
if len(items) != 2: if len(items) != 2:
if mode == 'train' or mode == 'val': if mode == 'train' or mode == 'val':
raise ValueError( raise ValueError("File list format incorrect! In training or evaluation task it should be"
"File list format incorrect! In training or evaluation task it should be"
" image_name{}label_name\\n".format(separator)) " image_name{}label_name\\n".format(separator))
image_path = os.path.join(self.dataset_root, items[0]) image_path = os.path.join(self.dataset_root, items[0])
label_path = None label_path = None
...@@ -138,4 +125,3 @@ class SegDataset(paddle.io.Dataset): ...@@ -138,4 +125,3 @@ class SegDataset(paddle.io.Dataset):
def __len__(self) -> int: def __len__(self) -> int:
return len(self.file_list) return len(self.file_list)
...@@ -91,7 +91,8 @@ class ESC50(AudioClassificationDataset): ...@@ -91,7 +91,8 @@ class ESC50(AudioClassificationDataset):
else: else:
data_file = 'dev.npz' data_file = 'dev.npz'
feat_cfg = dict(sample_rate=self.sample_rate, feat_cfg = dict(
sample_rate=self.sample_rate,
window_size=1024, window_size=1024,
hop_size=320, hop_size=320,
mel_bins=64, mel_bins=64,
...@@ -99,7 +100,8 @@ class ESC50(AudioClassificationDataset): ...@@ -99,7 +100,8 @@ class ESC50(AudioClassificationDataset):
fmax=14000, fmax=14000,
window='hann') window='hann')
super().__init__(base_path=base_path, super().__init__(
base_path=base_path,
data_file=data_file, data_file=data_file,
file_type='npz', file_type='npz',
mode=mode, mode=mode,
......
...@@ -24,6 +24,7 @@ import paddlehub.env as hubenv ...@@ -24,6 +24,7 @@ import paddlehub.env as hubenv
from paddlehub.utils.download import download_data from paddlehub.utils.download import download_data
from paddlehub.datasets.base_seg_dataset import SegDataset from paddlehub.datasets.base_seg_dataset import SegDataset
@download_data(url='https://paddleseg.bj.bcebos.com/dataset/optic_disc_seg.zip') @download_data(url='https://paddleseg.bj.bcebos.com/dataset/optic_disc_seg.zip')
class OpticDiscSeg(SegDataset): class OpticDiscSeg(SegDataset):
""" """
...@@ -36,9 +37,7 @@ class OpticDiscSeg(SegDataset): ...@@ -36,9 +37,7 @@ class OpticDiscSeg(SegDataset):
edge (bool, optional): Whether to compute edge while training. Default: False edge (bool, optional): Whether to compute edge while training. Default: False
""" """
def __init__(self, def __init__(self, transforms: Callable = None, mode: str = 'train'):
transforms: Callable = None,
mode: str = 'train'):
self.transforms = transforms self.transforms = transforms
mode = mode.lower() mode = mode.lower()
self.mode = mode self.mode = mode
...@@ -47,14 +46,11 @@ class OpticDiscSeg(SegDataset): ...@@ -47,14 +46,11 @@ class OpticDiscSeg(SegDataset):
self.ignore_index = 255 self.ignore_index = 255
if mode not in ['train', 'val', 'test']: if mode not in ['train', 'val', 'test']:
raise ValueError( raise ValueError("`mode` should be 'train', 'val' or 'test', but got {}.".format(mode))
"`mode` should be 'train', 'val' or 'test', but got {}.".format(
mode))
if self.transforms is None: if self.transforms is None:
raise ValueError("`transforms` is necessary, but it is None.") raise ValueError("`transforms` is necessary, but it is None.")
if mode == 'train': if mode == 'train':
file_path = os.path.join(hubenv.DATA_HOME, 'optic_disc_seg', 'train_list.txt') file_path = os.path.join(hubenv.DATA_HOME, 'optic_disc_seg', 'train_list.txt')
elif mode == 'test': elif mode == 'test':
...@@ -67,9 +63,7 @@ class OpticDiscSeg(SegDataset): ...@@ -67,9 +63,7 @@ class OpticDiscSeg(SegDataset):
items = line.strip().split() items = line.strip().split()
if len(items) != 2: if len(items) != 2:
if mode == 'train' or mode == 'val': if mode == 'train' or mode == 'val':
raise Exception( raise Exception("File list format incorrect! It should be" " image_name label_name\\n")
"File list format incorrect! It should be"
" image_name label_name\\n")
image_path = os.path.join(hubenv.DATA_HOME, 'optic_disc_seg', items[0]) image_path = os.path.join(hubenv.DATA_HOME, 'optic_disc_seg', items[0])
grt_path = None grt_path = None
else: else:
......
...@@ -35,6 +35,7 @@ class DetectCatagory: ...@@ -35,6 +35,7 @@ class DetectCatagory:
label_ids(List(int)): The dataset label ids. label_ids(List(int)): The dataset label ids.
category_to_id_map(dict): Mapping relations of category and id for images. category_to_id_map(dict): Mapping relations of category and id for images.
""" """
def __init__(self, attrbox: Callable, data_dir: str): def __init__(self, attrbox: Callable, data_dir: str):
self.attrbox = attrbox self.attrbox = attrbox
self.img_dir = data_dir self.img_dir = data_dir
...@@ -62,6 +63,7 @@ class ParseImages: ...@@ -62,6 +63,7 @@ class ParseImages:
Returns: Returns:
imgs(dict): The input for detection model, it is a dict. imgs(dict): The input for detection model, it is a dict.
""" """
def __init__(self, attrbox: Callable, data_dir: str, category_to_id_map: dict): def __init__(self, attrbox: Callable, data_dir: str, category_to_id_map: dict):
self.attrbox = attrbox self.attrbox = attrbox
self.img_dir = data_dir self.img_dir = data_dir
...@@ -94,6 +96,7 @@ class GTAnotations: ...@@ -94,6 +96,7 @@ class GTAnotations:
Returns: Returns:
img(dict): Set specific value on the attributes of 'gt boxes' and 'gt labels' for input. img(dict): Set specific value on the attributes of 'gt boxes' and 'gt labels' for input.
""" """
def __init__(self, attrbox: Callable, category_to_id_map: dict): def __init__(self, attrbox: Callable, category_to_id_map: dict):
self.attrbox = attrbox self.attrbox = attrbox
self.category_to_id_map = category_to_id_map self.category_to_id_map = category_to_id_map
...@@ -154,6 +157,7 @@ class DetectionData(paddle.io.Dataset): ...@@ -154,6 +157,7 @@ class DetectionData(paddle.io.Dataset):
Returns: Returns:
DataSet: An iterable object for data iterating DataSet: An iterable object for data iterating
""" """
def __init__(self, transform: Callable, size: int = 416, mode: str = 'train'): def __init__(self, transform: Callable, size: int = 416, mode: str = 'train'):
self.mode = mode self.mode = mode
self.transform = transform self.transform = transform
......
...@@ -68,7 +68,6 @@ class Trainer(object): ...@@ -68,7 +68,6 @@ class Trainer(object):
if not isinstance(self.model, paddle.nn.Layer): if not isinstance(self.model, paddle.nn.Layer):
raise TypeError('The model {} is not a `paddle.nn.Layer` object.'.format(self.model.__name__)) raise TypeError('The model {} is not a `paddle.nn.Layer` object.'.format(self.model.__name__))
if self.local_rank == 0 and not os.path.exists(self.checkpoint_dir): if self.local_rank == 0 and not os.path.exists(self.checkpoint_dir):
os.makedirs(self.checkpoint_dir) os.makedirs(self.checkpoint_dir)
......
...@@ -126,8 +126,8 @@ class ImageClassifierModule(RunModule, ImageServing): ...@@ -126,8 +126,8 @@ class ImageClassifierModule(RunModule, ImageServing):
""" """
top_k = int(top_k) top_k = int(top_k)
images_decode = [base64_to_cv2(image) for image in images] images_decode = [base64_to_cv2(image) for image in images]
resdicts = self.predict(images=images_decode, top_k=top_k,**kwargs) resdicts = self.predict(images=images_decode, top_k=top_k, **kwargs)
final={} final = {}
for resdict in resdicts: for resdict in resdicts:
for key, value in resdict.items(): for key, value in resdict.items():
resdict[key] = float(value) resdict[key] = float(value)
...@@ -144,18 +144,13 @@ class ImageClassifierModule(RunModule, ImageServing): ...@@ -144,18 +144,13 @@ class ImageClassifierModule(RunModule, ImageServing):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(images=[args.input_path], top_k=args.top_k)
images=[args.input_path],
top_k=args.top_k)
return results return results
...@@ -164,18 +159,13 @@ class ImageClassifierModule(RunModule, ImageServing): ...@@ -164,18 +159,13 @@ class ImageClassifierModule(RunModule, ImageServing):
Add the command config options. Add the command config options.
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--top_k', type=int, default=1, help="top_k classification result.")
'--top_k',
type=int,
default=1,
help="top_k classification result.")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
class ImageColorizeModule(RunModule, ImageServing): class ImageColorizeModule(RunModule, ImageServing):
...@@ -288,11 +278,11 @@ class ImageColorizeModule(RunModule, ImageServing): ...@@ -288,11 +278,11 @@ class ImageColorizeModule(RunModule, ImageServing):
""" """
images_decode = [base64_to_cv2(image) for image in images] images_decode = [base64_to_cv2(image) for image in images]
visual = self.predict(images=images_decode, **kwargs) visual = self.predict(images=images_decode, **kwargs)
final={} final = {}
for i, visual_ret in enumerate(visual): for i, visual_ret in enumerate(visual):
h, w, c = images_decode[i].shape h, w, c = images_decode[i].shape
for key, value in visual_ret.items(): for key, value in visual_ret.items():
value = cv2.resize(cv2.cvtColor(value,cv2.COLOR_RGB2BGR), (w, h), cv2.INTER_NEAREST) value = cv2.resize(cv2.cvtColor(value, cv2.COLOR_RGB2BGR), (w, h), cv2.INTER_NEAREST)
visual_ret[key] = cv2_to_base64(value) visual_ret[key] = cv2_to_base64(value)
final['data'] = visual final['data'] = visual
return final return final
...@@ -307,19 +297,13 @@ class ImageColorizeModule(RunModule, ImageServing): ...@@ -307,19 +297,13 @@ class ImageColorizeModule(RunModule, ImageServing):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.predict( results = self.predict(images=[args.input_path], visualization=args.visualization, save_path=args.output_dir)
images=[args.input_path],
visualization=args.visualization,
save_path=args.output_dir)
return results return results
...@@ -329,22 +313,15 @@ class ImageColorizeModule(RunModule, ImageServing): ...@@ -329,22 +313,15 @@ class ImageColorizeModule(RunModule, ImageServing):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--output_dir', '--output_dir', type=str, default='colorization', help="save visualization result.")
type=str,
default='colorization',
help="save visualization result.")
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--visualization', '--visualization', type=bool, default=True, help="whether to save output as images.")
type=bool,
default=True,
help="whether to save output as images.")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.")
class Yolov3Module(RunModule, ImageServing): class Yolov3Module(RunModule, ImageServing):
...@@ -523,7 +500,12 @@ class StyleTransferModule(RunModule, ImageServing): ...@@ -523,7 +500,12 @@ class StyleTransferModule(RunModule, ImageServing):
return {'loss': loss, 'metrics': {'content gap': content_loss, 'style gap': style_loss}} return {'loss': loss, 'metrics': {'content gap': content_loss, 'style gap': style_loss}}
def predict(self, origin: list, style: Union[str, np.ndarray], batch_size: int = 1, visualization: bool = True, save_path: str = 'style_tranfer'): def predict(self,
origin: list,
style: Union[str, np.ndarray],
batch_size: int = 1,
visualization: bool = True,
save_path: str = 'style_tranfer'):
''' '''
Colorize images Colorize images
...@@ -578,7 +560,7 @@ class StyleTransferModule(RunModule, ImageServing): ...@@ -578,7 +560,7 @@ class StyleTransferModule(RunModule, ImageServing):
images_decode = [base64_to_cv2(image) for image in images[0]] images_decode = [base64_to_cv2(image) for image in images[0]]
style_decode = base64_to_cv2(images[1]) style_decode = base64_to_cv2(images[1])
results = self.predict(origin=images_decode, style=style_decode, **kwargs) results = self.predict(origin=images_decode, style=style_decode, **kwargs)
final={} final = {}
final['data'] = [cv2_to_base64(result) for result in results] final['data'] = [cv2_to_base64(result) for result in results]
return final return final
...@@ -592,12 +574,9 @@ class StyleTransferModule(RunModule, ImageServing): ...@@ -592,12 +574,9 @@ class StyleTransferModule(RunModule, ImageServing):
prog='hub run {}'.format(self.name), prog='hub run {}'.format(self.name),
usage='%(prog)s', usage='%(prog)s',
add_help=True) add_help=True)
self.arg_input_group = self.parser.add_argument_group( self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
title="Config options", title="Config options", description="Run configuration for controlling module behavior, not required.")
description=
"Run configuration for controlling module behavior, not required.")
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
...@@ -615,25 +594,17 @@ class StyleTransferModule(RunModule, ImageServing): ...@@ -615,25 +594,17 @@ class StyleTransferModule(RunModule, ImageServing):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--output_dir', '--output_dir', type=str, default='style_tranfer', help="The directory to save output images.")
type=str,
default='style_tranfer',
help="The directory to save output images.")
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--visualization', '--visualization', type=bool, default=True, help="whether to save output as images.")
type=bool,
default=True,
help="whether to save output as images.")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
Add the command input options. Add the command input options.
""" """
self.arg_input_group.add_argument( self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
'--input_path', type=str, help="path to image.") self.arg_input_group.add_argument('--style_path', type=str, help="path to style image.")
self.arg_input_group.add_argument(
'--style_path', type=str, help="path to style image.")
class ImageSegmentationModule(ImageServing, RunModule): class ImageSegmentationModule(ImageServing, RunModule):
...@@ -659,13 +630,17 @@ class ImageSegmentationModule(ImageServing, RunModule): ...@@ -659,13 +630,17 @@ class ImageSegmentationModule(ImageServing, RunModule):
if logit.shape[-2:] != label.shape[-2:]: if logit.shape[-2:] != label.shape[-2:]:
logit = F.interpolate(logit, label.shape[-2:], mode='bilinear') logit = F.interpolate(logit, label.shape[-2:], mode='bilinear')
logit = logit.transpose([0,2,3,1]) logit = logit.transpose([0, 2, 3, 1])
loss_ce = criterionCE(logit, label) loss_ce = criterionCE(logit, label)
loss += loss_ce / len(logits) loss += loss_ce / len(logits)
return {"loss": loss} return {"loss": loss}
def predict(self, images: Union[str, np.ndarray], batch_size: int = 1, visualization: bool = True, save_path: str = 'seg_result') -> List[np.ndarray]: def predict(self,
images: Union[str, np.ndarray],
batch_size: int = 1,
visualization: bool = True,
save_path: str = 'seg_result') -> List[np.ndarray]:
''' '''
Obtain segmentation results. Obtain segmentation results.
...@@ -679,7 +654,7 @@ class ImageSegmentationModule(ImageServing, RunModule): ...@@ -679,7 +654,7 @@ class ImageSegmentationModule(ImageServing, RunModule):
output(list[np.ndarray]) : The segmentation mask. output(list[np.ndarray]) : The segmentation mask.
''' '''
self.eval() self.eval()
result=[] result = []
total_num = len(images) total_num = len(images)
loop_num = int(np.ceil(total_num / batch_size)) loop_num = int(np.ceil(total_num / batch_size))
...@@ -697,17 +672,17 @@ class ImageSegmentationModule(ImageServing, RunModule): ...@@ -697,17 +672,17 @@ class ImageSegmentationModule(ImageServing, RunModule):
pred = paddle.argmax(pred[0], axis=1, keepdim=True, dtype='int32') pred = paddle.argmax(pred[0], axis=1, keepdim=True, dtype='int32')
for num in range(pred.shape[0]): for num in range(pred.shape[0]):
if isinstance(images[handle_id+num], str): if isinstance(images[handle_id + num], str):
image = cv2.imread(images[handle_id+num]) image = cv2.imread(images[handle_id + num])
else: else:
image = images[handle_id+num] image = images[handle_id + num]
h, w, c = image.shape h, w, c = image.shape
pred_final = utils.reverse_transform(pred[num: num+1], (h,w), self.transforms.transforms) pred_final = utils.reverse_transform(pred[num:num + 1], (h, w), self.transforms.transforms)
pred_final = paddle.squeeze(pred_final) pred_final = paddle.squeeze(pred_final)
pred_final = pred_final.numpy().astype('uint8') pred_final = pred_final.numpy().astype('uint8')
if visualization: if visualization:
added_image = utils.visualize(images[handle_id+num], pred_final, weight=0.6) added_image = utils.visualize(images[handle_id + num], pred_final, weight=0.6)
pred_mask = utils.get_pseudo_color_map(pred_final) pred_mask = utils.get_pseudo_color_map(pred_final)
pred_image_path = os.path.join(save_path, 'image', str(time.time()) + ".png") pred_image_path = os.path.join(save_path, 'image', str(time.time()) + ".png")
pred_mask_path = os.path.join(save_path, 'mask', str(time.time()) + ".png") pred_mask_path = os.path.join(save_path, 'mask', str(time.time()) + ".png")
...@@ -728,7 +703,7 @@ class ImageSegmentationModule(ImageServing, RunModule): ...@@ -728,7 +703,7 @@ class ImageSegmentationModule(ImageServing, RunModule):
""" """
images_decode = [base64_to_cv2(image) for image in images] images_decode = [base64_to_cv2(image) for image in images]
visual = self.predict(images=images_decode, **kwargs) visual = self.predict(images=images_decode, **kwargs)
final=[] final = []
for mask in visual: for mask in visual:
final.append(cv2_to_base64(mask)) final.append(cv2_to_base64(mask))
return final return final
...@@ -23,7 +23,6 @@ from paddlehub.utils import log, utils, xarfile ...@@ -23,7 +23,6 @@ from paddlehub.utils import log, utils, xarfile
def download_data(url): def download_data(url):
def _wrapper(Dataset): def _wrapper(Dataset):
def _check_download(): def _check_download():
save_name = os.path.basename(url).split('.')[0] save_name = os.path.basename(url).split('.')[0]
......
...@@ -40,6 +40,7 @@ from paddlehub.utils.log import logger ...@@ -40,6 +40,7 @@ from paddlehub.utils.log import logger
class Version(packaging.version.Version): class Version(packaging.version.Version):
'''Extended implementation of packaging.version.Version''' '''Extended implementation of packaging.version.Version'''
def match(self, condition: str) -> bool: def match(self, condition: str) -> bool:
''' '''
Determine whether the given condition are met Determine whether the given condition are met
...@@ -105,6 +106,7 @@ class Version(packaging.version.Version): ...@@ -105,6 +106,7 @@ class Version(packaging.version.Version):
class Timer(object): class Timer(object):
'''Calculate runing speed and estimated time of arrival(ETA)''' '''Calculate runing speed and estimated time of arrival(ETA)'''
def __init__(self, total_step: int): def __init__(self, total_step: int):
self.total_step = total_step self.total_step = total_step
self.last_start_step = 0 self.last_start_step = 0
...@@ -408,7 +410,8 @@ def extract_melspectrogram(y, ...@@ -408,7 +410,8 @@ def extract_melspectrogram(y,
logger.error('Failed to import librosa. Please check that librosa and numba are correctly installed.') logger.error('Failed to import librosa. Please check that librosa and numba are correctly installed.')
raise raise
s = librosa.stft(y, s = librosa.stft(
y,
n_fft=window_size, n_fft=window_size,
hop_length=hop_size, hop_length=hop_size,
win_length=window_size, win_length=window_size,
......
...@@ -145,18 +145,15 @@ class Padding: ...@@ -145,18 +145,15 @@ class Padding:
label_padding_value: int = 255): label_padding_value: int = 255):
if isinstance(target_size, list) or isinstance(target_size, tuple): if isinstance(target_size, list) or isinstance(target_size, tuple):
if len(target_size) != 2: if len(target_size) != 2:
raise ValueError( raise ValueError('`target_size` should include 2 elements, but it is {}'.format(target_size))
'`target_size` should include 2 elements, but it is {}'.
format(target_size))
else: else:
raise TypeError( raise TypeError("Type of target_size is invalid. It should be list or tuple, now is {}".format(
"Type of target_size is invalid. It should be list or tuple, now is {}" type(target_size)))
.format(type(target_size)))
self.target_size = target_size self.target_size = target_size
self.im_padding_value = im_padding_value self.im_padding_value = im_padding_value
self.label_padding_value = label_padding_value self.label_padding_value = label_padding_value
def __call__(self, im: np.ndarray , label: np.ndarray = None) -> Tuple: def __call__(self, im: np.ndarray, label: np.ndarray = None) -> Tuple:
""" """
Args: Args:
im (np.ndarray): The Image data. im (np.ndarray): The Image data.
...@@ -179,13 +176,12 @@ class Padding: ...@@ -179,13 +176,12 @@ class Padding:
'The size of image should be less than `target_size`, but the size of image ({}, {}) is larger than `target_size` ({}, {})' 'The size of image should be less than `target_size`, but the size of image ({}, {}) is larger than `target_size` ({}, {})'
.format(im_width, im_height, target_width, target_height)) .format(im_width, im_height, target_width, target_height))
else: else:
im = cv2.copyMakeBorder(im, 0, pad_height, 0, pad_width, cv2.BORDER_CONSTANT, im = cv2.copyMakeBorder(im, 0, pad_height, 0, pad_width, cv2.BORDER_CONSTANT, value=self.im_padding_value)
value=self.im_padding_value)
if label is not None: if label is not None:
label = cv2.copyMakeBorder(label, 0, pad_height, 0, pad_width, cv2.BORDER_CONSTANT, label = cv2.copyMakeBorder(
value=self.label_padding_value) label, 0, pad_height, 0, pad_width, cv2.BORDER_CONSTANT, value=self.label_padding_value)
if label is None: if label is None:
return (im,) return (im, )
else: else:
return (im, label) return (im, label)
...@@ -200,15 +196,13 @@ class Normalize: ...@@ -200,15 +196,13 @@ class Normalize:
ValueError: When mean/std is not list or any value in std is 0. ValueError: When mean/std is not list or any value in std is 0.
""" """
def __init__(self, mean: Union[List[float], Tuple[float]] = (0.5, 0.5, 0.5), def __init__(self,
mean: Union[List[float], Tuple[float]] = (0.5, 0.5, 0.5),
std: Union[List[float], Tuple[float]] = (0.5, 0.5, 0.5)): std: Union[List[float], Tuple[float]] = (0.5, 0.5, 0.5)):
self.mean = mean self.mean = mean
self.std = std self.std = std
if not (isinstance(self.mean, (list, tuple)) if not (isinstance(self.mean, (list, tuple)) and isinstance(self.std, (list, tuple))):
and isinstance(self.std, (list, tuple))): raise ValueError("{}: input type is invalid. It should be list or tuple".format(self))
raise ValueError(
"{}: input type is invalid. It should be list or tuple".format(
self))
from functools import reduce from functools import reduce
if reduce(lambda x, y: x * y, self.std) == 0: if reduce(lambda x, y: x * y, self.std) == 0:
raise ValueError('{}: std is invalid!'.format(self)) raise ValueError('{}: std is invalid!'.format(self))
...@@ -227,7 +221,7 @@ class Normalize: ...@@ -227,7 +221,7 @@ class Normalize:
im = F.normalize(im, mean, std) im = F.normalize(im, mean, std)
if label is None: if label is None:
return (im,) return (im, )
else: else:
return (im, label) return (im, label)
...@@ -260,17 +254,13 @@ class Resize: ...@@ -260,17 +254,13 @@ class Resize:
def __init__(self, target_size: Union[List[int], Tuple[int]] = (512, 512), interp: str = 'LINEAR'): def __init__(self, target_size: Union[List[int], Tuple[int]] = (512, 512), interp: str = 'LINEAR'):
self.interp = interp self.interp = interp
if not (interp == "RANDOM" or interp in self.interp_dict): if not (interp == "RANDOM" or interp in self.interp_dict):
raise ValueError("`interp` should be one of {}".format( raise ValueError("`interp` should be one of {}".format(self.interp_dict.keys()))
self.interp_dict.keys()))
if isinstance(target_size, list) or isinstance(target_size, tuple): if isinstance(target_size, list) or isinstance(target_size, tuple):
if len(target_size) != 2: if len(target_size) != 2:
raise ValueError( raise ValueError('`target_size` should include 2 elements, but it is {}'.format(target_size))
'`target_size` should include 2 elements, but it is {}'.
format(target_size))
else: else:
raise TypeError( raise TypeError("Type of `target_size` is invalid. It should be list or tuple, but it is {}".format(
"Type of `target_size` is invalid. It should be list or tuple, but it is {}" type(target_size)))
.format(type(target_size)))
self.target_size = target_size self.target_size = target_size
...@@ -298,10 +288,9 @@ class Resize: ...@@ -298,10 +288,9 @@ class Resize:
interp = self.interp interp = self.interp
im = F.resize(im, self.target_size, self.interp_dict[interp]) im = F.resize(im, self.target_size, self.interp_dict[interp])
if label is not None: if label is not None:
label = F.resize(label, self.target_size, label = F.resize(label, self.target_size, cv2.INTER_NEAREST)
cv2.INTER_NEAREST)
if label is None: if label is None:
return (im,) return (im, )
else: else:
return (im, label) return (im, label)
...@@ -31,6 +31,7 @@ class Compose: ...@@ -31,6 +31,7 @@ class Compose:
to_rgb(bool): Whether to transform the input from BGR mode to RGB mode, default is False. to_rgb(bool): Whether to transform the input from BGR mode to RGB mode, default is False.
channel_first(bool): whether to permute image from channel laste to channel first channel_first(bool): whether to permute image from channel laste to channel first
""" """
def __init__(self, transforms: Callable, to_rgb: bool = False, channel_first: bool = True): def __init__(self, transforms: Callable, to_rgb: bool = False, channel_first: bool = True):
if not isinstance(transforms, list): if not isinstance(transforms, list):
raise TypeError('The transforms must be a list!') raise TypeError('The transforms must be a list!')
...@@ -57,16 +58,20 @@ class Compose: ...@@ -57,16 +58,20 @@ class Compose:
im = F.permute(im) im = F.permute(im)
return im return im
class Permute: class Permute:
""" """
Repermute the input image from [H, W, C] to [C, H, W]. Repermute the input image from [H, W, C] to [C, H, W].
""" """
def __init__(self): def __init__(self):
pass pass
def __call__(self, im): def __call__(self, im):
im = F.permute(im) im = F.permute(im)
return im return im
class RandomHorizontalFlip: class RandomHorizontalFlip:
""" """
Randomly flip the image horizontally according to given probability. Randomly flip the image horizontally according to given probability.
...@@ -74,6 +79,7 @@ class RandomHorizontalFlip: ...@@ -74,6 +79,7 @@ class RandomHorizontalFlip:
Args: Args:
prob(float): The probability for flipping the image horizontally, default is 0.5. prob(float): The probability for flipping the image horizontally, default is 0.5.
""" """
def __init__(self, prob: float = 0.5): def __init__(self, prob: float = 0.5):
self.prob = prob self.prob = prob
...@@ -90,6 +96,7 @@ class RandomVerticalFlip: ...@@ -90,6 +96,7 @@ class RandomVerticalFlip:
Args: Args:
prob(float): The probability for flipping the image vertically, default is 0.5. prob(float): The probability for flipping the image vertically, default is 0.5.
""" """
def __init__(self, prob: float = 0.5): def __init__(self, prob: float = 0.5):
self.prob = prob self.prob = prob
...@@ -146,6 +153,7 @@ class ResizeByLong: ...@@ -146,6 +153,7 @@ class ResizeByLong:
Args: Args:
long_size(int|list[int]): The target size of long side. long_size(int|list[int]): The target size of long side.
""" """
def __init__(self, long_size: Union[List[int], int]): def __init__(self, long_size: Union[List[int], int]):
self.long_size = long_size self.long_size = long_size
...@@ -162,6 +170,7 @@ class ResizeRangeScaling: ...@@ -162,6 +170,7 @@ class ResizeRangeScaling:
min_value(int): The minimum value for targeted size. min_value(int): The minimum value for targeted size.
max_value(int): The maximum value for targeted size. max_value(int): The maximum value for targeted size.
""" """
def __init__(self, min_value: int = 400, max_value: int = 600): def __init__(self, min_value: int = 400, max_value: int = 600):
if min_value > max_value: if min_value > max_value:
raise ValueError('min_value must be less than max_value, ' raise ValueError('min_value must be less than max_value, '
...@@ -188,6 +197,7 @@ class ResizeStepScaling: ...@@ -188,6 +197,7 @@ class ResizeStepScaling:
scale_step_size(float): Scale interval. scale_step_size(float): Scale interval.
""" """
def __init__(self, min_scale_factor: float = 0.75, max_scale_factor: float = 1.25, scale_step_size: float = 0.25): def __init__(self, min_scale_factor: float = 0.75, max_scale_factor: float = 1.25, scale_step_size: float = 0.25):
if min_scale_factor > max_scale_factor: if min_scale_factor > max_scale_factor:
raise ValueError('min_scale_factor must be less than max_scale_factor, ' raise ValueError('min_scale_factor must be less than max_scale_factor, '
...@@ -224,6 +234,7 @@ class Normalize: ...@@ -224,6 +234,7 @@ class Normalize:
std(list): Standard deviation for normalization. std(list): Standard deviation for normalization.
channel_first(bool): im channel firest or last channel_first(bool): im channel firest or last
""" """
def __init__(self, mean: list = [0.5, 0.5, 0.5], std: list = [0.5, 0.5, 0.5], channel_first: bool = False): def __init__(self, mean: list = [0.5, 0.5, 0.5], std: list = [0.5, 0.5, 0.5], channel_first: bool = False):
self.mean = mean self.mean = mean
self.std = std self.std = std
...@@ -253,6 +264,7 @@ class Padding: ...@@ -253,6 +264,7 @@ class Padding:
target_size(Union[List[int], Tuple[int], int]): Targeted image size. target_size(Union[List[int], Tuple[int], int]): Targeted image size.
im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5]. im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5].
""" """
def __init__(self, target_size: Union[List[int], Tuple[int], int], im_padding_value: list = [127.5, 127.5, 127.5]): def __init__(self, target_size: Union[List[int], Tuple[int], int], im_padding_value: list = [127.5, 127.5, 127.5]):
if isinstance(target_size, list) or isinstance(target_size, tuple): if isinstance(target_size, list) or isinstance(target_size, tuple):
if len(target_size) != 2: if len(target_size) != 2:
...@@ -292,6 +304,7 @@ class RandomPaddingCrop: ...@@ -292,6 +304,7 @@ class RandomPaddingCrop:
crop_size(Union[List[int], Tuple[int], int]): Targeted image size. crop_size(Union[List[int], Tuple[int], int]): Targeted image size.
im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5]. im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5].
""" """
def __init__(self, crop_size, im_padding_value=[127.5, 127.5, 127.5]): def __init__(self, crop_size, im_padding_value=[127.5, 127.5, 127.5]):
if isinstance(crop_size, list) or isinstance(crop_size, tuple): if isinstance(crop_size, list) or isinstance(crop_size, tuple):
if len(crop_size) != 2: if len(crop_size) != 2:
...@@ -339,6 +352,7 @@ class RandomBlur: ...@@ -339,6 +352,7 @@ class RandomBlur:
Args: Args:
prob(float): The probability to blur the image, default is 0.1. prob(float): The probability to blur the image, default is 0.1.
""" """
def __init__(self, prob: float = 0.1): def __init__(self, prob: float = 0.1):
self.prob = prob self.prob = prob
...@@ -370,6 +384,7 @@ class RandomRotation: ...@@ -370,6 +384,7 @@ class RandomRotation:
max_rotation(float): Upper bound of rotation angle. max_rotation(float): Upper bound of rotation angle.
im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5]. im_padding_value(list): Border value for 3 channels, default is [127.5, 127.5, 127.5].
""" """
def __init__(self, max_rotation: float = 15, im_padding_value: list = [127.5, 127.5, 127.5]): def __init__(self, max_rotation: float = 15, im_padding_value: list = [127.5, 127.5, 127.5]):
self.max_rotation = max_rotation self.max_rotation = max_rotation
self.im_padding_value = im_padding_value self.im_padding_value = im_padding_value
...@@ -401,8 +416,6 @@ class RandomRotation: ...@@ -401,8 +416,6 @@ class RandomRotation:
return im return im
class RandomDistort: class RandomDistort:
""" """
Random adjust brightness, contrast, saturation and hue according to the given random range and probability, respectively. Random adjust brightness, contrast, saturation and hue according to the given random range and probability, respectively.
...@@ -418,6 +431,7 @@ class RandomDistort: ...@@ -418,6 +431,7 @@ class RandomDistort:
hue_range(float): Boundary of hue. hue_range(float): Boundary of hue.
hue_prob(float): Probability for disturb the hue of image. hue_prob(float): Probability for disturb the hue of image.
""" """
def __init__(self, def __init__(self,
brightness_range: float = 0.5, brightness_range: float = 0.5,
brightness_prob: float = 0.5, brightness_prob: float = 0.5,
......
...@@ -14,10 +14,8 @@ class TestHubModule(unittest.TestCase): ...@@ -14,10 +14,8 @@ class TestHubModule(unittest.TestCase):
self.assertEqual(results[0]['tag'], ['TIME', 'v', 'q', 'n']) self.assertEqual(results[0]['tag'], ['TIME', 'v', 'q', 'n'])
self.assertEqual(results[1]['word'], ['天气预报', '说', '今天', '要', '下雨']) self.assertEqual(results[1]['word'], ['天气预报', '说', '今天', '要', '下雨'])
self.assertEqual(results[1]['tag'], ['n', 'v', 'TIME', 'v', 'v']) self.assertEqual(results[1]['tag'], ['n', 'v', 'TIME', 'v', 'v'])
self.assertEqual(results[2]['word'], self.assertEqual(results[2]['word'], ['下', '一班', '地铁', '马上', '就要', '到', '了'])
['下', '一班', '地铁', '马上', '就要', '到', '了']) self.assertEqual(results[2]['tag'], ['f', 'm', 'n', 'd', 'v', 'v', 'xc'])
self.assertEqual(results[2]['tag'],
['f', 'm', 'n', 'd', 'v', 'v', 'xc'])
def test_senta(self): def test_senta(self):
senta = hub.Module(name="senta_bilstm") senta = hub.Module(name="senta_bilstm")
...@@ -45,13 +43,11 @@ class TestHubModule(unittest.TestCase): ...@@ -45,13 +43,11 @@ class TestHubModule(unittest.TestCase):
if result['similarity'] > max_score: if result['similarity'] > max_score:
max_score = result['similarity'] max_score = result['similarity']
result_text = result['text_2'] result_text = result['text_2']
print("The most matching with the %s is %s" % (test_text_1[0], print("The most matching with the %s is %s" % (test_text_1[0], result_text))
result_text))
def test_ssd(self): def test_ssd(self):
ssd = hub.Module(name="ssd_mobilenet_v1_pascal") ssd = hub.Module(name="ssd_mobilenet_v1_pascal")
test_img_path = os.path.join( test_img_path = os.path.join(os.path.dirname(__file__), "resources", "test_img_cat.jpg")
os.path.dirname(__file__), "resources", "test_img_cat.jpg")
input_dict = {"image": [test_img_path]} input_dict = {"image": [test_img_path]}
results = ssd.object_detection(data=input_dict) results = ssd.object_detection(data=input_dict)
for result in results: for result in results:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册