File size: 5,660 Bytes
e637785
 
 
 
 
 
 
86e22bf
e637785
6118f1d
e637785
 
a39b066
 
 
5386bb5
a39b066
 
 
 
745c38a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a39b066
 
 
 
 
 
745c38a
a39b066
 
 
 
745c38a
 
a39b066
 
745c38a
 
 
 
 
 
 
 
a39b066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ad2e72
 
 
 
 
 
 
 
 
 
 
 
 
a39b066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2add545
 
 
 
 
 
 
 
 
 
 
 
 
a39b066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d342a8
a39b066
 
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
---
title: Objectlocalization
emoji: πŸ‘
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 5.18.0
app_file: src/app.py
pinned: false
short_description: Using RESTNET-RCNN-RPN-FNN to detect lego pieces
---


# LEGO Object Detection using Faster R-CNN

[Faster R-CNN](https://pytorch.org/vision/main/models/generated/torchvision.models.detection.fasterrcnn_resnet50_fpn.html)

This project trains a **Faster R-CNN** model with a **ResNet-50 backbone** to detect LEGO objects using a custom dataset.

---
## πŸ” Project Overview

This project implements an advanced object detection system specifically designed for LEGO pieces using a combination of powerful deep learning architectures:

1. **ResNet-50 Backbone**: 
   - Serves as the feature extractor
   - Pre-trained on ImageNet for robust feature learning
   - Deep residual learning framework for improved training of deep networks

2. **Region Proposal Network (RPN)**:
   - Scans the image and proposes potential object regions
   - Generates anchor boxes of various scales and ratios
   - Outputs "objectness" scores and bounding box refinements

3. **Fast Neural Network (FNN)**:
   - Performs final classification and bounding box regression
   - Takes features from proposed regions
   - Outputs class probabilities and precise box coordinates

### Key Features

- **End-to-End Training**: The entire network is trained jointly for optimal performance
- **Multi-Scale Detection**: Capable of detecting LEGO pieces of varying sizes
- **Real-Time Processing**: Efficient architecture allows for quick inference
- **High Accuracy**: Achieves strong mean Average Precision (mAP) on LEGO detection

## **Project Structure**
```yaml
lego_detection/
│── models/                   # Trained models
β”‚   β”œβ”€β”€ lego_fasterrcnn.pth   # Saved model
β”‚   β”œβ”€β”€ faster_rcnn_custom.pth   # Latest model
β”‚
│── datasets/                  # Dataset folder
β”‚   β”œβ”€β”€ images/                # Training images
β”‚   β”œβ”€β”€ annotations/           # Corresponding XML annotations
β”‚   β”œβ”€β”€ test_images/           # Testing the model
β”‚   β”œβ”€β”€ annotations.json/      # To format annotation in one only file
β”‚
│── src/                       # Source code
β”‚   β”œβ”€β”€ transformdata.py       # Formats the data to COCO.json
β”‚   β”œβ”€β”€ new_trainer.py         # Train the model based on the new assumptions
β”‚   β”œβ”€β”€ app.py                 # Allow users to interact with this model
β”‚   β”œβ”€β”€ Attempt1               # First Implementation
β”‚     β”œβ”€β”€ dataset.py             # Dataset class (LegoDataset)
β”‚     β”œβ”€β”€ train.py               # Training script
β”‚     β”œβ”€β”€ evaluate.py            # mAP Calculation
β”‚     β”œβ”€β”€ utils.py               # IoU, AP calculation functions
β”‚
│── config.yaml                # Hyperparameters & settings
│── README.md                  # Project documentation
```

---

## ⚑ **Setup Instructions**
### **1️⃣ Install Dependencies**
```bash
pip install -r requirements.txt
```

### **2️⃣ Update Configuration**
Modify **`config.yaml`** to adjust **hyperparameters, dataset paths, and model settings**.

---

## **visualize using Gradio**

1) If the model is not in models please (add it from the submitted file) Im trying to add the model but its too big for github standars.

2) Run the following Bash

```bash
python src/app.py
```
3) Evaluate and give me 100. I know, im awesome.  

---

## πŸš€ **Training the Model**
Run the following command to start training:
```bash
python src/train.py
```
This script will:
βœ… Train Faster R-CNN with **LegoDataset**  
βœ… Log training **loss & mAP**  
βœ… Save the trained model in `models/lego_fasterrcnn.pth`

---

## πŸ“Š **Monitoring Training Progress**
Use the Jupyter Notebook to **visualize loss & mAP over epochs**:
```bash
jupyter notebook notebooks/training_visualization.ipynb
```

---

## πŸ› οΈ **Hyperparameters (`config.yaml`)**
Modify the **`config.yaml`** file to fine-tune the model:
```yaml
model:
  backbone: resnet50
  num_classes: 2
  pretrained: true
  learning_rate: 0.0001
  epochs: 5
  batch_size: 8
  optimizer: adam

dataset:
  image_dir: datasets/images
  annotation_dir: datasets/annotations
  train_split: 0.8
  val_split: 0.2

evaluation:
  iou_threshold: 0.5
```

---

## πŸ“ **Training Strategies for Faster R-CNN with ResNet-50 Backbone**

| Trainable Backbone Layers | Epochs | Batch Size | Recommended Learning Rate | Optimizer | Scheduler         |
|--------------------------|--------|-----------|--------------------------|-----------|------------------|
| 0                        | 10     | 4         | 0.0100                   | SGD       | StepLR(3, 0.1)   |
| 3                        | 10     | 8         | 0.0050                   | SGD       | StepLR(3, 0.1)   |
| 5                        | 10     | 16        | 0.0001                   | AdamW     | CosineAnnealing  |
| 3                        | 20     | 8         | 0.0050                   | SGD       | StepLR(5, 0.1)   |
| 5                        | 20     | 16        | 0.0001                   | AdamW     | CosineAnnealing  |


---

## πŸ“‘ **Evaluating the Model**
Once training is complete, evaluate performance using:
```bash
python src/evaluate.py
```

---

## πŸ’‘ **Troubleshooting & Tips**
### ❓ **Training Takes Too Long?**
- Reduce `epochs` in `config.yaml`
- Use a **smaller dataset** for testing

### ❓ **mAP is too low?**
- Increase `epochs`
- Check dataset annotations
- Tune learning rate

---

## πŸ† **Contributors**
- πŸ‘€ **Alex** - Machine Learning Engineer



πŸš€ **Happy Training!**