kadirnar commited on
Commit
e67f813
·
1 Parent(s): 9563531
Files changed (6) hide show
  1. app.py +42 -0
  2. data/testv1.jpg +0 -0
  3. data/testv2.jpg +0 -0
  4. data/testv3.jpg +0 -0
  5. requirements.txt +6 -0
  6. utils.py +91 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from skimage.util import montage as montage2d
2
+ from utils import load_model, preprocess_image, attempt_download_from_hub
3
+ import matplotlib.pyplot as plt
4
+
5
+ import gradio as gr
6
+
7
+
8
+ def keras_inference(img_data, model_path):
9
+ model_path = attempt_download_from_hub(model_path)
10
+ seg_model = load_model(model_path)
11
+ out_img = preprocess_image(img_data)
12
+ pred_y = seg_model.predict(out_img)
13
+
14
+ plt.imshow(montage2d(pred_y[:, :, :, 0]), cmap = 'bone_r')
15
+ plt.savefig('output.png')
16
+ return 'output.png'
17
+
18
+ inputs = [
19
+ gr.Image(type='filepath', label='Image'),
20
+ gr.Dropdown(['keras_model.h5'], label='Model Path')
21
+ ]
22
+
23
+ outputs = gr.Image(label='Segmentation')
24
+
25
+ examples = [
26
+ ['data/testv1.jpg', 'kadirnar/Keras-Segmenting-Buildings-v1'],
27
+ ['data/testv2.jpg', 'kadirnar/Keras-Segmenting-Buildings-v1'],
28
+ ['data/testv3.jpg', 'kadirnar/Keras-Segmenting-Buildings-v1'],
29
+ ]
30
+
31
+ title = 'Segmenting Buildings in Satellite Images with Keras'
32
+
33
+ demo_app = gr.Interface(
34
+ keras_inference,
35
+ inputs,
36
+ outputs,
37
+ title=title,
38
+ examples=examples,
39
+ cache_examples=True,
40
+ )
41
+
42
+ demo_app.launch(debug=True, enable_queue=True)
data/testv1.jpg ADDED
data/testv2.jpg ADDED
data/testv3.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ huggingface_hub==0.12.0
2
+ keras==2.11.0
3
+ matplotlib==3.6.2
4
+ numpy==1.23.5
5
+ opencv_python==4.6.0.66
6
+ scikit_image==0.19.3
utils.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #https://www.kaggle.com/code/kmader/segmenting-buildings-in-satellite-images
2
+
3
+ from keras import models, layers
4
+ from skimage.io import imread
5
+ import numpy as np
6
+ import cv2
7
+
8
+ t_x_shape = (300, 300, 3)
9
+ GAUSSIAN_NOISE = 0.1
10
+ BASE_DEPTH = 16
11
+ BLOCK_COUNT = 1
12
+ SPATIAL_DROPOUT = 0.25
13
+ EDGE_CROP = 16
14
+
15
+
16
+ def conv_bn(x, filt, dl_rate=(1,1), preblock = False):
17
+ y = layers.Convolution2D(filt, (3, 3),
18
+ activation='linear',
19
+ padding='same',
20
+ dilation_rate=dl_rate,
21
+ use_bias=False)(x)
22
+ if preblock: return y
23
+ y = layers.BatchNormalization()(y)
24
+ return layers.Activation('elu')(y)
25
+
26
+ def keras_model():
27
+ in_layer = layers.Input(t_x_shape, name = 'RGB_Input')
28
+ pp_in_layer = layers.GaussianNoise(GAUSSIAN_NOISE)(in_layer)
29
+ pp_in_layer = layers.BatchNormalization()(pp_in_layer)
30
+
31
+ c = conv_bn(pp_in_layer, BASE_DEPTH//2)
32
+ c = conv_bn(c, BASE_DEPTH//2)
33
+ c = conv_bn(c, BASE_DEPTH)
34
+
35
+ skip_layers = [pp_in_layer]
36
+ for j in range(BLOCK_COUNT):
37
+ depth_steps = int(np.log2(t_x_shape[0])-2)
38
+ d = layers.concatenate(skip_layers+[conv_bn(c, BASE_DEPTH*2**j, (2**i, 2**i), preblock=True)
39
+ for i in range(depth_steps)])
40
+ d = layers.SpatialDropout2D(SPATIAL_DROPOUT)(d)
41
+ d = layers.BatchNormalization()(d)
42
+ d = layers.Activation('elu')(d)
43
+ # bottleneck
44
+ d = conv_bn(d, BASE_DEPTH*2**(j+1))
45
+ skip_layers += [c]
46
+ c = d
47
+ d = layers.Convolution2D(1, (1, 1), activation='sigmoid', padding='same')(d)
48
+ d = layers.Cropping2D((EDGE_CROP, EDGE_CROP))(d)
49
+ d = layers.ZeroPadding2D((EDGE_CROP, EDGE_CROP))(d)
50
+
51
+ seg_model = models.Model(inputs = [in_layer],outputs = [d])
52
+
53
+ return seg_model
54
+
55
+ def load_model(weight_path):
56
+ seg_model = keras_model()
57
+ seg_model.load_weights(weight_path)
58
+ return seg_model
59
+
60
+ def preprocess_image(img_data):
61
+ img_data = imread(img_data)
62
+
63
+ if img_data.shape[:2] == (300, 300):
64
+ img_data = img_data
65
+ else:
66
+ img_data = cv2.resize(img_data, (300, 300))
67
+
68
+ out_img = []
69
+ out_img += [img_data]
70
+
71
+ out_img = (np.stack(out_img, 0)/255.0).astype(np.float32)
72
+ return out_img
73
+
74
+
75
+ def attempt_download_from_hub(repo_id, hf_token=None):
76
+ # https://github.com/fcakyon/yolov5-pip/blob/main/yolov5/utils/downloads.py
77
+ from huggingface_hub import hf_hub_download, list_repo_files
78
+ from huggingface_hub.utils._errors import RepositoryNotFoundError
79
+ from huggingface_hub.utils._validators import HFValidationError
80
+ try:
81
+ repo_files = list_repo_files(repo_id=repo_id, repo_type='model', token=hf_token)
82
+ model_file = [f for f in repo_files if f.endswith('.h5')][0]
83
+ file = hf_hub_download(
84
+ repo_id=repo_id,
85
+ filename=model_file,
86
+ repo_type='model',
87
+ token=hf_token,
88
+ )
89
+ return file
90
+ except (RepositoryNotFoundError, HFValidationError):
91
+ return None