Skip to content

Commit e8ea9e4

Browse files
author
hemant
committed
use hdf5 file and feature extraction edit
1 parent a50d4ab commit e8ea9e4

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

cnn3d_similarity_inference.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def extract_features_from_vid(video_path):
5151
opt.n_classes = 400
5252

5353
model = generate_model(opt)
54-
print('loading model {}'.format(opt.model))
54+
# print('loading model {}'.format(opt.model))
5555
model_data = torch.load(opt.model)
5656
assert opt.arch == model_data['arch']
5757
model.load_state_dict(model_data['state_dict'])
@@ -67,14 +67,14 @@ def extract_features_from_vid(video_path):
6767

6868
if os.path.exists(opt.input):
6969
subprocess.call('mkdir tmp', shell=True)
70-
subprocess.call('ffmpeg -i {} tmp/image_%05d.jpg'.format(video_path),
71-
shell=True)
72-
result = classify_video('tmp', video_filename, class_names, model, opt)
70+
subprocess.call('ffmpeg -loglevel panic -i {} tmp/image_%05d.jpg'.format(video_path),shell=True)
71+
result = classify_video('tmp', video_path, class_names, model, opt)
7372
outputs.append(result)
7473
subprocess.call('rm -rf tmp', shell=True)
7574
else:
7675
print('{} does not exist'.format(video_path))
77-
return outputs[0]
76+
77+
return np.array(outputs[0]['clips'][0]['features']).astype('float32')
7878

7979
def pickle_to_hdf5(pickle_file='./cnn3d_features/ucf101_3dcnn_features'):
8080
features = pickle.load(open(pickle_file, 'rb'))
@@ -159,17 +159,17 @@ def load_video_labels(video_labels_filename, features_path):
159159
video_labels = vl['ucf101_cdd3d_video_labels'][:]
160160
return video_labels
161161
else:
162-
print(video_labels_filename + ' does not exist!')
162+
print(video_labels_filename+' does not exist!')
163163

164164

165-
def similar_cnn3d_ucf_video(video_path, feature_vectors, k=5, dist=False, verbose=False, gpu=True, query_features=None):
165+
def similar_cnn3d_ucf_video(video_path, k=5, dist=False, verbose=False, gpu=True, query_features=None):
166166
"""
167167
This function extracts features from the query video and performs kNN similarity search.
168168
"""
169169
try:
170-
# query_features = extract_features_from_vid(video_path)
171-
assert query_features.shape == (1, 512)
172-
distances, feature_indices = knn_cnn_features.run_knn_features(feature_vectors, test_vectors=query_features,
170+
query_features = extract_features_from_vid(video_path)
171+
assert query_features.shape == (512,)
172+
distances, feature_indices = knn_cnn_features.run_knn_features(feature_vectors, test_vectors=query_features[np.newaxis,:],
173173
k=k, dist=True, gpu=gpu)
174174
if verbose:
175175
print(video_labels[feature_indices][0])
@@ -197,8 +197,8 @@ def full_build():
197197
"""
198198
This function runs a full data processing build from raw extracted features.
199199
"""
200-
s_time = time.time()
201-
print('Starting full 3D CNN feature extraction...')
200+
# s_time = time.time()
201+
# print('Starting full 3D CNN feature extraction...')
202202

203203
# feature extraction (run this only if full feature extraction needed)
204204
# print('Extracting features from UCF101 dataset...')
@@ -227,8 +227,8 @@ def full_build():
227227
query_video = video_labels[0]
228228
query_features = feature_vectors[0]
229229
query_features = np.reshape(query_features.astype(np.float32), (1, -1))
230-
cnn3d_dist, cnn3d_indices = similar_cnn3d_ucf_video(query_video, feature_vectors,
231-
k=3, dist=True, verbose=False, gpu=False,
230+
cnn3d_dist, cnn3d_indices = similar_cnn3d_ucf_video(query_video,
231+
k=3, dist=True, verbose=False, gpu=True,
232232
query_features=query_features)
233233
output_query_results(cnn3d_indices, video_labels, video_labels[0])
234234
print('\nFull build successfully completed!')
@@ -242,23 +242,23 @@ def quick_query_test():
242242
perform feature extraction for a single video, instead using one of the
243243
extracted features as query video features.
244244
"""
245-
# data processing
246-
feature_vectors, video_labels = process_output('./cnn3d_features/ucf101_3dcnn_features_sample.json')
247-
248-
# save and load processed features
249-
save_output(feature_vectors, video_labels, 'cnn3d_ucf101')
250-
feature_vectors = load_feature_vectors('feature_vectors_cnn3d_ucf101',
245+
if not use_h5:
246+
# data processing
247+
feature_vectors, video_labels = process_output('./cnn3d_features/ucf101_3dcnn_features_sample.json')
248+
249+
# save and load processed features
250+
save_output(feature_vectors, video_labels, 'cnn3d_ucf101')
251+
feature_vectors = load_feature_vectors('feature_vectors_cnn3d_ucf101',
252+
'./cnn3d_features/')
253+
video_labels = load_video_labels('video_labels_cnn3d_ucf101',
251254
'./cnn3d_features/')
252-
video_labels = load_video_labels('video_labels_cnn3d_ucf101',
253-
'./cnn3d_features/')
254-
255255
# test_query
256256
# query_video_path = 'v_ApplyEyeMakeup_g04_c03.avi'
257257
query_video = video_labels[0]
258258
query_features = feature_vectors[0]
259259
query_features = np.reshape(query_features.astype(np.float32), (1, -1))
260-
cnn3d_dist, cnn3d_indices = similar_cnn3d_ucf_video(query_video, feature_vectors,
261-
k=3, dist=True, verbose=False, gpu=False,
260+
cnn3d_dist, cnn3d_indices = similar_cnn3d_ucf_video(query_video,
261+
k=3, dist=True, verbose=False, gpu=True,
262262
query_features=query_features)
263263
output_query_results(cnn3d_indices, video_labels, video_labels[0])
264264
print('\nTest successfully completed!')
@@ -270,7 +270,17 @@ def load_cnn3d_feature_data_ucf():
270270
h5f.close()
271271
return feature_vectors, video_labels
272272

273+
use_h5=True
274+
if use_h5:
275+
feature_vectors, video_labels = load_cnn3d_feature_data_ucf()
276+
277+
# import time
278+
# start = time.time()
279+
# for i in range(10):
280+
# similar_cnn3d_ucf_video('data/UCF101/v_ApplyEyeMakeup_g01_c01.avi', verbose=False)
281+
# print((time.time()-start)/10)
282+
# 2.168000817298889 seconds
283+
273284
# test or full build
274285
# quick_query_test()
275286
# full_build()
276-
# load_cnn3d_feature_data_ucf()

0 commit comments

Comments
 (0)