Spaces:
Runtime error
Runtime error
File size: 24,031 Bytes
c310e19 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import datetime
import logging
import os
import pickle
import subprocess
import time
import cv2
import numpy as np
import torch
from maskrcnn_benchmark.utils.chars import char2num, get_tight_rect, getstr_grid
from PIL import Image, ImageDraw
from tqdm import tqdm
from ..utils.comm import is_main_process, scatter_gather, synchronize
import pdb
# TO DO: format output with dictionnary
def compute_on_dataset(model, data_loader, device, cfg):
model.eval()
results_dict = {}
seg_results = []
cpu_device = torch.device("cpu")
total_time = 0
for _, batch in tqdm(enumerate(data_loader)):
images, targets, image_paths = batch
images = images.to(device)
with torch.no_grad():
if cfg.MODEL.SEG_ON:
predictions, proposals, seg_results_dict = model(
images
)
seg_results.append(
[image_paths, proposals, seg_results_dict['rotated_boxes'], seg_results_dict['polygons'], seg_results_dict['preds'], seg_results_dict['scores']]
)
# if cfg.MODEL.MASK_ON and predictions is not None:
if predictions is not None:
if cfg.MODEL.CHAR_MASK_ON or cfg.SEQUENCE.SEQ_ON:
global_predictions = predictions[0]
char_predictions = predictions[1]
char_mask = char_predictions["char_mask"]
boxes = char_predictions["boxes"]
seq_words = char_predictions["seq_outputs"]
seq_scores = char_predictions["seq_scores"]
detailed_seq_scores = char_predictions["detailed_seq_scores"]
global_predictions = [o.to(cpu_device) for o in global_predictions]
results_dict.update(
{
image_paths[0]: [
global_predictions[0],
char_mask,
boxes,
seq_words,
seq_scores,
detailed_seq_scores,
]
}
)
else:
global_predictions = [o.to(cpu_device) for o in predictions]
results_dict.update(
{
image_paths[0]: [
global_predictions[0],
]
}
)
else:
predictions = model(images)
if predictions is not None:
if not (cfg.MODEL.CHAR_MASK_ON and cfg.SEQUENCE.SEQ_ON):
global_predictions = predictions
global_predictions = [o.to(cpu_device) for o in global_predictions]
results_dict.update(
{
image_paths[0]: [
global_predictions[0],
]
}
)
else:
global_predictions = predictions[0]
char_predictions = predictions[1]
if cfg.MODEL.CHAR_MASK_ON:
char_mask = char_predictions["char_mask"]
else:
char_mask = None
boxes = char_predictions["boxes"]
seq_words = char_predictions["seq_outputs"]
seq_scores = char_predictions["seq_scores"]
detailed_seq_scores = char_predictions["detailed_seq_scores"]
global_predictions = [o.to(cpu_device) for o in global_predictions]
results_dict.update(
{
image_paths[0]: [
global_predictions[0],
char_mask,
boxes,
seq_words,
seq_scores,
detailed_seq_scores,
]
}
)
return results_dict, seg_results
def polygon2rbox(polygon, image_height, image_width):
poly = np.array(polygon).reshape((-1, 2))
rect = cv2.minAreaRect(poly)
corners = cv2.boxPoints(rect)
corners = np.array(corners, dtype="int")
pts = get_tight_rect(corners, 0, 0, image_height, image_width, 1)
pts = list(map(int, pts))
return pts
def mask2polygon(mask, box, im_size, threshold=0.5, output_folder=None):
# mask 32*128
image_width, image_height = im_size[0], im_size[1]
box_h = box[3] - box[1]
box_w = box[2] - box[0]
cls_polys = (mask * 255).astype(np.uint8)
poly_map = np.array(Image.fromarray(cls_polys).resize((box_w, box_h)))
poly_map = poly_map.astype(np.float32) / 255
poly_map = cv2.GaussianBlur(poly_map, (3, 3), sigmaX=3)
ret, poly_map = cv2.threshold(poly_map, threshold, 1, cv2.THRESH_BINARY)
if "total_text" in output_folder or "cute80" in output_folder:
SE1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
poly_map = cv2.erode(poly_map, SE1)
poly_map = cv2.dilate(poly_map, SE1)
poly_map = cv2.morphologyEx(poly_map, cv2.MORPH_CLOSE, SE1)
try:
_, contours, _ = cv2.findContours(
(poly_map * 255).astype(np.uint8), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE
)
except:
contours, _ = cv2.findContours(
(poly_map * 255).astype(np.uint8), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE
)
if len(contours) == 0:
# print(contours)
# print(len(contours))
return None
max_area = 0
max_cnt = contours[0]
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
max_cnt = cnt
# perimeter = cv2.arcLength(max_cnt, True)
epsilon = 0.01 * cv2.arcLength(max_cnt, True)
approx = cv2.approxPolyDP(max_cnt, epsilon, True)
pts = approx.reshape((-1, 2))
pts[:, 0] = pts[:, 0] + box[0]
pts[:, 1] = pts[:, 1] + box[1]
polygon = list(pts.reshape((-1,)))
polygon = list(map(int, polygon))
if len(polygon) < 6:
return None
else:
SE1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
poly_map = cv2.erode(poly_map, SE1)
poly_map = cv2.dilate(poly_map, SE1)
poly_map = cv2.morphologyEx(poly_map, cv2.MORPH_CLOSE, SE1)
idy, idx = np.where(poly_map == 1)
xy = np.vstack((idx, idy))
xy = np.transpose(xy)
hull = cv2.convexHull(xy, clockwise=True)
# reverse order of points.
if hull is None:
return None
hull = hull[::-1]
# find minimum area bounding box.
rect = cv2.minAreaRect(hull)
corners = cv2.boxPoints(rect)
corners = np.array(corners, dtype="int")
pts = get_tight_rect(corners, box[0], box[1], image_height, image_width, 1)
polygon = [x * 1.0 for x in pts]
polygon = list(map(int, polygon))
return polygon
def _accumulate_predictions_from_multiple_gpus(predictions_per_gpu):
all_predictions = scatter_gather(predictions_per_gpu)
if not is_main_process():
return
# merge the list of dicts
predictions = {}
for p in all_predictions:
predictions.update(p)
return predictions
def format_output(out_dir, boxes, img_name):
with open(
os.path.join(out_dir, "res_" + img_name.split(".")[0] + ".txt"), "wt"
) as res:
## char score save dir
ssur_name = os.path.join(out_dir, "res_" + img_name.split(".")[0])
for i, box in enumerate(boxes):
save_name = ssur_name + "_" + str(i) + ".pkl"
save_dict = {}
if "total_text" in out_dir or "cute80" in out_dir:
# np.save(save_name, box[-2])
save_dict["seg_char_scores"] = box[-3]
save_dict["seq_char_scores"] = box[-2]
box = (
",".join([str(x) for x in box[:4]])
+ ";"
+ ",".join([str(x) for x in box[4 : 4 + int(box[-1])]])
+ ";"
+ ",".join([str(x) for x in box[4 + int(box[-1]) : -3]])
+ ","
+ save_name
)
else:
save_dict["seg_char_scores"] = box[-2]
save_dict["seq_char_scores"] = box[-1]
np.save(save_name, box[-1])
box = ",".join([str(x) for x in box[:-2]]) + "," + save_name
with open(save_name, "wb") as f:
pickle.dump(save_dict, f, protocol=2)
res.write(box + "\n")
def format_seg_output(results_dir, rotated_boxes_this_image, polygons_this_image, scores, img_name, ratio):
height_ratio, width_ratio = ratio
with open(
os.path.join(results_dir, "res_" + img_name.split(".")[0] + ".txt"), "wt"
) as res:
if "total_text" in results_dir or "cute80" in results_dir:
for i, box in enumerate(polygons_this_image):
box = box[0]
box[0::2] = box[0::2] * width_ratio
box[1::2] = box[1::2] * height_ratio
save_dict = {}
# result = ",".join([str(int(x[0])) + ',' +str(int(x[1])) for x in box])
result = ",".join([str(int(x)) for x in box])
score = scores[i].item()
res.write(result + ',' + str(score) + "\n")
else:
for i, box in enumerate(rotated_boxes_this_image):
box[0::2] = box[0::2] * width_ratio
box[1::2] = box[1::2] * height_ratio
save_dict = {}
result = ",".join([str(int(x[0])) + ',' +str(int(x[1])) for x in box])
score = scores[i].item()
res.write(result + ',' + str(score) + "\n")
def process_char_mask(char_masks, boxes, threshold=192):
texts, rec_scores, rec_char_scores, char_polygons = [], [], [], []
for index in range(char_masks.shape[0]):
box = list(boxes[index])
box = list(map(int, box))
text, rec_score, rec_char_score, char_polygon = getstr_grid(
char_masks[index, :, :, :].copy(), box, threshold=threshold
)
texts.append(text)
rec_scores.append(rec_score)
rec_char_scores.append(rec_char_score)
char_polygons.append(char_polygon)
# segmss.append(segms)
return texts, rec_scores, rec_char_scores, char_polygons
def creat_color_map(n_class, width):
splits = int(np.ceil(np.power((n_class * 1.0), 1.0 / 3)))
maps = []
for i in range(splits):
r = int(i * width * 1.0 / (splits - 1))
for j in range(splits):
g = int(j * width * 1.0 / (splits - 1))
for k in range(splits - 1):
b = int(k * width * 1.0 / (splits - 1))
maps.append((r, g, b, 200))
return maps
def visualization(image, polygons, resize_ratio, colors, char_polygons=None, words=None):
draw = ImageDraw.Draw(image, "RGBA")
for polygon in polygons:
# draw.polygon(polygon, fill=None, outline=(0, 255, 0, 255))
# print(polygon)
polygon.append(polygon[0])
polygon.append(polygon[1])
# print(polygon)
color = '#33FF33'
draw.line(polygon, fill=color, width=5)
# if char_polygons is not None:
# for i, char_polygon in enumerate(char_polygons):
# for j, polygon in enumerate(char_polygon):
# polygon = [int(x * resize_ratio) for x in polygon]
# char = words[i][j]
# color = colors[char2num(char)]
# draw.polygon(polygon, fill=color, outline=color)
def vis_seg_map(image_path, seg_map, rotated_boxes, polygons_this_image, proposals, vis_dir):
img_name = image_path.split("/")[-1]
image = cv2.imread(image_path)
height, width, _ = image.shape
seg_map = seg_map.data.cpu().numpy()
img = Image.fromarray(image).convert("RGB")
# height_ratio = height / seg_map.shape[1]
# width_ratio = width / seg_map.shape[2]
# print('seg_map.shape:', seg_map.shape)
# print('image.shape:', image.shape)
seg_image = (
Image.fromarray((seg_map[0, :proposals.size[1], :proposals.size[0]] * 255).astype(np.uint8))
.convert("RGB")
.resize((width, height))
)
visu_image = Image.blend(seg_image, img, 0.5)
img_draw = ImageDraw.Draw(visu_image)
if "total_text" in vis_dir or "cute80" in vis_dir:
for box in polygons_this_image:
# box[:, 0] = box[:, 0]
# box[:, 1] = box[:, 1]
tuple_box = [tuple(x) for x in box[0].reshape(-1, 2).tolist()]
tuple_box.append(tuple_box[0])
img_draw.line(tuple_box, fill=(0, 255, 0), width=5)
else:
for box in rotated_boxes:
# box[:, 0] = box[:, 0]
# box[:, 1] = box[:, 1]
tuple_box = [tuple(x) for x in box.tolist()]
tuple_box.append(tuple_box[0])
img_draw.line(tuple_box, fill=(0, 255, 0), width=5)
visu_image.save(vis_dir + "/seg_" + img_name)
def prepare_results_for_evaluation(
predictions, output_folder, model_name, seg_predictions=None, vis=False, cfg=None
):
results_dir = os.path.join(output_folder, model_name + "_results")
if not os.path.isdir(results_dir):
os.mkdir(results_dir)
seg_results_dir = os.path.join(output_folder, model_name + "_seg_results")
if not os.path.isdir(seg_results_dir):
os.mkdir(seg_results_dir)
if vis:
visu_dir = os.path.join(output_folder, model_name + "_visu")
if not os.path.isdir(visu_dir):
os.mkdir(visu_dir)
seg_visu_dir = os.path.join(output_folder, model_name + "_seg_visu")
if not os.path.isdir(seg_visu_dir):
os.mkdir(seg_visu_dir)
if len(seg_predictions) > 0:
for seg_prediction in seg_predictions:
image_paths, proposals, rotated_boxes, polygons, seg_maps, seg_scores = (
seg_prediction[0],
seg_prediction[1],
seg_prediction[2],
seg_prediction[3],
seg_prediction[4],
seg_prediction[5],
)
for batch_id in range(len(image_paths)):
image_path = image_paths[batch_id]
im_name = image_path.split("/")[-1]
image = cv2.imread(image_path)
height, width, _ = image.shape
rotated_boxes_this_image = rotated_boxes[batch_id]
polygons_this_image = polygons[batch_id]
proposals_this_image = proposals[batch_id]
seg_map = seg_maps[batch_id]
seg_score = seg_scores[batch_id]
height, width, _ = image.shape
height_ratio = height / proposals_this_image.size[1]
width_ratio = width / proposals_this_image.size[0]
format_seg_output(seg_results_dir, rotated_boxes_this_image, polygons_this_image, seg_score, im_name, (height_ratio, width_ratio))
if vis:
vis_seg_map(image_path, seg_map, rotated_boxes_this_image, polygons_this_image, proposals_this_image, seg_visu_dir)
if (not cfg.MODEL.TRAIN_DETECTION_ONLY):
for image_path, prediction in predictions.items():
im_name = image_path.split("/")[-1]
if cfg.MODEL.CHAR_MASK_ON or cfg.SEQUENCE.SEQ_ON:
global_prediction, char_mask, boxes_char, seq_words, seq_scores, detailed_seq_scores = (
prediction[0],
prediction[1],
prediction[2],
prediction[3],
prediction[4],
prediction[5],
)
if char_mask is not None:
words, rec_scores, rec_char_scoress, char_polygons = process_char_mask(
char_mask, boxes_char
)
else:
global_prediction = prediction[0]
test_image_width, test_image_height = global_prediction.size
img = Image.open(image_path)
width, height = img.size
resize_ratio = float(height) / test_image_height
global_prediction = global_prediction.resize((width, height))
boxes = global_prediction.bbox.tolist()
if cfg.MODEL.ROI_BOX_HEAD.INFERENCE_USE_BOX:
scores = global_prediction.get_field("scores").tolist()
if not cfg.MODEL.SEG.USE_SEG_POLY:
masks = global_prediction.get_field("mask").cpu().numpy()
else:
masks = global_prediction.get_field("masks").get_polygons()
result_logs = []
polygons = []
for k, box in enumerate(boxes):
if box[2] - box[0] < 1 or box[3] - box[1] < 1:
continue
box = list(map(int, box))
if not cfg.MODEL.SEG.USE_SEG_POLY:
mask = masks[k, 0, :, :]
polygon = mask2polygon(
mask, box, img.size, threshold=0.5, output_folder=output_folder
)
else:
polygon = list(masks[k].get_polygons()[0].cpu().numpy())
if not ("total_text" in output_folder or "cute80" in output_folder):
polygon = polygon2rbox(polygon, height, width)
if polygon is None:
polygon = [
box[0],
box[1],
box[2],
box[1],
box[2],
box[3],
box[0],
box[3],
]
continue
polygons.append(polygon)
if cfg.MODEL.ROI_BOX_HEAD.INFERENCE_USE_BOX:
score = scores[k]
else:
score = 1.0
if cfg.MODEL.CHAR_MASK_ON or cfg.SEQUENCE.SEQ_ON:
if char_mask is None:
word = 'aaa'
rec_score = 1.0
char_score = None
else:
word = words[k]
rec_score = rec_scores[k]
char_score = rec_char_scoress[k]
seq_word = seq_words[k]
seq_char_scores = seq_scores[k]
seq_score = sum(seq_char_scores) / float(len(seq_char_scores))
detailed_seq_score = detailed_seq_scores[k]
detailed_seq_score = np.squeeze(np.array(detailed_seq_score), axis=1)
else:
word = 'aaa'
rec_score = 1.0
char_score = [1.0, 1.0, 1.0]
seq_word = 'aaa'
seq_char_scores = [1.0, 1.0, 1.0]
seq_score = 1.0
detailed_seq_score = None
if "total_text" in output_folder or "cute80" in output_folder:
result_log = (
[int(x * 1.0) for x in box[:4]]
+ polygon
+ [word]
+ [seq_word]
+ [score]
+ [rec_score]
+ [seq_score]
+ [char_score]
+ [detailed_seq_score]
+ [len(polygon)]
)
else:
result_log = (
[int(x * 1.0) for x in box[:4]]
+ polygon
+ [word]
+ [seq_word]
+ [score]
+ [rec_score]
+ [seq_score]
+ [char_score]
+ [detailed_seq_score]
)
result_logs.append(result_log)
if vis:
colors = creat_color_map(37, 255)
if cfg.MODEL.CHAR_MASK_ON:
visualization(img, polygons, resize_ratio, colors, char_polygons, words)
else:
visualization(img, polygons, resize_ratio, colors)
img.save(os.path.join(visu_dir, im_name))
format_output(results_dir, result_logs, im_name)
def inference(
model,
data_loader,
iou_types=("bbox",),
box_only=False,
device="cuda",
expected_results=(),
expected_results_sigma_tol=4,
output_folder=None,
model_name=None,
cfg=None,
):
# convert to a torch.device for efficiency
model_name = model_name.split(".")[0] + "_" + str(cfg.INPUT.MIN_SIZE_TEST)
predictions_path = os.path.join(output_folder, model_name + "_predictions.pth")
seg_predictions_path = os.path.join(
output_folder, model_name + "_seg_predictions.pth"
)
# if os.path.isfile(predictions_path) and os.path.isfile(seg_predictions_path):
if False:
predictions = torch.load(predictions_path)
seg_predictions = torch.load(seg_predictions_path)
else:
device = torch.device(device)
num_devices = (
torch.distributed.get_world_size()
if torch.distributed.is_initialized()
else 1
)
logger = logging.getLogger("maskrcnn_benchmark.inference")
dataset = data_loader.dataset
logger.info("Start evaluation on {} images".format(len(dataset)))
start_time = time.time()
predictions, seg_predictions = compute_on_dataset(
model, data_loader, device, cfg
)
# wait for all processes to complete before measuring the time
synchronize()
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=total_time))
logger.info(
"Total inference time: {} ({} s / img per device, on {} devices)".format(
total_time_str, total_time * num_devices / len(dataset), num_devices
)
)
# predictions = _accumulate_predictions_from_multiple_gpus(predictions)
# if not is_main_process():
# return
if output_folder:
torch.save(predictions, predictions_path)
torch.save(seg_predictions, seg_predictions_path)
prepare_results_for_evaluation(
predictions,
output_folder,
model_name,
seg_predictions=seg_predictions,
vis=cfg.TEST.VIS,
cfg=cfg
)
|