add tryin.py
This commit is contained in:
parent
40e77d1034
commit
1afc0ea96c
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
# Dataset
|
||||
dataset/
|
||||
dataset.yaml
|
||||
dataset.yml
|
||||
|
||||
@ -9,10 +9,9 @@ dependencies:
|
||||
- tqdm
|
||||
- pyyaml
|
||||
- simsimd
|
||||
#- torch
|
||||
#- numpy
|
||||
#- opencv-contrib-python
|
||||
#- scikit-learn # @Todo: Remove it later if we don't need it.
|
||||
- torch
|
||||
- torchvision
|
||||
- numpy
|
||||
- pip
|
||||
- pip:
|
||||
- fastapi
|
||||
|
||||
110
runs/detect/runs/detect/coffee_v1/args.yaml
Normal file
110
runs/detect/runs/detect/coffee_v1/args.yaml
Normal file
@ -0,0 +1,110 @@
|
||||
task: detect
|
||||
mode: train
|
||||
model: yolov8n.pt
|
||||
data: dataset.yml
|
||||
epochs: 150
|
||||
time: null
|
||||
patience: 30
|
||||
batch: 8
|
||||
imgsz: 640
|
||||
save: true
|
||||
save_period: 10
|
||||
cache: false
|
||||
device: cpu
|
||||
workers: 8
|
||||
project: runs/detect
|
||||
name: coffee_v1
|
||||
exist_ok: true
|
||||
pretrained: true
|
||||
optimizer: auto
|
||||
verbose: true
|
||||
seed: 42
|
||||
deterministic: true
|
||||
single_cls: false
|
||||
rect: false
|
||||
cos_lr: false
|
||||
close_mosaic: 10
|
||||
resume: false
|
||||
amp: true
|
||||
fraction: 1.0
|
||||
profile: false
|
||||
freeze: null
|
||||
multi_scale: 0.0
|
||||
compile: false
|
||||
overlap_mask: true
|
||||
mask_ratio: 4
|
||||
dropout: 0.0
|
||||
val: true
|
||||
split: val
|
||||
save_json: false
|
||||
conf: 0.25
|
||||
iou: 0.5
|
||||
max_det: 300
|
||||
half: false
|
||||
dnn: false
|
||||
plots: true
|
||||
end2end: null
|
||||
source: null
|
||||
vid_stride: 1
|
||||
stream_buffer: false
|
||||
visualize: false
|
||||
augment: false
|
||||
agnostic_nms: false
|
||||
classes: null
|
||||
retina_masks: false
|
||||
embed: null
|
||||
show: false
|
||||
save_frames: false
|
||||
save_txt: false
|
||||
save_conf: false
|
||||
save_crop: false
|
||||
show_labels: true
|
||||
show_conf: true
|
||||
show_boxes: true
|
||||
line_width: null
|
||||
format: torchscript
|
||||
keras: false
|
||||
optimize: false
|
||||
int8: false
|
||||
dynamic: false
|
||||
simplify: true
|
||||
opset: null
|
||||
workspace: null
|
||||
nms: false
|
||||
lr0: 0.005
|
||||
lrf: 0.01
|
||||
momentum: 0.938
|
||||
weight_decay: 0.0005
|
||||
warmup_epochs: 5
|
||||
warmup_momentum: 0.8
|
||||
warmup_bias_lr: 0.1
|
||||
box: 7.5
|
||||
cls: 0.5
|
||||
cls_pw: 0.0
|
||||
dfl: 1.5
|
||||
pose: 12.0
|
||||
kobj: 1.0
|
||||
rle: 1.0
|
||||
angle: 1.0
|
||||
nbs: 64
|
||||
hsv_h: 0.015
|
||||
hsv_s: 0.7
|
||||
hsv_v: 0.4
|
||||
degrees: 5.0
|
||||
translate: 0.1
|
||||
scale: 0.5
|
||||
shear: 0.0
|
||||
perspective: 0.0
|
||||
flipud: 0.0
|
||||
fliplr: 0.5
|
||||
bgr: 0.0
|
||||
mosaic: 1.0
|
||||
mixup: 0.1
|
||||
cutmix: 0.0
|
||||
copy_paste: 0.1
|
||||
copy_paste_mode: flip
|
||||
auto_augment: randaugment
|
||||
erasing: 0.4
|
||||
cfg: null
|
||||
tracker: botsort.yaml
|
||||
save_dir: C:\Users\TRIZ\clones\YOLO-Coffee-Object-Detection\runs\detect\runs\detect\coffee_v1
|
||||
BIN
runs/detect/runs/detect/coffee_v1/labels.jpg
Normal file
BIN
runs/detect/runs/detect/coffee_v1/labels.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 220 KiB |
2
runs/detect/runs/detect/coffee_v1/results.csv
Normal file
2
runs/detect/runs/detect/coffee_v1/results.csv
Normal file
@ -0,0 +1,2 @@
|
||||
epoch,time,train/box_loss,train/cls_loss,train/dfl_loss,metrics/precision(B),metrics/recall(B),metrics/mAP50(B),metrics/mAP50-95(B),val/box_loss,val/cls_loss,val/dfl_loss,lr/pg0,lr/pg1,lr/pg2
|
||||
1,3381.49,1.0705,3.09597,1.50767,0.60417,0.5,0.4975,0.31896,1.31385,1.90113,1.49596,0.000117376,0.000117376,0.000117376
|
||||
|
BIN
runs/detect/runs/detect/coffee_v1/train_batch0.jpg
Normal file
BIN
runs/detect/runs/detect/coffee_v1/train_batch0.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 444 KiB |
BIN
runs/detect/runs/detect/coffee_v1/train_batch1.jpg
Normal file
BIN
runs/detect/runs/detect/coffee_v1/train_batch1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 395 KiB |
BIN
runs/detect/runs/detect/coffee_v1/train_batch2.jpg
Normal file
BIN
runs/detect/runs/detect/coffee_v1/train_batch2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 364 KiB |
BIN
runs/detect/runs/detect/coffee_v1/weights/best.pt
Normal file
BIN
runs/detect/runs/detect/coffee_v1/weights/best.pt
Normal file
Binary file not shown.
BIN
runs/detect/runs/detect/coffee_v1/weights/epoch0.pt
Normal file
BIN
runs/detect/runs/detect/coffee_v1/weights/epoch0.pt
Normal file
Binary file not shown.
BIN
runs/detect/runs/detect/coffee_v1/weights/last.pt
Normal file
BIN
runs/detect/runs/detect/coffee_v1/weights/last.pt
Normal file
Binary file not shown.
151
train.py
Normal file
151
train.py
Normal file
@ -0,0 +1,151 @@
|
||||
"""
|
||||
STEP 3 - train.py
|
||||
================
|
||||
|
||||
Fine-tunes YOLOv8-nano on your augmented dataset.
|
||||
Pre-trained COCO weights give you a huve head-start - the backbone
|
||||
already knows edges, textures, and shapes; it only needs to learn
|
||||
what Famico (and other products) look like.
|
||||
|
||||
Install:
|
||||
pip install ultralytics
|
||||
|
||||
Usage:
|
||||
python train.py
|
||||
|
||||
Outputs:
|
||||
runs/detect/coffee_v1/weights/best.pt <- use this for detection
|
||||
"""
|
||||
|
||||
import os
|
||||
import torch
|
||||
from pathlib import Path
|
||||
from ultralytics import YOLO
|
||||
|
||||
# ── Configuration ───────────────────────────────────────────────────
|
||||
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
|
||||
|
||||
DATASET_YAML = "dataset.yml" # Generated by prepare_dataset.py
|
||||
MODEL = "yolov8n.pt" # nano - fast, good for few-shot
|
||||
# alternativse to try: yolov8s.pt (better acc)
|
||||
# yolov8m.pt (even better, needs GPU)
|
||||
|
||||
PROJECT = "runs/detect"
|
||||
RUN_NAME = "coffee_v1"
|
||||
IMG_SIZE = 640
|
||||
|
||||
|
||||
# ── Training hyperparameters ───────────────────────────────────────────────────
|
||||
#
|
||||
# Tuned for: small dataset (few-shot), packaged goods, retail shelf.
|
||||
#
|
||||
EPOCHS = 150
|
||||
PATIENCE = 30 # Early Stopping - stops if no improvement
|
||||
BATCH = 8 # Lower if you get OOM on CPU/small GPU.
|
||||
LR0 = 0.005 # Initial LR (Learing Rate) - lower than default (0.01)
|
||||
|
||||
LRF = 0.01 # final LR = LR0 * LRF
|
||||
MOMENTUM = 0.938
|
||||
WEIGHT_DECAY = 0.0005
|
||||
WARMUP_EPOCHS = 5 # Longer warmup stabilises few-shot training.
|
||||
IOU_THRESH = 0.5 # IoU threshold for NMS.
|
||||
CONF_THRESH = 0.25 # confidence threshold at val time.
|
||||
|
||||
|
||||
# ── YOLOv8 built-in augmentation (on top of our albumentatios pass) ────────────
|
||||
#
|
||||
# These run during tarining batches - complementary to augment.py.
|
||||
# Mosaic is the single most impactful augmentation for small datasets.
|
||||
#
|
||||
MOSAIC = 1.0 # mosaic probability
|
||||
MIXUP = 0.1 # mixup probability
|
||||
COPY_PASTE = 0.1
|
||||
DEGREES = 5.0 # degrees ±5°
|
||||
TRANSLATE = 0.1
|
||||
SCALE = 0.5
|
||||
FLIPUD = 0.0
|
||||
FLIPLR = 0.5
|
||||
HSV_H = 0.015
|
||||
HSV_S = 0.7
|
||||
HSV_V = 0.4
|
||||
|
||||
def check_enviroment():
|
||||
device = "cuda" if torch.cuda.is_available() else "cpu"
|
||||
if device == "cpu":
|
||||
print("[INFO] No GPU detected - trainning on CPU")
|
||||
print(" Expected time: ~60-90min for 150 epochs on a typical laptop.")
|
||||
print(" Tips: use Google Colab or Kaggle they got free GPU, if training is too slow.\n")
|
||||
else:
|
||||
gpu= torch.cuda.get_device_name(0)
|
||||
print(f"[INFO] GPU detected: {gpu}")
|
||||
print(" Expected time: ~5-15min.\n")
|
||||
return device
|
||||
|
||||
def train():
|
||||
device = check_enviroment()
|
||||
|
||||
if not Path(DATASET_YAML).exists():
|
||||
print(f"[ERROR] {DATASET_YAML} not found.")
|
||||
print(" Run prepare_dataset.py first.")
|
||||
return
|
||||
|
||||
model = YOLO(MODEL)
|
||||
print(f"Starting training: {EPOCHS} epochs, img size {IMG_SIZE}")
|
||||
print(f"Dataset: {DATASET_YAML}")
|
||||
print(f"Output: {PROJECT}/{RUN_NAME}/weights/best.pt\n")
|
||||
|
||||
results = model.train(
|
||||
data = DATASET_YAML,
|
||||
epochs = EPOCHS,
|
||||
patience = PATIENCE,
|
||||
imgsz = IMG_SIZE,
|
||||
batch = BATCH,
|
||||
device = device,
|
||||
project = PROJECT,
|
||||
name = RUN_NAME,
|
||||
exist_ok = True,
|
||||
|
||||
lr0 = LR0,
|
||||
lrf = LRF,
|
||||
momentum = MOMENTUM,
|
||||
weight_decay = WEIGHT_DECAY,
|
||||
warmup_epochs = WARMUP_EPOCHS,
|
||||
|
||||
iou = IOU_THRESH,
|
||||
conf = CONF_THRESH,
|
||||
|
||||
# Built-in augmentation
|
||||
mosaic = MOSAIC,
|
||||
mixup = MIXUP,
|
||||
copy_paste = COPY_PASTE,
|
||||
degrees = DEGREES,
|
||||
translate = TRANSLATE,
|
||||
scale = SCALE,
|
||||
flipud = FLIPUD,
|
||||
fliplr = FLIPLR,
|
||||
hsv_h = HSV_H,
|
||||
hsv_s = HSV_S,
|
||||
hsv_v = HSV_V,
|
||||
|
||||
save = True,
|
||||
save_period = 10, # Checkpoint every 10 epochs
|
||||
plots = True, # Saves training curvse as PNGs
|
||||
verbose = True,
|
||||
seed = 42,
|
||||
)
|
||||
|
||||
best_weights = Path(PROJECT) / RUN_NAME / "weights" / "best.pt"
|
||||
print("\n" + "-" * 60)
|
||||
print(f"Training complete.")
|
||||
print(f"Best weights saved -> {best_weights}")
|
||||
print(f"mAP50: {results.results_dict.get('metrics/mAP50(B)', 'N/A'):.3f}")
|
||||
print(f"mAP50-95: {results.results_dict.get('metrics/mAP50-95(B)', 'N/A'):.3f}")
|
||||
print("-" * 60)
|
||||
print("\nRun next: python detect.py --source scene.jpg")
|
||||
|
||||
return best_weights
|
||||
|
||||
if __name__ == "__main__":
|
||||
train()
|
||||
|
||||
|
||||
BIN
yolov8n.pt
Normal file
BIN
yolov8n.pt
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user