forked from ayushdabra/dubai-satellite-imagery-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_activations.py
97 lines (72 loc) · 3.57 KB
/
get_activations.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
91
92
93
94
95
96
97
!pip install keract
import keract
import numpy as np
import pandas as pd
import albumentations as A
import matplotlib.pyplot as plt
%matplotlib inline
import os, re, sys, random, shutil, cv2
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam, Nadam
from tensorflow.keras import applications, optimizers
from tensorflow.keras.applications import InceptionResNetV2
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Activation, MaxPool2D, Conv2DTranspose, Concatenate, ZeroPadding2D, Dropout
def conv_block(input, num_filters):
x = Conv2D(num_filters, 3, padding="same")(input)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(num_filters, 3, padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
return x
def decoder_block(input, skip_features, num_filters):
x = Conv2DTranspose(num_filters, (2, 2), strides=2, padding="same")(input)
x = Concatenate()([x, skip_features])
x = conv_block(x, num_filters)
return x
def build_inception_resnetv2_unet(input_shape):
""" Input """
inputs = Input(input_shape)
""" Pre-trained InceptionResNetV2 Model """
encoder = InceptionResNetV2(include_top=False, weights="imagenet", input_tensor=inputs)
""" Encoder """
s1 = encoder.get_layer("input_1").output ## (512 x 512)
s2 = encoder.get_layer("activation").output ## (255 x 255)
s2 = ZeroPadding2D(( (1, 0), (1, 0) ))(s2) ## (256 x 256)
s3 = encoder.get_layer("activation_3").output ## (126 x 126)
s3 = ZeroPadding2D((1, 1))(s3) ## (128 x 128)
s4 = encoder.get_layer("activation_74").output ## (61 x 61)
s4 = ZeroPadding2D(( (2, 1),(2, 1) ))(s4) ## (64 x 64)
""" Bridge """
b1 = encoder.get_layer("activation_161").output ## (30 x 30)
b1 = ZeroPadding2D((1, 1))(b1) ## (32 x 32)
""" Decoder """
d1 = decoder_block(b1, s4, 512) ## (64 x 64)
d2 = decoder_block(d1, s3, 256) ## (128 x 128)
d3 = decoder_block(d2, s2, 128) ## (256 x 256)
d4 = decoder_block(d3, s1, 64) ## (512 x 512)
""" Output """
dropout = Dropout(0.3)(d4)
outputs = Conv2D(6, 1, padding="same", activation="softmax")(dropout)
model = Model(inputs, outputs, name="InceptionResNetV2-UNet")
return model
K.clear_session()
def dice_coef(y_true, y_pred):
return (2. * K.sum(y_true * y_pred) + 1.) / (K.sum(y_true) + K.sum(y_pred) + 1.)
model = build_inception_resnetv2_unet(input_shape = (512, 512, 3))
model.compile(optimizer=Adam(lr = 0.0001), loss='categorical_crossentropy', metrics=[dice_coef, "accuracy"])
model.summary()
model.load_weights("../input/inceptionresnetv2unet/InceptionResNetV2-UNet.h5")
!mkdir activations
image = load_img('../input/augmented-dubai-aerial-imagery-dataset/val_images/val/image_t4_008.jpg', target_size= (512, 512))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)
y_hat = model.predict(image)
activations= keract.get_activations(model, image, nodes_to_evaluate= None, output_format= 'simple', auto_compile= True)
keract.display_activations(activations, cmap='viridis', save= True, directory= './activations')