In [1]:
import tensorflow as tf
from keras import backend as K


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-5d026006ffce> in <module>()
----> 1 import tensorflow as tf
      2 from keras import backend as K

ModuleNotFoundError: No module named 'tensorflow'

In [9]:
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
with tf.Session() as sess:
    # add this line to use TensorBoard.
    writer = tf.summary.FileWriter('./graphs', sess.graph)
    print (sess.run(x))
    writer.close()


5

In [14]:
a = tf.constant(2, name="c")
b = tf.constant(3, name="d")
x = tf.add(a, b, name="add")
writer = tf.summary.FileWriter("./graphs", sess.graph)
with tf.Session() as sess:
    print (sess.run(x)) # >> 5


5

In [8]:
a = tf.constant([1, 2,3], name="a")
b = tf.constant([[0, 1, 2], [3, 4,5]], name="b")
x = tf.add(a, b, name="add1")
y = tf.add(b, a, name="add2")
z = tf.multiply(a, b, name="mul1")
w = tf.multiply(b, a, name="mul2")
with tf.Session() as sess:
    x, y ,z,w = sess.run([x, y, z, w])
    print("a+b:", x)
    print("b+a:", y)
    print("a*b:", z)
    print("b*a:", w)


a+b: [[1 3 5]
 [4 6 8]]
b+a: [[1 3 5]
 [4 6 8]]
a*b: [[ 0  2  6]
 [ 3  8 15]]
b*a: [[ 0  2  6]
 [ 3  8 15]]

In [24]:
def repeat(x, num_repeats):
    ones = tf.ones((1, num_repeats), dtype='int32')
    x = tf.reshape(x, shape=(-1,1))
    x = tf.matmul(x, ones)
    return tf.reshape(x, [-1])

a = tf.range(32, name="a")
b = tf.constant(3600, name="b")
y = repeat(a*b, 30*30)


with tf.Session() as sess:
    x = sess.run(y)
    print(x)
    print(x.shape)
# 0,..(900 times)..,0,3600*1,..(900 times)..,3600*1,3600*2,....,3600*2, ......., 3600*31


[     0      0      0 ..., 111600 111600 111600]
(28800,)

In [15]:
# gather 蒐集某 index 以下所有的值,並保留維度
a = tf.constant(2, name="a")
b = tf.constant([[1,2,3],[4,5,6],[7,8,4],[3,2,3],[1,1,23]], name="b")
c = tf.constant([1,2], name="c")
y = tf.gather(b, a)
z = tf.gather(b, c)
with tf.Session() as sess:
    x,w,f = sess.run([y,z,k])
    print(x)
    print(z)
    print(z.eval())


[7 8 4]
Tensor("Gather_14:0", shape=(2, 3), dtype=int32)
[[4 5 6]
 [7 8 4]]

In [16]:
a = tf.constant([1,2,3,4,5,6,7,8,9,10], name="a")
b = tf.constant([1,3,3,3,5,3,7,3,9,3], name="b")
c = tf.constant([1,2,3,4,5,6,7,8,9,10], name="c")
d = tf.constant([2,2,3,4,1,6,7,1,9,2], name="d")
area_a = tf.expand_dims((b-a)*(d-c), 1)
area_b = tf.expand_dims((b-a)*(c-d), 1)
y = tf.add_n([area_a, area_b])
with tf.Session() as sess:
    x = sess.run(y)
    print(x)
    print(area_a)
    print(area_b)


[[0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]]
Tensor("ExpandDims:0", shape=(10, 1), dtype=int32)
Tensor("ExpandDims_1:0", shape=(10, 1), dtype=int32)

In [6]:
def meshgrid(height, width):
    x_linspace = tf.linspace(-1., 1., width)
    y_linspace = tf.linspace(-1., 1., height)
    x_coordinates, y_coordinates = tf.meshgrid(x_linspace, y_linspace)
    x_coordinates = tf.reshape(x_coordinates, [-1])
    y_coordinates = tf.reshape(y_coordinates, [-1])
    ones = tf.ones_like(x_coordinates)
    indices_grid = tf.concat([x_coordinates, y_coordinates, ones], 0)
    return indices_grid

