Upload README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Bottle Detection with Ultralytics YOLO
|
| 3 |
+
|
| 4 |
+
This project provides scripts and instructions to train and run inference on a bottle (or bottle cap) detection dataset using Ultralytics YOLO (v8+).
|
| 5 |
+
|
| 6 |
+
## Environment Setup
|
| 7 |
+
|
| 8 |
+
1. **Install dependencies** (recommended: use a virtual environment):
|
| 9 |
+
```bash
|
| 10 |
+
python3 -m venv .venv
|
| 11 |
+
source .venv/bin/activate
|
| 12 |
+
pip install ultralytics
|
| 13 |
+
```
|
| 14 |
+
|
| 15 |
+
2. **Prepare your dataset**
|
| 16 |
+
- Organize your dataset as:
|
| 17 |
+
```
|
| 18 |
+
dataset/
|
| 19 |
+
images/
|
| 20 |
+
train/
|
| 21 |
+
val/
|
| 22 |
+
labels/
|
| 23 |
+
train/
|
| 24 |
+
val/
|
| 25 |
+
data.yaml
|
| 26 |
+
```
|
| 27 |
+
- The `data.yaml` should specify absolute paths for `train` and `val` images.
|
| 28 |
+
|
| 29 |
+
## Training a Model
|
| 30 |
+
|
| 31 |
+
Edit and run `train.py` to train a model from scratch or fine-tune a pretrained model:
|
| 32 |
+
|
| 33 |
+
```
|
| 34 |
+
from ultralytics import YOLO
|
| 35 |
+
|
| 36 |
+
model = YOLO('yolov8n.pt') # or your custom/pretrained .pt file
|
| 37 |
+
|
| 38 |
+
model.train(
|
| 39 |
+
data='/absolute/path/to/data.yaml',
|
| 40 |
+
epochs=50,
|
| 41 |
+
imgsz=640,
|
| 42 |
+
batch=16,
|
| 43 |
+
project='runs/train',
|
| 44 |
+
name='experiment_name',
|
| 45 |
+
)
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
**Tips:**
|
| 49 |
+
- You can adjust `epochs`, `imgsz`, `batch`, and other parameters as needed.
|
| 50 |
+
- For small objects, consider increasing `imgsz` (e.g., 1024).
|
| 51 |
+
- Use a GPU for best performance.
|
| 52 |
+
|
| 53 |
+
## Inference with a Trained Model
|
| 54 |
+
|
| 55 |
+
Edit and run `infer.py` to run inference on images:
|
| 56 |
+
|
| 57 |
+
```
|
| 58 |
+
from ultralytics import YOLO
|
| 59 |
+
|
| 60 |
+
model = YOLO('/absolute/path/to/weights/best.pt')
|
| 61 |
+
|
| 62 |
+
results = model.predict(
|
| 63 |
+
source='/path/to/image/or/folder',
|
| 64 |
+
save=True, # Save images with boxes
|
| 65 |
+
conf=0.15, # Confidence threshold
|
| 66 |
+
classes=[0, 1] # (Optional) restrict to certain classes
|
| 67 |
+
)
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
Output images with bounding boxes will be saved in a `runs/predict` directory.
|
| 71 |
+
|
| 72 |
+
## Using Pretrained Models
|
| 73 |
+
|
| 74 |
+
You can use any YOLOv8 pretrained weights (e.g., `yolov8n.pt`, `yolov8s.pt`) as the starting point for training or inference by changing the model path in the scripts.
|
| 75 |
+
|
| 76 |
+
## References
|
| 77 |
+
|
| 78 |
+
- [Ultralytics YOLO Docs](https://docs.ultralytics.com/)
|
| 79 |
+
- [YOLOv8 GitHub](https://github.com/ultralytics/ultralytics)
|