Advertisement
mpoggi

myNet.py, Movidius NSC

Jan 9th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3.  
  4. USING_SLICE = True
  5.  
  6.  
  7. def leaky_relu(x, alpha=0.2):
  8.     return tf.maximum(x, alpha * x)
  9.  
  10. def conv2d_leaky(input, kernel_shape, bias_shape, strides=1, relu=True, padding='SAME'):
  11.  
  12.     # Conv2D
  13.     weights = tf.get_variable("weights", kernel_shape, initializer=tf.contrib.layers.xavier_initializer())
  14.     biases = tf.get_variable("biases", bias_shape, initializer=tf.truncated_normal_initializer())
  15.     output = tf.nn.conv2d(input, weights, strides=[1, strides, strides, 1], padding=padding)
  16.     output = tf.nn.bias_add(output, biases)
  17.  
  18.     # ReLU (if required)
  19.     if relu == False:
  20.         print ('WARNING: reLU disabled')
  21.     else:
  22.         output = leaky_relu(output)
  23.     return output
  24.  
  25. def decoder(volume):
  26.     with tf.variable_scope("conv1") as scope:
  27.       conv1 = conv2d_leaky(volume, [3, 3, volume.get_shape().as_list()[3], 128], [128], 1, True)
  28.     with tf.variable_scope("conv2") as scope:
  29.       conv2 = conv2d_leaky(conv1, [3, 3, conv1.get_shape().as_list()[3], 128], [128], 1, True)
  30.     with tf.variable_scope("conv3") as scope:
  31.       conv3 = conv2d_leaky(conv2, [3, 3, conv2.get_shape().as_list()[3], 96], [96], 1, True)
  32.     with tf.variable_scope("conv4") as scope:
  33.       conv4 = conv2d_leaky(conv3, [3, 3, conv3.get_shape().as_list()[3], 64], [64], 1, True)
  34.     with tf.variable_scope("conv5") as scope:
  35.       conv5 = conv2d_leaky(conv4, [3, 3, conv4.get_shape().as_list()[3], 32], [32], 1, True)
  36.     with tf.variable_scope("conv6") as scope:
  37.       conv6 = conv2d_leaky(conv5, [3, 3, conv5.get_shape().as_list()[3], 8], [8], 1, False)
  38.     return conv6
  39.  
  40. def encoder(input_batch):
  41.     with tf.variable_scope("conv1"):
  42.       conv1 = conv2d_leaky(input_batch, [3, 3, 3, 16], [16], 2, True)
  43.     with tf.variable_scope("conv2"):
  44.       conv2 = conv2d_leaky(conv1, [3, 3, 16, 16], [16], 1, True)
  45.     with tf.variable_scope("conv3"):
  46.       conv3 = conv2d_leaky(conv2, [3, 3, 16, 32], [32], 2, True)
  47.     with tf.variable_scope("conv4"):
  48.       conv4 = conv2d_leaky(conv3, [3, 3, 32, 32], [32], 1, True)
  49.     with tf.variable_scope("conv5"):
  50.       conv5 = conv2d_leaky(conv4, [3, 3, 32, 64], [64], 2, True)
  51.     with tf.variable_scope("conv6"):
  52.       conv6 = conv2d_leaky(conv5, [3, 3, 64, 64], [64], 1, True)
  53.     with tf.variable_scope("conv7"):
  54.       conv7 = conv2d_leaky(conv6, [3, 3, 64, 96], [96], 2, True)
  55.     with tf.variable_scope("conv8"):
  56.       conv8 = conv2d_leaky(conv7, [3, 3, 96, 96], [96], 1, True)
  57.     with tf.variable_scope("conv9"):
  58.       conv9 = conv2d_leaky(conv8, [3, 3, 96, 128], [128], 2, True)
  59.     with tf.variable_scope("conv10"):
  60.       conv10 = conv2d_leaky(conv9, [3, 3, 128, 128], [128], 1, True)
  61.     with tf.variable_scope("conv11"):
  62.       conv11 = conv2d_leaky(conv10, [3, 3, 128, 192], [192], 2, True)
  63.     with tf.variable_scope("conv12"):
  64.       conv12 = conv2d_leaky(conv11, [3, 3, 192, 192], [192], 1, True)
  65.     return conv12
  66.  
  67. def myNet(image, sliceOp=True):
  68.   with tf.variable_scope("encoder-1") as scope:
  69.     features1 = encoder(image)
  70.   with tf.variable_scope("encoder-2") as scope:
  71.     features2 = encoder(image)
  72.   if sliceOp:
  73.     features = tf.concat([features1, tf.slice(features2, [0, 0, 0, 0], [-1, -1, -1, 1])], -1)
  74.   else:
  75.     features = tf.concat([features1, features2], -1)
  76.   output = decoder(features)
  77.   return output
  78.  
  79. def run(name, height, width):
  80.   with tf.Graph().as_default():
  81.     image = tf.placeholder("float", [1, height, width, 3], name="input")
  82.     output = myNet(image, USING_SLICE)
  83.     with tf.variable_scope("result") as scope:
  84.        output = output + output
  85.  
  86.     init = tf.group(tf.global_variables_initializer(),
  87.                    tf.local_variables_initializer())
  88.  
  89.     saver = tf.train.Saver()
  90.  
  91.     with tf.Session() as sess:
  92.         #loading weights TODO
  93.         sess.run(init)
  94.         saver.save(sess, "output/"+name)
  95.  
  96. run('myNet', 576, 960)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement