forked from wffjwbbf/ComDesignProject
handle warnings for package lost by raising errors
This commit is contained in:
parent
d0cf4e32a1
commit
673e3c70e5
|
|
@ -911,6 +911,7 @@ class autoShape(nn.Module):
|
|||
shape1.append([y * g for y in s])
|
||||
imgs[i] = im # update
|
||||
shape1 = [make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)] # inference shape
|
||||
raise Exception('letterbox has been dropped')
|
||||
x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs] # pad
|
||||
x = np.stack(x, 0) if n > 1 else x[0][None] # stack
|
||||
x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW
|
||||
|
|
@ -1244,7 +1245,8 @@ class RepConv_OREPA(nn.Module):
|
|||
self.nonlinearity = nonlinear
|
||||
|
||||
if use_se:
|
||||
self.se = SEBlock(self.out_channels, internal_neurons=self.out_channels // 16)
|
||||
raise Exception('SEBlock has been dropped')
|
||||
# self.se = SEBlock(self.out_channels, internal_neurons=self.out_channels // 16)
|
||||
else:
|
||||
self.se = nn.Identity()
|
||||
|
||||
|
|
@ -1490,6 +1492,7 @@ class SwinTransformerLayer(nn.Module):
|
|||
dim, window_size=(self.window_size, self.window_size), num_heads=num_heads,
|
||||
qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
|
||||
|
||||
raise Exception('DropPath has been dropped')
|
||||
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
||||
self.norm2 = norm_layer(dim)
|
||||
mlp_hidden_dim = int(dim * mlp_ratio)
|
||||
|
|
@ -1835,7 +1838,7 @@ class SwinTransformerLayer_v2(nn.Module):
|
|||
dim, window_size=(self.window_size, self.window_size), num_heads=num_heads,
|
||||
qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=drop,
|
||||
pretrained_window_size=(pretrained_window_size, pretrained_window_size))
|
||||
|
||||
raise Exception('DropPath has been dropped')
|
||||
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
||||
self.norm2 = norm_layer(dim)
|
||||
mlp_hidden_dim = int(dim * mlp_ratio)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import torch
|
|||
import torch.nn as nn
|
||||
|
||||
from models.common import Conv, DWConv
|
||||
from utils.google_utils import attempt_download
|
||||
|
||||
|
||||
class CrossConv(nn.Module):
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ from utils.torch_utils import time_synchronized, fuse_conv_and_bn, model_info, s
|
|||
select_device, copy_attr
|
||||
from utils.loss import SigmoidBin
|
||||
|
||||
try:
|
||||
import thop # for FLOPS computation
|
||||
except ImportError:
|
||||
thop = None
|
||||
# try:
|
||||
# import thop # for FLOPS computation
|
||||
# except ImportError:
|
||||
# thop = None
|
||||
|
||||
|
||||
class Detect(nn.Module):
|
||||
|
|
@ -512,6 +512,7 @@ class Model(nn.Module):
|
|||
if isinstance(cfg, dict):
|
||||
self.yaml = cfg # model dict
|
||||
else: # is *.yaml
|
||||
raise Exception('yaml has been discarded')
|
||||
import yaml # for torch hub
|
||||
self.yaml_file = Path(cfg).name
|
||||
with open(cfg) as f:
|
||||
|
|
@ -613,6 +614,7 @@ class Model(nn.Module):
|
|||
|
||||
if profile:
|
||||
c = isinstance(m, (Detect, IDetect, IAuxDetect, IBin))
|
||||
raise Exception('thop has been discarded')
|
||||
o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPS
|
||||
for _ in range(10):
|
||||
m(x.copy() if c else x)
|
||||
|
|
|
|||
155
utils/add_nms.py
155
utils/add_nms.py
|
|
@ -1,155 +0,0 @@
|
|||
import numpy as np
|
||||
import onnx
|
||||
from onnx import shape_inference
|
||||
try:
|
||||
import onnx_graphsurgeon as gs
|
||||
except Exception as e:
|
||||
print('Import onnx_graphsurgeon failure: %s' % e)
|
||||
|
||||
import logging
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class RegisterNMS(object):
|
||||
def __init__(
|
||||
self,
|
||||
onnx_model_path: str,
|
||||
precision: str = "fp32",
|
||||
):
|
||||
|
||||
self.graph = gs.import_onnx(onnx.load(onnx_model_path))
|
||||
assert self.graph
|
||||
LOGGER.info("ONNX graph created successfully")
|
||||
# Fold constants via ONNX-GS that PyTorch2ONNX may have missed
|
||||
self.graph.fold_constants()
|
||||
self.precision = precision
|
||||
self.batch_size = 1
|
||||
def infer(self):
|
||||
"""
|
||||
Sanitize the graph by cleaning any unconnected nodes, do a topological resort,
|
||||
and fold constant inputs values. When possible, run shape inference on the
|
||||
ONNX graph to determine tensor shapes.
|
||||
"""
|
||||
for _ in range(3):
|
||||
count_before = len(self.graph.nodes)
|
||||
|
||||
self.graph.cleanup().toposort()
|
||||
try:
|
||||
for node in self.graph.nodes:
|
||||
for o in node.outputs:
|
||||
o.shape = None
|
||||
model = gs.export_onnx(self.graph)
|
||||
model = shape_inference.infer_shapes(model)
|
||||
self.graph = gs.import_onnx(model)
|
||||
except Exception as e:
|
||||
LOGGER.info(f"Shape inference could not be performed at this time:\n{e}")
|
||||
try:
|
||||
self.graph.fold_constants(fold_shapes=True)
|
||||
except TypeError as e:
|
||||
LOGGER.error(
|
||||
"This version of ONNX GraphSurgeon does not support folding shapes, "
|
||||
f"please upgrade your onnx_graphsurgeon module. Error:\n{e}"
|
||||
)
|
||||
raise
|
||||
|
||||
count_after = len(self.graph.nodes)
|
||||
if count_before == count_after:
|
||||
# No new folding occurred in this iteration, so we can stop for now.
|
||||
break
|
||||
|
||||
def save(self, output_path):
|
||||
"""
|
||||
Save the ONNX model to the given location.
|
||||
Args:
|
||||
output_path: Path pointing to the location where to write
|
||||
out the updated ONNX model.
|
||||
"""
|
||||
self.graph.cleanup().toposort()
|
||||
model = gs.export_onnx(self.graph)
|
||||
onnx.save(model, output_path)
|
||||
LOGGER.info(f"Saved ONNX model to {output_path}")
|
||||
|
||||
def register_nms(
|
||||
self,
|
||||
*,
|
||||
score_thresh: float = 0.25,
|
||||
nms_thresh: float = 0.45,
|
||||
detections_per_img: int = 100,
|
||||
):
|
||||
"""
|
||||
Register the ``EfficientNMS_TRT`` plugin node.
|
||||
NMS expects these shapes for its input tensors:
|
||||
- box_net: [batch_size, number_boxes, 4]
|
||||
- class_net: [batch_size, number_boxes, number_labels]
|
||||
Args:
|
||||
score_thresh (float): The scalar threshold for score (low scoring boxes are removed).
|
||||
nms_thresh (float): The scalar threshold for IOU (new boxes that have high IOU
|
||||
overlap with previously selected boxes are removed).
|
||||
detections_per_img (int): Number of best detections to keep after NMS.
|
||||
"""
|
||||
|
||||
self.infer()
|
||||
# Find the concat node at the end of the network
|
||||
op_inputs = self.graph.outputs
|
||||
op = "EfficientNMS_TRT"
|
||||
attrs = {
|
||||
"plugin_version": "1",
|
||||
"background_class": -1, # no background class
|
||||
"max_output_boxes": detections_per_img,
|
||||
"score_threshold": score_thresh,
|
||||
"iou_threshold": nms_thresh,
|
||||
"score_activation": False,
|
||||
"box_coding": 0,
|
||||
}
|
||||
|
||||
if self.precision == "fp32":
|
||||
dtype_output = np.float32
|
||||
elif self.precision == "fp16":
|
||||
dtype_output = np.float16
|
||||
else:
|
||||
raise NotImplementedError(f"Currently not supports precision: {self.precision}")
|
||||
|
||||
# NMS Outputs
|
||||
output_num_detections = gs.Variable(
|
||||
name="num_dets",
|
||||
dtype=np.int32,
|
||||
shape=[self.batch_size, 1],
|
||||
) # A scalar indicating the number of valid detections per batch image.
|
||||
output_boxes = gs.Variable(
|
||||
name="det_boxes",
|
||||
dtype=dtype_output,
|
||||
shape=[self.batch_size, detections_per_img, 4],
|
||||
)
|
||||
output_scores = gs.Variable(
|
||||
name="det_scores",
|
||||
dtype=dtype_output,
|
||||
shape=[self.batch_size, detections_per_img],
|
||||
)
|
||||
output_labels = gs.Variable(
|
||||
name="det_classes",
|
||||
dtype=np.int32,
|
||||
shape=[self.batch_size, detections_per_img],
|
||||
)
|
||||
|
||||
op_outputs = [output_num_detections, output_boxes, output_scores, output_labels]
|
||||
|
||||
# Create the NMS Plugin node with the selected inputs. The outputs of the node will also
|
||||
# become the final outputs of the graph.
|
||||
self.graph.layer(op=op, name="batched_nms", inputs=op_inputs, outputs=op_outputs, attrs=attrs)
|
||||
LOGGER.info(f"Created NMS plugin '{op}' with attributes: {attrs}")
|
||||
|
||||
self.graph.outputs = op_outputs
|
||||
|
||||
self.infer()
|
||||
|
||||
def save(self, output_path):
|
||||
"""
|
||||
Save the ONNX model to the given location.
|
||||
Args:
|
||||
output_path: Path pointing to the location where to write
|
||||
out the updated ONNX model.
|
||||
"""
|
||||
self.graph.cleanup().toposort()
|
||||
model = gs.export_onnx(self.graph)
|
||||
onnx.save(model, output_path)
|
||||
LOGGER.info(f"Saved ONNX model to {output_path}")
|
||||
|
|
@ -86,10 +86,12 @@ def kmean_anchors(path='./data/coco.yaml', n=9, img_size=640, thr=4.0, gen=1000,
|
|||
return x, x.max(1)[0] # x, best_x
|
||||
|
||||
def anchor_fitness(k): # mutation fitness
|
||||
raise Exception('some value unexisting')
|
||||
_, best = metric(torch.tensor(k, dtype=torch.float32), wh)
|
||||
return (best * (best > thr).float()).mean() # fitness
|
||||
|
||||
def print_results(k):
|
||||
raise Exception('some value unexisting')
|
||||
k = k[np.argsort(k.prod(1))] # sort small to large
|
||||
x, best = metric(k, wh0)
|
||||
bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
|
||||
|
|
@ -100,15 +102,16 @@ def kmean_anchors(path='./data/coco.yaml', n=9, img_size=640, thr=4.0, gen=1000,
|
|||
print('%i,%i' % (round(x[0]), round(x[1])), end=', ' if i < len(k) - 1 else '\n') # use in *.cfg
|
||||
return k
|
||||
|
||||
if isinstance(path, str): # *.yaml file
|
||||
with open(path) as f:
|
||||
data_dict = yaml.load(f, Loader=yaml.SafeLoader) # model dict
|
||||
from utils.datasets import LoadImagesAndLabels
|
||||
dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
|
||||
else:
|
||||
dataset = path # dataset
|
||||
# if isinstance(path, str): # *.yaml file
|
||||
# with open(path) as f:
|
||||
# data_dict = yaml.load(f, Loader=yaml.SafeLoader) # model dict
|
||||
# from utils.datasets import LoadImagesAndLabels
|
||||
# dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
|
||||
# else:
|
||||
# dataset = path # dataset
|
||||
|
||||
# Get label wh
|
||||
raise Exception('dataset has been discarded')
|
||||
shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
||||
wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ import torch
|
|||
import torchvision
|
||||
# import yaml
|
||||
|
||||
from utils.google_utils import gsutil_getsize
|
||||
from utils.metrics import fitness
|
||||
from utils.torch_utils import init_torch_seeds
|
||||
|
||||
|
|
@ -813,35 +812,35 @@ def strip_optimizer(f='best.pt', s=''): # from utils.general import *; strip_op
|
|||
print(f"Optimizer stripped from {f},{(' saved as %s,' % s) if s else ''} {mb:.1f}MB")
|
||||
|
||||
|
||||
def print_mutation(hyp, results, yaml_file='hyp_evolved.yaml', bucket=''):
|
||||
# Print mutation results to evolve.txt (for use with train.py --evolve)
|
||||
a = '%10s' * len(hyp) % tuple(hyp.keys()) # hyperparam keys
|
||||
b = '%10.3g' * len(hyp) % tuple(hyp.values()) # hyperparam values
|
||||
c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3)
|
||||
print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c))
|
||||
# def print_mutation(hyp, results, yaml_file='hyp_evolved.yaml', bucket=''):
|
||||
# # Print mutation results to evolve.txt (for use with train.py --evolve)
|
||||
# a = '%10s' * len(hyp) % tuple(hyp.keys()) # hyperparam keys
|
||||
# b = '%10.3g' * len(hyp) % tuple(hyp.values()) # hyperparam values
|
||||
# c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3)
|
||||
# print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c))
|
||||
|
||||
if bucket:
|
||||
url = 'gs://%s/evolve.txt' % bucket
|
||||
if gsutil_getsize(url) > (os.path.getsize('evolve.txt') if os.path.exists('evolve.txt') else 0):
|
||||
os.system('gsutil cp %s .' % url) # download evolve.txt if larger than local
|
||||
# if bucket:
|
||||
# url = 'gs://%s/evolve.txt' % bucket
|
||||
# if gsutil_getsize(url) > (os.path.getsize('evolve.txt') if os.path.exists('evolve.txt') else 0):
|
||||
# os.system('gsutil cp %s .' % url) # download evolve.txt if larger than local
|
||||
|
||||
with open('evolve.txt', 'a') as f: # append result
|
||||
f.write(c + b + '\n')
|
||||
x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0) # load unique rows
|
||||
x = x[np.argsort(-fitness(x))] # sort
|
||||
np.savetxt('evolve.txt', x, '%10.3g') # save sort by fitness
|
||||
# with open('evolve.txt', 'a') as f: # append result
|
||||
# f.write(c + b + '\n')
|
||||
# x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0) # load unique rows
|
||||
# x = x[np.argsort(-fitness(x))] # sort
|
||||
# np.savetxt('evolve.txt', x, '%10.3g') # save sort by fitness
|
||||
|
||||
# Save yaml
|
||||
for i, k in enumerate(hyp.keys()):
|
||||
hyp[k] = float(x[0, i + 7])
|
||||
with open(yaml_file, 'w') as f:
|
||||
results = tuple(x[0, :7])
|
||||
c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3)
|
||||
f.write('# Hyperparameter Evolution Results\n# Generations: %g\n# Metrics: ' % len(x) + c + '\n\n')
|
||||
yaml.dump(hyp, f, sort_keys=False)
|
||||
# # Save yaml
|
||||
# for i, k in enumerate(hyp.keys()):
|
||||
# hyp[k] = float(x[0, i + 7])
|
||||
# with open(yaml_file, 'w') as f:
|
||||
# results = tuple(x[0, :7])
|
||||
# c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3)
|
||||
# f.write('# Hyperparameter Evolution Results\n# Generations: %g\n# Metrics: ' % len(x) + c + '\n\n')
|
||||
# yaml.dump(hyp, f, sort_keys=False)
|
||||
|
||||
if bucket:
|
||||
os.system('gsutil cp evolve.txt %s gs://%s' % (yaml_file, bucket)) # upload
|
||||
# if bucket:
|
||||
# os.system('gsutil cp evolve.txt %s gs://%s' % (yaml_file, bucket)) # upload
|
||||
|
||||
|
||||
def apply_classifier(x, model, img, im0):
|
||||
|
|
|
|||
|
|
@ -1,123 +0,0 @@
|
|||
# Google utils: https://cloud.google.com/storage/docs/reference/libraries
|
||||
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
import torch
|
||||
|
||||
|
||||
def gsutil_getsize(url=''):
|
||||
# gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
|
||||
s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8')
|
||||
return eval(s.split(' ')[0]) if len(s) else 0 # bytes
|
||||
|
||||
|
||||
def attempt_download(file, repo='WongKinYiu/yolov7'):
|
||||
# Attempt file download if does not exist
|
||||
file = Path(str(file).strip().replace("'", '').lower())
|
||||
|
||||
if not file.exists():
|
||||
try:
|
||||
response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api
|
||||
assets = [x['name'] for x in response['assets']] # release assets
|
||||
tag = response['tag_name'] # i.e. 'v1.0'
|
||||
except: # fallback plan
|
||||
assets = ['yolov7.pt', 'yolov7-tiny.pt', 'yolov7x.pt', 'yolov7-d6.pt', 'yolov7-e6.pt',
|
||||
'yolov7-e6e.pt', 'yolov7-w6.pt']
|
||||
tag = subprocess.check_output('git tag', shell=True).decode().split()[-1]
|
||||
|
||||
name = file.name
|
||||
if name in assets:
|
||||
msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'
|
||||
redundant = False # second download option
|
||||
try: # GitHub
|
||||
url = f'https://github.com/{repo}/releases/download/{tag}/{name}'
|
||||
print(f'Downloading {url} to {file}...')
|
||||
torch.hub.download_url_to_file(url, file)
|
||||
assert file.exists() and file.stat().st_size > 1E6 # check
|
||||
except Exception as e: # GCP
|
||||
print(f'Download error: {e}')
|
||||
assert redundant, 'No secondary mirror'
|
||||
url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'
|
||||
print(f'Downloading {url} to {file}...')
|
||||
os.system(f'curl -L {url} -o {file}') # torch.hub.download_url_to_file(url, weights)
|
||||
finally:
|
||||
if not file.exists() or file.stat().st_size < 1E6: # check
|
||||
file.unlink(missing_ok=True) # remove partial downloads
|
||||
print(f'ERROR: Download failure: {msg}')
|
||||
print('')
|
||||
return
|
||||
|
||||
|
||||
def gdrive_download(id='', file='tmp.zip'):
|
||||
# Downloads a file from Google Drive. from yolov7.utils.google_utils import *; gdrive_download()
|
||||
t = time.time()
|
||||
file = Path(file)
|
||||
cookie = Path('cookie') # gdrive cookie
|
||||
print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='')
|
||||
file.unlink(missing_ok=True) # remove existing file
|
||||
cookie.unlink(missing_ok=True) # remove existing cookie
|
||||
|
||||
# Attempt file download
|
||||
out = "NUL" if platform.system() == "Windows" else "/dev/null"
|
||||
os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}')
|
||||
if os.path.exists('cookie'): # large file
|
||||
s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}'
|
||||
else: # small file
|
||||
s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"'
|
||||
r = os.system(s) # execute, capture return
|
||||
cookie.unlink(missing_ok=True) # remove existing cookie
|
||||
|
||||
# Error check
|
||||
if r != 0:
|
||||
file.unlink(missing_ok=True) # remove partial
|
||||
print('Download error ') # raise Exception('Download error')
|
||||
return r
|
||||
|
||||
# Unzip if archive
|
||||
if file.suffix == '.zip':
|
||||
print('unzipping... ', end='')
|
||||
os.system(f'unzip -q {file}') # unzip
|
||||
file.unlink() # remove zip to free space
|
||||
|
||||
print(f'Done ({time.time() - t:.1f}s)')
|
||||
return r
|
||||
|
||||
|
||||
def get_token(cookie="./cookie"):
|
||||
with open(cookie) as f:
|
||||
for line in f:
|
||||
if "download" in line:
|
||||
return line.split()[-1]
|
||||
return ""
|
||||
|
||||
# def upload_blob(bucket_name, source_file_name, destination_blob_name):
|
||||
# # Uploads a file to a bucket
|
||||
# # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
|
||||
#
|
||||
# storage_client = storage.Client()
|
||||
# bucket = storage_client.get_bucket(bucket_name)
|
||||
# blob = bucket.blob(destination_blob_name)
|
||||
#
|
||||
# blob.upload_from_filename(source_file_name)
|
||||
#
|
||||
# print('File {} uploaded to {}.'.format(
|
||||
# source_file_name,
|
||||
# destination_blob_name))
|
||||
#
|
||||
#
|
||||
# def download_blob(bucket_name, source_blob_name, destination_file_name):
|
||||
# # Uploads a blob from a bucket
|
||||
# storage_client = storage.Client()
|
||||
# bucket = storage_client.get_bucket(bucket_name)
|
||||
# blob = bucket.blob(source_blob_name)
|
||||
#
|
||||
# blob.download_to_filename(destination_file_name)
|
||||
#
|
||||
# print('Blob {} downloaded to {}.'.format(
|
||||
# source_blob_name,
|
||||
# destination_file_name))
|
||||
128
utils/plots.py
128
utils/plots.py
|
|
@ -19,8 +19,6 @@ from PIL import Image, ImageDraw, ImageFont
|
|||
from scipy.signal import butter, filtfilt
|
||||
|
||||
from utils.general import xywh2xyxy, xyxy2xywh
|
||||
from utils.metrics import fitness
|
||||
|
||||
# Settings
|
||||
matplotlib.rc('font', **{'size': 11})
|
||||
matplotlib.use('Agg') # for writing to files only
|
||||
|
|
@ -269,77 +267,77 @@ def plot_study_txt(path='', x=None): # from utils.plots import *; plot_study_tx
|
|||
plt.savefig(str(Path(path).name) + '.png', dpi=300)
|
||||
|
||||
|
||||
def plot_labels(labels, names=(), save_dir=Path(''), loggers=None):
|
||||
# plot dataset labels
|
||||
print('Plotting labels... ')
|
||||
c, b = labels[:, 0], labels[:, 1:].transpose() # classes, boxes
|
||||
nc = int(c.max() + 1) # number of classes
|
||||
colors = color_list()
|
||||
x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height'])
|
||||
# def plot_labels(labels, names=(), save_dir=Path(''), loggers=None):
|
||||
# # plot dataset labels
|
||||
# print('Plotting labels... ')
|
||||
# c, b = labels[:, 0], labels[:, 1:].transpose() # classes, boxes
|
||||
# nc = int(c.max() + 1) # number of classes
|
||||
# colors = color_list()
|
||||
# x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height'])
|
||||
|
||||
# seaborn correlogram
|
||||
sns.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9))
|
||||
plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200)
|
||||
plt.close()
|
||||
# # seaborn correlogram
|
||||
# sns.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9))
|
||||
# plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200)
|
||||
# plt.close()
|
||||
|
||||
# matplotlib labels
|
||||
matplotlib.use('svg') # faster
|
||||
ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel()
|
||||
ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8)
|
||||
ax[0].set_ylabel('instances')
|
||||
if 0 < len(names) < 30:
|
||||
ax[0].set_xticks(range(len(names)))
|
||||
ax[0].set_xticklabels(names, rotation=90, fontsize=10)
|
||||
else:
|
||||
ax[0].set_xlabel('classes')
|
||||
sns.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9)
|
||||
sns.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9)
|
||||
# # matplotlib labels
|
||||
# matplotlib.use('svg') # faster
|
||||
# ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel()
|
||||
# ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8)
|
||||
# ax[0].set_ylabel('instances')
|
||||
# if 0 < len(names) < 30:
|
||||
# ax[0].set_xticks(range(len(names)))
|
||||
# ax[0].set_xticklabels(names, rotation=90, fontsize=10)
|
||||
# else:
|
||||
# ax[0].set_xlabel('classes')
|
||||
# sns.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9)
|
||||
# sns.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9)
|
||||
|
||||
# rectangles
|
||||
labels[:, 1:3] = 0.5 # center
|
||||
labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000
|
||||
img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255)
|
||||
for cls, *box in labels[:1000]:
|
||||
ImageDraw.Draw(img).rectangle(box, width=1, outline=colors[int(cls) % 10]) # plot
|
||||
ax[1].imshow(img)
|
||||
ax[1].axis('off')
|
||||
# # rectangles
|
||||
# labels[:, 1:3] = 0.5 # center
|
||||
# labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000
|
||||
# img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255)
|
||||
# for cls, *box in labels[:1000]:
|
||||
# ImageDraw.Draw(img).rectangle(box, width=1, outline=colors[int(cls) % 10]) # plot
|
||||
# ax[1].imshow(img)
|
||||
# ax[1].axis('off')
|
||||
|
||||
for a in [0, 1, 2, 3]:
|
||||
for s in ['top', 'right', 'left', 'bottom']:
|
||||
ax[a].spines[s].set_visible(False)
|
||||
# for a in [0, 1, 2, 3]:
|
||||
# for s in ['top', 'right', 'left', 'bottom']:
|
||||
# ax[a].spines[s].set_visible(False)
|
||||
|
||||
plt.savefig(save_dir / 'labels.jpg', dpi=200)
|
||||
matplotlib.use('Agg')
|
||||
plt.close()
|
||||
# plt.savefig(save_dir / 'labels.jpg', dpi=200)
|
||||
# matplotlib.use('Agg')
|
||||
# plt.close()
|
||||
|
||||
# loggers
|
||||
for k, v in loggers.items() or {}:
|
||||
if k == 'wandb' and v:
|
||||
v.log({"Labels": [v.Image(str(x), caption=x.name) for x in save_dir.glob('*labels*.jpg')]}, commit=False)
|
||||
# # loggers
|
||||
# for k, v in loggers.items() or {}:
|
||||
# if k == 'wandb' and v:
|
||||
# v.log({"Labels": [v.Image(str(x), caption=x.name) for x in save_dir.glob('*labels*.jpg')]}, commit=False)
|
||||
|
||||
|
||||
def plot_evolution(yaml_file='data/hyp.finetune.yaml'): # from utils.plots import *; plot_evolution()
|
||||
# Plot hyperparameter evolution results in evolve.txt
|
||||
with open(yaml_file) as f:
|
||||
hyp = yaml.load(f, Loader=yaml.SafeLoader)
|
||||
x = np.loadtxt('evolve.txt', ndmin=2)
|
||||
f = fitness(x)
|
||||
# weights = (f - f.min()) ** 2 # for weighted results
|
||||
plt.figure(figsize=(10, 12), tight_layout=True)
|
||||
matplotlib.rc('font', **{'size': 8})
|
||||
for i, (k, v) in enumerate(hyp.items()):
|
||||
y = x[:, i + 7]
|
||||
# mu = (y * weights).sum() / weights.sum() # best weighted result
|
||||
mu = y[f.argmax()] # best single result
|
||||
plt.subplot(6, 5, i + 1)
|
||||
plt.scatter(y, f, c=hist2d(y, f, 20), cmap='viridis', alpha=.8, edgecolors='none')
|
||||
plt.plot(mu, f.max(), 'k+', markersize=15)
|
||||
plt.title('%s = %.3g' % (k, mu), fontdict={'size': 9}) # limit to 40 characters
|
||||
if i % 5 != 0:
|
||||
plt.yticks([])
|
||||
print('%15s: %.3g' % (k, mu))
|
||||
plt.savefig('evolve.png', dpi=200)
|
||||
print('\nPlot saved as evolve.png')
|
||||
# def plot_evolution(yaml_file='data/hyp.finetune.yaml'): # from utils.plots import *; plot_evolution()
|
||||
# # Plot hyperparameter evolution results in evolve.txt
|
||||
# with open(yaml_file) as f:
|
||||
# hyp = yaml.load(f, Loader=yaml.SafeLoader)
|
||||
# x = np.loadtxt('evolve.txt', ndmin=2)
|
||||
# f = fitness(x)
|
||||
# # weights = (f - f.min()) ** 2 # for weighted results
|
||||
# plt.figure(figsize=(10, 12), tight_layout=True)
|
||||
# matplotlib.rc('font', **{'size': 8})
|
||||
# for i, (k, v) in enumerate(hyp.items()):
|
||||
# y = x[:, i + 7]
|
||||
# # mu = (y * weights).sum() / weights.sum() # best weighted result
|
||||
# mu = y[f.argmax()] # best single result
|
||||
# plt.subplot(6, 5, i + 1)
|
||||
# plt.scatter(y, f, c=hist2d(y, f, 20), cmap='viridis', alpha=.8, edgecolors='none')
|
||||
# plt.plot(mu, f.max(), 'k+', markersize=15)
|
||||
# plt.title('%s = %.3g' % (k, mu), fontdict={'size': 9}) # limit to 40 characters
|
||||
# if i % 5 != 0:
|
||||
# plt.yticks([])
|
||||
# print('%15s: %.3g' % (k, mu))
|
||||
# plt.savefig('evolve.png', dpi=200)
|
||||
# print('\nPlot saved as evolve.png')
|
||||
|
||||
|
||||
def profile_idetection(start=0, stop=0, labels=(), save_dir=''):
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ import torch.nn as nn
|
|||
import torch.nn.functional as F
|
||||
import torchvision
|
||||
|
||||
try:
|
||||
import thop # for FLOPS computation
|
||||
except ImportError:
|
||||
thop = None
|
||||
# try:
|
||||
# import thop # for FLOPS computation
|
||||
# except ImportError:
|
||||
# thop = None
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue