File size: 14,074 Bytes
4975158
83a443e
 
 
 
 
 
 
 
 
 
 
 
4975158
83a443e
4975158
83a443e
 
 
 
 
 
4975158
83a443e
 
 
 
 
 
4975158
83a443e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4975158
 
 
 
83a443e
 
 
4975158
 
 
83a443e
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# app.py
import os
import io
import zipfile
import json
import shutil
from pathlib import Path
from PIL import Image
import torch
from torchvision import models, transforms, datasets
from torch.utils.data import DataLoader, random_split
import torch.nn as nn
import torch.optim as optim
import gradio as gr
import time

ROOT = Path(".")
DATA_ZIP_NAME = "dataset.zip"   # upload your Roboflow export here
WORK_DIR = ROOT / "roboflow_dataset"
CLASSIFY_DIR = ROOT / "classification_data"
MODEL_PATH = ROOT / "model.pth"
CLASSES_JSON = ROOT / "classes.json"

# Training config (tweak if needed)
BATCH_SIZE = 16
IMG_SIZE = 224
NUM_EPOCHS = int(os.environ.get("NUM_EPOCHS", 3))   # small default for Spaces CPU
LR = 1e-3
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")


def safe_mkdir(p: Path):
    p.mkdir(parents=True, exist_ok=True)


def extract_zip_to_workdir(zip_path: Path, out_dir: Path):
    if out_dir.exists():
        shutil.rmtree(out_dir)
    safe_mkdir(out_dir)
    with zipfile.ZipFile(zip_path, "r") as z:
        z.extractall(out_dir)


def find_classes_mapping(workdir: Path):
    # Roboflow usually includes a data.yaml or classes.txt or a names list.
    # Try common locations.
    data_yaml = workdir / "data.yaml"
    classes_txt = workdir / "classes.txt"
    # Sometimes Roboflow includes a folder "labels" and a file "labels.names" or "classes.txt"
    if classes_txt.exists():
        names = [x.strip() for x in classes_txt.read_text().splitlines() if x.strip()]
        return names
    if data_yaml.exists():
        import yaml  # note: pyyaml must be in requirements if needed
        try:
            parsed = yaml.safe_load(data_yaml.read_text())
            if "names" in parsed:
                # could be list or dict
                n = parsed["names"]
                if isinstance(n, dict):
                    return [n[k] for k in sorted(n.keys(), key=lambda x: int(x))]
                elif isinstance(n, list):
                    return n
        except Exception:
            pass
    # fallback: try to find a file named "classes.txt" or "labels.names"
    for candidate in workdir.rglob("classes.txt"):
        names = [x.strip() for x in candidate.read_text().splitlines() if x.strip()]
        if names:
            return names
    for candidate in workdir.rglob("labels.names"):
        names = [x.strip() for x in candidate.read_text().splitlines() if x.strip()]
        if names:
            return names
    # last resort: scan label files to get max class index, produce numeric names
    max_idx = -1
    for lbl in workdir.rglob("labels/*.txt"):
        for line in lbl.read_text().splitlines():
            parts = line.strip().split()
            if len(parts) >= 1:
                try:
                    idx = int(float(parts[0]))
                    max_idx = max(max_idx, idx)
                except:
                    pass
    if max_idx >= 0:
        return [f"class_{i}" for i in range(max_idx + 1)]
    return []


