Spaces:
Runtime error
Runtime error
jeremyLE-Ekimetrics
commited on
Commit
•
709a47d
1
Parent(s):
1527861
fix
Browse files- app.py +0 -0
- biomap/checkpoint/model/model.pt +1 -1
- biomap/helper.py +4 -4
- biomap/streamlit_app.py +83 -52
- biomap/utils_gee.py +10 -2
app.py
DELETED
File without changes
|
biomap/checkpoint/model/model.pt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
oid sha256:106fe1ea7f4f0819e360823374bce7840a1a150b39a2e45090612c159a25dfca
|
3 |
-
size 95521785
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
oid sha256:106fe1ea7f4f0819e360823374bce7840a1a150b39a2e45090612c159a25dfca
|
3 |
+
size 95521785
|
biomap/helper.py
CHANGED
@@ -15,7 +15,7 @@ import streamlit as st
|
|
15 |
import cv2
|
16 |
|
17 |
@st.cache_data(hash_funcs={LitUnsupervisedSegmenter: lambda dt: dt.name})
|
18 |
-
def inference_on_location(model,
|
19 |
"""Performe an inference on the latitude and longitude between the start date and the end date
|
20 |
|
21 |
Args:
|
@@ -65,13 +65,13 @@ def inference_on_location(model, longitude=2.98, latitude=48.81, start_date=2020
|
|
65 |
|
66 |
images = [np.asarray(img) for img in imgs]
|
67 |
labeled_imgs = [np.asarray(img) for img in labeled_imgs]
|
68 |
-
title=f"TimeLapse at location {
|
69 |
fig = plot_imgs_labels(dates, images, labeled_imgs, scores_details, scores, title=title)
|
70 |
# fig.save("test.png")
|
71 |
return fig
|
72 |
|
73 |
@st.cache_data(hash_funcs={LitUnsupervisedSegmenter: lambda dt: dt.name})
|
74 |
-
def inference_on_location_and_month(model,
|
75 |
"""Performe an inference on the latitude and longitude between the start date and the end date
|
76 |
|
77 |
Args:
|
@@ -100,7 +100,7 @@ def inference_on_location_and_month(model, longitude = 2.98, latitude = 48.81, s
|
|
100 |
logging.info(f"Calculated Biodiversity Score : {score}")
|
101 |
img, label, labeled_img = transform_to_pil(outputs[0])
|
102 |
|
103 |
-
title=f"Prediction at location {
|
104 |
fig = plot_image([start_date], [np.asarray(img)], [np.asarray(labeled_img)], [score_details], [score],title=title)
|
105 |
return fig
|
106 |
|
|
|
15 |
import cv2
|
16 |
|
17 |
@st.cache_data(hash_funcs={LitUnsupervisedSegmenter: lambda dt: dt.name})
|
18 |
+
def inference_on_location(model, latitude=48.81, longitude=2.98, start_date=2020, end_date=2022, how="year"):
|
19 |
"""Performe an inference on the latitude and longitude between the start date and the end date
|
20 |
|
21 |
Args:
|
|
|
65 |
|
66 |
images = [np.asarray(img) for img in imgs]
|
67 |
labeled_imgs = [np.asarray(img) for img in labeled_imgs]
|
68 |
+
title=f"TimeLapse at location ({location[0]:.2f},{location[1]:.2f}) between {start_date} and {end_date}"
|
69 |
fig = plot_imgs_labels(dates, images, labeled_imgs, scores_details, scores, title=title)
|
70 |
# fig.save("test.png")
|
71 |
return fig
|
72 |
|
73 |
@st.cache_data(hash_funcs={LitUnsupervisedSegmenter: lambda dt: dt.name})
|
74 |
+
def inference_on_location_and_month(model, latitude = 48.81, longitude = 2.98, start_date = '2020-03-20'):
|
75 |
"""Performe an inference on the latitude and longitude between the start date and the end date
|
76 |
|
77 |
Args:
|
|
|
100 |
logging.info(f"Calculated Biodiversity Score : {score}")
|
101 |
img, label, labeled_img = transform_to_pil(outputs[0])
|
102 |
|
103 |
+
title=f"Prediction at location ({location[0]:.2f},{location[1]:.2f}) at {start_date}"
|
104 |
fig = plot_image([start_date], [np.asarray(img)], [np.asarray(labeled_img)], [score_details], [score],title=title)
|
105 |
return fig
|
106 |
|
biomap/streamlit_app.py
CHANGED
@@ -54,72 +54,103 @@ def init_app(cfg_name) -> LitUnsupervisedSegmenter:
|
|
54 |
def app(model):
|
55 |
if "infered" not in st.session_state:
|
56 |
st.session_state["infered"] = False
|
|
|
|
|
|
|
|
|
57 |
|
58 |
st.markdown("<h1 style='text-align: center;'>🐢 Biomap by Ekimetrics 🐢</h1>", unsafe_allow_html=True)
|
59 |
st.markdown("<h2 style='text-align: center;'>Estimate Biodiversity in the world with the help of land cover.</h2>", unsafe_allow_html=True)
|
60 |
st.markdown("<p style='text-align: center;'>The segmentation model is an association of UNet and DinoV1 trained on the dataset CORINE. Land use is divided into 6 differents classes : Each class is assigned a GBS score from 0 to 1</p>", unsafe_allow_html=True)
|
61 |
st.markdown("<p style='text-align: center;'>Buildings : 0.1 | Infrastructure : 0.1 | Cultivation : 0.4 | Wetland : 0.9 | Water : 0.9 | Natural green : 1 </p>", unsafe_allow_html=True)
|
62 |
st.markdown("<p style='text-align: center;'>The score is then averaged on the full image.</p>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
98 |
col_tab2_1, col_tab2_2 = st.columns(2)
|
99 |
with col_tab2_1:
|
100 |
-
|
|
|
101 |
with col_tab2_2:
|
102 |
-
|
103 |
-
|
104 |
-
date = st.text_input("date", "2021-01-01", placeholder="2021-01-01")
|
105 |
-
|
106 |
-
submit2 = st.button("Predict Single Image", use_container_width=True)
|
107 |
|
|
|
|
|
108 |
|
109 |
-
if submit:
|
110 |
-
fig = inference_on_location(model, lat, long, start_date, end_date, segment_interval)
|
111 |
-
st.session_state["infered"] = True
|
112 |
-
st.session_state["previous_fig"] = fig
|
113 |
|
114 |
-
if submit2:
|
115 |
-
fig = inference_on_location_and_month(model, lat, long, date)
|
116 |
-
st.session_state["infered"] = True
|
117 |
-
st.session_state["previous_fig"] = fig
|
118 |
-
|
119 |
-
if st.session_state["infered"]:
|
120 |
-
st.plotly_chart(st.session_state["previous_fig"], use_container_width=True)
|
121 |
-
|
122 |
-
|
123 |
if __name__ == "__main__":
|
124 |
model = init_app("my_train_config.yml")
|
125 |
app(model)
|
|
|
54 |
def app(model):
|
55 |
if "infered" not in st.session_state:
|
56 |
st.session_state["infered"] = False
|
57 |
+
if "submit" not in st.session_state:
|
58 |
+
st.session_state["submit"] = False
|
59 |
+
if "submit2" not in st.session_state:
|
60 |
+
st.session_state["submit2"] = False
|
61 |
|
62 |
st.markdown("<h1 style='text-align: center;'>🐢 Biomap by Ekimetrics 🐢</h1>", unsafe_allow_html=True)
|
63 |
st.markdown("<h2 style='text-align: center;'>Estimate Biodiversity in the world with the help of land cover.</h2>", unsafe_allow_html=True)
|
64 |
st.markdown("<p style='text-align: center;'>The segmentation model is an association of UNet and DinoV1 trained on the dataset CORINE. Land use is divided into 6 differents classes : Each class is assigned a GBS score from 0 to 1</p>", unsafe_allow_html=True)
|
65 |
st.markdown("<p style='text-align: center;'>Buildings : 0.1 | Infrastructure : 0.1 | Cultivation : 0.4 | Wetland : 0.9 | Water : 0.9 | Natural green : 1 </p>", unsafe_allow_html=True)
|
66 |
st.markdown("<p style='text-align: center;'>The score is then averaged on the full image.</p>", unsafe_allow_html=True)
|
67 |
+
if st.session_state["submit"]:
|
68 |
+
fig = inference_on_location(model, st.session_state["lat"], st.session_state["long"], st.session_state["start_date"], st.session_state["end_date"], st.session_state["segment_interval"])
|
69 |
+
st.session_state["infered"] = True
|
70 |
+
st.session_state["previous_fig"] = fig
|
71 |
|
72 |
+
if st.session_state["submit2"]:
|
73 |
+
fig = inference_on_location_and_month(model, st.session_state["lat_2"], st.session_state["long_2"], st.session_state["date_2"])
|
74 |
+
st.session_state["infered"] = True
|
75 |
+
st.session_state["previous_fig"] = fig
|
76 |
+
|
77 |
+
if st.session_state["infered"]:
|
78 |
+
st.plotly_chart(st.session_state["previous_fig"], use_container_width=True)
|
79 |
+
|
80 |
+
m = folium.Map(location=[DEFAULT_LATITUDE, DEFAULT_LONGITUDE], zoom_start=DEFAULT_ZOOM)
|
81 |
+
m.add_child(folium.LatLngPopup())
|
82 |
+
tabs1, tabs2 = st.tabs(["TimeLapse", "Single Image"])
|
83 |
+
with tabs1:
|
84 |
+
|
85 |
+
|
86 |
+
submit = st.button("Predict TimeLapse", use_container_width=True, type="primary")
|
87 |
+
st.session_state["submit"] = submit
|
88 |
+
|
89 |
+
col_1, col_2 = st.columns([0.5,0.5])
|
90 |
+
with col_1:
|
91 |
+
|
92 |
+
f_map = st_folium(m, key="tab1", width=FOLIUM_WIDTH, height=FOLIUM_HEIGHT)
|
93 |
+
|
94 |
+
selected_latitude = DEFAULT_LATITUDE
|
95 |
+
selected_longitude = DEFAULT_LONGITUDE
|
96 |
+
|
97 |
+
if f_map.get("last_clicked"):
|
98 |
+
selected_latitude = f_map["last_clicked"]["lat"]
|
99 |
+
selected_longitude = f_map["last_clicked"]["lng"]
|
100 |
+
|
101 |
+
with col_2:
|
102 |
+
col_tab1_1, col_tab1_2 = st.columns(2)
|
103 |
+
with col_tab1_1:
|
104 |
+
lat = st.text_input("latitude", value=selected_latitude)
|
105 |
+
st.session_state["lat"] = lat
|
106 |
+
with col_tab1_2:
|
107 |
+
long = st.text_input("longitude", value=selected_longitude)
|
108 |
+
st.session_state["long"] = long
|
109 |
+
|
110 |
+
col_tab1_11, col_tab1_22 = st.columns(2)
|
111 |
+
years = list(range(MIN_YEAR, MAX_YEAR, 1))
|
112 |
+
with col_tab1_11:
|
113 |
+
start_date = st.selectbox("Start date", years)
|
114 |
+
st.session_state["start_date"] = start_date
|
115 |
+
|
116 |
+
end_years = [year for year in years if year > start_date]
|
117 |
+
with col_tab1_22:
|
118 |
+
end_date = st.selectbox("End date", end_years)
|
119 |
+
st.session_state["end_date"] = end_date
|
120 |
+
|
121 |
+
segment_interval = st.radio("Interval of time between two segmentation", options=['month','2months', 'year'],horizontal=True)
|
122 |
+
st.session_state["segment_interval"] = segment_interval
|
123 |
+
|
124 |
+
with tabs2:
|
125 |
+
submit2 = st.button("Predict Single Image", use_container_width=True, type="primary")
|
126 |
+
st.session_state["submit2"] = submit2
|
127 |
+
|
128 |
+
col_1_tab_2, col_2_tab_2 = st.columns([0.5,0.5])
|
129 |
+
with col_1_tab_2:
|
130 |
+
m_tab_2 = folium.Map(location=[DEFAULT_LATITUDE, DEFAULT_LONGITUDE], zoom_start=DEFAULT_ZOOM)
|
131 |
+
m_tab_2.add_child(folium.LatLngPopup())
|
132 |
+
f_map_tab_2 = st_folium(m, key="tab2", width=FOLIUM_WIDTH, height=FOLIUM_HEIGHT)
|
133 |
|
134 |
+
selected_latitude_2 = DEFAULT_LATITUDE
|
135 |
+
selected_longitude_2 = DEFAULT_LONGITUDE
|
136 |
+
|
137 |
+
if f_map_tab_2.get("last_clicked"):
|
138 |
+
selected_latitude_2 = f_map_tab_2["last_clicked"]["lat"]
|
139 |
+
selected_longitude_2 = f_map_tab_2["last_clicked"]["lng"]
|
140 |
+
|
141 |
+
with col_2_tab_2:
|
142 |
col_tab2_1, col_tab2_2 = st.columns(2)
|
143 |
with col_tab2_1:
|
144 |
+
lat_2 = st.text_input("lat.", value=selected_latitude_2)
|
145 |
+
st.session_state["lat_2"] = lat_2
|
146 |
with col_tab2_2:
|
147 |
+
long_2 = st.text_input("long.", value=selected_longitude_2)
|
148 |
+
st.session_state["long_2"] = long_2
|
|
|
|
|
|
|
149 |
|
150 |
+
date_2 = st.text_input("date", "2021-01-01", placeholder="2021-01-01")
|
151 |
+
st.session_state["date_2"] = date_2
|
152 |
|
|
|
|
|
|
|
|
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
if __name__ == "__main__":
|
155 |
model = init_app("my_train_config.yml")
|
156 |
app(model)
|
biomap/utils_gee.py
CHANGED
@@ -6,10 +6,16 @@ import matplotlib.pyplot as plt
|
|
6 |
import os
|
7 |
from pathlib import Path
|
8 |
import logging
|
|
|
9 |
|
10 |
#Initialize
|
11 |
-
service_account =
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
ee.Initialize(credentials)
|
14 |
|
15 |
def get_image(location, d1, d2):
|
@@ -143,6 +149,8 @@ def extract_img(location,start_date,end_date, width = 0.01 , len = 0.01,scale=5)
|
|
143 |
Returns:
|
144 |
img: image as numpy array
|
145 |
"""
|
|
|
|
|
146 |
ee_img, geometry = extract_ee_img(location, width,start_date,end_date , len)
|
147 |
url = get_url(ee_img, geometry, scale)
|
148 |
img = extract_np_from_url(url)
|
|
|
6 |
import os
|
7 |
from pathlib import Path
|
8 |
import logging
|
9 |
+
import json
|
10 |
|
11 |
#Initialize
|
12 |
+
service_account = os.environ["SERVICE_ACCOUNT_EE"]
|
13 |
+
private_key = json.loads(os.environ["PRIVATE_KEY_EE"])
|
14 |
+
|
15 |
+
with open(os.path.join(os.path.dirname(__file__), '.private-key-2.json'), "w") as ipt:
|
16 |
+
json.dump(private_key, ipt)
|
17 |
+
|
18 |
+
credentials = ee.ServiceAccountCredentials(service_account, os.path.join(os.path.dirname(__file__), '.private-key-2.json'))
|
19 |
ee.Initialize(credentials)
|
20 |
|
21 |
def get_image(location, d1, d2):
|
|
|
149 |
Returns:
|
150 |
img: image as numpy array
|
151 |
"""
|
152 |
+
# reversed longitude latitude
|
153 |
+
location = (location[1], location[0])
|
154 |
ee_img, geometry = extract_ee_img(location, width,start_date,end_date , len)
|
155 |
url = get_url(ee_img, geometry, scale)
|
156 |
img = extract_np_from_url(url)
|