-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
murmur
committed
Jul 24, 2019
1 parent
1ec148f
commit fd24aba
Showing
7 changed files
with
186 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.vscode | ||
*.h5 | ||
*.pth | ||
log_dir | ||
save_dir | ||
score_dir | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import torch.nn as nn | ||
from collections import OrderedDict | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# http://www.geeksforgeeks.org/knapsack-problem/ | ||
|
||
import numpy as np | ||
|
||
def knapsack(v, w, max_weight): | ||
rows = len(v) + 1 | ||
cols = max_weight + 1 | ||
|
||
# adding dummy values as later on we consider these values as indexed from 1 for convinence | ||
|
||
v = np.r_[[0], v] | ||
w = np.r_[[0], w] | ||
|
||
# row : values , #col : weights | ||
dp_array = [[0 for i in range(cols)] for j in range(rows)] | ||
|
||
# 0th row and 0th column have value 0 | ||
|
||
# values | ||
for i in range(1, rows): | ||
# weights | ||
for j in range(1, cols): | ||
# if this weight exceeds max_weight at that point | ||
if j - w[i] < 0: | ||
dp_array[i][j] = dp_array[i - 1][j] | ||
|
||
# max of -> last ele taken | this ele taken + max of previous values possible | ||
else: | ||
dp_array[i][j] = max(dp_array[i - 1][j], v[i] + dp_array[i - 1][j - w[i]]) | ||
|
||
# return dp_array[rows][cols] : will have the max value possible for given wieghts | ||
|
||
chosen = [] | ||
i = rows - 1 | ||
j = cols - 1 | ||
|
||
# Get the items to be picked | ||
while i > 0 and j > 0: | ||
|
||
# ith element is added | ||
if dp_array[i][j] != dp_array[i - 1][j]: | ||
# add the value | ||
chosen.append(i-1) | ||
# decrease the weight possible (j) | ||
j = j - w[i] | ||
# go to previous row | ||
i = i - 1 | ||
|
||
else: | ||
i = i - 1 | ||
|
||
return dp_array[rows - 1][cols - 1], chosen | ||
|
||
# main | ||
if __name__ == "__main__": | ||
values = list(map(int, input().split())) | ||
weights = list(map(int, input().split())) | ||
max_weight = int(input()) | ||
|
||
max_value, chosen = knapsack(values, weights, max_weight) | ||
|
||
print("The max value possible is") | ||
print(max_value) | ||
|
||
print("The index chosen for these are") | ||
print(' '.join(str(x) for x in chosen)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import torch | ||
import numpy as np | ||
|
||
from knapsack import knapsack | ||
|
||
|
||
def eval_metrics(y_pred: torch.Tensor, y_true: torch.Tensor): | ||
overlap = (y_pred * y_true).sum().item() | ||
precision = overlap / (y_pred.sum().item() + 1e-8) | ||
recall = overlap / (y_true.sum().item() + 1e-8) | ||
if precision == 0 and recall == 0: | ||
fscore = 0 | ||
else: | ||
fscore = 2 * precision * recall / (precision + recall) | ||
return [precision, recall, fscore] | ||
|
||
|
||
def select_keyshots(num_frames, cps, weight, value): | ||
_, selected = knapsack(value, weight, int(0.15 * num_frames)) | ||
selected = selected[::-1] | ||
key_labels = np.zeros(shape=(num_frames,)) | ||
for i in selected: | ||
key_labels[cps[i][0]:cps[i][1]] = 1 | ||
return selected, key_labels | ||
|
||
|
||
def upsample(down_arr, N): | ||
up_arr = np.zeros(N) | ||
ratio = N // 320 | ||
l = (N - ratio * 320) // 2 | ||
i = 0 | ||
while i < 320: | ||
up_arr[l:l+ratio] = np.ones(ratio, dtype=int) * down_arr[i] | ||
l += ratio | ||
i += 1 | ||
return up_arr | ||
|
||
|
||
def eval_single(video_info, pred_score): | ||
""" | ||
Evaluate F-score of given video and pred_score. | ||
Args: | ||
video_info: hdf5 dataset instance, containing necessary infomation for evaluation. | ||
pred_score: output of FCSN model. | ||
Returns: | ||
evaluation result (precision, recall, f-score). | ||
""" | ||
N = video_info['length'][()] | ||
cps = video_info['change_points'][()] | ||
weight = video_info['n_frame_per_seg'][()] | ||
true_summary_arr = video_info['user_summary'][()] | ||
pred_score = np.array(pred_score.cpu().data) | ||
pred_score = upsample(pred_score, N) | ||
pred_value = np.array([pred_score[cp[0]:cp[1]].mean() for cp in cps]) | ||
pred_selected, pred_summary = select_keyshots(N, cps, weight, pred_value) | ||
eval_arr = [eval_metrics(pred_summary, true_summary) for true_summary in true_summary_arr] | ||
eval_res = np.mean(eval_arr, axis=0) | ||
return eval_res.tolist() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters