pull/2109/head
hanoch 2024-08-19 15:56:20 +03:00
parent c8c3363926
commit 18086f7c35
4 changed files with 27 additions and 8 deletions

11
test.py
View File

@ -103,15 +103,18 @@ def test(data,
jdict, stats, ap, ap_class, wandb_images = [], [], [], [], []
for batch_i, (img, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)):
img = img.to(device, non_blocking=True)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if 1:
assert (img.shape[-1] == 640)
img = img.half() if half else img.float()
# uint8 to fp16/32
# img /= 255.0 # 0 - 255 to 0.0 - 1.0 c# already done inside dataloader
targets = targets.to(device)
nb, _, height, width = img.shape # batch size, channels, height, width
with torch.no_grad():
# Run model
t = time_synchronized()
out, train_out = model(img, augment=augment) # inference and training outputs
out, train_out = model(img, augment=augment) # inference(4 coordination, obj conf, cls conf ) and training outputs(un normalized coordination in yolo format and 3 scales diferent outputs) (2,2,80,80,7), (2,2,40,40,7) : 640/8=40
t0 += time_synchronized() - t
# Compute loss
@ -139,7 +142,7 @@ def test(data,
continue
# Predictions
predn = pred.clone()
predn = pred.clone() # *xyxy, conf, cls in predn [x y ,w ,h, conf, cls]
scale_coords(img[si].shape[1:], predn[:, :4], shapes[si][0], shapes[si][1]) # native-space pred
# Append to text file

View File

@ -393,7 +393,7 @@ def train(hyp, opt, device, tb_writer=None):
# Plot
if plots and ni < 10:
f = save_dir / f'train_batch{ni}.jpg' # filename
Thread(target=plot_images, args=(imgs, targets, paths, f), daemon=True).start()
Thread(target=plot_images, args=(imgs, targets, paths, f, opt.input_channels), daemon=True).start()
# if tb_writer:
# tb_writer.add_image(f, result, dataformats='HWC', global_step=epoch)
# tb_writer.add_graph(torch.jit.trace(model, imgs, strict=False), []) # add model graph
@ -418,6 +418,7 @@ def train(hyp, opt, device, tb_writer=None):
results, maps, times = test.test(data_dict,
batch_size=batch_size * 2,
imgsz=imgsz_test,
save_json=True,
model=ema.ema,
single_cls=opt.single_cls,
dataloader=testloader,
@ -715,7 +716,15 @@ if __name__ == '__main__':
"""
TODO
Anchors,
hyp['anchor_t'] = 4 let the AR<=4 => TODO check if valid
Ive reduced anchors to 2 per anchors: 2
Sampler : torch_weighted : WeightedRandomSampler
python train.py --workers 8 --device 'cpu' --batch-size 32 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights 'v7' --name yolov7 --hyp data/hyp.scratch.p5.yaml
--workers 8 --device cpu --batch-size 32 --data data/tir_od.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights 'v7' --name yolov7 --cache-images --hyp data/hyp.tir_od.tiny.yaml --adam --norm-type single_image_percentile_0_1
--workers 8 --device cpu --batch-size 32 --data data/tir_od.yaml --img 640 640 --cfg cfg/training/yolov7-tiny.yaml --weights 'v7' --name yolov7 --cache-images --hyp data/hyp.tir_od.tiny.yaml --adam --norm-type single_image_percentile_0_1 --input-channels 1 --multi-scale
--multi-scale training with resized image resolution not good for TIR
"""

View File

@ -335,7 +335,7 @@ def train(hyp, opt, device, tb_writer=None):
optimizer.zero_grad()
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
ni = i + nb * epoch # number integrated batches (since train start)
imgs = imgs.to(device, non_blocking=True).float() / 255.0 # uint8 to float32, 0-255 to 0.0-1.0
imgs = imgs.to(device, non_blocking=True).float() #/ 255.0 # uint8 to float32, 0-255 to 0.0-1.0
# Warmup
if ni <= nw:

View File

@ -111,7 +111,7 @@ def output_to_target(output):
return np.array(targets)
def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=640, max_subplots=16):
def plot_images(images, targets, paths=None, fname='images.jpg', input_channels=3, names=None, max_size=640, max_subplots=16):
# Plot image grid with labels
if isinstance(images, torch.Tensor):
@ -148,7 +148,14 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max
if scale_factor < 1:
img = cv2.resize(img, (w, h))
mosaic[block_y:block_y + h, block_x:block_x + w, :] = img
if img.ndim > 2: # GL no permute
# Convert
mosaic[block_y:block_y + h, block_x:block_x + w, :] = img
else:
# img = img[np.newaxis,...] # unsqueeze
mosaic[block_y:block_y + h, block_x:block_x + w, :] = np.repeat(img[np.newaxis, :, :], 3, axis=0).transpose(1, 2, 0)
if len(targets) > 0:
image_targets = targets[targets[:, 0] == i]
boxes = xywh2xyxy(image_targets[:, 2:6]).T