This commit is contained in:
zase414 committed 2026-11-27 16:47:43 +01:00
1 parent c870ec0961
commit 368aa71658
7 files changed
+328 -99

No files matched your search

+8 -5
View File
@@ -292,7 +292,9 @@ class FruitDataset(Dataset):
### Check
Temporarily add this at the bottom of `train.py`, or run it in a Python shell from the repo folder:
> **First make the files importable.** `from train import FruitDataset` makes Python read the *whole* `train.py`, which in turn imports `model.py`. Any line that still reads `x = # ‼️ ...` with nothing after the `=` is a `SyntaxError`, so the import fails even though your Dataset is fine. Until you fill those lines in, give them a placeholder value, e.g. `species_criterion = None # ‼️ ...` (and `embedding_bank, all_labels = None, None`). Do this in **both** `train.py` and `model.py`.
Run this in a Python shell from the repo folder (`uv run python` or `python`):
```python
import yaml
@@ -300,10 +302,10 @@ from train import FruitDataset
cfg = yaml.safe_load(open("confs/config.yml"))
ds = FruitDataset(cfg["data_dir"], cfg)
img, mask, name, label, idx = ds[0]
print(len(ds), len(ds.class_names)) # ~1969, 30
print(len(ds), len(ds.class_names)) # 1584, 30
print(img.shape, img.min().item(), img.max().item()) # [3, 64, 64], ≈ -1, ≈ 1
print(mask.shape, mask.unique()) # [1, 64, 64], tensor([0., 1.])
print(name, label) # e.g. Apple_Braeburn tensor(0)
print(name, label) # Apple_Gala tensor(0)
```
If `mask.unique()` shows only `0.` for most samples, the mask threshold or the file pairing is wrong. Fix that now, not after a 30-minute training run.
@@ -788,13 +790,14 @@ Change **one thing at a time**. Each run gets its own `experiments/experiment_<t
These are not marked with ‼️, but each one will stop you:
1. **No `.to(device)` anywhere in the training or validation loops.** On a GPU the model is on `cuda` while batches stay on `cpu`. The first `model(images)` fails with *"Expected all tensors to be on the same device"*. On a CPU-only machine it silently works, so the bug only shows up on the cluster. Fix: Step 5d / 5e.
2. **`dataset_size = 1969` is hardcoded** in `compute_embedding_bank`. If your dataset folder has a different number of images, the bank is too small (index error) or contains extra rows. Fix: Step 5f.
2. **`dataset_size = 1969` is hardcoded** in `compute_embedding_bank`, but the provided dataset has **1584** images. The bank silently gets 385 extra rows that are never filled in; with a larger dataset it would crash with an index error. Fix: Step 5f.
3. **`compute_seg_metrics` sometimes returns a plain `0` instead of a tensor.** It does this when a denominator is zero, typically early in training when the model predicts *no* fruit pixels. The validation loop then calls `p1.item()` on an `int` and crashes with `AttributeError: 'int' object has no attribute 'item'`. Fix: Step 5e (`float(p1)`).
4. **`MyModel(input_size=786432, hidden_size=128, ...)`** in `main()` ([train.py:691-693](train.py#L691-L693)) is left over from a fully-connected network. Your CNN's `__init__` must still *accept* these arguments, or it crashes. The README only mentions `output_size`.
5. **`compute_embedding_bank` leaves the model in `eval()` mode.** If `model.train()` sits only once before the epoch loop, BatchNorm and Dropout stay in evaluation mode for the entire training. Put it at the top of *every* epoch (Step 5c).
6. **Mask values.** The IoU code checks `== 1`. Masks stored as 0/255 give IoU 0 with no error. Fix: binarise in the Dataset (Step 2).
7. **`triplet margin=1.0` default** is large for cosine distance (range 0–2). Pass a smaller margin from the config.
8. **README says `data/`, config says `Data/`.** Make `data_dir` match wherever you actually unzipped the dataset.
8. **The unfilled ‼️ lines are syntax errors**, so you cannot import or test any part of `train.py`/`model.py` until *every* blank is filled. Use `None` placeholders while working step by step (see the Step 2 Check).
9. **README says `data/`, config says `Data/`.** Make `data_dir` match wherever you actually unzipped the dataset.
---