AdilzhanB commited on
Commit
80af471
·
1 Parent(s): d14fbdf

App change

Browse files
Files changed (3) hide show
  1. README.md +32 -1
  2. app.py +86 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -10,5 +10,36 @@ pinned: false
10
  license: mit
11
  short_description: 🩺 Bone Age Prediction Gradio App
12
  ---
 
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  license: mit
11
  short_description: 🩺 Bone Age Prediction Gradio App
12
  ---
13
+ # 🩺 Bone Age Prediction Gradio App
14
 
15
+ This Hugging Face Space provides an interactive demo for bone age estimation from hand X-ray images, based on the [bone-age-resnet-80m](https://huggingface.co/Adilbai/bone-age-resnet-80m) deep learning model (ResNet152, finetuned on the RSNA Pediatric Bone Age dataset).
16
+
17
+ ## 🚀 What does this app do?
18
+
19
+ - **Upload a hand X-ray** (PNG/JPG).
20
+ - **Select the patient's gender** (Male/Female).
21
+ - **Get an instant prediction** of bone age in months (and years/months format) using a state-of-the-art neural network.
22
+
23
+ ## 🧠 Model Details
24
+
25
+ - **Architecture:** ResNet152 + custom head (≈80M parameters)
26
+ - **Input:** 256x256 hand X-ray image & gender
27
+ - **Output:** Bone age (months)
28
+ - **Training Data:** [RSNA Bone Age Challenge](https://www.kaggle.com/datasets/kmader/rsna-bone-age)
29
+ - **Model Card:** [bone-age-resnet-80m](https://huggingface.co/Adilbai/bone-age-resnet-80m)
30
+
31
+ ## 🌟 How to use
32
+
33
+ 1. Upload a clear hand X-ray image (preferably as PNG).
34
+ 2. Select the appropriate gender.
35
+ 3. Press "Submit" to get the predicted bone age.
36
+
37
+ > **Note:** This app is for educational and research purposes only. Not for clinical use.
38
+
39
+ ## 🏷️ Citation
40
+
41
+ If you use this demo or model, please cite the [RSNA Bone Age dataset](https://www.kaggle.com/datasets/kmader/rsna-bone-age) and this Hugging Face Space/model.
42
+
43
+ ---
44
+
45
+ Built with ❤️ using Gradio and Hugging Face Spaces.
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import torch
4
+ import numpy as np
5
+ import requests
6
+ from io import BytesIO
7
+ from torchvision import transforms
8
+ import onnxruntime as ort
9
+
10
+ # ======================
11
+ # Model & Preprocessing
12
+ # ======================
13
+ MODEL_ONNX_URL = "https://huggingface.co/Adilbai/bone-age-resnet-80m/resolve/main/resnet_bone_age_80m.onnx"
14
+
15
+ def download_model(url, filename):
16
+ if not os.path.exists(filename):
17
+ print(f"Downloading model from {url}")
18
+ r = requests.get(url)
19
+ with open(filename, "wb") as f:
20
+ f.write(r.content)
21
+
22
+ import os
23
+ MODEL_PATH = "resnet_bone_age_80m.onnx"
24
+ download_model(MODEL_ONNX_URL, MODEL_PATH)
25
+
26
+ # Set up ONNX session
27
+ ort_session = ort.InferenceSession(MODEL_PATH)
28
+
29
+ # Define image preprocessing (must match training)
30
+ transform = transforms.Compose([
31
+ transforms.Resize((256, 256)),
32
+ transforms.ToTensor(),
33
+ transforms.Normalize([0.5]*3, [0.5]*3)
34
+ ])
35
+
36
+ # ======================
37
+ # Inference Function
38
+ # ======================
39
+ def predict_bone_age(image, gender):
40
+ """
41
+ image: PIL.Image
42
+ gender: string ("Male" or "Female")
43
+ """
44
+ # Preprocess image
45
+ img_tensor = transform(image).unsqueeze(0).numpy()
46
+ # Gender: 0=male, 1=female
47
+ gender_val = 0.0 if gender.lower() == "male" else 1.0
48
+ gender_tensor = np.array([[gender_val]], dtype=np.float32)
49
+
50
+ # ONNX inference
51
+ outputs = ort_session.run(None, {"image": img_tensor, "gender": gender_tensor})
52
+ pred_age = outputs[0][0]
53
+
54
+ # Display as years and months
55
+ years = int(pred_age // 12)
56
+ months = int(pred_age % 12)
57
+ result_str = (
58
+ f"Predicted Bone Age: **{pred_age:.1f} months** \n"
59
+ f"≈ {years} years, {months} months"
60
+ )
61
+ return result_str
62
+
63
+ # ======================
64
+ # Gradio UI
65
+ # ======================
66
+ app_title = "Bone Age Prediction from Hand X-ray"
67
+ app_desc = """
68
+ Upload a hand X-ray image and select the patient's gender. This app will predict the bone age (in months) using a powerful deep learning model.
69
+ - Model: [bone-age-resnet-80m](https://huggingface.co/Adilbai/bone-age-resnet-80m)
70
+ - Data: RSNA Pediatric Bone Age Challenge
71
+ - **For research/educational use only.**
72
+ """
73
+ iface = gr.Interface(
74
+ fn=predict_bone_age,
75
+ inputs=[
76
+ gr.Image(type="pil", label="Hand X-ray Image"),
77
+ gr.Radio(["Male", "Female"], label="Gender")
78
+ ],
79
+ outputs=gr.Markdown(label="Prediction"),
80
+ title=app_title,
81
+ description=app_desc,
82
+ allow_flagging="never"
83
+ )
84
+
85
+ if __name__ == "__main__":
86
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ torchvision
4
+ onnxruntime
5
+ Pillow
6
+ requests