File size: 5,550 Bytes
ea74b05
 
 
b5f9319
67c6bc5
3395b9d
67c6bc5
 
f19ce52
67c6bc5
3395b9d
67c6bc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24d4efe
67c6bc5
 
24d4efe
67c6bc5
 
24d4efe
67c6bc5
 
24d4efe
ea74b05
 
67c6bc5
28cbe7a
67c6bc5
b5f9319
294085b
b5f9319
 
67c6bc5
 
 
ea74b05
 
67c6bc5
488e557
67c6bc5
 
ea74b05
67c6bc5
 
488e557
 
 
 
67c6bc5
488e557
67c6bc5
 
 
 
 
 
488e557
67c6bc5
488e557
 
67c6bc5
 
488e557
 
 
 
67c6bc5
488e557
 
67c6bc5
488e557
 
67c6bc5
488e557
 
 
 
67c6bc5
488e557
67c6bc5
 
 
488e557
 
 
 
67c6bc5
488e557
 
67c6bc5
488e557
 
 
 
ea74b05
67c6bc5
 
488e557
 
 
 
67c6bc5
 
488e557
 
67c6bc5
488e557
 
 
 
 
 
 
 
 
 
 
 
ea74b05
67c6bc5
488e557
 
 
 
67c6bc5
488e557
67c6bc5
 
b5f9319
3395b9d
294085b
 
 
 
 
 
67c6bc5
 
 
 
 
 
 
 
 
3395b9d
67c6bc5
 
 
 
ea74b05
67c6bc5
ea74b05
67c6bc5
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
---
license: apache-2.0
tags:
- computer-vision
- image-classification
- skincare
- dermatology
- medical-ai
- vit
- pytorch
datasets:
- 0xnu/skincare
inference: true
widget:
- src: >-
    https://huggingface.co/0xnu/skincare-detection/resolve/main/joe.jpeg
  example_title: "Sample Skin Image"
model-index:
- name: skincare-detection
  results:
  - task:
      type: image-classification
    datasets:
    - 0xnu/skincare
    metrics:
    - name: accuracy
      type: accuracy
      value: 0.4864
    - name: f1
      type: f1
      value: 0.2502
    - name: precision
      type: precision
      value: 0.3184
    - name: recall
      type: recall
      value: 0.2366
---

# Skincare Disease Classification Model

A PyTorch-based deep learning model for classifying skincare diseases and conditions from images using transfer learning with EfficientNet-B0, ResNet50, and Vision Transformer (ViT).

### Code Example

```python
#!/usr/bin/env python3

import torch
from transformers import ViTImageProcessor, ViTForImageClassification
from PIL import Image
import json
from pathlib import Path
from typing import List, Dict, Union
import argparse

class SkincareClassifier:
    def __init__(self, model_name: str = '0xnu/skincare-detection'):
        self.processor = ViTImageProcessor.from_pretrained(model_name)
        self.model = ViTForImageClassification.from_pretrained(model_name)
        self.model.eval()
        self.id2label = self.model.config.id2label
    
    def classify(self, image_path: Union[str, Path], min_conf: float = 0.01) -> Dict:
        try:
            image = Image.open(image_path).convert('RGB')
            inputs = self.processor(images=image, return_tensors="pt")
            
            with torch.no_grad():
                outputs = self.model(**inputs)
                probs = torch.softmax(outputs.logits, dim=-1)[0]
            
            pred_id = outputs.logits.argmax().item()
            scores = {self.id2label[i]: float(probs[i]) for i in range(len(probs)) if probs[i] >= min_conf}
            
            return {
                'image': Path(image_path).name,
                'prediction': self.id2label[pred_id],
                'confidence': float(probs[pred_id]),
                'all_scores': dict(sorted(scores.items(), key=lambda x: x[1], reverse=True))
            }
        except Exception as e:
            return {'image': str(image_path), 'error': str(e)}
    
    def classify_batch(self, paths: List[Union[str, Path]], **kwargs) -> List[Dict]:
        return [self.classify(path, **kwargs) for path in paths]
    
    def classify_dir(self, dir_path: Union[str, Path], **kwargs) -> List[Dict]:
        extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'}
        paths = [p for p in Path(dir_path).rglob('*') if p.suffix.lower() in extensions]
        return self.classify_batch(paths, **kwargs) if paths else []
    
    def print_results(self, results: Union[Dict, List[Dict]]):
        if isinstance(results, dict):
            results = [results]
        
        for r in results:
            if 'error' in r:
                print(f"❌ {r['image']}: {r['error']}")
                continue
            
            print(f"📸 {r['image']}")
            print(f"🎯 {r['prediction'].upper()}: {r['confidence']:.1%}")
            
            for cls, conf in r['all_scores'].items():
                bar = "█" * int(conf * 20)
                print(f"   {cls:>8}: {conf:.1%} {bar}")
            print("-" * 30)

def main():
    parser = argparse.ArgumentParser(description="Skincare Image Classification")
    parser.add_argument('input', help='Image file or directory')
    parser.add_argument('--model', default='0xnu/skincare-detection')
    parser.add_argument('--output', help='JSON output file')
    parser.add_argument('--min-conf', type=float, default=0.01)
    args = parser.parse_args()
    
    classifier = SkincareClassifier(args.model)
    input_path = Path(args.input)
    
    if input_path.is_file():
        results = classifier.classify(input_path, args.min_conf)
    elif input_path.is_dir():
        results = classifier.classify_dir(input_path, min_conf=args.min_conf)
    else:
        return print(f"❌ Invalid path: {input_path}")
    
    classifier.print_results(results)
    
    if args.output:
        with open(args.output, 'w') as f:
            json.dump(results, f, indent=2, default=str)

if __name__ == "__main__":
    if len(__import__('sys').argv) == 1:
        classifier = SkincareClassifier()
        if Path('joe.jpeg').exists():
            classifier.print_results(classifier.classify('joe.jpeg'))
        else:
            print("Usage: python skincare.py <image_path>")
    else:
        main()
```

### Execute Code

```sh
python skincare.py joe.jpeg
```

### Limitations and Considerations

- Model performance depends on training data quality and diversity
- May not generalise well to significantly different image distributions
- Should not be used as sole diagnostic tool for medical decisions
- Requires validation by qualified healthcare professionals for clinical use
- Performance may vary across different skin types and demographics

### Ethical Considerations

- This model is for educational and research purposes
- Medical applications require proper validation and regulatory approval
- Consider bias in training data and ensure diverse representation
- Implement appropriate safeguards for sensitive medical applications

### Copyright

(c) Copyright 2025 Finbarrs Oketunji. All Rights Reserved.