-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvisualize_weed.py
More file actions
387 lines (347 loc) · 12.9 KB
/
Copy pathvisualize_weed.py
File metadata and controls
387 lines (347 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import base64
import contextlib
from io import BytesIO
import streamlit as st
from PIL import Image
from torchmetrics.functional import f1_score
import torch
import numpy as np
import pandas as pd
from roweeder.data.utils import DataDict
from roweeder.detector import (
HoughCropRowDetector,
HoughDetectorDict,
get_vegetation_detector as get_vegetation_detector_fn,
)
from roweeder.data import get_dataset
from roweeder.data.phenobench import PhenoBenchDataset
from roweeder.labeling import get_drawn_img, label_from_row, label, save_and_label
from roweeder.visualize import map_grayscale_to_rgb
def change_state(src, dest):
st.session_state[dest] = st.session_state[src]
st.session_state["i"] = st.session_state[src]
@st.cache_resource
def get_vegetation_detector(ndvi_threshold=0.5, exg_threshold=0.3, exg_min_area=100):
name = st.session_state["vegetation_detector"]
# Ensure NDVIDetector is not used with PhenoBench
if st.session_state.get("modality") == "PhenoBench" and name != "ExGDetector":
st.warning("NDVI is not supported for PhenoBench. Switching to ExGDetector.")
name = "ExGDetector"
st.session_state["vegetation_detector"] = name
if name == "NDVIDetector":
# Default indices for WeedMap (R,G,B,NIR,RE)
nir_idx = 3
red_idx = 0
if st.session_state.get("modality") == "PhenoBench":
nir_idx = 0
red_idx = 2
params = {
"threshold": ndvi_threshold,
"nir_idx": nir_idx,
"red_idx": red_idx,
}
elif name == "ExGDetector":
params = {
"threshold": exg_threshold,
"min_area": exg_min_area,
}
else:
raise ValueError(f"Unknown vegetation detector: {name}")
return get_vegetation_detector_fn(name, params)
def get_thumbnail(path):
i = Image.open(path)
i.thumbnail((150, 150), Image.LANCZOS)
return i
def image_base64(im):
if isinstance(im, str):
im = get_thumbnail(im)
with BytesIO() as buffer:
im.save(buffer, "jpeg")
return base64.b64encode(buffer.getvalue()).decode()
def image_formatter(im):
return f'<img src="data:image/jpeg;base64,{image_base64(im)}">'
def gt_fix(gt):
gt[gt == 10000] = 1
gt[gt == 240] = 2
return gt
def display_prediction():
st_state = st.session_state
print(f"st_state", st_state)
if st_state["img_name"] != "":
found = False
for k, name in enumerate(st_state["images"]):
if st_state["img_name"] in name:
i = k
found = True
st_state["i"] = i
if not found:
st.write("img not found")
return
else:
try:
i = st_state["i"]
except KeyError:
i = 0
st_state["i"] = i
col1, col2, col3, col4, col5 = st.columns(5)
data_dict = st_state["dataset"][i]
img = data_dict.image
print(f"img name: {data_dict.name}")
print(f"img shape: {img.shape}")
if isinstance(img, torch.Tensor):
img = img.unsqueeze(0)
gt = data_dict.target
mask = st.session_state["labeller"](img)
st.write(mask.shape)
detector = HoughCropRowDetector(
threshold=st.session_state["threshold"],
crop_detector=st.session_state["labeller"],
step_theta=st.session_state["step_theta"],
step_rho=st.session_state["step_rho"],
angle_error=st.session_state["angle_error"],
clustering_tol=st.session_state["clustering_tol"],
uniform_significance=st.session_state["uniform_significance"],
theta_reduction_threshold=st.session_state["theta_reduction_threshold"],
theta_value=st.session_state["theta_value"],
)
res = detector.predict_from_mask(mask)
lines = res[HoughDetectorDict.LINES]
original_lines = res[HoughDetectorDict.ORIGINAL_LINES]
uniform_significance = res[HoughDetectorDict.UNIFORM_SIGNIFICANCE]
zero_reason = res[HoughDetectorDict.ZERO_REASON]
gt = gt_fix(torch.tensor(np.array(gt))).cuda()
to_draw_gt = (gt.cpu().numpy()).astype(np.uint8)
to_draw_gt = map_grayscale_to_rgb(to_draw_gt)
to_draw_mask = mask.cpu().numpy().astype(np.uint8)
line_mask = get_drawn_img(
torch.zeros_like(torch.tensor(to_draw_mask)).numpy(), lines, color=(255, 0, 255)
)
argmask = mask[0].type(torch.uint8)
weed_map, weed_map_slic, slic = label_from_row(img, argmask, torch.tensor(line_mask).permute(2, 0, 1)[0])
pred = weed_map.argmax(dim=0)
f1 = f1_score(
weed_map.argmax(dim=0).cuda(),
gt,
num_classes=3,
average="macro",
task="multiclass",
multidim_average="global",
)
weed_map = weed_map.argmax(dim=0).cpu().numpy().astype(np.uint8)
weed_map = map_grayscale_to_rgb(
weed_map, mapping={1: (0, 255, 0), 2: (255, 0, 0)}
).transpose(2, 0, 1)
weed_map_lines = get_drawn_img(weed_map, lines, color=(255, 0, 255))
to_draw_mask = to_draw_mask[0]
weed_map = np.moveaxis(weed_map, 0, -1)
st.write(data_dict.name)
st.write("f1 score: ", f1)
st.write("uniform_significance: ", uniform_significance)
st.write("zero_reason: ", zero_reason)
with col1:
st.write("## Image")
output_img = (img[:3].squeeze(0).permute(1, 2, 0).numpy() * 255).astype(
np.uint8
)
output_img = Image.fromarray(output_img)
st.image(output_img, width=300)
with col2:
st.write("## GT")
st.image(Image.fromarray(to_draw_gt), width=300)
with col3:
st.write("## Mask")
st.image(Image.fromarray(to_draw_mask), width=300)
with col4:
st.write("## Prediction")
st.image(weed_map, width=300)
with col5:
st.write("## Lines")
st.image(Image.fromarray(weed_map_lines), width=300)
st.write("## Lines")
st.dataframe(pd.DataFrame(lines.cpu(), columns=["rho", "theta"]))
st.write("## Original Lines")
st.dataframe(
pd.DataFrame(
(original_lines.cpu() if original_lines is not None else []),
columns=["rho", "theta"],
)
)
if __name__ == "__main__":
st.set_page_config(layout="wide")
default_roots = {
"PhenoBench": "dataset/PhenoBench",
"New Dataset": "dataset/patches/512"
}
with st.sidebar:
st.selectbox("Modality", ["PhenoBench", "New Dataset"], key="modality")
st.text_input(
value=default_roots[st.session_state["modality"]],
key="root",
label="root",
)
if st.session_state["modality"] == "PhenoBench":
# For PhenoBench, we use a simple train/val split selector
split = st.selectbox("Split", [["train"], ["val"]], key="phenobench_split")
dataset = get_dataset(
st.session_state["root"], st.session_state["modality"], fields=split
)
else:
fields = st.multiselect(
"Fields",
["000", "001", "002", "003", "004"],
key="fields",
default=["000", "001", "002", "003", "004"],
)
dataset = get_dataset(
st.session_state["root"], st.session_state["modality"], fields
)
st.session_state["dataset"] = dataset
st.slider(
"i",
max_value=len(st.session_state["dataset"])-1,
step=1,
key="slider_i",
on_change=lambda: change_state("slider_i", "number_i"),
) # 👈 this is a widget
st.number_input(
"i",
key="number_i",
value=0,
on_change=lambda: change_state("number_i", "slider_i"),
)
st.text_input(value="", label="img_name", key="img_name")
if st.session_state["modality"] == "PhenoBench":
if st.session_state.get("vegetation_detector") != "ExGDetector":
st.warning("NDVI is not supported for PhenoBench. Switching to ExGDetector.")
st.session_state["vegetation_detector"] = "ExGDetector"
st.selectbox(
"Vegetation Detector",
["ExGDetector"],
key="vegetation_detector",
index=0,
disabled=True
)
else:
st.selectbox(
"Vegetation Detector",
["NDVIDetector", "ExGDetector"],
key="vegetation_detector",
index=0,
)
ndvi_threshold = st.slider(
"ndvi_threshold",
min_value=0.0,
max_value=1.0,
step=0.01,
value=0.6,
key="ndvi_threshold",
)
exg_threshold = st.slider(
"exg_threshold",
min_value=-1.0,
max_value=2.0,
step=0.01,
value=-2.0,
key="exg_threshold",
)
exg_min_area = st.number_input(
"exg_min_area",
min_value=0,
max_value=1000,
value=60,
step=1,
key="exg_min_area",
)
st.session_state["labeller"] = get_vegetation_detector(
ndvi_threshold=ndvi_threshold,
exg_threshold=exg_threshold,
exg_min_area=exg_min_area,
)
col1, col2 = st.columns(2)
with col1:
st.slider(
"threshold", min_value=0, max_value=255, step=1, value=150, key="threshold"
)
st.slider(
"step_theta", min_value=1, max_value=10, step=1, value=1, key="step_theta"
)
st.slider(
"step_rho", min_value=1, max_value=10, step=1, value=1, key="step_rho"
)
# New theta override option: Checkbox to enable override
override_theta = st.checkbox("Override theta calculation?", key="override_theta")
if override_theta:
# The number_input widget creates st.session_state["theta_value"] automatically.
theta_value = st.number_input("Fixed theta value", key="theta_value", value=0.0, step=0.01)
else:
theta_value = None
with col2:
st.slider(
"angle_error", min_value=0, max_value=10, step=1, value=3, key="angle_error"
)
st.slider(
"clustering_tol",
min_value=0,
max_value=50,
step=1,
value=3,
key="clustering_tol",
)
st.slider(
"uniform_significance",
min_value=0.0,
max_value=1.0,
step=0.01,
value=0.1,
key="uniform_significance",
)
st.slider(
"theta_reduction_threshold",
min_value=0.0,
max_value=1.0,
step=0.01,
value=1.0,
key="theta_reduction_threshold",
)
display_prediction()
st.text_input(value="dataset/generated", label="out_dir", key="out_dir")
if st.button("label"):
bar = st.progress(0)
for i in save_and_label(
outdir=st.session_state["out_dir"],
plant_detector_params=dict(
name=st.session_state["vegetation_detector"],
params=dict(
checkpoint=st.session_state["checkpoint"],
threshold=st.session_state["ndvi_threshold"],
),
),
hough_detector_params=dict(
threshold=st.session_state["threshold"],
step_theta=st.session_state["step_theta"],
step_rho=st.session_state["step_rho"],
angle_error=st.session_state["angle_error"],
clustering_tol=st.session_state["clustering_tol"],
uniform_significance=st.session_state["uniform_significance"],
theta_reduction_threshold=st.session_state["theta_reduction_threshold"],
theta_value=st.session_state["theta_value"],
),
dataset_params=dict(
root=st.session_state["root"],
modality=st.session_state["modality"],
fields=st.session_state["fields"],
),
interactive=True,
):
bar.progress(i / len(st.session_state["dataset"]))
# st.slider('theta', min_value=0.0, max_value=5.0, step=0.01, value=0.0, key="theta")
# st.slider('rho', min_value=0, max_value=1000, step=1, value=0, key="rho")
# blank = np.zeros((3, 300, 300), np.uint8)
# blank = get_drawn_img(blank, [(st.session_state['rho'], st.session_state['theta'])])
# st.image(blank, width=300)
# st.write(np.rad2deg(st.session_state['theta']))
# st.write(np.sin(2*st.session_state['theta']))
# st.write((1 - np.abs(np.sin(2*st.session_state['theta']))))
# st.slider('min_reduce', min_value=0.0, max_value=1.0, step=0.01, value=0.5, key="min_reduce")
# min_reduce = st.session_state['min_reduce']
# st.write((1 - np.abs(np.sin(2*st.session_state['min_reduce']))) * (1 - min_reduce) / 1 + min_reduce)