-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodel.py
83 lines (69 loc) · 2.21 KB
/
model.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import cv2
import json
import math
import numpy as np
from PIL import Image
import tensorflow as tf
from keras import layers
from keras.optimizers import Adam
from keras.models import Sequential
from keras.applications import DenseNet121
from keras.callbacks import Callback, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
np.random.seed(2020)
tf.set_random_seed(2020)
"""
Pretrained model accepts image resolution of 224x224,
therefore image is resized to this resolution
"""
def preprocess_image(image_path, desired_size=224):
im = Image.open(image_path)
im = im.resize((desired_size,) * 2, resample=Image.LANCZOS)
return im
"""
Function to build the model for inference on the image
"""
def build_model():
"""
Pretrained model use Densenet as the base model in specific DenseNet121 is used
Model weights are saved in file DenseNet-BC-121-32-no-top.h5
"""
densenet = DenseNet121(
weights="model/DenseNet-BC-121-32-no-top.h5",
include_top=False,
input_shape=(224, 224, 3),
)
"""
On top of Densenet average pooling layer, dropout layer and
finally dense layer with 5 classsed with sigmoid is used
"""
model = Sequential()
model.add(densenet)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(5, activation="sigmoid"))
"""
model parameters used in training:
optimizer : adam
learning rate : 10e-5
"""
model.compile(
loss="binary_crossentropy", optimizer=Adam(lr=0.00005), metrics=["accuracy"]
)
return model
"""
Image are classified into 5 categories based on severity using the pretrained model
Return the maximum probability class for given image
"""
def classify_image(img):
# Build model used for classification
# Load weights from pretrained model
model = build_model()
model.load_weights("model/model.h5")
# Create preprocessed image to be evaluated and predict its class
x_val = np.empty((1, 224, 224, 3), dtype=np.uint8)
x_val[0, :, :, :] = preprocess_image(img)
y_val_pred = model.predict(x_val)
# Return the maximum probability class
return np.argmax(np.squeeze(y_val_pred[0]))