forked from mindspore-Ecosystem/mindspore
remove dependency of mmcv
This commit is contained in:
parent
f3769b1b58
commit
35a7af4a4d
|
@ -17,7 +17,7 @@
|
|||
from __future__ import division
|
||||
import numpy as np
|
||||
from numpy import random
|
||||
import mmcv
|
||||
import cv2
|
||||
import mindspore.dataset as de
|
||||
import mindspore.dataset.vision.c_transforms as C
|
||||
import mindspore.dataset.transforms.c_transforms as CC
|
||||
|
@ -50,7 +50,7 @@ class PhotoMetricDistortion:
|
|||
self.contrast_upper)
|
||||
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):
|
||||
img[..., 1] *= random.uniform(self.saturation_lower,
|
||||
|
@ -61,7 +61,7 @@ class PhotoMetricDistortion:
|
|||
img[..., 0][img[..., 0] > 360] -= 360
|
||||
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:
|
||||
if random.randint(2):
|
||||
|
@ -97,11 +97,19 @@ class Expand:
|
|||
boxes += np.tile((left, top), 2)
|
||||
return img, boxes, labels
|
||||
|
||||
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_column(img, gt_bboxes, gt_label, gt_num, img_shape):
|
||||
"""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_width), return_scale=True)
|
||||
img_data, scale_factor2 = rescale_with_tuple(img_data, (config.img_height, config.img_width))
|
||||
scale_factor = scale_factor * scale_factor2
|
||||
img_shape = np.append(img_shape, scale_factor)
|
||||
img_shape = np.asarray(img_shape, dtype=np.float32)
|
||||
|
@ -117,8 +125,12 @@ def rescale_column(img, gt_bboxes, gt_label, gt_num, img_shape):
|
|||
def resize_column(img, gt_bboxes, gt_label, gt_num, img_shape):
|
||||
"""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)
|
||||
|
@ -134,8 +146,12 @@ def resize_column(img, gt_bboxes, gt_label, gt_num, img_shape):
|
|||
def resize_column_test(img, gt_bboxes, gt_label, gt_num, img_shape):
|
||||
"""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
|
||||
|
||||
scale_factor = np.array(
|
||||
[w_scale, h_scale, w_scale, h_scale], dtype=np.float32)
|
||||
img_shape = (config.img_height, config.img_width)
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
import os
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import mmcv
|
||||
import cv2
|
||||
|
||||
from src.utils import metrics
|
||||
from model_utils.config import config
|
||||
|
@ -37,8 +37,8 @@ def get_pred(file, result_path):
|
|||
|
||||
def get_gt_bboxes_labels(label_file, img_file):
|
||||
img_data = np.array(Image.open(img_file))
|
||||
img_data, w_scale, h_scale = mmcv.imresize(
|
||||
img_data, (config.img_width, config.img_height), return_scale=True)
|
||||
img_data, w_scale, h_scale = cv2.resize(
|
||||
img_data, (config.img_width, config.img_height), interpolation=cv2.INTER_LINEAR)
|
||||
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)
|
||||
|
|
|
@ -20,7 +20,7 @@ import os
|
|||
import numpy as np
|
||||
from numpy import random
|
||||
|
||||
import mmcv
|
||||
import cv2
|
||||
import mindspore.dataset as de
|
||||
import mindspore.dataset.vision.c_transforms as C
|
||||
import mindspore.dataset.transforms.c_transforms as CC
|
||||
|
@ -107,7 +107,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):
|
||||
|
@ -121,7 +121,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:
|
||||
|
@ -166,8 +166,8 @@ class Expand:
|
|||
def resize_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
||||
"""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, w_scale, h_scale = cv2.resize(
|
||||
img_data, (config.img_width, config.img_height), interpolation=cv2.INTER_LINEAR)
|
||||
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)
|
||||
|
@ -184,8 +184,8 @@ def resize_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
|||
def resize_column_test(img, img_shape, gt_bboxes, gt_label, gt_num):
|
||||
"""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, w_scale, h_scale = cv2.resize(
|
||||
img_data, (config.img_width, config.img_height), interpolation=cv2.INTER_LINEAR)
|
||||
scale_factor = np.array(
|
||||
[w_scale, h_scale, w_scale, h_scale], dtype=np.float32)
|
||||
img_shape = (config.img_height, config.img_width)
|
||||
|
@ -202,14 +202,23 @@ def resize_column_test(img, img_shape, gt_bboxes, gt_label, gt_num):
|
|||
|
||||
def impad_to_multiple_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
||||
"""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)
|
||||
|
||||
|
||||
def imnormalize_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
||||
"""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)
|
||||
|
||||
|
@ -217,7 +226,7 @@ def imnormalize_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
|||
def flip_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
||||
"""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
|
||||
|
||||
|
|
|
@ -21,7 +21,6 @@ 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
|
||||
|
@ -103,7 +102,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):
|
||||
|
@ -117,7 +116,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:
|
||||
|
@ -157,12 +156,24 @@ class Expand:
|
|||
boxes += np.tile((left, top), 2)
|
||||
return img, boxes, labels
|
||||
|
||||
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, config):
|
||||
"""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
|
||||
|
@ -183,9 +194,9 @@ def rescale_column(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
|||
|
||||
def rescale_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
||||
"""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]
|
||||
|
@ -204,8 +215,12 @@ def rescale_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
|||
def resize_column(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
||||
"""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)
|
||||
|
@ -222,8 +237,12 @@ def resize_column(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
|||
def resize_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
||||
"""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
|
||||
|
||||
scale_factor = np.array(
|
||||
[w_scale, h_scale, w_scale, h_scale], dtype=np.float32)
|
||||
img_shape = np.append(img_shape, (h_scale, w_scale))
|
||||
|
@ -239,14 +258,23 @@ def resize_column_test(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
|||
|
||||
def impad_to_multiple_column(img, img_shape, gt_bboxes, gt_label, gt_num, config):
|
||||
"""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)
|
||||
|
||||
|
||||
def imnormalize_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
||||
"""imnormalize operation for image"""
|
||||
img_data = mmcv.imnormalize(img, np.array([123.675, 116.28, 103.53]), np.array([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)
|
||||
|
||||
|
@ -254,7 +282,7 @@ def imnormalize_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
|||
def flip_column(img, img_shape, gt_bboxes, gt_label, gt_num):
|
||||
"""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
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
"""coco eval for fasterrcnn"""
|
||||
import json
|
||||
import numpy as np
|
||||
import mmcv
|
||||
from pycocotools.coco import COCO
|
||||
from pycocotools.cocoeval import COCOeval
|
||||
|
||||
|
@ -42,7 +41,7 @@ def coco_eval(result_files, result_types, coco, max_dets=(100, 300, 1000), singl
|
|||
if not anns:
|
||||
return summary_init
|
||||
|
||||
if mmcv.is_str(coco):
|
||||
if isinstance(coco, str):
|
||||
coco = COCO(coco)
|
||||
assert isinstance(coco, COCO)
|
||||
|
||||
|
@ -208,18 +207,22 @@ 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['proposal'] = '{}.{}.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
|
||||
|
|
|
@ -18,7 +18,6 @@ from __future__ import division
|
|||
|
||||
import os
|
||||
import re
|
||||
import mmcv
|
||||
import cv2
|
||||
import numpy as np
|
||||
from numpy import random
|
||||
|
@ -117,7 +116,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):
|
||||
|
@ -131,7 +130,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:
|
||||
|
@ -176,11 +175,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
|
||||
|
@ -188,7 +200,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
|
||||
])
|
||||
|
||||
|
@ -210,9 +222,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]
|
||||
|
@ -230,8 +242,12 @@ 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)
|
||||
|
@ -242,7 +258,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)
|
||||
|
@ -250,8 +266,12 @@ 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, w_scale, h_scale = 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)
|
||||
|
||||
|
@ -259,20 +279,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, np.array([123.675, 116.28, 103.53]), np.array([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
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
# ============================================================================
|
||||
"""coco eval for maskrcnn_mobilenetv1"""
|
||||
import json
|
||||
import mmcv
|
||||
import cv2
|
||||
import numpy as np
|
||||
from pycocotools.coco import COCO
|
||||
from pycocotools.cocoeval import COCOeval
|
||||
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue