def testInputParserErrorWrongName(self): x0 = np.array([[1], [2]]) x1 = np.array(range(5)) input_path = os.path.join(test.get_temp_dir(), 'input.npz') np.savez(input_path, a=x0, b=x1) input_str = 'x=' + input_path + '[c]' with self.assertRaises(RuntimeError): saved_model_cli.load_inputs_from_input_arg_string(input_str, '', '')
def testInputParserNPZ(self): x0 = np.array([[1], [2]]) input_path = os.path.join(test.get_temp_dir(), 'input.npz') np.savez(input_path, a=x0) input_str = 'x=' + input_path + '[a],y=' + input_path feed_dict = saved_model_cli.load_inputs_from_input_arg_string(input_str) self.assertTrue(np.all(feed_dict['x'] == x0)) self.assertTrue(np.all(feed_dict['y'] == x0))
def testInputParserBothDuplicate(self): x0 = np.array([[1], [2]]) input_path = os.path.join(test.get_temp_dir(), 'input.npz') np.savez(input_path, a=x0) x1 = np.ones([2, 10]) input_str = 'x0=' + input_path + '[a]' input_expr_str = 'x0=np.ones([2,10])' feed_dict = saved_model_cli.load_inputs_from_input_arg_string( input_str, input_expr_str, '') self.assertTrue(np.all(feed_dict['x0'] == x1))
def testInputParserQuoteAndWhitespace(self): x0 = np.array([[1], [2]]) x1 = np.array(range(6)).reshape(2, 3) input0_path = os.path.join(test.get_temp_dir(), 'input0.npy') input1_path = os.path.join(test.get_temp_dir(), 'input1.npy') np.save(input0_path, x0) np.save(input1_path, x1) input_str = '"x0=' + input0_path + '[x0] , x1 = ' + input1_path + '"' feed_dict = saved_model_cli.load_inputs_from_input_arg_string(input_str) self.assertTrue(np.all(feed_dict['x0'] == x0)) self.assertTrue(np.all(feed_dict['x1'] == x1))
def testInputParserPythonExpression(self): x1 = np.ones([2, 10]) x2 = np.array([[1], [2], [3]]) x3 = np.mgrid[0:5, 0:5] x4 = [[3], [4]] input_expr_str = ('x1=np.ones([2,10]);x2=np.array([[1],[2],[3]]);' 'x3=np.mgrid[0:5,0:5];x4=[[3],[4]]') feed_dict = saved_model_cli.load_inputs_from_input_arg_string( '', input_expr_str, '') self.assertTrue(np.all(feed_dict['x1'] == x1)) self.assertTrue(np.all(feed_dict['x2'] == x2)) self.assertTrue(np.all(feed_dict['x3'] == x3)) self.assertTrue(np.all(feed_dict['x4'] == x4))
def testInputParserPickle(self): pkl0 = {'a': 5, 'b': np.array(range(4))} pkl1 = np.array([1]) pkl2 = np.array([[1], [3]]) input_path0 = os.path.join(test.get_temp_dir(), 'pickle0.pkl') input_path1 = os.path.join(test.get_temp_dir(), 'pickle1.pkl') input_path2 = os.path.join(test.get_temp_dir(), 'pickle2.pkl') with open(input_path0, 'wb') as f: pickle.dump(pkl0, f) with open(input_path1, 'wb') as f: pickle.dump(pkl1, f) with open(input_path2, 'wb') as f: pickle.dump(pkl2, f) input_str = 'x=' + input_path0 + '[b],y=' + input_path1 + '[c],' input_str += 'z=' + input_path2 feed_dict = saved_model_cli.load_inputs_from_input_arg_string(input_str) self.assertTrue(np.all(feed_dict['x'] == pkl0['b'])) self.assertTrue(np.all(feed_dict['y'] == pkl1)) self.assertTrue(np.all(feed_dict['z'] == pkl2))
def testInputParserPickle(self): pkl0 = {'a': 5, 'b': np.array(range(4))} pkl1 = np.array([1]) pkl2 = np.array([[1], [3]]) input_path0 = os.path.join(test.get_temp_dir(), 'pickle0.pkl') input_path1 = os.path.join(test.get_temp_dir(), 'pickle1.pkl') input_path2 = os.path.join(test.get_temp_dir(), 'pickle2.pkl') with open(input_path0, 'wb') as f: pickle.dump(pkl0, f) with open(input_path1, 'wb') as f: pickle.dump(pkl1, f) with open(input_path2, 'wb') as f: pickle.dump(pkl2, f) input_str = 'x=' + input_path0 + '[b];y=' + input_path1 + '[c];' input_str += 'z=' + input_path2 feed_dict = saved_model_cli.load_inputs_from_input_arg_string( input_str, '', '') self.assertTrue(np.all(feed_dict['x'] == pkl0['b'])) self.assertTrue(np.all(feed_dict['y'] == pkl1)) self.assertTrue(np.all(feed_dict['z'] == pkl2))