dbuscombe commited on
Commit
2d8181a
1 Parent(s): 7e137c1
.gitattributes CHANGED
@@ -32,3 +32,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ examples/*.* filter=lfs diff=lfs merge=lfs -text
36
+ saved_model/*.* filter=lfs diff=lfs merge=lfs -text
37
+ saved_model/*.* filter=lfs diff=lfs merge=lfs -text
38
+ examples/*.* filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import os
4
+ os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
5
+
6
+ from glob import glob
7
+ import tensorflow as tf
8
+ import matplotlib.pyplot as plt
9
+ from skimage.transform import resize
10
+ from skimage.io import imsave
11
+ from skimage.filters import threshold_otsu
12
+
13
+ from doodleverse_utils.prediction_imports import *
14
+ from doodleverse_utils.imports import *
15
+
16
+
17
+ #load model
18
+ filepath = './saved_model'
19
+ model = tf.keras.models.load_model(filepath, compile = True)
20
+ model.compile
21
+
22
+ #segmentation
23
+ def segment(input_img, use_tta, use_otsu, dims=(512, 512)):
24
+
25
+ N = 2
26
+
27
+ if use_otsu:
28
+ print("Use Otsu threshold")
29
+ else:
30
+ print("No Otsu threshold")
31
+
32
+ if use_tta:
33
+ print("Use TTA")
34
+ else:
35
+ print("Do not use TTA")
36
+
37
+
38
+ worig, horig, channels = input_img.shape
39
+
40
+ w, h = dims[0], dims[1]
41
+
42
+ print("Original dimensions {}x{}".format(worig,horig))
43
+ print("New dimensions {}x{}".format(w,h))
44
+
45
+ img = standardize(input_img)
46
+
47
+ img = resize(img, dims, preserve_range=True, clip=True)
48
+
49
+ img = np.expand_dims(img,axis=0)
50
+
51
+ est_label = model.predict(img)
52
+
53
+ if use_tta:
54
+ #Test Time Augmentation
55
+ est_label2 = np.flipud(model.predict((np.flipud(img)), batch_size=1))
56
+ est_label3 = np.fliplr(model.predict((np.fliplr(img)), batch_size=1))
57
+ est_label4 = np.flipud(np.fliplr(model.predict((np.flipud(np.fliplr(img))))))
58
+
59
+ #soft voting - sum the softmax scores to return the new TTA estimated softmax scores
60
+ est_label = est_label + est_label2 + est_label3 + est_label4
61
+ est_label /= 4
62
+
63
+ pred = np.squeeze(est_label, axis=0)
64
+ pred = resize(pred, (worig, horig), preserve_range=True, clip=True)
65
+
66
+ mask = np.argmax(pred,-1)
67
+
68
+ imsave("greyscale_download_me.png", mask.astype('uint8'))
69
+
70
+ class_label_colormap = [
71
+ "#3366CC",
72
+ "#DC3912",
73
+ "#FF9900",
74
+ "#109618",
75
+ "#990099",
76
+ "#0099C6",
77
+ "#DD4477",
78
+ "#66AA00",
79
+ "#B82E2E",
80
+ "#316395",
81
+ ]
82
+
83
+ # add classes
84
+ class_label_colormap = class_label_colormap[:N]
85
+
86
+ color_label = label_to_colors(
87
+ mask,
88
+ input_img[:, :, 0] == 0,
89
+ alpha=128,
90
+ colormap=class_label_colormap,
91
+ color_class_offset=0,
92
+ do_alpha=False,
93
+ )
94
+
95
+ imsave("color_download_me.png", color_label)
96
+
97
+
98
+ if use_otsu:
99
+ thres = threshold_otsu(mask)
100
+ print("Otsu threshold is {}".format(thres))
101
+ water_nowater = (mask>thres).astype('uint8')
102
+ else:
103
+ water_nowater = (mask>=1).astype('uint8')
104
+
105
+
106
+ #overlay plot
107
+ plt.clf()
108
+ plt.subplot(121)
109
+ plt.imshow(input_img[:,:,-1],cmap='gray')
110
+ plt.imshow(color_label, alpha=0.4)
111
+ plt.axis("off")
112
+ plt.margins(x=0, y=0)
113
+
114
+ plt.subplot(122)
115
+ plt.imshow(input_img[:,:,-1],cmap='gray')
116
+ plt.contour(water_nowater, levels=[0], colors='r')
117
+ plt.axis("off")
118
+ plt.margins(x=0, y=0)
119
+
120
+ plt.savefig("overlay_download_me.png", dpi=300, bbox_inches="tight")
121
+
122
+ return color_label, plt , "greyscale_download_me.png", "color_download_me.png", "overlay_download_me.png"
123
+
124
+
125
+
126
+ with open("article.html", "r", encoding='utf-8') as f:
127
+ article= f.read()
128
+
129
+ title = "Segment Satellite imagery"
130
+ description = "This simple model demonstration segments 15-m Landsat-7/8 or 10-m Sentinel-2 RGB (visible spectrum) imagery into the following classes: 1. water and 2. other. Please note that, ordinarily, ensemble models are used in predictive mode. Here, we are using just one model, i.e. without ensembling. Allows upload of 3-band imagery in jpg format and download of label imagery only one at a time. "
131
+
132
+
133
+ examples= [[l] for l in glob('examples/*.jpg')]
134
+
135
+ inp = gr.Image()
136
+ out1 = gr.Image(type='numpy')
137
+ out2 = gr.Plot(type='matplotlib')
138
+ out3 = gr.File()
139
+ out4 = gr.File()
140
+ out5 = gr.File()
141
+
142
+ inp2 = gr.inputs.Checkbox(default=False, label="Use TTA")
143
+ inp3 = gr.inputs.Checkbox(default=False, label="Use Otsu")
144
+
145
+ Segapp = gr.Interface(segment, [inp, inp2, inp3],
146
+ [out1, out2, out3, out4, out5],
147
+ title = title, description = description, examples=examples, article=article,
148
+ theme="grass")
149
+
150
+ Segapp.launch(enable_queue=True)
151
+
152
+
article.html ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ <p style='text-align: center'>
2
+ <a href='https://github.com/Doodleverse' target='_blank'>Part of the Doodleverse</a> | <a href='https://github.com/MARDAScience' target='_blank'>MARDAScience Github Repo</a> | <a href='https://zenodo.org/record/7384255' target='_blank'>Zenodo model repository</a>
3
+ </p>
example_images/2013-04-14-15-42-53_L8_DUCK_SDS_BENCHMARK_rgb_pan.jpg ADDED
example_images/2017-09-02-18-46-11_RGB_L8.jpg ADDED
example_images/2019-03-05-10-47-51_L8_TRUCVERT_SDS_BENCHMARK_rgb_pan.jpg ADDED
example_images/20190715T154911_N0213_R054_T18TWL_20190715T200600_010646.jpg ADDED
example_images/20190730T154819_20190730T155818_T18SVF_lrr_db.jpg ADDED
example_images/20190804T154911_20190804T160118_T18SVF_lrr_ID1.jpg ADDED
example_images/20191124T154549_20191124T154739_T18SVF_lrr_db.jpg ADDED
example_images/20191128T144731_N0213_R139_T20PLS_20191128T171514_013769.jpg ADDED
example_images/20191222T125301_N0213_R052_T24MXV_20191222T144240_016520.jpg ADDED
example_images/20200207T113309_N0214_R080_T28PCA_20200207T132518_020527.jpg ADDED
example_images/20200207T163439_N0214_R083_T16RBU_20200207T201114_022656.jpg ADDED
example_images/20200207T163439_N0214_R083_T16RBU_20200207T201114_023215.jpg ADDED
example_images/20200207T163439_N0214_R083_T16RBU_20200207T201114_023538.jpg ADDED
example_images/2021-09-17-18-22-50_L8_TORREYPINES_SDS_BENCHMARK_rgb_pan.jpg ADDED
example_images/2021-10-26-23-44-09_L8_NARRABEEN_SDS_BENCHMARK_rgb_pan.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ tensorflow
2
+ numpy
3
+ matplotlib
4
+ scikit-image
5
+ doodleverse_utils
saved_model/keras_metadata.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:55bc51ea5be7fae6332e9aa0c4a28cacf8d70b71046d4dfcae2c62a350473dcc
3
+ size 223371
saved_model/saved_model.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7bfd3ea0da120d8d8e8f92338955e44c584bc187c108640ff13556c68a1d38f2
3
+ size 1859619
saved_model/variables/variables.data-00000-of-00001 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:44428075abf569220b6484cd0de2a05774788f27a11467447f19941fe4b13cff
3
+ size 23047137
saved_model/variables/variables.index ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2393079c3b5b548d55173010ff1867fb0a9eacd9169cfdb39d5d00f5712be92a
3
+ size 10734