def test_softmax(self): self.assertCountEqual(common_tool.softmax([1, 2, 3, 4, 5]), self._softmax([1, 2, 3, 4, 5])) self.assertCountEqual(common_tool.softmax([0, 0, 0, 0, 0]), self._softmax([0, 0, 0, 0, 0])) self.assertCountEqual(common_tool.softmax([-1, -2, -3, -4, -5]), self._softmax([-1, -2, -3, -4, -5]))
def test_softmax_error(self): self.assertRaises(ValueError, lambda: common_tool.softmax([])) self.assertRaises(ValueError, lambda: common_tool.softmax(())) self.assertRaises(TypeError, lambda: common_tool.softmax({})) self.assertRaises(TypeError, lambda: common_tool.softmax(None)) self.assertRaises(TypeError, lambda: common_tool.softmax(""))
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])
# 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]