-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvideotest_psnr.py
151 lines (120 loc) · 4.99 KB
/
videotest_psnr.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
import tensorflow as tf
import os
import time
import numpy as np
import pickle
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from models import generator
from utils import DataLoader, load, save, psnr_error
from constant import const
import evaluate
slim = tf.contrib.slim
os.environ['CUDA_DEVICES_ORDER'] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = const.GPU
dataset_name = const.DATASET
test_folder = const.TEST_FOLDER
video_num = const.VIDEO_NUM
num_his = const.NUM_HIS
height, width = 256, 256
snapshot_dir = const.SNAPSHOT_DIR
psnr_dir = const.PSNR_DIR
evaluate_name = const.EVALUATE
print(const)
# define dataset
with tf.name_scope('dataset'):
test_video_clips_tensor = tf.placeholder(shape=[1, height, width, 3 * (num_his + 1)],
dtype=tf.float32)
test_inputs = test_video_clips_tensor[..., 0:num_his*3]
test_gt = test_video_clips_tensor[..., -3:]
print('test inputs = {}'.format(test_inputs))
print('test prediction gt = {}'.format(test_gt))
# define testing generator function and
# in testing, only generator networks, there is no discriminator networks and flownet.
with tf.variable_scope('generator', reuse=None):
print('testing = {}'.format(tf.get_variable_scope().name))
test_outputs = generator(test_inputs, layers=4, output_channel=3)
test_psnr_error = psnr_error(gen_frames=test_outputs, gt_frames=test_gt)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
# dataset
data_loader = DataLoader(test_folder, height, width)
# initialize weights
sess.run(tf.global_variables_initializer())
print('Init global successfully!')
# tf saver
saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=None)
restore_var = [v for v in tf.global_variables()]
loader = tf.train.Saver(var_list=restore_var)
def inference_func(ckpt, video_num):
load(loader, sess, ckpt)
videos_info = data_loader.videos
num_videos = len(videos_info.keys())
total = 0
timestamp = time.time()
x = []
y = []
v = 0
for video_name, video in videos_info.items():
v = v + 1
if v == video_num:
length = video['length']
total += length
psnrs = np.empty(shape=(length,), dtype=np.float32)
for i in range(num_his, length):
video_clip = data_loader.get_video_clips(video_name, i - num_his, i + 1)
psnr = sess.run(test_psnr_error,
feed_dict={test_video_clips_tensor: video_clip[np.newaxis, ...]})
psnrs[i] = psnr
print('video = {} / {}, i = {} / {}, psnr = {:.6f}'.format(
video_name, num_videos, i, length, psnr))
x.append(i)
y.append(psnr)
x = [ int(i) for i in x ]
y = [ float(i) for i in y ]
s = "Video{}_Anomaly_Detection".format(video_name)
plt.plot(x,y)
plt.xlabel("#Frame")
plt.ylabel("PSNR")
plt.title(s)
plt.savefig(s + '.jpg')
if os.path.isdir(snapshot_dir):
def check_ckpt_valid(ckpt_name):
is_valid = False
ckpt = ''
if ckpt_name.startswith('model.ckpt-'):
ckpt_name_splits = ckpt_name.split('.')
ckpt = str(ckpt_name_splits[0]) + '.' + str(ckpt_name_splits[1])
ckpt_path = os.path.join(snapshot_dir, ckpt)
if os.path.exists(ckpt_path + '.index') and os.path.exists(ckpt_path + '.meta') and \
os.path.exists(ckpt_path + '.data-00000-of-00001'):
is_valid = True
return is_valid, ckpt
def scan_psnr_folder():
tested_ckpt_in_psnr_sets = set()
for test_psnr in os.listdir(psnr_dir):
tested_ckpt_in_psnr_sets.add(test_psnr)
return tested_ckpt_in_psnr_sets
def scan_model_folder():
saved_models = set()
for ckpt_name in os.listdir(snapshot_dir):
is_valid, ckpt = check_ckpt_valid(ckpt_name)
if is_valid:
saved_models.add(ckpt)
return saved_models
tested_ckpt_sets = scan_psnr_folder()
while True:
all_model_ckpts = scan_model_folder()
new_model_ckpts = all_model_ckpts - tested_ckpt_sets
for ckpt_name in new_model_ckpts:
# inference
ckpt = os.path.join(snapshot_dir, ckpt_name)
inference_func(ckpt, dataset_name, evaluate_name)
tested_ckpt_sets.add(ckpt_name)
print('waiting for models...')
evaluate.evaluate('compute_auc', psnr_dir)
time.sleep(60)
else:
inference_func(snapshot_dir, video_num)