Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Deeply Hydrated
gun-classification-model
Commits
43d7e709
Commit
43d7e709
authored
Nov 04, 2021
by
Jakob Overgaard
Browse files
heat map not working
parent
fa1cdbb4
Changes
9
Hide whitespace changes
Inline
Side-by-side
data.zip
0 → 100644
View file @
43d7e709
File added
models/version1/keras_metadata.pb
0 → 100644
View file @
43d7e709
Rroot"_tf_keras_sequential*R{"name": "sequential", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "must_restore_from_config": false, "class_name": "Sequential", "config": {"name": "sequential", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 180, 180, 3]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "rescaling_1_input"}}, {"class_name": "Rescaling", "config": {"name": "rescaling_1", "trainable": true, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 180, 180, 3]}, "dtype": "float32", "scale": 0.00392156862745098, "offset": 0.0}}, {"class_name": "Conv2D", "config": {"name": "conv2d", "trainable": true, "dtype": "float32", "filters": 16, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [2, 2]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [2, 2]}, "data_format": "channels_last"}}, {"class_name": "Conv2D", "config": {"name": "conv2d_1", "trainable": true, "dtype": "float32", "filters": 32, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_1", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [2, 2]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [2, 2]}, "data_format": "channels_last"}}, {"class_name": "Conv2D", "config": {"name": "conv2d_2", "trainable": true, "dtype": "float32", "filters": 64, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "maxPool2", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [1, 1]}, "data_format": "channels_last"}}, {"class_name": "Flatten", "config": {"name": "flatten", "trainable": true, "dtype": "float32", "data_format": "channels_last"}}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 3, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "shared_object_id": 21, "build_input_shape": {"class_name": "TensorShape", "items": [null, 180, 180, 3]}, "is_graph_network": true, "full_save_spec": {"class_name": "__tuple__", "items": [[{"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 180, 180, 3]}, "float32", "rescaling_1_input"]}], {}]}, "save_spec": {"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 180, 180, 3]}, "float32", "rescaling_1_input"]}, "keras_version": "2.6.0", "backend": "tensorflow", "model_config": {"class_name": "Sequential", "config": {"name": "sequential", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 180, 180, 3]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "rescaling_1_input"}, "shared_object_id": 0}, {"class_name": "Rescaling", "config": {"name": "rescaling_1", "trainable": true, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 180, 180, 3]}, "dtype": "float32", "scale": 0.00392156862745098, "offset": 0.0}, "shared_object_id": 1}, {"class_name": "Conv2D", "config": {"name": "conv2d", "trainable": true, "dtype": "float32", "filters": 16, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 4}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [2, 2]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [2, 2]}, "data_format": "channels_last"}, "shared_object_id": 5}, {"class_name": "Conv2D", "config": {"name": "conv2d_1", "trainable": true, "dtype": "float32", "filters": 32, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 6}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 7}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 8}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_1", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [2, 2]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [2, 2]}, "data_format": "channels_last"}, "shared_object_id": 9}, {"class_name": "Conv2D", "config": {"name": "conv2d_2", "trainable": true, "dtype": "float32", "filters": 64, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 10}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 11}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 12}, {"class_name": "MaxPooling2D", "config": {"name": "maxPool2", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [1, 1]}, "data_format": "channels_last"}, "shared_object_id": 13}, {"class_name": "Flatten", "config": {"name": "flatten", "trainable": true, "dtype": "float32", "data_format": "channels_last"}, "shared_object_id": 14}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 15}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 16}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 17}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 3, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 18}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 19}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 20}]}}, "training_config": {"loss": {"class_name": "SparseCategoricalCrossentropy", "config": {"reduction": "auto", "name": "sparse_categorical_crossentropy", "from_logits": true}, "shared_object_id": 22}, "metrics": [[{"class_name": "MeanMetricWrapper", "config": {"name": "accuracy", "dtype": "float32", "fn": "sparse_categorical_accuracy"}, "shared_object_id": 23}]], "weighted_metrics": null, "loss_weights": null, "optimizer_config": {"class_name": "Adam", "config": {"name": "Adam", "learning_rate": 0.0010000000474974513, "decay": 0.0, "beta_1": 0.8999999761581421, "beta_2": 0.9990000128746033, "epsilon": 1e-07, "amsgrad": false}}}}2
root.layer-0"_tf_keras_layer*{"name": "rescaling_1", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": {"class_name": "__tuple__", "items": [null, 180, 180, 3]}, "stateful": false, "must_restore_from_config": false, "class_name": "Rescaling", "config": {"name": "rescaling_1", "trainable": true, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 180, 180, 3]}, "dtype": "float32", "scale": 0.00392156862745098, "offset": 0.0}, "shared_object_id": 1}2
root.layer_with_weights-0"_tf_keras_layer* {"name": "conv2d", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Conv2D", "config": {"name": "conv2d", "trainable": true, "dtype": "float32", "filters": 16, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 4, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 4, "axes": {"-1": 3}}, "shared_object_id": 24}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 180, 180, 3]}}2
root.layer-2"_tf_keras_layer*{"name": "max_pooling2d", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "MaxPooling2D", "config": {"name": "max_pooling2d", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [2, 2]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [2, 2]}, "data_format": "channels_last"}, "shared_object_id": 5, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": 4, "max_ndim": null, "min_ndim": null, "axes": {}}, "shared_object_id": 25}}2
root.layer_with_weights-1"_tf_keras_layer* {"name": "conv2d_1", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Conv2D", "config": {"name": "conv2d_1", "trainable": true, "dtype": "float32", "filters": 32, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 6}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 7}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 8, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 4, "axes": {"-1": 16}}, "shared_object_id": 26}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 90, 90, 16]}}2
root.layer-4"_tf_keras_layer*{"name": "max_pooling2d_1", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_1", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [2, 2]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [2, 2]}, "data_format": "channels_last"}, "shared_object_id": 9, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": 4, "max_ndim": null, "min_ndim": null, "axes": {}}, "shared_object_id": 27}}2
root.layer_with_weights-2"_tf_keras_layer* {"name": "conv2d_2", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Conv2D", "config": {"name": "conv2d_2", "trainable": true, "dtype": "float32", "filters": 64, "kernel_size": {"class_name": "__tuple__", "items": [3, 3]}, "strides": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "same", "data_format": "channels_last", "dilation_rate": {"class_name": "__tuple__", "items": [1, 1]}, "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 10}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 11}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 12, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 4, "axes": {"-1": 32}}, "shared_object_id": 28}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 45, 45, 32]}}2
root.layer-6"_tf_keras_layer*{"name": "maxPool2", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "MaxPooling2D", "config": {"name": "maxPool2", "trainable": true, "dtype": "float32", "pool_size": {"class_name": "__tuple__", "items": [1, 1]}, "padding": "valid", "strides": {"class_name": "__tuple__", "items": [1, 1]}, "data_format": "channels_last"}, "shared_object_id": 13, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": 4, "max_ndim": null, "min_ndim": null, "axes": {}}, "shared_object_id": 29}}2
root.layer-7"_tf_keras_layer*{"name": "flatten", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Flatten", "config": {"name": "flatten", "trainable": true, "dtype": "float32", "data_format": "channels_last"}, "shared_object_id": 14, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 1, "axes": {}}, "shared_object_id": 30}}2
root.layer_with_weights-3"_tf_keras_layer*{"name": "dense", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 15}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 16}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 17, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 129600}}, "shared_object_id": 31}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 129600]}}2
root.layer_with_weights-4"_tf_keras_layer*{"name": "dense_1", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 3, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 18}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 19}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 20, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 128}}, "shared_object_id": 32}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 128]}}2
root.keras_api.metrics.0"_tf_keras_metric*{"class_name": "Mean", "name": "loss", "dtype": "float32", "config": {"name": "loss", "dtype": "float32"}, "shared_object_id": 33}2
root.keras_api.metrics.1"_tf_keras_metric*{"class_name": "MeanMetricWrapper", "name": "accuracy", "dtype": "float32", "config": {"name": "accuracy", "dtype": "float32", "fn": "sparse_categorical_accuracy"}, "shared_object_id": 23}2
\ No newline at end of file
models/version1/saved_model.pb
0 → 100644
View file @
43d7e709
File added
models/version1/variables/variables.data-00000-of-00001
0 → 100644
View file @
43d7e709
File added
models/version1/variables/variables.index
0 → 100644
View file @
43d7e709
File added
src/models/train_model.py
View file @
43d7e709
...
...
@@ -70,6 +70,8 @@ print(np.min(first_image), np.max(first_image))
num_classes
=
3
model
=
Sequential
([
layers
.
Rescaling
(
1.
/
255
,
input_shape
=
(
img_height
,
img_width
,
3
)),
layers
.
Conv2D
(
16
,
3
,
padding
=
'same'
,
activation
=
'relu'
),
...
...
@@ -77,23 +79,26 @@ model = Sequential([
layers
.
Conv2D
(
32
,
3
,
padding
=
'same'
,
activation
=
'relu'
),
layers
.
MaxPooling2D
(),
layers
.
Conv2D
(
64
,
3
,
padding
=
'same'
,
activation
=
'relu'
),
layers
.
MaxPooling2D
(),
layers
.
MaxPooling2D
(
1
,
name
=
'maxPool2'
),
layers
.
Flatten
(),
layers
.
Dense
(
128
,
activation
=
'relu'
),
layers
.
Dense
(
num_classes
)
])
model
.
compile
(
optimizer
=
'adam'
,
loss
=
tf
.
keras
.
losses
.
SparseCategoricalCrossentropy
(
from_logits
=
True
),
metrics
=
[
'accuracy'
])
epochs
=
10
history
=
model
.
fit
(
train_ds
,
validation_data
=
val_ds
,
epochs
=
epochs
)
outputs
=
[
layer
.
output
for
layer
in
model
.
layers
]
model
.
save
(
"../../models/version1"
)
...
...
src/visualization/cam.jpg
0 → 100644
View file @
43d7e709
58 KB
src/visualization/heatMap.py
View file @
43d7e709
from
keras_preprocessing
import
image
import
numpy
as
np
import
tensorflow
as
tf
from
tensorflow
import
keras
from
keras.preprocessing.image
import
load_img
# Display
from
IPython.display
import
Image
,
display
import
matplotlib.pyplot
as
plt
import
matplotlib.cm
as
cm
import
matplotlib.image
as
mpimg
import
pathlib
data_dir
=
pathlib
.
Path
(
"../../data/"
)
model_builder
=
keras
.
applications
.
xception
.
Xception
img_size
=
(
299
,
299
)
...
...
@@ -15,9 +20,72 @@ decode_predictions = keras.applications.xception.decode_predictions
last_conv_layer_name
=
"block14_sepconv2_act"
# The local path to our target image
img_path
=
keras
.
utils
.
get_file
(
"test-tree.jpg"
,
)
img
=
mpimg
.
imread
(
data_dir
/
"test-tree.jpg"
)
img_path
=
img
display
(
Image
(
img_path
))
\ No newline at end of file
imgPlot
=
plt
.
imshow
(
img
)
plt
.
show
()
def
get_img_array
(
img_path
,
size
):
# `img` is a PIL image of size 299x299
img
=
keras
.
preprocessing
.
image
.
load_img
(
img_path
,
target_size
=
size
)
# `array` is a float32 Numpy array of shape (299, 299, 3)
array
=
keras
.
preprocessing
.
image
.
img_to_array
(
img
)
# We add a dimension to transform our array into a "batch"
# of size (1, 299, 299, 3)
array
=
np
.
expand_dims
(
array
,
axis
=
0
)
return
array
def
make_gradcam_heatmap
(
img_array
,
model
,
last_conv_layer_name
,
pred_index
=
None
):
# First, we create a model that maps the input image to the activations
# of the last conv layer as well as the output predictions
grad_model
=
tf
.
keras
.
models
.
Model
(
[
model
.
inputs
],
[
model
.
get_layer
(
last_conv_layer_name
).
output
,
model
.
output
]
)
# Then, we compute the gradient of the top predicted class for our input image
# with respect to the activations of the last conv layer
with
tf
.
GradientTape
()
as
tape
:
last_conv_layer_output
,
preds
=
grad_model
(
img_array
)
if
pred_index
is
None
:
pred_index
=
tf
.
argmax
(
preds
[
0
])
class_channel
=
preds
[:,
pred_index
]
# This is the gradient of the output neuron (top predicted or chosen)
# with regard to the output feature map of the last conv layer
grads
=
tape
.
gradient
(
class_channel
,
last_conv_layer_output
)
# This is a vector where each entry is the mean intensity of the gradient
# over a specific feature map channel
pooled_grads
=
tf
.
reduce_mean
(
grads
,
axis
=
(
0
,
1
,
2
))
# We multiply each channel in the feature map array
# by "how important this channel is" with regard to the top predicted class
# then sum all the channels to obtain the heatmap class activation
last_conv_layer_output
=
last_conv_layer_output
[
0
]
heatmap
=
last_conv_layer_output
@
pooled_grads
[...,
tf
.
newaxis
]
heatmap
=
tf
.
squeeze
(
heatmap
)
# For visualization purpose, we will also normalize the heatmap between 0 & 1
heatmap
=
tf
.
maximum
(
heatmap
,
0
)
/
tf
.
math
.
reduce_max
(
heatmap
)
return
heatmap
.
numpy
()
# Prepare image
img_array
=
preprocess_input
(
get_img_array
(
img_path
,
size
=
img_size
))
# Make model
model
=
model_builder
(
weights
=
"imagenet"
)
# Remove last layer's softmax
model
.
layers
[
-
1
].
activation
=
None
# Print what the top predicted class is
preds
=
model
.
predict
(
img_array
)
print
(
"Predicted:"
,
decode_predictions
(
preds
,
top
=
1
)[
0
])
# Generate class activation heatmap
heatmap
=
make_gradcam_heatmap
(
img_array
,
model
,
last_conv_layer_name
)
# Display heatmap
plt
.
matshow
(
heatmap
)
plt
.
show
()
\ No newline at end of file
src/visualization/heatMapGet.py
0 → 100644
View file @
43d7e709
import
tensorflow
as
tf
import
tensorflow.keras.backend
as
K
from
tensorflow
import
keras
from
tensorflow.keras.applications.inception_v3
import
InceptionV3
from
tensorflow.keras.preprocessing
import
image
from
tensorflow.keras.applications.inception_v3
import
preprocess_input
,
decode_predictions
import
numpy
as
np
import
os
import
matplotlib.pyplot
as
plt
import
cv2
import
pathlib
model_dir
=
pathlib
.
Path
(
"../../models/version1"
)
data_dir
=
pathlib
.
Path
(
"../../data/"
)
model
=
model_dir
ORIGINAL
=
(
data_dir
/
"test-tree.jpg"
)
DIM
=
299
img
=
image
.
load_img
(
ORIGINAL
,
target_size
=
(
DIM
,
DIM
))
imgPlot
=
plt
.
imshow
(
img
)
plt
.
show
()
x
=
image
.
img_to_array
(
img
)
x
=
np
.
expand_dims
(
x
,
axis
=
0
)
x
=
preprocess_input
(
x
)
with
tf
.
GradientTape
()
as
tape
:
last_conv_layer
=
model
.
get_layer
(
'conv2d_93'
)
iterate
=
tf
.
keras
.
models
.
Model
([
model
.
inputs
],
[
model
.
output
,
last_conv_layer
.
output
])
model_out
,
last_conv_layer
=
iterate
(
x
)
class_out
=
model_out
[:,
np
.
argmax
(
model_out
[
0
])]
grads
=
tape
.
gradient
(
class_out
,
last_conv_layer
)
pooled_grads
=
K
.
mean
(
grads
,
axis
=
(
0
,
1
,
2
))
heatmap
=
tf
.
reduce_mean
(
tf
.
multiply
(
pooled_grads
,
last_conv_layer
),
axis
=-
1
)
heatmap
=
np
.
maximum
(
heatmap
,
0
)
heatmap
/=
np
.
max
(
heatmap
)
print
(
heatmap
.
shape
)
heatmap
=
heatmap
.
reshape
((
8
,
8
))
plt
.
matshow
(
heatmap
)
plt
.
show
()
img
=
plt
.
imread
(
ORIGINAL
)
INTENSITY
=
0.5
heatmap
=
cv2
.
resize
(
heatmap
,
(
img
.
shape
[
1
],
img
.
shape
[
0
]))
heatmap
=
cv2
.
applyColorMap
(
np
.
uint8
(
255
*
heatmap
),
cv2
.
COLORMAP_JET
)
img
=
heatmap
*
INTENSITY
+
img
imgPlot
=
plt
.
imshow
(
img
)
plt
.
show
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment