Spaces:
Runtime error
Runtime error
Upload utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from tensorflow.keras import layers
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
def create_model():
|
| 7 |
+
baseModel = tf.keras.applications.efficientnet.EfficientNetB0(include_top=False, weights='imagenet')
|
| 8 |
+
baseModel.trainable = False
|
| 9 |
+
|
| 10 |
+
inputs = layers.Input(shape=(224, 224, 3), name="input_layer")
|
| 11 |
+
|
| 12 |
+
x = baseModel(inputs)
|
| 13 |
+
x = layers.AveragePooling2D()(x)
|
| 14 |
+
x = layers.Flatten(name='Flatten')(x)
|
| 15 |
+
x = layers.Dense(units=128, activation='relu')(x)
|
| 16 |
+
x = layers.Dropout(rate=0.5)(x)
|
| 17 |
+
outputs = layers.Dense(units=1, activation='sigmoid')(x)
|
| 18 |
+
|
| 19 |
+
model = tf.keras.Model(inputs, outputs)
|
| 20 |
+
|
| 21 |
+
initial_learning_rate = 0.001
|
| 22 |
+
|
| 23 |
+
model.compile(loss='binary_crossentropy',
|
| 24 |
+
optimizer=tf.keras.optimizers.Adam(learning_rate=initial_learning_rate),
|
| 25 |
+
metrics = ['AUC'])
|
| 26 |
+
|
| 27 |
+
return model
|
| 28 |
+
|
| 29 |
+
def get_optimal_font_scale(text, width):
|
| 30 |
+
for scale in np.arange(1,0.1,-0.2):
|
| 31 |
+
scale = round(scale,2)
|
| 32 |
+
textSize = cv2.getTextSize(text, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=scale, thickness=1)
|
| 33 |
+
new_width = textSize[0][0]
|
| 34 |
+
if (new_width <= width):
|
| 35 |
+
return scale
|
| 36 |
+
return 0.1
|