- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm currently evaluating the Movidius Stick using some trivial mnist examples.
My setup works for regular TensorFlow models, but I get an MYRIAD_ERROR when I use a compiled Keras model. I use the docker/keras-full to train the model and create an inference.
How to reproduce:
#Imports
#################
from keras.models import Model
from keras.layers import Activation
from keras.layers.convolutional import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense, Flatten, Dropout, Input
from keras.optimizers import SGD
from keras.layers.normalization import BatchNormalization
from keras.initializers import RandomNormal
from keras import backend as K
from keras.datasets import mnist
import keras
from keras.preprocessing.text import one_hot
from keras.utils import to_categorical
import numpy as np
# Constants
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
inp = Input(shape=input_shape, name="model_input")
conv1 = Conv2D(4,(3, 3),activation='relu',name = "Conv1")(inp)
conv2 = Conv2D(8, (3, 3), activation='relu',name = "Conv2")(conv1)
conv3 = Conv2D(8, (3, 3),strides=2, activation='relu',name = "Conv3")(conv2)
conv4 = Conv2D(16, (3, 3),strides=2, activation='relu',name = "Conv4")(conv3)
drop1 = Dropout(0.25, name = "Dropout")(conv4)
conv5 = Conv2D(10, (5, 5), strides=5, activation='relu', name = "Conv5")(drop1)
flat = Flatten(name = "Flatten")(conv5)
softmax = Dense(num_classes, activation='softmax', name = "Softmax")(flat)
model = Model(inputs=inp, outputs=softmax)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
inp = Input(shape=input_shape, name="model_input")
conv1 = Conv2D(4,(3, 3),activation='relu',name = "Conv1")(inp)
conv2 = Conv2D(8, (3, 3), activation='relu',name = "Conv2")(conv1)
conv3 = Conv2D(8, (3, 3),strides=2, activation='relu',name = "Conv3")(conv2)
conv4 = Conv2D(16, (3, 3),strides=2, activation='relu',name = "Conv4")(conv3)
conv5 = Conv2D(10, (5, 5), strides=5, activation='relu', name = "Conv5")(conv4)
flat = Flatten(name = "Flatten")(conv5)
softmax = Dense(num_classes, activation='softmax', name = "Softmax")(flat)
infmodel = Model(inputs=inp, outputs=softmax)
infmodel.compile(loss=model.loss, optimizer=model.optimizer, metrics=model.metrics)
infmodel.set_weights(model.get_weights())
MODEL_LOC = "host/mnist_model.json"
WEIGHT_LOC = "host/mnist_weights.h5"
#infmodel.save("host/mnist_model.h5")
with open(MODEL_LOC,"w") as f:
f.write(infmodel.to_json())
infmodel.save_weights(WEIGHT_LOC)
print("Model and weights saved to ", MODEL_LOC,WEIGHT_LOC)
You must change in the JSON the batch_input_shape
to [1, 28, 28, 1]
so the compiler doesn't get an uncaught error InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'model_input' with dtype float and shape [?,28,28,1]
[[Node: model_input = Placeholder[dtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
In a new file/jupyter notebook to have a clean session:
import tensorflow as tf
from keras import backend as K
from keras.models import Sequential
import keras
from tensorflow.python.tools import optimize_for_inference_lib as opti
import numpy as np
img_rows = 28
img_cols = 28
num_classes = 10
# Disclaimer: We do this in a seperate file with a seperate kernel to ensure
# nothing carries over from the other session.
# Please make sure you changed the input dimensions in the json from -1,28,28,1 to 1,28,28,1
MODEL_LOC = "host/mnist_model.json"
WEIGHT_LOC = "host/mnist_weights.h5"
def loadKerasModel(model_path,weight_path):
# Load Trained Model
with open(model_path,"r") as f:
model_json = f.read()
model = keras.models.model_from_json(model_json)
model.load_weights(weight_path)
print(model.summary())
return model
model = loadKerasModel(MODEL_LOC,WEIGHT_LOC)
# Convert the Keras to a Tensorflow model
saver = tf.train.Saver()
with K.get_session() as sess:
K.set_learning_phase(0)
saver.save(sess,"host/mnist_keras")
This allows me to compile the model using mvNCCompile mnist_keras.meta -in "model_input" -on "Softmax/Softmax" -o mnist_keras.graph
Running the model on the Movidius stick
#! /usr/bin/env python3
from mvnc import mvncapi as mvnc
import sys
import numpy
import cv2
INPUT_IMAGE = "4.png"
GRAPH_FILE = "./mnist_keras.graph"
# Check for device existance and then open it. If no device, abort
devices = mvnc.EnumerateDevices()
if len(devices) == 0:
print('No devices found')
quit()
device = mvnc.Device(devices[0])
device.OpenDevice()
# Load the Graph we created before
with open(GRAPH_FILE, 'rb') as f:
unallocatedGraph = f.read()
categories = ['0','1','2','3','4','5','6','7','8','9']
img = cv2.imread(INPUT_IMAGE)
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img.astype(numpy.float32)
print("Console Print of the Image")
print("=============================")
for i,x in enumerate(img):
for j,y in enumerate(x):
if y > 125:
print("#",end="")
img[i][j]= (-0.5)
else:
print("_",end="")
img[i][j]=0.5
print("")
print("=============================")
# Allocate the graph
graph = device.AllocateGraph(unallocatedGraph)
print("Graph Allocated...")
# Run it
graph.LoadTensor(img.astype(numpy.float32), None)
print("Network Executed...")
# Get the softmax output
output, userobj = graph.GetResult()
print("Result Fetched...")
maxidx = numpy.argmax(output)
for i in range(len(output)):
if i == maxidx:
print(">>",end="")
else:
print(" ",end="")
print(categories[i], output[i])
Output is
Graph Allocated...
Network Executed...
/usr/lib/python3/dist-packages/apport/report.py:13: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import fnmatch, glob, traceback, errno, sys, atexit, locale, imp
Traceback (most recent call last):
File "run.py", line 52, in <module>
output, userobj = graph.GetResult()
File "/usr/local/lib/python3.6/dist-packages/mvnc/mvncapi.py", line 264, in GetResult
raise Exception(Status(status))
Exception: mvncStatus.MYRIAD_ERROR
Anybody any ideas why I get a MYRIAD_ERROR?
- Tags:
- Keras
Link Copied
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page