Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deploy/triton-inference-server/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def preprocess(img, input_shape, letter_box=True):
def postprocess(num_dets, det_boxes, det_scores, det_classes, img_w, img_h, input_shape, letter_box=True):
boxes = det_boxes[0, :num_dets[0][0]] / np.array([input_shape[0], input_shape[1], input_shape[0], input_shape[1]], dtype=np.float32)
scores = det_scores[0, :num_dets[0][0]]
classes = det_classes[0, :num_dets[0][0]].astype(np.int)
classes = det_classes[0, :num_dets[0][0]].astype(int)

old_h, old_w = img_h, img_w
offset_h, offset_w = 0, 0
Expand All @@ -43,7 +43,7 @@ def postprocess(num_dets, det_boxes, det_scores, det_classes, img_w, img_h, inpu
boxes = boxes * np.array([old_w, old_h, old_w, old_h], dtype=np.float32)
if letter_box:
boxes -= np.array([offset_w, offset_h, offset_w, offset_h], dtype=np.float32)
boxes = boxes.astype(np.int)
boxes = boxes.astype(int)

detected_objects = []
for box, score, label in zip(boxes, scores, classes):
Expand Down
6 changes: 5 additions & 1 deletion export.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
parser.add_argument('--include-nms', action='store_true', help='export end2end onnx')
parser.add_argument('--fp16', action='store_true', help='CoreML FP16 half-precision export')
parser.add_argument('--int8', action='store_true', help='CoreML INT8 quantization')
parser.add_argument('--export-snapml', action='store_true', help='Export SnapML compatible model')
opt = parser.parse_args()
opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
opt.dynamic = opt.dynamic and not opt.end2end
Expand Down Expand Up @@ -66,6 +67,9 @@
# m.forward = m.forward_export # assign forward (optional)
model.model[-1].export = not opt.grid # set Detect() layer grid export
y = model(img) # dry run
if opt.export_snapml:
print("Export SnapML")
model.model[-1].export_snapml = True
if opt.include_nms:
model.model[-1].include_nms = True
y = None
Expand Down Expand Up @@ -202,4 +206,4 @@
print('ONNX export failure: %s' % e)

# Finish
print('\nExport complete (%.2fs). Visualize with https://github.com/lutzroeder/netron.' % (time.time() - t))
print('\nExport complete (%.2fs). Visualize with https://github.com/lutzroeder/netron.' % (time.time() - t))
2 changes: 1 addition & 1 deletion models/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def attempt_load(weights, map_location=None):
model = Ensemble()
for w in weights if isinstance(weights, list) else [weights]:
attempt_download(w)
ckpt = torch.load(w, map_location=map_location) # load
ckpt = torch.load(w, map_location=map_location, weights_only=False) # load
model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model

# Compatibility updates
Expand Down
18 changes: 17 additions & 1 deletion models/yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Detect(nn.Module):
end2end = False
include_nms = False
concat = False
export_snapml = False

def __init__(self, nc=80, anchors=(), ch=()): # detection layer
super(Detect, self).__init__()
Expand All @@ -45,6 +46,11 @@ def forward(self, x):
self.training |= self.export
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv

if self.export_snapml:
z.append(x[i].sigmoid())
continue

bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

Expand All @@ -64,6 +70,8 @@ def forward(self, x):

if self.training:
out = x
elif self.export_snapml:
out = z
elif self.end2end:
out = torch.cat(z, 1)
elif self.include_nms:
Expand Down Expand Up @@ -100,6 +108,7 @@ class IDetect(nn.Module):
end2end = False
include_nms = False
concat = False
export_snapml = False

def __init__(self, nc=80, anchors=(), ch=()): # detection layer
super(IDetect, self).__init__()
Expand Down Expand Up @@ -143,6 +152,11 @@ def fuseforward(self, x):
self.training |= self.export
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv

if self.export_snapml:
z.append(x[i].sigmoid())
continue

bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

Expand All @@ -163,6 +177,8 @@ def fuseforward(self, x):

if self.training:
out = x
elif self.export_snapml:
out = z
elif self.end2end:
out = torch.cat(z, 1)
elif self.include_nms:
Expand Down Expand Up @@ -840,4 +856,4 @@ def parse_model(d, ch): # model_dict, input_channels(3)
# tb_writer = SummaryWriter()
# print("Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/")
# tb_writer.add_graph(model.model, img) # add model to tensorboard
# tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard
# tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Pillow>=7.1.2
PyYAML>=5.3.1
requests>=2.23.0
scipy>=1.4.1
torch>=1.7.0,!=1.12.0
torchvision>=0.8.1,!=0.13.0
torch>=2.5.0
torchvision>=0.20.0
tqdm>=4.41.0
protobuf<4.21.3

Expand Down
6 changes: 3 additions & 3 deletions tools/instance.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def train(hyp, opt, device, tb_writer=None):
loggers = {'wandb': None} # loggers dict
if rank in [-1, 0]:
opt.hyp = hyp # add hyperparameters
run_id = torch.load(weights, map_location=device).get('wandb_id') if weights.endswith('.pt') and os.path.isfile(weights) else None
run_id = torch.load(weights, map_location=device, weights_only=False).get('wandb_id') if weights.endswith('.pt') and os.path.isfile(weights) else None
wandb_logger = WandbLogger(opt, Path(opt.save_dir).stem, run_id, data_dict)
loggers['wandb'] = wandb_logger.wandb
data_dict = wandb_logger.data_dict
Expand All @@ -84,7 +84,7 @@ def train(hyp, opt, device, tb_writer=None):
if pretrained:
with torch_distributed_zero_first(rank):
attempt_download(weights) # download if not found locally
ckpt = torch.load(weights, map_location=device) # load checkpoint
ckpt = torch.load(weights, map_location=device, weights_only=False) # load checkpoint
model = Model(opt.cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
exclude = ['anchor'] if (opt.cfg or hyp.get('anchors')) and not opt.resume else [] # exclude keys
state_dict = ckpt['model'].float().state_dict() # to FP32
Expand Down
2 changes: 1 addition & 1 deletion train_aux.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def train(hyp, opt, device, tb_writer=None):
if pretrained:
with torch_distributed_zero_first(rank):
attempt_download(weights) # download if not found locally
ckpt = torch.load(weights, map_location=device) # load checkpoint
ckpt = torch.load(weights, map_location=device, weights_only=False) # load checkpoint
model = Model(opt.cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
exclude = ['anchor'] if (opt.cfg or hyp.get('anchors')) and not opt.resume else [] # exclude keys
state_dict = ckpt['model'].float().state_dict() # to FP32
Expand Down
10 changes: 5 additions & 5 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, r
self.label_files = img2label_paths(self.img_files) # labels
cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache') # cached labels
if cache_path.is_file():
cache, exists = torch.load(cache_path), True # load
cache, exists = torch.load(cache_path, weights_only=False), True # load
#if cache['hash'] != get_hash(self.label_files + self.img_files) or 'version' not in cache: # changed
# cache, exists = self.cache_labels(cache_path, prefix), False # re-cache
else:
Expand All @@ -415,7 +415,7 @@ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, r
x[:, 0] = 0

n = len(shapes) # number of images
bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index
bi = np.floor(np.arange(n) / batch_size).astype(int) # batch index
nb = bi[-1] + 1 # number of batches
self.batch = bi # batch index of image
self.n = n
Expand Down Expand Up @@ -443,7 +443,7 @@ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, r
elif mini > 1:
shapes[i] = [1, 1 / mini]

self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride
self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(int) * stride

# Cache images into memory for faster training (WARNING: large datasets may exceed system RAM)
self.imgs = [None] * n
Expand Down Expand Up @@ -1200,7 +1200,7 @@ def pastein(image, labels, sample_labels, sample_images, sample_masks):
r_image = cv2.resize(sample_images[sel_ind], (r_w, r_h))
temp_crop = image[ymin:ymin+r_h, xmin:xmin+r_w]
m_ind = r_mask > 0
if m_ind.astype(np.int).sum() > 60:
if m_ind.astype(int).sum() > 60:
temp_crop[m_ind] = r_image[m_ind]
#print(sample_labels[sel_ind])
#print(sample_images[sel_ind].shape)
Expand Down Expand Up @@ -1283,7 +1283,7 @@ def extract_boxes(path='../coco/'): # from utils.datasets import *; extract_box
b = x[1:] * [w, h, w, h] # box
# b[2:] = b[2:].max() # rectangle to square
b[2:] = b[2:] * 1.2 + 3 # pad
b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int)
b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(int)

b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image
b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
Expand Down
4 changes: 2 additions & 2 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def labels_to_class_weights(labels, nc=80):
return torch.Tensor()

labels = np.concatenate(labels, 0) # labels.shape = (866643, 5) for COCO
classes = labels[:, 0].astype(np.int) # labels = [class xywh]
classes = labels[:, 0].astype(int) # labels = [class xywh]
weights = np.bincount(classes, minlength=nc) # occurrences per class

# Prepend gridpoint count (for uCE training)
Expand All @@ -234,7 +234,7 @@ def labels_to_class_weights(labels, nc=80):

def labels_to_image_weights(labels, nc=80, class_weights=np.ones(80)):
# Produces image weights based on class_weights and image contents
class_counts = np.array([np.bincount(x[:, 0].astype(np.int), minlength=nc) for x in labels])
class_counts = np.array([np.bincount(x[:, 0].astype(int), minlength=nc) for x in labels])
image_weights = (class_weights.reshape(1, nc) * class_counts).sum(1)
# index = random.choices(range(n), weights=image_weights, k=1) # weight image sample
return image_weights
Expand Down
4 changes: 2 additions & 2 deletions utils/google_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def attempt_download(file, repo='WongKinYiu/yolov7'):
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'
tag = 'v0.1'# 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]
tag = 'v0.1'#subprocess.check_output('git tag', shell=True).decode().split()[-1]

name = file.name
if name in assets:
Expand Down
3 changes: 2 additions & 1 deletion utils/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ def build_targets(self, p, targets, imgs):
all_gj.append(gj)
all_gi.append(gi)
all_anch.append(anch[i][idx])
from_which_layer.append(torch.ones(size=(len(b),)) * i)
from_which_layer.append((torch.ones(size=(len(b),)) * i).to('cuda'))

fg_pred = pi[b, a, gj, gi]
p_obj.append(fg_pred[:, 4:5])
Expand Down Expand Up @@ -754,6 +754,7 @@ def build_targets(self, p, targets, imgs):
matching_matrix[:, anchor_matching_gt > 1] *= 0.0
matching_matrix[cost_argmin, anchor_matching_gt > 1] = 1.0
fg_mask_inboxes = matching_matrix.sum(0) > 0.0
fg_mask_inboxes = fg_mask_inboxes.to(torch.device('cuda'))
matched_gt_inds = matching_matrix[:, fg_mask_inboxes].argmax(0)

from_which_layer = from_which_layer[fg_mask_inboxes]
Expand Down