haydpw commited on
Commit
08fa61a
1 Parent(s): 48ca81f

Add application file

Browse files
Files changed (37) hide show
  1. .gitignore +7 -0
  2. Dockerfile +14 -0
  3. README.md +2 -11
  4. api/roboflow_inference_client.py +13 -0
  5. exceptions/NotFaceError.py +3 -0
  6. main.py +84 -0
  7. models/efficientnet_face_detection.h5 +3 -0
  8. models/face_classifier.py +98 -0
  9. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/config.json +82 -0
  10. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/model.safetensors +3 -0
  11. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/optimizer.pt +3 -0
  12. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/preprocessor_config.json +39 -0
  13. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/rng_state.pth +3 -0
  14. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/scheduler.pt +3 -0
  15. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/trainer_state.json +0 -0
  16. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/training_args.bin +3 -0
  17. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/config.json +82 -0
  18. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/model.safetensors +3 -0
  19. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/optimizer.pt +3 -0
  20. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/preprocessor_config.json +39 -0
  21. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/rng_state.pth +3 -0
  22. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/scheduler.pt +3 -0
  23. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/trainer_state.json +0 -0
  24. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/training_args.bin +3 -0
  25. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/config.json +82 -0
  26. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/model.safetensors +3 -0
  27. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/optimizer.pt +3 -0
  28. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/preprocessor_config.json +39 -0
  29. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/rng_state.pth +3 -0
  30. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/scheduler.pt +3 -0
  31. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/trainer_state.json +0 -0
  32. models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/training_args.bin +3 -0
  33. models/segformer-b0-finetuned-segments-skin-outputs/runs/Jul10_13-17-20_44c7630ab4c4/events.out.tfevents.1720617441.44c7630ab4c4.317.0 +3 -0
  34. requirements.txt +0 -0
  35. update_requirements.bat +42 -0
  36. update_requirements.sh +37 -0
  37. utils/helpers.py +15 -0
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ .env
2
+ .venv
3
+
4
+ # ignore all __pycache__ file
5
+ __pycache__/
6
+
7
+ *.pyc
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9.4
2
+
3
+ RUN useradd -m -u 1000 haydpw
4
+ USER haydpw
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ COPY --chown=user . .
8
+
9
+ WORKDIR /
10
+
11
+ RUN pip install --no-chace-dir --upgrade -r requirements.txt
12
+
13
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
14
+
README.md CHANGED
@@ -1,11 +1,2 @@
1
- ---
2
- title: Skin Segmentation And Scoring
3
- emoji: 🐠
4
- colorFrom: purple
5
- colorTo: indigo
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # skin-segmentation-api
2
+ Semantic skin segmentation for Paragon beauty app
 
 
 
 
 
 
 
 
 
