-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
90 lines (70 loc) · 2.73 KB
/
utils.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
84
85
86
87
88
89
90
# Copyright 2020 Samson Woof
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import requests
import os
import cv2
import numpy as np
from PIL import Image
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.utils import get_file
WEIGHTS_PATH_VGG16_MURA = "https://github.com/samson6460/tf_keras_gradcamplusplus/releases/download/Weights/tf_keras_vgg16_mura_model.h5"
def vgg16_mura_model():
"""Get a vgg16 model.
The model can classify bone X-rays into three categories:
wrist, shoulder and elbow.
It will download the weights automatically for the first time.
Return:
A tf.keras model object.
"""
path_weights = get_file(
"tf_keras_vgg16_mura_model.h5",
WEIGHTS_PATH_VGG16_MURA,
cache_subdir="models")
model = load_model(path_weights)
return model
def preprocess_image(img_path, target_size=(224, 224)):
"""Preprocess the image by reshape and normalization.
Args:
img_path: A string.
target_size: A tuple, reshape to this size.
Return:
An image array.
"""
img = image.load_img(img_path, target_size=target_size)
img = image.img_to_array(img)
img /= 255
return img
def show_imgwithheat(img_path, heatmap, alpha=0.4, return_array=False):
"""Show the image with heatmap.
Args:
img_path: string.
heatmap: image array, get it by calling grad_cam().
alpha: float, transparency of heatmap.
return_array: bool, return a superimposed image array or not.
Return:
None or image array.
"""
img = cv2.imread(img_path)
heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
heatmap = (heatmap*255).astype("uint8")
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
superimposed_img = heatmap * alpha + img
superimposed_img = np.clip(superimposed_img, 0, 255).astype("uint8")
superimposed_img = cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB)
imgwithheat = Image.fromarray(superimposed_img)
try:
display(imgwithheat)
except NameError:
imgwithheat.show()
if return_array:
return superimposed_img