未验证 提交 84840481 编写于 作者: A Aurelius84 提交者: GitHub

[Dy2Stat]Modify all jit.save path into tempfile under dygraph_to_static directory (#42842) (#42860)

* [Dy2Stat]Modify all jit.save path into tempfile

* [Dy2Stat]Modify all jit.save path into tempfile
上级 af79273d
......@@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import math
import numpy as np
import unittest
import paddle
import tempfile
from paddle.jit import to_static
import paddle.fluid as fluid
from paddle.fluid import ParamAttr
......@@ -422,11 +424,6 @@ class Args(object):
prop_boundary_ratio = 0.5
num_sample = 2
num_sample_perbin = 2
model_save_dir = "./inference"
model_save_prefix = "./inference/bmn"
model_filename = "bmn" + INFER_MODEL_SUFFIX
params_filename = "bmn" + INFER_PARAMS_SUFFIX
dy_param_path = './bmn_dy_param'
def optimizer(cfg, parameter_list):
......@@ -559,78 +556,6 @@ def fake_data_reader(args, mode='train'):
return reader
def train_bmn(args, place, to_static):
program_translator.enable(to_static)
loss_data = []
with fluid.dygraph.guard(place):
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
global local_random
local_random = np.random.RandomState(SEED)
bmn = BMN(args)
adam = optimizer(args, parameter_list=bmn.parameters())
train_reader = fake_data_reader(args, 'train')
for epoch in range(args.epoch):
for batch_id, data in enumerate(train_reader()):
video_feat = np.array(
[item[0] for item in data]).astype(DATATYPE)
gt_iou_map = np.array(
[item[1] for item in data]).astype(DATATYPE)
gt_start = np.array([item[2] for item in data]).astype(DATATYPE)
gt_end = np.array([item[3] for item in data]).astype(DATATYPE)
x_data = to_variable(video_feat)
gt_iou_map = to_variable(gt_iou_map)
gt_start = to_variable(gt_start)
gt_end = to_variable(gt_end)
gt_iou_map.stop_gradient = True
gt_start.stop_gradient = True
gt_end.stop_gradient = True
pred_bm, pred_start, pred_end = bmn(x_data)
loss, tem_loss, pem_reg_loss, pem_cls_loss = bmn_loss_func(
pred_bm, pred_start, pred_end, gt_iou_map, gt_start, gt_end,
args)
avg_loss = fluid.layers.mean(loss)
avg_loss.backward()
adam.minimize(avg_loss)
bmn.clear_gradients()
# log loss data to verify correctness
loss_data += [
avg_loss.numpy()[0], tem_loss.numpy()[0],
pem_reg_loss.numpy()[0], pem_cls_loss.numpy()[0]
]
if args.log_interval > 0 and (
batch_id % args.log_interval == 0):
print('[TRAIN] Epoch {}, iter {} '.format(epoch, batch_id)
+ '\tLoss = {}, \ttem_loss = {}, \tpem_reg_loss = {}, \tpem_cls_loss = {}'.format(
'%f' % avg_loss.numpy()[0], '%f' % tem_loss.numpy()[0], \
'%f' % pem_reg_loss.numpy()[0], '%f' % pem_cls_loss.numpy()[0]))
# validation
if batch_id % args.valid_interval == 0 and batch_id > 0:
bmn.eval()
val_loss_data = val_bmn(bmn, args)
bmn.train()
loss_data += val_loss_data
if batch_id == args.train_batch_num:
if to_static:
fluid.dygraph.jit.save(bmn, args.model_save_prefix)
else:
fluid.dygraph.save_dygraph(bmn.state_dict(),
args.dy_param_path)
break
return np.array(loss_data)
# Validation
def val_bmn(model, args):
val_reader = fake_data_reader(args, 'valid')
......@@ -677,10 +602,93 @@ class TestTrain(unittest.TestCase):
self.place = fluid.CPUPlace() if not fluid.is_compiled_with_cuda() \
else fluid.CUDAPlace(0)
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, 'inference')
self.model_save_prefix = os.path.join(self.model_save_dir, 'bmn')
self.model_filename = "bmn" + INFER_MODEL_SUFFIX
self.params_filename = "bmn" + INFER_PARAMS_SUFFIX
self.dy_param_path = os.path.join(self.temp_dir.name, 'bmn_dy_param')
def tearDown(self):
self.temp_dir.cleanup()
def train_bmn(self, args, place, to_static):
program_translator.enable(to_static)
loss_data = []
with fluid.dygraph.guard(place):
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
global local_random
local_random = np.random.RandomState(SEED)
bmn = BMN(args)
adam = optimizer(args, parameter_list=bmn.parameters())
train_reader = fake_data_reader(args, 'train')
for epoch in range(args.epoch):
for batch_id, data in enumerate(train_reader()):
video_feat = np.array(
[item[0] for item in data]).astype(DATATYPE)
gt_iou_map = np.array(
[item[1] for item in data]).astype(DATATYPE)
gt_start = np.array(
[item[2] for item in data]).astype(DATATYPE)
gt_end = np.array(
[item[3] for item in data]).astype(DATATYPE)
x_data = to_variable(video_feat)
gt_iou_map = to_variable(gt_iou_map)
gt_start = to_variable(gt_start)
gt_end = to_variable(gt_end)
gt_iou_map.stop_gradient = True
gt_start.stop_gradient = True
gt_end.stop_gradient = True
pred_bm, pred_start, pred_end = bmn(x_data)
loss, tem_loss, pem_reg_loss, pem_cls_loss = bmn_loss_func(
pred_bm, pred_start, pred_end, gt_iou_map, gt_start,
gt_end, args)
avg_loss = fluid.layers.mean(loss)
avg_loss.backward()
adam.minimize(avg_loss)
bmn.clear_gradients()
# log loss data to verify correctness
loss_data += [
avg_loss.numpy()[0], tem_loss.numpy()[0],
pem_reg_loss.numpy()[0], pem_cls_loss.numpy()[0]
]
if args.log_interval > 0 and (
batch_id % args.log_interval == 0):
print('[TRAIN] Epoch {}, iter {} '.format(epoch, batch_id)
+ '\tLoss = {}, \ttem_loss = {}, \tpem_reg_loss = {}, \tpem_cls_loss = {}'.format(
'%f' % avg_loss.numpy()[0], '%f' % tem_loss.numpy()[0], \
'%f' % pem_reg_loss.numpy()[0], '%f' % pem_cls_loss.numpy()[0]))
# validation
if batch_id % args.valid_interval == 0 and batch_id > 0:
bmn.eval()
val_loss_data = val_bmn(bmn, args)
bmn.train()
loss_data += val_loss_data
if batch_id == args.train_batch_num:
if to_static:
fluid.dygraph.jit.save(bmn, self.model_save_prefix)
else:
fluid.dygraph.save_dygraph(bmn.state_dict(),
self.dy_param_path)
break
return np.array(loss_data)
def test_train(self):
static_res = train_bmn(self.args, self.place, to_static=True)
dygraph_res = train_bmn(self.args, self.place, to_static=False)
static_res = self.train_bmn(self.args, self.place, to_static=True)
dygraph_res = self.train_bmn(self.args, self.place, to_static=False)
self.assertTrue(
np.allclose(dygraph_res, static_res),
"dygraph_res: {},\n static_res: {}".format(
......@@ -726,8 +734,7 @@ class TestTrain(unittest.TestCase):
with fluid.dygraph.guard(self.place):
bmn = BMN(self.args)
# load dygraph trained parameters
model_dict, _ = fluid.load_dygraph(self.args.dy_param_path +
".pdparams")
model_dict, _ = fluid.load_dygraph(self.dy_param_path + ".pdparams")
bmn.set_dict(model_dict)
bmn.eval()
......@@ -743,10 +750,10 @@ class TestTrain(unittest.TestCase):
# load inference model
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(
self.args.model_save_dir,
self.model_save_dir,
executor=exe,
model_filename=self.args.model_filename,
params_filename=self.args.params_filename)
model_filename=self.model_filename,
params_filename=self.params_filename)
pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data},
fetch_list=fetch_targets)
......@@ -755,7 +762,7 @@ class TestTrain(unittest.TestCase):
def predict_dygraph_jit(self, data):
with fluid.dygraph.guard(self.place):
bmn = fluid.dygraph.jit.load(self.args.model_save_prefix)
bmn = fluid.dygraph.jit.load(self.model_save_prefix)
bmn.eval()
x = to_variable(data)
......@@ -765,9 +772,8 @@ class TestTrain(unittest.TestCase):
return pred_res
def predict_analysis_inference(self, data):
output = PredictorTools(self.args.model_save_dir,
self.args.model_filename,
self.args.params_filename, [data])
output = PredictorTools(self.model_save_dir, self.model_filename,
self.params_filename, [data])
out = output()
return out
......
......@@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import paddle
import unittest
import numpy as np
import tempfile
class BufferLayers(paddle.nn.Layer):
......@@ -66,11 +68,15 @@ class TestSequential(unittest.TestCase):
def setUp(self):
paddle.set_device('cpu')
self.seed = 2021
self.temp_dir = tempfile.TemporaryDirectory()
self._init_config()
def _init_config(self):
self.net = SequentialNet(BufferLayers, 10, 3)
self.model_path = './sequential_net'
self.model_path = os.path.join(self.temp_dir.name, 'sequential_net')
def tearDown(self):
self.temp_dir.cleanup()
def _init_seed(self):
paddle.seed(self.seed)
......@@ -108,7 +114,8 @@ class TestSequential(unittest.TestCase):
class TestNestSequential(TestSequential):
def _init_config(self):
self.net = NestSequentialNet()
self.model_path = './nested_sequential_net'
self.model_path = os.path.join(self.temp_dir.name,
'nested_sequential_net')
if __name__ == '__main__':
......
......@@ -14,7 +14,8 @@
import numpy as np
import unittest
import os
import tempfile
import paddle
import paddle.fluid as fluid
from paddle.static import InputSpec
......@@ -100,7 +101,11 @@ class TestStaticFunctionInstance(unittest.TestCase):
class TestInputSpec(unittest.TestCase):
def setUp(self):
pass
self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name, 'simple_net')
def tearDown(self):
self.temp_dir.cleanup()
def test_with_input_spec(self):
with fluid.dygraph.guard(fluid.CPUPlace()):
......@@ -116,8 +121,8 @@ class TestInputSpec(unittest.TestCase):
# 2. test save load
net.inner_function(x)
jit.save(net, './simple_net')
infer_net = fluid.dygraph.jit.load('./simple_net')
jit.save(net, self.model_path)
infer_net = fluid.dygraph.jit.load(self.model_path)
pred = infer_net(x)
self.assertTrue(np.allclose(out.numpy(), pred.numpy()))
......@@ -438,12 +443,19 @@ class SetBuffersNet2(paddle.nn.Layer):
class TestSetBuffers(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name, 'SetBuffersNet1')
def tearDown(self):
self.temp_dir.cleanup()
def test_set_buffers1(self):
paddle.disable_static()
net = SetBuffersNet1()
out = net()
self.assertEqual(out.numpy().tolist(), [2])
paddle.jit.save(net, './SetBuffersNet1')
paddle.jit.save(net, self.model_path)
paddle.enable_static()
def test_set_buffers2(self):
......
......@@ -16,6 +16,8 @@ from __future__ import print_function
import numpy as np
import unittest
import os
import tempfile
import paddle
import paddle.fluid as fluid
......@@ -532,12 +534,20 @@ class TestForOriginalTuple(TestTransformForOriginalList):
class TestForZip(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_for_zip_error(self):
with self.assertRaises(RuntimeError):
paddle.jit.save(for_zip_error, './for_zip_error')
model_path = os.path.join(self.temp_dir.name, 'for_zip_error')
paddle.jit.save(for_zip_error, model_path)
def test_for_zip(self):
paddle.jit.save(for_zip, './for_zip')
model_path = os.path.join(self.temp_dir.name, 'for_zip')
paddle.jit.save(for_zip, model_path)
if __name__ == '__main__':
......
......@@ -17,6 +17,8 @@ from __future__ import print_function
import numpy as np
import paddle
import unittest
import os
import tempfile
class GradLayer(paddle.nn.Layer):
......@@ -88,8 +90,15 @@ class TestGradLinear(TestGrad):
self.func = GradLinearLayer()
self.x = paddle.ones(shape=[10, 2, 5], dtype='float32')
self.x.stop_gradient = False
self.infer_model_path = "double_grad_infer_model"
self.train_model_path = "double_grad_train_model"
self.temp_dir = tempfile.TemporaryDirectory()
self.infer_model_path = os.path.join(self.temp_dir.name,
'double_grad_infer_model')
self.train_model_path = os.path.join(self.temp_dir.name,
'double_grad_train_model')
def tearDown(self):
self.temp_dir.cleanup()
def test_save_infer_program(self):
input_spec = [
......@@ -113,7 +122,7 @@ class TestGradLinear(TestGrad):
avg_loss = paddle.mean(paddle.abs(out - 1))
avg_loss.backward()
optimizer.minimize(avg_loss)
print(self.x.grad.mean())
self.func.clear_gradients()
paddle.jit.save(self.func, self.train_model_path)
......@@ -129,8 +138,15 @@ class TestNoGradLinear(TestGradLinear):
self.func = NoGradLinearLayer()
self.x = paddle.ones(shape=[10, 2, 5], dtype='float32')
self.x.stop_gradient = False
self.infer_model_path = "no_grad_infer_model"
self.train_model_path = "no_grad_train_model"
self.temp_dir = tempfile.TemporaryDirectory()
self.infer_model_path = os.path.join(self.temp_dir.name,
'no_grad_infer_model')
self.train_model_path = os.path.join(self.temp_dir.name,
'no_grad_train_model')
def tearDown(self):
self.temp_dir.cleanup()
if __name__ == '__main__':
......
......@@ -19,6 +19,7 @@ import numpy as np
import unittest
import os
import tempfile
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
import paddle
......@@ -406,11 +407,6 @@ class Args(object):
base_learning_rate = 0.01
bigru_num = 2
print_steps = 1
model_save_dir = "./inference"
model_save_prefix = "./inference/lac"
model_filename = "lac" + INFER_MODEL_SUFFIX
params_filename = "lac" + INFER_PARAMS_SUFFIX
dy_param_path = "./lac_dy_param"
def get_random_input_data(batch_size, vocab_size, num_labels, max_seq_len=64):
......@@ -458,84 +454,86 @@ def create_dataloader(reader, place):
return data_loader
def do_train(args, to_static):
program_translator.enable(to_static)
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
with fluid.dygraph.guard(place):
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
reader = get_random_input_data(args.batch_size, args.vocab_size,
args.num_labels)
train_loader = create_dataloader(reader, place)
model = LexNet(args)
optimizer = fluid.optimizer.AdamOptimizer(
learning_rate=args.base_learning_rate,
parameter_list=model.parameters())
chunk_eval = ChunkEval(
int(math.ceil((args.num_labels - 1) / 2.0)), "IOB")
step = 0
chunk_evaluator = fluid.metrics.ChunkEvaluator()
chunk_evaluator.reset()
loss_data = []
for epoch_id in range(args.epoch):
for batch in train_loader():
words, targets, length = batch
start_time = time.time()
avg_cost, crf_decode = model(words, targets, length)
loss_data.append(avg_cost.numpy()[0])
# backward and optimization
avg_cost.backward()
optimizer.minimize(avg_cost)
model.clear_gradients()
end_time = time.time()
if step % args.print_steps == 0:
(precision, recall, f1_score, num_infer_chunks,
num_label_chunks, num_correct_chunks) = chunk_eval(
input=crf_decode, label=targets, seq_length=length)
outputs = [avg_cost, precision, recall, f1_score]
avg_cost, precision, recall, f1_score = [
np.mean(x.numpy()) for x in outputs
]
print(
"[train] step = %d, loss = %f, P: %f, R: %f, F1: %f, elapsed time %f"
% (step, avg_cost, precision, recall, f1_score,
end_time - start_time))
step += 1
# save inference model
if to_static:
fluid.dygraph.jit.save(
layer=model,
path=args.model_save_prefix,
input_spec=[input_specs[0], input_specs[-1]],
output_spec=[crf_decode])
else:
fluid.dygraph.save_dygraph(model.state_dict(), args.dy_param_path)
return np.array(loss_data)
class TestLACModel(unittest.TestCase):
def setUp(self):
self.args = Args()
self.place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, 'inference')
self.model_save_prefix = os.path.join(self.model_save_dir, 'lac')
self.model_filename = "lac" + INFER_MODEL_SUFFIX
self.params_filename = "lac" + INFER_PARAMS_SUFFIX
self.dy_param_path = os.path.join(self.temp_dir.name, 'lac_dy_param')
def train(self, args, to_static):
program_translator.enable(to_static)
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
with fluid.dygraph.guard(place):
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
reader = get_random_input_data(args.batch_size, args.vocab_size,
args.num_labels)
train_loader = create_dataloader(reader, place)
model = LexNet(args)
optimizer = fluid.optimizer.AdamOptimizer(
learning_rate=args.base_learning_rate,
parameter_list=model.parameters())
chunk_eval = ChunkEval(
int(math.ceil((args.num_labels - 1) / 2.0)), "IOB")
step = 0
chunk_evaluator = fluid.metrics.ChunkEvaluator()
chunk_evaluator.reset()
loss_data = []
for epoch_id in range(args.epoch):
for batch in train_loader():
words, targets, length = batch
start_time = time.time()
avg_cost, crf_decode = model(words, targets, length)
loss_data.append(avg_cost.numpy()[0])
# backward and optimization
avg_cost.backward()
optimizer.minimize(avg_cost)
model.clear_gradients()
end_time = time.time()
if step % args.print_steps == 0:
(precision, recall, f1_score, num_infer_chunks,
num_label_chunks, num_correct_chunks) = chunk_eval(
input=crf_decode, label=targets, seq_length=length)
outputs = [avg_cost, precision, recall, f1_score]
avg_cost, precision, recall, f1_score = [
np.mean(x.numpy()) for x in outputs
]
print(
"[train] step = %d, loss = %f, P: %f, R: %f, F1: %f, elapsed time %f"
% (step, avg_cost, precision, recall, f1_score,
end_time - start_time))
step += 1
# save inference model
if to_static:
fluid.dygraph.jit.save(
layer=model,
path=self.model_save_prefix,
input_spec=[input_specs[0], input_specs[-1]],
output_spec=[crf_decode])
else:
fluid.dygraph.save_dygraph(model.state_dict(),
self.dy_param_path)
def train(self, to_static):
out = do_train(self.args, to_static)
return out
return np.array(loss_data)
def test_train(self):
st_out = self.train(to_static=True)
dy_out = self.train(to_static=False)
st_out = self.train(self.args, to_static=True)
dy_out = self.train(self.args, to_static=False)
self.assertTrue(
np.allclose(dy_out, st_out),
msg="dygraph output:\n{},\nstatic output:\n {}.".format(dy_out,
......@@ -565,8 +563,7 @@ class TestLACModel(unittest.TestCase):
with fluid.dygraph.guard(self.place):
model = LexNet(self.args)
# load dygraph trained parameters
model_dict, _ = fluid.load_dygraph(self.args.dy_param_path +
".pdparams")
model_dict, _ = fluid.load_dygraph(self.dy_param_path + ".pdparams")
model.set_dict(model_dict)
model.eval()
......@@ -585,10 +582,10 @@ class TestLACModel(unittest.TestCase):
# load inference model
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(
self.args.model_save_dir,
self.model_save_dir,
executor=exe,
model_filename=self.args.model_filename,
params_filename=self.args.params_filename)
model_filename=self.model_filename,
params_filename=self.params_filename)
words, targets, length = batch
pred_res = exe.run(
......@@ -601,7 +598,7 @@ class TestLACModel(unittest.TestCase):
def predict_dygraph_jit(self, batch):
words, targets, length = batch
with fluid.dygraph.guard(self.place):
model = fluid.dygraph.jit.load(self.args.model_save_prefix)
model = fluid.dygraph.jit.load(self.model_save_prefix)
model.eval()
pred_res = model(to_variable(words), to_variable(length))
......
......@@ -14,8 +14,9 @@
import unittest
import paddle
import os
import numpy as np
import tempfile
def forward_post_hook1(layer, input, output):
......@@ -54,7 +55,11 @@ class TestNestLayerHook(unittest.TestCase):
def setUp(self):
paddle.seed(2022)
self.x = paddle.randn([4, 10])
self.path = "./net_hook"
self.temp_dir = tempfile.TemporaryDirectory()
self.path = os.path.join(self.temp_dir.name, 'net_hook')
def tearDown(self):
self.temp_dir.cleanup()
def train_net(self, to_static=False):
paddle.seed(2022)
......
......@@ -16,6 +16,8 @@ import numpy as np
import paddle
import unittest
from paddle import nn
import os
import tempfile
class LSTMLayer(nn.Layer):
......@@ -40,6 +42,12 @@ class Net(nn.Layer):
class TestLstm(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def run_lstm(self, to_static):
paddle.jit.ProgramTranslator().enable(to_static)
......@@ -78,11 +86,12 @@ class TestLstm(unittest.TestCase):
x = paddle.randn((2, 10, 12))
net = paddle.jit.to_static(
net, input_spec=[paddle.static.InputSpec(shape=[-1, 10, 12])])
paddle.jit.save(net, 'simple_lstm')
model_path = os.path.join(self.temp_dir.name, 'simple_lstm')
paddle.jit.save(net, model_path)
dygraph_out = net(x)
# load saved model
load_net = paddle.jit.load('simple_lstm')
load_net = paddle.jit.load(model_path)
static_out = load_net(x)
self.assertTrue(
......@@ -115,6 +124,12 @@ class LinearNet(nn.Layer):
class TestSaveInEvalMode(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_save_in_eval(self):
paddle.jit.ProgramTranslator().enable(True)
net = LinearNet()
......@@ -131,9 +146,11 @@ class TestSaveInEvalMode(unittest.TestCase):
# save directly
net = paddle.jit.to_static(
net, input_spec=[paddle.static.InputSpec(shape=[-1, 10])])
paddle.jit.save(net, 'linear_net')
model_path = os.path.join(self.temp_dir.name, 'linear_net')
paddle.jit.save(net, model_path)
# load saved model
load_net = paddle.jit.load('linear_net')
load_net = paddle.jit.load(model_path)
x = paddle.randn((2, 10))
eval_out = net(x)
......@@ -146,6 +163,12 @@ class TestSaveInEvalMode(unittest.TestCase):
class TestEvalAfterSave(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_eval_after_save(self):
x = paddle.randn((2, 10, 12)).astype('float32')
net = Net(12, 2)
......@@ -159,8 +182,9 @@ class TestEvalAfterSave(unittest.TestCase):
x = paddle.randn((2, 10, 12)).astype('float32')
dy_out = net(x)
# save model
paddle.jit.save(net, 'jit.save/lstm', input_spec=[x])
load_net = paddle.jit.load('jit.save/lstm')
model_path = os.path.join(self.temp_dir.name, 'jit.save/lstm')
paddle.jit.save(net, model_path, input_spec=[x])
load_net = paddle.jit.load(model_path)
load_out = load_net(x)
self.assertTrue(np.allclose(dy_out.numpy(), load_out.numpy()))
# eval
......
......@@ -15,6 +15,8 @@
from __future__ import print_function
import unittest
import os
import tempfile
from time import time
import numpy as np
......@@ -134,6 +136,10 @@ class TestMNIST(unittest.TestCase):
paddle.dataset.mnist.train(),
batch_size=self.batch_size,
drop_last=True)
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
class TestMNISTWithToStatic(TestMNIST):
......@@ -227,9 +233,10 @@ class TestMNISTWithToStatic(TestMNIST):
def check_jit_save_load(self, model, inputs, input_spec, to_static, gt_out):
if to_static:
infer_model_path = "./test_mnist_inference_model_by_jit_save"
model_save_dir = "./inference"
model_save_prefix = "./inference/mnist"
infer_model_path = os.path.join(
self.temp_dir.name, 'test_mnist_inference_model_by_jit_save')
model_save_dir = os.path.join(self.temp_dir.name, 'inference')
model_save_prefix = os.path.join(model_save_dir, 'mnist')
model_filename = "mnist" + INFER_MODEL_SUFFIX
params_filename = "mnist" + INFER_PARAMS_SUFFIX
fluid.dygraph.jit.save(
......
......@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import tempfile
import time
import numpy as np
import paddle
......@@ -439,11 +441,11 @@ class Args(object):
train_step = 10
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
model_save_dir = "./inference"
model_save_prefix = "./inference/" + model
model_filename = model + INFER_MODEL_SUFFIX
params_filename = model + INFER_PARAMS_SUFFIX
dy_state_dict_save_path = model + ".dygraph"
model_save_dir = None
model_save_prefix = None
model_filename = None
params_filename = None
dy_state_dict_save_path = None
def train_mobilenet(args, to_static):
......@@ -571,13 +573,21 @@ def predict_analysis_inference(args, data):
class TestMobileNet(unittest.TestCase):
def setUp(self):
self.args = Args()
self.temp_dir = tempfile.TemporaryDirectory()
self.args.model_save_dir = os.path.join(self.temp_dir.name,
"./inference")
def tearDown(self):
self.temp_dir.cleanup()
def train(self, model_name, to_static):
self.args.model = model_name
self.args.model_save_prefix = "./inference/" + model_name
self.args.model_save_prefix = os.path.join(self.temp_dir.name,
"./inference/" + model_name)
self.args.model_filename = model_name + INFER_MODEL_SUFFIX
self.args.params_filename = model_name + INFER_PARAMS_SUFFIX
self.args.dy_state_dict_save_path = model_name + ".dygraph"
self.args.dy_state_dict_save_path = os.path.join(
self.temp_dir.name, model_name + ".dygraph")
out = train_mobilenet(self.args, to_static)
return out
......@@ -590,10 +600,12 @@ class TestMobileNet(unittest.TestCase):
def assert_same_predict(self, model_name):
self.args.model = model_name
self.args.model_save_prefix = "./inference/" + model_name
self.args.model_save_prefix = os.path.join(self.temp_dir.name,
"./inference/" + model_name)
self.args.model_filename = model_name + INFER_MODEL_SUFFIX
self.args.params_filename = model_name + INFER_PARAMS_SUFFIX
self.args.dy_state_dict_save_path = model_name + ".dygraph"
self.args.dy_state_dict_save_path = os.path.join(
self.temp_dir.name, model_name + ".dygraph")
local_random = np.random.RandomState(SEED)
image = local_random.random_sample([1, 3, 224, 224]).astype('float32')
dy_pre = predict_dygraph(self.args, image)
......
......@@ -19,6 +19,7 @@ os.environ["FLAGS_enable_eager_mode"] = "0"
import math
import time
import unittest
import tempfile
import numpy as np
......@@ -37,11 +38,6 @@ epoch_num = 1
place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() \
else paddle.CPUPlace()
MODEL_SAVE_DIR = "./inference"
MODEL_SAVE_PREFIX = "./inference/resnet_v2"
MODEL_FILENAME = "resnet_v2" + paddle.fluid.dygraph.io.INFER_MODEL_SUFFIX
PARAMS_FILENAME = "resnet_v2" + paddle.fluid.dygraph.io.INFER_PARAMS_SUFFIX
DY_STATE_DICT_SAVE_PATH = "./resnet_v2.dygraph"
program_translator = paddle.jit.ProgramTranslator()
if paddle.is_compiled_with_cuda():
......@@ -210,133 +206,145 @@ def reader_decorator(reader):
return __reader__
def train(to_static):
"""
Tests model decorated by `dygraph_to_static_output` in static mode. For users, the model is defined in dygraph mode and trained in static mode.
"""
paddle.disable_static(place)
np.random.seed(SEED)
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
train_reader = paddle.batch(
reader_decorator(paddle.dataset.flowers.train(use_xmap=False)),
batch_size=batch_size,
drop_last=True)
data_loader = paddle.io.DataLoader.from_generator(capacity=5, iterable=True)
data_loader.set_sample_list_generator(train_reader)
resnet = ResNet()
optimizer = optimizer_setting(parameter_list=resnet.parameters())
for epoch in range(epoch_num):
total_loss = 0.0
total_acc1 = 0.0
total_acc5 = 0.0
total_sample = 0
for batch_id, data in enumerate(data_loader()):
start_time = time.time()
img, label = data
pred = resnet(img)
loss = paddle.nn.functional.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(x=loss)
acc_top1 = paddle.metric.accuracy(input=pred, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=pred, label=label, k=5)
avg_loss.backward()
optimizer.minimize(avg_loss)
resnet.clear_gradients()
total_loss += avg_loss
total_acc1 += acc_top1
total_acc5 += acc_top5
total_sample += 1
end_time = time.time()
if batch_id % 2 == 0:
print( "epoch %d | batch step %d, loss %0.3f, acc1 %0.3f, acc5 %0.3f, time %f" % \
( epoch, batch_id, total_loss.numpy() / total_sample, \
total_acc1.numpy() / total_sample, total_acc5.numpy() / total_sample, end_time-start_time))
if batch_id == 10:
if to_static:
paddle.jit.save(resnet, MODEL_SAVE_PREFIX)
else:
paddle.fluid.dygraph.save_dygraph(resnet.state_dict(),
DY_STATE_DICT_SAVE_PATH)
# avoid dataloader throw abort signaal
data_loader._reset()
break
paddle.enable_static()
return total_loss.numpy()
def predict_dygraph(data):
program_translator.enable(False)
paddle.disable_static(place)
resnet = ResNet()
model_dict, _ = paddle.fluid.dygraph.load_dygraph(DY_STATE_DICT_SAVE_PATH)
resnet.set_dict(model_dict)
resnet.eval()
pred_res = resnet(
paddle.to_tensor(
data=data, dtype=None, place=None, stop_gradient=True))
ret = pred_res.numpy()
paddle.enable_static()
return ret
def predict_static(data):
exe = paddle.static.Executor(place)
[inference_program, feed_target_names,
fetch_targets] = paddle.static.load_inference_model(
MODEL_SAVE_DIR,
executor=exe,
model_filename=MODEL_FILENAME,
params_filename=PARAMS_FILENAME)
pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data},
fetch_list=fetch_targets)
return pred_res[0]
def predict_dygraph_jit(data):
paddle.disable_static(place)
resnet = paddle.jit.load(MODEL_SAVE_PREFIX)
resnet.eval()
pred_res = resnet(data)
ret = pred_res.numpy()
paddle.enable_static()
return ret
def predict_analysis_inference(data):
output = PredictorTools(MODEL_SAVE_DIR, MODEL_FILENAME, PARAMS_FILENAME,
[data])
out = output()
return out
class TestResnet(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, "./inference")
self.model_save_prefix = os.path.join(self.temp_dir.name,
"./inference/resnet_v2")
self.model_filename = "resnet_v2" + paddle.fluid.dygraph.io.INFER_MODEL_SUFFIX
self.params_filename = "resnet_v2" + paddle.fluid.dygraph.io.INFER_PARAMS_SUFFIX
self.dy_state_dict_save_path = os.path.join(self.temp_dir.name,
"./resnet_v2.dygraph")
def tearDown(self):
self.temp_dir.cleanup()
def do_train(self, to_static):
"""
Tests model decorated by `dygraph_to_static_output` in static mode. For users, the model is defined in dygraph mode and trained in static mode.
"""
paddle.disable_static(place)
np.random.seed(SEED)
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
train_reader = paddle.batch(
reader_decorator(paddle.dataset.flowers.train(use_xmap=False)),
batch_size=batch_size,
drop_last=True)
data_loader = paddle.io.DataLoader.from_generator(
capacity=5, iterable=True)
data_loader.set_sample_list_generator(train_reader)
resnet = ResNet()
optimizer = optimizer_setting(parameter_list=resnet.parameters())
for epoch in range(epoch_num):
total_loss = 0.0
total_acc1 = 0.0
total_acc5 = 0.0
total_sample = 0
for batch_id, data in enumerate(data_loader()):
start_time = time.time()
img, label = data
pred = resnet(img)
loss = paddle.nn.functional.cross_entropy(
input=pred, label=label)
avg_loss = paddle.mean(x=loss)
acc_top1 = paddle.metric.accuracy(input=pred, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=pred, label=label, k=5)
avg_loss.backward()
optimizer.minimize(avg_loss)
resnet.clear_gradients()
total_loss += avg_loss
total_acc1 += acc_top1
total_acc5 += acc_top5
total_sample += 1
end_time = time.time()
if batch_id % 2 == 0:
print( "epoch %d | batch step %d, loss %0.3f, acc1 %0.3f, acc5 %0.3f, time %f" % \
( epoch, batch_id, total_loss.numpy() / total_sample, \
total_acc1.numpy() / total_sample, total_acc5.numpy() / total_sample, end_time-start_time))
if batch_id == 10:
if to_static:
paddle.jit.save(resnet, self.model_save_prefix)
else:
paddle.fluid.dygraph.save_dygraph(
resnet.state_dict(), self.dy_state_dict_save_path)
# avoid dataloader throw abort signaal
data_loader._reset()
break
paddle.enable_static()
return total_loss.numpy()
def predict_dygraph(self, data):
program_translator.enable(False)
paddle.disable_static(place)
resnet = ResNet()
model_dict, _ = paddle.fluid.dygraph.load_dygraph(
self.dy_state_dict_save_path)
resnet.set_dict(model_dict)
resnet.eval()
pred_res = resnet(
paddle.to_tensor(
data=data, dtype=None, place=None, stop_gradient=True))
ret = pred_res.numpy()
paddle.enable_static()
return ret
def predict_static(self, data):
exe = paddle.static.Executor(place)
[inference_program, feed_target_names,
fetch_targets] = paddle.static.load_inference_model(
self.model_save_dir,
executor=exe,
model_filename=self.model_filename,
params_filename=self.params_filename)
pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data},
fetch_list=fetch_targets)
return pred_res[0]
def predict_dygraph_jit(self, data):
paddle.disable_static(place)
resnet = paddle.jit.load(self.model_save_prefix)
resnet.eval()
pred_res = resnet(data)
ret = pred_res.numpy()
paddle.enable_static()
return ret
def predict_analysis_inference(self, data):
output = PredictorTools(self.model_save_dir, self.model_filename,
self.params_filename, [data])
out = output()
return out
def train(self, to_static):
program_translator.enable(to_static)
return train(to_static)
return self.do_train(to_static)
def verify_predict(self):
image = np.random.random([1, 3, 224, 224]).astype('float32')
dy_pre = predict_dygraph(image)
st_pre = predict_static(image)
dy_jit_pre = predict_dygraph_jit(image)
predictor_pre = predict_analysis_inference(image)
dy_pre = self.predict_dygraph(image)
st_pre = self.predict_static(image)
dy_jit_pre = self.predict_dygraph_jit(image)
predictor_pre = self.predict_analysis_inference(image)
self.assertTrue(
np.allclose(dy_pre, st_pre),
msg="dy_pre:\n {}\n, st_pre: \n{}.".format(dy_pre, st_pre))
......@@ -361,7 +369,7 @@ class TestResnet(unittest.TestCase):
paddle.fluid.set_flags({'FLAGS_use_mkldnn': True})
try:
if paddle.fluid.core.is_compiled_with_mkldnn():
train(to_static=True)
self.train(to_static=True)
finally:
paddle.fluid.set_flags({'FLAGS_use_mkldnn': False})
......
......@@ -15,6 +15,7 @@
from __future__ import print_function
import os
import tempfile
import unittest
import numpy as np
......@@ -48,6 +49,12 @@ class SimpleFcLayer(fluid.dygraph.Layer):
class TestDyToStaticSaveInferenceModel(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_save_inference_model(self):
fc_size = 20
x_data = np.random.random((fc_size, fc_size)).astype('float32')
......@@ -66,8 +73,10 @@ class TestDyToStaticSaveInferenceModel(unittest.TestCase):
adam.minimize(loss)
layer.clear_gradients()
# test for saving model in dygraph.guard
infer_model_prefix = "./test_dy2stat_inference_in_guard/model"
infer_model_dir = "./test_dy2stat_inference_in_guard"
infer_model_prefix = os.path.join(
self.temp_dir.name, "test_dy2stat_inference_in_guard/model")
infer_model_dir = os.path.join(self.temp_dir.name,
"test_dy2stat_inference_in_guard")
fluid.dygraph.jit.save(
layer=layer,
path=infer_model_prefix,
......@@ -90,8 +99,10 @@ class TestDyToStaticSaveInferenceModel(unittest.TestCase):
expected_persistable_vars = set([p.name for p in model.parameters()])
infer_model_prefix = "./test_dy2stat_inference/model"
infer_model_dir = "./test_dy2stat_inference"
infer_model_prefix = os.path.join(self.temp_dir.name,
"test_dy2stat_inference/model")
infer_model_dir = os.path.join(self.temp_dir.name,
"test_dy2stat_inference")
model_filename = "model" + INFER_MODEL_SUFFIX
params_filename = "model" + INFER_PARAMS_SUFFIX
fluid.dygraph.jit.save(
......
......@@ -15,6 +15,8 @@
from __future__ import print_function
import unittest
import os
import tempfile
import numpy as np
import paddle.fluid as fluid
......@@ -30,6 +32,14 @@ place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace(
class TestDyToStaticSaveLoad(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name,
"test_dy2stat_save_load")
def tearDown(self):
self.temp_dir.cleanup()
def test_save_load_same_result(self):
program_translator = ProgramTranslator()
x_data = np.random.randn(30, 10, 32).astype('float32')
......@@ -50,7 +60,8 @@ class TestDyToStaticSaveLoad(unittest.TestCase):
adam.minimize(static_loss)
net.clear_gradients()
# Save parameters
fluid.save_dygraph(net.state_dict(), "./test_dy2stat_save_load")
fluid.save_dygraph(net.state_dict(), self.model_path)
# minimize() will update parameter, call net() to get output and avg_loss.
# Switch into eval mode.
net.eval()
......@@ -61,7 +72,7 @@ class TestDyToStaticSaveLoad(unittest.TestCase):
dygraph_net = Linear(32, 64)
# Load parameters
model_dict, _ = fluid.load_dygraph("./test_dy2stat_save_load")
model_dict, _ = fluid.load_dygraph(self.model_path)
dygraph_net.set_dict(model_dict)
# Switch into eval mode.
dygraph_net.eval()
......
......@@ -16,6 +16,8 @@ import logging
import math
import time
import unittest
import os
import tempfile
import numpy as np
import paddle
......@@ -35,11 +37,6 @@ BATCH_SIZE = 8
EPOCH_NUM = 1
PRINT_STEP = 2
STEP_NUM = 10
MODEL_SAVE_DIR = "./inference"
MODEL_SAVE_PREFIX = "./inference/se_resnet"
MODEL_FILENAME = "se_resnet" + INFER_MODEL_SUFFIX
PARAMS_FILENAME = "se_resnet" + INFER_PARAMS_SUFFIX
DY_STATE_DICT_SAVE_PATH = "./se_resnet.dygraph"
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() \
else fluid.CPUPlace()
......@@ -327,129 +324,6 @@ class SeResNeXt(fluid.dygraph.Layer):
return out, avg_loss, acc_top1, acc_top5
def train(train_reader, to_static):
program_translator = ProgramTranslator()
program_translator.enable(to_static)
np.random.seed(SEED)
with fluid.dygraph.guard(place):
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
se_resnext = SeResNeXt()
optimizer = optimizer_setting(train_parameters, se_resnext.parameters())
for epoch_id in range(EPOCH_NUM):
total_loss = 0.0
total_acc1 = 0.0
total_acc5 = 0.0
total_sample = 0
step_idx = 0
speed_list = []
for step_id, data in enumerate(train_reader()):
dy_x_data = np.array([x[0].reshape(3, 224, 224)
for x in data]).astype('float32')
y_data = np.array([x[1] for x in data]).astype('int64').reshape(
BATCH_SIZE, 1)
img = to_variable(dy_x_data)
label = to_variable(y_data)
label.stop_gradient = True
pred, avg_loss, acc_top1, acc_top5 = se_resnext(img, label)
dy_out = avg_loss.numpy()
avg_loss.backward()
optimizer.minimize(avg_loss)
se_resnext.clear_gradients()
lr = optimizer._global_learning_rate().numpy()
total_loss += dy_out
total_acc1 += acc_top1.numpy()
total_acc5 += acc_top5.numpy()
total_sample += 1
if step_id % PRINT_STEP == 0:
if step_id == 0:
logging.info( "epoch %d | step %d, loss %0.3f, acc1 %0.3f, acc5 %0.3f" % \
( epoch_id, step_id, total_loss / total_sample, \
total_acc1 / total_sample, total_acc5 / total_sample))
avg_batch_time = time.time()
else:
speed = PRINT_STEP / (time.time() - avg_batch_time)
speed_list.append(speed)
logging.info( "epoch %d | step %d, loss %0.3f, acc1 %0.3f, acc5 %0.3f, speed %.3f steps/s" % \
( epoch_id, step_id, total_loss / total_sample, \
total_acc1 / total_sample, total_acc5 / total_sample, speed))
avg_batch_time = time.time()
step_idx += 1
if step_idx == STEP_NUM:
if to_static:
fluid.dygraph.jit.save(
se_resnext,
MODEL_SAVE_PREFIX, [img],
output_spec=[pred])
else:
fluid.dygraph.save_dygraph(se_resnext.state_dict(),
DY_STATE_DICT_SAVE_PATH)
break
return pred.numpy(), avg_loss.numpy(), acc_top1.numpy(), acc_top5.numpy(
)
def predict_dygraph(data):
program_translator = ProgramTranslator()
program_translator.enable(False)
with fluid.dygraph.guard(place):
se_resnext = SeResNeXt()
model_dict, _ = fluid.dygraph.load_dygraph(DY_STATE_DICT_SAVE_PATH)
se_resnext.set_dict(model_dict)
se_resnext.eval()
label = np.random.random([1, 1]).astype("int64")
img = fluid.dygraph.to_variable(data)
label = fluid.dygraph.to_variable(label)
pred_res, _, _, _ = se_resnext(img, label)
return pred_res.numpy()
def predict_static(data):
paddle.enable_static()
exe = fluid.Executor(place)
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(
MODEL_SAVE_DIR,
executor=exe,
model_filename=MODEL_FILENAME,
params_filename=PARAMS_FILENAME)
pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data},
fetch_list=fetch_targets)
return pred_res[0]
def predict_dygraph_jit(data):
with fluid.dygraph.guard(place):
se_resnext = fluid.dygraph.jit.load(MODEL_SAVE_PREFIX)
se_resnext.eval()
pred_res = se_resnext(data)
return pred_res.numpy()
def predict_analysis_inference(data):
output = PredictorTools(MODEL_SAVE_DIR, MODEL_FILENAME, PARAMS_FILENAME,
[data])
out = output()
return out
class TestSeResnet(unittest.TestCase):
def setUp(self):
self.train_reader = paddle.batch(
......@@ -457,13 +331,148 @@ class TestSeResnet(unittest.TestCase):
use_xmap=False, cycle=True),
batch_size=BATCH_SIZE,
drop_last=True)
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, "inference")
self.model_save_prefix = os.path.join(self.temp_dir.name,
"inference/se_resnet")
self.model_filename = "se_resnet" + INFER_MODEL_SUFFIX
self.params_filename = "se_resnet" + INFER_PARAMS_SUFFIX
self.dy_state_dict_save_path = os.path.join(self.temp_dir.name,
"se_resnet.dygraph")
def tearDown(self):
self.temp_dir.cleanup()
def train(self, train_reader, to_static):
program_translator = ProgramTranslator()
program_translator.enable(to_static)
np.random.seed(SEED)
with fluid.dygraph.guard(place):
paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED)
se_resnext = SeResNeXt()
optimizer = optimizer_setting(train_parameters,
se_resnext.parameters())
for epoch_id in range(EPOCH_NUM):
total_loss = 0.0
total_acc1 = 0.0
total_acc5 = 0.0
total_sample = 0
step_idx = 0
speed_list = []
for step_id, data in enumerate(train_reader()):
dy_x_data = np.array(
[x[0].reshape(3, 224, 224)
for x in data]).astype('float32')
y_data = np.array(
[x[1] for x in data]).astype('int64').reshape(
BATCH_SIZE, 1)
img = to_variable(dy_x_data)
label = to_variable(y_data)
label.stop_gradient = True
pred, avg_loss, acc_top1, acc_top5 = se_resnext(img, label)
dy_out = avg_loss.numpy()
avg_loss.backward()
optimizer.minimize(avg_loss)
se_resnext.clear_gradients()
lr = optimizer._global_learning_rate().numpy()
total_loss += dy_out
total_acc1 += acc_top1.numpy()
total_acc5 += acc_top5.numpy()
total_sample += 1
if step_id % PRINT_STEP == 0:
if step_id == 0:
logging.info( "epoch %d | step %d, loss %0.3f, acc1 %0.3f, acc5 %0.3f" % \
( epoch_id, step_id, total_loss / total_sample, \
total_acc1 / total_sample, total_acc5 / total_sample))
avg_batch_time = time.time()
else:
speed = PRINT_STEP / (time.time() - avg_batch_time)
speed_list.append(speed)
logging.info( "epoch %d | step %d, loss %0.3f, acc1 %0.3f, acc5 %0.3f, speed %.3f steps/s" % \
( epoch_id, step_id, total_loss / total_sample, \
total_acc1 / total_sample, total_acc5 / total_sample, speed))
avg_batch_time = time.time()
step_idx += 1
if step_idx == STEP_NUM:
if to_static:
fluid.dygraph.jit.save(
se_resnext,
self.model_save_prefix, [img],
output_spec=[pred])
else:
fluid.dygraph.save_dygraph(
se_resnext.state_dict(),
self.dy_state_dict_save_path)
break
return pred.numpy(), avg_loss.numpy(), acc_top1.numpy(
), acc_top5.numpy()
def predict_dygraph(self, data):
program_translator = ProgramTranslator()
program_translator.enable(False)
with fluid.dygraph.guard(place):
se_resnext = SeResNeXt()
model_dict, _ = fluid.dygraph.load_dygraph(
self.dy_state_dict_save_path)
se_resnext.set_dict(model_dict)
se_resnext.eval()
label = np.random.random([1, 1]).astype("int64")
img = fluid.dygraph.to_variable(data)
label = fluid.dygraph.to_variable(label)
pred_res, _, _, _ = se_resnext(img, label)
return pred_res.numpy()
def predict_static(self, data):
paddle.enable_static()
exe = fluid.Executor(place)
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(
self.model_save_dir,
executor=exe,
model_filename=self.model_filename,
params_filename=self.params_filename)
pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data},
fetch_list=fetch_targets)
return pred_res[0]
def predict_dygraph_jit(self, data):
with fluid.dygraph.guard(place):
se_resnext = fluid.dygraph.jit.load(self.model_save_prefix)
se_resnext.eval()
pred_res = se_resnext(data)
return pred_res.numpy()
def predict_analysis_inference(self, data):
output = PredictorTools(self.model_save_dir, self.model_filename,
self.params_filename, [data])
out = output()
return out
def verify_predict(self):
image = np.random.random([1, 3, 224, 224]).astype('float32')
dy_pre = predict_dygraph(image)
st_pre = predict_static(image)
dy_jit_pre = predict_dygraph_jit(image)
predictor_pre = predict_analysis_inference(image)
dy_pre = self.predict_dygraph(image)
st_pre = self.predict_static(image)
dy_jit_pre = self.predict_dygraph_jit(image)
predictor_pre = self.predict_analysis_inference(image)
self.assertTrue(
np.allclose(dy_pre, st_pre),
msg="dy_pre:\n {}\n, st_pre: \n{}.".format(dy_pre, st_pre))
......@@ -483,9 +492,9 @@ class TestSeResnet(unittest.TestCase):
flat_predictor_pre[i], flat_st_pre[i]))
def test_check_result(self):
pred_1, loss_1, acc1_1, acc5_1 = train(
pred_1, loss_1, acc1_1, acc5_1 = self.train(
self.train_reader, to_static=False)
pred_2, loss_2, acc1_2, acc5_2 = train(
pred_2, loss_2, acc1_2, acc5_2 = self.train(
self.train_reader, to_static=True)
self.assertTrue(
......
......@@ -13,6 +13,7 @@
# limitations under the License.
import os
import tempfile
import time
import unittest
......@@ -22,7 +23,7 @@ from paddle.fluid.clip import GradientClipByGlobalNorm
from paddle.fluid.dygraph.dygraph_to_static import ProgramTranslator
from seq2seq_dygraph_model import BaseModel, AttentionModel
from seq2seq_utils import Seq2SeqModelHyperParams as args
from seq2seq_utils import Seq2SeqModelHyperParams
from seq2seq_utils import get_data_iter
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace(
)
......@@ -43,7 +44,7 @@ def prepare_input(batch):
return inputs, np.sum(tar_mask)
def train(attn_model=False):
def train(args, attn_model=False):
with fluid.dygraph.guard(place):
fluid.default_startup_program().random_seed = 2020
fluid.default_main_program().random_seed = 2020
......@@ -117,7 +118,7 @@ def train(attn_model=False):
return loss.numpy()
def infer(attn_model=False):
def infer(args, attn_model=False):
with fluid.dygraph.guard(place):
if attn_model:
......@@ -160,19 +161,32 @@ def infer(attn_model=False):
class TestSeq2seq(unittest.TestCase):
def setUp(self):
self.args = Seq2SeqModelHyperParams
self.temp_dir = tempfile.TemporaryDirectory()
self.args.base_model_path = os.path.join(self.temp_dir.name,
self.args.base_model_path)
self.args.attn_model_path = os.path.join(self.temp_dir.name,
self.args.attn_model_path)
self.args.reload_model = os.path.join(self.temp_dir.name,
self.args.reload_model)
def tearDown(self):
self.temp_dir.cleanup()
def run_dygraph(self, mode="train", attn_model=False):
program_translator.enable(False)
if mode == "train":
return train(attn_model)
return train(self.args, attn_model)
else:
return infer(attn_model)
return infer(self.args, attn_model)
def run_static(self, mode="train", attn_model=False):
program_translator.enable(True)
if mode == "train":
return train(attn_model)
return train(self.args, attn_model)
else:
return infer(attn_model)
return infer(self.args, attn_model)
def _test_train(self, attn_model=False):
dygraph_loss = self.run_dygraph(mode="train", attn_model=attn_model)
......
......@@ -13,7 +13,8 @@
# limitations under the License.
from __future__ import print_function
import os
import tempfile
import unittest
import numpy as np
......@@ -166,15 +167,20 @@ class TestSetValue(TestSliceWithoutControlFlow):
class TestSetValueWithLayerAndSave(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name,
"layer_use_set_value")
def tearDown(self):
self.temp_dir.cleanup()
def test_set_value_with_save(self):
prog_trans.enable(True)
model = LayerWithSetValue(input_dim=10, hidden=1)
x = paddle.full(shape=[5, 10], fill_value=5.0, dtype="float32")
paddle.jit.save(
layer=model,
path="./layer_use_set_value",
input_spec=[x],
output_spec=None)
layer=model, path=self.model_path, input_spec=[x], output_spec=None)
class TestSliceSupplementSpecialCase(unittest.TestCase):
......
......@@ -11,7 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import tempfile
import paddle
import unittest
import numpy as np
......@@ -72,11 +73,16 @@ class TestTyping(unittest.TestCase):
self.x = paddle.randn([4, 16])
self.spec = [paddle.static.InputSpec(shape=[None, 16], dtype='float32')]
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def build_net(self):
return LinearNetWithTuple(self.in_num, self.out_num)
def save_and_load(self, suffix=''):
path = './layer_typing_' + suffix
path = os.path.join(self.temp_dir.name, 'layer_typing_' + suffix)
paddle.jit.save(self.net, path, input_spec=self.spec)
return paddle.jit.load(path)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册