api/roboflow_inference_client.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from inference_sdk import InferenceHTTPClient
2
+ import dotenv
3
+ import os
4
+
5
+
6
+
7
+ def create_client(api_key):
8
+ client = InferenceHTTPClient(
9
+ api_url="https://classify.roboflow.com",
10
+ api_key=api_key
11
+ )
12
+ return client
13
+
exceptions/NotFaceError.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ class NotFaceError(Exception):
2
+ def __init__(self, *args: object) -> None:
3
+ super().__init__(*args)
main.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Union
2
+ import dotenv
3
+ import traceback
4
+ import json
5
+ import io
6
+ import os
7
+ import base64
8
+ from fastapi import FastAPI, File, HTTPException, UploadFile, Response
9
+ import models.face_classifier as classifier
10
+ from fastapi.middleware.cors import CORSMiddleware
11
+ from PIL import Image
12
+ from rembg import remove
13
+ from utils.helpers import image_to_base64, calculate_mask_area
14
+
15
+
16
+ dotenv.load_dotenv()
17
+
18
+ app = FastAPI()
19
+
20
+ app.add_middleware(
21
+ CORSMiddleware,
22
+ allow_origins=["*"],
23
+ allow_credentials=True,
24
+ allow_methods=["*"],
25
+ allow_headers=["*"],
26
+ )
27
+
28
+
29
+ # CLIENT = create_client(os.getenv("ROBOFLOW_API_KEY"))
30
+ # model = FaceClassifierModel(client=CLIENT)
31
+
32
+ model = classifier.FaceSegmentationModel()
33
+
34
+
35
+
36
+ @app.post("/segment/", summary="Classify skin type based on image given",tags=["Classify"])
37
+ async def predict_image(file: UploadFile = File(...)):
38
+ try:
39
+ # Menangani file yang diunggah
40
+ image_file = await file.read()
41
+ pil_image= Image.open(io.BytesIO(image_file)).convert("RGB")
42
+
43
+ # resize image to 512x512
44
+ pil_image = pil_image.resize((512, 512))
45
+
46
+ image_bg_removed= remove(pil_image, bgcolor=(0,0,255,255))
47
+
48
+ # Memanggil metode classify untuk melakukan klasifikasi
49
+ results = model.infer(image_bg_removed)
50
+
51
+ print(len(results))
52
+
53
+ background_element = next((element for element in results if element['label'] == 'background'), None)
54
+
55
+ if background_element:
56
+ background_area = calculate_mask_area(background_element['mask'])
57
+ else:
58
+ background_area = 0
59
+
60
+ # change the mask to base64 and calculate the score
61
+ for i in range(len(results)):
62
+ results[i]["mask"] = image_to_base64(results[i]["mask"])
63
+ if results[i]["label"] == "background":
64
+ continue
65
+ mask_area = calculate_mask_area(results[i]["mask"])
66
+ score = mask_area / (512 * 512 - background_area)
67
+ results[i]["score"] = score
68
+
69
+
70
+ # add original image base 64 as original image:
71
+ image_bg_removed = image_bg_removed.convert("RGB")
72
+
73
+ response = {
74
+ "original_image": image_to_base64(image_bg_removed),
75
+ "segmentation_results":results
76
+ }
77
+
78
+ # Kembalikan hasil klasifikasi
79
+ return Response(content=json.dumps(response), status_code=200)
80
+
81
+ except Exception as e:
82
+ # Mendapatkan stack trace
83
+ error_traceback = traceback.format_exc()
84
+ raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
models/efficientnet_face_detection.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5317ef37d26a7eb4803afbbfd9c806973a208ecb34a13b3933d2395ac720d84f
3
+ size 16678304
models/face_classifier.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from keras._tf_keras.keras.models import load_model
2
+ import keras
3
+ import warnings
4
+ import traceback
5
+ import cv2
6
+ import sys
7
+ import tensorflow as tf
8
+ import numpy as np
9
+ import exceptions
10
+ import os
11
+ from PIL import Image
12
+ from exceptions.NotFaceError import NotFaceError
13
+ from inference_sdk import InferenceHTTPClient
14
+ from transformers import pipeline, SegformerForSemanticSegmentation, SegformerImageProcessor, SegformerFeatureExtractor
15
+
16
+ def warning_with_traceback(message, category, filename, lineno, file=None, line=None):
17
+ log = file if hasattr(file,'write') else sys.stderr
18
+ traceback.print_stack(file=log)
19
+ log.write(warnings.formatwarning(message, category, filename, lineno, line))
20
+
21
+ warnings.showwarning = warning_with_traceback
22
+
23
+
24
+ @keras.saving.register_keras_serializable()
25
+ class CustomPreprocessingLayer(tf.keras.layers.Layer):
26
+ def __init__(self, input_shape, **kwargs):
27
+ self.input_shape = input_shape
28
+ super(CustomPreprocessingLayer, self).__init__(**kwargs)
29
+
30
+ def build(self, input_shape):
31
+ pass # No trainable weights to build
32
+
33
+ def call(self, image_matrix):
34
+ image = tf.convert_to_tensor(image_matrix, dtype=tf.int32)
35
+ image = tf.image.resize(image, [self.input_shape[0], self.input_shape[1]])
36
+ return image
37
+ def get_config(self):
38
+ config = super(CustomPreprocessingLayer, self).get_config()
39
+ config.update({'input_shape': self.input_shape})
40
+ return config
41
+
42
+ @classmethod
43
+ def from_config(cls, config):
44
+ return cls(**config)
45
+
46
+ class FaceClassifierModel:
47
+ def __init__(self, client:InferenceHTTPClient, image_size=224, batcb_size=16):
48
+ self.model = load_model("./models/efficientnet_face_detection.h5")
49
+ self.image_size = image_size
50
+ self.batch_size = batcb_size
51
+ self.seed = 42
52
+ self.client = client
53
+
54
+ async def classify(self, image_bytes: str, confidence_threshold=0.5):
55
+ tf.random.set_seed(self.seed)
56
+ nparr = np.frombuffer(image_bytes, np.uint8)
57
+
58
+ # Dekode array NumPy menjadi citra OpenCV
59
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
60
+ image = cv2.resize(image, [self.image_size, self.image_size])
61
+ image_expanded = tf.expand_dims(image,axis=0)
62
+ image_batch = tf.data.Dataset.from_tensor_slices(image_expanded).batch(self.batch_size)
63
+ pred = self.model.predict(image_batch)
64
+ if pred[0][0] <= confidence_threshold:
65
+ raise NotFaceError("Ini bukan wajah")
66
+
67
+ # lanjut klasifikasi muka
68
+ result = await self.client.infer_async(image, model_id="skinclassification-kyxvj/1")
69
+ result["face_confidence"] = float(pred[0][0])
70
+ return result
71
+
72
+
73
+ class FaceSegmentationModel:
74
+ def __init__(self):
75
+ model_checkpoint = os.path.join("models","segformer-b0-finetuned-segments-skin-outputs", "checkpoint-1640")
76
+ self.model = SegformerForSemanticSegmentation.from_pretrained(model_checkpoint, local_files_only=True)
77
+ self.image_processor = SegformerImageProcessor.from_pretrained(model_checkpoint, local_files_only=True)
78
+ self.pipeline = pipeline("image-segmentation", model=self.model, image_processor=self.image_processor)
79
+
80
+ def infer(self, image:Image.Image):
81
+ '''
82
+ Infer the input image. it will return list of {'score', 'label', and 'mask'}
83
+
84
+ Example:
85
+ [{'score': None,
86
+ 'label': 'background',
87
+ 'mask': <PIL.Image.Image image mode=L size=500x500>},
88
+ {'score': None,
89
+ 'label': 'acne',
90
+ 'mask': <PIL.Image.Image image mode=L size=500x500>},
91
+ {'score': None,
92
+ 'label': 'dry',
93
+ 'mask': <PIL.Image.Image image mode=L size=500x500>}]
94
+ '''
95
+ results = self.pipeline(image)
96
+ return results
97
+
98
+
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/config.json ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "nvidia/mit-b0",
3
+ "architectures": [
4
+ "SegformerForSemanticSegmentation"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.0,
7
+ "classifier_dropout_prob": 0.1,
8
+ "decoder_hidden_size": 256,
9
+ "depths": [
10
+ 2,
11
+ 2,
12
+ 2,
13
+ 2
14
+ ],
15
+ "downsampling_rates": [
16
+ 1,
17
+ 4,
18
+ 8,
19
+ 16
20
+ ],
21
+ "drop_path_rate": 0.1,
22
+ "hidden_act": "gelu",
23
+ "hidden_dropout_prob": 0.0,
24
+ "hidden_sizes": [
25
+ 32,
26
+ 64,
27
+ 160,
28
+ 256
29
+ ],
30
+ "id2label": {
31
+ "0": "background",
32
+ "1": "acne",
33
+ "2": "dry",
34
+ "3": "oily"
35
+ },
36
+ "image_size": 224,
37
+ "initializer_range": 0.02,
38
+ "label2id": {
39
+ "acne": 1,
40
+ "background": 0,
41
+ "dry": 2,
42
+ "oily": 3
43
+ },
44
+ "layer_norm_eps": 1e-06,
45
+ "mlp_ratios": [
46
+ 4,
47
+ 4,
48
+ 4,
49
+ 4
50
+ ],
51
+ "model_type": "segformer",
52
+ "num_attention_heads": [
53
+ 1,
54
+ 2,
55
+ 5,
56
+ 8
57
+ ],
58
+ "num_channels": 3,
59
+ "num_encoder_blocks": 4,
60
+ "patch_sizes": [
61
+ 7,
62
+ 3,
63
+ 3,
64
+ 3
65
+ ],
66
+ "reshape_last_stage": true,
67
+ "semantic_loss_ignore_index": 255,
68
+ "sr_ratios": [
69
+ 8,
70
+ 4,
71
+ 2,
72
+ 1
73
+ ],
74
+ "strides": [
75
+ 4,
76
+ 2,
77
+ 2,
78
+ 2
79
+ ],
80
+ "torch_dtype": "float32",
81
+ "transformers_version": "4.41.2"
82
+ }
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b113dd70ae08759ae88e63e3ba8c36c8d7ae14f0f73d62b37c3c62818427245a
3
+ size 14886832
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/optimizer.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8cb5172747645a4de909559836444a27932598a7b60531576ea49e86a48a1289
3
+ size 29886586
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/preprocessor_config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_valid_processor_keys": [
3
+ "images",
4
+ "segmentation_maps",
5
+ "do_resize",
6
+ "size",
7
+ "resample",
8
+ "do_rescale",
9
+ "rescale_factor",
10
+ "do_normalize",
11
+ "image_mean",
12
+ "image_std",
13
+ "do_reduce_labels",
14
+ "return_tensors",
15
+ "data_format",
16
+ "input_data_format"
17
+ ],
18
+ "do_normalize": true,
19
+ "do_reduce_labels": false,
20
+ "do_rescale": true,
21
+ "do_resize": true,
22
+ "image_mean": [
23
+ 0.485,
24
+ 0.456,
25
+ 0.406
26
+ ],
27
+ "image_processor_type": "SegformerImageProcessor",
28
+ "image_std": [
29
+ 0.229,
30
+ 0.224,
31
+ 0.225
32
+ ],
33
+ "resample": 2,
34
+ "rescale_factor": 0.00392156862745098,
35
+ "size": {
36
+ "height": 512,
37
+ "width": 512
38
+ }
39
+ }
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/rng_state.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:706d5e18e0bcc00af9508719f6801a358319a9b2bea62936270cc31440a2c5ee
3
+ size 13990
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/scheduler.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d2813ff910e7d207a59baff21caaa804f1364972acd9b94863d3ee624dc0d1e3
3
+ size 1064
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/trainer_state.json ADDED
The diff for this file is too large to render. See raw diff
 
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1620/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8a40ebbe9f4a4f2e901367dcd786dad8a884dfe4f6555f2a55fbebf29372529d
3
+ size 5112
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/config.json ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "nvidia/mit-b0",
3
+ "architectures": [
4
+ "SegformerForSemanticSegmentation"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.0,
7
+ "classifier_dropout_prob": 0.1,
8
+ "decoder_hidden_size": 256,
9
+ "depths": [
10
+ 2,
11
+ 2,
12
+ 2,
13
+ 2
14
+ ],
15
+ "downsampling_rates": [
16
+ 1,
17
+ 4,
18
+ 8,
19
+ 16
20
+ ],
21
+ "drop_path_rate": 0.1,
22
+ "hidden_act": "gelu",
23
+ "hidden_dropout_prob": 0.0,
24
+ "hidden_sizes": [
25
+ 32,
26
+ 64,
27
+ 160,
28
+ 256
29
+ ],
30
+ "id2label": {
31
+ "0": "background",
32
+ "1": "acne",
33
+ "2": "dry",
34
+ "3": "oily"
35
+ },
36
+ "image_size": 224,
37
+ "initializer_range": 0.02,
38
+ "label2id": {
39
+ "acne": 1,
40
+ "background": 0,
41
+ "dry": 2,
42
+ "oily": 3
43
+ },
44
+ "layer_norm_eps": 1e-06,
45
+ "mlp_ratios": [
46
+ 4,
47
+ 4,
48
+ 4,
49
+ 4
50
+ ],
51
+ "model_type": "segformer",
52
+ "num_attention_heads": [
53
+ 1,
54
+ 2,
55
+ 5,
56
+ 8
57
+ ],
58
+ "num_channels": 3,
59
+ "num_encoder_blocks": 4,
60
+ "patch_sizes": [
61
+ 7,
62
+ 3,
63
+ 3,
64
+ 3
65
+ ],
66
+ "reshape_last_stage": true,
67
+ "semantic_loss_ignore_index": 255,
68
+ "sr_ratios": [
69
+ 8,
70
+ 4,
71
+ 2,
72
+ 1
73
+ ],
74
+ "strides": [
75
+ 4,
76
+ 2,
77
+ 2,
78
+ 2
79
+ ],
80
+ "torch_dtype": "float32",
81
+ "transformers_version": "4.41.2"
82
+ }
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7663fa19c0154a4aef1ba06568f117f16864bf54c25993d2aff19a245db8fae2
3
+ size 14886832
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/optimizer.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2490d4a61e40a1214274e1914d7f8076ec2242d14b456515d9591f184eac7bfb
3
+ size 29886586
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/preprocessor_config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_valid_processor_keys": [
3
+ "images",
4
+ "segmentation_maps",
5
+ "do_resize",
6
+ "size",
7
+ "resample",
8
+ "do_rescale",
9
+ "rescale_factor",
10
+ "do_normalize",
11
+ "image_mean",
12
+ "image_std",
13
+ "do_reduce_labels",
14
+ "return_tensors",
15
+ "data_format",
16
+ "input_data_format"
17
+ ],
18
+ "do_normalize": true,
19
+ "do_reduce_labels": false,
20
+ "do_rescale": true,
21
+ "do_resize": true,
22
+ "image_mean": [
23
+ 0.485,
24
+ 0.456,
25
+ 0.406
26
+ ],
27
+ "image_processor_type": "SegformerImageProcessor",
28
+ "image_std": [
29
+ 0.229,
30
+ 0.224,
31
+ 0.225
32
+ ],
33
+ "resample": 2,
34
+ "rescale_factor": 0.00392156862745098,
35
+ "size": {
36
+ "height": 512,
37
+ "width": 512
38
+ }
39
+ }
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/rng_state.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9267e5c2da8f7e26291010326e3fae282f67edb5974de0985b158b51f4a83ef1
3
+ size 13990
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/scheduler.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:83effdf75fb04c222304704de02ea25ba33e71443d005c74d835b27e9cb5e7d2
3
+ size 1064
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/trainer_state.json ADDED
The diff for this file is too large to render. See raw diff
 
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-1640/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8a40ebbe9f4a4f2e901367dcd786dad8a884dfe4f6555f2a55fbebf29372529d
3
+ size 5112
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/config.json ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "nvidia/mit-b0",
3
+ "architectures": [
4
+ "SegformerForSemanticSegmentation"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.0,
7
+ "classifier_dropout_prob": 0.1,
8
+ "decoder_hidden_size": 256,
9
+ "depths": [
10
+ 2,
11
+ 2,
12
+ 2,
13
+ 2
14
+ ],
15
+ "downsampling_rates": [
16
+ 1,
17
+ 4,
18
+ 8,
19
+ 16
20
+ ],
21
+ "drop_path_rate": 0.1,
22
+ "hidden_act": "gelu",
23
+ "hidden_dropout_prob": 0.0,
24
+ "hidden_sizes": [
25
+ 32,
26
+ 64,
27
+ 160,
28
+ 256
29
+ ],
30
+ "id2label": {
31
+ "0": "background",
32
+ "1": "acne",
33
+ "2": "dry",
34
+ "3": "oily"
35
+ },
36
+ "image_size": 224,
37
+ "initializer_range": 0.02,
38
+ "label2id": {
39
+ "acne": 1,
40
+ "background": 0,
41
+ "dry": 2,
42
+ "oily": 3
43
+ },
44
+ "layer_norm_eps": 1e-06,
45
+ "mlp_ratios": [
46
+ 4,
47
+ 4,
48
+ 4,
49
+ 4
50
+ ],
51
+ "model_type": "segformer",
52
+ "num_attention_heads": [
53
+ 1,
54
+ 2,
55
+ 5,
56
+ 8
57
+ ],
58
+ "num_channels": 3,
59
+ "num_encoder_blocks": 4,
60
+ "patch_sizes": [
61
+ 7,
62
+ 3,
63
+ 3,
64
+ 3
65
+ ],
66
+ "reshape_last_stage": true,
67
+ "semantic_loss_ignore_index": 255,
68
+ "sr_ratios": [
69
+ 8,
70
+ 4,
71
+ 2,
72
+ 1
73
+ ],
74
+ "strides": [
75
+ 4,
76
+ 2,
77
+ 2,
78
+ 2
79
+ ],
80
+ "torch_dtype": "float32",
81
+ "transformers_version": "4.41.2"
82
+ }
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1d7053510fb0a0c8e6d2df90e8b01106c91745b9264c3949430b1e9089a9a478
3
+ size 14886832
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/optimizer.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fa1f746da01695702a4bec54dfe2abe27a39edb5579004a1b970deb2fd27fcab
3
+ size 29886586
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/preprocessor_config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_valid_processor_keys": [
3
+ "images",
4
+ "segmentation_maps",
5
+ "do_resize",
6
+ "size",
7
+ "resample",
8
+ "do_rescale",
9
+ "rescale_factor",
10
+ "do_normalize",
11
+ "image_mean",
12
+ "image_std",
13
+ "do_reduce_labels",
14
+ "return_tensors",
15
+ "data_format",
16
+ "input_data_format"
17
+ ],
18
+ "do_normalize": true,
19
+ "do_reduce_labels": false,
20
+ "do_rescale": true,
21
+ "do_resize": true,
22
+ "image_mean": [
23
+ 0.485,
24
+ 0.456,
25
+ 0.406
26
+ ],
27
+ "image_processor_type": "SegformerImageProcessor",
28
+ "image_std": [
29
+ 0.229,
30
+ 0.224,
31
+ 0.225
32
+ ],
33
+ "resample": 2,
34
+ "rescale_factor": 0.00392156862745098,
35
+ "size": {
36
+ "height": 512,
37
+ "width": 512
38
+ }
39
+ }
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/rng_state.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1b7af371e5ab3ec21ee88d4107bdd1fe2827f1d3f8e95f47292bafbf8b28622
3
+ size 13990
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/scheduler.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba02d6444725590e035a377e4bb8bb416bb1c21fb5d03ae7faa1cc51769398f5
3
+ size 1064
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/trainer_state.json ADDED
The diff for this file is too large to render. See raw diff
 
