diff --git a/tests/imageid/imageid.py b/tests/imageid/imageid.py index 83bfe505..c5868966 100644 --- a/tests/imageid/imageid.py +++ b/tests/imageid/imageid.py @@ -14,7 +14,8 @@ detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( - os.path.join(execution_path, "./testing/resnet50_coco_best_v2.0.1.h5")) + os.path.join(execution_path, "./testing/resnet50_coco_best_v2.0.1.h5") +) detector.loadModel() custom_objects = detector.CustomObjects( person=True, diff --git a/tests/imageid/test.py b/tests/imageid/test.py index c3a72307..c5868966 100644 --- a/tests/imageid/test.py +++ b/tests/imageid/test.py @@ -4,6 +4,7 @@ argument -- description Return: return_description """ + import os from imageai.Detection import ObjectDetection @@ -13,7 +14,8 @@ detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( - os.path.join(execution_path, "./testing/resnet50_coco_best_v2.0.1.h5")) + os.path.join(execution_path, "./testing/resnet50_coco_best_v2.0.1.h5") +) detector.loadModel() custom_objects = detector.CustomObjects( person=True, diff --git a/tests/open_images/0.py b/tests/open_images/0.py index 4b2daa66..46d79776 100644 --- a/tests/open_images/0.py +++ b/tests/open_images/0.py @@ -1,8 +1,8 @@ info = data_other.iloc[data_idx] # getting the info of the index img = cv2.imread(f'./Img/{info["Path"]}') # reading the img -height, width = cv2.imread( - "./Img/" + - info["Path"]).shape[:2] # getting the height and width of the image +height, width = cv2.imread("./Img/" + info["Path"]).shape[ + :2 +] # getting the height and width of the image xmin, ymin, xmax, ymax = ( info["XMin"], info["YMin"], @@ -19,6 +19,5 @@ w = xmax - xmin h = ymax - ymin x, y, w, h = round(x), round(y), round(w), round(h) -roi = img[y:y + h, x:x + w] # crop the image -cv2.rectangle(img, (x, y), (x + w, y + h), (200, 0, 0), - 10) # draw box around the bbox +roi = img[y : y + h, x : x + w] # crop the image +cv2.rectangle(img, (x, y), (x + w, y + h), (200, 0, 0), 10) # draw box around the bbox diff --git a/tests/open_images/find_card_v1.py b/tests/open_images/find_card_v1.py index 4c722cdc..4b138673 100644 --- a/tests/open_images/find_card_v1.py +++ b/tests/open_images/find_card_v1.py @@ -21,7 +21,8 @@ sat = hsv[:, :, 1] # Convert the hsv Image to GrayScale cv2.imwrite("sat.png", sat) # Saving the Sat thresh = cv2.threshold(sat, 15, 255, cv2.THRESH_BINARY)[ - 1] # The Outline of the Sat which is the GrayScale Img + 1 + ] # The Outline of the Sat which is the GrayScale Img cv2.imwrite("thresh.png", thresh) # Saving the thresh thresh = 255 - thresh # Converts Black to White and White to Black cv2.imwrite("thresh-2.png", thresh) # Saving the thresh @@ -42,19 +43,20 @@ im, cv2.COLOR_BGR2GRAY ) # Conver the black and white image to a image like https://i.stack.imgur.com/UPOZC.png cv2.imwrite("gray.png", gray) # Saving the gray image - contours, _ = cv2.findContours( - gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2:] # Finding the boxes + contours, _ = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[ + -2: + ] # Finding the boxes for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) # Convert cnt to x,y,w,h if ( - w > 250 and h > 250 + w > 250 and h > 250 ): # Checking if the h and w of the image is higher than 175 so this will only get the card idx += 1 cv2.rectangle(img, (x, y), (x + w, y + h), (200, 0, 0)) img = cv2.imread(f"./Imgs/{file}") # get the original image img = cv2.resize(img, (2000, 2000)) - crop_img = img[y:y + h, x:x + w] # Cropping + crop_img = img[y : y + h, x : x + w] # Cropping if f"./Imgs/{file}" in list(data): data[f"{file}"]["X"].append(x) data[f"{file}"]["Y"].append(y) @@ -80,5 +82,5 @@ f"./Preds/{file}.jpeg", cv2.rectangle(img, (X, Y), (X + W, Y + H), (200, 0, 0)), ) # Saving the corped image - crop_img = img[Y:Y + H, X:X + W] # Cropping + crop_img = img[Y : Y + H, X : X + W] # Cropping cv2.imwrite(f"./Imgs/{key}", crop_img) # Saving the corped image diff --git a/tests/pytorch-lightning/Learning-PyTorch-Lightning-main/0.py b/tests/pytorch-lightning/Learning-PyTorch-Lightning-main/0.py index b0a980e8..1cef3156 100644 --- a/tests/pytorch-lightning/Learning-PyTorch-Lightning-main/0.py +++ b/tests/pytorch-lightning/Learning-PyTorch-Lightning-main/0.py @@ -46,25 +46,22 @@ def training_step(self, batch, batch_idx): @staticmethod def train_dataloader(): train_dataset = torchvision.datasets.MNIST( - root="./data", - train=True, - transform=transforms.ToTensor(), - download=True) - train_loader = torch.utils.data.DataLoader(dataset=train_dataset, - batch_size=batch_size, - num_workers=4, - shuffle=False) + root="./data", train=True, transform=transforms.ToTensor(), download=True + ) + train_loader = torch.utils.data.DataLoader( + dataset=train_dataset, batch_size=batch_size, num_workers=4, shuffle=False + ) return train_loader @staticmethod def val_dataloader(): test_dataset = torchvision.datasets.MNIST( - root="./data", train=False, transform=transforms.ToTensor()) + root="./data", train=False, transform=transforms.ToTensor() + ) - test_loader = torch.utils.data.DataLoader(dataset=test_dataset, - batch_size=batch_size, - num_workers=4, - shuffle=False) + test_loader = torch.utils.data.DataLoader( + dataset=test_dataset, batch_size=batch_size, num_workers=4, shuffle=False + ) return test_loader def validation_step(self, batch, batch_idx): diff --git a/tests/tflite/test.py b/tests/tflite/test.py index 53a7ff70..3296ed1f 100644 --- a/tests/tflite/test.py +++ b/tests/tflite/test.py @@ -4,6 +4,7 @@ argument -- description Return: return_description """ + import datetime import os diff --git a/tests/tflite/tflite.py b/tests/tflite/tflite.py index fdc3057f..2b4e912e 100644 --- a/tests/tflite/tflite.py +++ b/tests/tflite/tflite.py @@ -8,8 +8,7 @@ from tflite_model_maker import model_spec, object_detector from tflite_model_maker.config import ExportFormat, QuantizationConfig -print("Num GPUs Available: ", - len(tf.config.experimental.list_physical_devices("GPU"))) +print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices("GPU"))) class TFLite: @@ -29,8 +28,7 @@ def __init__( train_whole_model: bool = True, export_dir: str = ".", model_path: str = "model.tflite", - data_loading_csv: - str = "gs://cloud-ml-data/img/openimage/csv/salads_ml_use.csv", + data_loading_csv: str = "gs://cloud-ml-data/img/openimage/csv/salads_ml_use.csv", ) -> None: """sumary_line Keyword arguments: @@ -43,8 +41,7 @@ def __init__( self.train_whole_model = train_whole_model self.export_dir = export_dir self.data_loading_csv = data_loading_csv - self.log_dir = "logs/fit/" + datetime.datetime.now().strftime( - "%Y%m%d-%H%M%S") + self.log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") self.epochs = epochs self.threshold = 0.125 self.model_path = model_path @@ -136,21 +133,27 @@ def create_test_image(self, image_path): """ with tf.device("/GPU:0"): print("Creating test images") - _, input_height, input_width, _ = self.interpreter.get_input_details( - )[0]["shape"] + _, input_height, input_width, _ = self.interpreter.get_input_details()[0][ + "shape" + ] # Read image in tf encoded format img = tf.io.read_file(image_path) img = tf.io.decode_image( - img, channels=3) # Decode the image (Load the image) + img, channels=3 + ) # Decode the image (Load the image) img = tf.image.convert_image_dtype( - img, tf.uint8) # Convert to Data Type Unit8 + img, tf.uint8 + ) # Convert to Data Type Unit8 self.original_image = img self.resized_img = tf.image.resize( - img, (input_height, input_width)) # Resize Image + img, (input_height, input_width) + ) # Resize Image self.resized_img = self.resized_img[ - tf.newaxis, :] # Add 1 dimension to the image + tf.newaxis, : + ] # Add 1 dimension to the image self.resized_img = tf.cast( - self.resized_img, dtype=tf.uint8) # Convert to Data Type Unit8 + self.resized_img, dtype=tf.uint8 + ) # Convert to Data Type Unit8 def predict_test_image(self): """sumary_line diff --git a/tests/torch_object_detection/data_loader.py b/tests/torch_object_detection/data_loader.py index 0025b53c..6319ecff 100644 --- a/tests/torch_object_detection/data_loader.py +++ b/tests/torch_object_detection/data_loader.py @@ -68,13 +68,13 @@ def __getitem__(self, idx): # convert everything into a torch.Tensor boxes = torch.as_tensor(boxes, dtype=torch.float32) # there is only one class - labels = torch.ones((num_objs, ), dtype=torch.int64) + labels = torch.ones((num_objs,), dtype=torch.int64) masks = torch.as_tensor(masks, dtype=torch.uint8) image_id = torch.tensor([idx]) area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0]) # suppose all instances are not crowd - iscrowd = torch.zeros((num_objs, ), dtype=torch.int64) + iscrowd = torch.zeros((num_objs,), dtype=torch.int64) target = { "boxes": boxes, @@ -95,7 +95,6 @@ def __len__(self): return len(self.imgs) -dl = torch.utils.data.DataLoader(PennFudanDataset(), - batch_size=32, - shuffle=True, - num_workers=2) +dl = torch.utils.data.DataLoader( + PennFudanDataset(), batch_size=32, shuffle=True, num_workers=2 +) diff --git a/tests/yolo/yolo.py b/tests/yolo/yolo.py index 698363d6..abb89333 100644 --- a/tests/yolo/yolo.py +++ b/tests/yolo/yolo.py @@ -79,8 +79,9 @@ sys.path.append(str(ROOT)) # add ROOT to PATH ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative -LOCAL_RANK = int(os.getenv( - "LOCAL_RANK", -1)) # https://pytorch.org/docs/stable/elastic/run.html +LOCAL_RANK = int( + os.getenv("LOCAL_RANK", -1) +) # https://pytorch.org/docs/stable/elastic/run.html RANK = int(os.getenv("RANK", -1)) WORLD_SIZE = int(os.getenv("WORLD_SIZE", 1)) @@ -126,8 +127,8 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary with open(hyp, errors="ignore") as f: hyp = yaml.safe_load(f) # load hyps dict LOGGER.info( - colorstr("hyperparameters: ") + ", ".join(f"{k}={v}" - for k, v in hyp.items())) + colorstr("hyperparameters: ") + ", ".join(f"{k}={v}" for k, v in hyp.items()) + ) # Save run settings if not evolve: @@ -139,8 +140,7 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary # Loggers data_dict = None if RANK in [-1, 0]: - loggers = Loggers(save_dir, weights, opt, hyp, - LOGGER) # loggers instance + loggers = Loggers(save_dir, weights, opt, hyp, LOGGER) # loggers instance if loggers.wandb: data_dict = loggers.wandb.data_dict if resume: @@ -163,13 +163,14 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary data_dict = data_dict or check_dataset(data) # check if None train_path, val_path = data_dict["train"], data_dict["val"] nc = 1 if single_cls else int(data_dict["nc"]) # number of classes - names = (["item"] if single_cls and len(data_dict["names"]) != 1 else - data_dict["names"]) # class names + names = ( + ["item"] if single_cls and len(data_dict["names"]) != 1 else data_dict["names"] + ) # class names if len(names) != nc: - raise AssertionError( - f"{len(names)} names found for nc={nc} dataset in {data}") + raise AssertionError(f"{len(names)} names found for nc={nc} dataset in {data}") is_coco = isinstance(val_path, str) and val_path.endswith( - "coco/val2017.txt") # COCO dataset + "coco/val2017.txt" + ) # COCO dataset # Model check_suffix(weights, ".pt") # check weights @@ -178,31 +179,30 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary with torch_distributed_zero_first(LOCAL_RANK): # download if not found locally weights = attempt_download(weights) - ckpt = torch.load(weights, map_location="cpu" - ) # load checkpoint to CPU to avoid CUDA memory leak - model = Model(cfg or ckpt["model"].yaml, - ch=3, - nc=nc, - anchors=hyp.get("anchors")).to(device) # create - exclude = (["anchor"] if - (cfg or hyp.get("anchors")) and not resume else [] - ) # exclude keys + ckpt = torch.load( + weights, map_location="cpu" + ) # load checkpoint to CPU to avoid CUDA memory leak + model = Model( + cfg or ckpt["model"].yaml, ch=3, nc=nc, anchors=hyp.get("anchors") + ).to( + device + ) # create + exclude = ( + ["anchor"] if (cfg or hyp.get("anchors")) and not resume else [] + ) # exclude keys # checkpoint state_dict as FP32 csd = ckpt["model"].float().state_dict() - csd = intersect_dicts(csd, model.state_dict(), - exclude=exclude) # intersect + csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect model.load_state_dict(csd, strict=False) # load LOGGER.info( f"Transferred {len(csd)}/{len(model.state_dict())} items from {weights}" ) # report else: - model = Model(cfg, ch=3, nc=nc, - anchors=hyp.get("anchors")).to(device) # create + model = Model(cfg, ch=3, nc=nc, anchors=hyp.get("anchors")).to(device) # create # Freeze freeze = [ - f"model.{x}." - for x in (freeze if len(freeze) > 1 else range(freeze[0])) + f"model.{x}." for x in (freeze if len(freeze) > 1 else range(freeze[0])) ] # layers to freeze for k, v in model.named_parameters(): v.requires_grad = True # train all layers @@ -234,41 +234,40 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary if isinstance(v, nn.BatchNorm2d): # weight (no decay) g0.append(v.weight) elif hasattr(v, "weight") and isinstance( - v.weight, nn.Parameter): # weight (with decay) + v.weight, nn.Parameter + ): # weight (with decay) g1.append(v.weight) if opt.optimizer == "Adam": - optimizer = Adam(g0, lr=hyp["lr0"], - betas=(hyp["momentum"], - 0.999)) # adjust beta1 to momentum + optimizer = Adam( + g0, lr=hyp["lr0"], betas=(hyp["momentum"], 0.999) + ) # adjust beta1 to momentum elif opt.optimizer == "AdamW": - optimizer = AdamW(g0, lr=hyp["lr0"], - betas=(hyp["momentum"], - 0.999)) # adjust beta1 to momentum + optimizer = AdamW( + g0, lr=hyp["lr0"], betas=(hyp["momentum"], 0.999) + ) # adjust beta1 to momentum else: - optimizer = SGD(g0, - lr=hyp["lr0"], - momentum=hyp["momentum"], - nesterov=True) - - optimizer.add_param_group({ - "params": g1, - "weight_decay": hyp["weight_decay"] - }) # add g1 with weight_decay + optimizer = SGD(g0, lr=hyp["lr0"], momentum=hyp["momentum"], nesterov=True) + + optimizer.add_param_group( + {"params": g1, "weight_decay": hyp["weight_decay"]} + ) # add g1 with weight_decay optimizer.add_param_group({"params": g2}) # add g2 (biases) LOGGER.info( f"{colorstr('optimizer:')} {type(optimizer).__name__} with parameter groups " - f"{len(g0)} weight (no decay), {len(g1)} weight, {len(g2)} bias") + f"{len(g0)} weight (no decay), {len(g1)} weight, {len(g2)} bias" + ) # Scheduler if opt.linear_lr: - lf = (lambda x: (1 - x / (epochs - 1)) * - (1.0 - hyp["lrf"]) + hyp["lrf"]) # linear + lf = ( + lambda x: (1 - x / (epochs - 1)) * (1.0 - hyp["lrf"]) + hyp["lrf"] + ) # linear else: lf = one_cycle(1, hyp["lrf"], epochs) # cosine 1->hyp['lrf'] scheduler = lr_scheduler.LambdaLR( - optimizer, - lr_lambda=lf) # plot_lr_scheduler(optimizer, scheduler, epochs) + optimizer, lr_lambda=lf + ) # plot_lr_scheduler(optimizer, scheduler, epochs) # EMA ema = ModelEMA(model) if RANK in [-1, 0] else None @@ -360,10 +359,7 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary # Anchors if not opt.noautoanchor: - check_anchors(dataset, - model=model, - thr=hyp["anchor_t"], - imgsz=imgsz) + check_anchors(dataset, model=model, thr=hyp["anchor_t"], imgsz=imgsz) model.half().float() # pre-reduce anchor precision callbacks.run("on_pretrain_routine_end") @@ -377,7 +373,7 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary nl = de_parallel(model).model[-1].nl hyp["box"] *= 3 / nl # scale to layers hyp["cls"] *= nc / 80 * 3 / nl # scale to classes and layers - hyp["obj"] *= (imgsz / 640)**2 * 3 / nl # scale to image size and layers + hyp["obj"] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers hyp["label_smoothing"] = opt.label_smoothing model.nc = nc # attach number of classes to model model.hyp = hyp # attach hyperparameters to model @@ -388,8 +384,9 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary # Start training t0 = time.time() - nw = max(round(hyp["warmup_epochs"] * nb), - 1000) # number of warmup iterations, max(3 epochs, 1k iterations) + nw = max( + round(hyp["warmup_epochs"] * nb), 1000 + ) # number of warmup iterations, max(3 epochs, 1k iterations) last_opt_step = -1 maps = np.zeros(nc) # mAP per class results = (0, 0, 0, 0, 0, 0, 0) @@ -401,22 +398,24 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary f"Image sizes {imgsz} train, {imgsz} val\n" f"Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n" f"Logging results to {colorstr('bold', save_dir)}\n" - f"Starting training for {epochs} epochs...") + f"Starting training for {epochs} epochs..." + ) for epoch in range( - start_epoch, epochs + start_epoch, epochs ): # epoch ------------------------------------------------------------------ model.train() # Update image weights (optional, single-GPU only) if opt.image_weights: - cw = (model.class_weights.cpu().numpy() * (1 - maps)**2 / nc - ) # class weights - iw = labels_to_image_weights(dataset.labels, - nc=nc, - class_weights=cw) # image weights - dataset.indices = random.choices(range(dataset.n), - weights=iw, - k=dataset.n) # rand weighted idx + cw = ( + model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc + ) # class weights + iw = labels_to_image_weights( + dataset.labels, nc=nc, class_weights=cw + ) # image weights + dataset.indices = random.choices( + range(dataset.n), weights=iw, k=dataset.n + ) # rand weighted idx # Update mosaic border (optional) # b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs) @@ -427,32 +426,32 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary train_loader.sampler.set_epoch(epoch) pbar = enumerate(train_loader) LOGGER.info( - ("\n" + "%10s" * 7) % - ("Epoch", "gpu_mem", "box", "obj", "cls", "labels", "img_size")) + ("\n" + "%10s" * 7) + % ("Epoch", "gpu_mem", "box", "obj", "cls", "labels", "img_size") + ) if RANK in [-1, 0]: pbar = tqdm( - pbar, total=nb, - bar_format="{l_bar}{bar:10}{r_bar}{bar:-10b}") # progress bar + pbar, total=nb, bar_format="{l_bar}{bar:10}{r_bar}{bar:-10b}" + ) # progress bar optimizer.zero_grad() for i, ( - imgs, - targets, - paths, - _, + imgs, + targets, + paths, + _, ) in ( - pbar + pbar ): # batch ------------------------------------------------------------- # number integrated batches (since train start) ni = i + nb * epoch - imgs = (imgs.to(device, non_blocking=True).float() / 255 - ) # uint8 to float32, 0-255 to 0.0-1.0 + imgs = ( + imgs.to(device, non_blocking=True).float() / 255 + ) # uint8 to float32, 0-255 to 0.0-1.0 # Warmup if ni <= nw: xi = [0, nw] # x interp - accumulate = max( - 1, - np.interp(ni, xi, [1, nbs / batch_size]).round()) + accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round()) for j, x in enumerate(optimizer.param_groups): # bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0 x["lr"] = np.interp( @@ -465,26 +464,27 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary ) if "momentum" in x: x["momentum"] = np.interp( - ni, xi, [hyp["warmup_momentum"], hyp["momentum"]]) + ni, xi, [hyp["warmup_momentum"], hyp["momentum"]] + ) # Multi-scale if opt.multi_scale: - sz = random.randrange(imgsz * 0.5, - imgsz * 1.5 + gs) // gs * gs # size + sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size sf = sz / max(imgs.shape[2:]) # scale factor if sf != 1: - ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:] - ] # new shape (stretched to gs-multiple) - imgs = nn.functional.interpolate(imgs, - size=ns, - mode="bilinear", - align_corners=False) + ns = [ + math.ceil(x * sf / gs) * gs for x in imgs.shape[2:] + ] # new shape (stretched to gs-multiple) + imgs = nn.functional.interpolate( + imgs, size=ns, mode="bilinear", align_corners=False + ) # Forward with amp.autocast(enabled=cuda): pred = model(imgs) # forward loss, loss_items = compute_loss( - pred, targets.to(device)) # loss scaled by batch_size + pred, targets.to(device) + ) # loss scaled by batch_size if RANK != -1: loss *= WORLD_SIZE # gradient averaged between devices in DDP mode if opt.quad: @@ -504,17 +504,19 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary # Log if RANK in [-1, 0]: - mloss = (mloss * i + loss_items) / (i + 1 - ) # update mean losses + mloss = (mloss * i + loss_items) / (i + 1) # update mean losses # (GB) mem = f"{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G" - pbar.set_description(("%10s" * 2 + "%10.4g" * 5) % ( - f"{epoch}/{epochs - 1}", - mem, - *mloss, - targets.shape[0], - imgs.shape[-1], - )) + pbar.set_description( + ("%10s" * 2 + "%10.4g" * 5) + % ( + f"{epoch}/{epochs - 1}", + mem, + *mloss, + targets.shape[0], + imgs.shape[-1], + ) + ) callbacks.run( "on_train_batch_end", ni, @@ -536,11 +538,9 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary if RANK in [-1, 0]: # mAP callbacks.run("on_train_epoch_end", epoch=epoch) - ema.update_attr(model, - include=[ - "yaml", "nc", "hyp", "names", "stride", - "class_weights" - ]) + ema.update_attr( + model, include=["yaml", "nc", "hyp", "names", "stride", "class_weights"] + ) final_epoch = (epoch + 1 == epochs) or stopper.possible_stop if not noval or final_epoch: # Calculate mAP results, maps, _ = val.run( @@ -557,13 +557,13 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary ) # Update best mAP - fi = fitness(np.array(results).reshape( - 1, -1)) # weighted combination of [P, R, mAP@.5, mAP@.5-.95] + fi = fitness( + np.array(results).reshape(1, -1) + ) # weighted combination of [P, R, mAP@.5, mAP@.5-.95] if fi > best_fitness: best_fitness = fi log_vals = list(mloss) + list(results) + lr - callbacks.run("on_fit_epoch_end", log_vals, epoch, best_fitness, - fi) + callbacks.run("on_fit_epoch_end", log_vals, epoch, best_fitness, fi) # Save model if (not nosave) or (final_epoch and not evolve): # if save @@ -574,8 +574,7 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary "ema": deepcopy(ema.ema).half(), "updates": ema.updates, "optimizer": optimizer.state_dict(), - "wandb_id": - loggers.wandb.wandb_run.id if loggers.wandb else None, + "wandb_id": loggers.wandb.wandb_run.id if loggers.wandb else None, "date": datetime.now().isoformat(), } @@ -583,11 +582,15 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary torch.save(ckpt, last) if best_fitness == fi: torch.save(ckpt, best) - if ((epoch > 0) and (opt.save_period > 0) - and (epoch % opt.save_period == 0)): + if ( + (epoch > 0) + and (opt.save_period > 0) + and (epoch % opt.save_period == 0) + ): torch.save(ckpt, w / f"epoch{epoch}.pt") - callbacks.run("on_model_save", last, epoch, final_epoch, - best_fitness, fi) + callbacks.run( + "on_model_save", last, epoch, final_epoch, best_fitness, fi + ) # Stop Single-GPU if RANK == -1 and stopper(epoch=epoch, fitness=fi): @@ -619,8 +622,9 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary batch_size=batch_size // WORLD_SIZE * 2, imgsz=imgsz, model=attempt_load(f, device).half(), - iou_thres=0.65 if is_coco else - 0.60, # best pycocotools results at 0.65 + iou_thres=( + 0.65 if is_coco else 0.60 + ), # best pycocotools results at 0.65 single_cls=single_cls, dataloader=val_loader, save_dir=save_dir, @@ -648,15 +652,13 @@ def train(hyp, opt, device, callbacks): # path/to/hyp.yaml or hyp dictionary def parse_opt(known=False): parser = argparse.ArgumentParser() - parser.add_argument("--weights", - type=str, - default=ROOT / "yolov5s.pt", - help="initial weights path") + parser.add_argument( + "--weights", type=str, default=ROOT / "yolov5s.pt", help="initial weights path" + ) parser.add_argument("--cfg", type=str, default="", help="model.yaml path") - parser.add_argument("--data", - type=str, - default=ROOT / "data/coco128.yaml", - help="dataset.yaml path") + parser.add_argument( + "--data", type=str, default=ROOT / "data/coco128.yaml", help="dataset.yaml path" + ) parser.add_argument( "--hyp", type=str, @@ -678,9 +680,7 @@ def parse_opt(known=False): default=640, help="train, val image size (pixels)", ) - parser.add_argument("--rect", - action="store_true", - help="rectangular training") + parser.add_argument("--rect", action="store_true", help="rectangular training") parser.add_argument( "--resume", nargs="?", @@ -688,15 +688,15 @@ def parse_opt(known=False): default=False, help="resume most recent training", ) - parser.add_argument("--nosave", - action="store_true", - help="only save final checkpoint") - parser.add_argument("--noval", - action="store_true", - help="only validate final epoch") - parser.add_argument("--noautoanchor", - action="store_true", - help="disable AutoAnchor") + parser.add_argument( + "--nosave", action="store_true", help="only save final checkpoint" + ) + parser.add_argument( + "--noval", action="store_true", help="only validate final epoch" + ) + parser.add_argument( + "--noautoanchor", action="store_true", help="disable AutoAnchor" + ) parser.add_argument( "--evolve", type=int, @@ -717,12 +717,12 @@ def parse_opt(known=False): action="store_true", help="use weighted image selection for training", ) - parser.add_argument("--device", - default="", - help="cuda device, i.e. 0 or 0,1,2,3 or cpu") - parser.add_argument("--multi-scale", - action="store_true", - help="vary img-size +/- 50%%") + parser.add_argument( + "--device", default="", help="cuda device, i.e. 0 or 0,1,2,3 or cpu" + ) + parser.add_argument( + "--multi-scale", action="store_true", help="vary img-size +/- 50%%" + ) parser.add_argument( "--single-cls", action="store_true", @@ -746,9 +746,9 @@ def parse_opt(known=False): default=8, help="max dataloader workers (per RANK in DDP mode)", ) - parser.add_argument("--project", - default=ROOT / "runs/train", - help="save to project/name") + parser.add_argument( + "--project", default=ROOT / "runs/train", help="save to project/name" + ) parser.add_argument("--name", default="exp", help="save to project/name") parser.add_argument( "--exist-ok", @@ -757,10 +757,9 @@ def parse_opt(known=False): ) parser.add_argument("--quad", action="store_true", help="quad dataloader") parser.add_argument("--linear-lr", action="store_true", help="linear LR") - parser.add_argument("--label-smoothing", - type=float, - default=0.0, - help="Label smoothing epsilon") + parser.add_argument( + "--label-smoothing", type=float, default=0.0, help="Label smoothing epsilon" + ) parser.add_argument( "--patience", type=int, @@ -780,10 +779,9 @@ def parse_opt(known=False): default=-1, help="Save checkpoint every x epochs (disabled if < 1)", ) - parser.add_argument("--local_rank", - type=int, - default=-1, - help="DDP parameter, do not modify") + parser.add_argument( + "--local_rank", type=int, default=-1, help="DDP parameter, do not modify" + ) # Weights & Biases arguments parser.add_argument("--entity", default=None, help="W&B: Entity") @@ -819,8 +817,9 @@ def main(opt, callbacks=Callbacks()): check_requirements(exclude=["thop"]) # Resume - if (opt.resume and not check_wandb_resume(opt) - and not opt.evolve): # resume an interrupted run + if ( + opt.resume and not check_wandb_resume(opt) and not opt.evolve + ): # resume an interrupted run ckpt = ( opt.resume if isinstance(opt.resume, str) else get_latest_run() ) # specified or most recent path @@ -842,7 +841,7 @@ def main(opt, callbacks=Callbacks()): raise AssertionError("either --cfg or --weights must be specified") if opt.evolve: if opt.project == str( - ROOT / "runs/train" + ROOT / "runs/train" ): # if default project name, rename to runs/evolve opt.project = str(ROOT / "runs/evolve") opt.exist_ok, opt.resume = ( @@ -850,8 +849,8 @@ def main(opt, callbacks=Callbacks()): False, ) # pass resume to exist_ok and disable resume opt.save_dir = str( - increment_path(Path(opt.project) / opt.name, - exist_ok=opt.exist_ok)) + increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok) + ) # DDP mode device = select_device(opt.device, batch_size=opt.batch_size) @@ -873,8 +872,7 @@ def main(opt, callbacks=Callbacks()): raise AssertionError("insufficient CUDA devices for DDP command") torch.cuda.set_device(LOCAL_RANK) device = torch.device("cuda", LOCAL_RANK) - dist.init_process_group( - backend="nccl" if dist.is_nccl_available() else "gloo") + dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo") # Train if not opt.evolve: @@ -940,12 +938,12 @@ def main(opt, callbacks=Callbacks()): ) # only val/save final epoch evolve_yaml, evolve_csv = save_dir / "hyp_evolve.yaml", save_dir / "evolve.csv" if opt.bucket: - os.system(f"gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}" - ) # download evolve.csv if exists + os.system( + f"gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}" + ) # download evolve.csv if exists for _ in range(opt.evolve): # generations to evolve - if evolve_csv.exists( - ): # if evolve.csv exists: select best hyps and mutate + if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate # Select parent(s) parent = "single" # parent selection method: 'single' or 'weighted' x = np.loadtxt(evolve_csv, ndmin=2, delimiter=",", skiprows=1) @@ -953,11 +951,9 @@ def main(opt, callbacks=Callbacks()): x = x[np.argsort(-fitness(x))][:n] # top n mutations w = fitness(x) - fitness(x).min() + 1e-6 # weights (sum > 0) if parent == "single" or len(x) == 1: - x = x[random.choices(range(n), - weights=w)[0]] # weighted selection + x = x[random.choices(range(n), weights=w)[0]] # weighted selection elif parent == "weighted": - x = (x * w.reshape( - n, 1)).sum(0) / w.sum() # weighted combination + x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination # Mutate mp, s = 0.8, 0.2 # mutation probability, sigma @@ -966,11 +962,10 @@ def main(opt, callbacks=Callbacks()): g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1 ng = len(meta) v = np.ones(ng) - while all( - v == 1 - ): # mutate until a change occurs (prevent duplicates) - v = (g * (npr.random(ng) < mp) * npr.randn(ng) * - npr.random() * s + 1).clip(0.3, 3.0) + while all(v == 1): # mutate until a change occurs (prevent duplicates) + v = ( + g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1 + ).clip(0.3, 3.0) for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300) hyp[k] = float(x[i + 7] * v[i]) # mutate @@ -991,7 +986,8 @@ def main(opt, callbacks=Callbacks()): LOGGER.info( f"Hyperparameter evolution finished {opt.evolve} generations\n" f"Results saved to {colorstr('bold', save_dir)}\n" - f"Usage example: $ python train.py --hyp {evolve_yaml}") + f"Usage example: $ python train.py --hyp {evolve_yaml}" + ) def run(**kwargs):