Here’s the code:
def get_available_models_names():
availale_models = ['xception',
'resnet50',
'inceptionv3',
'inceptionresnetv2',
'mobilenet',
'densenet',
'nasnet',
'mobilenetv2'
]
return availale_models
def run_model(model_name, input_shape, out_shape, classes):
model_fn, preprocess_fn = get_model_functions(model_name)
# Load pretrained keras model
model = model_fn(include_top=True, weights='imagenet',
input_shape=input_shape, classes=classes)
# Load and preprocess image
img = Image.open(IMAGE_FILE).resize((224, 224))
data = np.array(img)[np.newaxis, :].astype('float32')
data = preprocess_fn(data).transpose([0, 3, 1, 2]) # Need NCHW format for GPU
target = 'llvm'
ctx = tvm.cpu()
# Compile with nnvm
# convert the keras model(NHWC layout) to NNVM format(NCHW layout), then compile
sym, params = nnvm.frontend.from_keras(model)
shape_dict = {'input_1': data.shape}
with nnvm.compiler.build_config(opt_level=2):
graph, lib, params = nnvm.compiler.build(sym, target, shape_dict, params=params)
# Execute with tvm
m = graph_runtime.create(graph, lib, ctx)
# set inputs
m.set_input('input_1', tvm.nd.array(data.astype('float32')))
m.set_input(**params)
# execute
m.run()
# get outputs
tvm_out = m.get_output(0, tvm.nd.empty(out_shape, 'float32')).asnumpy()
top1_tvm = np.argmax(tvm_out)
return top1_tvm
def main():
input_shape = (224, 224, 3)
classes = 1000
out_shape = (classes,)
# Run each model once
results = list()
for model_name in get_available_models_names():
print("Running model:", model_name)
top_1 = run_model(model_name, input_shape, out_shape, classes)
print('\n')
break
if __name__ == "__main__":
main()
Like this, the code will run fine and return a result. If I remove the break
in main then I get the error in my first post. I know that each of the first 4 models in the list work individually, but I get this error when I try to execute each after some other working model.