提交 d6b6c6ff 编写于 作者: W wuzewu

Fix travis-ci issue.

上级 b100dc5d
......@@ -27,6 +27,7 @@ install:
else
pip install --upgrade paddlepaddle;
pip install -r requirements.txt;
pip install yapf==0.26.0
fi
notifications:
......
......@@ -26,16 +26,15 @@ parser.add_argument("--wav", type=str, required=True, help="Audio file to infer.
parser.add_argument("--sr", type=int, default=44100, help="Sample rate of inference audio.")
parser.add_argument("--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("--checkpoint",
type=str,
default='./checkpoint/best_model/model.pdparams',
help="Checkpoint of model.")
parser.add_argument(
"--checkpoint", type=str, default='./checkpoint/best_model/model.pdparams', help="Checkpoint of model.")
args = parser.parse_args()
if __name__ == '__main__':
label_map = {idx: label for idx, label in enumerate(ESC50.label_list)}
model = hub.Module(name=args.model_type,
model = hub.Module(
name=args.model_type,
version='1.0.0',
task='sound-cls',
num_class=ESC50.num_class,
......
......@@ -22,7 +22,8 @@ from paddlehub.datasets import ESC50
parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--num_epoch", type=int, default=50, help="Number of epoches for fine-tuning.")
parser.add_argument("--use_gpu",
parser.add_argument(
"--use_gpu",
type=ast.literal_eval,
default=True,
help="Whether use GPU for fine-tuning, input should be True or False")
......
......@@ -173,8 +173,3 @@ bash search.sh
cd PaddleHub/demo/autaug/
bash train.sh
```
......@@ -23,6 +23,7 @@ logger = log.get_logger(level=logging.INFO)
import auto_augment
auto_augment_path = auto_augment.__file__
class HubFitterClassifer(object):
"""Trains an instance of the Model class."""
......@@ -43,8 +44,7 @@ class HubFitterClassifer(object):
# `import paddle`. Otherwise, it would not take any effect.
set_paddle_flags(
# 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 paddlehub as hub
from paddlehub_utils.trainer import CustomTrainer
......@@ -63,23 +63,18 @@ class HubFitterClassifer(object):
paddle.disable_static(paddle.CUDAPlace(paddle.distributed.get_rank()))
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)
optimizer = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
trainer = CustomTrainer(
model=model,
optimizer=optimizer,
checkpoint_dir='img_classification_ckpt')
model = hub.Module(
name=hparams["task_config"]["classifier"]["model_name"],
label_list=self.class_to_id_dict.keys(),
load_checkpoint=None)
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
trainer = CustomTrainer(model=model, optimizer=optimizer, checkpoint_dir='img_classification_ckpt')
self.model = model
self.optimizer = optimizer
trainer.init_train_and_eval(
train_dataset,
epochs=100,
batch_size=32,
eval_dataset=eval_dataset,
save_interval=1)
train_dataset, epochs=100, batch_size=32, eval_dataset=eval_dataset, save_interval=1)
self.trainer = trainer
def _fit_param(self, show: bool = False) -> None:
......@@ -123,8 +118,7 @@ class HubFitterClassifer(object):
"""
self.hparams = new_hparams
self.trainer.train_loader.dataset.reset_policy(
new_hparams.search_space)
self.trainer.train_loader.dataset.reset_policy(new_hparams.search_space)
return None
def save_model(self, checkpoint_dir: str, step: Optional[str] = None) -> str:
......@@ -134,8 +128,7 @@ class HubFitterClassifer(object):
step: If provided, creates a checkpoint with the given step
number, instead of overwriting the existing checkpoints.
"""
checkpoint_path = os.path.join(checkpoint_dir,
'epoch') + '-' + str(step)
checkpoint_path = os.path.join(checkpoint_dir, 'epoch') + '-' + str(step)
logger.info('Saving model checkpoint to {}'.format(checkpoint_path))
self.trainer.save_model(os.path.join(checkpoint_path, "checkpoint"))
......@@ -145,8 +138,7 @@ class HubFitterClassifer(object):
"""Loads a checkpoint with the architecture structure stored in the name."""
ckpt_path = os.path.join(checkpoint_path, "checkpoint")
self.trainer.load_model(ckpt_path)
logger.info(
'Loaded child model checkpoint from {}'.format(checkpoint_path))
logger.info('Loaded child model checkpoint from {}'.format(checkpoint_path))
def eval_child_model(self, mode: str, pass_id: int = 0) -> dict:
"""Evaluate the child model.
......@@ -204,10 +196,7 @@ class HubFitterClassifer(object):
"""Trains the model `m` for one epoch."""
start_time = time.time()
train_acc = self.train_one_epoch(curr_epoch)
logger.info(
'Epoch:{} time(min): {}'.format(
curr_epoch,
(time.time() - start_time) / 60.0))
logger.info('Epoch:{} time(min): {}'.format(curr_epoch, (time.time() - start_time) / 60.0))
return train_acc
def _compute_final_accuracies(self, iteration: int) -> dict:
......@@ -228,8 +217,7 @@ class HubFitterClassifer(object):
self._fit_param()
train_acc = self._run_training_loop(epoch)
valid_acc = self.eval_child_model(mode="val", pass_id=epoch)
logger.info('valid acc: {}'.format(
valid_acc))
logger.info('valid acc: {}'.format(valid_acc))
all_metric = {}
all_metric.update(train_acc)
all_metric.update(valid_acc)
......
......@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# -*- coding: utf-8 -*-
# *******************************************************************************
#
......@@ -36,8 +35,7 @@ import paddlehub.vision.transforms as transforms
from PIL import ImageFile
from auto_augment.autoaug.transform.autoaug_transform import AutoAugTransform
ImageFile.LOAD_TRUNCATED_IMAGES = True
__imagenet_stats = {'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225]}
__imagenet_stats = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
class PbaAugment(object):
......@@ -45,8 +43,7 @@ class PbaAugment(object):
pytorch 分类 PbaAugment transform
"""
def __init__(
self,
def __init__(self,
input_size: int = 224,
scale_size: int = 256,
normalize: Optional[list] = None,
......@@ -64,27 +61,20 @@ class PbaAugment(object):
"""
if normalize is None:
normalize = {
'mean': [
0.485, 0.456, 0.406], 'std': [
0.229, 0.224, 0.225]}
normalize = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
policy = kwargs["policy"]
assert stage in ["search", "train"]
train_epochs = kwargs["hp_policy_epochs"]
self.auto_aug_transform = AutoAugTransform.create(
policy, stage=stage, train_epochs=train_epochs)
self.auto_aug_transform = AutoAugTransform.create(policy, stage=stage, train_epochs=train_epochs)
#self.auto_aug_transform = PbtAutoAugmentClassiferTransform(conf)
if pre_transform:
self.pre_transform = transforms.Resize(input_size)
self.post_transform = transforms.Compose(
transforms=[
transforms.Permute(),
transforms.Normalize(**normalize, channel_first=True)
],
channel_first = False
)
transforms=[transforms.Permute(),
transforms.Normalize(**normalize, channel_first=True)],
channel_first=False)
self.cur_epoch = 0
def set_epoch(self, indx: int) -> None:
......@@ -164,8 +154,7 @@ class PicReader(paddle.io.Dataset):
PicReader
"""
def __init__(
self,
def __init__(self,
root_path: str,
list_file: str,
meta: bool = False,
......@@ -222,8 +211,7 @@ class PicReader(paddle.io.Dataset):
img = cv2.resize(img, (scale_size, scale_size))
self.cache_img_buff[image_path] = img
except BaseException:
print("img_path:{} can not by cv2".format(
image_path).format(image_path))
print("img_path:{} can not by cv2".format(image_path).format(image_path))
pass
......@@ -265,9 +253,7 @@ class PicReader(paddle.io.Dataset):
with open(self.list_file) as f:
lines = f.read().splitlines()
print(
"PicReader:: found {} picture in `{}'".format(
len(lines), self.list_file))
print("PicReader:: found {} picture in `{}'".format(len(lines), self.list_file))
for i, line in enumerate(lines):
record = re.split(delimiter, line)
# record = line.split()
......@@ -401,20 +387,12 @@ def _read_classes(csv_file: str) -> dict:
class_name = row.strip()
# print(class_id, class_name)
except ValueError:
six.raise_from(
ValueError(
'line {}: format should be \'class_name\''.format(line)),
None)
six.raise_from(ValueError('line {}: format should be \'class_name\''.format(line)), None)
class_id = _parse(
line,
int,
'line {}: malformed class ID: {{}}'.format(line))
class_id = _parse(line, int, 'line {}: malformed class ID: {{}}'.format(line))
if class_name in result:
raise ValueError(
'line {}: duplicate class name: \'{}\''.format(
line, class_name))
raise ValueError('line {}: duplicate class name: \'{}\''.format(line, class_name))
result[class_name] = class_id
return result
......@@ -439,10 +417,7 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple:
epochs = hparams.task_config.classifier.epochs
no_cache_img = hparams.task_config.classifier.get("no_cache_img", False)
normalize = {
'mean': [
0.485, 0.456, 0.406], 'std': [
0.229, 0.224, 0.225]}
normalize = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
if TrainTransform is None:
TrainTransform = PbaAugment(
......@@ -453,10 +428,7 @@ def _init_loader(hparams: dict, TrainTransform=None) -> tuple:
hp_policy_epochs=epochs,
)
delimiter = hparams.data_config.delimiter
kwargs = dict(
conf=hparams,
delimiter=delimiter
)
kwargs = dict(conf=hparams, delimiter=delimiter)
if hparams.task_config.classifier.use_class_map:
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:
list_file=val_list,
transform=transforms.Compose(
transforms=[
transforms.Resize(
(224,
224)),
transforms.Resize((224, 224)),
transforms.Permute(),
transforms.Normalize(
**normalize, channel_first=True)],
channel_first = False),
transforms.Normalize(**normalize, channel_first=True)
],
channel_first=False),
class_to_id_dict=class_to_id_dict,
cache_img=not no_cache_img,
**kwargs)
......
......@@ -18,6 +18,7 @@ from paddle.distributed import ParallelEnv
from paddlehub.utils.log import logger
from paddlehub.utils.utils import Timer
class CustomTrainer(Trainer):
def __init__(self, **kwargs) -> None:
super(CustomTrainer, self).__init__(**kwargs)
......@@ -33,10 +34,7 @@ class CustomTrainer(Trainer):
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)
def init_train(self,
train_dataset: paddle.io.Dataset,
batch_size: int = 1,
num_workers: int = 0) -> tuple:
def init_train(self, train_dataset: paddle.io.Dataset, batch_size: int = 1, num_workers: int = 0) -> tuple:
use_gpu = True
place = paddle.CUDAPlace(ParallelEnv().dev_id) if use_gpu else paddle.CPUPlace()
paddle.disable_static(place)
......@@ -47,7 +45,8 @@ class CustomTrainer(Trainer):
train_dataset, batch_sampler=batch_sampler, places=place, num_workers=num_workers, return_list=True)
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_metrics = defaultdict(int)
self.model.train()
......@@ -70,15 +69,13 @@ class CustomTrainer(Trainer):
if self.use_vdl:
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,
steps_per_epoch)
print_msg = 'Epoch={}/{}, Step={}/{}'.format(current_epoch, epochs, batch_idx + 1, steps_per_epoch)
print_msg += ' loss={:.4f}'.format(avg_loss)
for metric, value in avg_metrics.items():
value /= log_interval
if self.use_vdl:
self.log_writer.add_scalar(
tag='TRAIN/{}'.format(metric), step=timer.current_step, value=value)
self.log_writer.add_scalar(tag='TRAIN/{}'.format(metric), step=timer.current_step, value=value)
print_msg += ' {}={:.4f}'.format(metric, value)
print_msg += ' lr={:.6f} step/sec={:.2f} | ETA {}'.format(lr, timer.timing, timer.eta)
......@@ -139,9 +136,7 @@ class CustomTrainer(Trainer):
self.save_model(best_model_path)
self._save_metrics()
metric_msg = [
'{}={:.4f}'.format(metric, value) for metric, value in self.best_metrics.items()
]
metric_msg = ['{}={:.4f}'.format(metric, value) for metric, value in self.best_metrics.items()]
metric_msg = ' '.join(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.utils.yaml_config import get_config
from hub_fitter import HubFitterClassifer
......@@ -9,14 +8,21 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser()
parser.add_argument("--config",help="config file",)
parser.add_argument("--workspace",default=None, help="work_space",)
parser.add_argument(
"--config",
help="config file",
)
parser.add_argument(
"--workspace",
default=None,
help="work_space",
)
def main():
search_test()
def search_test():
args = parser.parse_args()
config = args.config
......@@ -38,14 +44,13 @@ def search_test():
resource_config=resource_config,
data_config=data_config,
search_space=search_space,
fitter=HubFitterClassifer
)
fitter=HubFitterClassifer)
result = exper.search() # 开始搜索任务
policy = result.get_best_policy() # 最佳策略获取, policy格式见 搜索结果应用格式
print("policy is:{}".format(policy))
dump_path = os.path.join(workspace, "auto_aug_config.json")
result.dump_best_policy(
path=dump_path)
result.dump_best_policy(path=dump_path)
if __name__ == "__main__":
main()
......@@ -24,9 +24,20 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser()
parser.add_argument("--config",help="config file",)
parser.add_argument("--workspace",default=None, help="work_space",)
parser.add_argument("--policy",default=None, help="data aug policy",)
parser.add_argument(
"--config",
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__':
args = parser.parse_args()
......@@ -39,10 +50,7 @@ if __name__ == '__main__':
input_size = task_config.classifier.input_size
scale_size = task_config.classifier.scale_size
normalize = {
'mean': [
0.485, 0.456, 0.406], 'std': [
0.229, 0.224, 0.225]}
normalize = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
epochs = task_config.classifier.epochs
policy = args.policy
......@@ -50,13 +58,11 @@ if __name__ == '__main__':
print("use normal train transform")
TrainTransform = transforms.Compose(
transforms=[
transforms.Resize(
(input_size,
input_size)),
transforms.Resize((input_size, input_size)),
transforms.Permute(),
transforms.Normalize(
**normalize, channel_first=True)],
channel_first = False)
transforms.Normalize(**normalize, channel_first=True)
],
channel_first=False)
else:
TrainTransform = PbaAugment(
input_size=input_size,
......@@ -64,17 +70,12 @@ if __name__ == '__main__':
normalize=normalize,
policy=policy,
hp_policy_epochs=epochs,
stage="train"
)
stage="train")
train_dataset, eval_dataset = _init_loader(config, TrainTransform=TrainTransform)
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(
learning_rate=0.001, parameters=model.parameters())
trainer = CustomTrainer(
model=model,
optimizer=optimizer,
checkpoint_dir='img_classification_ckpt')
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
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)
......@@ -6,7 +6,8 @@ from paddlehub.datasets import Canvas
if __name__ == '__main__':
transform = T.Compose([T.Resize((256, 256), interpolation='NEAREST'),
transform = T.Compose(
[T.Resize((256, 256), interpolation='NEAREST'),
T.RandomPaddingCrop(crop_size=176),
T.RGB2LAB()], to_rgb=True)
......
......@@ -159,5 +159,3 @@ https://github.com/PaddlePaddle/PaddleSeg
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
......@@ -24,9 +24,7 @@ if __name__ == '__main__':
'华裔作家韩素音女士曾三次到大足,称“大足石窟是一座未被开发的金矿”。',
]
data = [[split_char.join(text)] for text in text_a]
label_map = {
idx: label for idx, label in enumerate(label_list)
}
label_map = {idx: label for idx, label in enumerate(label_list)}
model = hub.Module(
name='ernie_tiny',
......@@ -39,4 +37,3 @@ if __name__ == '__main__':
results = model.predict(data=data, max_seq_len=128, batch_size=1, use_gpu=True)
for idx, text in enumerate(text_a):
print(f'Text:\n{text} \nLable: \n{", ".join(results[idx][1:len(text)+1])} \n')
......@@ -21,7 +21,11 @@ import argparse
parser = argparse.ArgumentParser(__doc__)
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("--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.")
......@@ -30,12 +34,9 @@ parser.add_argument("--save_interval", type=int, default=1, help="Save checkpoin
args = parser.parse_args()
if __name__ == '__main__':
label_list = MSRA_NER.label_list
label_map = {
idx: label for idx, label in enumerate(label_list)
}
label_map = {idx: label for idx, label in enumerate(label_list)}
model = hub.Module(
name='ernie_tiny',
......@@ -45,21 +46,9 @@ if __name__ == '__main__':
)
tokenizer = model.get_tokenizer()
train_dataset = MSRA_NER(
tokenizer=tokenizer,
max_seq_len=args.max_seq_len,
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'
)
train_dataset = MSRA_NER(tokenizer=tokenizer, max_seq_len=args.max_seq_len, 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())
trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu)
......
......@@ -3,4 +3,4 @@ import paddlehub as hub
if __name__ == '__main__':
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')
\ No newline at end of file
result = model.predict(origin=["venice-boat.jpg"], style="candy.jpg", visualization=True, save_path='style_tranfer')
......@@ -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)
......@@ -19,17 +19,19 @@ from model import BoWModel
import ast
import argparse
parser = argparse.ArgumentParser(__doc__)
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("--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("--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()
if __name__ == '__main__':
# Data to be prdicted
data = [
......@@ -44,12 +46,9 @@ if __name__ == '__main__':
embedder = hub.Module(name=args.hub_embedding_name)
tokenizer = embedder.get_tokenizer()
model = BoWModel(
embedder=embedder,
tokenizer=tokenizer,
load_checkpoint=args.checkpoint,
label_map=label_map)
model = BoWModel(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):
print('Data: {} \t Lable: {}'.format(text[0], results[idx]))
......@@ -21,7 +21,6 @@ from model import BoWModel
import ast
import argparse
parser = argparse.ArgumentParser(__doc__)
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.")
......@@ -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("--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("--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()
if __name__ == '__main__':
embedder = hub.Module(name=args.hub_embedding_name)
tokenizer = embedder.get_tokenizer()
......@@ -44,8 +46,7 @@ if __name__ == '__main__':
test_dataset = ChnSentiCorp(tokenizer=tokenizer, max_seq_len=args.max_seq_len, mode='test')
model = BoWModel(embedder=embedder)
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.train(
train_dataset,
......
......@@ -21,7 +21,11 @@ import argparse
parser = argparse.ArgumentParser(__doc__)
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("--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.")
......@@ -33,15 +37,11 @@ args = parser.parse_args()
if __name__ == '__main__':
model = hub.Module(name='ernie_tiny', version='2.0.1', task='seq-cls')
train_dataset = ChnSentiCorp(
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')
test_dataset = ChnSentiCorp(
tokenizer=model.get_tokenizer(), max_seq_len=args.max_seq_len, mode='test')
train_dataset = ChnSentiCorp(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')
test_dataset = ChnSentiCorp(tokenizer=model.get_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.train(
train_dataset,
......
......@@ -21,7 +21,11 @@ import argparse
parser = argparse.ArgumentParser(__doc__)
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("--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.")
......@@ -38,8 +42,7 @@ if __name__ == '__main__':
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')
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.train(
train_dataset,
......
......@@ -93,6 +93,3 @@ $ hub serving start -m chinese_ocr_db_crnn_mobile
</p>
* 如需在线快速体验,请点击[PaddleHub教程合集](https://aistudio.baidu.com/aistudio/projectdetail/231146),可使用AI Studio平台提供的GPU算力进行快速尝试。
......@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn10",
@moduleinfo(
name="panns_cnn10",
version="1.0.0",
summary="",
author="Baidu",
......@@ -52,15 +53,15 @@ class PANN(nn.Layer):
self.num_class = num_class
if task == 'sound-cls':
self.cnn10 = CNN10(extract_embedding=True,
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams'))
self.cnn10 = CNN10(
extract_embedding=True, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams'))
self.dropout = nn.Dropout(0.1)
self.fc = nn.Linear(self.cnn10.emb_size, num_class)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
else:
self.cnn10 = CNN10(extract_embedding=False,
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams'))
self.cnn10 = CNN10(
extract_embedding=False, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn10', 'cnn10.pdparams'))
self.task = task
if load_checkpoint is not None and os.path.isfile(load_checkpoint):
......
......@@ -25,13 +25,15 @@ class ConvBlock(nn.Layer):
def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2D(in_channels=in_channels,
self.conv1 = nn.Conv2D(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
padding=(1, 1),
bias_attr=False)
self.conv2 = nn.Conv2D(in_channels=out_channels,
self.conv2 = nn.Conv2D(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
......
......@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn14",
@moduleinfo(
name="panns_cnn14",
version="1.0.0",
summary="",
author="Baidu",
......@@ -52,15 +53,15 @@ class PANN(nn.Layer):
self.num_class = num_class
if task == 'sound-cls':
self.cnn14 = CNN14(extract_embedding=True,
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams'))
self.cnn14 = CNN14(
extract_embedding=True, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams'))
self.dropout = nn.Dropout(0.1)
self.fc = nn.Linear(self.cnn14.emb_size, num_class)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
else:
self.cnn14 = CNN14(extract_embedding=False,
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams'))
self.cnn14 = CNN14(
extract_embedding=False, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn14', 'cnn14.pdparams'))
self.task = task
if load_checkpoint is not None and os.path.isfile(load_checkpoint):
......
......@@ -25,13 +25,15 @@ class ConvBlock(nn.Layer):
def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2D(in_channels=in_channels,
self.conv1 = nn.Conv2D(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
padding=(1, 1),
bias_attr=False)
self.conv2 = nn.Conv2D(in_channels=out_channels,
self.conv2 = nn.Conv2D(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
......
......@@ -27,7 +27,8 @@ from paddlehub.module.module import moduleinfo
from paddlehub.utils.log import logger
@moduleinfo(name="panns_cnn6",
@moduleinfo(
name="panns_cnn6",
version="1.0.0",
summary="",
author="Baidu",
......@@ -52,15 +53,15 @@ class PANN(nn.Layer):
self.num_class = num_class
if task == 'sound-cls':
self.cnn6 = CNN6(extract_embedding=True,
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams'))
self.cnn6 = CNN6(
extract_embedding=True, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams'))
self.dropout = nn.Dropout(0.1)
self.fc = nn.Linear(self.cnn6.emb_size, num_class)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
else:
self.cnn6 = CNN6(extract_embedding=False,
checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams'))
self.cnn6 = CNN6(
extract_embedding=False, checkpoint=os.path.join(MODULE_HOME, 'panns_cnn6', 'cnn6.pdparams'))
self.task = task
if load_checkpoint is not None and os.path.isfile(load_checkpoint):
......
......@@ -25,7 +25,8 @@ class ConvBlock5x5(nn.Layer):
def __init__(self, in_channels, out_channels):
super(ConvBlock5x5, self).__init__()
self.conv1 = nn.Conv2D(in_channels=in_channels,
self.conv1 = nn.Conv2D(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=(5, 5),
stride=(1, 1),
......
......@@ -18,6 +18,7 @@ class ColorizeHint:
hint(np.ndarray): hint images
mask(np.ndarray): mask images
"""
def __init__(self, percent: float, num_points: int = None, samp: str = 'normal', use_avg: bool = True):
self.percent = percent
self.num_points = num_points
......@@ -52,11 +53,9 @@ class ColorizeHint:
# add color point
if self.use_avg:
# embed()
hint[nn, :, h:h + P, w:w + P] = np.mean(np.mean(data[nn, :, h:h + P, w:w + P],
axis=2,
keepdims=True),
axis=1,
keepdims=True).reshape(1, C, 1, 1)
hint[nn, :, h:h + P, w:w + P] = np.mean(
np.mean(data[nn, :, h:h + P, w:w + P], axis=2, keepdims=True), axis=1, keepdims=True).reshape(
1, C, 1, 1)
else:
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
......@@ -82,6 +81,7 @@ class ColorizePreprocess:
data(dict):The preprocessed data for colorization.
"""
def __init__(self,
ab_thresh: float = 0.,
p: float = 0.,
......@@ -113,8 +113,8 @@ class ColorizePreprocess:
data['B'] = data_lab[:, 1:, :, :]
if self.ab_thresh > 0: # mask out grayscale images
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)),
axis=1)
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)), axis=1)
mask = (mask >= thresh)
data['A'] = data['A'][mask, :, :, :]
data['B'] = data['B'][mask, :, :, :]
......
......@@ -40,6 +40,7 @@ class UserGuidedColorization(nn.Layer):
load_checkpoint (str): Pretrained checkpoint path.
"""
def __init__(self, use_tanh: bool = True, load_checkpoint: str = None):
super(UserGuidedColorization, self).__init__()
self.input_nc = 4
......
......@@ -14,6 +14,7 @@ from paddlehub.module.cv_module import StyleTransferModule
class GramMatrix(nn.Layer):
"""Calculate gram matrix"""
def forward(self, y):
(b, ch, h, w) = y.shape
features = y.reshape((b, ch, w * h))
......@@ -24,6 +25,7 @@ class GramMatrix(nn.Layer):
class ConvLayer(nn.Layer):
"""Basic conv layer with reflection padding layer"""
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int):
super(ConvLayer, self).__init__()
pad = int(np.floor(kernel_size / 2))
......@@ -51,6 +53,7 @@ class UpsampleConvLayer(nn.Layer):
Return:
img(paddle.Tensor): UpsampleConvLayer output.
"""
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int, upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
......@@ -85,6 +88,7 @@ class Bottleneck(nn.Layer):
Return:
img(paddle.Tensor): Bottleneck output.
"""
def __init__(self,
inplanes: int,
planes: int,
......@@ -98,8 +102,8 @@ class Bottleneck(nn.Layer):
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),
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,
stride=1))
norm_layer(planes), nn.ReLU(), nn.Conv2D(
planes, planes * self.expansion, kernel_size=1, stride=1))
self.conv_block = nn.Sequential(*conv_block)
def forward(self, x: paddle.Tensor):
......@@ -125,14 +129,12 @@ class UpBottleneck(nn.Layer):
Return:
img(paddle.Tensor): UpBottleneck output.
"""
def __init__(self, inplanes: int, planes: int, stride: int = 2, norm_layer: nn.Layer = nn.BatchNorm2D):
super(UpBottleneck, self).__init__()
self.expansion = 4
self.residual_layer = UpsampleConvLayer(inplanes,
planes * self.expansion,
kernel_size=1,
stride=1,
upsample=stride)
self.residual_layer = UpsampleConvLayer(
inplanes, planes * self.expansion, kernel_size=1, stride=1, upsample=stride)
conv_block = []
conv_block += [norm_layer(inplanes), nn.ReLU(), nn.Conv2D(inplanes, planes, kernel_size=1, stride=1)]
conv_block += [
......@@ -163,6 +165,7 @@ class Inspiration(nn.Layer):
Return:
img(paddle.Tensor): UpBottleneck output.
"""
def __init__(self, C: int, B: int = 1):
super(Inspiration, self).__init__()
......@@ -179,8 +182,8 @@ class Inspiration(nn.Layer):
self.P = paddle.bmm(self.weight.expand_as(self.G), self.G)
x = paddle.bmm(
self.P.transpose((0, 2, 1)).expand((X.shape[0], self.C, self.C)), X.reshape(
(X.shape[0], X.shape[1], -1))).reshape(X.shape)
self.P.transpose((0, 2, 1)).expand((X.shape[0], self.C, self.C)), X.reshape((X.shape[0], X.shape[1],
-1))).reshape(X.shape)
return x
def __repr__(self):
......@@ -190,6 +193,7 @@ class Inspiration(nn.Layer):
class Vgg16(nn.Layer):
""" First four layers from Vgg16."""
def __init__(self):
super(Vgg16, self).__init__()
self.conv1_1 = nn.Conv2D(3, 64, kernel_size=3, stride=1, padding=1)
......@@ -264,12 +268,8 @@ class MSGNet(nn.Layer):
Return:
img(paddle.Tensor): MSGNet output.
"""
def __init__(self,
input_nc=3,
output_nc=3,
ngf=128,
n_blocks=6,
norm_layer=nn.InstanceNorm2D,
def __init__(self, input_nc=3, output_nc=3, ngf=128, n_blocks=6, norm_layer=nn.InstanceNorm2D,
load_checkpoint=None):
super(MSGNet, self).__init__()
self.gram = GramMatrix()
......
......@@ -194,7 +194,7 @@ class ResNet50_vd(nn.Layer):
label_file = os.path.join(self.directory, 'label_list.txt')
files = open(label_file)
for line in files.readlines():
line=line.strip('\n')
line = line.strip('\n')
label_list.append(line)
self.labels = label_list
class_dim = len(self.labels)
......@@ -248,7 +248,12 @@ class ResNet50_vd(nn.Layer):
print("load pretrained checkpoint success")
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)
def forward(self, inputs: paddle.Tensor):
......
......@@ -111,5 +111,3 @@ paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
paddlex >= 1.3.0
......@@ -12,7 +12,6 @@ import paddle.nn as nn
from paddlex.seg import transforms as T
from paddlehub.module.module import moduleinfo, runnable, serving
METER_SHAPE = 512
CIRCLE_CENTER = [256, 256]
CIRCLE_RADIUS = 250
......@@ -44,11 +43,13 @@ def cv2_to_base64(image: np.ndarray):
return base64.b64encode(data.tostring()).decode('utf8')
@moduleinfo(name="barometer_reader",
@moduleinfo(
name="barometer_reader",
type="CV/image_editing",
author="paddlepaddle",
author_email="",
summary="meter_reader implements the detection and automatic reading of traditional mechanical pointer meters based on Meter detection and pointer segmentation.",
summary=
"meter_reader implements the detection and automatic reading of traditional mechanical pointer meters based on Meter detection and pointer segmentation.",
version="1.0.0")
class BarometerReader(nn.Layer):
def __init__(self):
......@@ -121,8 +122,7 @@ class BarometerReader(nn.Layer):
if pointer_flag:
if pointer_data[i] == 0 and pointer_data[i + 1] == 0:
one_pointer_end = i - 1
pointer_location = (
one_pointer_start + one_pointer_end) / 2
pointer_location = (one_pointer_start + one_pointer_end) / 2
one_pointer_start = 0
one_pointer_end = 0
pointer_flag = False
......@@ -135,8 +135,7 @@ class BarometerReader(nn.Layer):
if scale_location[i] <= pointer_location and pointer_location < scale_location[i + 1]:
scales = i + (pointer_location - scale_location[i]) / (
scale_location[i + 1] - scale_location[i] + 1e-05) + 1
ratio = (pointer_location - scale_location[0]) / (
scale_location[scale_num - 1] - scale_location[0] + 1e-05)
ratio = (pointer_location - scale_location[0]) / (scale_location[scale_num - 1] - scale_location[0] + 1e-05)
result = {'scale_num': scale_num, 'scales': scales, 'ratio': ratio}
return result
......@@ -147,7 +146,7 @@ class BarometerReader(nn.Layer):
erode_kernel: int = 4,
use_erode: bool = True,
visualization: bool = False,
save_dir: str ='output'):
save_dir: str = 'output'):
if isinstance(im_file, str):
im = cv2.imread(im_file).astype('float32')
......@@ -173,13 +172,7 @@ class BarometerReader(nn.Layer):
meter_shape = sub_image.shape
scale_x = float(METER_SHAPE) / float(meter_shape[1])
scale_y = float(METER_SHAPE) / float(meter_shape[0])
meter_meter = cv2.resize(
sub_image,
None,
None,
fx=scale_x,
fy=scale_y,
interpolation=cv2.INTER_LINEAR)
meter_meter = cv2.resize(sub_image, None, None, fx=scale_x, fy=scale_y, interpolation=cv2.INTER_LINEAR)
meter_meter = meter_meter.astype('float32')
resized_meters.append(meter_meter)
......@@ -191,9 +184,7 @@ class BarometerReader(nn.Layer):
for j in range(i, im_size):
meter_images.append(resized_meters[j - i])
result = self.segmenter.batch_predict(
transforms=self.seg_transform,
img_file_list=meter_images)
result = self.segmenter.batch_predict(transforms=self.seg_transform, img_file_list=meter_images)
if use_erode:
kernel = np.ones((erode_kernel, erode_kernel), np.uint8)
......@@ -250,21 +241,16 @@ class BarometerReader(nn.Layer):
prog='hub run {}'.format(self.name),
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(
title="Config options",
description=
"Run configuration for controlling module behavior, not required.")
title="Config options", description="Run configuration for controlling module behavior, not required.")
self.add_module_input_arg()
args = self.parser.parse_args(argvs)
results = self.predict(im_file=args.input_path)
return results
def add_module_input_arg(self):
"""
Add the command input options.
"""
self.arg_input_group.add_argument(
'--input_path', type=str, help="path to image.")
self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
......@@ -116,4 +116,3 @@ https://github.com/PaddlePaddle/PaddleDetection
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
......@@ -42,38 +42,28 @@ def visualize_box_mask(im, results, labels=None, mask_resolution=14, threshold=0
im (PIL.Image.Image): visualized image
"""
if not labels:
labels = ['background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire', 'hydrant', 'stop sign', 'parking meter',
'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle',
'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush']
labels = [
'background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat',
'traffic light', 'fire', 'hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush'
]
if isinstance(im, str):
im = Image.open(im).convert('RGB')
else:
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
im = Image.fromarray(im)
if 'masks' in results and 'boxes' in results:
im = draw_mask(
im,
results['boxes'],
results['masks'],
labels,
resolution=mask_resolution)
im = draw_mask(im, results['boxes'], results['masks'], labels, resolution=mask_resolution)
if 'boxes' in results:
im = draw_box(im, results['boxes'], labels)
if 'segm' in results:
im = draw_segm(
im,
results['segm'],
results['label'],
results['score'],
labels,
threshold=threshold)
im = draw_segm(im, results['segm'], results['label'], results['score'], labels, threshold=threshold)
return im
......@@ -200,28 +190,19 @@ def draw_box(im, np_boxes, labels):
color = tuple(clsid2color[clsid])
# draw bbox
draw.line(
[(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
(xmin, ymin)],
draw.line([(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)],
width=draw_thickness,
fill=color)
# draw label
text = "{} {:.4f}".format(labels[clsid], score)
tw, th = draw.textsize(text)
draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
draw.rectangle([(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
return im
def draw_segm(im,
np_segms,
np_label,
np_score,
labels,
threshold=0.5,
alpha=0.7):
def draw_segm(im, np_segms, np_label, np_score, labels, threshold=0.5, alpha=0.7):
"""
Draw segmentation on image.
"""
......@@ -250,28 +231,17 @@ def draw_segm(im,
sum_y = np.sum(mask, axis=1)
y = np.where(sum_y > 0.5)[0]
x0, x1, y0, y1 = x[0], x[-1], y[0], y[-1]
cv2.rectangle(im, (x0, y0), (x1, y1),
tuple(color_mask.astype('int32').tolist()), 1)
cv2.rectangle(im, (x0, y0), (x1, y1), tuple(color_mask.astype('int32').tolist()), 1)
bbox_text = '%s %.2f' % (labels[clsid], score)
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),
tuple(color_mask.astype('int32').tolist()), -1)
cv2.putText(
im,
bbox_text, (x0, y0 - 2),
cv2.FONT_HERSHEY_SIMPLEX,
0.3, (0, 0, 0),
1,
lineType=cv2.LINE_AA)
cv2.rectangle(im, (x0, y0), (x0 + t_size[0], y0 - t_size[1] - 3), tuple(color_mask.astype('int32').tolist()),
-1)
cv2.putText(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'))
def load_predictor(model_dir,
run_mode='fluid',
batch_size=1,
use_gpu=False,
min_subgraph_size=3):
def load_predictor(model_dir, run_mode='fluid', batch_size=1, use_gpu=False, min_subgraph_size=3):
"""set AnalysisConfig, generate AnalysisPredictor
Args:
model_dir (str): root path of __model__ and __params__
......@@ -282,20 +252,16 @@ def load_predictor(model_dir,
ValueError: predict by TensorRT need use_gpu == True.
"""
if not use_gpu and not run_mode == 'fluid':
raise ValueError(
"Predict by TensorRT mode: {}, expect use_gpu==True, but use_gpu == {}"
.format(run_mode, use_gpu))
raise ValueError("Predict by TensorRT mode: {}, expect use_gpu==True, but use_gpu == {}".format(
run_mode, use_gpu))
if run_mode == 'trt_int8':
raise ValueError("TensorRT int8 mode is not supported now, "
"please use trt_fp32 or trt_fp16 instead.")
raise ValueError("TensorRT int8 mode is not supported now, " "please use trt_fp32 or trt_fp16 instead.")
precision_map = {
'trt_int8': fluid.core.AnalysisConfig.Precision.Int8,
'trt_fp32': fluid.core.AnalysisConfig.Precision.Float32,
'trt_fp16': fluid.core.AnalysisConfig.Precision.Half
}
config = fluid.core.AnalysisConfig(
os.path.join(model_dir, '__model__'),
os.path.join(model_dir, '__params__'))
config = fluid.core.AnalysisConfig(os.path.join(model_dir, '__model__'), os.path.join(model_dir, '__params__'))
if use_gpu:
# initial GPU memory(M), device ID
config.enable_use_gpu(100, 0)
......
......@@ -33,20 +33,16 @@ class Detector(object):
threshold (float): threshold to reserve the result for output.
"""
def __init__(self,
min_subgraph_size: int = 60,
use_gpu=False,
threshold: float = 0.5):
def __init__(self, min_subgraph_size: int = 60, use_gpu=False, threshold: float = 0.5):
model_dir = os.path.join(self.directory, 'solov2_r50_fpn_1x')
self.predictor = D.load_predictor(
model_dir,
min_subgraph_size=min_subgraph_size,
use_gpu=use_gpu)
self.compose = [P.Resize(max_size=1333),
self.predictor = D.load_predictor(model_dir, min_subgraph_size=min_subgraph_size, use_gpu=use_gpu)
self.compose = [
P.Resize(max_size=1333),
P.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
P.Permute(),
P.PadStride(stride=32)]
P.PadStride(stride=32)
]
def transform(self, im: Union[str, np.ndarray]):
im, im_info = P.preprocess(im, self.compose)
......@@ -61,17 +57,14 @@ class Detector(object):
for box in np_boxes:
print('class_id:{:d}, confidence:{:.4f},'
'left_top:[{:.2f},{:.2f}],'
' right_bottom:[{:.2f},{:.2f}]'.format(
int(box[0]), box[1], box[2], box[3], box[4], box[5]))
' right_bottom:[{:.2f},{:.2f}]'.format(int(box[0]), box[1], box[2], box[3], box[4], box[5]))
results['boxes'] = np_boxes
if np_masks is not None:
np_masks = np_masks[expect_boxes, :, :, :]
results['masks'] = np_masks
return results
def predict(self,
image: Union[str, np.ndarray],
threshold: float = 0.5):
def predict(self, image: Union[str, np.ndarray], threshold: float = 0.5):
'''
Args:
image (str/np.ndarray): path of image/ np.ndarray read by cv2
......@@ -117,12 +110,9 @@ class DetectorSOLOv2(Detector):
use_gpu (bool): whether use gpu
threshold (float): threshold to reserve the result for output.
"""
def __init__(self,
use_gpu: bool = False,
threshold: float = 0.5):
super(DetectorSOLOv2, self).__init__(
use_gpu=use_gpu,
threshold=threshold)
def __init__(self, use_gpu: bool = False, threshold: float = 0.5):
super(DetectorSOLOv2, self).__init__(use_gpu=use_gpu, threshold=threshold)
def predict(self,
image: Union[str, np.ndarray],
......@@ -148,12 +138,9 @@ class DetectorSOLOv2(Detector):
self.predictor.zero_copy_run()
output_names = self.predictor.get_output_names()
np_label = self.predictor.get_output_tensor(output_names[
0]).copy_to_cpu()
np_score = self.predictor.get_output_tensor(output_names[
1]).copy_to_cpu()
np_segms = self.predictor.get_output_tensor(output_names[
2]).copy_to_cpu()
np_label = self.predictor.get_output_tensor(output_names[0]).copy_to_cpu()
np_score = self.predictor.get_output_tensor(output_names[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)
if visualization:
......
......@@ -78,19 +78,12 @@ class Resize(object):
im_channel = im.shape[2]
im_scale_x, im_scale_y = self.generate_scale(im)
if self.use_cv2:
im = cv2.resize(
im,
None,
None,
fx=im_scale_x,
fy=im_scale_y,
interpolation=self.interp)
im = cv2.resize(im, None, None, fx=im_scale_x, fy=im_scale_y, interpolation=self.interp)
else:
resize_w = int(im_scale_x * float(im.shape[1]))
resize_h = int(im_scale_y * float(im.shape[0]))
if self.max_size != 0:
raise TypeError(
'If you set max_size to cap the maximum size of image,'
raise TypeError('If you set max_size to cap the maximum size of image,'
'please set use_cv2 to True to resize the image.')
im = im.astype('uint8')
im = Image.fromarray(im)
......@@ -99,8 +92,7 @@ class Resize(object):
# padding im when image_shape fixed by infer_cfg.yml
if self.max_size != 0 and self.image_shape is not None:
padding_im = np.zeros(
(self.max_size, self.max_size, im_channel), dtype=np.float32)
padding_im = np.zeros((self.max_size, self.max_size, im_channel), dtype=np.float32)
im_h, im_w = im.shape[:2]
padding_im[:im_h, :im_w, :] = im
im = padding_im
......
......@@ -207,10 +207,7 @@ class BodyPoseModel(nn.Layer):
save_path = os.path.join(save_path, img_name)
cv2.imwrite(save_path, canvas)
results = {
'candidate': candidate,
'subset': subset,
'data': canvas}
results = {'candidate': candidate, 'subset': subset, 'data': canvas}
return results
......@@ -221,7 +218,7 @@ class BodyPoseModel(nn.Layer):
"""
images_decode = [P.base64_to_cv2(image) for image in images]
results = self.predict(img=images_decode[0], **kwargs)
final={}
final = {}
final['candidate'] = P.cv2_to_base64(results['candidate'])
final['subset'] = P.cv2_to_base64(results['subset'])
final['data'] = P.cv2_to_base64(results['data'])
......@@ -238,19 +235,13 @@ class BodyPoseModel(nn.Layer):
prog='hub run {}'.format(self.name),
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(
title="Config options",
description=
"Run configuration for controlling module behavior, not required.")
title="Config options", description="Run configuration for controlling module behavior, not required.")
self.add_module_config_arg()
self.add_module_input_arg()
args = self.parser.parse_args(argvs)
results = self.predict(
img=args.input_path,
save_path=args.output_dir,
visualization=args.visualization)
results = self.predict(img=args.input_path, save_path=args.output_dir, visualization=args.visualization)
return results
......@@ -260,19 +251,12 @@ class BodyPoseModel(nn.Layer):
"""
self.arg_config_group.add_argument(
'--output_dir',
type=str,
default='openpose_body',
help="The directory to save output images.")
'--output_dir', type=str, default='openpose_body', help="The directory to save output images.")
self.arg_config_group.add_argument(
'--visualization',
type=bool,
default=True,
help="whether to save output as images.")
'--visualization', type=bool, default=True, help="whether to save output as images.")
def add_module_input_arg(self):
"""
Add the command input options.
"""
self.arg_input_group.add_argument(
'--input_path', type=str, help="path to image.")
\ No newline at end of file
self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
......@@ -170,7 +170,11 @@ class HandPoseModel(nn.Layer):
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.visualization = visualization
if isinstance(img, str):
......@@ -193,7 +197,11 @@ class HandPoseModel(nn.Layer):
peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y)
all_hand_peaks.append(peaks)
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)
if self.visualization:
if not os.path.exists(save_path):
......@@ -202,9 +210,7 @@ class HandPoseModel(nn.Layer):
save_path = os.path.join(save_path, img_name)
cv2.imwrite(save_path, canvas)
results = {
'all_hand_peaks': all_hand_peaks,
'data': canvas}
results = {'all_hand_peaks': all_hand_peaks, 'data': canvas}
return results
......@@ -215,8 +221,8 @@ class HandPoseModel(nn.Layer):
"""
images_decode = [P.base64_to_cv2(image) for image in images]
results = self.predict(img=images_decode[0], **kwargs)
final={}
final['all_hand_peaks']=[peak.tolist() for peak in results['all_hand_peaks']]
final = {}
final['all_hand_peaks'] = [peak.tolist() for peak in results['all_hand_peaks']]
final['data'] = P.cv2_to_base64(results['data'])
return final
......@@ -230,20 +236,14 @@ class HandPoseModel(nn.Layer):
prog='hub run {}'.format(self.name),
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(
title="Config options",
description=
"Run configuration for controlling module behavior, not required.")
title="Config options", description="Run configuration for controlling module behavior, not required.")
self.add_module_config_arg()
self.add_module_input_arg()
args = self.parser.parse_args(argvs)
results = self.predict(
img=args.input_path,
save_path=args.output_dir,
scale=args.scale,
visualization=args.visualization)
img=args.input_path, save_path=args.output_dir, scale=args.scale, visualization=args.visualization)
return results
......@@ -253,24 +253,14 @@ class HandPoseModel(nn.Layer):
"""
self.arg_config_group.add_argument(
'--output_dir',
type=str,
default='openpose_hand',
help="The directory to save output images.")
'--output_dir', type=str, default='openpose_hand', help="The directory to save output images.")
self.arg_config_group.add_argument(
'--scale',
type=list,
default=[0.5, 1.0, 1.5, 2.0],
help="The search scale for openpose hands model.")
'--scale', 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(
'--visualization',
type=bool,
default=True,
help="whether to save output as images.")
'--visualization', type=bool, default=True, help="whether to save output as images.")
def add_module_input_arg(self):
"""
Add the command input options.
"""
self.arg_input_group.add_argument(
'--input_path', type=str, help="path to image.")
\ No newline at end of file
self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
......@@ -232,6 +232,7 @@ class ResizeScaling:
resize_img = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=self.interpolation)
return resize_img
def npmax(array: np.ndarray):
"""Get max value and index."""
arrayindex = array.argmax(1)
......
......@@ -6,6 +6,7 @@ import numpy as np
from paddle.nn import Layer
from paddlehub.module.module import moduleinfo
@moduleinfo(
name="ExtremeC3_Portrait_Segmentation", # 模型名称
type="CV/semantic_segmentation", # 模型类型
......@@ -26,7 +27,7 @@ class ExtremeC3_Portrait_Segmentation(Layer):
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]
# 读取数据函数
......@@ -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_%d.png' % i), result)
results.append({
'mask': mask,
'result': result
})
results.append({'mask': mask, 'result': result})
return results
# 关键点检测函数
def Segmentation(self,
images=None,
paths=None,
batch_size=1,
output_dir='output',
visualization=False):
def Segmentation(self, images=None, paths=None, batch_size=1, output_dir='output', visualization=False):
# 加载数据处理器
datas = self.load_datas(paths, images)
......
......@@ -6,6 +6,7 @@ import numpy as np
from paddle.nn import Layer
from paddlehub.module.module import moduleinfo
@moduleinfo(
name="SINet_Portrait_Segmentation", # 模型名称
type="CV/semantic_segmentation", # 模型类型
......@@ -26,7 +27,7 @@ class SINet_Portrait_Segmentation(Layer):
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]
# 读取数据函数
......@@ -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_%d.png' % i), result)
results.append({
'mask': mask,
'result': result
})
results.append({'mask': mask, 'result': result})
return results
# 关键点检测函数
def Segmentation(self,
images=None,
paths=None,
batch_size=1,
output_dir='output',
visualization=False):
def Segmentation(self, images=None, paths=None, batch_size=1, output_dir='output', visualization=False):
# 加载数据处理器
datas = self.load_datas(paths, images)
......
......@@ -30,8 +30,7 @@ def SyncBatchNorm(*args, **kwargs):
class ConvBNLayer(nn.Layer):
"""Basic conv bn relu layer."""
def __init__(
self,
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
......@@ -44,8 +43,7 @@ class ConvBNLayer(nn.Layer):
super(ConvBNLayer, self).__init__()
self.is_vd_mode = is_vd_mode
self._pool2d_avg = AvgPool2D(
kernel_size=2, stride=2, padding=0, ceil_mode=True)
self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True)
self._conv = Conv2D(
in_channels=in_channels,
out_channels=out_channels,
......@@ -83,11 +81,7 @@ class BottleneckBlock(nn.Layer):
super(BottleneckBlock, self).__init__()
self.conv0 = ConvBNLayer(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
act='relu',
name=name + "_branch2a")
in_channels=in_channels, out_channels=out_channels, kernel_size=1, act='relu', name=name + "_branch2a")
self.dilation = dilation
......@@ -100,11 +94,7 @@ class BottleneckBlock(nn.Layer):
dilation=dilation,
name=name + "_branch2b")
self.conv2 = ConvBNLayer(
in_channels=out_channels,
out_channels=out_channels * 4,
kernel_size=1,
act=None,
name=name + "_branch2c")
in_channels=out_channels, out_channels=out_channels * 4, kernel_size=1, act=None, name=name + "_branch2c")
if not shortcut:
self.short = ConvBNLayer(
......@@ -139,12 +129,7 @@ class BottleneckBlock(nn.Layer):
class SeparableConvBNReLU(nn.Layer):
"""Depthwise Separable Convolution."""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
super(SeparableConvBNReLU, self).__init__()
self.depthwise_conv = ConvBN(
in_channels,
......@@ -153,8 +138,7 @@ class SeparableConvBNReLU(nn.Layer):
padding=padding,
groups=in_channels,
**kwargs)
self.piontwise_conv = ConvBNReLU(
in_channels, out_channels, kernel_size=1, groups=1)
self.piontwise_conv = ConvBNReLU(in_channels, out_channels, kernel_size=1, groups=1)
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
x = self.depthwise_conv(x)
......@@ -165,15 +149,9 @@ class SeparableConvBNReLU(nn.Layer):
class ConvBN(nn.Layer):
"""Basic conv bn layer"""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
super(ConvBN, self).__init__()
self._conv = Conv2D(
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
......@@ -185,16 +163,10 @@ class ConvBN(nn.Layer):
class ConvBNReLU(nn.Layer):
"""Basic conv bn relu layer."""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
super(ConvBNReLU, self).__init__()
self._conv = Conv2D(
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
......@@ -251,8 +223,7 @@ class Activation(nn.Layer):
act_name = act_dict[act]
self.act_func = eval("activation.{}()".format(act_name))
else:
raise KeyError("{} does not exist in the current {}".format(
act, act_dict.keys()))
raise KeyError("{} does not exist in the current {}".format(act, act_dict.keys()))
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
......@@ -281,7 +252,7 @@ class ASPPModule(nn.Layer):
in_channels: int,
out_channels: int,
align_corners: bool,
use_sep_conv: bool= False,
use_sep_conv: bool = False,
image_pooling: bool = False):
super().__init__()
......@@ -311,10 +282,7 @@ class ASPPModule(nn.Layer):
out_size += 1
self.image_pooling = image_pooling
self.conv_bn_relu = ConvBNReLU(
in_channels=out_channels * out_size,
out_channels=out_channels,
kernel_size=1)
self.conv_bn_relu = ConvBNReLU(in_channels=out_channels * out_size, out_channels=out_channels, kernel_size=1)
self.dropout = nn.Dropout(p=0.1) # drop rate
......@@ -322,20 +290,12 @@ class ASPPModule(nn.Layer):
outputs = []
for block in self.aspp_blocks:
y = block(x)
y = F.interpolate(
y,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
y = F.interpolate(y, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
outputs.append(y)
if self.image_pooling:
img_avg = self.global_avg_pool(x)
img_avg = F.interpolate(
img_avg,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
img_avg = F.interpolate(img_avg, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
outputs.append(img_avg)
x = paddle.concat(outputs, axis=1)
......
......@@ -27,7 +27,6 @@ from deeplabv3p_resnet50_voc.resnet import ResNet50_vd
import deeplabv3p_resnet50_voc.layers as L
@moduleinfo(
name="deeplabv3p_resnet50_voc",
type="CV/semantic_segmentation",
......@@ -72,9 +71,8 @@ class DeepLabV3PResnet50(nn.Layer):
super(DeepLabV3PResnet50, self).__init__()
self.backbone = ResNet50_vd()
backbone_channels = [self.backbone.feat_channels[i] for i in backbone_indices]
self.head = DeepLabV3PHead(num_classes, backbone_indices,
backbone_channels, aspp_ratios,
aspp_out_channels, align_corners)
self.head = DeepLabV3PHead(num_classes, backbone_indices, backbone_channels, aspp_ratios, aspp_out_channels,
align_corners)
self.align_corners = align_corners
self.transforms = T.Compose([T.Normalize()])
......@@ -96,11 +94,8 @@ class DeepLabV3PResnet50(nn.Layer):
feat_list = self.backbone(x)
logit_list = self.head(feat_list)
return [
F.interpolate(
logit,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners) for logit in logit_list]
F.interpolate(logit, x.shape[2:], mode='bilinear', align_corners=self.align_corners) for logit in logit_list
]
class DeepLabV3PHead(nn.Layer):
......@@ -123,17 +118,13 @@ class DeepLabV3PHead(nn.Layer):
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],
aspp_ratios: Tuple[float], aspp_out_channels: int, align_corners: bool):
def __init__(self, num_classes: int, backbone_indices: Tuple[paddle.Tensor],
backbone_channels: Tuple[paddle.Tensor], aspp_ratios: Tuple[float], aspp_out_channels: int,
align_corners: bool):
super().__init__()
self.aspp = L.ASPPModule(
aspp_ratios,
backbone_channels[1],
aspp_out_channels,
align_corners,
use_sep_conv=True,
image_pooling=True)
aspp_ratios, 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.backbone_indices = backbone_indices
......@@ -160,25 +151,17 @@ class Decoder(nn.Layer):
def __init__(self, num_classes: int, in_channels: int, align_corners: bool):
super(Decoder, self).__init__()
self.conv_bn_relu1 = L.ConvBNReLU(
in_channels=in_channels, out_channels=48, kernel_size=1)
self.conv_bn_relu1 = L.ConvBNReLU(in_channels=in_channels, out_channels=48, kernel_size=1)
self.conv_bn_relu2 = L.SeparableConvBNReLU(
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 = nn.Conv2D(
in_channels=256, out_channels=num_classes, kernel_size=1)
self.conv_bn_relu2 = L.SeparableConvBNReLU(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 = nn.Conv2D(in_channels=256, out_channels=num_classes, kernel_size=1)
self.align_corners = align_corners
def forward(self, x: paddle.Tensor, low_level_feat: paddle.Tensor) -> paddle.Tensor:
low_level_feat = self.conv_bn_relu1(low_level_feat)
x = F.interpolate(
x,
low_level_feat.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
x = F.interpolate(x, low_level_feat.shape[2:], mode='bilinear', align_corners=self.align_corners)
x = paddle.concat([x, low_level_feat], axis=1)
x = self.conv_bn_relu2(x)
x = self.conv_bn_relu3(x)
......
......@@ -36,11 +36,7 @@ class BasicBlock(nn.Layer):
act='relu',
name=name + "_branch2a")
self.conv1 = L.ConvBNLayer(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3,
act=None,
name=name + "_branch2b")
in_channels=out_channels, out_channels=out_channels, kernel_size=3, act=None, name=name + "_branch2b")
if not shortcut:
self.short = L.ConvBNLayer(
......@@ -67,8 +63,7 @@ class BasicBlock(nn.Layer):
class ResNet50_vd(nn.Layer):
def __init__(self,
multi_grid: tuple = (1, 2, 4)):
def __init__(self, multi_grid: tuple = (1, 2, 4)):
super(ResNet50_vd, self).__init__()
depth = [3, 4, 6, 3]
num_channels = [64, 256, 512, 1024]
......@@ -76,26 +71,11 @@ class ResNet50_vd(nn.Layer):
self.feat_channels = [c * 4 for c in num_filters]
dilation_dict = {2: 2, 3: 4}
self.conv1_1 = L.ConvBNLayer(
in_channels=3,
out_channels=32,
kernel_size=3,
stride=2,
act='relu',
name="conv1_1")
in_channels=3, out_channels=32, kernel_size=3, stride=2, act='relu', name="conv1_1")
self.conv1_2 = L.ConvBNLayer(
in_channels=32,
out_channels=32,
kernel_size=3,
stride=1,
act='relu',
name="conv1_2")
in_channels=32, out_channels=32, kernel_size=3, stride=1, act='relu', name="conv1_2")
self.conv1_3 = L.ConvBNLayer(
in_channels=32,
out_channels=64,
kernel_size=3,
stride=1,
act='relu',
name="conv1_3")
in_channels=32, 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.stage_list = []
......@@ -104,18 +84,15 @@ class ResNet50_vd(nn.Layer):
block_list = []
for i in range(depth[block]):
conv_name = "res" + str(block + 2) + chr(97 + i)
dilation_rate = dilation_dict[
block] if dilation_dict and block in dilation_dict else 1
dilation_rate = dilation_dict[block] if dilation_dict and block in dilation_dict else 1
if block == 3:
dilation_rate = dilation_rate * multi_grid[i]
bottleneck_block = self.add_sublayer(
'bb_%d_%d' % (block, i),
L.BottleneckBlock(
in_channels=num_channels[block]
if i == 0 else num_filters[block] * 4,
in_channels=num_channels[block] if i == 0 else num_filters[block] * 4,
out_channels=num_filters[block],
stride=2 if i == 0 and block != 0
and dilation_rate == 1 else 1,
stride=2 if i == 0 and block != 0 and dilation_rate == 1 else 1,
shortcut=shortcut,
if_first=block == i == 0,
name=conv_name,
......
......@@ -51,8 +51,8 @@ class HRNet_W18(nn.Layer):
def __init__(self,
pretrained: str = None,
stage1_num_modules: int = 1,
stage1_num_blocks: tuple = (4,),
stage1_num_channels: tuple = (64,),
stage1_num_blocks: tuple = (4, ),
stage1_num_channels: tuple = (64, ),
stage2_num_modules: int = 1,
stage2_num_blocks: tuple = (4, 4),
stage2_num_channels: tuple = (18, 36),
......@@ -83,20 +83,10 @@ class HRNet_W18(nn.Layer):
self.feat_channels = [sum(stage4_num_channels)]
self.conv_layer1_1 = L.ConvBNReLU(
in_channels=3,
out_channels=64,
kernel_size=3,
stride=2,
padding='same',
bias_attr=False)
in_channels=3, out_channels=64, kernel_size=3, stride=2, padding='same', bias_attr=False)
self.conv_layer1_2 = L.ConvBNReLU(
in_channels=64,
out_channels=64,
kernel_size=3,
stride=2,
padding='same',
bias_attr=False)
in_channels=64, out_channels=64, kernel_size=3, stride=2, padding='same', bias_attr=False)
self.la1 = Layer1(
num_channels=64,
......@@ -106,9 +96,7 @@ class HRNet_W18(nn.Layer):
name="layer2")
self.tr1 = TransitionLayer(
in_channels=[self.stage1_num_channels[0] * 4],
out_channels=self.stage2_num_channels,
name="tr1")
in_channels=[self.stage1_num_channels[0] * 4], out_channels=self.stage2_num_channels, name="tr1")
self.st2 = Stage(
num_channels=self.stage2_num_channels,
......@@ -120,9 +108,7 @@ class HRNet_W18(nn.Layer):
align_corners=align_corners)
self.tr2 = TransitionLayer(
in_channels=self.stage2_num_channels,
out_channels=self.stage3_num_channels,
name="tr2")
in_channels=self.stage2_num_channels, out_channels=self.stage3_num_channels, name="tr2")
self.st3 = Stage(
num_channels=self.stage3_num_channels,
num_modules=self.stage3_num_modules,
......@@ -133,9 +119,7 @@ class HRNet_W18(nn.Layer):
align_corners=align_corners)
self.tr3 = TransitionLayer(
in_channels=self.stage3_num_channels,
out_channels=self.stage4_num_channels,
name="tr3")
in_channels=self.stage3_num_channels, out_channels=self.stage4_num_channels, name="tr3")
self.st4 = Stage(
num_channels=self.stage4_num_channels,
num_modules=self.stage4_num_modules,
......@@ -161,30 +145,16 @@ class HRNet_W18(nn.Layer):
st4 = self.st4(tr3)
x0_h, x0_w = st4[0].shape[2:]
x1 = F.interpolate(
st4[1], (x0_h, x0_w),
mode='bilinear',
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)
x1 = F.interpolate(st4[1], (x0_h, x0_w), mode='bilinear', 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)
return [x]
class Layer1(nn.Layer):
def __init__(self,
num_channels: int,
num_filters: int,
num_blocks: int,
has_se: bool = False,
name: str = None):
def __init__(self, num_channels: int, num_filters: int, num_blocks: int, has_se: bool = False, name: str = None):
super(Layer1, self).__init__()
self.bottleneck_block_list = []
......@@ -253,12 +223,7 @@ class TransitionLayer(nn.Layer):
class Branches(nn.Layer):
def __init__(self,
num_blocks: int,
in_channels: int,
out_channels: int,
has_se: bool = False,
name: str = None):
def __init__(self, num_blocks: int, in_channels: int, out_channels: int, has_se: bool = False, name: str = None):
super(Branches, self).__init__()
self.basic_block_list = []
......@@ -273,8 +238,7 @@ class Branches(nn.Layer):
num_channels=in_ch,
num_filters=out_channels[i],
has_se=has_se,
name=name + '_branch_layer_' + str(i + 1) + '_' +
str(j + 1)))
name=name + '_branch_layer_' + str(i + 1) + '_' + str(j + 1)))
self.basic_block_list[i].append(basic_block_func)
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
......@@ -301,11 +265,7 @@ class BottleneckBlock(nn.Layer):
self.downsample = downsample
self.conv1 = L.ConvBNReLU(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=1,
padding='same',
bias_attr=False)
in_channels=num_channels, out_channels=num_filters, kernel_size=1, padding='same', bias_attr=False)
self.conv2 = L.ConvBNReLU(
in_channels=num_filters,
......@@ -316,26 +276,15 @@ class BottleneckBlock(nn.Layer):
bias_attr=False)
self.conv3 = L.ConvBN(
in_channels=num_filters,
out_channels=num_filters * 4,
kernel_size=1,
padding='same',
bias_attr=False)
in_channels=num_filters, out_channels=num_filters * 4, kernel_size=1, padding='same', bias_attr=False)
if self.downsample:
self.conv_down = L.ConvBN(
in_channels=num_channels,
out_channels=num_filters * 4,
kernel_size=1,
padding='same',
bias_attr=False)
in_channels=num_channels, out_channels=num_filters * 4, kernel_size=1, padding='same', bias_attr=False)
if self.has_se:
self.se = SELayer(
num_channels=num_filters * 4,
num_filters=num_filters * 4,
reduction_ratio=16,
name=name + '_fc')
num_channels=num_filters * 4, num_filters=num_filters * 4, reduction_ratio=16, name=name + '_fc')
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
residual = x
......@@ -375,26 +324,14 @@ class BasicBlock(nn.Layer):
padding='same',
bias_attr=False)
self.conv2 = L.ConvBN(
in_channels=num_filters,
out_channels=num_filters,
kernel_size=3,
padding='same',
bias_attr=False)
in_channels=num_filters, out_channels=num_filters, kernel_size=3, padding='same', bias_attr=False)
if self.downsample:
self.conv_down = L.ConvBNReLU(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=1,
padding='same',
bias_attr=False)
in_channels=num_channels, out_channels=num_filters, kernel_size=1, padding='same', bias_attr=False)
if self.has_se:
self.se = SELayer(
num_channels=num_filters,
num_filters=num_filters,
reduction_ratio=16,
name=name + '_fc')
self.se = SELayer(num_channels=num_filters, num_filters=num_filters, reduction_ratio=16, name=name + '_fc')
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
residual = x
......@@ -423,17 +360,11 @@ class SELayer(nn.Layer):
med_ch = int(num_channels / reduction_ratio)
stdv = 1.0 / math.sqrt(num_channels * 1.0)
self.squeeze = nn.Linear(
num_channels,
med_ch,
weight_attr=paddle.ParamAttr(
initializer=nn.initializer.Uniform(-stdv, stdv)))
num_channels, med_ch, weight_attr=paddle.ParamAttr(initializer=nn.initializer.Uniform(-stdv, stdv)))
stdv = 1.0 / math.sqrt(med_ch * 1.0)
self.excitation = nn.Linear(
med_ch,
num_filters,
weight_attr=paddle.ParamAttr(
initializer=nn.initializer.Uniform(-stdv, stdv)))
med_ch, num_filters, weight_attr=paddle.ParamAttr(initializer=nn.initializer.Uniform(-stdv, stdv)))
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
pool = self.pool2d_gap(x)
......@@ -442,8 +373,7 @@ class SELayer(nn.Layer):
squeeze = F.relu(squeeze)
excitation = self.excitation(squeeze)
excitation = F.sigmoid(excitation)
excitation = paddle.reshape(
excitation, shape=[-1, self._num_channels, 1, 1])
excitation = paddle.reshape(excitation, shape=[-1, self._num_channels, 1, 1])
out = x * excitation
return out
......@@ -507,11 +437,7 @@ class HighResolutionModule(nn.Layer):
super(HighResolutionModule, self).__init__()
self.branches_func = Branches(
num_blocks=num_blocks,
in_channels=num_channels,
out_channels=num_filters,
has_se=has_se,
name=name)
num_blocks=num_blocks, in_channels=num_channels, out_channels=num_filters, has_se=has_se, name=name)
self.fuse_func = FuseLayers(
in_channels=num_filters,
......@@ -557,8 +483,7 @@ class FuseLayers(nn.Layer):
for k in range(i - j):
if k == i - j - 1:
residual_func = self.add_sublayer(
"residual_{}_layer_{}_{}_{}".format(
name, i + 1, j + 1, k + 1),
"residual_{}_layer_{}_{}_{}".format(name, i + 1, j + 1, k + 1),
L.ConvBN(
in_channels=pre_num_filters,
out_channels=out_channels[i],
......@@ -569,8 +494,7 @@ class FuseLayers(nn.Layer):
pre_num_filters = out_channels[i]
else:
residual_func = self.add_sublayer(
"residual_{}_layer_{}_{}_{}".format(
name, i + 1, j + 1, k + 1),
"residual_{}_layer_{}_{}_{}".format(name, i + 1, j + 1, k + 1),
L.ConvBNReLU(
in_channels=pre_num_filters,
out_channels=out_channels[j],
......@@ -592,11 +516,7 @@ class FuseLayers(nn.Layer):
y = self.residual_func_list[residual_func_idx](x[j])
residual_func_idx += 1
y = F.interpolate(
y,
residual_shape,
mode='bilinear',
align_corners=self.align_corners)
y = F.interpolate(y, residual_shape, mode='bilinear', align_corners=self.align_corners)
residual = residual + y
elif j < i:
y = x[j]
......
......@@ -30,8 +30,7 @@ def SyncBatchNorm(*args, **kwargs):
class ConvBNLayer(nn.Layer):
"""Basic conv bn relu layer."""
def __init__(
self,
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
......@@ -44,8 +43,7 @@ class ConvBNLayer(nn.Layer):
super(ConvBNLayer, self).__init__()
self.is_vd_mode = is_vd_mode
self._pool2d_avg = AvgPool2D(
kernel_size=2, stride=2, padding=0, ceil_mode=True)
self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True)
self._conv = Conv2D(
in_channels=in_channels,
out_channels=out_channels,
......@@ -83,11 +81,7 @@ class BottleneckBlock(nn.Layer):
super(BottleneckBlock, self).__init__()
self.conv0 = ConvBNLayer(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
act='relu',
name=name + "_branch2a")
in_channels=in_channels, out_channels=out_channels, kernel_size=1, act='relu', name=name + "_branch2a")
self.dilation = dilation
......@@ -100,11 +94,7 @@ class BottleneckBlock(nn.Layer):
dilation=dilation,
name=name + "_branch2b")
self.conv2 = ConvBNLayer(
in_channels=out_channels,
out_channels=out_channels * 4,
kernel_size=1,
act=None,
name=name + "_branch2c")
in_channels=out_channels, out_channels=out_channels * 4, kernel_size=1, act=None, name=name + "_branch2c")
if not shortcut:
self.short = ConvBNLayer(
......@@ -139,12 +129,7 @@ class BottleneckBlock(nn.Layer):
class SeparableConvBNReLU(nn.Layer):
"""Depthwise Separable Convolution."""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
super(SeparableConvBNReLU, self).__init__()
self.depthwise_conv = ConvBN(
in_channels,
......@@ -153,8 +138,7 @@ class SeparableConvBNReLU(nn.Layer):
padding=padding,
groups=in_channels,
**kwargs)
self.piontwise_conv = ConvBNReLU(
in_channels, out_channels, kernel_size=1, groups=1)
self.piontwise_conv = ConvBNReLU(in_channels, out_channels, kernel_size=1, groups=1)
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
x = self.depthwise_conv(x)
......@@ -165,15 +149,9 @@ class SeparableConvBNReLU(nn.Layer):
class ConvBN(nn.Layer):
"""Basic conv bn layer"""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
super(ConvBN, self).__init__()
self._conv = Conv2D(
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
......@@ -185,16 +163,10 @@ class ConvBN(nn.Layer):
class ConvBNReLU(nn.Layer):
"""Basic conv bn relu layer."""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = 'same',
**kwargs: dict):
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, padding: str = 'same', **kwargs: dict):
super(ConvBNReLU, self).__init__()
self._conv = Conv2D(
in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._conv = Conv2D(in_channels, out_channels, kernel_size, padding=padding, **kwargs)
self._batch_norm = SyncBatchNorm(out_channels)
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
......@@ -251,8 +223,7 @@ class Activation(nn.Layer):
act_name = act_dict[act]
self.act_func = eval("activation.{}()".format(act_name))
else:
raise KeyError("{} does not exist in the current {}".format(
act, act_dict.keys()))
raise KeyError("{} does not exist in the current {}".format(act, act_dict.keys()))
def forward(self, x: paddle.Tensor) -> paddle.Tensor:
......@@ -276,13 +247,7 @@ class ASPPModule(nn.Layer):
image_pooling (bool, optional): If augmented with image-level features. Default: False
"""
def __init__(self,
aspp_ratios,
in_channels,
out_channels,
align_corners,
use_sep_conv=False,
image_pooling=False):
def __init__(self, aspp_ratios, in_channels, out_channels, align_corners, use_sep_conv=False, image_pooling=False):
super().__init__()
self.align_corners = align_corners
......@@ -311,10 +276,7 @@ class ASPPModule(nn.Layer):
out_size += 1
self.image_pooling = image_pooling
self.conv_bn_relu = ConvBNReLU(
in_channels=out_channels * out_size,
out_channels=out_channels,
kernel_size=1)
self.conv_bn_relu = ConvBNReLU(in_channels=out_channels * out_size, out_channels=out_channels, kernel_size=1)
self.dropout = nn.Dropout(p=0.1) # drop rate
......@@ -322,20 +284,12 @@ class ASPPModule(nn.Layer):
outputs = []
for block in self.aspp_blocks:
y = block(x)
y = F.interpolate(
y,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
y = F.interpolate(y, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
outputs.append(y)
if self.image_pooling:
img_avg = self.global_avg_pool(x)
img_avg = F.interpolate(
img_avg,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners)
img_avg = F.interpolate(img_avg, x.shape[2:], mode='bilinear', align_corners=self.align_corners)
outputs.append(img_avg)
x = paddle.concat(outputs, axis=1)
......
......@@ -25,6 +25,7 @@ from paddlehub.module.cv_module import ImageSegmentationModule
import ocrnet_hrnetw18_voc.layers as L
from ocrnet_hrnetw18_voc.hrnet import HRNet_W18
@moduleinfo(
name="ocrnet_hrnetw18_voc",
type="CV/semantic_segmentation",
......@@ -90,11 +91,8 @@ class OCRNetHRNetW18(nn.Layer):
feats = [feats[i] for i in self.backbone_indices]
logit_list = self.head(feats)
logit_list = [
F.interpolate(
logit,
x.shape[2:],
mode='bilinear',
align_corners=self.align_corners) for logit in logit_list]
F.interpolate(logit, x.shape[2:], mode='bilinear', align_corners=self.align_corners) for logit in logit_list
]
return logit_list
......@@ -108,32 +106,23 @@ class OCRHead(nn.Layer):
ocr_key_channels(int, optional): The number of key channels in ObjectAttentionBlock. Default: 256.
"""
def __init__(self,
num_classes: int,
in_channels: int,
ocr_mid_channels: int = 512,
ocr_key_channels: int = 256):
def __init__(self, num_classes: int, in_channels: int, ocr_mid_channels: int = 512, ocr_key_channels: int = 256):
super().__init__()
self.num_classes = num_classes
self.spatial_gather = SpatialGatherBlock()
self.spatial_ocr = SpatialOCRModule(ocr_mid_channels, ocr_key_channels,
ocr_mid_channels)
self.spatial_ocr = SpatialOCRModule(ocr_mid_channels, ocr_key_channels, ocr_mid_channels)
self.indices = [-2, -1] if len(in_channels) > 1 else [-1, -1]
self.conv3x3_ocr = L.ConvBNReLU(
in_channels[self.indices[1]], ocr_mid_channels, 3, padding=1)
self.conv3x3_ocr = L.ConvBNReLU(in_channels[self.indices[1]], ocr_mid_channels, 3, padding=1)
self.cls_head = nn.Conv2D(ocr_mid_channels, self.num_classes, 1)
self.aux_head = nn.Sequential(
L.ConvBNReLU(in_channels[self.indices[0]],
in_channels[self.indices[0]], 1),
L.ConvBNReLU(in_channels[self.indices[0]], in_channels[self.indices[0]], 1),
nn.Conv2D(in_channels[self.indices[0]], self.num_classes, 1))
def forward(self, feat_list: List[paddle.Tensor]) -> paddle.Tensor:
feat_shallow, feat_deep = feat_list[self.indices[0]], feat_list[
self.indices[1]]
feat_shallow, feat_deep = feat_list[self.indices[0]], feat_list[self.indices[1]]
soft_regions = self.aux_head(feat_shallow)
pixels = self.conv3x3_ocr(feat_deep)
......@@ -171,17 +160,11 @@ class SpatialGatherBlock(nn.Layer):
class SpatialOCRModule(nn.Layer):
"""Aggregate the global object representation to update the representation for each pixel."""
def __init__(self,
in_channels: int,
key_channels: int,
out_channels: int,
dropout_rate: float = 0.1):
def __init__(self, in_channels: int, key_channels: int, out_channels: int, dropout_rate: float = 0.1):
super().__init__()
self.attention_block = ObjectAttentionBlock(in_channels, key_channels)
self.conv1x1 = nn.Sequential(
L.ConvBNReLU(2 * in_channels, out_channels, 1),
nn.Dropout2D(dropout_rate))
self.conv1x1 = nn.Sequential(L.ConvBNReLU(2 * in_channels, out_channels, 1), nn.Dropout2D(dropout_rate))
def forward(self, pixels: paddle.Tensor, regions: paddle.Tensor) -> paddle.Tensor:
context = self.attention_block(pixels, regions)
......@@ -201,12 +184,10 @@ class ObjectAttentionBlock(nn.Layer):
self.key_channels = key_channels
self.f_pixel = nn.Sequential(
L.ConvBNReLU(in_channels, key_channels, 1),
L.ConvBNReLU(key_channels, key_channels, 1))
L.ConvBNReLU(in_channels, key_channels, 1), L.ConvBNReLU(key_channels, key_channels, 1))
self.f_object = nn.Sequential(
L.ConvBNReLU(in_channels, key_channels, 1),
L.ConvBNReLU(key_channels, key_channels, 1))
L.ConvBNReLU(in_channels, key_channels, 1), L.ConvBNReLU(key_channels, key_channels, 1))
self.f_down = L.ConvBNReLU(in_channels, key_channels, 1)
......
......@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
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.metric = paddle.metric.Accuracy()
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.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-cased', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-cased', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -173,5 +170,4 @@ class Bert(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return BertTokenizer.from_pretrained(
pretrained_model_name_or_path='bert-base-cased', *args, **kwargs)
return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-cased', *args, **kwargs)
......@@ -60,37 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-chinese',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-base-chinese', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-chinese',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-base-chinese', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +170,4 @@ class Bert(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return BertTokenizer.from_pretrained(
pretrained_model_name_or_path='bert-base-chinese', *args, **kwargs)
return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-chinese', *args, **kwargs)
......@@ -60,37 +60,29 @@ class Bert(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-cased',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-base-multilingual-cased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-cased',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-base-multilingual-cased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
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.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.metric = paddle.metric.Accuracy()
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:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +130,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......
......@@ -60,37 +60,29 @@ class Bert(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-uncased',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-base-multilingual-uncased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-base-multilingual-uncased',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-base-multilingual-uncased', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
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.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.metric = paddle.metric.Accuracy()
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:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +130,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......
......@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
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.metric = paddle.metric.Accuracy()
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.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -173,5 +170,4 @@ class Bert(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return BertTokenizer.from_pretrained(
pretrained_model_name_or_path='bert-base-uncased', *args, **kwargs)
return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-uncased', *args, **kwargs)
......@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
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.metric = paddle.metric.Accuracy()
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.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-cased', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-cased', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -173,5 +170,4 @@ class Bert(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return BertTokenizer.from_pretrained(
pretrained_model_name_or_path='bert-large-cased', *args, **kwargs)
return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-large-cased', *args, **kwargs)
......@@ -60,29 +60,27 @@ class Bert(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
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.metric = paddle.metric.Accuracy()
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.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -130,8 +128,7 @@ class Bert(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -173,5 +170,4 @@ class Bert(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return BertTokenizer.from_pretrained(
pretrained_model_name_or_path='bert-large-uncased', *args, **kwargs)
return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', *args, **kwargs)
......@@ -35,8 +35,7 @@ from paddlehub.utils.log import logger
author="ymcui",
author_email="ymcui@ir.hit.edu.cn",
type="nlp/semantic_model",
meta=TransformerModule
)
meta=TransformerModule)
class BertWwm(nn.Layer):
"""
BertWwm model
......@@ -61,37 +60,27 @@ class BertWwm(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-chinese',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-wwm-chinese', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-chinese',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-wwm-chinese', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -139,8 +128,7 @@ class BertWwm(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -182,5 +170,4 @@ class BertWwm(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return BertTokenizer.from_pretrained(
pretrained_model_name_or_path='bert-wwm-chinese', *args, **kwargs)
return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-wwm-chinese', *args, **kwargs)
......@@ -35,8 +35,7 @@ from paddlehub.utils.log import logger
author="ymcui",
author_email="ymcui@ir.hit.edu.cn",
type="nlp/semantic_model",
meta=TransformerModule
)
meta=TransformerModule)
class BertWwm(nn.Layer):
"""
BertWwm model
......@@ -61,37 +60,27 @@ class BertWwm(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = BertForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-ext-chinese',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-wwm-ext-chinese', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = BertForTokenClassification.from_pretrained(
pretrained_model_name_or_path='bert-wwm-ext-chinese',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='bert-wwm-ext-chinese', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -139,8 +128,7 @@ class BertWwm(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -182,5 +170,4 @@ class BertWwm(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return BertTokenizer.from_pretrained(
pretrained_model_name_or_path='bert-wwm-ext-chinese', *args, **kwargs)
return BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-wwm-ext-chinese', *args, **kwargs)
......@@ -60,37 +60,27 @@ class Electra(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-base',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='chinese-electra-base', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-base',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='chinese-electra-base', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +128,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +170,4 @@ class Electra(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ElectraTokenizer.from_pretrained(
pretrained_model_name_or_path='chinese-electra-base', *args, **kwargs)
return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='chinese-electra-base', *args, **kwargs)
......@@ -60,37 +60,27 @@ class Electra(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-small',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='chinese-electra-small', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='chinese-electra-small',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='chinese-electra-small', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +128,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +170,4 @@ class Electra(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ElectraTokenizer.from_pretrained(
pretrained_model_name_or_path='chinese-electra-small', *args, **kwargs)
return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='chinese-electra-small', *args, **kwargs)
......@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger
@moduleinfo(
name="electra-base",
version="1.0.1",
summary=
"electra-base, 12-layer, 768-hidden, 12-heads, 110M parameters. The module is executed as paddle.dygraph.",
summary="electra-base, 12-layer, 768-hidden, 12-heads, 110M parameters. The module is executed as paddle.dygraph.",
author="paddlepaddle",
author_email="",
type="nlp/semantic_model",
......@@ -60,37 +59,27 @@ class Electra(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='electra-base',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='electra-base', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='electra-base',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='electra-base', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-base', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-base', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +127,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +169,4 @@ class Electra(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ElectraTokenizer.from_pretrained(
pretrained_model_name_or_path='electra-base', *args, **kwargs)
return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='electra-base', *args, **kwargs)
......@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger
@moduleinfo(
name="electra-large",
version="1.0.1",
summary=
"electra-large, 24-layer, 1024-hidden, 16-heads, 335M parameters. The module is executed as paddle.dygraph.",
summary="electra-large, 24-layer, 1024-hidden, 16-heads, 335M parameters. The module is executed as paddle.dygraph.",
author="paddlepaddle",
author_email="",
type="nlp/semantic_model",
......@@ -60,37 +59,27 @@ class Electra(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='electra-large',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='electra-large', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='electra-large',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='electra-large', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-large', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-large', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +127,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +169,4 @@ class Electra(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ElectraTokenizer.from_pretrained(
pretrained_model_name_or_path='electra-large', *args, **kwargs)
return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='electra-large', *args, **kwargs)
......@@ -29,8 +29,7 @@ from paddlehub.utils.log import logger
@moduleinfo(
name="electra-small",
version="1.0.1",
summary=
"electra-small, 12-layer, 256-hidden, 4-heads, 14M parameters. The module is executed as paddle.dygraph.",
summary="electra-small, 12-layer, 256-hidden, 4-heads, 14M parameters. The module is executed as paddle.dygraph.",
author="paddlepaddle",
author_email="",
type="nlp/semantic_model",
......@@ -60,37 +59,27 @@ class Electra(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = ElectraForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='electra-small',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='electra-small', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = ElectraForTokenClassification.from_pretrained(
pretrained_model_name_or_path='electra-small',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='electra-small', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-small', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ElectraModel.from_pretrained(pretrained_model_name_or_path='electra-small', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +127,7 @@ class Electra(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +169,4 @@ class Electra(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ElectraTokenizer.from_pretrained(
pretrained_model_name_or_path='electra-small', *args, **kwargs)
return ElectraTokenizer.from_pretrained(pretrained_model_name_or_path='electra-small', *args, **kwargs)
......@@ -60,29 +60,27 @@ class Ernie(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
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.metric = paddle.metric.Accuracy()
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.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-1.0', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-1.0', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -130,8 +128,7 @@ class Ernie(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -173,5 +170,4 @@ class Ernie(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ErnieTokenizer.from_pretrained(
pretrained_model_name_or_path='ernie-1.0', *args, **kwargs)
return ErnieTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-1.0', *args, **kwargs)
......@@ -59,29 +59,27 @@ class ErnieTiny(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
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.metric = paddle.metric.Accuracy()
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.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-tiny', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-tiny', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -129,8 +127,7 @@ class ErnieTiny(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -172,5 +169,4 @@ class ErnieTiny(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ErnieTinyTokenizer.from_pretrained(
pretrained_model_name_or_path='ernie-tiny', *args, **kwargs)
return ErnieTinyTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-tiny', *args, **kwargs)
......@@ -60,37 +60,27 @@ class ErnieV2(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = ErnieForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-en',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='ernie-2.0-en', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = ErnieForTokenClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-en',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='ernie-2.0-en', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +128,7 @@ class ErnieV2(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +170,4 @@ class ErnieV2(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ErnieTokenizer.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-en', *args, **kwargs)
\ No newline at end of file
return ErnieTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-2.0-en', *args, **kwargs)
......@@ -60,37 +60,27 @@ class ErnieV2(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = ErnieForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-large-en',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='ernie-2.0-large-en', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = ErnieForTokenClassification.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-large-en',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='ernie-2.0-large-en', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = ErnieModel.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +128,7 @@ class ErnieV2(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +170,4 @@ class ErnieV2(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return ErnieTokenizer.from_pretrained(
pretrained_model_name_or_path='ernie-2.0-large-en', *args, **kwargs)
return ErnieTokenizer.from_pretrained(pretrained_model_name_or_path='ernie-2.0-large-en', *args, **kwargs)
......@@ -60,37 +60,27 @@ class Roberta(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='rbt3',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='rbt3', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='rbt3',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='rbt3', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbt3', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbt3', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +128,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +170,4 @@ class Roberta(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return RobertaTokenizer.from_pretrained(
pretrained_model_name_or_path='rbt3', *args, **kwargs)
return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='rbt3', *args, **kwargs)
......@@ -60,37 +60,27 @@ class Roberta(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='rbtl3',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='rbtl3', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='rbtl3',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='rbtl3', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbtl3', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='rbtl3', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -138,8 +128,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -181,5 +170,4 @@ class Roberta(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return RobertaTokenizer.from_pretrained(
pretrained_model_name_or_path='rbtl3', *args, **kwargs)
return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='rbtl3', *args, **kwargs)
......@@ -61,37 +61,27 @@ class Roberta(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext-large',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='roberta-wwm-ext-large', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext-large',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='roberta-wwm-ext-large', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -139,8 +129,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -182,5 +171,4 @@ class Roberta(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return RobertaTokenizer.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext-large', *args, **kwargs)
return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext-large', *args, **kwargs)
......@@ -61,37 +61,27 @@ class Roberta(nn.Layer):
task = 'seq-cls'
logger.warning(
"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':
self.model = RobertaForSequenceClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='roberta-wwm-ext', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = paddle.metric.Accuracy()
elif task == 'token-cls':
self.model = RobertaForTokenClassification.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext',
num_classes=self.num_classes,
**kwargs
)
pretrained_model_name_or_path='roberta-wwm-ext', num_classes=self.num_classes, **kwargs)
self.criterion = paddle.nn.loss.CrossEntropyLoss()
self.metric = ChunkEvaluator(
label_list=[self.label_map[i] for i in sorted(self.label_map.keys())]
)
self.metric = ChunkEvaluator(label_list=[self.label_map[i] for i in sorted(self.label_map.keys())])
elif task == 'text-matching':
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', **kwargs)
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.metric = paddle.metric.Accuracy()
elif task is None:
self.model = RobertaModel.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', **kwargs)
else:
raise RuntimeError("Unknown task {}, task should be one in {}".format(
task, self._tasks_supported))
raise RuntimeError("Unknown task {}, task should be one in {}".format(task, self._tasks_supported))
self.task = task
......@@ -139,8 +129,7 @@ class Roberta(nn.Layer):
loss = self.criterion(logits, labels.unsqueeze(-1))
num_infer_chunks, num_label_chunks, num_correct_chunks = \
self.metric.compute(None, seq_lengths, preds, labels)
self.metric.update(
num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
self.metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
_, _, f1_score = map(float, self.metric.accumulate())
return token_level_probs, loss, {'f1_score': f1_score}
return token_level_probs
......@@ -182,5 +171,4 @@ class Roberta(nn.Layer):
"""
Gets the tokenizer that is customized for this module.
"""
return RobertaTokenizer.from_pretrained(
pretrained_model_name_or_path='roberta-wwm-ext', *args, **kwargs)
return RobertaTokenizer.from_pretrained(pretrained_model_name_or_path='roberta-wwm-ext', *args, **kwargs)
......@@ -29,7 +29,8 @@ class DataFormatError(Exception):
self.args = args
@moduleinfo(name="simnet_bow",
@moduleinfo(
name="simnet_bow",
version="1.2.0",
summary="Baidu's open-source similarity network model based on bow_pairwise.",
author="baidu-nlp",
......@@ -106,11 +107,11 @@ class SimnetBow(hub.Module):
seq_len_used = fluid.layers.squeeze(seq_len, axes=[1])
# Add embedding layer.
w_param_attrs = fluid.ParamAttr(name="emb",
initializer=fluid.initializer.TruncatedNormal(scale=0.02),
trainable=trainable)
w_param_attrs = fluid.ParamAttr(
name="emb", initializer=fluid.initializer.TruncatedNormal(scale=0.02), trainable=trainable)
dict_dim = 500002
emb_1 = fluid.layers.embedding(input=text_1,
emb_1 = fluid.layers.embedding(
input=text_1,
size=[dict_dim, 128],
is_sparse=True,
padding_idx=dict_dim - 1,
......@@ -122,7 +123,8 @@ class SimnetBow(hub.Module):
if num_slots > 1:
text_2 = fluid.data(name='text_2', shape=[-1, max_seq_len], dtype='int64', lod_level=0)
emb_2 = fluid.embedding(input=text_2,
emb_2 = fluid.embedding(
input=text_2,
size=[dict_dim, 128],
is_sparse=True,
padding_idx=dict_dim - 1,
......@@ -134,7 +136,8 @@ class SimnetBow(hub.Module):
if num_slots > 2:
text_3 = fluid.data(name='text_3', shape=[-1, max_seq_len], dtype='int64', lod_level=0)
emb_3 = fluid.embedding(input=text_3,
emb_3 = fluid.embedding(
input=text_3,
size=[dict_dim, 128],
is_sparse=True,
padding_idx=dict_dim - 1,
......@@ -295,10 +298,8 @@ class SimnetBow(hub.Module):
"""
Run as a command
"""
self.parser = argparse.ArgumentParser(description="Run the simnet_bow module.",
prog='hub run simnet_bow',
usage='%(prog)s',
add_help=True)
self.parser = argparse.ArgumentParser(
description="Run the simnet_bow module.", prog='hub run simnet_bow', usage='%(prog)s', add_help=True)
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(
......@@ -323,10 +324,8 @@ class SimnetBow(hub.Module):
"""
Add the command config options
"""
self.arg_config_group.add_argument('--use_gpu',
type=ast.literal_eval,
default=False,
help="whether use GPU for prediction")
self.arg_config_group.add_argument(
'--use_gpu', type=ast.literal_eval, 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")
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册