- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I've implemented a custom network (LeNet-5) in TensorFlow, trained it and then saved the graph (https://github.com/vincenzosantopietro/LeNet-5-Tensorflow).
After that, I've created a new version of the network for inference purposes and re-saved the graph.
import tensorflow as tf
from tensorflow.contrib.layers import flatten
import sys
num_outputs = 10
def lenet(x):
# Layer 1: Input 32x32x1, Output 28x28x6
conv1_kernels = tf.Variable(tf.truncated_normal(shape=[5, 5, 1, 6], mean=0, stddev=1))
conv1_biases = tf.get_variable(name="conv1_biases", shape=[6],
initializer=tf.random_normal_initializer(stddev=0.3))
with tf.name_scope("ConvPoolReLU1"):
conv1 = tf.nn.conv2d(x,conv1_kernels, [1, 1, 1, 1], padding='VALID') + conv1_biases
# Pooling -> from 28x28 to 14x14
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
# Activation
conv1 = tf.nn.relu(pool1)
# Layer 2: Input 14x14x6, Output 10x10x16
conv2_kernels = tf.Variable(tf.truncated_normal(shape=[5, 5, 6, 16], mean=0, stddev=1))
conv2_biases = tf.get_variable(name="conv2_biases", shape=[16],
initializer=tf.random_normal_initializer(stddev=1))
with tf.name_scope("ConvPoolReLU2"):
conv2 = tf.nn.conv2d(conv1, conv2_kernels, [1, 1, 1, 1], padding='VALID') + conv2_biases
# Pooling -> from 10x10x16 to 5x5x16
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
# Activation 2
conv2 = tf.nn.relu(pool2)
# Flatten -> from 5x5x16 to 400x1
flattened = flatten(conv2)
with tf.name_scope("FullyConnectedLayer1"):
# Fully Connected Layer n.1
fcl1_weights = tf.Variable(tf.truncated_normal(shape=[400, 120], mean=0, stddev=1))
fcl1_biases = tf.get_variable(name="fc1_biases", shape=[120],
initializer=tf.random_normal_initializer(stddev=1))
fcl1 = tf.matmul(flattened, fcl1_weights) + fcl1_biases
# Activation 3
fcl1 = tf.nn.relu(fcl1)
with tf.name_scope("FullyConnectedLayer2"):
# Fully Connected Layer n.2
fcl2_weights = tf.Variable(tf.truncated_normal(shape=[120, 84], mean=0, stddev=1))
fcl2_biases = tf.get_variable(name="fc2_biases", shape=[84],
initializer=tf.random_normal_initializer(stddev=1))
fcl2 = tf.matmul(fcl1, fcl2_weights) + fcl2_biases
# Activation 4
fcl2 = tf.nn.relu(fcl2)
with tf.name_scope("FullyConnectedLayer3"):
# Fully Connected Layer n.3
fcl3_weights = tf.Variable(tf.truncated_normal(shape=[84, 10], mean=0, stddev=1))
fcl3_biases = tf.get_variable(name="fc3_biases", shape=[10],
initializer=tf.random_normal_initializer(stddev=1))
logits = tf.matmul(fcl2, fcl3_weights) + fcl3_biases
return logits
def main(_):
print('Saving model for inference on Intel\'s Neural Compute Stick')
# Create the model
x = tf.placeholder(tf.float32, [1,32, 32, 1], name='input')
logits = lenet(x)
output = tf.nn.softmax(logits,name='softmax')
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
saver.restore(sess, 'tmp/model.ckpt')
saver.save(sess, 'tmp/model-inference')
if __name__ == '__main__':
tf.app.run(main=main, argv=[sys.argv[0]])
Then I used the mvNCCompile command to export the .graph file needed by the NCS: ``mvNCCompile model-inference.meta -s 12 -in=input -on=softmax -is 32 32 -o lenet5.graph
I've also implemented a new script to load the model on the NCS and make some inference on the MNIST dataset (there's padding around images to make the input size fit) but when I run it, the NCS keeps reporting MYRIAD_ERROR.
from mvnc import mvncapi as mvnc
import sys
import numpy as np
import cv2
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets("MNIST_data/", reshape=False, one_hot=True)
X_test, y_test = mnist.test.images, mnist.test.labels
X_test = np.pad(X_test, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
# Enumerating devices
mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
devices = mvnc.EnumerateDevices()
if len(devices) == 0:
print('No devices found')
quit()
#Opening NCS device
device = mvnc.Device(devices[0])
device.OpenDevice()
with open('lenet5.graph',mode='rb') as f:
graphfile = f.read()
print('lenet-5 for MNIST_data - Input Size: 32x32x1')
graph = device.AllocateGraph(graphfile)
for i in range(len(X_test)/10):
cv2.imshow("img",X_test[i])
if cv2.waitKey(200) > 0:
graph.DeallocateGraph()
device.CloseDevice()
exit()
x = np.reshape(X_test[i].astype(np.float32),(1,32,32,1))
if(graph.LoadTensor(x,None)):
print('Loaded Tensor')
output, _ = graph.GetResult()
#print('output:' + output)
graph.DeallocateGraph()
device.CloseDevice()
print('Finished')
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Device 0 Address: 7 - VID/PID 03e7:2150
Starting wait for connect with 2000ms timeout
Found Address: 7 - VID/PID 03e7:2150
Found EP 0x81 : max packet size is 512 bytes
Found EP 0x01 : max packet size is 512 bytes
Found and opened device
Performing bulk write of 865724 bytes…
Successfully sent 865724 bytes of data in 227.821180 ms (3.623977 MB/s)
Boot successful, device address 7
Device 0 Address: 1 - VID/PID 03e7:f63b
Found Address: 1 - VID/PID 03e7:f63b
done
Booted 1 -> VSC
lenet-5 for MNIST_data - Input Size: 32x32x1
Loaded Tensor
Traceback (most recent call last):
File "inference_script.py", line 43, in
output, _ = graph.GetResult()
File "/usr/local/lib/python2.7/dist-packages/mvnc/mvncapi.py", line 264, in GetResult
raise Exception(Status(status))
Exception: mvncStatus.MYRIAD_ERROR
I'm using the NCS on my MacBook Pro on Ubuntu 16.04.
Is this error related to the inference model or the script I made to load the model on the NCS?
- Tags:
- Tensorflow
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@vincenzosantopietro Can you provide a link to your meta files? I'd like to try to help you debug this issue by reproducing the issue myself on my bench. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@vincenzosantopietro Hi Vincent. I was not able to reproduce your issue "myriad error", but I did run into a different error "[Error 25] Myriad Error: "5x5 Convolution outputChannels Dimension too small."". I was using your myriad-inference.meta model files. By default, the SDK chooses an optimized function that requires the channels to be >= 8.
I found a workaround with this issue by including a conf file named "model-inference.conf" with the following content: (Make sure there is an extra line after the last line in the conf file or else it will not parse correctly.)
ConvPoolReLU1/Conv2D
generic_spatial
ConvPoolReLU2/Conv2D
generic_spatial
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page