File size: 9,118 Bytes
5373059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
STEP 3 - Verify Dataset + Train / Resume YOLOv11n or Train YOLOv11s
=====================================================================
HOW TO USE:
  Verify only      : python train.py --verify
  Fresh train n    : python train.py --train
  Resume n 50 more : python train.py --resume
  Train s          : python train.py --trains
"""

import argparse
import yaml
from pathlib import Path
from collections import Counter


DATA_YAML   = "data/combined/data.yaml"
OUTPUT_DIR  = "runs/pothole_v1"
WEIGHTS_N   = "yolo11n.pt"
WEIGHTS_S   = "yolo11s.pt"

# ⚠️ Yeh path tera last.pt ka hai β€” check kar sahi hai ya nahi
RESUME_CKPT = r"C:\Users\HP\runs\detect\runs\pothole_v1\yolo11n_pothole\weights\last.pt"


# -------------------------------------------------------
# VERIFY
# -------------------------------------------------------

def verify_dataset():
    print("=" * 55)
    print("  Dataset Verification")
    print("=" * 55)

    yaml_path = Path(DATA_YAML)
    if not yaml_path.exists():
        print(f"\n❌ data.yaml not found at: {DATA_YAML}")
        return False

    with open(yaml_path, "r") as f:
        data = yaml.safe_load(f)

    dataset_root = Path(data["path"])
    class_names  = data["names"]

    print(f"\n  Dataset root : {dataset_root}")
    print(f"  Classes      : {class_names}")
    print(f"  Num classes  : {data['nc']}\n")

    all_ok = True

    for split in ["train", "val", "test"]:
        img_dir = dataset_root / split / "images"
        lbl_dir = dataset_root / split / "labels"

        img_count = len(list(img_dir.glob("*"))) if img_dir.exists() else 0
        lbl_count = len(list(lbl_dir.glob("*.txt"))) if lbl_dir.exists() else 0

        class_counts = Counter()
        empty_labels = 0
        if lbl_dir.exists():
            for lbl_file in lbl_dir.glob("*.txt"):
                content = lbl_file.read_text().strip()
                if not content:
                    empty_labels += 1
                    continue
                for line in content.splitlines():
                    parts = line.strip().split()
                    if parts:
                        class_counts[int(parts[0])] += 1

        status = "βœ…" if img_count > 0 and lbl_count > 0 else "❌"
        if img_count == 0:
            all_ok = False

        print(f"  {status} {split.upper():6s} | Images: {img_count:5d} | "
              f"Labels: {lbl_count:5d} | Annotations: {sum(class_counts.values()):6d} | "
              f"Empty labels: {empty_labels}")

    print()
    if all_ok:
        print("  βœ… Dataset looks good! Ready to train.")
    else:
        print("  ❌ Issues found. Re-run merge_datasets.py")

    print("=" * 55)
    return all_ok


# -------------------------------------------------------
# RESUME YOLOv11n (50 more epochs β†’ 51 to 100)
# -------------------------------------------------------

def resume_model():
    print("\n" + "=" * 55)
    print("  Resuming YOLOv11n β€” Epoch 51 β†’ 100")
    print("=" * 55)

    try:
        from ultralytics import YOLO
    except ImportError:
        print("\n❌ ultralytics not installed!")
        return

    ckpt = Path(RESUME_CKPT)
    if not ckpt.exists():
        print(f"\n❌ Checkpoint nahi mila: {RESUME_CKPT}")
        print("\n   Fix: RESUME_CKPT path check karo is file mein (line 20)")
        print("   last.pt yahan hoga:")
        print("   C:\\Users\\HP\\runs\\detect\\runs\\pothole_v1\\yolo11n_pothole\\weights\\last.pt")
        return

    print(f"\n  Checkpoint: {RESUME_CKPT}")
    print("  last.pt mein resume state nahi tha β€” explicitly settings pass kar rahe hain\n")

    yaml_path = Path(DATA_YAML)
    if not yaml_path.exists():
        print(f"\n❌ data.yaml nahi mila: {DATA_YAML}")
        return

    model = YOLO(str(ckpt))

    model.train(
        data=str(yaml_path.resolve()),
        epochs=100,        # 100 tak chalega (jo baki hain woh)
        imgsz=416,
        batch=8,
        device=0,
        amp=False,
        workers=2,
        cache=False,
        patience=15,
        save=True,
        project=OUTPUT_DIR,
        name="yolo11n_pothole",
        exist_ok=True,
        hsv_h=0.015,
        hsv_s=0.7,
        hsv_v=0.4,
        flipud=0.1,
        fliplr=0.5,
        mosaic=1.0,
        mixup=0.1,
    )

    print("\n" + "=" * 55)
    print("  βœ… RESUME COMPLETE!")
    print("=" * 55)
    _print_results(OUTPUT_DIR, "yolo11n_pothole")


# -------------------------------------------------------
# FRESH TRAIN YOLOv11n
# -------------------------------------------------------

def train_model():
    print("\n" + "=" * 55)
    print("  Training YOLOv11n β€” GTX 1650 (4GB VRAM) Config")
    print("=" * 55)

    try:
        from ultralytics import YOLO
    except ImportError:
        print("\n❌ ultralytics not installed!")
        return

    yaml_path = Path(DATA_YAML)
    if not yaml_path.exists():
        print(f"\n❌ data.yaml not found: {DATA_YAML}")
        return

    print("\n  Loading YOLOv11n pretrained weights...")
    model = YOLO(WEIGHTS_N)

    print("\n  Starting training:")
    print("   imgsz=416  batch=8  amp=False  epochs=50\n")

    model.train(
        data=str(yaml_path.resolve()),
        epochs=50,
        imgsz=416,
        batch=8,
        device=0,
        amp=False,
        workers=2,
        cache=False,
        patience=15,
        save=True,
        project=OUTPUT_DIR,
        name="yolo11n_pothole",
        exist_ok=True,
        hsv_h=0.015,
        hsv_s=0.7,
        hsv_v=0.4,
        flipud=0.1,
        fliplr=0.5,
        mosaic=1.0,
        mixup=0.1,
    )

    print("\n" + "=" * 55)
    print("  βœ… TRAINING COMPLETE!")
    print("=" * 55)
    _print_results(OUTPUT_DIR, "yolo11n_pothole")


# -------------------------------------------------------
# TRAIN YOLOv11s
# -------------------------------------------------------

def train_model_s():
    print("\n" + "=" * 55)
    print("  Training YOLOv11s β€” GTX 1650 (4GB VRAM) Config")
    print("=" * 55)

    try:
        from ultralytics import YOLO
    except ImportError:
        print("\n❌ ultralytics not installed!")
        return

    yaml_path = Path(DATA_YAML)
    if not yaml_path.exists():
        print(f"\n❌ data.yaml not found: {DATA_YAML}")
        return

    if not Path(WEIGHTS_S).exists():
        print("\n  yolo11s.pt nahi mili!")
        print("  Download karo:")
        print("  https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11s.pt")
        print("  Aur project folder mein rakh do\n")

    print("\n  Loading YOLOv11s pretrained weights...")
    model = YOLO(WEIGHTS_S)

    print("\n  Starting YOLOv11s training:")
    print("   imgsz=512  batch=8  amp=False  epochs=80\n")

    model.train(
        data=str(yaml_path.resolve()),
        epochs=80,
        imgsz=512,        # v11n se bada β€” better accuracy
        batch=8,
        device=0,
        amp=False,
        workers=2,
        cache=False,
        patience=15,
        save=True,
        project=OUTPUT_DIR,
        name="yolo11s_pothole",  # alag folder β€” v11n se mix nahi hoga
        exist_ok=True,
        hsv_h=0.015,
        hsv_s=0.7,
        hsv_v=0.4,
        flipud=0.1,
        fliplr=0.5,
        mosaic=1.0,
        mixup=0.1,
        optimizer='AdamW',
lr0=0.001,
    )

    print("\n" + "=" * 55)
    print("  βœ… YOLOv11s TRAINING COMPLETE!")
    print("=" * 55)
    _print_results(OUTPUT_DIR, "yolo11s_pothole")


# -------------------------------------------------------
# HELPER
# -------------------------------------------------------

def _print_results(project, name):
    base = Path(project) / name
    best = base / "weights" / "best.pt"
    results_png = base / "results.png"

    print(f"\n  Best weights : {best}")
    print(f"  Results      : {results_png}")
    print(f"\n  Inference:")
    print(f"  yolo detect predict model={best} source=your_image.jpg conf=0.25")
    print("=" * 55)


# -------------------------------------------------------
# ENTRY POINT
# -------------------------------------------------------

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--verify", action="store_true", help="Verify merged dataset")
    parser.add_argument("--train",  action="store_true", help="Fresh train YOLOv11n")
    parser.add_argument("--resume", action="store_true", help="Resume YOLOv11n 50 more epochs")
    parser.add_argument("--trains", action="store_true", help="Train YOLOv11s")
    args = parser.parse_args()

    if not any([args.verify, args.train, args.resume, args.trains]):
        print("Usage:")
        print("  python train.py --verify   β†’ dataset verify karo")
        print("  python train.py --train    β†’ YOLOv11n fresh train")
        print("  python train.py --resume   β†’ YOLOv11n resume (50 more epochs)")
        print("  python train.py --trains   β†’ YOLOv11s train karo")
    else:
        if args.verify:
            verify_dataset()
        if args.train:
            train_model()
        if args.resume:
            resume_model()
        if args.trains:
            train_model_s()