init
This commit is contained in:
130
finetune/tools/analysis_tools/analyze_logs.py
Normal file
130
finetune/tools/analysis_tools/analyze_logs.py
Normal file
@@ -0,0 +1,130 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
"""Modified from https://github.com/open-
|
||||
mmlab/mmdetection/blob/master/tools/analysis_tools/analyze_logs.py."""
|
||||
import argparse
|
||||
import json
|
||||
from collections import defaultdict
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import seaborn as sns
|
||||
|
||||
|
||||
def plot_curve(log_dicts, args):
|
||||
if args.backend is not None:
|
||||
plt.switch_backend(args.backend)
|
||||
sns.set_style(args.style)
|
||||
# if legend is None, use {filename}_{key} as legend
|
||||
legend = args.legend
|
||||
if legend is None:
|
||||
legend = []
|
||||
for json_log in args.json_logs:
|
||||
for metric in args.keys:
|
||||
legend.append(f'{json_log}_{metric}')
|
||||
assert len(legend) == (len(args.json_logs) * len(args.keys))
|
||||
metrics = args.keys
|
||||
|
||||
num_metrics = len(metrics)
|
||||
for i, log_dict in enumerate(log_dicts):
|
||||
epochs = list(log_dict.keys())
|
||||
for j, metric in enumerate(metrics):
|
||||
print(f'plot curve of {args.json_logs[i]}, metric is {metric}')
|
||||
plot_epochs = []
|
||||
plot_iters = []
|
||||
plot_values = []
|
||||
# In some log files exist lines of validation,
|
||||
# `mode` list is used to only collect iter number
|
||||
# of training line.
|
||||
for epoch in epochs:
|
||||
epoch_logs = log_dict[epoch]
|
||||
if metric not in epoch_logs.keys():
|
||||
continue
|
||||
if metric in ['mIoU', 'mAcc', 'aAcc']:
|
||||
plot_epochs.append(epoch)
|
||||
plot_values.append(epoch_logs[metric][0])
|
||||
else:
|
||||
for idx in range(len(epoch_logs[metric])):
|
||||
plot_iters.append(epoch_logs['step'][idx])
|
||||
plot_values.append(epoch_logs[metric][idx])
|
||||
ax = plt.gca()
|
||||
label = legend[i * num_metrics + j]
|
||||
if metric in ['mIoU', 'mAcc', 'aAcc']:
|
||||
ax.set_xticks(plot_epochs)
|
||||
plt.xlabel('step')
|
||||
plt.plot(plot_epochs, plot_values, label=label, marker='o')
|
||||
else:
|
||||
plt.xlabel('iter')
|
||||
plt.plot(plot_iters, plot_values, label=label, linewidth=0.5)
|
||||
plt.legend()
|
||||
if args.title is not None:
|
||||
plt.title(args.title)
|
||||
if args.out is None:
|
||||
plt.show()
|
||||
else:
|
||||
print(f'save curve to: {args.out}')
|
||||
plt.savefig(args.out)
|
||||
plt.cla()
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Analyze Json Log')
|
||||
parser.add_argument(
|
||||
'json_logs',
|
||||
type=str,
|
||||
nargs='+',
|
||||
help='path of train log in json format')
|
||||
parser.add_argument(
|
||||
'--keys',
|
||||
type=str,
|
||||
nargs='+',
|
||||
default=['mIoU'],
|
||||
help='the metric that you want to plot')
|
||||
parser.add_argument('--title', type=str, help='title of figure')
|
||||
parser.add_argument(
|
||||
'--legend',
|
||||
type=str,
|
||||
nargs='+',
|
||||
default=None,
|
||||
help='legend of each plot')
|
||||
parser.add_argument(
|
||||
'--backend', type=str, default=None, help='backend of plt')
|
||||
parser.add_argument(
|
||||
'--style', type=str, default='dark', help='style of plt')
|
||||
parser.add_argument('--out', type=str, default=None)
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def load_json_logs(json_logs):
|
||||
# load and convert json_logs to log_dict, key is step, value is a sub dict
|
||||
# keys of sub dict is different metrics
|
||||
# value of sub dict is a list of corresponding values of all iterations
|
||||
log_dicts = [dict() for _ in json_logs]
|
||||
prev_step = 0
|
||||
for json_log, log_dict in zip(json_logs, log_dicts):
|
||||
with open(json_log) as log_file:
|
||||
for line in log_file:
|
||||
log = json.loads(line.strip())
|
||||
# the final step in json file is 0.
|
||||
if 'step' in log and log['step'] != 0:
|
||||
step = log['step']
|
||||
prev_step = step
|
||||
else:
|
||||
step = prev_step
|
||||
if step not in log_dict:
|
||||
log_dict[step] = defaultdict(list)
|
||||
for k, v in log.items():
|
||||
log_dict[step][k].append(v)
|
||||
return log_dicts
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
json_logs = args.json_logs
|
||||
for json_log in json_logs:
|
||||
assert json_log.endswith('.json')
|
||||
log_dicts = load_json_logs(json_logs)
|
||||
plot_curve(log_dicts, args)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
121
finetune/tools/analysis_tools/benchmark.py
Normal file
121
finetune/tools/analysis_tools/benchmark.py
Normal file
@@ -0,0 +1,121 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import argparse
|
||||
import os.path as osp
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from mmengine import Config
|
||||
from mmengine.fileio import dump
|
||||
from mmengine.model.utils import revert_sync_batchnorm
|
||||
from mmengine.registry import init_default_scope
|
||||
from mmengine.runner import Runner, load_checkpoint
|
||||
from mmengine.utils import mkdir_or_exist
|
||||
|
||||
from mmseg.registry import MODELS
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='MMSeg benchmark a model')
|
||||
parser.add_argument('config', help='test config file path')
|
||||
parser.add_argument('checkpoint', help='checkpoint file')
|
||||
parser.add_argument(
|
||||
'--log-interval', type=int, default=50, help='interval of logging')
|
||||
parser.add_argument(
|
||||
'--work-dir',
|
||||
help=('if specified, the results will be dumped '
|
||||
'into the directory as json'))
|
||||
parser.add_argument('--repeat-times', type=int, default=1)
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
cfg = Config.fromfile(args.config)
|
||||
|
||||
init_default_scope(cfg.get('default_scope', 'mmseg'))
|
||||
|
||||
timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime())
|
||||
if args.work_dir is not None:
|
||||
mkdir_or_exist(osp.abspath(args.work_dir))
|
||||
json_file = osp.join(args.work_dir, f'fps_{timestamp}.json')
|
||||
else:
|
||||
# use config filename as default work_dir if cfg.work_dir is None
|
||||
work_dir = osp.join('./work_dirs',
|
||||
osp.splitext(osp.basename(args.config))[0])
|
||||
mkdir_or_exist(osp.abspath(work_dir))
|
||||
json_file = osp.join(work_dir, f'fps_{timestamp}.json')
|
||||
|
||||
repeat_times = args.repeat_times
|
||||
# set cudnn_benchmark
|
||||
torch.backends.cudnn.benchmark = False
|
||||
cfg.model.pretrained = None
|
||||
|
||||
benchmark_dict = dict(config=args.config, unit='img / s')
|
||||
overall_fps_list = []
|
||||
cfg.test_dataloader.batch_size = 1
|
||||
for time_index in range(repeat_times):
|
||||
print(f'Run {time_index + 1}:')
|
||||
# build the dataloader
|
||||
data_loader = Runner.build_dataloader(cfg.test_dataloader)
|
||||
|
||||
# build the model and load checkpoint
|
||||
cfg.model.train_cfg = None
|
||||
model = MODELS.build(cfg.model)
|
||||
|
||||
if 'checkpoint' in args and osp.exists(args.checkpoint):
|
||||
load_checkpoint(model, args.checkpoint, map_location='cpu')
|
||||
|
||||
if torch.cuda.is_available():
|
||||
model = model.cuda()
|
||||
|
||||
model = revert_sync_batchnorm(model)
|
||||
|
||||
model.eval()
|
||||
|
||||
# the first several iterations may be very slow so skip them
|
||||
num_warmup = 5
|
||||
pure_inf_time = 0
|
||||
total_iters = 200
|
||||
|
||||
# benchmark with 200 batches and take the average
|
||||
for i, data in enumerate(data_loader):
|
||||
data = model.data_preprocessor(data, True)
|
||||
inputs = data['inputs']
|
||||
data_samples = data['data_samples']
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.synchronize()
|
||||
start_time = time.perf_counter()
|
||||
|
||||
with torch.no_grad():
|
||||
model(inputs, data_samples, mode='predict')
|
||||
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.synchronize()
|
||||
elapsed = time.perf_counter() - start_time
|
||||
|
||||
if i >= num_warmup:
|
||||
pure_inf_time += elapsed
|
||||
if (i + 1) % args.log_interval == 0:
|
||||
fps = (i + 1 - num_warmup) / pure_inf_time
|
||||
print(f'Done image [{i + 1:<3}/ {total_iters}], '
|
||||
f'fps: {fps:.2f} img / s')
|
||||
|
||||
if (i + 1) == total_iters:
|
||||
fps = (i + 1 - num_warmup) / pure_inf_time
|
||||
print(f'Overall fps: {fps:.2f} img / s\n')
|
||||
benchmark_dict[f'overall_fps_{time_index + 1}'] = round(fps, 2)
|
||||
overall_fps_list.append(fps)
|
||||
break
|
||||
benchmark_dict['average_fps'] = round(np.mean(overall_fps_list), 2)
|
||||
benchmark_dict['fps_variance'] = round(np.var(overall_fps_list), 4)
|
||||
print(f'Average fps of {repeat_times} evaluations: '
|
||||
f'{benchmark_dict["average_fps"]}')
|
||||
print(f'The variance of {repeat_times} evaluations: '
|
||||
f'{benchmark_dict["fps_variance"]}')
|
||||
dump(benchmark_dict, json_file, indent=4)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
77
finetune/tools/analysis_tools/browse_dataset.py
Normal file
77
finetune/tools/analysis_tools/browse_dataset.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import argparse
|
||||
import os.path as osp
|
||||
|
||||
from mmengine.config import Config, DictAction
|
||||
from mmengine.utils import ProgressBar
|
||||
|
||||
from mmseg.registry import DATASETS, VISUALIZERS
|
||||
from mmseg.utils import register_all_modules
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Browse a dataset')
|
||||
parser.add_argument('config', help='train config file path')
|
||||
parser.add_argument(
|
||||
'--output-dir',
|
||||
default=None,
|
||||
type=str,
|
||||
help='If there is no display interface, you can save it')
|
||||
parser.add_argument('--not-show', default=False, action='store_true')
|
||||
parser.add_argument(
|
||||
'--show-interval',
|
||||
type=float,
|
||||
default=2,
|
||||
help='the interval of show (s)')
|
||||
parser.add_argument(
|
||||
'--cfg-options',
|
||||
nargs='+',
|
||||
action=DictAction,
|
||||
help='override some settings in the used config, the key-value pair '
|
||||
'in xxx=yyy format will be merged into config file. If the value to '
|
||||
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
|
||||
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
|
||||
'Note that the quotation marks are necessary and that no white space '
|
||||
'is allowed.')
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
cfg = Config.fromfile(args.config)
|
||||
if args.cfg_options is not None:
|
||||
cfg.merge_from_dict(args.cfg_options)
|
||||
|
||||
# register all modules in mmdet into the registries
|
||||
register_all_modules()
|
||||
|
||||
dataset = DATASETS.build(cfg.train_dataloader.dataset)
|
||||
visualizer = VISUALIZERS.build(cfg.visualizer)
|
||||
visualizer.dataset_meta = dataset.metainfo
|
||||
|
||||
progress_bar = ProgressBar(len(dataset))
|
||||
for item in dataset:
|
||||
img = item['inputs'].permute(1, 2, 0).numpy()
|
||||
img = img[..., [2, 1, 0]] # bgr to rgb
|
||||
data_sample = item['data_samples'].numpy()
|
||||
img_path = osp.basename(item['data_samples'].img_path)
|
||||
|
||||
out_file = osp.join(
|
||||
args.output_dir,
|
||||
osp.basename(img_path)) if args.output_dir is not None else None
|
||||
|
||||
visualizer.add_datasample(
|
||||
name=osp.basename(img_path),
|
||||
image=img,
|
||||
data_sample=data_sample,
|
||||
draw_gt=True,
|
||||
draw_pred=False,
|
||||
wait_time=args.show_interval,
|
||||
out_file=out_file,
|
||||
show=not args.not_show)
|
||||
progress_bar.update()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
197
finetune/tools/analysis_tools/confusion_matrix.py
Normal file
197
finetune/tools/analysis_tools/confusion_matrix.py
Normal file
@@ -0,0 +1,197 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import argparse
|
||||
import os
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from matplotlib.ticker import MultipleLocator
|
||||
from mmengine.config import Config, DictAction
|
||||
from mmengine.registry import init_default_scope
|
||||
from mmengine.utils import mkdir_or_exist, progressbar
|
||||
from PIL import Image
|
||||
|
||||
from mmseg.registry import DATASETS
|
||||
|
||||
init_default_scope('mmseg')
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Generate confusion matrix from segmentation results')
|
||||
parser.add_argument('config', help='test config file path')
|
||||
parser.add_argument(
|
||||
'prediction_path', help='prediction path where test folder result')
|
||||
parser.add_argument(
|
||||
'save_dir', help='directory where confusion matrix will be saved')
|
||||
parser.add_argument(
|
||||
'--show', action='store_true', help='show confusion matrix')
|
||||
parser.add_argument(
|
||||
'--color-theme',
|
||||
default='winter',
|
||||
help='theme of the matrix color map')
|
||||
parser.add_argument(
|
||||
'--title',
|
||||
default='Normalized Confusion Matrix',
|
||||
help='title of the matrix color map')
|
||||
parser.add_argument(
|
||||
'--cfg-options',
|
||||
nargs='+',
|
||||
action=DictAction,
|
||||
help='override some settings in the used config, the key-value pair '
|
||||
'in xxx=yyy format will be merged into config file. If the value to '
|
||||
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
|
||||
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
|
||||
'Note that the quotation marks are necessary and that no white space '
|
||||
'is allowed.')
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def calculate_confusion_matrix(dataset, results):
|
||||
"""Calculate the confusion matrix.
|
||||
|
||||
Args:
|
||||
dataset (Dataset): Test or val dataset.
|
||||
results (list[ndarray]): A list of segmentation results in each image.
|
||||
"""
|
||||
n = len(dataset.METAINFO['classes'])
|
||||
confusion_matrix = np.zeros(shape=[n, n])
|
||||
assert len(dataset) == len(results)
|
||||
ignore_index = dataset.ignore_index
|
||||
reduce_zero_label = dataset.reduce_zero_label
|
||||
prog_bar = progressbar.ProgressBar(len(results))
|
||||
for idx, per_img_res in enumerate(results):
|
||||
res_segm = per_img_res
|
||||
gt_segm = dataset[idx]['data_samples'] \
|
||||
.gt_sem_seg.data.squeeze().numpy().astype(np.uint8)
|
||||
gt_segm, res_segm = gt_segm.flatten(), res_segm.flatten()
|
||||
if reduce_zero_label:
|
||||
gt_segm = gt_segm - 1
|
||||
to_ignore = gt_segm == ignore_index
|
||||
|
||||
gt_segm, res_segm = gt_segm[~to_ignore], res_segm[~to_ignore]
|
||||
inds = n * gt_segm + res_segm
|
||||
mat = np.bincount(inds, minlength=n**2).reshape(n, n)
|
||||
confusion_matrix += mat
|
||||
prog_bar.update()
|
||||
return confusion_matrix
|
||||
|
||||
|
||||
def plot_confusion_matrix(confusion_matrix,
|
||||
labels,
|
||||
save_dir=None,
|
||||
show=True,
|
||||
title='Normalized Confusion Matrix',
|
||||
color_theme='OrRd'):
|
||||
"""Draw confusion matrix with matplotlib.
|
||||
|
||||
Args:
|
||||
confusion_matrix (ndarray): The confusion matrix.
|
||||
labels (list[str]): List of class names.
|
||||
save_dir (str|optional): If set, save the confusion matrix plot to the
|
||||
given path. Default: None.
|
||||
show (bool): Whether to show the plot. Default: True.
|
||||
title (str): Title of the plot. Default: `Normalized Confusion Matrix`.
|
||||
color_theme (str): Theme of the matrix color map. Default: `winter`.
|
||||
"""
|
||||
# normalize the confusion matrix
|
||||
per_label_sums = confusion_matrix.sum(axis=1)[:, np.newaxis]
|
||||
confusion_matrix = \
|
||||
confusion_matrix.astype(np.float32) / per_label_sums * 100
|
||||
|
||||
num_classes = len(labels)
|
||||
fig, ax = plt.subplots(
|
||||
figsize=(2 * num_classes, 2 * num_classes * 0.8), dpi=300)
|
||||
cmap = plt.get_cmap(color_theme)
|
||||
im = ax.imshow(confusion_matrix, cmap=cmap)
|
||||
colorbar = plt.colorbar(mappable=im, ax=ax)
|
||||
colorbar.ax.tick_params(labelsize=20) # 设置 colorbar 标签的字体大小
|
||||
|
||||
title_font = {'weight': 'bold', 'size': 20}
|
||||
ax.set_title(title, fontdict=title_font)
|
||||
label_font = {'size': 40}
|
||||
plt.ylabel('Ground Truth Label', fontdict=label_font)
|
||||
plt.xlabel('Prediction Label', fontdict=label_font)
|
||||
|
||||
# draw locator
|
||||
xmajor_locator = MultipleLocator(1)
|
||||
xminor_locator = MultipleLocator(0.5)
|
||||
ax.xaxis.set_major_locator(xmajor_locator)
|
||||
ax.xaxis.set_minor_locator(xminor_locator)
|
||||
ymajor_locator = MultipleLocator(1)
|
||||
yminor_locator = MultipleLocator(0.5)
|
||||
ax.yaxis.set_major_locator(ymajor_locator)
|
||||
ax.yaxis.set_minor_locator(yminor_locator)
|
||||
|
||||
# draw grid
|
||||
ax.grid(True, which='minor', linestyle='-')
|
||||
|
||||
# draw label
|
||||
ax.set_xticks(np.arange(num_classes))
|
||||
ax.set_yticks(np.arange(num_classes))
|
||||
ax.set_xticklabels(labels, fontsize=20)
|
||||
ax.set_yticklabels(labels, fontsize=20)
|
||||
|
||||
ax.tick_params(
|
||||
axis='x', bottom=False, top=True, labelbottom=False, labeltop=True)
|
||||
plt.setp(
|
||||
ax.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
|
||||
|
||||
# draw confusion matrix value
|
||||
for i in range(num_classes):
|
||||
for j in range(num_classes):
|
||||
ax.text(
|
||||
j,
|
||||
i,
|
||||
'{}%'.format(
|
||||
round(confusion_matrix[i, j], 2
|
||||
) if not np.isnan(confusion_matrix[i, j]) else -1),
|
||||
ha='center',
|
||||
va='center',
|
||||
color='k',
|
||||
size=20)
|
||||
|
||||
ax.set_ylim(len(confusion_matrix) - 0.5, -0.5) # matplotlib>3.1.1
|
||||
|
||||
fig.tight_layout()
|
||||
if save_dir is not None:
|
||||
mkdir_or_exist(save_dir)
|
||||
plt.savefig(
|
||||
os.path.join(save_dir, 'confusion_matrix.png'), format='png')
|
||||
if show:
|
||||
plt.show()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
|
||||
cfg = Config.fromfile(args.config)
|
||||
if args.cfg_options is not None:
|
||||
cfg.merge_from_dict(args.cfg_options)
|
||||
|
||||
results = []
|
||||
for img in sorted(os.listdir(args.prediction_path)):
|
||||
img = os.path.join(args.prediction_path, img)
|
||||
image = Image.open(img)
|
||||
image = np.copy(image)
|
||||
results.append(image)
|
||||
|
||||
assert isinstance(results, list)
|
||||
if isinstance(results[0], np.ndarray):
|
||||
pass
|
||||
else:
|
||||
raise TypeError('invalid type of prediction results')
|
||||
|
||||
dataset = DATASETS.build(cfg.test_dataloader.dataset)
|
||||
confusion_matrix = calculate_confusion_matrix(dataset, results)
|
||||
plot_confusion_matrix(
|
||||
confusion_matrix,
|
||||
dataset.METAINFO['classes'],
|
||||
save_dir=args.save_dir,
|
||||
show=args.show,
|
||||
title=args.title,
|
||||
color_theme=args.color_theme)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
124
finetune/tools/analysis_tools/get_flops.py
Normal file
124
finetune/tools/analysis_tools/get_flops.py
Normal file
@@ -0,0 +1,124 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import argparse
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
from mmengine import Config, DictAction
|
||||
from mmengine.logging import MMLogger
|
||||
from mmengine.model import revert_sync_batchnorm
|
||||
from mmengine.registry import init_default_scope
|
||||
|
||||
from mmseg.models import BaseSegmentor
|
||||
from mmseg.registry import MODELS
|
||||
from mmseg.structures import SegDataSample
|
||||
|
||||
try:
|
||||
from mmengine.analysis import get_model_complexity_info
|
||||
from mmengine.analysis.print_helper import _format_size
|
||||
except ImportError:
|
||||
raise ImportError('Please upgrade mmengine >= 0.6.0 to use this script.')
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Get the FLOPs of a segmentor')
|
||||
parser.add_argument('config', help='train config file path')
|
||||
parser.add_argument(
|
||||
'--shape',
|
||||
type=int,
|
||||
nargs='+',
|
||||
default=[2048, 1024],
|
||||
help='input image size')
|
||||
parser.add_argument(
|
||||
'--cfg-options',
|
||||
nargs='+',
|
||||
action=DictAction,
|
||||
help='override some settings in the used config, the key-value pair '
|
||||
'in xxx=yyy format will be merged into config file. If the value to '
|
||||
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
|
||||
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
|
||||
'Note that the quotation marks are necessary and that no white space '
|
||||
'is allowed.')
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def inference(args: argparse.Namespace, logger: MMLogger) -> dict:
|
||||
config_name = Path(args.config)
|
||||
|
||||
if not config_name.exists():
|
||||
logger.error(f'Config file {config_name} does not exist')
|
||||
|
||||
cfg: Config = Config.fromfile(config_name)
|
||||
cfg.work_dir = tempfile.TemporaryDirectory().name
|
||||
cfg.log_level = 'WARN'
|
||||
if args.cfg_options is not None:
|
||||
cfg.merge_from_dict(args.cfg_options)
|
||||
|
||||
init_default_scope(cfg.get('scope', 'mmseg'))
|
||||
|
||||
if len(args.shape) == 1:
|
||||
input_shape = (3, args.shape[0], args.shape[0])
|
||||
elif len(args.shape) == 2:
|
||||
input_shape = (3, ) + tuple(args.shape)
|
||||
else:
|
||||
raise ValueError('invalid input shape')
|
||||
result = {}
|
||||
|
||||
model: BaseSegmentor = MODELS.build(cfg.model)
|
||||
if hasattr(model, 'auxiliary_head'):
|
||||
model.auxiliary_head = None
|
||||
if torch.cuda.is_available():
|
||||
model.cuda()
|
||||
model = revert_sync_batchnorm(model)
|
||||
result['ori_shape'] = input_shape[-2:]
|
||||
result['pad_shape'] = input_shape[-2:]
|
||||
data_batch = {
|
||||
'inputs': [torch.rand(input_shape)],
|
||||
'data_samples': [SegDataSample(metainfo=result)]
|
||||
}
|
||||
data = model.data_preprocessor(data_batch)
|
||||
model.eval()
|
||||
if cfg.model.decode_head.type in ['MaskFormerHead', 'Mask2FormerHead']:
|
||||
# TODO: Support MaskFormer and Mask2Former
|
||||
raise NotImplementedError('MaskFormer and Mask2Former are not '
|
||||
'supported yet.')
|
||||
outputs = get_model_complexity_info(
|
||||
model,
|
||||
input_shape=None,
|
||||
inputs=data['inputs'],
|
||||
show_table=False,
|
||||
show_arch=False)
|
||||
result['flops'] = _format_size(outputs['flops'])
|
||||
result['params'] = _format_size(outputs['params'])
|
||||
result['compute_type'] = 'direct: randomly generate a picture'
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
args = parse_args()
|
||||
logger = MMLogger.get_instance(name='MMLogger')
|
||||
|
||||
result = inference(args, logger)
|
||||
split_line = '=' * 30
|
||||
ori_shape = result['ori_shape']
|
||||
pad_shape = result['pad_shape']
|
||||
flops = result['flops']
|
||||
params = result['params']
|
||||
compute_type = result['compute_type']
|
||||
|
||||
if pad_shape != ori_shape:
|
||||
print(f'{split_line}\nUse size divisor set input shape '
|
||||
f'from {ori_shape} to {pad_shape}')
|
||||
print(f'{split_line}\nCompute type: {compute_type}\n'
|
||||
f'Input shape: {pad_shape}\nFlops: {flops}\n'
|
||||
f'Params: {params}\n{split_line}')
|
||||
print('!!!Please be cautious if you use the results in papers. '
|
||||
'You may need to check if all ops are supported and verify '
|
||||
'that the flops computation is correct.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
127
finetune/tools/analysis_tools/visualization_cam.py
Normal file
127
finetune/tools/analysis_tools/visualization_cam.py
Normal file
@@ -0,0 +1,127 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
"""Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM).
|
||||
|
||||
requirement: pip install grad-cam
|
||||
"""
|
||||
|
||||
from argparse import ArgumentParser
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from mmengine import Config
|
||||
from mmengine.model import revert_sync_batchnorm
|
||||
from PIL import Image
|
||||
from pytorch_grad_cam import GradCAM
|
||||
from pytorch_grad_cam.utils.image import preprocess_image, show_cam_on_image
|
||||
|
||||
from mmseg.apis import inference_model, init_model, show_result_pyplot
|
||||
from mmseg.utils import register_all_modules
|
||||
|
||||
|
||||
class SemanticSegmentationTarget:
|
||||
"""wrap the model.
|
||||
|
||||
requirement: pip install grad-cam
|
||||
|
||||
Args:
|
||||
category (int): Visualization class.
|
||||
mask (ndarray): Mask of class.
|
||||
size (tuple): Image size.
|
||||
"""
|
||||
|
||||
def __init__(self, category, mask, size):
|
||||
self.category = category
|
||||
self.mask = torch.from_numpy(mask)
|
||||
self.size = size
|
||||
if torch.cuda.is_available():
|
||||
self.mask = self.mask.cuda()
|
||||
|
||||
def __call__(self, model_output):
|
||||
model_output = torch.unsqueeze(model_output, dim=0)
|
||||
model_output = F.interpolate(
|
||||
model_output, size=self.size, mode='bilinear')
|
||||
model_output = torch.squeeze(model_output, dim=0)
|
||||
|
||||
return (model_output[self.category, :, :] * self.mask).sum()
|
||||
|
||||
|
||||
def main():
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('img', help='Image file')
|
||||
parser.add_argument('config', help='Config file')
|
||||
parser.add_argument('checkpoint', help='Checkpoint file')
|
||||
parser.add_argument(
|
||||
'--out-file',
|
||||
default='prediction.png',
|
||||
help='Path to output prediction file')
|
||||
parser.add_argument(
|
||||
'--cam-file', default='vis_cam.png', help='Path to output cam file')
|
||||
parser.add_argument(
|
||||
'--target-layers',
|
||||
default='backbone.layer4[2]',
|
||||
help='Target layers to visualize CAM')
|
||||
parser.add_argument(
|
||||
'--category-index', default='7', help='Category to visualize CAM')
|
||||
parser.add_argument(
|
||||
'--device', default='cuda:0', help='Device used for inference')
|
||||
args = parser.parse_args()
|
||||
|
||||
# build the model from a config file and a checkpoint file
|
||||
register_all_modules()
|
||||
model = init_model(args.config, args.checkpoint, device=args.device)
|
||||
if args.device == 'cpu':
|
||||
model = revert_sync_batchnorm(model)
|
||||
|
||||
# test a single image
|
||||
result = inference_model(model, args.img)
|
||||
|
||||
# show the results
|
||||
show_result_pyplot(
|
||||
model,
|
||||
args.img,
|
||||
result,
|
||||
draw_gt=False,
|
||||
show=False if args.out_file is not None else True,
|
||||
out_file=args.out_file)
|
||||
|
||||
# result data conversion
|
||||
prediction_data = result.pred_sem_seg.data
|
||||
pre_np_data = prediction_data.cpu().numpy().squeeze(0)
|
||||
|
||||
target_layers = args.target_layers
|
||||
target_layers = [eval(f'model.{target_layers}')]
|
||||
|
||||
category = int(args.category_index)
|
||||
mask_float = np.float32(pre_np_data == category)
|
||||
|
||||
# data processing
|
||||
image = np.array(Image.open(args.img).convert('RGB'))
|
||||
height, width = image.shape[0], image.shape[1]
|
||||
rgb_img = np.float32(image) / 255
|
||||
config = Config.fromfile(args.config)
|
||||
image_mean = config.data_preprocessor['mean']
|
||||
image_std = config.data_preprocessor['std']
|
||||
input_tensor = preprocess_image(
|
||||
rgb_img,
|
||||
mean=[x / 255 for x in image_mean],
|
||||
std=[x / 255 for x in image_std])
|
||||
|
||||
# Grad CAM(Class Activation Maps)
|
||||
# Can also be LayerCAM, XGradCAM, GradCAMPlusPlus, EigenCAM, EigenGradCAM
|
||||
targets = [
|
||||
SemanticSegmentationTarget(category, mask_float, (height, width))
|
||||
]
|
||||
with GradCAM(
|
||||
model=model,
|
||||
target_layers=target_layers,
|
||||
use_cuda=torch.cuda.is_available()) as cam:
|
||||
grayscale_cam = cam(input_tensor=input_tensor, targets=targets)[0, :]
|
||||
cam_image = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True)
|
||||
|
||||
# save cam file
|
||||
Image.fromarray(cam_image).save(args.cam_file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user