@ -20,7 +20,7 @@ from paddle.fluid.initializer import MSRA
from paddle . fluid . param_attr import ParamAttr
from paddle . fluid . param_attr import ParamAttr
from paddle . fluid . dygraph . nn import Conv2D , Pool2D , BatchNorm , Linear
from paddle . fluid . dygraph . nn import Conv2D , Pool2D , BatchNorm , Linear
from paddle . fluid . dygraph import declarative , ProgramTranslator
from paddle . fluid . dygraph import declarative , ProgramTranslator
from paddle . fluid . dygraph . io import VARIABLE_FILENAME
from paddle . fluid . dygraph . io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX
import unittest
import unittest
@ -439,7 +439,10 @@ class Args(object):
train_step = 10
train_step = 10
place = fluid . CUDAPlace ( 0 ) if fluid . is_compiled_with_cuda (
place = fluid . CUDAPlace ( 0 ) if fluid . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
) else fluid . CPUPlace ( )
model_save_path = model + " .inference.model "
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 "
dy_state_dict_save_path = model + " .dygraph "
@ -504,7 +507,7 @@ def train_mobilenet(args, to_static):
t_last = time . time ( )
t_last = time . time ( )
if batch_id > args . train_step :
if batch_id > args . train_step :
if to_static :
if to_static :
fluid . dygraph . jit . save ( net , args . model_save_p ath )
fluid . dygraph . jit . save ( net , args . model_save_p refix )
else :
else :
fluid . dygraph . save_dygraph ( net . state_dict ( ) ,
fluid . dygraph . save_dygraph ( net . state_dict ( ) ,
args . dy_state_dict_save_path )
args . dy_state_dict_save_path )
@ -514,11 +517,15 @@ def train_mobilenet(args, to_static):
def predict_static ( args , data ) :
def predict_static ( args , data ) :
paddle . enable_static ( )
exe = fluid . Executor ( args . place )
exe = fluid . Executor ( args . place )
# load inference model
# load inference model
[ inference_program , feed_target_names ,
[ inference_program , feed_target_names ,
fetch_targets ] = fluid . io . load_inference_model (
fetch_targets ] = fluid . io . load_inference_model (
args . model_save_path , executor = exe , params_filename = VARIABLE_FILENAME )
args . model_save_dir ,
executor = exe ,
model_filename = args . model_filename ,
params_filename = args . params_filename )
pred_res = exe . run ( inference_program ,
pred_res = exe . run ( inference_program ,
feed = { feed_target_names [ 0 ] : data } ,
feed = { feed_target_names [ 0 ] : data } ,
@ -545,7 +552,7 @@ def predict_dygraph(args, data):
def predict_dygraph_jit ( args , data ) :
def predict_dygraph_jit ( args , data ) :
with fluid . dygraph . guard ( args . place ) :
with fluid . dygraph . guard ( args . place ) :
model = fluid . dygraph . jit . load ( args . model_save_p ath )
model = fluid . dygraph . jit . load ( args . model_save_p refix )
model . eval ( )
model . eval ( )
pred_res = model ( data )
pred_res = model ( data )
@ -554,7 +561,8 @@ def predict_dygraph_jit(args, data):
def predict_analysis_inference ( args , data ) :
def predict_analysis_inference ( args , data ) :
output = PredictorTools ( args . model_save_path , VARIABLE_FILENAME , [ data ] )
output = PredictorTools ( args . model_save_dir , args . model_filename ,
args . params_filename , [ data ] )
out = output ( )
out = output ( )
return out
return out
@ -565,7 +573,9 @@ class TestMobileNet(unittest.TestCase):
def train ( self , model_name , to_static ) :
def train ( self , model_name , to_static ) :
self . args . model = model_name
self . args . model = model_name
self . args . model_save_path = model_name + " .inference.model "
self . args . model_save_prefix = " ./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 = model_name + " .dygraph "
out = train_mobilenet ( self . args , to_static )
out = train_mobilenet ( self . args , to_static )
return out
return out
@ -579,7 +589,9 @@ class TestMobileNet(unittest.TestCase):
def assert_same_predict ( self , model_name ) :
def assert_same_predict ( self , model_name ) :
self . args . model = model_name
self . args . model = model_name
self . args . model_save_path = model_name + " .inference.model "
self . args . model_save_prefix = " ./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 = model_name + " .dygraph "
local_random = np . random . RandomState ( SEED )
local_random = np . random . RandomState ( SEED )
image = local_random . random_sample ( [ 1 , 3 , 224 , 224 ] ) . astype ( ' float32 ' )
image = local_random . random_sample ( [ 1 , 3 , 224 , 224 ] ) . astype ( ' float32 ' )