Spaces:
Sleeping
Sleeping
artelabsuper
commited on
Commit
•
85993f4
1
Parent(s):
af0160d
demo work with demo button
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- .gitignore +2 -0
- app.py +148 -1
- demo_data/best_model_annual.pth +3 -0
- demo_data/best_model_daily.pth +3 -0
- demo_data/classes-newmapping.txt +8 -0
- demo_data/lombardia/example/2/20190104.tif +3 -0
- demo_data/lombardia/example/2/20190104_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190109.tif +3 -0
- demo_data/lombardia/example/2/20190109_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190114.tif +3 -0
- demo_data/lombardia/example/2/20190114_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190119.tif +3 -0
- demo_data/lombardia/example/2/20190119_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190124.tif +3 -0
- demo_data/lombardia/example/2/20190124_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190129.tif +3 -0
- demo_data/lombardia/example/2/20190129_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190203.tif +3 -0
- demo_data/lombardia/example/2/20190203_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190208.tif +3 -0
- demo_data/lombardia/example/2/20190208_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190213.tif +3 -0
- demo_data/lombardia/example/2/20190213_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190218.tif +3 -0
- demo_data/lombardia/example/2/20190218_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190223.tif +3 -0
- demo_data/lombardia/example/2/20190223_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190228.tif +3 -0
- demo_data/lombardia/example/2/20190228_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190305.tif +3 -0
- demo_data/lombardia/example/2/20190305_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190310.tif +3 -0
- demo_data/lombardia/example/2/20190310_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190315.tif +3 -0
- demo_data/lombardia/example/2/20190315_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190320.tif +3 -0
- demo_data/lombardia/example/2/20190320_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190325.tif +3 -0
- demo_data/lombardia/example/2/20190325_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190330.tif +3 -0
- demo_data/lombardia/example/2/20190330_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190404.tif +3 -0
- demo_data/lombardia/example/2/20190404_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190409.tif +3 -0
- demo_data/lombardia/example/2/20190409_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190414.tif +3 -0
- demo_data/lombardia/example/2/20190414_MSAVI.tif +3 -0
- demo_data/lombardia/example/2/20190419.tif +3 -0
- demo_data/lombardia/example/2/20190419_MSAVI.tif +3 -0
.gitattributes
CHANGED
@@ -31,3 +31,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
31 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
32 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
33 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
31 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
32 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
33 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.tif filter=lfs diff=lfs merge=lfs -text
|
.gitignore
CHANGED
@@ -1,2 +1,4 @@
|
|
1 |
venv
|
2 |
__pycache__
|
|
|
|
|
|
1 |
venv
|
2 |
__pycache__
|
3 |
+
uploaded_samples
|
4 |
+
demo_data/results
|
app.py
CHANGED
@@ -1,5 +1,41 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
import zipfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
st.title('Sentinel 2 Crop Mapping')
|
5 |
st.markdown('Using a <model> and giving a zip that contains 32 tiff named correctly you can reach prediction of crop mapping og the area.')
|
@@ -9,6 +45,117 @@ file_uploaded = st.file_uploader(
|
|
9 |
type=["zip"],
|
10 |
accept_multiple_files=False,
|
11 |
)
|
|
|
12 |
if file_uploaded is not None:
|
13 |
with zipfile.ZipFile(file_uploaded, "r") as z:
|
14 |
-
z.extractall("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
# Load Model
|
9 |
+
# @title Load pretrained weights
|
10 |
+
|
11 |
+
best_model_daily_file_name = "best_model_daily.pth"
|
12 |
+
best_model_annual_file_name = "best_model_annual.pth"
|
13 |
+
|
14 |
+
first_input_batch = torch.zeros(71, 9, 5, 48, 48)
|
15 |
+
# first_input_batch = first_input_batch.view(-1, *first_input_batch.shape[2:])
|
16 |
+
daily_model = FPN(opt, first_input_batch, opt.win_size)
|
17 |
+
annual_model = SimpleNN(opt)
|
18 |
+
|
19 |
+
if torch.cuda.is_available():
|
20 |
+
daily_model = torch.nn.DataParallel(daily_model).cuda()
|
21 |
+
annual_model = torch.nn.DataParallel(annual_model).cuda()
|
22 |
+
daily_model = torch.nn.DataParallel(daily_model).cuda()
|
23 |
+
annual_model = torch.nn.DataParallel(annual_model).cuda()
|
24 |
+
else:
|
25 |
+
daily_model = torch.nn.DataParallel(daily_model).cpu()
|
26 |
+
annual_model = torch.nn.DataParallel(annual_model).cpu()
|
27 |
+
daily_model = torch.nn.DataParallel(daily_model).cpu()
|
28 |
+
annual_model = torch.nn.DataParallel(annual_model).cpu()
|
29 |
+
|
30 |
+
print('trying to resume previous saved models...')
|
31 |
+
state = resume(
|
32 |
+
os.path.join(opt.resume_path, best_model_daily_file_name),
|
33 |
+
model=daily_model, optimizer=None)
|
34 |
+
state = resume(
|
35 |
+
os.path.join(opt.resume_path, best_model_annual_file_name),
|
36 |
+
model=annual_model, optimizer=None)
|
37 |
+
daily_model = daily_model.eval()
|
38 |
+
annual_model = annual_model.eval()
|
39 |
|
40 |
st.title('Sentinel 2 Crop Mapping')
|
41 |
st.markdown('Using a <model> and giving a zip that contains 32 tiff named correctly you can reach prediction of crop mapping og the area.')
|
|
|
45 |
type=["zip"],
|
46 |
accept_multiple_files=False,
|
47 |
)
|
48 |
+
sample_path = None
|
49 |
if file_uploaded is not None:
|
50 |
with zipfile.ZipFile(file_uploaded, "r") as z:
|
51 |
+
z.extractall("uploaded_samples")
|
52 |
+
sample_path = "uploaded_samples/" + file_uploaded.name[:-4]
|
53 |
+
st.markdown('or use a demo sample')
|
54 |
+
if st.button('sample 1'):
|
55 |
+
sample_path = 'demo_data/lombardia'
|
56 |
+
|
57 |
+
if sample_path is not None:
|
58 |
+
st.markdown(f'elaborating {sample_path}...')
|
59 |
+
|
60 |
+
validationdataset = SentinelDailyAnnualDatasetNoLabel(
|
61 |
+
sample_path,
|
62 |
+
opt.years,
|
63 |
+
opt.classes_path,
|
64 |
+
opt.sample_duration,
|
65 |
+
opt.win_size,
|
66 |
+
tileids=None)
|
67 |
+
validationdataloader = torch.utils.data.DataLoader(
|
68 |
+
validationdataset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.workers)
|
69 |
+
|
70 |
+
st.markdown(f'predict in progress...')
|
71 |
+
|
72 |
+
out_dir = os.path.join(opt.result_path, "seg_maps")
|
73 |
+
if not os.path.exists(out_dir):
|
74 |
+
os.makedirs(out_dir)
|
75 |
+
for i, (x_dailies, dates, dirs_path) in enumerate(validationdataloader):
|
76 |
+
with torch.no_grad():
|
77 |
+
# x_dailies, dates, dirs_path = next(iter(validationdataloader))
|
78 |
+
# reshape merging the first two dimensions
|
79 |
+
x_dailies = x_dailies.view(-1, *x_dailies.shape[2:])
|
80 |
+
if torch.cuda.is_available():
|
81 |
+
x_dailies = x_dailies.cuda()
|
82 |
+
|
83 |
+
feat_daily, outs_daily = daily_model.forward(x_dailies)
|
84 |
+
# return to original size of batch and year
|
85 |
+
outs_daily = outs_daily.view(
|
86 |
+
opt.batch_size, opt.sample_duration, *outs_daily.shape[1:])
|
87 |
+
feat_daily = feat_daily.view(
|
88 |
+
opt.batch_size, opt.sample_duration, *feat_daily.shape[1:])
|
89 |
+
|
90 |
+
_, out_annual = annual_model.forward(feat_daily)
|
91 |
+
pred_annual = torch.argmax(out_annual, dim=1).squeeze(1)
|
92 |
+
pred_annual = pred_annual.cpu().numpy()
|
93 |
+
# Remapping the labels
|
94 |
+
pred_annual_nn = ids_to_labels(
|
95 |
+
validationdataloader, pred_annual).astype(numpy.uint8)
|
96 |
+
|
97 |
+
for batch in range(feat_daily.shape[0]):
|
98 |
+
# _, profile = read(os.path.join(dirs_path[batch], '20191230_MSAVI.tif')) # todo get the last image
|
99 |
+
_, tmp_path = get_patch_id(validationdataset.samples, 0)
|
100 |
+
dates = get_all_dates(
|
101 |
+
tmp_path, validationdataset.max_seq_length)
|
102 |
+
last_tif_path = os.path.join(tmp_path, dates[-1] + ".tif")
|
103 |
+
_, profile = read(last_tif_path)
|
104 |
+
profile["name"] = dirs_path[batch]
|
105 |
+
|
106 |
+
pth = dirs_path[batch].split(os.path.sep)[-3:]
|
107 |
+
full_pth_patch = os.path.join(
|
108 |
+
out_dir, pth[1] + '-' + pth[0], pth[2])
|
109 |
+
|
110 |
+
if not os.path.exists(full_pth_patch):
|
111 |
+
os.makedirs(full_pth_patch)
|
112 |
+
full_pth_pred = os.path.join(
|
113 |
+
full_pth_patch, 'patch-pred-nn.tif')
|
114 |
+
profile.update({
|
115 |
+
'nodata': None,
|
116 |
+
'dtype': 'uint8',
|
117 |
+
'count': 1})
|
118 |
+
with rasterio.open(full_pth_pred, 'w', **profile) as dst:
|
119 |
+
dst.write_band(1, pred_annual_nn[batch])
|
120 |
+
|
121 |
+
# patch_predictions = None
|
122 |
+
for ch in range(len(dates)):
|
123 |
+
soft_seg = outs_daily[batch, ch, :, :, :]
|
124 |
+
# transform probs into a hard segmentation
|
125 |
+
pred_daily = torch.argmax(soft_seg, dim=0)
|
126 |
+
pred_daily = pred_daily.cpu()
|
127 |
+
daily_pred = ids_to_labels(
|
128 |
+
validationdataloader, pred_daily).astype(numpy.uint8)
|
129 |
+
# if patch_predictions is None:
|
130 |
+
# patch_predictions = numpy.expand_dims(daily_pred, axis=0)
|
131 |
+
# else:
|
132 |
+
# patch_predictions = numpy.concatenate((patch_predictions, numpy.expand_dims(daily_pred, axis=0)),
|
133 |
+
# axis=0)
|
134 |
+
|
135 |
+
# save GT image in opt.root_path
|
136 |
+
full_pth_date = os.path.join(
|
137 |
+
full_pth_patch, dates[ch][batch] + f'-ch{ch}-b{batch}-daily-pred.tif')
|
138 |
+
profile.update({
|
139 |
+
'nodata': None,
|
140 |
+
'dtype': 'uint8',
|
141 |
+
'count': 1})
|
142 |
+
with rasterio.open(full_pth_date, 'w', **profile) as dst:
|
143 |
+
dst.write_band(1, daily_pred)
|
144 |
+
|
145 |
+
st.markdown('End prediction')
|
146 |
+
|
147 |
+
folder = "demo_data/results/seg_maps/example-lombardia/2"
|
148 |
+
paths = os.listdir(folder)
|
149 |
+
|
150 |
+
file_picker = st.selectbox("Select day predict (annual is patch-pred-nn.tif)", paths, index=paths.index('patch-pred-nn.tif'))
|
151 |
+
|
152 |
+
file_path = os.path.join(folder, file_picker)
|
153 |
+
print(file_path)
|
154 |
+
target, profile = read(file_path)
|
155 |
+
target = np.squeeze(target)
|
156 |
+
target = [classes_color_map[p] for p in target]
|
157 |
+
|
158 |
+
fig, ax = plt.subplots()
|
159 |
+
ax.imshow(target)
|
160 |
+
st.pyplot(fig)
|
161 |
+
|
demo_data/best_model_annual.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6c4d7a0be5d24a55e19f257b37b5bd034f8dceb82e4355e2c98c35995b7bce36
|
3 |
+
size 326134860
|
demo_data/best_model_daily.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:820efc0edf6e22b12049a0dcce37880808d3e5c8773ed7b30441ab2893515253
|
3 |
+
size 646423341
|
demo_data/classes-newmapping.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0|Unknown|20180201|20181230
|
2 |
+
2|Other cereals|20180324|20180512
|
3 |
+
4|Woods and other tree crops|20180521|20180610
|
4 |
+
7|Forage|20180402|20180504
|
5 |
+
9|Corn|20180612|20180729
|
6 |
+
12|Rice|20180606|20180902
|
7 |
+
1,3,5,6,8,10,11,13,14,15,16,20|Unknow cropland|20180201|20181230
|
8 |
+
17,18,19|No arable land|20180201|20181230
|
demo_data/lombardia/example/2/20190104.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190104_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190109.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190109_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190114.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190114_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190119.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190119_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190124.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190124_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190129.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190129_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190203.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190203_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190208.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190208_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190213.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190213_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190218.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190218_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190223.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190223_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190228.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190228_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190305.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190305_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190310.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190310_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190315.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190315_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190320.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190320_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190325.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190325_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190330.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190330_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190404.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190404_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190409.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190409_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190414.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190414_MSAVI.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190419.tif
ADDED
Git LFS Details
|
demo_data/lombardia/example/2/20190419_MSAVI.tif
ADDED
Git LFS Details
|