Spaces:
Sleeping
Sleeping
ignaziogallo
commited on
Commit
·
08c1d69
1
Parent(s):
b7e5937
added new samples
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- app.py +102 -17
- demo_data/lombardia/{example/2 → 2019/1509}/20190104.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190109.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190114.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190119.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190124.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190129.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190203.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190208.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190213.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190218.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190223.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190228.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190305.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190310.tif +2 -2
- demo_data/lombardia/{example/2/20190104_MSAVI.tif → 2019/1509/20190315.tif} +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190320.tif +2 -2
- demo_data/lombardia/{example/2/20190109_MSAVI.tif → 2019/1509/20190325.tif} +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190330.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190404.tif +2 -2
- demo_data/lombardia/2019/1509/20190409.tif +3 -0
- demo_data/lombardia/{example/2 → 2019/1509}/20190414.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190419.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190424.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190429.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190504.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190509.tif +2 -2
- demo_data/lombardia/2019/1509/20190514.tif +3 -0
- demo_data/lombardia/{example/2 → 2019/1509}/20190519.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190524.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190529.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190603.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190613.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190618.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190623.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190628.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190703.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190708.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190713.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190718.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190723.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190728.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190802.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190807.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190812.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190817.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190822.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190827.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190901.tif +2 -2
- demo_data/lombardia/{example/2 → 2019/1509}/20190906.tif +2 -2
app.py
CHANGED
@@ -1,11 +1,63 @@
|
|
1 |
-
import os
|
2 |
import streamlit as st
|
3 |
import zipfile
|
4 |
-
import torch
|
5 |
from utils import *
|
|
|
6 |
import matplotlib.pyplot as plt
|
|
|
|
|
7 |
from matplotlib import colors
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
if not hasattr(st, 'paths'):
|
10 |
st.paths = None
|
11 |
if not hasattr(st, 'daily_model'):
|
@@ -43,7 +95,7 @@ if not hasattr(st, 'daily_model'):
|
|
43 |
|
44 |
|
45 |
st.title('In-season and dynamic crop mapping using 3D convolution neural networks and sentinel-2 time series')
|
46 |
-
st.markdown(""" Demo App for the model presented in the paper:
|
47 |
```
|
48 |
@article{gallo2022in_season,
|
49 |
title = {In-season and dynamic crop mapping using 3D convolution neural networks and sentinel-2 time series},
|
@@ -57,6 +109,7 @@ st.markdown(""" Demo App for the model presented in the paper:
|
|
57 |
author = {Ignazio Gallo and Luigi Ranghetti and Nicola Landro and Riccardo {La Grassa} and Mirco Boschetti},
|
58 |
}
|
59 |
```
|
|
|
60 |
""")
|
61 |
|
62 |
file_uploaded = st.file_uploader(
|
@@ -65,6 +118,7 @@ file_uploaded = st.file_uploader(
|
|
65 |
accept_multiple_files=False,
|
66 |
)
|
67 |
sample_path = None
|
|
|
68 |
if file_uploaded is not None:
|
69 |
with zipfile.ZipFile(file_uploaded, "r") as z:
|
70 |
z.extractall("uploaded_samples")
|
@@ -72,10 +126,20 @@ if file_uploaded is not None:
|
|
72 |
st.markdown('or use a demo sample')
|
73 |
if st.button('sample 1'):
|
74 |
sample_path = 'demo_data/lombardia'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
paths = None
|
77 |
if sample_path is not None:
|
78 |
-
st.markdown(f'elaborating {sample_path}...')
|
79 |
|
80 |
validationdataset = SentinelDailyAnnualDatasetNoLabel(
|
81 |
sample_path,
|
@@ -83,11 +147,11 @@ if sample_path is not None:
|
|
83 |
opt.classes_path,
|
84 |
opt.sample_duration,
|
85 |
opt.win_size,
|
86 |
-
tileids=
|
87 |
validationdataloader = torch.utils.data.DataLoader(
|
88 |
validationdataset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.workers)
|
89 |
|
90 |
-
st.markdown(
|
91 |
|
92 |
out_dir = os.path.join(opt.result_path, "seg_maps")
|
93 |
if not os.path.exists(out_dir):
|
@@ -154,7 +218,7 @@ if sample_path is not None:
|
|
154 |
|
155 |
# save GT image in opt.root_path
|
156 |
full_pth_date = os.path.join(
|
157 |
-
full_pth_patch, dates[ch]
|
158 |
profile.update({
|
159 |
'nodata': None,
|
160 |
'dtype': 'uint8',
|
@@ -164,15 +228,21 @@ if sample_path is not None:
|
|
164 |
|
165 |
st.markdown('End prediction')
|
166 |
|
167 |
-
|
168 |
-
|
|
|
|
|
169 |
|
170 |
if st.paths is not None:
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
174 |
|
175 |
-
file_path = os.path.join(
|
176 |
# print(file_path)
|
177 |
target, profile = read(file_path)
|
178 |
target = np.squeeze(target)
|
@@ -186,18 +256,33 @@ if st.paths is not None:
|
|
186 |
# print(colors.to_hex(c))
|
187 |
markdown_legend += f'<div style="color:gray;background-color: {colors.to_hex(c)};">{l}</div><br>'
|
188 |
|
189 |
-
col1, col2 = st.columns(2)
|
190 |
with col1:
|
|
|
191 |
st.pyplot(fig)
|
192 |
with col2:
|
|
|
193 |
st.markdown(markdown_legend, unsafe_allow_html=True)
|
194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
st.markdown("""
|
196 |
## Lombardia Dataset
|
197 |
You can download other patches from the original dataset created and published on
|
198 |
[Kaggle](https://www.kaggle.com/datasets/ignazio/sentinel2-crop-mapping) and used in our paper.
|
199 |
-
|
200 |
## How to build an input file for the Demo
|
201 |
-
|
202 |
-
crop mapping og the area...
|
203 |
""")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import zipfile
|
|
|
3 |
from utils import *
|
4 |
+
import numpy as np
|
5 |
import matplotlib.pyplot as plt
|
6 |
+
import matplotlib.animation as animation
|
7 |
+
import streamlit.components.v1 as components
|
8 |
from matplotlib import colors
|
9 |
|
10 |
+
st.set_page_config(layout="wide")
|
11 |
+
|
12 |
+
def create_animation(images, pred_dates):
|
13 |
+
print('Creating composition of images...')
|
14 |
+
fps = 2
|
15 |
+
fig_an, ax_an = plt.subplots()
|
16 |
+
plt.title("")
|
17 |
+
a = images[0]
|
18 |
+
im = ax_an.imshow(a, interpolation='none', aspect='auto', vmin=0, vmax=1)
|
19 |
+
|
20 |
+
title = ax_an.text(0.5, 0.85, "", bbox={'facecolor': 'w', 'alpha': 0.5, 'pad': 5},
|
21 |
+
transform=ax_an.transAxes, ha="center")
|
22 |
+
|
23 |
+
def animate_func(idx):
|
24 |
+
title.set_text("date: " + pred_dates[idx])
|
25 |
+
im.set_array(images[idx])
|
26 |
+
return [im]
|
27 |
+
|
28 |
+
anima = animation.FuncAnimation(fig_an, animate_func, frames=len(images), interval=1000 / fps, blit=True,
|
29 |
+
repeat=False)
|
30 |
+
print('Done!')
|
31 |
+
return anima
|
32 |
+
|
33 |
+
|
34 |
+
def load_daily_preds_as_animations(pred_full_paths, pred_dates):
|
35 |
+
daily_preds = []
|
36 |
+
for path in pred_full_paths:
|
37 |
+
img, _ = read(path)
|
38 |
+
img = np.squeeze(img)
|
39 |
+
img = [classes_color_map[p] for p in img]
|
40 |
+
daily_preds.append(img)
|
41 |
+
anima = create_animation(daily_preds, pred_dates)
|
42 |
+
return anima
|
43 |
+
|
44 |
+
|
45 |
+
def load_src_images_as_animations(img_paths, pred_dates):
|
46 |
+
imgs = []
|
47 |
+
for path in img_paths:
|
48 |
+
img, _ = read(path)
|
49 |
+
|
50 |
+
# https://custom-scripts.sentinel-hub.com/custom-scripts/sentinel-2/composites/
|
51 |
+
# IREA image:
|
52 |
+
# False colors (8,4,3): 2,blue-B3,green-B4,5,6,7,red-B8,11,12
|
53 |
+
# Simple RGB (4, 3, 2): blue-B2,green-B3,red-B4,5,6,7,8,11,12
|
54 |
+
rgb = img[[2, 1, 0], :, :]
|
55 |
+
rgb = np.moveaxis(rgb, 0, -1)
|
56 |
+
imgs.append(rgb/np.amax(rgb))
|
57 |
+
anima = create_animation(imgs, pred_dates)
|
58 |
+
return anima
|
59 |
+
|
60 |
+
|
61 |
if not hasattr(st, 'paths'):
|
62 |
st.paths = None
|
63 |
if not hasattr(st, 'daily_model'):
|
|
|
95 |
|
96 |
|
97 |
st.title('In-season and dynamic crop mapping using 3D convolution neural networks and sentinel-2 time series')
|
98 |
+
st.markdown(""" Demo App for the model presented in the [paper](https://www.sciencedirect.com/science/article/pii/S0924271622003203):
|
99 |
```
|
100 |
@article{gallo2022in_season,
|
101 |
title = {In-season and dynamic crop mapping using 3D convolution neural networks and sentinel-2 time series},
|
|
|
109 |
author = {Ignazio Gallo and Luigi Ranghetti and Nicola Landro and Riccardo {La Grassa} and Mirco Boschetti},
|
110 |
}
|
111 |
```
|
112 |
+
***The demo doesn't work properly, we are working to fix the bugs!*
|
113 |
""")
|
114 |
|
115 |
file_uploaded = st.file_uploader(
|
|
|
118 |
accept_multiple_files=False,
|
119 |
)
|
120 |
sample_path = None
|
121 |
+
tileids = None
|
122 |
if file_uploaded is not None:
|
123 |
with zipfile.ZipFile(file_uploaded, "r") as z:
|
124 |
z.extractall("uploaded_samples")
|
|
|
126 |
st.markdown('or use a demo sample')
|
127 |
if st.button('sample 1'):
|
128 |
sample_path = 'demo_data/lombardia'
|
129 |
+
tileids = ['24']
|
130 |
+
if st.button('sample 2'):
|
131 |
+
sample_path = 'demo_data/lombardia'
|
132 |
+
tileids = ['712']
|
133 |
+
if st.button('sample 3'):
|
134 |
+
sample_path = 'demo_data/lombardia'
|
135 |
+
tileids = ['814']
|
136 |
+
if st.button('sample 4'):
|
137 |
+
sample_path = 'demo_data/lombardia'
|
138 |
+
tileids = ['1509']
|
139 |
|
140 |
paths = None
|
141 |
if sample_path is not None:
|
142 |
+
# st.markdown(f'elaborating {sample_path} ...')
|
143 |
|
144 |
validationdataset = SentinelDailyAnnualDatasetNoLabel(
|
145 |
sample_path,
|
|
|
147 |
opt.classes_path,
|
148 |
opt.sample_duration,
|
149 |
opt.win_size,
|
150 |
+
tileids=tileids)
|
151 |
validationdataloader = torch.utils.data.DataLoader(
|
152 |
validationdataset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.workers)
|
153 |
|
154 |
+
st.markdown('Model prediction in progress ...')
|
155 |
|
156 |
out_dir = os.path.join(opt.result_path, "seg_maps")
|
157 |
if not os.path.exists(out_dir):
|
|
|
218 |
|
219 |
# save GT image in opt.root_path
|
220 |
full_pth_date = os.path.join(
|
221 |
+
full_pth_patch, dates[ch] + '-daily-pred.tif')
|
222 |
profile.update({
|
223 |
'nodata': None,
|
224 |
'dtype': 'uint8',
|
|
|
228 |
|
229 |
st.markdown('End prediction')
|
230 |
|
231 |
+
# folder_out = "demo_data/results/seg_maps/example-lombardia/2"
|
232 |
+
folder_out = os.path.join("demo_data/results/seg_maps/"+opt.years[0]+"-lombardia/", tileids[0])
|
233 |
+
st.paths = os.listdir(folder_out)
|
234 |
+
st.paths.sort()
|
235 |
|
236 |
if st.paths is not None:
|
237 |
+
# folder_out = os.path.join("demo_data/results/seg_maps/example-lombardia/", tileids[0])
|
238 |
+
folder_src = os.path.join("demo_data/lombardia/", opt.years[0], tileids[0])
|
239 |
+
st.markdown("""
|
240 |
+
### Predictions
|
241 |
+
""")
|
242 |
+
# file_picker = st.selectbox("Select day predict (annual is patch-pred-nn.tif)",
|
243 |
+
# st.paths, index=st.paths.index('patch-pred-nn.tif'))
|
244 |
|
245 |
+
file_path = os.path.join(folder_out, 'patch-pred-nn.tif')
|
246 |
# print(file_path)
|
247 |
target, profile = read(file_path)
|
248 |
target = np.squeeze(target)
|
|
|
256 |
# print(colors.to_hex(c))
|
257 |
markdown_legend += f'<div style="color:gray;background-color: {colors.to_hex(c)};">{l}</div><br>'
|
258 |
|
259 |
+
col1, col2 = st.columns([2,1])
|
260 |
with col1:
|
261 |
+
st.markdown("**Long-term (annual) prediction**")
|
262 |
st.pyplot(fig)
|
263 |
with col2:
|
264 |
+
st.markdown("**Legend**")
|
265 |
st.markdown(markdown_legend, unsafe_allow_html=True)
|
266 |
|
267 |
+
st.markdown("**Short-term (daily) prediction**")
|
268 |
+
img_full_paths = [os.path.join(folder_out, path) for path in st.paths if 'daily-pred' in path]
|
269 |
+
pred_dates = [path[:8] for path in st.paths if 'daily-pred' in path]
|
270 |
+
anim = load_daily_preds_as_animations(img_full_paths, pred_dates)
|
271 |
+
components.html(anim.to_jshtml(), height=600)
|
272 |
+
|
273 |
+
st.markdown("**Input time series**")
|
274 |
+
list_dir = os.listdir(folder_src)
|
275 |
+
list_dir.sort()
|
276 |
+
img_full_paths = [os.path.join(folder_src, f) for f in list_dir if f.endswith(".tif")]
|
277 |
+
pred_dates = [f[:8] for f in list_dir if f.endswith(".tif")]
|
278 |
+
anim_src = load_src_images_as_animations(img_full_paths, pred_dates)
|
279 |
+
components.html(anim_src.to_jshtml(), height=600)
|
280 |
+
|
281 |
st.markdown("""
|
282 |
## Lombardia Dataset
|
283 |
You can download other patches from the original dataset created and published on
|
284 |
[Kaggle](https://www.kaggle.com/datasets/ignazio/sentinel2-crop-mapping) and used in our paper.
|
285 |
+
|
286 |
## How to build an input file for the Demo
|
287 |
+
Working in progress: to be defined ...
|
|
|
288 |
""")
|
demo_data/lombardia/{example/2 → 2019/1509}/20190104.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190109.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190114.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190119.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190124.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190129.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190203.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190208.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190213.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190218.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190223.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190228.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190305.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190310.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2/20190104_MSAVI.tif → 2019/1509/20190315.tif}
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190320.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2/20190109_MSAVI.tif → 2019/1509/20190325.tif}
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190330.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190404.tif
RENAMED
File without changes
|
demo_data/lombardia/2019/1509/20190409.tif
ADDED
Git LFS Details
|
demo_data/lombardia/{example/2 → 2019/1509}/20190414.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190419.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190424.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190429.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190504.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190509.tif
RENAMED
File without changes
|
demo_data/lombardia/2019/1509/20190514.tif
ADDED
Git LFS Details
|
demo_data/lombardia/{example/2 → 2019/1509}/20190519.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190524.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190529.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190603.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190613.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190618.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190623.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190628.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190703.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190708.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190713.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190718.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190723.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190728.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190802.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190807.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190812.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190817.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190822.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190827.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190901.tif
RENAMED
File without changes
|
demo_data/lombardia/{example/2 → 2019/1509}/20190906.tif
RENAMED
File without changes
|