models/segformer-b0-finetuned-segments-skin-outputs/checkpoint-800/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8a40ebbe9f4a4f2e901367dcd786dad8a884dfe4f6555f2a55fbebf29372529d
3
+ size 5112
models/segformer-b0-finetuned-segments-skin-outputs/runs/Jul10_13-17-20_44c7630ab4c4/events.out.tfevents.1720617441.44c7630ab4c4.317.0 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:da95df5064431d7aa08d6994bea475a274e5008df1c46b239ad39aedf7d64de3
3
+ size 397851
requirements.txt ADDED
Binary file (5.33 kB). View file
 
update_requirements.bat ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+
3
+ :: Check if --skip argument is present
4
+ set skip_installation=false
5
+ for %%i in (%*) do (
6
+ if "%%i"=="--skip" (
7
+ set skip_installation=true
8
+ )
9
+ )
10
+
11
+ :: Install packages from requirements.txt if not skipped and file exists
12
+ if "%skip_installation%"=="false" (
13
+ if exist requirements.txt (
14
+ echo Installing packages from requirements.txt...
15
+ pip install -r requirements.txt
16
+ ) else (
17
+ echo requirements.txt not found. Skipping installation from file.
18
+ )
19
+ ) else (
20
+ echo Skipping installation from requirements.txt...
21
+ )
22
+
23
+ :: Remove --skip from the arguments
24
+ set "args="
25
+ for %%i in (%*) do (
26
+ if not "%%i"=="--skip" (
27
+ set "args=!args! %%i"
28
+ )
29
+ )
30
+ set args=%args:~1%
31
+
32
+ :: Install additional packages passed as arguments
33
+ if "%*" neq "" (
34
+ echo Installing additional packages: %*
35
+ pip install %*
36
+ )
37
+
38
+ :: Perbarui requirements.txt
39
+ echo Updating requirements.txt...
40
+ pip freeze > requirements.txt
41
+
42
+ echo Done.
update_requirements.sh ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Check if --skip argument is present
4
+ skip_installation=false
5
+ args=()
6
+
7
+ for arg in "$@"; do
8
+ if [ "$arg" == "--skip" ]; then
9
+ skip_installation=true
10
+ else
11
+ args+=("$arg")
12
+ fi
13
+ done
14
+
15
+ # Install packages from requirements.txt if not skipped and file exists
16
+ if [ "$skip_installation" == false ]; then
17
+ if [ -f requirements.txt ]; then
18
+ echo "Installing packages from requirements.txt..."
19
+ pip install -r requirements.txt
20
+ else
21
+ echo "requirements.txt not found. Skipping installation from file."
22
+ fi
23
+ else
24
+ echo "Skipping installation from requirements.txt..."
25
+ fi
26
+
27
+ # Install additional packages passed as arguments
28
+ if [ ${#args[@]} -ne 0 ]; then
29
+ echo "Installing additional packages: ${args[*]}"
30
+ pip install "${args[@]}"
31
+ fi
32
+
33
+ # Update requirements.txt
34
+ echo "Updating requirements.txt..."
35
+ pip freeze > requirements.txt
36
+
37
+ echo "Done."
utils/helpers.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import io
3
+ import base64
4
+ import numpy as np
5
+
6
+ def image_to_base64(image: Image.Image) -> str:
7
+ buffered = io.BytesIO()
8
+
9
+ image.save(buffered, format="JPEG")
10
+ return base64.b64encode(buffered.getvalue()).decode("utf-8")
11
+
12
+ def calculate_mask_area(mask: Image.Image) -> int:
13
+ mask_array = np.array(mask)
14
+ non_zero_pixels = np.count_nonzero(mask_array)
15
+ return non_zero_pixels