File size: 1,785 Bytes
2f34ba3
22a70b4
 
 
2f34ba3
22a70b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# predict.py
from tensorflow.keras.preprocessing import image
import numpy as np
import tensorflow as tf
from model_loader import load_model

model = load_model()

# Pre-calculated optimal temperature value
TEMPERATURE = 2.77

class_names_mapping = {
    0: "AKIEC",
    1: "BCC",
    2: "BKL",
    3: "DF",
    4: "MEL",
    5: "NV",
    6: "VASC"
}

full_names = {
    "AKIEC": "Actinic Keratoses and Intraepithelial Carcinoma (AKIEC)",
    "BCC": "Basal Cell Carcinoma (BCC)",
    "BKL": "Benign Keratosis-like Lesions (BKL)",
    "DF": "Dermatofibroma (DF)",
    "MEL": "Melanoma (MEL)",
    "NV": "Melanocytic Nevi (NV)",
    "VASC": "Vascular Lesions (VASC)"
}

def preprocess_image(file):
    img = image.load_img(file, target_size=(224, 224))
    img_array = image.img_to_array(img) / 255.0
    img_array = (img_array - 0.5) * 2
    return np.expand_dims(img_array, axis=0)

def predict_image(file):
    processed = preprocess_image(file)

    # Obtaining logits by disabling the final softmax
    logits_model = tf.keras.Model(inputs=model.input, outputs=model.layers[-2].output)
    logits = logits_model(processed)

    # Apply final layer without softmax (if the last layer is Dense with softmax)
    final_dense = model.layers[-1]
    logits = final_dense(logits)

    # Apply T-scaling: logits / T, then softmax
    scaled_logits = logits / TEMPERATURE
    scaled_probs = tf.nn.softmax(scaled_logits).numpy()[0]

    class_idx = int(np.argmax(scaled_probs))
    top_label = full_names[class_names_mapping[class_idx]]
    top_confidence = float(scaled_probs[class_idx])

    all_predictions = [
        {"label": class_names_mapping[i], "confidence": float(pred)}
        for i, pred in enumerate(scaled_probs)
    ]

    return top_label, top_confidence, all_predictions