def convert_roboflow_detection_to_classification(workdir: Path, outdir: Path):
    """
    Creates a folder-structured classification dataset:
    outdir/train/<class_name>/*.jpg
    outdir/valid/<class_name>/*.jpg
    It uses label files (YOLO txt) to assign the main class for each image.
    If bounding box info is available, it crops the bbox; otherwise it copies the image.
    """
    if outdir.exists():
        shutil.rmtree(outdir)
    safe_mkdir(outdir)

    # Try common image and label folders
    images_dirs = []
    labels_dirs = []
    for p in workdir.iterdir():
        if p.is_dir():
            if p.name.lower() in ("images", "image", "images/train", "train", "valid", "test"):
                images_dirs.append(p)
            if p.name.lower() in ("labels", "annotations"):
                labels_dirs.append(p)

    # simpler approach: look for 'images' and 'labels' in any depth
    images_all = list(workdir.rglob("images/*")) + list(workdir.rglob("images/*/*"))
    if not images_all:
        # fallback to all popular image file types in workdir
        images_all = [p for p in workdir.rglob("*") if p.suffix.lower() in (".jpg", ".jpeg", ".png")]

    # mapping of image filename (no path) to its full path
    img_map = {p.name: p for p in images_all}

    # find label files
    label_files = list(workdir.rglob("labels/*.txt")) + list(workdir.rglob("labels/*/*.txt"))
    if not label_files:
        # some exports put labels alongside images with same base name but different extension
        label_files = [p for p in workdir.rglob("*.txt") if p.stem in img_map]

    # find class names
    classes = find_classes_mapping(workdir)
    if not classes:
        # if not available, default to single class "unknown"
        classes = ["class_0"]

    # prepare train/valid split target folders (Roboflow often has train/valid folders; try to preserve)
    # We'll just create train and valid
    train_out = outdir / "train"
    valid_out = outdir / "valid"
    safe_mkdir(train_out)
    safe_mkdir(valid_out)

    # Load label->image mapping from label_files
    # We'll assume label files mirror the image names: e.g., images/train/img1.jpg and labels/train/img1.txt
    img_to_labels = {}
    for lbl in label_files:
        name = lbl.stem
        if name in img_map:
            img_to_labels[name] = lbl

    # If Roboflow has images split into train/valid dirs, detect them
    # Otherwise we'll create a split based on filenames (80/20)
    # Build a dataset list
    dataset_rows = []
    for img_name, img_path in img_map.items():
        lbl = img_to_labels.get(Path(img_name).stem)
        # Determine main class for this image (first label line)
        main_class = None
        bbox = None
        if lbl and lbl.exists():
            lines = [l for l in lbl.read_text().splitlines() if l.strip()]
            if lines:
                parts = lines[0].split()
                try:
                    cls_idx = int(float(parts[0]))
                    main_class = classes[cls_idx] if cls_idx < len(classes) else f"class_{cls_idx}"
                    if len(parts) >= 5:
                        # YOLO format: cls x_center y_center width height (normalized)
                        bbox = tuple(float(x) for x in parts[1:5])
                except Exception:
                    pass
        if not main_class:
            # fallback: mark as unknown
            main_class = "unknown"
            if "unknown" not in classes:
                classes.append("unknown")
        dataset_rows.append((img_path, main_class, bbox))

    # do deterministic split
    dataset_rows.sort(key=lambda x: x[0].name)
    split_idx = int(0.8 * len(dataset_rows))
    train_rows = dataset_rows[:split_idx]
    valid_rows = dataset_rows[split_idx:]

    def save_rows(rows, dest_folder):
        for img_path, cls_name, bbox in rows:
            dest_cls = dest_folder / cls_name
            safe_mkdir(dest_cls)
            try:
                img = Image.open(img_path).convert("RGB")
                if bbox:
                    # bbox are normalized; convert to pixel coords
                    w, h = img.size
                    xc, yc, bw, bh = bbox
                    left = int((xc - bw / 2) * w)
                    right = int((xc + bw / 2) * w)
                    top = int((yc - bh / 2) * h)
                    bottom = int((yc + bh / 2) * h)
                    # clamp
                    left = max(0, left); right = min(w, right)
                    top = max(0, top); bottom = min(h, bottom)
                    if right - left > 10 and bottom - top > 10:
                        img = img.crop((left, top, right, bottom))
                # save with a unique name
                dest_path = dest_cls / img_path.name
                img.save(dest_path)
            except Exception as e:
                print("Skipping", img_path, "due to", e)

    save_rows(train_rows, train_out)
    save_rows(valid_rows, valid_out)

    # Save classes json
    with open(CLASSES_JSON, "w") as f:
        json.dump(classes, f)
    return classes


def build_model(num_classes):
    model = models.resnet18(pretrained=True)
    in_features = model.fc.in_features
    model.fc = nn.Linear(in_features, num_classes)
    return model


