def test_one_hot_decoding(self): self.assertEqual(common_tool.onehot_decoding([1, 0, 0, 0, 0]), 0) self.assertEqual(common_tool.onehot_decoding([0, 1, 0, 0, 0]), 1) self.assertEqual(common_tool.onehot_decoding([0, 0, 0, 0, 1]), 4) self.assertEqual(common_tool.onehot_decoding([0, 0, 1, 0, 1]), 2) self.assertEqual(common_tool.onehot_decoding([0, 1, 0, 1, 1]), 1)
def infer(model, infer_data, ckpt_file): tf.reset_default_graph() with tf.Session(graph=model.graph) as sess_predict: model.saver.restore(sess_predict, ckpt_file) output = sess_predict.run([model.y], feed_dict={model.x: infer_data}) classification = common.softmax(output) y = common.onehot_decoding(classification) return y
def test_infer_c(self): dll = conv_infer_c.init(self.so_lib_path, self.input_info, self.output_info) res = [] for i in prepare_infer_dataset(): output = numpy.zeros(dtype=numpy.float32, shape=(1, 10)) conv_infer_c.infer_c(dll, numpy.expand_dims(i, 0).astype(numpy.float32), output) classification = common_tool.softmax(output) y = common_tool.onehot_decoding(classification) res.append(y[0]) self.assertCountEqual(res, [7, 5, 1, 0, 4, 1, 4, 9, 2, 9])
def test_one_hot_decoding_error(self): self.assertRaises(ValueError, lambda: common_tool.onehot_decoding([])) self.assertRaises(ValueError, lambda: common_tool.onehot_decoding(()))
import os from tensorflow import keras import samples.utils.common_tool as common if __name__ == '__main__': # prepare the infer date 28px * 28px image (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() x_test = x_test.reshape(10000, 784) / 255. image_data = x_test[:10, ...] h5_model = os.path.join('model', 'tensorflow_mlp.h5') model = keras.models.load_model(h5_model) results = model.predict(image_data) # output the result print('Predict Index ', common.onehot_decoding(results))
# prepare the infer date 28px * 28px image (x_train, y_train), (x_test, y_test) = mnist.load_data() x_test = x_test.reshape(10000, 784) / 255. image_data = x_test[:10, ...] # model path so_lib_path = path.join(path.dirname(__file__), 'out_c', 'qumico.so') # Load input_info = np.ctypeslib.ndpointer(dtype=np.float32, ndim=2, shape=(1, 784), flags='CONTIGUOUS') output_info = np.ctypeslib.ndpointer(dtype=np.float32, ndim=2, shape=(1, 10), flags='CONTIGUOUS') dll = init(so_lib_path, input_info, output_info) # infer res = [] for i in image_data: output = np.zeros(dtype=np.float32, shape=(1, 10)) infer_c(dll, np.expand_dims(i, 0).astype(np.float32), output) classification = common.softmax(output) y = common.onehot_decoding(classification) res.append(y[0]) print('Predict Index', res) # [7, 2, 1, 0, 4, 1, 4, 9, 5, 9]