Spaces:
Sleeping
Sleeping
Yumeng Liu
commited on
Commit
·
4fa7f30
1
Parent(s):
268c9e1
added model
Browse files- Dockerfile +1 -1
- README.md +0 -10
- app.py +2 -5
- model/config.json +42 -0
- model/model.safetensors +3 -0
- model/preprocessor_config.json +62 -0
- skin_cancer_test.jpeg +0 -0
- test_huggingface.py +0 -35
- test_main.py +0 -59
Dockerfile
CHANGED
@@ -10,7 +10,7 @@ COPY requirements.txt .
|
|
10 |
RUN pip install --no-cache-dir -r requirements.txt
|
11 |
|
12 |
# Copy app code
|
13 |
-
COPY
|
14 |
|
15 |
# Expose port
|
16 |
EXPOSE 7860
|
|
|
10 |
RUN pip install --no-cache-dir -r requirements.txt
|
11 |
|
12 |
# Copy app code
|
13 |
+
COPY ../backend .
|
14 |
|
15 |
# Expose port
|
16 |
EXPOSE 7860
|
README.md
DELETED
@@ -1,10 +0,0 @@
|
|
1 |
-
---
|
2 |
-
title: Derma Scan
|
3 |
-
emoji: 👁
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: green
|
6 |
-
sdk: docker
|
7 |
-
pinned: false
|
8 |
-
---
|
9 |
-
|
10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -28,11 +28,8 @@ app.add_middleware(
|
|
28 |
)
|
29 |
|
30 |
# Load model and processor
|
31 |
-
processor = AutoImageProcessor.from_pretrained(
|
32 |
-
|
33 |
-
use_fast=True
|
34 |
-
)
|
35 |
-
model = AutoModelForImageClassification.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
|
36 |
|
37 |
|
38 |
@app.get("/")
|
|
|
28 |
)
|
29 |
|
30 |
# Load model and processor
|
31 |
+
processor = AutoImageProcessor.from_pretrained("model")
|
32 |
+
model = AutoModelForImageClassification.from_pretrained("model")
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
@app.get("/")
|
model/config.json
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"ViTForImageClassification"
|
4 |
+
],
|
5 |
+
"attention_probs_dropout_prob": 0.0,
|
6 |
+
"encoder_stride": 16,
|
7 |
+
"hidden_act": "gelu",
|
8 |
+
"hidden_dropout_prob": 0.0,
|
9 |
+
"hidden_size": 768,
|
10 |
+
"id2label": {
|
11 |
+
"0": "benign_keratosis-like_lesions",
|
12 |
+
"1": "basal_cell_carcinoma",
|
13 |
+
"2": "actinic_keratoses",
|
14 |
+
"3": "vascular_lesions",
|
15 |
+
"4": "melanocytic_Nevi",
|
16 |
+
"5": "melanoma",
|
17 |
+
"6": "dermatofibroma"
|
18 |
+
},
|
19 |
+
"image_size": 224,
|
20 |
+
"initializer_range": 0.02,
|
21 |
+
"intermediate_size": 3072,
|
22 |
+
"label2id": {
|
23 |
+
"actinic_keratoses": 2,
|
24 |
+
"basal_cell_carcinoma": 1,
|
25 |
+
"benign_keratosis-like_lesions": 0,
|
26 |
+
"dermatofibroma": 6,
|
27 |
+
"melanocytic_Nevi": 4,
|
28 |
+
"melanoma": 5,
|
29 |
+
"vascular_lesions": 3
|
30 |
+
},
|
31 |
+
"layer_norm_eps": 1e-12,
|
32 |
+
"model_type": "vit",
|
33 |
+
"num_attention_heads": 12,
|
34 |
+
"num_channels": 3,
|
35 |
+
"num_hidden_layers": 12,
|
36 |
+
"patch_size": 16,
|
37 |
+
"pooler_act": "tanh",
|
38 |
+
"pooler_output_size": 768,
|
39 |
+
"qkv_bias": true,
|
40 |
+
"torch_dtype": "float32",
|
41 |
+
"transformers_version": "4.51.3"
|
42 |
+
}
|
model/model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b8136bbbdd085cc97f69bdef01bd0c4c54262cf3ba44f5a94ebc9c8c54e85d60
|
3 |
+
size 343239356
|
model/preprocessor_config.json
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "google/vit-base-patch16-224-in21k",
|
3 |
+
"architectures": [
|
4 |
+
"ViTForImageClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.0,
|
7 |
+
"do_convert_rgb": null,
|
8 |
+
"do_normalize": true,
|
9 |
+
"do_rescale": true,
|
10 |
+
"do_resize": true,
|
11 |
+
"encoder_stride": 16,
|
12 |
+
"hidden_act": "gelu",
|
13 |
+
"hidden_dropout_prob": 0.0,
|
14 |
+
"hidden_size": 768,
|
15 |
+
"id2label": {
|
16 |
+
"0": "benign_keratosis-like_lesions",
|
17 |
+
"1": "basal_cell_carcinoma",
|
18 |
+
"2": "actinic_keratoses",
|
19 |
+
"3": "vascular_lesions",
|
20 |
+
"4": "melanocytic_Nevi",
|
21 |
+
"5": "melanoma",
|
22 |
+
"6": "dermatofibroma"
|
23 |
+
},
|
24 |
+
"image_mean": [
|
25 |
+
0.5,
|
26 |
+
0.5,
|
27 |
+
0.5
|
28 |
+
],
|
29 |
+
"image_processor_type": "ViTImageProcessor",
|
30 |
+
"image_size": 224,
|
31 |
+
"image_std": [
|
32 |
+
0.5,
|
33 |
+
0.5,
|
34 |
+
0.5
|
35 |
+
],
|
36 |
+
"initializer_range": 0.02,
|
37 |
+
"intermediate_size": 3072,
|
38 |
+
"label2id": {
|
39 |
+
"actinic_keratoses": 2,
|
40 |
+
"basal_cell_carcinoma": 1,
|
41 |
+
"benign_keratosis-like_lesions": 0,
|
42 |
+
"dermatofibroma": 6,
|
43 |
+
"melanocytic_Nevi": 4,
|
44 |
+
"melanoma": 5,
|
45 |
+
"vascular_lesions": 3
|
46 |
+
},
|
47 |
+
"layer_norm_eps": 1e-12,
|
48 |
+
"model_type": "vit",
|
49 |
+
"num_attention_heads": 12,
|
50 |
+
"num_channels": 3,
|
51 |
+
"num_hidden_layers": 12,
|
52 |
+
"patch_size": 16,
|
53 |
+
"qkv_bias": true,
|
54 |
+
"resample": 2,
|
55 |
+
"rescale_factor": 0.00392156862745098,
|
56 |
+
"size": {
|
57 |
+
"height": 224,
|
58 |
+
"width": 224
|
59 |
+
},
|
60 |
+
"torch_dtype": "float32",
|
61 |
+
"transformers_version": "4.38.2"
|
62 |
+
}
|
skin_cancer_test.jpeg
DELETED
Binary file (65 kB)
|
|
test_huggingface.py
DELETED
@@ -1,35 +0,0 @@
|
|
1 |
-
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
2 |
-
from PIL import Image
|
3 |
-
import torch
|
4 |
-
import time
|
5 |
-
import torch.nn.functional as F
|
6 |
-
from pprint import pprint
|
7 |
-
|
8 |
-
# Load model and processor
|
9 |
-
processor = AutoImageProcessor.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
|
10 |
-
model = AutoModelForImageClassification.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
|
11 |
-
|
12 |
-
# Load and preprocess the image
|
13 |
-
image = Image.open("skin_cancer_test.jpeg").convert("RGB")
|
14 |
-
inputs = processor(images=image, return_tensors="pt")
|
15 |
-
|
16 |
-
start = time.time()
|
17 |
-
|
18 |
-
# Run inference
|
19 |
-
with torch.no_grad():
|
20 |
-
outputs = model(**inputs)
|
21 |
-
|
22 |
-
end = time.time()
|
23 |
-
|
24 |
-
# Get predicted class
|
25 |
-
# Convert logits to probabilities
|
26 |
-
logits = outputs.logits
|
27 |
-
probs = F.softmax(logits, dim=-1)[0] # Shape: [num_classes]
|
28 |
-
|
29 |
-
# Map class labels to probabilities
|
30 |
-
id2label = model.config.id2label
|
31 |
-
result = {id2label[i]: probs[i].item() for i in range(len(probs))}
|
32 |
-
result = {"probabilities": result}
|
33 |
-
# Print the result
|
34 |
-
pprint(result)
|
35 |
-
print("Elapsed time: ", end - start)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_main.py
DELETED
@@ -1,59 +0,0 @@
|
|
1 |
-
import unittest
|
2 |
-
import requests
|
3 |
-
|
4 |
-
BACKEND_URL = "https://dermadetect.onrender.com" # Change this to the backend url when testing
|
5 |
-
TEST_IMG_PATH = "skin_cancer_test.jpeg"
|
6 |
-
search_header = {"Content-Type": "application/json"}
|
7 |
-
|
8 |
-
|
9 |
-
class MyTestCase(unittest.TestCase):
|
10 |
-
|
11 |
-
def test_root(self):
|
12 |
-
expected_response = {"Hello": "World"}
|
13 |
-
expected_response_code = 200
|
14 |
-
response = requests.get(BACKEND_URL)
|
15 |
-
|
16 |
-
self.assertEqual(response.status_code, expected_response_code)
|
17 |
-
self.assertEqual(response.json(), expected_response)
|
18 |
-
|
19 |
-
def test_search_doc(self):
|
20 |
-
expected_response_code = 200
|
21 |
-
search_endpoint = BACKEND_URL + "/findDoc"
|
22 |
-
sample_payload = {
|
23 |
-
"query": "melanoma",
|
24 |
-
"page": 0,
|
25 |
-
"getRankingInfo": True,
|
26 |
-
"aroundRadius": 100000,
|
27 |
-
"aroundLatLng": "37.6436195373535, -121.867645263672"
|
28 |
-
}
|
29 |
-
response = requests.post(search_endpoint, json=sample_payload, headers=search_header)
|
30 |
-
self.assertEqual(response.status_code, expected_response_code)
|
31 |
-
self.assertTrue("hits" in response.json())
|
32 |
-
|
33 |
-
def test_search_doc_invalid_type(self):
|
34 |
-
expected_response_code = 422
|
35 |
-
search_endpoint = BACKEND_URL + "/findDoc"
|
36 |
-
sample_payload = {
|
37 |
-
"query": "melanoma",
|
38 |
-
"page": 0,
|
39 |
-
"getRankingInfo": True,
|
40 |
-
"aroundRadius": 100000,
|
41 |
-
"aroundLatLng": 123
|
42 |
-
}
|
43 |
-
response = requests.post(search_endpoint, json=sample_payload, headers=search_header)
|
44 |
-
self.assertEqual(response.status_code, expected_response_code)
|
45 |
-
|
46 |
-
def test_get_predictions(self):
|
47 |
-
prediction_endpoint = BACKEND_URL + "/predict"
|
48 |
-
expected_response_code = 200
|
49 |
-
with open(TEST_IMG_PATH, "rb") as file:
|
50 |
-
file_name = TEST_IMG_PATH.split("/")[-1]
|
51 |
-
files = {"file": (file_name, file, "image/jpeg")}
|
52 |
-
response = requests.post(prediction_endpoint, files=files)
|
53 |
-
|
54 |
-
self.assertEqual(expected_response_code, response.status_code)
|
55 |
-
self.assertTrue("probabilities" in response.json())
|
56 |
-
|
57 |
-
|
58 |
-
if __name__ == '__main__':
|
59 |
-
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|