def train_model(data_dir: Path, classes):
    print("Starting training. This may take some time on CPU.")
    num_classes = len(classes)
    model = build_model(num_classes).to(DEVICE)

    transform_train = transforms.Compose([
        transforms.Resize((IMG_SIZE, IMG_SIZE)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
    ])
    transform_valid = transforms.Compose([
        transforms.Resize((IMG_SIZE, IMG_SIZE)),
        transforms.ToTensor(),
        transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
    ])

    dataset_train = datasets.ImageFolder(str(data_dir / "train"), transform=transform_train)
    dataset_valid = datasets.ImageFolder(str(data_dir / "valid"), transform=transform_valid)

    # If ImageFolder class mapping differs from classes list, use folder names.
    # Dataloaders
    if len(dataset_train) == 0:
        raise RuntimeError("No training images found. Please check dataset structure.")

    loader_train = DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=True, num_workers=0)
    loader_valid = DataLoader(dataset_valid, batch_size=BATCH_SIZE, shuffle=False, num_workers=0)

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=LR)

    best_val = 0.0
    for epoch in range(NUM_EPOCHS):
        model.train()
        running = 0.0
        for imgs, labels in loader_train:
            imgs = imgs.to(DEVICE)
            labels = labels.to(DEVICE)
            optimizer.zero_grad()
            outputs = model(imgs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            running += loss.item()
        # validation
        model.eval()
        correct = 0
        total = 0
        with torch.no_grad():
            for imgs, labels in loader_valid:
                imgs = imgs.to(DEVICE)
                labels = labels.to(DEVICE)
                outputs = model(imgs)
                _, preds = torch.max(outputs, 1)
                correct += (preds == labels).sum().item()
                total += labels.size(0)
        acc = correct / total if total > 0 else 0.0
        print(f"Epoch {epoch+1}/{NUM_EPOCHS}, loss={running:.4f}, val_acc={acc:.4f}")
        if acc > best_val:
            best_val = acc
            # save best
            torch.save({
                "model_state": model.state_dict(),
                "classes": classes
            }, MODEL_PATH)
    print("Training complete. Best val acc:", best_val)
    # final save if not saved
    if not MODEL_PATH.exists():
        torch.save({
            "model_state": model.state_dict(),
            "classes": classes
        }, MODEL_PATH)
    return MODEL_PATH.exists()


def load_saved_model(path: Path):
    data = torch.load(path, map_location=DEVICE)
    classes = data.get("classes", None)
    if not classes and Path(CLASSES_JSON).exists():
        classes = json.loads(Path(CLASSES_JSON).read_text())
    if not classes:
        classes = [f"class_{i}" for i in range(2)]
    model = build_model(len(classes))
    model.load_state_dict(data["model_state"])
    model.to(DEVICE).eval()
    return model, classes


# Prepare model at startup
MODEL = None
BREEDS = None

def startup():
    global MODEL, BREEDS
    # If model exists, load directly
    if Path(MODEL_PATH).exists():
        try:
            MODEL, BREEDS = load_saved_model(Path(MODEL_PATH))
            print("Loaded existing model with classes:", BREEDS)
            return
        except Exception as e:
            print("Failed to load existing model:", e)

    # If dataset.zip exists, extract and convert, then train
    if Path(DATA_ZIP_NAME).exists():
        print("dataset.zip found. Extracting and preparing...")
        extract_zip_to_workdir(Path(DATA_ZIP_NAME), WORK_DIR)
        classes = convert_roboflow_detection_to_classification(WORK_DIR, CLASSIFY_DIR)
        print("Prepared classification dataset with classes:", classes)
        # train (may be slow on CPU)
        try:
            trained = train_model(CLASSIFY_DIR, classes)
            if trained:
                MODEL, BREEDS = load_saved_model(Path(MODEL_PATH))
        except Exception as e:
            print("Training failed:", e)
    else:
        print("No dataset.zip found. Please upload dataset.zip to the Space root or upload a model.pth")


# Prediction function
transform_predict = transforms.Compose([
    transforms.Resize((IMG_SIZE, IMG_SIZE)),
    transforms.ToTensor(),
    transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
])

def predict_image(pil_img):
    global MODEL, BREEDS
    if MODEL is None:
        return {"error": "Model not ready. Upload dataset.zip to train, or model.pth to load."}
    img = pil_img.convert("RGB")
    x = transform_predict(img).unsqueeze(0).to(DEVICE)
    with torch.no_grad():
        out = MODEL(x)
        probs = torch.nn.functional.softmax(out[0], dim=0).cpu().numpy()
    # top 3
    indices = probs.argsort()[::-1][:3]
    return {BREEDS[int(i)]: float(probs[int(i)]) for i in indices}

# Run startup (this will attempt to load or train)
start_time = time.time()
startup()
print("Startup complete in", time.time() - start_time, "seconds")

# Build Gradio app
demo = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    examples=[],
    title="Cow Breed Classifier",
    description="Upload a cow image. If you uploaded Roboflow dataset.zip to the Space root, the Space will auto-train on start (small number of epochs). If you already have a trained model.pth, upload that instead to skip training."
)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)