forked from google/youtube-8m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_extractor_test.py
51 lines (39 loc) · 1.64 KB
/
feature_extractor_test.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
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for feature_extractor."""
import json
import os
import feature_extractor
import numpy
from PIL import Image
from six.moves import cPickle
from tensorflow.python.platform import googletest
def _FilePath(filename):
return os.path.join('testdata', filename)
def _MeanElementWiseDifference(a, b):
"""Calculates element-wise percent difference between two numpy matrices."""
difference = numpy.abs(a - b)
denominator = numpy.maximum(numpy.abs(a), numpy.abs(b))
# We dont care if one is 0 and another is 0.01
return (difference / (0.01 + denominator)).mean()
class FeatureExtractorTest(googletest.TestCase):
def setUp(self):
self._extractor = feature_extractor.YouTube8MFeatureExtractor()
def testPCAOnFeatureVector(self):
sports_1m_test_data = cPickle.load(open(_FilePath('sports1m_frame.pkl')))
actual_pca = self._extractor.apply_pca(sports_1m_test_data['original'])
expected_pca = sports_1m_test_data['pca']
self.assertLess(_MeanElementWiseDifference(actual_pca, expected_pca), 1e-5)
if __name__ == '__main__':
googletest.main()