本文整理汇总了Python中smqtk.representation.descriptor_element.local_elements.DescriptorMemoryElement类的典型用法代码示例。如果您正苦于以下问题:Python DescriptorMemoryElement类的具体用法?Python DescriptorMemoryElement怎么用?Python DescriptorMemoryElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DescriptorMemoryElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_hash
def test_get_hash(self):
fit_descriptors = []
for i in range(5):
d = DescriptorMemoryElement(six.b('test'), i)
d.set_vector([-2. + i, -2. + i])
fit_descriptors.append(d)
# The following "rotation" matrix should cause any 2-feature descriptor
# to the right of the line ``y = -x`` to be True, and to the left as
# False. If on the line, should be True.
itq = ItqFunctor(bit_length=1, random_seed=0)
itq.mean_vec = numpy.array([0., 0.])
itq.rotation = numpy.array([[1. / sqrt(2)],
[1. / sqrt(2)]])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([1, 1])), [True])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([-1, -1])), [False])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([-1, 1])), [True])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([-1.001, 1])), [False])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([-1, 1.001])), [True])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([1, -1])), [True])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([1, -1.001])), [False])
numpy.testing.assert_array_equal(
itq.get_hash(numpy.array([1.001, -1])), [True])
示例2: test_fit_short_descriptors_for_bit_length
def test_fit_short_descriptors_for_bit_length(self):
# Should error when input descriptors have fewer dimensions than set bit
# length for output hash codes (limitation of PCA method currently
# used).
fit_descriptors = []
for i in range(3):
d = DescriptorMemoryElement(six.b('test'), i)
d.set_vector([-1+i, -1+i])
fit_descriptors.append(d)
itq = ItqFunctor(bit_length=8)
self.assertRaisesRegexp(
ValueError,
"Input descriptors have fewer features than requested bit encoding",
itq.fit, fit_descriptors
)
self.assertIsNone(itq.mean_vec)
self.assertIsNone(itq.rotation)
# Should behave the same when input is an iterable
self.assertRaisesRegexp(
ValueError,
"Input descriptors have fewer features than requested bit encoding",
itq.fit, iter(fit_descriptors)
)
self.assertIsNone(itq.mean_vec)
self.assertIsNone(itq.rotation)
示例3: test_build_index
def test_build_index(self):
# Empty memory data elements for storage
empty_data = 'base64://'
f = FlannNearestNeighborsIndex(empty_data, empty_data, empty_data)
# Internal elements should initialize have zero-length byte values
self.assertEqual(len(f._index_elem.get_bytes()), 0)
self.assertEqual(len(f._index_param_elem.get_bytes()), 0)
self.assertEqual(len(f._descr_cache_elem.get_bytes()), 0)
# Make unit vectors, one for each feature
dim = 8
test_descriptors = []
for i in range(dim):
v = numpy.zeros(dim, float)
v[i] = 1.
d = DescriptorMemoryElement('unit', i)
d.set_vector(v)
test_descriptors.append(d)
f.build_index(test_descriptors)
# Internal elements should not have non-zero byte values.
self.assertGreater(len(f._index_elem.get_bytes()), 0)
self.assertGreater(len(f._index_param_elem.get_bytes()), 0)
self.assertGreater(len(f._descr_cache_elem.get_bytes()), 0)
示例4: test_fit_with_cache
def test_fit_with_cache(self):
fit_descriptors = []
for i in range(5):
d = DescriptorMemoryElement(six.b('test'), i)
d.set_vector([-2. + i, -2. + i])
fit_descriptors.append(d)
itq = ItqFunctor(DataMemoryElement(), DataMemoryElement(),
bit_length=1, random_seed=0)
itq.fit(fit_descriptors)
# TODO: Explanation as to why this is the expected result.
numpy.testing.assert_array_almost_equal(itq.mean_vec, [0, 0])
numpy.testing.assert_array_almost_equal(itq.rotation, [[1 / sqrt(2)],
[1 / sqrt(2)]])
self.assertIsNotNone(itq.mean_vec_cache_elem)
numpy.testing.assert_array_almost_equal(
numpy.load(BytesIO(itq.mean_vec_cache_elem.get_bytes())),
[0, 0]
)
self.assertIsNotNone(itq.rotation_cache_elem)
numpy.testing.assert_array_almost_equal(
numpy.load(BytesIO(itq.rotation_cache_elem.get_bytes())),
[[1 / sqrt(2)],
[1 / sqrt(2)]]
)
示例5: test_normal_conditions
def test_normal_conditions(self, mock_dsi_count):
index = DummySI()
mock_dsi_count.return_value = 1
q = DescriptorMemoryElement('q', 0)
q.set_vector(numpy.random.rand(4))
index.nn(q)
示例6: _random_euclidean
def _random_euclidean(self, hash_ftor, hash_idx,
ftor_train_hook=lambda d: None):
# :param hash_ftor: Hash function class for generating hash codes for
# descriptors.
# :param hash_idx: Hash index instance to use in local LSH algo
# instance.
# :param ftor_train_hook: Function for training functor if necessary.
# make random descriptors
i = 1000
dim = 256
td = []
np.random.seed(self.RANDOM_SEED)
for j in range(i):
d = DescriptorMemoryElement('random', j)
d.set_vector(np.random.rand(dim))
td.append(d)
ftor_train_hook(td)
di = MemoryDescriptorIndex()
kvstore = MemoryKeyValueStore()
index = LSHNearestNeighborIndex(hash_ftor, di, kvstore,
hash_index=hash_idx,
distance_method='euclidean')
index.build_index(td)
# test query from build set -- should return same descriptor when k=1
q = td[255]
r, dists = index.nn(q, 1)
self.assertEqual(r[0], q)
# test query very near a build vector
td_q = td[0]
q = DescriptorMemoryElement('query', i)
v = td_q.vector().copy()
v_min = max(v.min(), 0.1)
v[0] += v_min
v[dim-1] -= v_min
q.set_vector(v)
r, dists = index.nn(q, 1)
self.assertFalse(np.array_equal(q.vector(), td_q.vector()))
self.assertEqual(r[0], td_q)
# random query
q = DescriptorMemoryElement('query', i+1)
q.set_vector(np.random.rand(dim))
# for any query of size k, results should at least be in distance order
r, dists = index.nn(q, 10)
for j in range(1, len(dists)):
self.assertGreater(dists[j], dists[j-1])
r, dists = index.nn(q, i)
for j in range(1, len(dists)):
self.assertGreater(dists[j], dists[j-1])
示例7: test_none_set
def test_none_set(self):
d = DescriptorMemoryElement('test', 0)
self.assertFalse(d.has_vector())
d.set_vector(numpy.ones(16))
self.assertTrue(d.has_vector())
numpy.testing.assert_equal(d.vector(), numpy.ones(16))
d.set_vector(None)
self.assertFalse(d.has_vector())
self.assertIs(d.vector(), None)
示例8: test_build_index_one
def test_build_index_one(self):
d = DescriptorMemoryElement('test', 0)
d.set_vector(numpy.zeros(8, float))
index = self._make_inst('euclidean')
index.build_index([d])
self.assertListEqual(
index._descr_cache,
[d]
)
self.assertIsNotNone(index._flann)
self.assertIsInstance(index._flann_build_params, dict)
示例9: test_none_set
def test_none_set(self):
d = DescriptorMemoryElement('test', 0)
ntools.assert_false(d.has_vector())
d.set_vector(numpy.ones(16))
ntools.assert_true(d.has_vector())
numpy.testing.assert_equal(d.vector(), numpy.ones(16))
d.set_vector(None)
ntools.assert_false(d.has_vector())
ntools.assert_is(d.vector(), None)
示例10: test_build_index_read_only
def test_build_index_read_only(self):
v = np.zeros(5, float)
v[0] = 1.
d = DescriptorMemoryElement('unit', 0)
d.set_vector(v)
test_descriptors = [d]
index = self._make_inst(read_only=True)
self.assertRaises(
ReadOnlyError,
index.build_index, test_descriptors
)
示例11: test_configuration
def test_configuration(self):
default_config = DescriptorMemoryElement.get_default_config()
ntools.assert_equal(default_config, {})
inst1 = DescriptorMemoryElement.from_config(default_config, 'test', 'a')
ntools.assert_equal(default_config, inst1.get_config())
ntools.assert_equal(inst1.type(), 'test')
ntools.assert_equal(inst1.uuid(), 'a')
# vector-based equality
inst2 = DescriptorMemoryElement.from_config(inst1.get_config(),
'test', 'a')
ntools.assert_equal(inst1, inst2)
示例12: test_classify
def test_classify(self):
c = IndexLabelClassifier(self.FILEPATH_TEST_LABELS)
m_expected = {
six.b('label_1'): 1,
six.b('label_2'): 2,
six.b('negative'): 3,
six.b('label_3'): 4,
six.b('Kitware'): 5,
six.b('label_4'): 6,
}
d = DescriptorMemoryElement('test', 0)
d.set_vector([1, 2, 3, 4, 5, 6])
m = c._classify(d)
self.assertEqual(m, m_expected)
示例13: _random_euclidean
def _random_euclidean(self, hash_ftor, hash_idx, ftor_train_hook=lambda d: None):
# make random descriptors
i = 1000
dim = 256
td = []
numpy.random.seed(self.RANDOM_SEED)
for j in xrange(i):
d = DescriptorMemoryElement("random", j)
d.set_vector(numpy.random.rand(dim))
td.append(d)
ftor_train_hook(td)
di = MemoryDescriptorIndex()
index = LSHNearestNeighborIndex(hash_ftor, di, hash_idx, distance_method="euclidean")
index.build_index(td)
# test query from build set -- should return same descriptor when k=1
q = td[255]
r, dists = index.nn(q, 1)
ntools.assert_equal(r[0], q)
# test query very near a build vector
td_q = td[0]
q = DescriptorMemoryElement("query", i)
v = td_q.vector().copy()
v_min = max(v.min(), 0.1)
v[0] += v_min
v[dim - 1] -= v_min
q.set_vector(v)
r, dists = index.nn(q, 1)
ntools.assert_false(numpy.array_equal(q.vector(), td_q.vector()))
ntools.assert_equal(r[0], td_q)
# random query
q = DescriptorMemoryElement("query", i + 1)
q.set_vector(numpy.random.rand(dim))
# for any query of size k, results should at least be in distance order
r, dists = index.nn(q, 10)
for j in xrange(1, len(dists)):
ntools.assert_greater(dists[j], dists[j - 1])
r, dists = index.nn(q, i)
for j in xrange(1, len(dists)):
ntools.assert_greater(dists[j], dists[j - 1])
DescriptorMemoryElement.MEMORY_CACHE = {}
示例14: test_clustering_equal_descriptors
def test_clustering_equal_descriptors(self):
# Test that clusters of descriptor of size n-features are correctly
# clustered together.
print("Creating dummy descriptors")
n_features = 8
n_descriptors = 20
index = MemoryDescriptorIndex()
c = 0
for i in range(n_features):
v = numpy.ndarray((8,))
v[...] = 0
v[i] = 1
for j in range(n_descriptors):
d = DescriptorMemoryElement('test', c)
d.set_vector(v)
index.add_descriptor(d)
c += 1
print("Creating test MBKM")
mbkm = MiniBatchKMeans(n_features, batch_size=12, verbose=True,
compute_labels=False, random_state=0)
# Initial fit with half of index
d_classes = mb_kmeans_build_apply(index, mbkm, n_descriptors)
# There should be 20 descriptors per class
for c in d_classes:
self.assertEqual(
len(d_classes[c]),
n_descriptors,
"Cluster %s did not have expected number of descriptors "
"(%d != %d)"
% (c, n_descriptors, len(d_classes[c]))
)
# Each descriptor in each cluster should be equal to the other
# descriptors in that cluster
uuids = list(d_classes[c])
v = index[uuids[0]].vector()
for uuid in uuids[1:]:
v2 = index[uuid].vector()
numpy.testing.assert_array_equal(v, v2,
"vector in cluster %d did not "
"match other vectors "
"(%s != %s)"
% (c, v, v2))
示例15: test_classify_invalid_descriptor_dimensions
def test_classify_invalid_descriptor_dimensions(self):
c = IndexLabelClassifier(self.FILEPATH_TEST_LABELS)
d = DescriptorMemoryElement('test', 0)
# One less
d.set_vector([1, 2, 3, 4, 5])
self.assertRaises(
RuntimeError,
c._classify, d
)
# One more
d.set_vector([1, 2, 3, 4, 5, 6, 7])
self.assertRaises(
RuntimeError,
c._classify, d
)