pcb defect detetcion application
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / box_utils / tf_ops.py
1 # -*- coding:utf-8 -*-
2
3 from __future__ import absolute_import, print_function, division
4
5 import tensorflow as tf
6
7 '''
8 all of these ops are derived from tenosrflow Object Detection API
9 '''
10 def indices_to_dense_vector(indices,
11                             size,
12                             indices_value=1.,
13                             default_value=0,
14                             dtype=tf.float32):
15   """Creates dense vector with indices set to specific (the para "indices_value" ) and rest to zeros.
16
17   This function exists because it is unclear if it is safe to use
18     tf.sparse_to_dense(indices, [size], 1, validate_indices=False)
19   with indices which are not ordered.
20   This function accepts a dynamic size (e.g. tf.shape(tensor)[0])
21
22   Args:
23     indices: 1d Tensor with integer indices which are to be set to
24         indices_values.
25     size: scalar with size (integer) of output Tensor.
26     indices_value: values of elements specified by indices in the output vector
27     default_value: values of other elements in the output vector.
28     dtype: data type.
29
30   Returns:
31     dense 1D Tensor of shape [size] with indices set to indices_values and the
32         rest set to default_value.
33   """
34   size = tf.to_int32(size)
35   zeros = tf.ones([size], dtype=dtype) * default_value
36   values = tf.ones_like(indices, dtype=dtype) * indices_value
37
38   return tf.dynamic_stitch([tf.range(size), tf.to_int32(indices)],
39                            [zeros, values])
40
41
42
43
44 def test_plt():
45   from PIL import Image
46   import matplotlib.pyplot as plt
47   import numpy as np
48
49   a = np.random.rand(20, 30)
50   print (a.shape)
51   # plt.subplot()
52   b = plt.imshow(a)
53   plt.show()
54
55
56 if __name__ == '__main__':
57   test_plt()