Fix float zeros format (#5491)
Browse files* Fix float zeros format
* 255 to integer
- detect.py +1 -1
- export.py +1 -1
- models/common.py +2 -2
- models/experimental.py +1 -1
- models/tf.py +3 -3
- models/yolo.py +2 -2
- train.py +4 -4
- utils/activations.py +1 -1
- utils/augmentations.py +1 -1
- utils/autoanchor.py +5 -5
- utils/datasets.py +4 -4
- utils/general.py +1 -1
- utils/loss.py +5 -5
- utils/plots.py +1 -1
- utils/torch_utils.py +3 -3
- val.py +1 -1
detect.py
CHANGED
@@ -136,7 +136,7 @@ def run(weights=ROOT / 'yolov5s.pt', # model.pt path(s)
|
|
136 |
else:
|
137 |
img = torch.from_numpy(img).to(device)
|
138 |
img = img.half() if half else img.float() # uint8 to fp16/32
|
139 |
-
img /= 255
|
140 |
if len(img.shape) == 3:
|
141 |
img = img[None] # expand for batch dim
|
142 |
t2 = time_sync()
|
|
|
136 |
else:
|
137 |
img = torch.from_numpy(img).to(device)
|
138 |
img = img.half() if half else img.float() # uint8 to fp16/32
|
139 |
+
img /= 255 # 0 - 255 to 0.0 - 1.0
|
140 |
if len(img.shape) == 3:
|
141 |
img = img[None] # expand for batch dim
|
142 |
t2 = time_sync()
|
export.py
CHANGED
@@ -117,7 +117,7 @@ def export_coreml(model, im, file, prefix=colorstr('CoreML:')):
|
|
117 |
|
118 |
model.train() # CoreML exports should be placed in model.train() mode
|
119 |
ts = torch.jit.trace(model, im, strict=False) # TorchScript model
|
120 |
-
ct_model = ct.convert(ts, inputs=[ct.ImageType('image', shape=im.shape, scale=1 / 255
|
121 |
ct_model.save(f)
|
122 |
|
123 |
LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
|
|
|
117 |
|
118 |
model.train() # CoreML exports should be placed in model.train() mode
|
119 |
ts = torch.jit.trace(model, im, strict=False) # TorchScript model
|
120 |
+
ct_model = ct.convert(ts, inputs=[ct.ImageType('image', shape=im.shape, scale=1 / 255, bias=[0, 0, 0])])
|
121 |
ct_model.save(f)
|
122 |
|
123 |
LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
|
models/common.py
CHANGED
@@ -339,7 +339,7 @@ class AutoShape(nn.Module):
|
|
339 |
x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs] # pad
|
340 |
x = np.stack(x, 0) if n > 1 else x[0][None] # stack
|
341 |
x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW
|
342 |
-
x = torch.from_numpy(x).to(p.device).type_as(p) / 255
|
343 |
t.append(time_sync())
|
344 |
|
345 |
with amp.autocast(enabled=p.device.type != 'cpu'):
|
@@ -362,7 +362,7 @@ class Detections:
|
|
362 |
def __init__(self, imgs, pred, files, times=None, names=None, shape=None):
|
363 |
super().__init__()
|
364 |
d = pred[0].device # device
|
365 |
-
gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1
|
366 |
self.imgs = imgs # list of images as numpy arrays
|
367 |
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
|
368 |
self.names = names # class names
|
|
|
339 |
x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs] # pad
|
340 |
x = np.stack(x, 0) if n > 1 else x[0][None] # stack
|
341 |
x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW
|
342 |
+
x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32
|
343 |
t.append(time_sync())
|
344 |
|
345 |
with amp.autocast(enabled=p.device.type != 'cpu'):
|
|
|
362 |
def __init__(self, imgs, pred, files, times=None, names=None, shape=None):
|
363 |
super().__init__()
|
364 |
d = pred[0].device # device
|
365 |
+
gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device=d) for im in imgs] # normalizations
|
366 |
self.imgs = imgs # list of images as numpy arrays
|
367 |
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
|
368 |
self.names = names # class names
|
models/experimental.py
CHANGED
@@ -32,7 +32,7 @@ class Sum(nn.Module):
|
|
32 |
self.weight = weight # apply weights boolean
|
33 |
self.iter = range(n - 1) # iter object
|
34 |
if weight:
|
35 |
-
self.w = nn.Parameter(-torch.arange(1
|
36 |
|
37 |
def forward(self, x):
|
38 |
y = x[0] # no weight
|
|
|
32 |
self.weight = weight # apply weights boolean
|
33 |
self.iter = range(n - 1) # iter object
|
34 |
if weight:
|
35 |
+
self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights
|
36 |
|
37 |
def forward(self, x):
|
38 |
y = x[0] # no weight
|
models/tf.py
CHANGED
@@ -98,7 +98,7 @@ class TFFocus(keras.layers.Layer):
|
|
98 |
self.conv = TFConv(c1 * 4, c2, k, s, p, g, act, w.conv)
|
99 |
|
100 |
def call(self, inputs): # x(b,w,h,c) -> y(b,w/2,h/2,4c)
|
101 |
-
# inputs = inputs / 255
|
102 |
return self.conv(tf.concat([inputs[:, ::2, ::2, :],
|
103 |
inputs[:, 1::2, ::2, :],
|
104 |
inputs[:, ::2, 1::2, :],
|
@@ -227,7 +227,7 @@ class TFDetect(keras.layers.Layer):
|
|
227 |
|
228 |
if not self.training: # inference
|
229 |
y = tf.sigmoid(x[i])
|
230 |
-
xy = (y[..., 0:2] * 2
|
231 |
wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]
|
232 |
# Normalize xywh to 0-1 to reduce calibration error
|
233 |
xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
|
@@ -414,7 +414,7 @@ def representative_dataset_gen(dataset, ncalib=100):
|
|
414 |
for n, (path, img, im0s, vid_cap, string) in enumerate(dataset):
|
415 |
input = np.transpose(img, [1, 2, 0])
|
416 |
input = np.expand_dims(input, axis=0).astype(np.float32)
|
417 |
-
input /= 255
|
418 |
yield [input]
|
419 |
if n >= ncalib:
|
420 |
break
|
|
|
98 |
self.conv = TFConv(c1 * 4, c2, k, s, p, g, act, w.conv)
|
99 |
|
100 |
def call(self, inputs): # x(b,w,h,c) -> y(b,w/2,h/2,4c)
|
101 |
+
# inputs = inputs / 255 # normalize 0-255 to 0-1
|
102 |
return self.conv(tf.concat([inputs[:, ::2, ::2, :],
|
103 |
inputs[:, 1::2, ::2, :],
|
104 |
inputs[:, ::2, 1::2, :],
|
|
|
227 |
|
228 |
if not self.training: # inference
|
229 |
y = tf.sigmoid(x[i])
|
230 |
+
xy = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy
|
231 |
wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]
|
232 |
# Normalize xywh to 0-1 to reduce calibration error
|
233 |
xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
|
|
|
414 |
for n, (path, img, im0s, vid_cap, string) in enumerate(dataset):
|
415 |
input = np.transpose(img, [1, 2, 0])
|
416 |
input = np.expand_dims(input, axis=0).astype(np.float32)
|
417 |
+
input /= 255
|
418 |
yield [input]
|
419 |
if n >= ncalib:
|
420 |
break
|
models/yolo.py
CHANGED
@@ -60,10 +60,10 @@ class Detect(nn.Module):
|
|
60 |
|
61 |
y = x[i].sigmoid()
|
62 |
if self.inplace:
|
63 |
-
y[..., 0:2] = (y[..., 0:2] * 2
|
64 |
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
65 |
else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
|
66 |
-
xy = (y[..., 0:2] * 2
|
67 |
wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
68 |
y = torch.cat((xy, wh, y[..., 4:]), -1)
|
69 |
z.append(y.view(bs, -1, self.no))
|
|
|
60 |
|
61 |
y = x[i].sigmoid()
|
62 |
if self.inplace:
|
63 |
+
y[..., 0:2] = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy
|
64 |
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
65 |
else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
|
66 |
+
xy = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy
|
67 |
wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
68 |
y = torch.cat((xy, wh, y[..., 4:]), -1)
|
69 |
z.append(y.view(bs, -1, self.no))
|
train.py
CHANGED
@@ -246,9 +246,9 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
|
246 |
|
247 |
# Model parameters
|
248 |
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
249 |
-
hyp['box'] *= 3
|
250 |
-
hyp['cls'] *= nc / 80
|
251 |
-
hyp['obj'] *= (imgsz / 640) ** 2 * 3
|
252 |
hyp['label_smoothing'] = opt.label_smoothing
|
253 |
model.nc = nc # attach number of classes to model
|
254 |
model.hyp = hyp # attach hyperparameters to model
|
@@ -293,7 +293,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
|
293 |
optimizer.zero_grad()
|
294 |
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
|
295 |
ni = i + nb * epoch # number integrated batches (since train start)
|
296 |
-
imgs = imgs.to(device, non_blocking=True).float() / 255
|
297 |
|
298 |
# Warmup
|
299 |
if ni <= nw:
|
|
|
246 |
|
247 |
# Model parameters
|
248 |
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
249 |
+
hyp['box'] *= 3 / nl # scale to layers
|
250 |
+
hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
|
251 |
+
hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
|
252 |
hyp['label_smoothing'] = opt.label_smoothing
|
253 |
model.nc = nc # attach number of classes to model
|
254 |
model.hyp = hyp # attach hyperparameters to model
|
|
|
293 |
optimizer.zero_grad()
|
294 |
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
|
295 |
ni = i + nb * epoch # number integrated batches (since train start)
|
296 |
+
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
|
297 |
|
298 |
# Warmup
|
299 |
if ni <= nw:
|
utils/activations.py
CHANGED
@@ -19,7 +19,7 @@ class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
|
|
19 |
@staticmethod
|
20 |
def forward(x):
|
21 |
# return x * F.hardsigmoid(x) # for torchscript and CoreML
|
22 |
-
return x * F.hardtanh(x + 3, 0
|
23 |
|
24 |
|
25 |
# Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
|
|
|
19 |
@staticmethod
|
20 |
def forward(x):
|
21 |
# return x * F.hardsigmoid(x) # for torchscript and CoreML
|
22 |
+
return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for torchscript, CoreML and ONNX
|
23 |
|
24 |
|
25 |
# Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
|
utils/augmentations.py
CHANGED
@@ -124,7 +124,7 @@ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleF
|
|
124 |
|
125 |
def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
|
126 |
border=(0, 0)):
|
127 |
-
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
|
128 |
# targets = [cls, xyxy]
|
129 |
|
130 |
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
|
|
|
124 |
|
125 |
def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
|
126 |
border=(0, 0)):
|
127 |
+
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10))
|
128 |
# targets = [cls, xyxy]
|
129 |
|
130 |
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
|
utils/autoanchor.py
CHANGED
@@ -34,10 +34,10 @@ def check_anchors(dataset, model, thr=4.0, imgsz=640):
|
|
34 |
|
35 |
def metric(k): # compute metric
|
36 |
r = wh[:, None] / k[None]
|
37 |
-
x = torch.min(r, 1
|
38 |
best = x.max(1)[0] # best_x
|
39 |
-
aat = (x > 1
|
40 |
-
bpr = (best > 1
|
41 |
return bpr, aat
|
42 |
|
43 |
anchors = m.anchors.clone() * m.stride.to(m.anchors.device).view(-1, 1, 1) # current anchors
|
@@ -80,12 +80,12 @@ def kmean_anchors(dataset='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen
|
|
80 |
"""
|
81 |
from scipy.cluster.vq import kmeans
|
82 |
|
83 |
-
thr = 1
|
84 |
prefix = colorstr('autoanchor: ')
|
85 |
|
86 |
def metric(k, wh): # compute metrics
|
87 |
r = wh[:, None] / k[None]
|
88 |
-
x = torch.min(r, 1
|
89 |
# x = wh_iou(wh, torch.tensor(k)) # iou metric
|
90 |
return x, x.max(1)[0] # x, best_x
|
91 |
|
|
|
34 |
|
35 |
def metric(k): # compute metric
|
36 |
r = wh[:, None] / k[None]
|
37 |
+
x = torch.min(r, 1 / r).min(2)[0] # ratio metric
|
38 |
best = x.max(1)[0] # best_x
|
39 |
+
aat = (x > 1 / thr).float().sum(1).mean() # anchors above threshold
|
40 |
+
bpr = (best > 1 / thr).float().mean() # best possible recall
|
41 |
return bpr, aat
|
42 |
|
43 |
anchors = m.anchors.clone() * m.stride.to(m.anchors.device).view(-1, 1, 1) # current anchors
|
|
|
80 |
"""
|
81 |
from scipy.cluster.vq import kmeans
|
82 |
|
83 |
+
thr = 1 / thr
|
84 |
prefix = colorstr('autoanchor: ')
|
85 |
|
86 |
def metric(k, wh): # compute metrics
|
87 |
r = wh[:, None] / k[None]
|
88 |
+
x = torch.min(r, 1 / r).min(2)[0] # ratio metric
|
89 |
# x = wh_iou(wh, torch.tensor(k)) # iou metric
|
90 |
return x, x.max(1)[0] # x, best_x
|
91 |
|
utils/datasets.py
CHANGED
@@ -634,13 +634,13 @@ class LoadImagesAndLabels(Dataset):
|
|
634 |
n = len(shapes) // 4
|
635 |
img4, label4, path4, shapes4 = [], [], path[:n], shapes[:n]
|
636 |
|
637 |
-
ho = torch.tensor([[0
|
638 |
-
wo = torch.tensor([[0
|
639 |
-
s = torch.tensor([[1, 1, .5, .5, .5, .5]]) # scale
|
640 |
for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW
|
641 |
i *= 4
|
642 |
if random.random() < 0.5:
|
643 |
-
im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2
|
644 |
0].type(img[i].type())
|
645 |
l = label[i]
|
646 |
else:
|
|
|
634 |
n = len(shapes) // 4
|
635 |
img4, label4, path4, shapes4 = [], [], path[:n], shapes[:n]
|
636 |
|
637 |
+
ho = torch.tensor([[0.0, 0, 0, 1, 0, 0]])
|
638 |
+
wo = torch.tensor([[0.0, 0, 1, 0, 0, 0]])
|
639 |
+
s = torch.tensor([[1, 1, 0.5, 0.5, 0.5, 0.5]]) # scale
|
640 |
for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW
|
641 |
i *= 4
|
642 |
if random.random() < 0.5:
|
643 |
+
im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2.0, mode='bilinear', align_corners=False)[
|
644 |
0].type(img[i].type())
|
645 |
l = label[i]
|
646 |
else:
|
utils/general.py
CHANGED
@@ -802,7 +802,7 @@ def apply_classifier(x, model, img, im0):
|
|
802 |
|
803 |
im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
|
804 |
im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32
|
805 |
-
im /= 255
|
806 |
ims.append(im)
|
807 |
|
808 |
pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction
|
|
|
802 |
|
803 |
im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
|
804 |
im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32
|
805 |
+
im /= 255 # 0 - 255 to 0.0 - 1.0
|
806 |
ims.append(im)
|
807 |
|
808 |
pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction
|
utils/loss.py
CHANGED
@@ -108,7 +108,7 @@ class ComputeLoss:
|
|
108 |
BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)
|
109 |
|
110 |
det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module
|
111 |
-
self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, .02]) # P3-P7
|
112 |
self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index
|
113 |
self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, 1.0, h, autobalance
|
114 |
for k in 'na', 'nc', 'nl', 'anchors':
|
@@ -129,7 +129,7 @@ class ComputeLoss:
|
|
129 |
ps = pi[b, a, gj, gi] # prediction subset corresponding to targets
|
130 |
|
131 |
# Regression
|
132 |
-
pxy = ps[:, :2].sigmoid() * 2
|
133 |
pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]
|
134 |
pbox = torch.cat((pxy, pwh), 1) # predicted box
|
135 |
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target)
|
@@ -189,15 +189,15 @@ class ComputeLoss:
|
|
189 |
if nt:
|
190 |
# Matches
|
191 |
r = t[:, :, 4:6] / anchors[:, None] # wh ratio
|
192 |
-
j = torch.max(r, 1
|
193 |
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
|
194 |
t = t[j] # filter
|
195 |
|
196 |
# Offsets
|
197 |
gxy = t[:, 2:4] # grid xy
|
198 |
gxi = gain[[2, 3]] - gxy # inverse
|
199 |
-
j, k = ((gxy % 1
|
200 |
-
l, m = ((gxi % 1
|
201 |
j = torch.stack((torch.ones_like(j), j, k, l, m))
|
202 |
t = t.repeat((5, 1, 1))[j]
|
203 |
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
|
|
|
108 |
BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)
|
109 |
|
110 |
det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module
|
111 |
+
self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7
|
112 |
self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index
|
113 |
self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, 1.0, h, autobalance
|
114 |
for k in 'na', 'nc', 'nl', 'anchors':
|
|
|
129 |
ps = pi[b, a, gj, gi] # prediction subset corresponding to targets
|
130 |
|
131 |
# Regression
|
132 |
+
pxy = ps[:, :2].sigmoid() * 2 - 0.5
|
133 |
pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]
|
134 |
pbox = torch.cat((pxy, pwh), 1) # predicted box
|
135 |
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target)
|
|
|
189 |
if nt:
|
190 |
# Matches
|
191 |
r = t[:, :, 4:6] / anchors[:, None] # wh ratio
|
192 |
+
j = torch.max(r, 1 / r).max(2)[0] < self.hyp['anchor_t'] # compare
|
193 |
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
|
194 |
t = t[j] # filter
|
195 |
|
196 |
# Offsets
|
197 |
gxy = t[:, 2:4] # grid xy
|
198 |
gxi = gain[[2, 3]] - gxy # inverse
|
199 |
+
j, k = ((gxy % 1 < g) & (gxy > 1)).T
|
200 |
+
l, m = ((gxi % 1 < g) & (gxi > 1)).T
|
201 |
j = torch.stack((torch.ones_like(j), j, k, l, m))
|
202 |
t = t.repeat((5, 1, 1))[j]
|
203 |
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
|
utils/plots.py
CHANGED
@@ -155,7 +155,7 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max
|
|
155 |
if isinstance(targets, torch.Tensor):
|
156 |
targets = targets.cpu().numpy()
|
157 |
if np.max(images[0]) <= 1:
|
158 |
-
images *= 255
|
159 |
bs, _, h, w = images.shape # batch size, _, height, width
|
160 |
bs = min(bs, max_subplots) # limit plot images
|
161 |
ns = np.ceil(bs ** 0.5) # number of subplots (square)
|
|
|
155 |
if isinstance(targets, torch.Tensor):
|
156 |
targets = targets.cpu().numpy()
|
157 |
if np.max(images[0]) <= 1:
|
158 |
+
images *= 255 # de-normalise (optional)
|
159 |
bs, _, h, w = images.shape # batch size, _, height, width
|
160 |
bs = min(bs, max_subplots) # limit plot images
|
161 |
ns = np.ceil(bs ** 0.5) # number of subplots (square)
|
utils/torch_utils.py
CHANGED
@@ -111,7 +111,7 @@ def profile(input, ops, n=10, device=None):
|
|
111 |
for m in ops if isinstance(ops, list) else [ops]:
|
112 |
m = m.to(device) if hasattr(m, 'to') else m # device
|
113 |
m = m.half() if hasattr(m, 'half') and isinstance(x, torch.Tensor) and x.dtype is torch.float16 else m
|
114 |
-
tf, tb, t = 0
|
115 |
try:
|
116 |
flops = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # GFLOPs
|
117 |
except:
|
@@ -177,7 +177,7 @@ def find_modules(model, mclass=nn.Conv2d):
|
|
177 |
|
178 |
def sparsity(model):
|
179 |
# Return global model sparsity
|
180 |
-
a, b = 0
|
181 |
for p in model.parameters():
|
182 |
a += p.numel()
|
183 |
b += (p == 0).sum()
|
@@ -336,7 +336,7 @@ class ModelEMA:
|
|
336 |
for k, v in self.ema.state_dict().items():
|
337 |
if v.dtype.is_floating_point:
|
338 |
v *= d
|
339 |
-
v += (1
|
340 |
|
341 |
def update_attr(self, model, include=(), exclude=('process_group', 'reducer')):
|
342 |
# Update EMA attributes
|
|
|
111 |
for m in ops if isinstance(ops, list) else [ops]:
|
112 |
m = m.to(device) if hasattr(m, 'to') else m # device
|
113 |
m = m.half() if hasattr(m, 'half') and isinstance(x, torch.Tensor) and x.dtype is torch.float16 else m
|
114 |
+
tf, tb, t = 0, 0, [0, 0, 0] # dt forward, backward
|
115 |
try:
|
116 |
flops = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # GFLOPs
|
117 |
except:
|
|
|
177 |
|
178 |
def sparsity(model):
|
179 |
# Return global model sparsity
|
180 |
+
a, b = 0, 0
|
181 |
for p in model.parameters():
|
182 |
a += p.numel()
|
183 |
b += (p == 0).sum()
|
|
|
336 |
for k, v in self.ema.state_dict().items():
|
337 |
if v.dtype.is_floating_point:
|
338 |
v *= d
|
339 |
+
v += (1 - d) * msd[k].detach()
|
340 |
|
341 |
def update_attr(self, model, include=(), exclude=('process_group', 'reducer')):
|
342 |
# Update EMA attributes
|
val.py
CHANGED
@@ -164,7 +164,7 @@ def run(data,
|
|
164 |
t1 = time_sync()
|
165 |
img = img.to(device, non_blocking=True)
|
166 |
img = img.half() if half else img.float() # uint8 to fp16/32
|
167 |
-
img /= 255
|
168 |
targets = targets.to(device)
|
169 |
nb, _, height, width = img.shape # batch size, channels, height, width
|
170 |
t2 = time_sync()
|
|
|
164 |
t1 = time_sync()
|
165 |
img = img.to(device, non_blocking=True)
|
166 |
img = img.half() if half else img.float() # uint8 to fp16/32
|
167 |
+
img /= 255 # 0 - 255 to 0.0 - 1.0
|
168 |
targets = targets.to(device)
|
169 |
nb, _, height, width = img.shape # batch size, channels, height, width
|
170 |
t2 = time_sync()
|