diff --git a/examples/mind.py b/examples/mind.py deleted file mode 100644 index 958d612..0000000 --- a/examples/mind.py +++ /dev/null @@ -1,67 +0,0 @@ -from micromind import MicroMind, Metric -from micromind.networks import PhiNet -from micromind.utils.parse import parse_arguments - -import torch -import torch.nn as nn -import torchvision -import torchvision.transforms as transforms - -batch_size = 128 - - -class ImageClassification(MicroMind): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - self.modules["classifier"] = PhiNet( - (3, 32, 32), include_top=True, num_classes=10 - ) - - def forward(self, batch): - return self.modules["classifier"](batch[0]) - - def compute_loss(self, pred, batch): - return nn.CrossEntropyLoss()(pred, batch[1]) - - -if __name__ == "__main__": - hparams = parse_arguments() - m = ImageClassification(hparams) - - def compute_accuracy(pred, batch): - tmp = (pred.argmax(1) == batch[1]).float() - return tmp - - transform = transforms.Compose( - [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))] - ) - - trainset = torchvision.datasets.CIFAR10( - root="data/cifar-10", train=True, download=True, transform=transform - ) - trainloader = torch.utils.data.DataLoader( - trainset, batch_size=batch_size, shuffle=True, num_workers=1 - ) - - testset = torchvision.datasets.CIFAR10( - root="data/cifar-10", train=False, download=True, transform=transform - ) - testloader = torch.utils.data.DataLoader( - testset, batch_size=batch_size, shuffle=False, num_workers=1 - ) - - acc = Metric(name="accuracy", fn=compute_accuracy) - - m.train( - epochs=10, - datasets={"train": trainloader, "val": testloader, "test": testloader}, - metrics=[acc], - debug=hparams.debug, - ) - - m.test( - datasets={"test": testloader}, - ) - - m.export("output_onnx", "onnx", (3, 32, 32)) diff --git a/examples/yolo_cfg/coco8.yaml b/examples/yolo_cfg/coco8.yaml deleted file mode 100644 index eeb5d9d..0000000 --- a/examples/yolo_cfg/coco8.yaml +++ /dev/null @@ -1,101 +0,0 @@ -# Ultralytics YOLO 🚀, AGPL-3.0 license -# COCO8 dataset (first 8 images from COCO train2017) by Ultralytics -# Example usage: yolo train data=coco8.yaml -# parent -# ├── ultralytics -# └── datasets -# └── coco8 ← downloads here (1 MB) - - -# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] -path: ../datasets/coco8 # dataset root dir -train: images/train # train images (relative to 'path') 4 images -val: images/val # val images (relative to 'path') 4 images -test: # test images (optional) - -# Classes -names: - 0: person - 1: bicycle - 2: car - 3: motorcycle - 4: airplane - 5: bus - 6: train - 7: truck - 8: boat - 9: traffic light - 10: fire hydrant - 11: stop sign - 12: parking meter - 13: bench - 14: bird - 15: cat - 16: dog - 17: horse - 18: sheep - 19: cow - 20: elephant - 21: bear - 22: zebra - 23: giraffe - 24: backpack - 25: umbrella - 26: handbag - 27: tie - 28: suitcase - 29: frisbee - 30: skis - 31: snowboard - 32: sports ball - 33: kite - 34: baseball bat - 35: baseball glove - 36: skateboard - 37: surfboard - 38: tennis racket - 39: bottle - 40: wine glass - 41: cup - 42: fork - 43: knife - 44: spoon - 45: bowl - 46: banana - 47: apple - 48: sandwich - 49: orange - 50: broccoli - 51: carrot - 52: hot dog - 53: pizza - 54: donut - 55: cake - 56: chair - 57: couch - 58: potted plant - 59: bed - 60: dining table - 61: toilet - 62: tv - 63: laptop - 64: mouse - 65: remote - 66: keyboard - 67: cell phone - 68: microwave - 69: oven - 70: toaster - 71: sink - 72: refrigerator - 73: book - 74: clock - 75: vase - 76: scissors - 77: teddy bear - 78: hair drier - 79: toothbrush - - -# Download script/URL (optional) -download: https://ultralytics.com/assets/coco8.zip diff --git a/examples/yolo_cfg/default.yaml b/examples/yolo_cfg/default.yaml deleted file mode 100644 index ac5b654..0000000 --- a/examples/yolo_cfg/default.yaml +++ /dev/null @@ -1,116 +0,0 @@ -# Ultralytics YOLO 🚀, AGPL-3.0 license -# Default training settings and hyperparameters for medium-augmentation COCO training - -task: detect # (str) YOLO task, i.e. detect, segment, classify, pose -mode: train # (str) YOLO mode, i.e. train, val, predict, export, track, benchmark - -# Train settings ------------------------------------------------------------------------------------------------------- -model: # (str, optional) path to model file, i.e. yolov8n.pt, yolov8n.yaml -data: # (str, optional) path to data file, i.e. coco128.yaml -epochs: 100 # (int) number of epochs to train for -patience: 50 # (int) epochs to wait for no observable improvement for early stopping of training -batch: 16 # (int) number of images per batch (-1 for AutoBatch) -imgsz: 640 # (int | list) input images size as int for train and val modes, or list[w,h] for predict and export modes -save: True # (bool) save train checkpoints and predict results -save_period: -1 # (int) Save checkpoint every x epochs (disabled if < 1) -cache: False # (bool) True/ram, disk or False. Use cache for data loading -device: # (int | str | list, optional) device to run on, i.e. cuda device=0 or device=0,1,2,3 or device=cpu -workers: 8 # (int) number of worker threads for data loading (per RANK if DDP) -project: # (str, optional) project name -name: # (str, optional) experiment name, results saved to 'project/name' directory -exist_ok: False # (bool) whether to overwrite existing experiment -pretrained: True # (bool | str) whether to use a pretrained model (bool) or a model to load weights from (str) -optimizer: auto # (str) optimizer to use, choices=[SGD, Adam, Adamax, AdamW, NAdam, RAdam, RMSProp, auto] -verbose: True # (bool) whether to print verbose output -seed: 0 # (int) random seed for reproducibility -deterministic: True # (bool) whether to enable deterministic mode -single_cls: False # (bool) train multi-class data as single-class -rect: False # (bool) rectangular training if mode='train' or rectangular validation if mode='val' -cos_lr: False # (bool) use cosine learning rate scheduler -close_mosaic: 10 # (int) disable mosaic augmentation for final epochs (0 to disable) -resume: False # (bool) resume training from last checkpoint -amp: True # (bool) Automatic Mixed Precision (AMP) training, choices=[True, False], True runs AMP check -fraction: 1.0 # (float) dataset fraction to train on (default is 1.0, all images in train set) -profile: False # (bool) profile ONNX and TensorRT speeds during training for loggers -freeze: None # (int | list, optional) freeze first n layers, or freeze list of layer indices during training -# Segmentation -overlap_mask: True # (bool) masks should overlap during training (segment train only) -mask_ratio: 4 # (int) mask downsample ratio (segment train only) -# Classification -dropout: 0.0 # (float) use dropout regularization (classify train only) - -# Val/Test settings ---------------------------------------------------------------------------------------------------- -val: True # (bool) validate/test during training -split: val # (str) dataset split to use for validation, i.e. 'val', 'test' or 'train' -save_json: False # (bool) save results to JSON file -save_hybrid: False # (bool) save hybrid version of labels (labels + additional predictions) -conf: # (float, optional) object confidence threshold for detection (default 0.25 predict, 0.001 val) -iou: 0.7 # (float) intersection over union (IoU) threshold for NMS -max_det: 300 # (int) maximum number of detections per image -half: False # (bool) use half precision (FP16) -dnn: False # (bool) use OpenCV DNN for ONNX inference -plots: True # (bool) save plots during train/val - -# Prediction settings -------------------------------------------------------------------------------------------------- -source: # (str, optional) source directory for images or videos -show: False # (bool) show results if possible -save_txt: False # (bool) save results as .txt file -save_conf: False # (bool) save results with confidence scores -save_crop: False # (bool) save cropped images with results -show_labels: True # (bool) show object labels in plots -show_conf: True # (bool) show object confidence scores in plots -vid_stride: 1 # (int) video frame-rate stride -stream_buffer: False # (bool) buffer all streaming frames (True) or return the most recent frame (False) -line_width: # (int, optional) line width of the bounding boxes, auto if missing -visualize: False # (bool) visualize model features -augment: False # (bool) apply image augmentation to prediction sources -agnostic_nms: False # (bool) class-agnostic NMS -classes: # (int | list[int], optional) filter results by class, i.e. classes=0, or classes=[0,2,3] -retina_masks: False # (bool) use high-resolution segmentation masks -boxes: True # (bool) Show boxes in segmentation predictions - -# Export settings ------------------------------------------------------------------------------------------------------ -format: torchscript # (str) format to export to, choices at https://docs.ultralytics.com/modes/export/#export-formats -keras: False # (bool) use Kera=s -optimize: False # (bool) TorchScript: optimize for mobile -int8: False # (bool) CoreML/TF INT8 quantization -dynamic: False # (bool) ONNX/TF/TensorRT: dynamic axes -simplify: False # (bool) ONNX: simplify model -opset: # (int, optional) ONNX: opset version -workspace: 4 # (int) TensorRT: workspace size (GB) -nms: False # (bool) CoreML: add NMS - -# Hyperparameters ------------------------------------------------------------------------------------------------------ -lr0: 0.01 # (float) initial learning rate (i.e. SGD=1E-2, Adam=1E-3) -lrf: 0.01 # (float) final learning rate (lr0 * lrf) -momentum: 0.937 # (float) SGD momentum/Adam beta1 -weight_decay: 0.0005 # (float) optimizer weight decay 5e-4 -warmup_epochs: 3.0 # (float) warmup epochs (fractions ok) -warmup_momentum: 0.8 # (float) warmup initial momentum -warmup_bias_lr: 0.1 # (float) warmup initial bias lr -box: 7.5 # (float) box loss gain -cls: 0.5 # (float) cls loss gain (scale with pixels) -dfl: 1.5 # (float) dfl loss gain -pose: 12.0 # (float) pose loss gain -kobj: 1.0 # (float) keypoint obj loss gain -label_smoothing: 0.0 # (float) label smoothing (fraction) -nbs: 64 # (int) nominal batch size -hsv_h: 0. # (float) image HSV-Hue augmentation (fraction) -hsv_s: 0. # (float) image HSV-Saturation augmentation (fraction) -hsv_v: 0. # (float) image HSV-Value augmentation (fraction) -degrees: 0.0 # (float) image rotation (+/- deg) -translate: 0. # (float) image translation (+/- fraction) -scale: 0. # (float) image scale (+/- gain) -shear: 0.0 # (float) image shear (+/- deg) -perspective: 0.0 # (float) image perspective (+/- fraction), range 0-0.001 -flipud: 0.0 # (float) image flip up-down (probability) -fliplr: 0.0 # (float) image flip left-right (probability) -mosaic: 0.0 # (float) image mosaic (probability) -mixup: 0.0 # (float) image mixup (probability) -copy_paste: 0.0 # (float) segment copy-paste (probability) - -# Custom config.yaml --------------------------------------------------------------------------------------------------- -cfg: # (str, optional) for overriding defaults.yaml - -# Tracker settings ------------------------------------------------------------------------------------------------------ -tracker: botsort.yaml # (str) tracker type, choices=[botsort.yaml, bytetrack.yaml] diff --git a/examples/yolo_cfg/coco.names b/recipes/objection_detection/cfg/coco.names similarity index 100% rename from examples/yolo_cfg/coco.names rename to recipes/objection_detection/cfg/coco.names diff --git a/recipes/objection_detection/cfg/coco128.yaml b/recipes/objection_detection/cfg/coco128.yaml new file mode 100644 index 0000000..f4fd87f --- /dev/null +++ b/recipes/objection_detection/cfg/coco128.yaml @@ -0,0 +1,142 @@ +# Default training settings and hyperparameters for medium-augmentation COCO training + +task: detect # (str) YOLO task, i.e. detect, segment, classify, pose +mode: train # (str) YOLO mode, i.e. train, val, predict, export, track, benchmark + +# Train settings ------------------------------------------------------------------------------------------------------- +imgsz: 640 # (int | list) input images size as int for train and val modes, or list[w,h] for predict and export modes +rect: False # (bool) rectangular training if mode='train' or rectangular validation if mode='val' +cache: False # (bool) True/ram, disk or False. Use cache for data loading +single_cls: False # (bool) train multi-class data as single-class +fraction: 1.0 # (float) dataset fraction to train on (default is 1.0, all images in train set) + +# Segmentation +overlap_mask: True # (bool) masks should overlap during training (segment train only) +mask_ratio: 4 # (int) mask downsample ratio (segment train only) + +# Prediction settings -------------------------------------------------------------------------------------------------- +classes: # (int | list[int], optional) filter results by class, i.e. classes=0, or classes=[0,2,3] + +# Hyperparameters ------------------------------------------------------------------------------------------------------ +box: 7.5 # (float) box loss gain +cls: 0.5 # (float) cls loss gain (scale with pixels) +dfl: 1.5 # (float) dfl loss gain + +hsv_h: 0. # (float) image HSV-Hue augmentation (fraction) +hsv_s: 0. # (float) image HSV-Saturation augmentation (fraction) +hsv_v: 0. # (float) image HSV-Value augmentation (fraction) +degrees: 0.0 # (float) image rotation (+/- deg) +translate: 0. # (float) image translation (+/- fraction) +scale: 0. # (float) image scale (+/- gain) +shear: 0.0 # (float) image shear (+/- deg) +perspective: 0.0 # (float) image perspective (+/- fraction), range 0-0.001 +flipud: 1.0 # (float) image flip up-down (probability) +fliplr: 1.0 # (float) image flip left-right (probability) +mosaic: 1.0 # (float) image mosaic (probability) +mixup: 1.0 # (float) image mixup (probability) +copy_paste: 1.0 # (float) segment copy-paste (probability) + + +# # DATASET -------------------------------------------------------------------------------------------------------------- + +# Ultralytics YOLO 🚀, AGPL-3.0 license +# COCO8 dataset (first 8 images from COCO train2017) by Ultralytics +# Example usage: yolo train data=coco8.yaml +# parent +# ├── ultralytics +# └── datasets +# └── coco8 ← downloads here (1 MB) + + +# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] +path: ../datasets/coco128 # dataset root dir +train: images/train2017 # train images (relative to 'path') 128 images +val: images/train2017 # val images (relative to 'path') 128 images +test: # test images (optional) + +# Classes +names: + 0: person + 1: bicycle + 2: car + 3: motorcycle + 4: airplane + 5: bus + 6: train + 7: truck + 8: boat + 9: traffic light + 10: fire hydrant + 11: stop sign + 12: parking meter + 13: bench + 14: bird + 15: cat + 16: dog + 17: horse + 18: sheep + 19: cow + 20: elephant + 21: bear + 22: zebra + 23: giraffe + 24: backpack + 25: umbrella + 26: handbag + 27: tie + 28: suitcase + 29: frisbee + 30: skis + 31: snowboard + 32: sports ball + 33: kite + 34: baseball bat + 35: baseball glove + 36: skateboard + 37: surfboard + 38: tennis racket + 39: bottle + 40: wine glass + 41: cup + 42: fork + 43: knife + 44: spoon + 45: bowl + 46: banana + 47: apple + 48: sandwich + 49: orange + 50: broccoli + 51: carrot + 52: hot dog + 53: pizza + 54: donut + 55: cake + 56: chair + 57: couch + 58: potted plant + 59: bed + 60: dining table + 61: toilet + 62: tv + 63: laptop + 64: mouse + 65: remote + 66: keyboard + 67: cell phone + 68: microwave + 69: oven + 70: toaster + 71: sink + 72: refrigerator + 73: book + 74: clock + 75: vase + 76: scissors + 77: teddy bear + 78: hair drier + 79: toothbrush + + +# Download script/URL (optional) +download: https://ultralytics.com/assets/coco128.zip diff --git a/examples/yolo_cfg/config.yaml b/recipes/objection_detection/cfg/coco8.yaml similarity index 100% rename from examples/yolo_cfg/config.yaml rename to recipes/objection_detection/cfg/coco8.yaml diff --git a/micromind/networks/detect.py b/recipes/objection_detection/inference.py similarity index 100% rename from micromind/networks/detect.py rename to recipes/objection_detection/inference.py diff --git a/examples/yolo.py b/recipes/objection_detection/train.py similarity index 98% rename from examples/yolo.py rename to recipes/objection_detection/train.py index f63f9cc..f8432b6 100644 --- a/examples/yolo.py +++ b/recipes/objection_detection/train.py @@ -149,7 +149,7 @@ def __init__(self, m_cfg, *args, **kwargs): w, r, d = 1, 1, 1 model = YOLOv8(w, r, d, 80) model.load_state_dict( - torch.load("../micromind/networks/yolov8l.pt"), strict=True + torch.load("../../micromind/networks/yolov8l.pt"), strict=True ) self.modules["yolo"] = model @@ -225,7 +225,7 @@ def mAP(self, pred, batch): if __name__ == "__main__": - m_cfg, data_cfg = load_config("yolo_cfg/config.yaml") + m_cfg, data_cfg = load_config("cfg/coco8.yaml") batch_size = 8 mode = "train"