aff = tf.constant([[1,2,3,4,5,6],[-.1,-.2,-.3,-.4,-.5,-.6]])
aff = tf.reshape(aff, shape=(2,2,3))
aff = tf.reshape(aff, (-1, 2, 3))
aff = tf.cast(aff, 'float32')
a = tf.constant(5, name="a")
b = meshgrid(a,a)
c = tf.expand_dims(b, 0)
d = tf.reshape(c, [-1])
e = tf.tile(d, tf.stack([2]))
f = tf.reshape(e, (2, 3, -1))
res = tf.matmul(aff, f)
x_s = tf.slice(res, [0, 0, 0], [-1, 1, -1])
y_s = tf.slice(res, [0, 1, 0], [-1, 1, -1])
with tf.Session() as sess:
    b,c,d,e,f,aff,res,x_s,y_s = sess.run([b,c,d,e,f,aff, res,x_s,y_s])
    print('b',b.shape)
    print(b)
#     print('c',c.shape)
#     print(c)
#     print('d',d.shape)
#     print(d)
#     print('e',e.shape)
#     print(e)
    print('meshgrid with batch',f.shape)
    print(f)
    print('aff',aff.shape)
    print(aff)
    print('new coor',res.shape)
    print(res)
    print('new x',x_s.shape)
    print(x_s)
    print('new y',y_s.shape)
    print(y_s)


meshgrid with batch (2, 3, 25)
[[[-1.  -0.5  0.   0.5  1.  -1.  -0.5  0.   0.5  1.  -1.  -0.5  0.   0.5
    1.  -1.  -0.5  0.   0.5  1.  -1.  -0.5  0.   0.5  1. ]
  [-1.  -1.  -1.  -1.  -1.  -0.5 -0.5 -0.5 -0.5 -0.5  0.   0.   0.   0.   0.
    0.5  0.5  0.5  0.5  0.5  1.   1.   1.   1.   1. ]
  [ 1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.
    1.   1.   1.   1.   1.   1.   1.   1.   1.   1. ]]

 [[-1.  -0.5  0.   0.5  1.  -1.  -0.5  0.   0.5  1.  -1.  -0.5  0.   0.5
    1.  -1.  -0.5  0.   0.5  1.  -1.  -0.5  0.   0.5  1. ]
  [-1.  -1.  -1.  -1.  -1.  -0.5 -0.5 -0.5 -0.5 -0.5  0.   0.   0.   0.   0.
    0.5  0.5  0.5  0.5  0.5  1.   1.   1.   1.   1. ]
  [ 1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.
    1.   1.   1.   1.   1.   1.   1.   1.   1.   1. ]]]
aff (2, 2, 3)
[[[ 1.          2.          3.        ]
  [ 4.          5.          6.        ]]

 [[-0.1        -0.2        -0.30000001]
  [-0.40000001 -0.5        -0.60000002]]]
new coor (2, 2, 25)
[[[  0.           0.5          1.           1.5          2.           1.
     1.5          2.           2.5          3.           2.           2.5
     3.           3.5          4.           3.           3.5          4.
     4.5          5.           4.           4.5          5.           5.5
     6.        ]
  [ -3.          -1.           1.           3.           5.          -0.5
     1.5          3.5          5.5          7.5          2.           4.
     6.           8.          10.           4.5          6.5          8.5
    10.5         12.5          7.           9.          11.          13.
    15.        ]]

 [[  0.          -0.05000001  -0.10000001  -0.15000001  -0.20000002
    -0.10000001  -0.15000001  -0.20000002  -0.25        -0.30000001
    -0.20000002  -0.25        -0.30000001  -0.35000002  -0.40000001
    -0.30000001  -0.35000002  -0.40000001  -0.45000002  -0.5         -0.40000001
    -0.45000002  -0.5         -0.55000001  -0.60000002]
  [  0.29999995   0.09999996  -0.10000002  -0.30000001  -0.5          0.04999995
    -0.15000004  -0.35000002  -0.55000001  -0.75        -0.20000002
    -0.40000004  -0.60000002  -0.80000001  -1.          -0.45000002
    -0.65000004  -0.85000002  -1.04999995  -1.25        -0.70000005
    -0.90000004  -1.10000002  -1.29999995  -1.5       ]]]
