File size: 7,907 Bytes
3ce0f15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Contributing Guide

Thank you for your interest in contributing to the Multimodal Skin Lesion Explainability project! This guide will help you get started.

## Code of Conduct

Please follow [HuggingFace's Code of Conduct](https://huggingface.co/code-of-conduct) in all interactions.

## Getting Started

### 1. Fork & Clone
```bash
# Fork the repository at HuggingFace or GitHub
git clone https://huggingface.co/spaces/<your-username>/<your-fork>
cd GradCAMPlusPlus_SkinLesion
```

### 2. Set Up Environment
```bash
# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies with dev tools
pip install -r requirements.txt
pip install black flake8 pytest  # Optional: for code style
```

### 3. Verify Installation
```bash
python app.py  # Should launch Gradio without errors
```

## Development Workflow

### Making Changes

1. **Create a feature branch**
   ```bash
   git checkout -b feature/your-feature-name
   ```

2. **Make your changes**
   - Follow existing code style
   - Add type hints to new functions
   - Include docstrings for complex logic

3. **Test locally**
   ```bash
   python app.py
   # Test the UI thoroughly before pushing
   ```

4. **Format code** (optional but recommended)
   ```bash
   black src/ app.py
   flake8 src/ app.py
   ```

5. **Commit with clear messages**
   ```bash
   git add .
   git commit -m "feat: add new feature description"
   ```

6. **Push and create Pull Request**
   ```bash
   git push origin feature/your-feature-name
   ```

## Types of Contributions

### πŸ› Bug Reports
**Found an issue?** Check [Issues](../../issues) first, then report with:
- Describe the bug clearly
- Steps to reproduce
- Expected vs. actual behavior
- Screenshots if relevant
- Environment details (OS, Python version, GPU/CPU)

### ✨ Feature Requests
**Have an idea?** Create an issue with:
- Clear description of the feature
- Why it's useful
- Suggested implementation (if you have one)
- Examples of similar solutions

### πŸ“š Documentation
**Improve docs?** Edit:
- `README.md` - Main documentation
- `DEPLOYMENT.md` - Deployment guide
- Code docstrings - Inline documentation
- Create tutorials or examples

### πŸ”§ Code Improvements

#### Areas for Contribution:
- **Model Improvements**: Optimize attention mechanisms, add new architectures
- **UI/UX**: Enhance Gradio interface, add new visualizations
- **Performance**: Reduce inference time, optimize memory usage
- **Testing**: Add test cases, improve code coverage
- **Documentation**: Add docstrings, improve clarity

#### Code Standards:
```python
# Type hints required
from typing import Optional, Tuple, List

def process_metadata(values: dict, enabled_groups: List[str]) -> str:
    """
    Process metadata values and generate CSV format.
    
    Args:
        values: Dictionary of patient/lesion fields
        enabled_groups: List of active metadata groups
        
    Returns:
        CSV-formatted string
        
    Raises:
        ValueError: If required fields are missing
    """
    # Implementation...
```

## Project Structure Reference

```
src/
β”œβ”€β”€ main.py                    # Gradio UI - Safe to modify
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ inference.py          # Model loading - Core logic
β”‚   β”œβ”€β”€ model_loader.py       # PyTorch model setup
β”‚   β”œβ”€β”€ cam.py                # GradCAM++ implementation
β”‚   β”œβ”€β”€ preprocessing.py      # Image/metadata processing
β”‚   β”œβ”€β”€ metadata_*.py         # Metadata handling
β”‚   └── ...                   # Attention mechanism files
utils/
β”œβ”€β”€ transforms.py             # Image transformations
└── load_local_variables.py   # Configuration loading

data/
β”œβ”€β”€ weights/TO_BE_USED/       # Model files (do not commit large files)
└── preprocess_data/          # Encoders, scalers
```

## Areas to Avoid (Breaking Changes)

- **Do not modify**: Input/output format of `run_inference()` without coordination
- **Do not change**: Metadata CSV schema without updating documentation
- **Do not remove**: Core model classes without providing migration path
- **Do not alter**: Pre-trained model weights (they're frozen)

## Testing

### Manual Testing
```bash
# Run the app locally
python app.py

# Test scenarios:
1. Upload test image (try different formats)
2. Toggle metadata groups
3. Test all model options
4. Verify heatmap generation
5. Check metadata CSV output
```

### Automated Testing (Optional)
```bash
# Create tests/test_inference.py
import pytest
from src.models.inference import get_available_model_choices

def test_model_loading():
    choices = get_available_model_choices()
    assert len(choices) > 0, "No models available"
    assert all(isinstance(c, tuple) for c in choices)

# Run tests
pytest tests/
```

## Deployment Considerations

Before submitting PR with changes:
- [ ] Changes work locally with `python app.py`
- [ ] No new dependencies added without updating `requirements.txt`
- [ ] No hardcoded local paths
- [ ] All imports are available in requirements
- [ ] Code produces no warnings when run

## GPU/Performance Notes

- Models are cached after first load - don't reload unnecessarily
- Use `torch.no_grad()` for inference (already implemented)
- Profile code for bottlenecks: `python -m cProfile app.py`

## Documentation Standards

### For New Features
1. Update `README.md` with feature description
2. Add docstrings to functions
3. Include usage examples in docstrings
4. Update relevant guide (DEPLOYMENT.md, etc.)

### Example Docstring
```python
def generate_heatmap(image_tensor: torch.Tensor, metadata_tensor: torch.Tensor) -> np.ndarray:
    """
    Generate GradCAM++ heatmap for given inputs.
    
    This method computes class-weighted gradients and generates attention maps.
    The output can be overlaid on the original image for visualization.
    
    Args:
        image_tensor: Preprocessed image (1, 3, H, W)
        metadata_tensor: Encoded metadata (1, 20)
        
    Returns:
        Normalized heatmap (H, W) with values in [0, 1]
        
    Example:
        >>> image = torch.randn(1, 3, 224, 224)
        >>> metadata = torch.randn(1, 20)
        >>> heatmap = generate_heatmap(image, metadata)
        >>> assert heatmap.shape == (224, 224)
    """
```

## Commit Message Style

Follow conventional commits:
```
feat: add new attention mechanism
fix: resolve heatmap generation bug
docs: update README with new feature
style: format code with black
refactor: optimize inference pipeline
test: add unit tests for metadata builder
chore: update dependencies
```

## Getting Help

- **Questions**: Post in [Discussions](../../discussions)
- **Documentation**: Check README.md and DEPLOYMENT.md
- **Issues**: Search existing issues first
- **Code Review**: Tag maintainers in your PR

## Recognition

Contributors will be:
- Listed in project README
- Credited in git commits
- Thanked in release notes
- Considered for maintainer roles (for significant contributions)

## Legal

- By contributing, you agree your work may be used under the MIT License
- Ensure you have rights to any code you submit
- Respect intellectual property and attribution

## Review Process

1. **Automated Checks**: CI/CD runs automatically
   - Code format check
   - Import validation
   - Model loading verification

2. **Manual Review**: Maintainers review for:
   - Code quality and style
   - Alignment with project goals
   - Documentation completeness
   - Performance impact

3. **Merge**: Once approved, changes are merged to main

## Questions?

- Check existing [Issues](../../issues)
- Read [DEPLOYMENT.md](DEPLOYMENT.md) for deployment questions
- Open a [Discussion](../../discussions) for questions

---

**Thank you for contributing!** πŸ™

Together we make skin lesion analysis more transparent and interpretable.

**Last Updated**: March 2026