forked from PyDMD/PyDMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial-12-cdmd.py
648 lines (469 loc) · 14.8 KB
/
tutorial-12-cdmd.py
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#!/usr/bin/env python
# coding: utf-8
# # Foreground Modeling using Dynamic Mode Decomposition
#
# This tutorial is inspired by the paper [Compressed dynamic mode decomposition for background modeling](https://arxiv.org/abs/1512.04205) by Erichson et al.
#
# Author: [Josh Myers-Dean](https://joshmyersdean.github.io/)
#
# Packages needed: **PyDMD**, NumPy, pandas, opencv-python, Matplotlib
# ## Download data
#
# First we will download the data, [SegTrackV2](https://web.engr.oregonstate.edu/~lif/SegTrack2/dataset.html) from Oregon State. This is an older binary segmentation dataset that offers a good test bed for this method. This cell could take some seconds to execute since it downloads and unzips a compressed file of size 200MB.
# In[1]:
import os
print("Downloading data...")
if not os.path.exists("SegTrackv2"):
get_ipython().system(
"wget https://web.engr.oregonstate.edu/~lif/SegTrack2/SegTrackv2.zip >& /dev/null"
)
get_ipython().system("unzip -qq SegTrackv2.zip")
print("Done!")
else:
print("Data already present")
# In[2]:
from pydmd import CDMD, DMD
from pydmd.plotter import plot_eigs
import matplotlib.pyplot as plt
from matplotlib import style
plt.gray()
style.use("tableau-colorblind10")
from matplotlib import animation
from IPython.display import HTML
import numpy as np
import pandas as pd
import cv2
from typing import Tuple
# We peak at the data to examine the amount of frames per object video. Note that the videos with small number frames will most likely have poor results. It is recommended to use the 'Frog' and 'Worm' videos.
# In[3]:
videos = sorted(os.listdir("SegTrackv2/JPEGImages"))[1:]
vid_metadata = {}
for i in videos:
vid_metadata[i] = len(os.listdir(f"SegTrackv2/JPEGImages/{i}"))
df = pd.DataFrame(vid_metadata.items(), columns=["Name", "Number of Frames"])
valid = [
"birds_of_paradise",
"birdfall",
"frog",
"monkey",
"parachute",
"soldier",
"worm",
]
df = df[df.Name.isin(valid)]
display(df)
# In[4]:
OBJ = "frog" # Change this to desired object
assert OBJ in df["Name"].to_list(), "Object not found in dataset"
# ## Methods needed for the tutorial
# Below we define (and comment) some methods which we're going to use later on.
# In[5]:
def get_video_dmd(
object: str = "frog", noise: bool = False, noise_amt: float = 0.01
) -> Tuple[np.ndarray, Tuple[int, int]]:
"""
Retreives a video in matrix format (i.e., each column is a frame)
Parameters
----------
object :
name of video
noise :
boolean to use noise or not
noise_amt :
standard deviation for noise ~ N(0, noise_amt)
Returns
-------
Tuple:
Matrix representation of the video
Original shape of frame
"""
imgs = []
jpeg_dir = "SegTrackv2/JPEGImages/"
shape = None
for i in sorted(os.listdir(f"{jpeg_dir}{object}")):
tmp = cv2.imread(f"{jpeg_dir}{object}/{i}", cv2.IMREAD_GRAYSCALE)
shape = tmp.shape
tmp = tmp.reshape(-1).astype(np.float32) / 255
imgs.append(tmp)
vid = np.vstack(imgs).T
if noise:
vid += np.random.normal(0, noise_amt, vid.shape)
vid = vid.clip(0, 1)
return np.vstack(imgs).T, shape
def get_video(object: str = "frog") -> np.ndarray:
"""
Retreives a video in tensor format (i.e., frames x heigh x width)
Parameters
----------
object :
name of video
Returns
-------
Numpy array:
Tensor representation of the video
"""
imgs = []
jpeg_dir = "SegTrackv2/JPEGImages/"
for i in sorted(os.listdir(f"{jpeg_dir}{object}")):
tmp = cv2.imread(f"{jpeg_dir}{object}/{i}", cv2.IMREAD_GRAYSCALE)
tmp = tmp.astype(np.float32) / 255
imgs.append(tmp)
return np.asarray(imgs)
def calc_iou(pred: np.ndarray, truth: np.ndarray, thresh: float = 0.1) -> float:
"""
Helper method to calculate IoU for single frame
Parameters
----------
pred :
background subtracted video
truth :
segmentation ground truth
thresh :
cut off for deciding if a pixel is foreground
Returns
-------
float:
IoU of a frame
"""
pred[pred < thresh] = 0
pred[pred >= thresh] = 1
intersection = np.logical_and(pred, truth).sum()
union = np.logical_or(pred, truth).sum()
return intersection / union if union > 0 else 0
def calc_miou(pred, truth, thresh=0.1) -> float:
"""
Calculate average IoU for a video
Parameters
----------
pred :
background subtracted video
truth :
segmentation ground truth
thresh :
cut off for deciding if a pixel is foreground
Returns
-------
float:
mIoU of a video
"""
assert pred.shape == truth.shape, "Pred and truth must be same shape"
pred = pred.copy()
miou = 0
for i in range(pred.shape[0]):
iou = calc_iou(pred[i, :, :], truth[i, :, :], thresh=thresh)
miou += iou
return miou / pred.shape[0]
def f1_score(y_true: np.ndarray, y_pred: np.ndarray, beta: int = 1) -> float:
"""
Calculate F1 score.
The original implmentation is written by Michal Haltuf on Kaggle.
Parameters
----------
y_true :
segmentation ground truth
y_pred :
background subtracted video
Returns
-------
float:
f1 score of a video
"""
tp = (y_true * y_pred).sum()
tn = ((1 - y_true) * (1 - y_pred)).sum()
fp = ((1 - y_true) * y_pred).sum()
fn = (y_true * (1 - y_pred)).sum()
epsilon = 1e-7
precision = tp / (tp + fp + epsilon)
recall = tp / (tp + fn + epsilon)
f1 = (1 + beta**2) * (precision * recall)
f1 /= beta**2 * precision + recall + epsilon
return f1
def calc_f1(pred: np.ndarray, truth: np.ndarray, thresh: float = 0.1) -> float:
"""
Calculate f1 score for a video
Parameters
----------
pred :
background subtracted video
truth :
segmentation ground truth
thresh :
cut off for deciding if a pixel is foreground
Returns
-------
float:
f1 score of a video
"""
assert pred.shape == truth.shape, "Pred and truth must be same shape"
pred = pred.copy()
truth = truth.copy()
pred[pred < thresh] = 0
pred[pred >= thresh] = 1
pred = pred.astype(np.uint8)
truth = truth.astype(np.uint8)
return f1_score(truth.flatten(), pred.flatten())
def get_gt_video(object: str = "frog") -> np.ndarray:
"""
Retreives a video of GT in tensor format (i.e., frames x heigh x width)
Parameters
----------
object :
name of video
Returns
-------
Numpy array:
Tensor representation of the GT video
"""
imgs = []
jpeg_dir = "SegTrackv2/GroundTruth/"
gt_dir = "SegTrackv2/JPEGImages/"
jpeg_list = sorted(os.listdir(f"{jpeg_dir}{object}"))
gt_list = sorted(os.listdir(f"{gt_dir}{object}"))
valid = list(set(jpeg_list).intersection(set(gt_list)))
for i in sorted(valid):
tmp = f"{gt_dir}/{i}"
tmp = cv2.imread(f"{jpeg_dir}{object}/{i}", cv2.IMREAD_GRAYSCALE)
tmp = tmp.astype(np.float32) / 255
imgs.append(tmp)
return np.asarray(imgs)
def play_video(object: str = "frog", interval: int = 10) -> None:
"""
Helper function to play original video
Parameters
----------
object :
name of video
interval :
delay in ms between frames
"""
video = get_video(object=object)
fig = plt.figure()
im = plt.imshow(video[0, :, :])
plt.close() # this is required to not display the generated image
def init():
im.set_data(video[0, :, :])
def animate(i):
im.set_data(video[i, :, :])
return im
anim = animation.FuncAnimation(
fig, animate, init_func=init, frames=video.shape[0], interval=interval
)
return HTML(anim.to_html5_video())
def play_gt_video(object: str = "frog", interval: int = 10):
"""
Helper function to play GT video
Parameters
----------
object :
name of video
interval :
delay in ms between frames
"""
video = get_gt_video(object=object)
fig = plt.figure()
im = plt.imshow(video[0, :, :])
plt.close() # this is required to not display the generated image
def init():
im.set_data(video[0, :, :])
def animate(i):
im.set_data(video[i, :, :])
return im
anim = animation.FuncAnimation(
fig, animate, init_func=init, frames=video.shape[0], interval=interval
)
return HTML(anim.to_html5_video())
def get_video_removed(video: np.ndarray, bg: np.ndarray) -> np.ndarray:
"""
Helper function to subtract background from video
Parameters
----------
video :
original video
bg :
background model
Returns
-------
np.ndarray :
predicted foreground
"""
for i in range(video.shape[0]):
video[i, :, :] -= bg
return video
def play_video_removed(
bg: np.ndarray,
object: str = "frog",
mask: bool = False,
interval: int = 10,
thresh: float = 0.1,
) -> None:
"""
Helper function to play foreground video
Parameters
----------
bg :
background model
object :
name of video
mask :
Show binary version
interval :
delay in ms between frames
thresh :
value to decide if pixel is foreground
"""
video = get_video(object=object)
fig = plt.figure()
subbed = video[0, :, :] - bg
if mask:
subbed[subbed > thresh] = 1
subbed[subbed <= thresh] = 0
else:
subbed = subbed.clip(0, 1)
im = plt.imshow(subbed)
plt.close() # this is required to not display the generated image
def init():
subbed = video[0, :, :] - bg
if mask:
subbed[subbed > thresh] = 1
subbed[subbed <= thresh] = 0
else:
subbed = subbed.clip(0, 1)
im.set_data(subbed)
def animate(i):
subbed = video[i, :, :] - bg
if mask:
subbed[subbed > thresh] = 1
subbed[subbed <= thresh] = 0
else:
subbed = subbed.clip(0, 1)
im.set_data(subbed)
return im
anim = animation.FuncAnimation(
fig, animate, init_func=init, frames=video.shape[0], interval=interval
)
return HTML(anim.to_html5_video())
# ## Let's peak at the data!
#
# The parameter `interval` is the delay (in ms) between frames. For shorter videos, use a higher interval.
# In[6]:
"""
play the video we will model,
change interval based on video size
"""
play_video(OBJ, interval=10)
# In[7]:
# show gt video
play_gt_video(OBJ, interval=10)
# ## Fitting a DMD instance
# Time to fit our video to (c)DMD, add noise, tinker with SVD rank, etc. We can also visualize our background model! Since we average the first `K` modes, we can visualize how our model changes wrt `K`.
#
# Recall that each column of our matrix will be a frame in the video and unless we have a long video, the system will be overdetermined. Note that in the paper the authors use an optimization scheme to decide on the number of modes; we are going to choose it emprically.
# In[8]:
use_noise = False
noise = 0.01
video, shape = get_video_dmd(OBJ, use_noise, noise) # get video
print(f"Condition number of video matrix is {np.linalg.cond(video): .3f}")
# In[9]:
comp = True # use compressed DMD
svd_rank = 0 # rank=0 will automatically detect rank, try other values next!
optim = True # Use optimized DMD
compression = ["linear", "sparse", "uniform", "sample"]
cmat = compression[2] # compression matrix
if comp:
dmd = CDMD(svd_rank=svd_rank, opt=optim, compression_matrix=cmat).fit(video)
else:
dmd = DMD(svd_rank=svd_rank, opt=optim).fit(video)
# In[10]:
modes = dmd.reconstructed_data.T.reshape(video.shape[1], shape[0], shape[1])
# Try changing the value of K, amount of modes we use
K = min(100, modes.shape[0])
modes = np.abs(modes) # deal with complex values
bg = np.zeros_like(modes[0, :, :])
# In[11]:
fig, axes = plt.subplots(5, 5, figsize=(16, 16))
axes = axes.flatten()
idx = 0
for k in range(K):
bg += modes[k, :, :]
if k % 4 == 0:
if idx >= len(axes):
continue
axes[idx].axis("off")
axes[idx].imshow(bg / (k + 1))
axes[idx].set_title(f"K = {k}")
idx += 1
plt.suptitle("Background Model using varying amount of Modes", y=0.92)
plt.show()
# We plot the background:
# In[12]:
bg /= K
plt.imshow(bg)
plt.title("Background Model")
plt.show()
# And an example frame:
# In[13]:
tmp = get_video(OBJ)
img = tmp[0, :, :]
fg = (img - bg).clip(0, 1)
plt.title("Foreground for Frame 0")
plt.imshow(fg)
plt.show()
# ## Results
# Time to get some quanitative (and qualitative) results! We will examine mean intersection over union (`mIoU`) and `F1`
# score, respectively. These metrics are a function of the threshold we choose for deciding foreground vs background.
#
# We can also visualize the eigenvalues, modes, and dynamics of our video computed from DMD.
# In[14]:
video_removed = get_video_removed(tmp, bg).clip(0, 1)
gt = get_gt_video(OBJ)
video_removed.shape, gt.shape
# We compute `mIoU` and `F1` as a function of threshold value, takes a bit to run.
# In[15]:
thresholds = np.linspace(0, 1, 10)
mious = []
f1s = []
for thresh in thresholds:
mious.append(calc_miou(video_removed, gt, thresh=thresh))
f1s.append(calc_f1(video_removed, gt, thresh=thresh))
# We plot the results:
# In[16]:
plt.figure(figsize=(20, 5))
plt.subplot(1, 2, 1)
plt.plot(mious, "bo-")
plt.title("mIoU vs Threshold")
plt.ylabel("mIoU")
plt.xlabel("Threshold")
plt.xticks(range(len(thresholds)), np.round(thresholds, 2))
plt.subplot(1, 2, 2)
plt.plot(f1s, "ro-")
plt.title("F1 Score vs Threshold")
plt.ylabel("F1")
plt.xlabel("Threshold")
plt.xticks(range(len(thresholds)), np.round(thresholds, 2))
plt.show()
# We now plot the video with the background removed. You can try playing with the threshold, keep in mind that it shoud remain inside $[0,1]$:
# In[17]:
# show binary output or not, if False thresh doesn't matter
use_mask = False
play_video_removed(bg, OBJ, mask=use_mask, thresh=0.03)
# We print the distances from the unit circle of the first 6 eigenvalues in `dmd.eigs`, and plot all.
# In[18]:
for idx, eig in enumerate(dmd.eigs[:6]):
print(
f"Eigenvalue {eig}: distance from unit circle {np.abs(np.abs(eig)-1): .5f}"
)
plot_eigs(dmd, show_axes=True, show_unit_circle=True)
# We also plot the first 6 modes and dynamics. The modes are hard to disentangle when SVD rank is larger than 3 but we can see the slow varying dynamic, which is our background mode!
# In[19]:
plt.figure(figsize=(20, 8))
plt.subplot(1, 2, 1)
for idx, mode in enumerate(dmd.modes.T[:6]):
plt.plot(mode.real, alpha=0.5, label=f"Mode {idx}")
plt.title("Modes")
plt.legend()
plt.subplot(1, 2, 2)
for idx, dynamic in enumerate(dmd.dynamics[:6]):
plt.plot(dynamic.real, label=f"Mode {idx}")
plt.title("Dynamics")
plt.legend()
plt.show()