parent
7ffcf05809
commit
c1f637c107
@ -0,0 +1,91 @@
|
|||||||
|
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
# ============================================================================
|
||||||
|
"""Evaluation callback when training"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import stat
|
||||||
|
from mindspore import save_checkpoint
|
||||||
|
from mindspore import log as logger
|
||||||
|
from mindspore.train.callback import Callback
|
||||||
|
|
||||||
|
class EvalCallBack(Callback):
|
||||||
|
"""
|
||||||
|
Evaluation callback when training.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
eval_function (function): evaluation function.
|
||||||
|
eval_param_dict (dict): evaluation parameters' configure dict.
|
||||||
|
interval (int): run evaluation interval, default is 1.
|
||||||
|
eval_start_epoch (int): evaluation start epoch, default is 1.
|
||||||
|
save_best_ckpt (bool): Whether to save best checkpoint, default is True.
|
||||||
|
besk_ckpt_name (str): bast checkpoint name, default is `best.ckpt`.
|
||||||
|
metrics_name (str): evaluation metrics name, default is `acc`.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
>>> EvalCallBack(eval_function, eval_param_dict)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, eval_function, eval_param_dict, interval=1, eval_start_epoch=1, save_best_ckpt=True,
|
||||||
|
ckpt_directory="./", besk_ckpt_name="best.ckpt", metrics_name="acc"):
|
||||||
|
super(EvalCallBack, self).__init__()
|
||||||
|
self.eval_param_dict = eval_param_dict
|
||||||
|
self.eval_function = eval_function
|
||||||
|
self.eval_start_epoch = eval_start_epoch
|
||||||
|
if interval < 1:
|
||||||
|
raise ValueError("interval should >= 1.")
|
||||||
|
self.interval = interval
|
||||||
|
self.save_best_ckpt = save_best_ckpt
|
||||||
|
self.best_res = 0
|
||||||
|
self.best_epoch = 0
|
||||||
|
if not os.path.isdir(ckpt_directory):
|
||||||
|
os.makedirs(ckpt_directory)
|
||||||
|
self.bast_ckpt_path = os.path.join(ckpt_directory, besk_ckpt_name)
|
||||||
|
self.metrics_name = metrics_name
|
||||||
|
|
||||||
|
def remove_ckpoint_file(self, file_name):
|
||||||
|
"""Remove the specified checkpoint file from this checkpoint manager and also from the directory."""
|
||||||
|
try:
|
||||||
|
os.chmod(file_name, stat.S_IWRITE)
|
||||||
|
os.remove(file_name)
|
||||||
|
except OSError:
|
||||||
|
logger.warning("OSError, failed to remove the older ckpt file %s.", file_name)
|
||||||
|
except ValueError:
|
||||||
|
logger.warning("ValueError, failed to remove the older ckpt file %s.", file_name)
|
||||||
|
|
||||||
|
def epoch_end(self, run_context):
|
||||||
|
"""Callback when epoch end."""
|
||||||
|
cb_params = run_context.original_args()
|
||||||
|
cur_epoch = cb_params.cur_epoch_num
|
||||||
|
if cur_epoch >= self.eval_start_epoch and (cur_epoch - self.eval_start_epoch) % self.interval == 0:
|
||||||
|
res = self.eval_function(self.eval_param_dict)
|
||||||
|
print("epoch: {}, {}: {}".format(cur_epoch, self.metrics_name, res), flush=True)
|
||||||
|
if res >= self.best_res:
|
||||||
|
self.best_res = res
|
||||||
|
self.best_epoch = cur_epoch
|
||||||
|
print("update best result: {}".format(res), flush=True)
|
||||||
|
if self.save_best_ckpt:
|
||||||
|
if os.path.exists(self.bast_ckpt_path):
|
||||||
|
self.remove_ckpoint_file(self.bast_ckpt_path)
|
||||||
|
save_checkpoint(cb_params.train_network, self.bast_ckpt_path)
|
||||||
|
print("update best checkpoint at: {}".format(self.bast_ckpt_path), flush=True)
|
||||||
|
|
||||||
|
def end(self, run_context):
|
||||||
|
print("End training, the best {0} is: {1}, the best {0} epoch is {2}".format(self.metrics_name,
|
||||||
|
self.best_res,
|
||||||
|
self.best_epoch), flush=True)
|
||||||
|
|
@ -0,0 +1,91 @@
|
|||||||
|
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
# ============================================================================
|
||||||
|
"""Evaluation callback when training"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import stat
|
||||||
|
from mindspore import save_checkpoint
|
||||||
|
from mindspore import log as logger
|
||||||
|
from mindspore.train.callback import Callback
|
||||||
|
|
||||||
|
class EvalCallBack(Callback):
|
||||||
|
"""
|
||||||
|
Evaluation callback when training.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
eval_function (function): evaluation function.
|
||||||
|
eval_param_dict (dict): evaluation parameters' configure dict.
|
||||||
|
interval (int): run evaluation interval, default is 1.
|
||||||
|
eval_start_epoch (int): evaluation start epoch, default is 1.
|
||||||
|
save_best_ckpt (bool): Whether to save best checkpoint, default is True.
|
||||||
|
besk_ckpt_name (str): bast checkpoint name, default is `best.ckpt`.
|
||||||
|
metrics_name (str): evaluation metrics name, default is `acc`.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
>>> EvalCallBack(eval_function, eval_param_dict)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, eval_function, eval_param_dict, interval=1, eval_start_epoch=1, save_best_ckpt=True,
|
||||||
|
ckpt_directory="./", besk_ckpt_name="best.ckpt", metrics_name="acc"):
|
||||||
|
super(EvalCallBack, self).__init__()
|
||||||
|
self.eval_param_dict = eval_param_dict
|
||||||
|
self.eval_function = eval_function
|
||||||
|
self.eval_start_epoch = eval_start_epoch
|
||||||
|
if interval < 1:
|
||||||
|
raise ValueError("interval should >= 1.")
|
||||||
|
self.interval = interval
|
||||||
|
self.save_best_ckpt = save_best_ckpt
|
||||||
|
self.best_res = 0
|
||||||
|
self.best_epoch = 0
|
||||||
|
if not os.path.isdir(ckpt_directory):
|
||||||
|
os.makedirs(ckpt_directory)
|
||||||
|
self.bast_ckpt_path = os.path.join(ckpt_directory, besk_ckpt_name)
|
||||||
|
self.metrics_name = metrics_name
|
||||||
|
|
||||||
|
def remove_ckpoint_file(self, file_name):
|
||||||
|
"""Remove the specified checkpoint file from this checkpoint manager and also from the directory."""
|
||||||
|
try:
|
||||||
|
os.chmod(file_name, stat.S_IWRITE)
|
||||||
|
os.remove(file_name)
|
||||||
|
except OSError:
|
||||||
|
logger.warning("OSError, failed to remove the older ckpt file %s.", file_name)
|
||||||
|
except ValueError:
|
||||||
|
logger.warning("ValueError, failed to remove the older ckpt file %s.", file_name)
|
||||||
|
|
||||||
|
def epoch_end(self, run_context):
|
||||||
|
"""Callback when epoch end."""
|
||||||
|
cb_params = run_context.original_args()
|
||||||
|
cur_epoch = cb_params.cur_epoch_num
|
||||||
|
if cur_epoch >= self.eval_start_epoch and (cur_epoch - self.eval_start_epoch) % self.interval == 0:
|
||||||
|
res = self.eval_function(self.eval_param_dict)
|
||||||
|
print("epoch: {}, {}: {}".format(cur_epoch, self.metrics_name, res), flush=True)
|
||||||
|
if res >= self.best_res:
|
||||||
|
self.best_res = res
|
||||||
|
self.best_epoch = cur_epoch
|
||||||
|
print("update best result: {}".format(res), flush=True)
|
||||||
|
if self.save_best_ckpt:
|
||||||
|
if os.path.exists(self.bast_ckpt_path):
|
||||||
|
self.remove_ckpoint_file(self.bast_ckpt_path)
|
||||||
|
save_checkpoint(cb_params.train_network, self.bast_ckpt_path)
|
||||||
|
print("update best checkpoint at: {}".format(self.bast_ckpt_path), flush=True)
|
||||||
|
|
||||||
|
def end(self, run_context):
|
||||||
|
print("End training, the best {0} is: {1}, the best {0} epoch is {2}".format(self.metrics_name,
|
||||||
|
self.best_res,
|
||||||
|
self.best_epoch), flush=True)
|
||||||
|
|
@ -0,0 +1,96 @@
|
|||||||
|
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
# ============================================================================
|
||||||
|
"""Evaluation utils for CTPN"""
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import numpy as np
|
||||||
|
from src.config import config
|
||||||
|
from src.text_connector.detector import detect
|
||||||
|
|
||||||
|
def exec_shell_cmd(cmd):
|
||||||
|
sub = subprocess.Popen(args="{}".format(cmd), shell=True, stdin=subprocess.PIPE, \
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||||
|
stdout_data, _ = sub.communicate()
|
||||||
|
if sub.returncode != 0:
|
||||||
|
raise ValueError("{} is not a executable command, please check.".format(cmd))
|
||||||
|
return stdout_data.strip()
|
||||||
|
|
||||||
|
def get_eval_result():
|
||||||
|
create_eval_bbox = 'rm -rf submit*.zip;cd ./submit/;zip -r ../submit.zip *.txt;cd ../;bash eval_res.sh'
|
||||||
|
os.system(create_eval_bbox)
|
||||||
|
get_eval_output = "grep hmean log | awk '{print $NF}' | awk -F} '{print $1}' |tail -n 1"
|
||||||
|
hmean = exec_shell_cmd(get_eval_output)
|
||||||
|
return float(hmean)
|
||||||
|
|
||||||
|
def eval_for_ctpn(network, dataset, eval_image_path):
|
||||||
|
network.set_train(False)
|
||||||
|
eval_iter = 0
|
||||||
|
img_basenames = []
|
||||||
|
output_dir = os.path.join(os.getcwd(), "submit")
|
||||||
|
if not os.path.exists(output_dir):
|
||||||
|
os.mkdir(output_dir)
|
||||||
|
for file in os.listdir(eval_image_path):
|
||||||
|
img_basenames.append(os.path.basename(file))
|
||||||
|
img_basenames = sorted(img_basenames)
|
||||||
|
for data in dataset.create_dict_iterator():
|
||||||
|
img_data = data['image']
|
||||||
|
img_metas = data['image_shape']
|
||||||
|
gt_bboxes = data['box']
|
||||||
|
gt_labels = data['label']
|
||||||
|
gt_num = data['valid_num']
|
||||||
|
# run net
|
||||||
|
output = network(img_data, gt_bboxes, gt_labels, gt_num)
|
||||||
|
gt_bboxes = gt_bboxes.asnumpy()
|
||||||
|
gt_labels = gt_labels.asnumpy()
|
||||||
|
gt_num = gt_num.asnumpy().astype(bool)
|
||||||
|
proposal = output[0]
|
||||||
|
proposal_mask = output[1]
|
||||||
|
for j in range(config.test_batch_size):
|
||||||
|
img = img_basenames[config.test_batch_size * eval_iter + j]
|
||||||
|
all_box_tmp = proposal[j].asnumpy()
|
||||||
|
all_mask_tmp = np.expand_dims(proposal_mask[j].asnumpy(), axis=1)
|
||||||
|
using_boxes_mask = all_box_tmp * all_mask_tmp
|
||||||
|
textsegs = using_boxes_mask[:, 0:4].astype(np.float32)
|
||||||
|
scores = using_boxes_mask[:, 4].astype(np.float32)
|
||||||
|
shape = img_metas.asnumpy()[0][:2].astype(np.int32)
|
||||||
|
bboxes = detect(textsegs, scores[:, np.newaxis], shape)
|
||||||
|
from PIL import Image, ImageDraw
|
||||||
|
im = Image.open(eval_image_path + '/' + img)
|
||||||
|
draw = ImageDraw.Draw(im)
|
||||||
|
image_h = img_metas.asnumpy()[j][2]
|
||||||
|
image_w = img_metas.asnumpy()[j][3]
|
||||||
|
gt_boxs = gt_bboxes[j][gt_num[j], :]
|
||||||
|
for gt_box in gt_boxs:
|
||||||
|
gt_x1 = gt_box[0] / image_w
|
||||||
|
gt_y1 = gt_box[1] / image_h
|
||||||
|
gt_x2 = gt_box[2] / image_w
|
||||||
|
gt_y2 = gt_box[3] / image_h
|
||||||
|
draw.line([(gt_x1, gt_y1), (gt_x1, gt_y2), (gt_x2, gt_y2), (gt_x2, gt_y1), (gt_x1, gt_y1)],\
|
||||||
|
fill='green', width=2)
|
||||||
|
file_name = "res_" + img.replace("jpg", "txt")
|
||||||
|
output_file = os.path.join(output_dir, file_name)
|
||||||
|
f = open(output_file, 'w')
|
||||||
|
for bbox in bboxes:
|
||||||
|
x1 = bbox[0] / image_w
|
||||||
|
y1 = bbox[1] / image_h
|
||||||
|
x2 = bbox[2] / image_w
|
||||||
|
y2 = bbox[3] / image_h
|
||||||
|
draw.line([(x1, y1), (x1, y2), (x2, y2), (x2, y1), (x1, y1)], fill='red', width=2)
|
||||||
|
str_tmp = str(int(x1)) + "," + str(int(y1)) + "," + str(int(x2)) + "," + str(int(y2))
|
||||||
|
f.write(str_tmp)
|
||||||
|
f.write("\n")
|
||||||
|
f.close()
|
||||||
|
im.save(img)
|
||||||
|
eval_iter = eval_iter + 1
|
Loading…
Reference in new issue