diff --git a/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py b/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py index 304e7cd9a5c325c2a178679473e3147d4c83c569..da25bc8d1fbaffce1c9a47056978626cd7e1bbcc 100644 --- a/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py +++ b/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py @@ -628,7 +628,7 @@ class ModuleApiTest(unittest.TestCase): model.prepare() if self.param_states: model.load(self.param_states, optim_state=None) - return model.test_batch(self.inputs) + return model.predict_batch(self.inputs) def check_output_with_place(self, place, mode="test"): dygraph_output = self._calc_output(place, mode, dygraph=True) diff --git a/python/paddle/hapi/model.py b/python/paddle/hapi/model.py index 4f36effe6dd62e97ccbdf2c6eed422ef3325f64c..ff962fb1c1d5cbc1cb648757b056f1657b966c96 100644 --- a/python/paddle/hapi/model.py +++ b/python/paddle/hapi/model.py @@ -261,7 +261,7 @@ class StaticGraphAdapter(object): self.mode = 'eval' return self._run(inputs, labels) - def test_batch(self, inputs): + def predict_batch(self, inputs): self.mode = 'test' return self._run(inputs, None) @@ -723,7 +723,7 @@ class DynamicGraphAdapter(object): else: return metrics - def test_batch(self, inputs): + def predict_batch(self, inputs): self.model.network.eval() self.mode = 'test' inputs = [to_variable(x) for x in to_list(inputs)] @@ -894,10 +894,13 @@ class Model(object): Run one training step on a batch of data. Args: - inputs (list): A list of numpy.ndarray, each is a batch of - input data. - labels (list): A list of numpy.ndarray, each is a batch of - input label. If has no labels, set None. Default is None. + inputs (numpy.ndarray|Tensor|list): Batch of input data. It could + be a numpy array or paddle.Tensor, or a list of arrays or + tensors (in case the model has multiple inputs). + labels (numpy.ndarray|Tensor|list): Batch of labels. It could be + a numpy array or paddle.Tensor, or a list of arrays or tensors + (in case the model has multiple labels). If has no labels, + set None. Default is None. Returns: A list of scalar training loss if the model has no metrics, @@ -941,10 +944,13 @@ class Model(object): Run one evaluating step on a batch of data. Args: - inputs (list): A list of numpy.ndarray, each is a batch of - input data. - labels (list): A list of numpy.ndarray, each is a batch of - input label. If has no labels, set None. Default is None. + inputs (numpy.ndarray|Tensor|list): Batch of input data. It could + be a numpy array or paddle.Tensor, or a list of arrays or + tensors (in case the model has multiple inputs). + labels (numpy.ndarray|Tensor|list): Batch of labels. It could be + a numpy array or paddle.Tensor, or a list of arrays or tensors + (in case the model has multiple labels). If has no labels, + set None. Default is None. Returns: A list of scalar testing loss if the model has no metrics, @@ -984,13 +990,14 @@ class Model(object): self._update_inputs() return loss - def test_batch(self, inputs): + def predict_batch(self, inputs): """ - Run one testing step on a batch of data. + Run one predicting step on a batch of data. Args: - inputs (list): A list of numpy.ndarray, each is a batch of - input data. + inputs (numpy.ndarray|Tensor|list): Batch of input data. It could + be a numpy array or paddle.Tensor, or a list of arrays or + tensors (in case the model has multiple inputs). Returns: A list of numpy.ndarray of predictions, that is the outputs @@ -1019,10 +1026,10 @@ class Model(object): model = paddle.Model(net, input, label) model.prepare() data = np.random.random(size=(4,784)).astype(np.float32) - out = model.test_batch([data]) + out = model.predict_batch([data]) print(out) """ - loss = self._adapter.test_batch(inputs) + loss = self._adapter.predict_batch(inputs) if fluid.in_dygraph_mode() and self._input_shapes is None: self._update_inputs() return loss @@ -1847,10 +1854,9 @@ class Model(object): logs[k] = v else: if self._inputs is not None: - outs = getattr(self, - mode + '_batch')(data[:len(self._inputs)]) + outs = self.predict_batch(data[:len(self._inputs)]) else: - outs = getattr(self, mode + '_batch')(data) + outs = self.predict_batch(data) outputs.append(outs) diff --git a/python/paddle/tests/test_model.py b/python/paddle/tests/test_model.py index 78307f6a1bd68df023bd63ca47df3898df12236b..bb5a7424a5095141a57fbf2381f0360bd8a783fc 100644 --- a/python/paddle/tests/test_model.py +++ b/python/paddle/tests/test_model.py @@ -284,6 +284,17 @@ class TestModel(unittest.TestCase): fluid.disable_dygraph() if dynamic else None + def test_predict_without_inputs(self): + fluid.enable_dygraph(self.device) + model = Model(LeNet()) + model.prepare() + model.load(self.weight_path) + model._inputs = None + output = model.predict( + self.test_dataset, batch_size=64, stack_outputs=True) + np.testing.assert_equal(output[0].shape[0], len(self.test_dataset)) + fluid.disable_dygraph() + class MyModel(paddle.nn.Layer): def __init__(self): @@ -370,7 +381,7 @@ class TestModelFunction(unittest.TestCase): inputs = [InputSpec([None, dim], 'float32', 'x')] model = Model(net, inputs) model.prepare() - out, = model.test_batch([data]) + out, = model.predict_batch([data]) np.testing.assert_allclose(out, ref, rtol=1e-6) fluid.disable_dygraph() if dynamic else None @@ -546,7 +557,7 @@ class TestModelFunction(unittest.TestCase): np.random.random((1, 1, 28, 28)), dtype=np.float32) model.save(save_dir, training=False) - ori_results = model.test_batch(tensor_img) + ori_results = model.predict_batch(tensor_img) fluid.disable_dygraph() if dynamic else None place = fluid.CPUPlace() if not fluid.is_compiled_with_cuda( @@ -569,7 +580,7 @@ class TestModelFunction(unittest.TestCase): mnist_data = MnistDataset(mode='train') paddle.disable_static() # without inputs - for initial in ["fit", "train_batch", "eval_batch", "test_batch"]: + for initial in ["fit", "train_batch", "eval_batch", "predict_batch"]: save_dir = tempfile.mkdtemp() if not os.path.exists(save_dir): os.makedirs(save_dir) @@ -590,7 +601,7 @@ class TestModelFunction(unittest.TestCase): elif initial == "eval_batch": model.eval_batch([img], [label]) else: - model.test_batch([img]) + model.predict_batch([img]) model.save(save_dir, training=False) shutil.rmtree(save_dir) diff --git a/python/paddle/tests/test_pretrained_model.py b/python/paddle/tests/test_pretrained_model.py index a36dd75549a9e2571da6f405a05f729f7a16b295..b24b51555c58190616f2a510f012768282505f90 100644 --- a/python/paddle/tests/test_pretrained_model.py +++ b/python/paddle/tests/test_pretrained_model.py @@ -40,10 +40,10 @@ class TestPretrainedModel(unittest.TestCase): if dygraph: model.save(path) - res['dygraph'] = model.test_batch(x) + res['dygraph'] = model.predict_batch(x) else: model.load(path) - res['static'] = model.test_batch(x) + res['static'] = model.predict_batch(x) if not dygraph: paddle.disable_static() diff --git a/python/paddle/tests/test_transforms.py b/python/paddle/tests/test_transforms.py index ac21f8a6192c4b786bc50a3284e6f9cc8f0df7e3..978200fd531c5b82df64ca3c1cf3d70741567700 100644 --- a/python/paddle/tests/test_transforms.py +++ b/python/paddle/tests/test_transforms.py @@ -205,6 +205,12 @@ class TestTransformsCV2(unittest.TestCase): assert isinstance(tensor, paddle.Tensor) np.testing.assert_equal(tensor.shape, (3, 50, 100)) + def test_keys(self): + fake_img1 = self.create_image((200, 150, 3)) + fake_img2 = self.create_image((200, 150, 3)) + trans_pad = transforms.Pad(10, keys=("image", )) + fake_img_padded = trans_pad((fake_img1, fake_img2)) + def test_exception(self): trans = transforms.Compose([transforms.Resize(-1)]) diff --git a/python/paddle/tests/test_vision_models.py b/python/paddle/tests/test_vision_models.py index 6489b02615bb94269f83c4ed780e555c487eacbe..5f35a1e0e5a4ba9d6c6683918a2a03c190089762 100644 --- a/python/paddle/tests/test_vision_models.py +++ b/python/paddle/tests/test_vision_models.py @@ -33,7 +33,7 @@ class TestVisonModels(unittest.TestCase): model = paddle.Model(net, input) model.prepare() - model.test_batch(x) + model.predict_batch(x) def test_mobilenetv2_pretrained(self): self.models_infer('mobilenet_v2', pretrained=False) @@ -77,7 +77,7 @@ class TestVisonModels(unittest.TestCase): lenet.prepare() x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32) - lenet.test_batch(x) + lenet.predict_batch(x) if __name__ == '__main__': diff --git a/python/paddle/vision/transforms/transforms.py b/python/paddle/vision/transforms/transforms.py index 9079f91aac9fa7f1c6714d55f52a3c2488accf36..06f3f231ef3d24dd4ed7ee54637e440eb1ec812b 100644 --- a/python/paddle/vision/transforms/transforms.py +++ b/python/paddle/vision/transforms/transforms.py @@ -272,7 +272,7 @@ class BaseTransform(object): else: outputs.append(apply_func(inputs[i])) if len(inputs) > len(self.keys): - outputs.extend(input[len(self.keys):]) + outputs.extend(inputs[len(self.keys):]) if len(outputs) == 1: outputs = outputs[0]