!23188 Remove mmcv lib

Merge pull request !23188 from zhuyuxiao/master
This commit is contained in:
i-robot 2021-09-10 07:44:19 +00:00 committed by Gitee
commit 9d5bbe73e4
3 changed files with 65 additions and 30 deletions

View File

@ -14,8 +14,8 @@
# ============================================================================
"""coco eval for maskrcnn"""
import json
import cv2
import numpy as np
import mmcv
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from pycocotools import mask as maskUtils
@ -43,7 +43,7 @@ def coco_eval(result_files, result_types, coco, max_dets=(100, 300, 1000), singl
anns = json.load(open(result_files['bbox']))
if not anns:
return summary_init
if mmcv.is_str(coco):
if isinstance(coco, str):
coco = COCO(coco)
assert isinstance(coco, COCO)
@ -216,17 +216,21 @@ def results2json(dataset, results, out_file):
json_results = det2json(dataset, results)
result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox')
result_files['proposal'] = '{}.{}.json'.format(out_file, 'bbox')
mmcv.dump(json_results, result_files['bbox'])
with open(result_files['bbox'], 'w') as fp:
json.dump(json_results, fp)
elif isinstance(results[0], tuple):
json_results = segm2json(dataset, results)
result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox')
result_files['segm'] = '{}.{}.json'.format(out_file, 'segm')
mmcv.dump(json_results[0], result_files['bbox'])
mmcv.dump(json_results[1], result_files['segm'])
with open(result_files['bbox'], 'w') as fp:
json.dump(json_results[0], fp)
with open(result_files['segm'], 'w') as fp:
json.dump(json_results[1], fp)
elif isinstance(results[0], np.ndarray):
json_results = proposal2json(dataset, results)
result_files['proposal'] = '{}.{}.json'.format(out_file, 'proposal')
mmcv.dump(json_results, result_files['proposal'])
with open(result_files['proposal'], 'w') as fp:
json.dump(json_results, fp)
else:
raise TypeError('invalid type of results')
return result_files
@ -261,7 +265,7 @@ def get_seg_masks(mask_pred, det_bboxes, det_labels, img_meta, rescale, num_clas
h = max(h, 1)
mask_pred_ = mask_pred[i, :, :]
im_mask = np.zeros((img_h, img_w), dtype=np.uint8)
bbox_mask = mmcv.imresize(mask_pred_, (w, h))
bbox_mask = cv2.resize(mask_pred_, (w, h), interpolation=cv2.INTER_LINEAR)
mask_thr_binary = 0.5
bbox_mask = (bbox_mask > mask_thr_binary).astype(np.uint8)
im_mask[bbox[1]:bbox[1] + h, bbox[0]:bbox[0] + w] = bbox_mask

View File

@ -20,7 +20,6 @@ import os
import numpy as np
from numpy import random
import cv2
import mmcv
import mindspore.dataset as de
import mindspore.dataset.vision.c_transforms as C
from mindspore.mindrecord import FileWriter
@ -104,7 +103,7 @@ class PhotoMetricDistortion:
img *= alpha
# convert color from BGR to HSV
img = mmcv.bgr2hsv(img)
img = cv2.cvtColor((img, getattr(cv2, f'COLOR_BGR2HSV')))
# random saturation
if random.randint(2):
@ -118,7 +117,7 @@ class PhotoMetricDistortion:
img[..., 0][img[..., 0] < 0] += 360
# convert color from HSV to BGR
img = mmcv.hsv2bgr(img)
img = cv2.cvtColor((img, getattr(cv2, f'COLOR_HSV2BGR')))
# random contrast
if mode == 0:
@ -163,11 +162,24 @@ class Expand:
return img, boxes, labels, mask
def rescale_with_tuple(img, scale):
h, w = img.shape[:2]
scale_factor = min(max(scale) / max(h, w), min(scale) / min(h, w))
new_size = int(w * float(scale_factor) + 0.5), int(h * float(scale_factor) + 0.5)
rescaled_img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR)
return rescaled_img, scale_factor
def rescale_with_factor(img, scale_factor):
h, w = img.shape[:2]
new_size = int(w * float(scale_factor) + 0.5), int(h * float(scale_factor) + 0.5)
return cv2.resize(img, new_size, interpolation=cv2.INTER_NEAREST)
def rescale_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
"""rescale operation for image"""
img_data, scale_factor = mmcv.imrescale(img, (config.img_width, config.img_height), return_scale=True)
img_data, scale_factor = rescale_with_tuple(img, (config.img_width, config.img_height))
if img_data.shape[0] > config.img_height:
img_data, scale_factor2 = mmcv.imrescale(img_data, (config.img_height, config.img_height), return_scale=True)
img_data, scale_factor2 = rescale_with_tuple(img_data, (config.img_height, config.img_height))
scale_factor = scale_factor*scale_factor2
gt_bboxes = gt_bboxes * scale_factor
@ -175,7 +187,7 @@ def rescale_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
gt_bboxes[:, 1::2] = np.clip(gt_bboxes[:, 1::2], 0, img_data.shape[0] - 1)
gt_mask_data = np.array([
mmcv.imrescale(mask, scale_factor, interpolation='nearest')
rescale_with_factor(mask, scale_factor)
for mask in gt_mask
])
@ -197,9 +209,9 @@ def rescale_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
def rescale_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
"""rescale operation for image of eval"""
img_data, scale_factor = mmcv.imrescale(img, (config.img_width, config.img_height), return_scale=True)
img_data, scale_factor = rescale_with_tuple(img, (config.img_width, config.img_height))
if img_data.shape[0] > config.img_height:
img_data, scale_factor2 = mmcv.imrescale(img_data, (config.img_height, config.img_height), return_scale=True)
img_data, scale_factor2 = rescale_with_tuple(img_data, (config.img_height, config.img_height))
scale_factor = scale_factor*scale_factor2
pad_h = config.img_height - img_data.shape[0]
@ -217,8 +229,11 @@ def rescale_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
def resize_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
"""resize operation for image"""
img_data = img
img_data, w_scale, h_scale = mmcv.imresize(
img_data, (config.img_width, config.img_height), return_scale=True)
img_data = cv2.resize(img_data, (config.img_width, config.img_height), interpolation=cv2.INTER_LINEAR)
h, w = img_data.shape[:2]
h_scale = config.img_height / h
w_scale = config.img_width / w
scale_factor = np.array(
[w_scale, h_scale, w_scale, h_scale], dtype=np.float32)
img_shape = (config.img_height, config.img_width, 1.0)
@ -229,7 +244,7 @@ def resize_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
gt_bboxes[:, 1::2] = np.clip(gt_bboxes[:, 1::2], 0, img_shape[0] - 1) # y1, y2 [0, H-1]
gt_mask_data = np.array([
mmcv.imresize(mask, (config.img_width, config.img_height), interpolation='nearest')
cv2.resize(mask, (config.img_width, config.img_height), interpolation=cv2.INTER_NEAREST)
for mask in gt_mask
])
return (img_data, img_shape, gt_bboxes, gt_label, gt_num, gt_mask_data)
@ -237,8 +252,11 @@ def resize_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
def resize_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
"""resize operation for image of eval"""
img_data = img
img_data, w_scale, h_scale = mmcv.imresize(
img_data, (config.img_width, config.img_height), return_scale=True)
img_data = cv2.resize(img_data, (config.img_width, config.img_height), interpolation=cv2.INTER_LINEAR)
h, w = img_data.shape[:2]
h_scale = config.img_height / h
w_scale = config.img_width / w
img_shape = np.append(img_shape, (h_scale, w_scale))
img_shape = np.asarray(img_shape, dtype=np.float32)
@ -246,20 +264,29 @@ def resize_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
def impad_to_multiple_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
"""impad operation for image"""
img_data = mmcv.impad(img, (config.img_height, config.img_width))
img_data = cv2.copyMakeBorder(img,
0, config.img_height - img.shape[0], 0, config.img_width - img.shape[1],
cv2.BORDER_CONSTANT,
value=0)
img_data = img_data.astype(np.float32)
return (img_data, img_shape, gt_bboxes, gt_label, gt_num, gt_mask)
def imnormalize_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
"""imnormalize operation for image"""
img_data = mmcv.imnormalize(img, [123.675, 116.28, 103.53], [58.395, 57.12, 57.375], True)
mean = np.asarray([123.675, 116.28, 103.53])
std = np.asarray([58.395, 57.12, 57.375])
img_data = img.copy().astype(np.float32)
cv2.cvtColor(img_data, cv2.COLOR_BGR2RGB, img_data) # inplace
cv2.subtract(img_data, np.float64(mean.reshape(1, -1)), img_data) # inplace
cv2.multiply(img_data, 1 / np.float64(std.reshape(1, -1)), img_data) # inplace
img_data = img_data.astype(np.float32)
return (img_data, img_shape, gt_bboxes, gt_label, gt_num, gt_mask)
def flip_column(img, img_shape, gt_bboxes, gt_label, gt_num, gt_mask):
"""flip operation for image"""
img_data = img
img_data = mmcv.imflip(img_data)
img_data = np.flip(img_data, axis=1)
flipped = gt_bboxes.copy()
_, w, _ = img_data.shape

View File

@ -14,8 +14,8 @@
# ============================================================================
"""coco eval for maskrcnn"""
import json
import cv2
import numpy as np
import mmcv
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from pycocotools import mask as maskUtils
@ -44,7 +44,7 @@ def coco_eval(result_files, result_types, coco, max_dets=(100, 300, 1000), singl
anns = json.load(open(result_files['bbox']))
if not anns:
return summary_init
if mmcv.is_str(coco):
if isinstance(coco, str):
coco = COCO(coco)
assert isinstance(coco, COCO)
@ -216,17 +216,21 @@ def results2json(dataset, results, out_file):
json_results = det2json(dataset, results)
result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox')
result_files['proposal'] = '{}.{}.json'.format(out_file, 'bbox')
mmcv.dump(json_results, result_files['bbox'])
with open(result_files['bbox'], 'w') as fp:
json.dump(json_results, fp)
elif isinstance(results[0], tuple):
json_results = segm2json(dataset, results)
result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox')
result_files['segm'] = '{}.{}.json'.format(out_file, 'segm')
mmcv.dump(json_results[0], result_files['bbox'])
mmcv.dump(json_results[1], result_files['segm'])
with open(result_files['bbox'], 'w') as fp:
json.dump(json_results[0], fp)
with open(result_files['segm'], 'w') as fp:
json.dump(json_results[1], fp)
elif isinstance(results[0], np.ndarray):
json_results = proposal2json(dataset, results)
result_files['proposal'] = '{}.{}.json'.format(out_file, 'proposal')
mmcv.dump(json_results, result_files['proposal'])
with open(result_files['proposal'], 'w') as fp:
json.dump(json_results, fp)
else:
raise TypeError('invalid type of results')
return result_files
@ -261,7 +265,7 @@ def get_seg_masks(mask_pred, det_bboxes, det_labels, img_meta, rescale, num_clas
h = max(h, 1)
mask_pred_ = mask_pred[i, :, :]
im_mask = np.zeros((img_h, img_w), dtype=np.uint8)
bbox_mask = mmcv.imresize(mask_pred_, (w, h))
bbox_mask = cv2.resize(mask_pred_, (w, h), interpolation=cv2.INTER_LINEAR)
bbox_mask = (bbox_mask > config.mask_thr_binary).astype(np.uint8)
im_mask[bbox[1]:bbox[1] + h, bbox[0]:bbox[0] + w] = bbox_mask