new x (2, 1, 25)
[[[ 0.          0.5         1.          1.5         2.          1.          1.5
    2.          2.5         3.          2.          2.5         3.          3.5
    4.          3.          3.5         4.          4.5         5.          4.
    4.5         5.          5.5         6.        ]]

 [[ 0.         -0.05000001 -0.10000001 -0.15000001 -0.20000002 -0.10000001
   -0.15000001 -0.20000002 -0.25       -0.30000001 -0.20000002 -0.25
   -0.30000001 -0.35000002 -0.40000001 -0.30000001 -0.35000002 -0.40000001
   -0.45000002 -0.5        -0.40000001 -0.45000002 -0.5        -0.55000001
   -0.60000002]]]
new y (2, 1, 25)
[[[ -3.          -1.           1.           3.           5.          -0.5
     1.5          3.5          5.5          7.5          2.           4.
     6.           8.          10.           4.5          6.5          8.5
    10.5         12.5          7.           9.          11.          13.
    15.        ]]

 [[  0.29999995   0.09999996  -0.10000002  -0.30000001  -0.5          0.04999995
    -0.15000004  -0.35000002  -0.55000001  -0.75        -0.20000002
    -0.40000004  -0.60000002  -0.80000001  -1.          -0.45000002
    -0.65000004  -0.85000002  -1.04999995  -1.25        -0.70000005
    -0.90000004  -1.10000002  -1.29999995  -1.5       ]]]

In [8]:
def meshgrid(height, width):
    x_linspace = tf.linspace(-1., 1., width)
    y_linspace = tf.linspace(-1., 1., height)
    x_coordinates, y_coordinates = tf.meshgrid(x_linspace, y_linspace)
    x_coordinates = tf.reshape(x_coordinates, [-1])
    y_coordinates = tf.reshape(y_coordinates, [-1])
#     ones = tf.ones_like(x_coordinates)
    indices_grid = tf.concat([x_coordinates, y_coordinates], 0)
    return indices_grid

aff_ori = tf.constant([[[1,2,3,4,5,6,7,8,9],[-.1,-.2,-.3,-.4,-.5,-.6,-.7,-.8,-.9]],
                  [[10,10,30,40,50,60,70,80,90],[-.01,-.02,-.03,-.04,-.05,-.06,-.07,-.08,-.09]]])
aff = tf.reshape(aff, shape=(2,2,-1))
aff = tf.cast(aff, 'float32')
a = tf.constant(3, name="a")
b = meshgrid(a,a)
c = tf.tile(b, tf.stack([2]))
d = tf.reshape(c, (2, 2, -1))
res = tf.add(aff, d)
x_s = tf.slice(res, [0, 0, 0], [-1, 1, -1])
y_s = tf.slice(res, [0, 1, 0], [-1, 1, -1])
with tf.Session() as sess:
    aff_ori,b,c,d,aff,res,x_s,y_s = sess.run([aff_ori,b,c,d,aff,res,x_s,y_s])
#     print(b.shape)
#     print(b)
#     print(c.shape)
#     print(c)
    print('flow_ori',aff_ori.shape)
    print('meshgrid with batch',d.shape)
    print(d)
    print('flow',aff.shape)
    print(aff)
    print('new coor',res.shape)
    print(res)
    print('new x',x_s.shape)
    print(x_s)
    print('new y',y_s.shape)
    print(y_s)


flow_ori (2, 2, 9)
meshgrid with batch (2, 2, 9)
[[[-1.  0.  1. -1.  0.  1. -1.  0.  1.]
  [-1. -1. -1.  0.  0.  0.  1.  1.  1.]]

 [[-1.  0.  1. -1.  0.  1. -1.  0.  1.]
  [-1. -1. -1.  0.  0.  0.  1.  1.  1.]]]
