Spaces:
Build error
Build error
englert
commited on
Commit
•
1865e20
1
Parent(s):
7de0d41
update app.py
Browse files- app.py +3 -5
- sampling_util.py +4 -4
app.py
CHANGED
@@ -23,8 +23,6 @@ model.eval()
|
|
23 |
avg_pool = torch.nn.AdaptiveAvgPool2d((1, 1))
|
24 |
|
25 |
def predict(input_file, downsample_size):
|
26 |
-
downsample_size = int(downsample_size)
|
27 |
-
|
28 |
base_directory = os.getcwd()
|
29 |
selected_directory = os.path.join(base_directory, "selected_images")
|
30 |
if os.path.isdir(selected_directory):
|
@@ -53,8 +51,8 @@ def predict(input_file, downsample_size):
|
|
53 |
img_vecs = np.asarray(img_vecs)
|
54 |
print("images encoded")
|
55 |
rv_indices, _ = furthest_neighbours(
|
56 |
-
img_vecs,
|
57 |
-
downsample_size,
|
58 |
seed=0)
|
59 |
indices = np.zeros((img_vecs.shape[0],))
|
60 |
indices[np.asarray(rv_indices)] = 1
|
@@ -88,7 +86,7 @@ demo = gr.Interface(
|
|
88 |
title="Frame selection by visual difference",
|
89 |
fn=predict,
|
90 |
inputs=[gr.components.Video(label="Upload Video File"),
|
91 |
-
gr.components.Number(label="Downsample size")],
|
92 |
outputs=gr.components.File(label="Zip"),
|
93 |
)
|
94 |
|
|
|
23 |
avg_pool = torch.nn.AdaptiveAvgPool2d((1, 1))
|
24 |
|
25 |
def predict(input_file, downsample_size):
|
|
|
|
|
26 |
base_directory = os.getcwd()
|
27 |
selected_directory = os.path.join(base_directory, "selected_images")
|
28 |
if os.path.isdir(selected_directory):
|
|
|
51 |
img_vecs = np.asarray(img_vecs)
|
52 |
print("images encoded")
|
53 |
rv_indices, _ = furthest_neighbours(
|
54 |
+
x=img_vecs,
|
55 |
+
downsample_size=downsample_size,
|
56 |
seed=0)
|
57 |
indices = np.zeros((img_vecs.shape[0],))
|
58 |
indices[np.asarray(rv_indices)] = 1
|
|
|
86 |
title="Frame selection by visual difference",
|
87 |
fn=predict,
|
88 |
inputs=[gr.components.Video(label="Upload Video File"),
|
89 |
+
gr.components.Number(label="Downsample size", precision=0)],
|
90 |
outputs=gr.components.File(label="Zip"),
|
91 |
)
|
92 |
|
sampling_util.py
CHANGED
@@ -3,14 +3,14 @@ import numpy as np
|
|
3 |
from fastdist2 import euclidean_vector_to_matrix_distance
|
4 |
|
5 |
|
6 |
-
def furthest_neighbours(x,
|
7 |
x = x.astype(np.float32)
|
8 |
np.random.seed(seed)
|
9 |
length = x.shape[0]
|
10 |
img_vecs_dims = x.shape[-1]
|
11 |
|
12 |
-
rv_indices = [np.random.randint(low=0, high=
|
13 |
-
selected_points = np.zeros((
|
14 |
selected_points[0, :] = x[rv_indices[0], :]
|
15 |
|
16 |
distance_for_selected_min = np.ones((length,)) * 1e15
|
@@ -18,7 +18,7 @@ def furthest_neighbours(x, downsampled_size, seed):
|
|
18 |
inactive_points = np.zeros(length, dtype=bool)
|
19 |
inactive_points[rv_indices[0]] = True
|
20 |
|
21 |
-
for i in (range(
|
22 |
distance_for_selected = euclidean_vector_to_matrix_distance(selected_points[i, :], x)
|
23 |
distance_for_selected_min = np.minimum(distance_for_selected_min, distance_for_selected)
|
24 |
furthest_point_idx = np.argmax(np.ma.array(distance_for_selected_min, mask=inactive_points))
|
|
|
3 |
from fastdist2 import euclidean_vector_to_matrix_distance
|
4 |
|
5 |
|
6 |
+
def furthest_neighbours(x, downsample_size, seed):
|
7 |
x = x.astype(np.float32)
|
8 |
np.random.seed(seed)
|
9 |
length = x.shape[0]
|
10 |
img_vecs_dims = x.shape[-1]
|
11 |
|
12 |
+
rv_indices = [np.random.randint(low=0, high=downsample_size - 1, size=1)[0]]
|
13 |
+
selected_points = np.zeros((downsample_size, img_vecs_dims), np.float32)
|
14 |
selected_points[0, :] = x[rv_indices[0], :]
|
15 |
|
16 |
distance_for_selected_min = np.ones((length,)) * 1e15
|
|
|
18 |
inactive_points = np.zeros(length, dtype=bool)
|
19 |
inactive_points[rv_indices[0]] = True
|
20 |
|
21 |
+
for i in (range(downsample_size - 1)):
|
22 |
distance_for_selected = euclidean_vector_to_matrix_distance(selected_points[i, :], x)
|
23 |
distance_for_selected_min = np.minimum(distance_for_selected_min, distance_for_selected)
|
24 |
furthest_point_idx = np.argmax(np.ma.array(distance_for_selected_min, mask=inactive_points))
|