(platform, model_file,
input_file, mace_out_file,
input_names, input_shapes, input_data_formats,
output_names, output_shapes, output_data_formats,
validation_threshold, input_data_types, log_file)
| 187 | |
| 188 | |
| 189 | def validate_tf_model(platform, model_file, |
| 190 | input_file, mace_out_file, |
| 191 | input_names, input_shapes, input_data_formats, |
| 192 | output_names, output_shapes, output_data_formats, |
| 193 | validation_threshold, input_data_types, log_file): |
| 194 | import tensorflow as tf |
| 195 | if not os.path.isfile(model_file): |
| 196 | util.MaceLogger.error( |
| 197 | VALIDATION_MODULE, |
| 198 | "Input graph file '" + model_file + "' does not exist!") |
| 199 | |
| 200 | tf.reset_default_graph() |
| 201 | input_graph_def = tf.GraphDef() |
| 202 | with open(model_file, "rb") as f: |
| 203 | data = f.read() |
| 204 | input_graph_def.ParseFromString(data) |
| 205 | tf.import_graph_def(input_graph_def, name="") |
| 206 | |
| 207 | with tf.Session() as session: |
| 208 | with session.graph.as_default() as graph: |
| 209 | tf.import_graph_def(input_graph_def, name="") |
| 210 | input_dict = {} |
| 211 | for i in range(len(input_names)): |
| 212 | input_value = load_data( |
| 213 | util.formatted_file_name(input_file, input_names[i]), |
| 214 | input_data_types[i]) |
| 215 | input_value = input_value.reshape(input_shapes[i]) |
| 216 | if input_data_formats[i] == DataFormat.NCHW and \ |
| 217 | len(input_shapes[i]) == 4: |
| 218 | input_value = input_value.transpose((0, 2, 3, 1)) |
| 219 | elif input_data_formats[i] == DataFormat.OIHW and \ |
| 220 | len(input_shapes[i]) == 4: |
| 221 | # OIHW -> HWIO |
| 222 | input_value = input_value.transpose((2, 3, 1, 0)) |
| 223 | input_node = graph.get_tensor_by_name( |
| 224 | normalize_tf_tensor_name(input_names[i])) |
| 225 | input_dict[input_node] = input_value |
| 226 | |
| 227 | output_nodes = [] |
| 228 | for name in output_names: |
| 229 | output_nodes.extend( |
| 230 | [graph.get_tensor_by_name( |
| 231 | normalize_tf_tensor_name(name))]) |
| 232 | output_values = session.run(output_nodes, feed_dict=input_dict) |
| 233 | for i in range(len(output_names)): |
| 234 | output_file_name = util.formatted_file_name( |
| 235 | mace_out_file, output_names[i]) |
| 236 | mace_out_value = load_data( |
| 237 | output_file_name, |
| 238 | get_data_type_by_value(output_values[i])) |
| 239 | mace_out_value, real_out_shape, real_out_data_format = \ |
| 240 | get_real_out_value_shape_df(platform, |
| 241 | mace_out_value, |
| 242 | output_shapes[i], |
| 243 | output_data_formats[i]) |
| 244 | compare_output(output_names[i], |
| 245 | mace_out_value, output_values[i], |
| 246 | validation_threshold, log_file, |
no test coverage detected