flow (2, 2, 9)
[[[  1.00000000e+00   2.00000000e+00   3.00000000e+00   4.00000000e+00
     5.00000000e+00   6.00000000e+00   7.00000000e+00   8.00000000e+00
     9.00000000e+00]
  [ -1.00000001e-01  -2.00000003e-01  -3.00000012e-01  -4.00000006e-01
    -5.00000000e-01  -6.00000024e-01  -6.99999988e-01  -8.00000012e-01
    -8.99999976e-01]]

 [[  1.00000000e+01   1.00000000e+01   3.00000000e+01   4.00000000e+01
     5.00000000e+01   6.00000000e+01   7.00000000e+01   8.00000000e+01
     9.00000000e+01]
  [ -9.99999978e-03  -1.99999996e-02  -2.99999993e-02  -3.99999991e-02
    -5.00000007e-02  -5.99999987e-02  -7.00000003e-02  -7.99999982e-02
    -9.00000036e-02]]]
new coor (2, 2, 9)
[[[  0.00000000e+00   2.00000000e+00   4.00000000e+00   3.00000000e+00
     5.00000000e+00   7.00000000e+00   6.00000000e+00   8.00000000e+00
     1.00000000e+01]
  [ -1.10000002e+00  -1.20000005e+00  -1.29999995e+00  -4.00000006e-01
    -5.00000000e-01  -6.00000024e-01   3.00000012e-01   1.99999988e-01
     1.00000024e-01]]

 [[  9.00000000e+00   1.00000000e+01   3.10000000e+01   3.90000000e+01
     5.00000000e+01   6.10000000e+01   6.90000000e+01   8.00000000e+01
     9.10000000e+01]
  [ -1.00999999e+00  -1.01999998e+00  -1.02999997e+00  -3.99999991e-02
    -5.00000007e-02  -5.99999987e-02   9.30000007e-01   9.20000017e-01
     9.09999967e-01]]]
new x (2, 1, 9)
[[[  0.   2.   4.   3.   5.   7.   6.   8.  10.]]

 [[  9.  10.  31.  39.  50.  61.  69.  80.  91.]]]
new y (2, 1, 9)
[[[-1.10000002 -1.20000005 -1.29999995 -0.40000001 -0.5        -0.60000002
    0.30000001  0.19999999  0.10000002]]

 [[-1.00999999 -1.01999998 -1.02999997 -0.04       -0.05       -0.06
    0.93000001  0.92000002  0.90999997]]]

In [8]:
def mean_squared_error(y_true, y_pred):
#     return K.mean(K.square(y_pred - y_true), axis=-1)
    return K.mean(K.sqrt(K.square(y_pred - y_true)), axis=-1)
    

a = tf.constant([[[[1.,2.],[4.,5.],[7.,8.]],
                [[1.,3.],[4.,6.],[8.,9.]],
                [[1.,3.],[4.,6.],[7.,8.]]],
                [[[1.,3.],[4.,5.],[7.,9.]],
                [[1.,2.],[4.,5.],[7.,9.]],
                [[1.,3.],[4.,5.],[7.,9.]]],
                [[[2.,3.],[4.,6.],[7.,9.]],
                [[2.,3.],[4.,6.],[7.,9.]],
                [[1.,3.],[4.,6.],[7.,9.]]]], name="a")
b = tf.constant([[[[2.,3.],[5.,7.],[8.,10.]],
                [[2.,4.],[6.,7.],[8.,10.]],
                [[3.,4.],[5.,6.],[9.,10.]]],
                [[[1.,3.],[5.,7.],[8.,10.]],
                [[3.,4.],[5.,7.],[9.,10.]],
                [[2.,3.],[5.,6.],[8.,10.]]],
                [[[2.,3.],[6.,7.],[8.,9.]],
                [[3.,4.],[6.,7.],[8.,9.]],
                [[2.,4.],[6.,7.],[8.,9.]]]], name="b")
a = tf.transpose(a,[0,3,2,1])
b = tf.transpose(b,[0,3,2,1])
x = mean_squared_error(a,b)

with tf.Session() as sess:
    a,b,x = sess.run([a,b,x])
print(x)
a.shape


[[[ 1.33333325  1.33333325  0.99999994]
  [ 0.99999994  0.99999994  1.66666651]]

 [[ 0.99999994  0.99999994  1.33333325]
  [ 0.66666663  1.66666651  0.99999994]]

 [[ 0.66666663  1.99999988  0.99999994]
  [ 0.66666663  0.99999994  0.        ]]]
Out[8]:
(3, 2, 3, 3)