Sharan Thakur
commited on
Commit
·
282421e
1
Parent(s):
2c417f9
Add inference code
Browse files- inference.py +33 -0
inference.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import keras
|
| 2 |
+
img_height = 162
|
| 3 |
+
img_width = 300
|
| 4 |
+
num_classes = 5
|
| 5 |
+
|
| 6 |
+
# base model for transfer learning
|
| 7 |
+
base_model = keras.applications.DenseNet121(
|
| 8 |
+
input_shape=(img_height, img_width, 3),
|
| 9 |
+
include_top=False,
|
| 10 |
+
)
|
| 11 |
+
base_model.trainable = False # Freeze the base model
|
| 12 |
+
|
| 13 |
+
model = keras.models.Sequential(
|
| 14 |
+
[
|
| 15 |
+
keras.layers.Input((img_height, img_width, 1)),
|
| 16 |
+
keras.layers.Lambda(
|
| 17 |
+
lambda x: tf.repeat(
|
| 18 |
+
x,
|
| 19 |
+
3,
|
| 20 |
+
axis=3,
|
| 21 |
+
)
|
| 22 |
+
), # Convert grayscale to RGB
|
| 23 |
+
keras.layers.Lambda(keras.applications.densenet.preprocess_input),
|
| 24 |
+
base_model,
|
| 25 |
+
keras.layers.GlobalAveragePooling2D(),
|
| 26 |
+
keras.layers.BatchNormalization(),
|
| 27 |
+
keras.layers.Dense(256, activation="relu"),
|
| 28 |
+
keras.layers.Dropout(0.5),
|
| 29 |
+
keras.layers.Dense(num_classes, activation="softmax"),
|
| 30 |
+
]
|
| 31 |
+
)
|
| 32 |
+
# Load the trained weights
|
| 33 |
+
model.load_weights('hf://c2p-cmd/knee_oa_classifier')
|