提交 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:
......
...@@ -127,9 +127,9 @@ ...@@ -127,9 +127,9 @@
如扫码失败,请添加微信15704308458,并备注“Hub”,运营同学会邀请您入群。 如扫码失败,请添加微信15704308458,并备注“Hub”,运营同学会邀请您入群。
<div id="QuickStart"> <div id="QuickStart">
## 快速开始 ## 快速开始
</div> </div>
```python ```python
......
...@@ -26,21 +26,20 @@ parser.add_argument("--wav", type=str, required=True, help="Audio file to infer. ...@@ -26,21 +26,20 @@ 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(
version='1.0.0', name=args.model_type,
task='sound-cls', version='1.0.0',
num_class=ESC50.num_class, task='sound-cls',
label_map=label_map, num_class=ESC50.num_class,
load_checkpoint=args.checkpoint) label_map=label_map,
load_checkpoint=args.checkpoint)
data = [librosa.load(args.wav, sr=args.sr)[0]] data = [librosa.load(args.wav, sr=args.sr)[0]]
result = model.predict(data, sample_rate=args.sr, batch_size=1, feat_type='mel', use_gpu=True) result = model.predict(data, sample_rate=args.sr, batch_size=1, feat_type='mel', use_gpu=True)
......
...@@ -22,10 +22,11 @@ from paddlehub.datasets import ESC50 ...@@ -22,10 +22,11 @@ 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(
type=ast.literal_eval, "--use_gpu",
default=True, type=ast.literal_eval,
help="Whether use GPU for fine-tuning, input should be True or False") 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("--batch_size", type=int, default=16, help="Total examples' number in batch for training.") parser.add_argument("--batch_size", type=int, default=16, 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")
......
...@@ -73,7 +73,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任 ...@@ -73,7 +73,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任
- train_img_prefix:str. 训练集数据路径前缀 - train_img_prefix:str. 训练集数据路径前缀
- train_ann_file:str, 训练集数据描述文件, - train_ann_file:str, 训练集数据描述文件,
- val_img_prefix:str, 验证集数据路径前缀 - val_img_prefix:str, 验证集数据路径前缀
...@@ -83,7 +83,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任 ...@@ -83,7 +83,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任
- delimiter: "," 数据描述文件采用的分隔符 - delimiter: "," 数据描述文件采用的分隔符
#### resource_config(资源配置) #### resource_config(资源配置)
...@@ -91,7 +91,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任 ...@@ -91,7 +91,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任
- cpu: float, 表示每个搜索进程的cpu分配资源,run_mode=="ray"模式下支持小数分配 - cpu: float, 表示每个搜索进程的cpu分配资源,run_mode=="ray"模式下支持小数分配
#### algo_config(算法配置) #### algo_config(算法配置)
...@@ -128,7 +128,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任 ...@@ -128,7 +128,7 @@ auto-augment软件包目前支持Paddle的图像分类任务和物体检测任
支持1,2模式混合定议 支持1,2模式混合定议
##### 图像分类算子 ##### 图像分类算子
...@@ -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,14 +43,13 @@ class PbaAugment(object): ...@@ -45,14 +43,13 @@ 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, pre_transform: bool = True,
pre_transform: bool = True, stage: str = "search",
stage: str = "search", **kwargs) -> None:
**kwargs) -> None:
""" """
Args: Args:
...@@ -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,15 +154,14 @@ class PicReader(paddle.io.Dataset): ...@@ -164,15 +154,14 @@ 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, transform: Optional[callable] = None,
transform: Optional[callable] = None, class_to_id_dict: Optional[dict] = None,
class_to_id_dict: Optional[dict] = None, cache_img: bool = False,
cache_img: bool = False, **kwargs) -> None:
**kwargs) -> None:
""" """
Args: Args:
...@@ -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,25 +18,23 @@ from paddle.distributed import ParallelEnv ...@@ -18,25 +18,23 @@ 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)
def init_train_and_eval(self, def init_train_and_eval(self,
train_dataset: paddle.io.Dataset, train_dataset: paddle.io.Dataset,
epochs: int = 1, epochs: int = 1,
batch_size: int = 1, batch_size: int = 1,
num_workers: int = 0, num_workers: int = 0,
eval_dataset: paddle.io.Dataset = None, eval_dataset: paddle.io.Dataset = None,
log_interval: int = 10, log_interval: int = 10,
save_interval: int = 10) -> None: save_interval: int = 10) -> None:
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)
...@@ -4,4 +4,4 @@ import paddlehub as hub ...@@ -4,4 +4,4 @@ import paddlehub as hub
if __name__ == '__main__': if __name__ == '__main__':
model = hub.Module(name='user_guided_colorization', load_checkpoint='/PATH/TO/CHECKPOINT') model = hub.Module(name='user_guided_colorization', load_checkpoint='/PATH/TO/CHECKPOINT')
model.set_config(prob=0.1) model.set_config(prob=0.1)
result = model.predict(images=['house.png']) result = model.predict(images=['house.png'])
\ No newline at end of file
...@@ -6,9 +6,10 @@ from paddlehub.datasets import Canvas ...@@ -6,9 +6,10 @@ from paddlehub.datasets import Canvas
if __name__ == '__main__': if __name__ == '__main__':
transform = T.Compose([T.Resize((256, 256), interpolation='NEAREST'), transform = T.Compose(
T.RandomPaddingCrop(crop_size=176), [T.Resize((256, 256), interpolation='NEAREST'),
T.RGB2LAB()], to_rgb=True) T.RandomPaddingCrop(crop_size=176),
T.RGB2LAB()], to_rgb=True)
color_set = Canvas(transform=transform, mode='train') color_set = Canvas(transform=transform, mode='train')
model = hub.Module(name='user_guided_colorization', load_checkpoint='/PATH/TO/CHECKPOINT') model = hub.Module(name='user_guided_colorization', load_checkpoint='/PATH/TO/CHECKPOINT')
......
...@@ -56,7 +56,7 @@ trainer = Trainer(model, optimizer, checkpoint_dir='test_ckpt_img_ocr', use_gpu= ...@@ -56,7 +56,7 @@ trainer = Trainer(model, optimizer, checkpoint_dir='test_ckpt_img_ocr', use_gpu=
* `learning_rate`: 全局学习率。 * `learning_rate`: 全局学习率。
* `parameters`: 待优化模型参数。 * `parameters`: 待优化模型参数。
#### 运行配置 #### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数: `Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
...@@ -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
...@@ -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='ocrnet_hrnetw18_voc', num_classes=2, pretrained='/PATH/TO/CHECKPOINT') model = hub.Module(name='ocrnet_hrnetw18_voc', num_classes=2, pretrained='/PATH/TO/CHECKPOINT')
model.predict(images=["N0007.jpg"], visualization=True) model.predict(images=["N0007.jpg"], visualization=True)
\ No newline at end of file
...@@ -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,36 +34,21 @@ parser.add_argument("--save_interval", type=int, default=1, help="Save checkpoin ...@@ -30,36 +34,21 @@ 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',
version='2.0.1', version='2.0.1',
task='token-cls', task='token-cls',
label_map=label_map, # Required for token classification task label_map=label_map, # Required for token classification task
) )
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)
......
...@@ -61,7 +61,7 @@ Paddle2.0rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详 ...@@ -61,7 +61,7 @@ Paddle2.0rc提供了多种优化器选择,如`SGD`, `Adam`, `Adamax`等,详
* `learning_rate`: 全局学习率。默认为1e-4; * `learning_rate`: 全局学习率。默认为1e-4;
* `parameters`: 待优化模型参数。 * `parameters`: 待优化模型参数。
#### 运行配置 #### 运行配置
`Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数: `Trainer` 主要控制Fine-tune的训练,包含以下可控制的参数:
......
...@@ -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
...@@ -11,4 +11,4 @@ if __name__ == "__main__": ...@@ -11,4 +11,4 @@ if __name__ == "__main__":
styledata = MiniCOCO(transform) styledata = MiniCOCO(transform)
optimizer = paddle.optimizer.Adam(learning_rate=0.0001, parameters=model.parameters()) optimizer = paddle.optimizer.Adam(learning_rate=0.0001, parameters=model.parameters())
trainer = Trainer(model, optimizer, checkpoint_dir='test_style_ckpt') trainer = Trainer(model, optimizer, checkpoint_dir='test_style_ckpt')
trainer.train(styledata, epochs=101, batch_size=4, log_interval=10, save_interval=10) trainer.train(styledata, epochs=101, batch_size=4, log_interval=10, save_interval=10)
\ 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,21 +27,22 @@ from paddlehub.module.module import moduleinfo ...@@ -27,21 +27,22 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn10", @moduleinfo(
version="1.0.0", name="panns_cnn10",
summary="", version="1.0.0",
author="Baidu", summary="",
author_email="", author="Baidu",
type="audio/sound_classification", author_email="",
meta=AudioClassifierModule) type="audio/sound_classification",
meta=AudioClassifierModule)
class PANN(nn.Layer): class PANN(nn.Layer):
def __init__( def __init__(
self, self,
task: str, task: str,
num_class: int = None, num_class: int = None,
label_map: Dict = None, label_map: Dict = None,
load_checkpoint: str = None, load_checkpoint: str = None,
**kwargs, **kwargs,
): ):
super(PANN, self).__init__() super(PANN, self).__init__()
...@@ -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,18 +25,20 @@ class ConvBlock(nn.Layer): ...@@ -25,18 +25,20 @@ 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(
out_channels=out_channels, in_channels=in_channels,
kernel_size=(3, 3), out_channels=out_channels,
stride=(1, 1), kernel_size=(3, 3),
padding=(1, 1), stride=(1, 1),
bias_attr=False) padding=(1, 1),
self.conv2 = nn.Conv2D(in_channels=out_channels, bias_attr=False)
out_channels=out_channels, self.conv2 = nn.Conv2D(
kernel_size=(3, 3), in_channels=out_channels,
stride=(1, 1), out_channels=out_channels,
padding=(1, 1), kernel_size=(3, 3),
bias_attr=False) stride=(1, 1),
padding=(1, 1),
bias_attr=False)
self.bn1 = nn.BatchNorm2D(out_channels) self.bn1 = nn.BatchNorm2D(out_channels)
self.bn2 = nn.BatchNorm2D(out_channels) self.bn2 = nn.BatchNorm2D(out_channels)
......
...@@ -27,21 +27,22 @@ from paddlehub.module.module import moduleinfo ...@@ -27,21 +27,22 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn14", @moduleinfo(
version="1.0.0", name="panns_cnn14",
summary="", version="1.0.0",
author="Baidu", summary="",
author_email="", author="Baidu",
type="audio/sound_classification", author_email="",
meta=AudioClassifierModule) type="audio/sound_classification",
meta=AudioClassifierModule)
class PANN(nn.Layer): class PANN(nn.Layer):
def __init__( def __init__(
self, self,
task: str, task: str,
num_class: int = None, num_class: int = None,
label_map: Dict = None, label_map: Dict = None,
load_checkpoint: str = None, load_checkpoint: str = None,
**kwargs, **kwargs,
): ):
super(PANN, self).__init__() super(PANN, self).__init__()
...@@ -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,18 +25,20 @@ class ConvBlock(nn.Layer): ...@@ -25,18 +25,20 @@ 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(
out_channels=out_channels, in_channels=in_channels,
kernel_size=(3, 3), out_channels=out_channels,
stride=(1, 1), kernel_size=(3, 3),
padding=(1, 1), stride=(1, 1),
bias_attr=False) padding=(1, 1),
self.conv2 = nn.Conv2D(in_channels=out_channels, bias_attr=False)
out_channels=out_channels, self.conv2 = nn.Conv2D(
kernel_size=(3, 3), in_channels=out_channels,
stride=(1, 1), out_channels=out_channels,
padding=(1, 1), kernel_size=(3, 3),
bias_attr=False) stride=(1, 1),
padding=(1, 1),
bias_attr=False)
self.bn1 = nn.BatchNorm2D(out_channels) self.bn1 = nn.BatchNorm2D(out_channels)
self.bn2 = nn.BatchNorm2D(out_channels) self.bn2 = nn.BatchNorm2D(out_channels)
......
...@@ -27,21 +27,22 @@ from paddlehub.module.module import moduleinfo ...@@ -27,21 +27,22 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn6", @moduleinfo(
version="1.0.0", name="panns_cnn6",
summary="", version="1.0.0",
author="Baidu", summary="",
author_email="", author="Baidu",
type="audio/sound_classification", author_email="",
meta=AudioClassifierModule) type="audio/sound_classification",
meta=AudioClassifierModule)
class PANN(nn.Layer): class PANN(nn.Layer):
def __init__( def __init__(
self, self,
task: str, task: str,
num_class: int = None, num_class: int = None,
label_map: Dict = None, label_map: Dict = None,
load_checkpoint: str = None, load_checkpoint: str = None,
**kwargs, **kwargs,
): ):
super(PANN, self).__init__() super(PANN, self).__init__()
...@@ -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,12 +25,13 @@ class ConvBlock5x5(nn.Layer): ...@@ -25,12 +25,13 @@ 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(
out_channels=out_channels, in_channels=in_channels,
kernel_size=(5, 5), out_channels=out_channels,
stride=(1, 1), kernel_size=(5, 5),
padding=(2, 2), stride=(1, 1),
bias_attr=False) padding=(2, 2),
bias_attr=False)
self.bn1 = nn.BatchNorm2D(out_channels) self.bn1 = nn.BatchNorm2D(out_channels)
def forward(self, x, pool_size=(2, 2), pool_type='avg'): def forward(self, x, pool_size=(2, 2), pool_type='avg'):
......
...@@ -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()
......
...@@ -997,4 +997,4 @@ earthstar ...@@ -997,4 +997,4 @@ earthstar
hen-of-the-woods hen-of-the-woods
bolete bolete
ear ear
toilet tissue toilet tissue
\ No newline at end of file
...@@ -193,8 +193,8 @@ class ResNet50_vd(nn.Layer): ...@@ -193,8 +193,8 @@ class ResNet50_vd(nn.Layer):
label_list = [] label_list = []
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)
...@@ -246,9 +246,14 @@ class ResNet50_vd(nn.Layer): ...@@ -246,9 +246,14 @@ class ResNet50_vd(nn.Layer):
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")
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):
...@@ -261,4 +266,4 @@ class ResNet50_vd(nn.Layer): ...@@ -261,4 +266,4 @@ class ResNet50_vd(nn.Layer):
feature = self.pool2d_avg(y) feature = self.pool2d_avg(y)
y = paddle.reshape(feature, shape=[-1, self.pool2d_avg_channels]) y = paddle.reshape(feature, shape=[-1, self.pool2d_avg_channels])
y = self.out(y) y = self.out(y)
return y, feature return y, feature
\ No newline at end of file
...@@ -12,13 +12,13 @@ $ hub run barometer_reader --input_path "/PATH/TO/IMAGE" ...@@ -12,13 +12,13 @@ $ hub run barometer_reader --input_path "/PATH/TO/IMAGE"
## API ## API
```python ```python
def predict(self, def predict(self,
im_file: Union[str, np.ndarray], im_file: Union[str, np.ndarray],
score_threshold: float = 0.5, score_threshold: float = 0.5,
seg_batch_size: int = 2, seg_batch_size: int = 2,
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'):
``` ```
...@@ -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,26 +43,28 @@ def cv2_to_base64(image: np.ndarray): ...@@ -44,26 +43,28 @@ 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(
type="CV/image_editing", name="barometer_reader",
author="paddlepaddle", type="CV/image_editing",
author_email="", author="paddlepaddle",
summary="meter_reader implements the detection and automatic reading of traditional mechanical pointer meters based on Meter detection and pointer segmentation.", author_email="",
version="1.0.0") 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")
class BarometerReader(nn.Layer): class BarometerReader(nn.Layer):
def __init__(self): def __init__(self):
super(BarometerReader, self).__init__() super(BarometerReader, self).__init__()
self.detector = pdx.load_model(os.path.join(self.directory, 'meter_det_inference_model')) self.detector = pdx.load_model(os.path.join(self.directory, 'meter_det_inference_model'))
self.segmenter = pdx.load_model(os.path.join(self.directory, 'meter_seg_inference_model')) self.segmenter = pdx.load_model(os.path.join(self.directory, 'meter_seg_inference_model'))
self.seg_transform = T.Compose([T.Normalize()]) self.seg_transform = T.Compose([T.Normalize()])
def read_process(self, label_maps: np.ndarray): def read_process(self, label_maps: np.ndarray):
line_images = self.creat_line_image(label_maps) line_images = self.creat_line_image(label_maps)
scale_data, pointer_data = self.convert_1d_data(line_images) scale_data, pointer_data = self.convert_1d_data(line_images)
self.scale_mean_filtration(scale_data) self.scale_mean_filtration(scale_data)
result = self.get_meter_reader(scale_data, pointer_data) result = self.get_meter_reader(scale_data, pointer_data)
return result return result
def creat_line_image(self, meter_image: np.ndarray): def creat_line_image(self, meter_image: np.ndarray):
line_image = np.zeros((LINE_HEIGHT, LINE_WIDTH), dtype=np.uint8) line_image = np.zeros((LINE_HEIGHT, LINE_WIDTH), dtype=np.uint8)
for row in range(LINE_HEIGHT): for row in range(LINE_HEIGHT):
...@@ -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,20 +135,19 @@ class BarometerReader(nn.Layer): ...@@ -135,20 +135,19 @@ 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
def predict(self, def predict(self,
im_file: Union[str, np.ndarray], im_file: Union[str, np.ndarray],
score_threshold: float = 0.5, score_threshold: float = 0.5,
seg_batch_size: int = 2, seg_batch_size: int = 2,
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')
else: else:
...@@ -168,21 +167,15 @@ class BarometerReader(nn.Layer): ...@@ -168,21 +167,15 @@ class BarometerReader(nn.Layer):
xmax = min(im.shape[1], int(xmin + w - 1)) xmax = min(im.shape[1], int(xmin + w - 1))
ymax = min(im.shape[0], int(ymin + h - 1)) ymax = min(im.shape[0], int(ymin + h - 1))
sub_image = im[ymin:(ymax + 1), xmin:(xmax + 1), :] sub_image = im[ymin:(ymax + 1), xmin:(xmax + 1), :]
# Resize the image with shape (METER_SHAPE, METER_SHAPE) # Resize the image with shape (METER_SHAPE, METER_SHAPE)
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)
meter_num = len(resized_meters) meter_num = len(resized_meters)
seg_results = list() seg_results = list()
for i in range(0, meter_num, seg_batch_size): for i in range(0, meter_num, seg_batch_size):
...@@ -190,22 +183,20 @@ class BarometerReader(nn.Layer): ...@@ -190,22 +183,20 @@ class BarometerReader(nn.Layer):
meter_images = list() meter_images = list()
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)
for i in range(len(result)): for i in range(len(result)):
result[i]['label_map'] = cv2.erode(result[i]['label_map'], kernel) result[i]['label_map'] = cv2.erode(result[i]['label_map'], kernel)
seg_results.extend(result) seg_results.extend(result)
results = list() results = list()
for i, seg_result in enumerate(seg_results): for i, seg_result in enumerate(seg_results):
result = self.read_process(seg_result['label_map']) result = self.read_process(seg_result['label_map'])
results.append(result) results.append(result)
meter_values = list() meter_values = list()
for i, result in enumerate(results): for i, result in enumerate(results):
if result['scale_num'] > TYPE_THRESHOLD: if result['scale_num'] > TYPE_THRESHOLD:
...@@ -222,9 +213,9 @@ class BarometerReader(nn.Layer): ...@@ -222,9 +213,9 @@ class BarometerReader(nn.Layer):
visual_results.append(res) visual_results.append(res)
if visualization: if visualization:
pdx.det.visualize(im_file, visual_results, -1, save_dir=save_dir) pdx.det.visualize(im_file, visual_results, -1, save_dir=save_dir)
return visual_results return visual_results
@serving @serving
def serving_method(self, image: str, **kwargs): def serving_method(self, image: str, **kwargs):
""" """
...@@ -239,7 +230,7 @@ class BarometerReader(nn.Layer): ...@@ -239,7 +230,7 @@ class BarometerReader(nn.Layer):
result['category'] = str(result['category']) result['category'] = str(result['category'])
res.append(result) res.append(result)
return res return res
@runnable @runnable
def run_cmd(self, argvs: list): def run_cmd(self, argvs: list):
""" """
...@@ -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), width=draw_thickness,
(xmin, ymin)], fill=color)
width=draw_thickness,
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) P.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
self.compose = [P.Resize(max_size=1333), P.Permute(),
P.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), P.PadStride(stride=32)
P.Permute(), ]
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],
...@@ -135,7 +125,7 @@ class DetectorSOLOv2(Detector): ...@@ -135,7 +125,7 @@ class DetectorSOLOv2(Detector):
threshold (float): threshold of predicted box' score threshold (float): threshold of predicted box' score
visualization (bool): Whether to save visualization result. visualization (bool): Whether to save visualization result.
save_dir (str): save path. save_dir (str): save path.
''' '''
inputs, im_info = self.transform(image) inputs, im_info = self.transform(image)
...@@ -148,14 +138,11 @@ class DetectorSOLOv2(Detector): ...@@ -148,14 +138,11 @@ 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:
if not os.path.exists(save_dir): if not os.path.exists(save_dir):
os.makedirs(save_dir) os.makedirs(save_dir)
......
...@@ -78,20 +78,13 @@ class Resize(object): ...@@ -78,20 +78,13 @@ 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)
im = im.resize((int(resize_w), int(resize_h)), self.interp) im = im.resize((int(resize_w), int(resize_h)), self.interp)
...@@ -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
......
...@@ -206,11 +206,8 @@ class BodyPoseModel(nn.Layer): ...@@ -206,11 +206,8 @@ class BodyPoseModel(nn.Layer):
img_name = str(time.time()) + '.png' img_name = str(time.time()) + '.png'
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,11 +218,11 @@ class BodyPoseModel(nn.Layer): ...@@ -221,11 +218,11 @@ 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'])
return final return final
@runnable @runnable
...@@ -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
...@@ -312,7 +312,7 @@ class Candidate: ...@@ -312,7 +312,7 @@ class Candidate:
subset = np.delete(subset, deleteIdx, axis=0) subset = np.delete(subset, deleteIdx, axis=0)
return candidate, subset return candidate, subset
class ResizeScaling: class ResizeScaling:
"""Resize images by scaling method. """Resize images by scaling method.
...@@ -341,4 +341,4 @@ def base64_to_cv2(b64str: str): ...@@ -341,4 +341,4 @@ def base64_to_cv2(b64str: str):
data = base64.b64decode(b64str.encode('utf8')) data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8) data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR) data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data return data
\ No newline at end of file
...@@ -51,7 +51,7 @@ class HandPoseModel(nn.Layer): ...@@ -51,7 +51,7 @@ class HandPoseModel(nn.Layer):
def __init__(self, load_checkpoint: str = None): def __init__(self, load_checkpoint: str = None):
super(HandPoseModel, self).__init__() super(HandPoseModel, self).__init__()
self.norm_func = T.Normalize(std=[1, 1, 1]) self.norm_func = T.Normalize(std=[1, 1, 1])
self.resize_func = P.ResizeScaling() self.resize_func = P.ResizeScaling()
self.hand_detect = P.HandDetect() self.hand_detect = P.HandDetect()
...@@ -170,14 +170,18 @@ class HandPoseModel(nn.Layer): ...@@ -170,14 +170,18 @@ 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):
org_img = cv2.imread(img) org_img = cv2.imread(img)
else: else:
org_img = img org_img = img
if not self.body_model: if not self.body_model:
self.body_model = hub.Module(name='openpose_body_estimation') self.body_model = hub.Module(name='openpose_body_estimation')
self.body_model.eval() self.body_model.eval()
...@@ -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):
...@@ -201,10 +209,8 @@ class HandPoseModel(nn.Layer): ...@@ -201,10 +209,8 @@ class HandPoseModel(nn.Layer):
img_name = str(time.time()) + '.png' img_name = str(time.time()) + '.png'
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,11 +221,11 @@ class HandPoseModel(nn.Layer): ...@@ -215,11 +221,11 @@ 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
@runnable @runnable
def run_cmd(self, argvs: list): def run_cmd(self, argvs: list):
""" """
...@@ -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
...@@ -213,8 +213,8 @@ class DrawHandPose: ...@@ -213,8 +213,8 @@ class DrawHandPose:
bg.draw() bg.draw()
canvas = np.frombuffer(bg.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3) canvas = np.frombuffer(bg.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3)
return canvas return canvas
class ResizeScaling: class ResizeScaling:
"""Resize images by scaling method. """Resize images by scaling method.
...@@ -231,7 +231,8 @@ class ResizeScaling: ...@@ -231,7 +231,8 @@ class ResizeScaling:
scale = scale_search * self.target / img.shape[0] scale = scale_search * self.target / img.shape[0]
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)
...@@ -134,4 +127,4 @@ class ExtremeC3_Portrait_Segmentation(Layer): ...@@ -134,4 +127,4 @@ class ExtremeC3_Portrait_Segmentation(Layer):
results = self.postprocess(outputs, datas, output_dir, visualization) results = self.postprocess(outputs, datas, output_dir, visualization)
# 返回结果 # 返回结果
return results return results
\ No newline at end of file
...@@ -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,22 +30,20 @@ def SyncBatchNorm(*args, **kwargs): ...@@ -30,22 +30,20 @@ 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, stride: int = 1,
stride: int = 1, dilation: int = 1,
dilation: int = 1, groups: int = 1,
groups: int = 1, is_vd_mode: bool = False,
is_vd_mode: bool = False, act: str = None,
act: str = None, name: str = None):
name: str = None):
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,27 +151,19 @@ class Decoder(nn.Layer): ...@@ -160,27 +151,19 @@ 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)
x = self.conv(x) x = self.conv(x)
return x return x
\ No newline at end of file
...@@ -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,35 +63,19 @@ class BasicBlock(nn.Layer): ...@@ -67,35 +63,19 @@ 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]
num_filters = [64, 128, 256, 512] num_filters = [64, 128, 256, 512]
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,
...@@ -134,4 +111,4 @@ class ResNet50_vd(nn.Layer): ...@@ -134,4 +111,4 @@ class ResNet50_vd(nn.Layer):
for block in stage: for block in stage:
y = block(y) y = block(y)
feat_list.append(y) feat_list.append(y)
return feat_list return feat_list
\ No newline at end of file
...@@ -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]
...@@ -609,4 +529,4 @@ class FuseLayers(nn.Layer): ...@@ -609,4 +529,4 @@ class FuseLayers(nn.Layer):
residual = F.relu(residual) residual = F.relu(residual)
outs.append(residual) outs.append(residual)
return outs return outs
\ No newline at end of file
...@@ -30,22 +30,20 @@ def SyncBatchNorm(*args, **kwargs): ...@@ -30,22 +30,20 @@ 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, stride: int = 1,
stride: int = 1, dilation: int = 1,
dilation: int = 1, groups: int = 1,
groups: int = 1, is_vd_mode: bool = False,
is_vd_mode: bool = False, act: str = None,
act: str = None, name: str = None):
name: str = None):
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,24 +284,16 @@ class ASPPModule(nn.Layer): ...@@ -322,24 +284,16 @@ 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)
x = self.conv_bn_relu(x) x = self.conv_bn_relu(x)
x = self.dropout(x) x = self.dropout(x)
return x return x
\ No newline at end of file
...@@ -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)
...@@ -240,4 +221,4 @@ class ObjectAttentionBlock(nn.Layer): ...@@ -240,4 +221,4 @@ class ObjectAttentionBlock(nn.Layer):
context = paddle.reshape(context, (n, self.key_channels, h, w)) context = paddle.reshape(context, (n, self.key_channels, h, w))
context = self.f_up(context) context = self.f_up(context)
return context return context
\ No newline at end of file
...@@ -167,4 +167,4 @@ paddlehub >= 2.0.0 ...@@ -167,4 +167,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0 ...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0 ...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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
......
...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0 ...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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
......
...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0 ...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0 ...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0 ...@@ -166,4 +166,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -152,11 +152,11 @@ paddlehub >= 2.0.0 ...@@ -152,11 +152,11 @@ paddlehub >= 2.0.0
* 1.0.0 * 1.0.0
初始发布 初始发布
* 2.0.0 * 2.0.0
全面升级动态图,接口有所变化。任务名称调整,增加序列标注任务`token-cls` 全面升级动态图,接口有所变化。任务名称调整,增加序列标注任务`token-cls`
* 2.0.1 * 2.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -159,4 +159,4 @@ paddlehub >= 2.0.0 ...@@ -159,4 +159,4 @@ paddlehub >= 2.0.0
* 2.0.1 * 2.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -151,11 +151,11 @@ paddlehub >= 2.0.0 ...@@ -151,11 +151,11 @@ paddlehub >= 2.0.0
* 1.0.0 * 1.0.0
初始发布 初始发布
* 2.0.0 * 2.0.0
全面升级动态图,接口有所变化。任务名称调整,增加序列标注任务`token-cls` 全面升级动态图,接口有所变化。任务名称调整,增加序列标注任务`token-cls`
* 2.0.1 * 2.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -158,4 +158,4 @@ paddlehub >= 2.0.0 ...@@ -158,4 +158,4 @@ paddlehub >= 2.0.0
* 2.0.1 * 2.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -154,4 +154,4 @@ paddlehub >= 2.0.0 ...@@ -154,4 +154,4 @@ paddlehub >= 2.0.0
* 1.0.1 * 1.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -154,4 +154,4 @@ paddlehub >= 2.0.0 ...@@ -154,4 +154,4 @@ paddlehub >= 2.0.0
* 1.0.1 * 1.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -154,4 +154,4 @@ paddlehub >= 2.0.0 ...@@ -154,4 +154,4 @@ paddlehub >= 2.0.0
* 1.0.1 * 1.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -187,4 +187,4 @@ paddlehub >= 2.0.0 ...@@ -187,4 +187,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -179,4 +179,4 @@ paddlehub >= 2.0.0 ...@@ -179,4 +179,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -175,4 +175,4 @@ paddlehub >= 2.0.0 ...@@ -175,4 +175,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ 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-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
...@@ -174,4 +174,4 @@ paddlehub >= 2.0.0 ...@@ -174,4 +174,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ 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)
...@@ -159,4 +159,4 @@ paddlehub >= 2.0.0 ...@@ -159,4 +159,4 @@ paddlehub >= 2.0.0
* 2.0.1 * 2.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -159,4 +159,4 @@ paddlehub >= 2.0.0 ...@@ -159,4 +159,4 @@ paddlehub >= 2.0.0
* 2.0.1 * 2.0.1
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -161,4 +161,4 @@ paddlehub >= 2.0.0 ...@@ -161,4 +161,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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)
...@@ -161,4 +161,4 @@ paddlehub >= 2.0.0 ...@@ -161,4 +161,4 @@ paddlehub >= 2.0.0
* 2.0.2 * 2.0.2
增加文本匹配任务`text-matching` 增加文本匹配任务`text-matching`
\ No newline at end of file
...@@ -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,12 +29,13 @@ class DataFormatError(Exception): ...@@ -29,12 +29,13 @@ class DataFormatError(Exception):
self.args = args self.args = args
@moduleinfo(name="simnet_bow", @moduleinfo(
version="1.2.0", name="simnet_bow",
summary="Baidu's open-source similarity network model based on bow_pairwise.", version="1.2.0",
author="baidu-nlp", summary="Baidu's open-source similarity network model based on bow_pairwise.",
author_email="", author="baidu-nlp",
type="nlp/sentiment_analysis") author_email="",
type="nlp/sentiment_analysis")
class SimnetBow(hub.Module): class SimnetBow(hub.Module):
def _initialize(self): def _initialize(self):
""" """
...@@ -106,40 +107,42 @@ class SimnetBow(hub.Module): ...@@ -106,40 +107,42 @@ 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(
size=[dict_dim, 128], input=text_1,
is_sparse=True, size=[dict_dim, 128],
padding_idx=dict_dim - 1, is_sparse=True,
dtype='float32', padding_idx=dict_dim - 1,
param_attr=w_param_attrs) dtype='float32',
param_attr=w_param_attrs)
emb_1_name = emb_1.name emb_1_name = emb_1.name
data_list = [text_1] data_list = [text_1]
emb_name_list = [emb_1_name] emb_name_list = [emb_1_name]
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(
size=[dict_dim, 128], input=text_2,
is_sparse=True, size=[dict_dim, 128],
padding_idx=dict_dim - 1, is_sparse=True,
dtype='float32', padding_idx=dict_dim - 1,
param_attr=w_param_attrs) dtype='float32',
param_attr=w_param_attrs)
emb_2_name = emb_2.name emb_2_name = emb_2.name
data_list.append(text_2) data_list.append(text_2)
emb_name_list.append(emb_2_name) emb_name_list.append(emb_2_name)
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(
size=[dict_dim, 128], input=text_3,
is_sparse=True, size=[dict_dim, 128],
padding_idx=dict_dim - 1, is_sparse=True,
dtype='float32', padding_idx=dict_dim - 1,
param_attr=w_param_attrs) dtype='float32',
param_attr=w_param_attrs)
emb_3_name = emb_3.name emb_3_name = emb_3.name
data_list.append(text_3) data_list.append(text_3)
emb_name_list.append(emb_3_name) emb_name_list.append(emb_3_name)
...@@ -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")
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册