(platform, model_file, input_file,
mace_out_file, weight_file,
input_names, input_shapes, input_data_formats,
output_names, output_shapes, output_data_formats,
validation_threshold, log_file)
| 290 | |
| 291 | |
| 292 | def validate_caffe_model(platform, model_file, input_file, |
| 293 | mace_out_file, weight_file, |
| 294 | input_names, input_shapes, input_data_formats, |
| 295 | output_names, output_shapes, output_data_formats, |
| 296 | validation_threshold, log_file): |
| 297 | os.environ['GLOG_minloglevel'] = '1' # suprress Caffe verbose prints |
| 298 | import caffe |
| 299 | if not os.path.isfile(model_file): |
| 300 | util.MaceLogger.error( |
| 301 | VALIDATION_MODULE, |
| 302 | "Input graph file '" + model_file + "' does not exist!") |
| 303 | if not os.path.isfile(weight_file): |
| 304 | util.MaceLogger.error( |
| 305 | VALIDATION_MODULE, |
| 306 | "Input weight file '" + weight_file + "' does not exist!") |
| 307 | |
| 308 | caffe.set_mode_cpu() |
| 309 | |
| 310 | net = caffe.Net(model_file, caffe.TEST, weights=weight_file) |
| 311 | |
| 312 | for i in range(len(input_names)): |
| 313 | input_value = load_data( |
| 314 | util.formatted_file_name(input_file, input_names[i])) |
| 315 | input_value = input_value.reshape(input_shapes[i]) |
| 316 | if input_data_formats[i] == DataFormat.NHWC and \ |
| 317 | len(input_shapes[i]) == 4: |
| 318 | input_value = input_value.transpose((0, 3, 1, 2)) |
| 319 | input_blob_name = input_names[i] |
| 320 | try: |
| 321 | if input_names[i] in net.top_names: |
| 322 | input_blob_name = net.top_names[input_names[i]][0] |
| 323 | except ValueError: |
| 324 | pass |
| 325 | new_shape = input_value.shape |
| 326 | net.blobs[input_blob_name].reshape(*new_shape) |
| 327 | for index in range(input_value.shape[0]): |
| 328 | net.blobs[input_blob_name].data[index] = input_value[index] |
| 329 | |
| 330 | net.forward() |
| 331 | |
| 332 | for i in range(len(output_names)): |
| 333 | value = net.blobs[output_names[i]].data |
| 334 | output_file_name = util.formatted_file_name( |
| 335 | mace_out_file, output_names[i]) |
| 336 | mace_out_value = load_data(output_file_name) |
| 337 | mace_out_value, real_output_shape, real_output_data_format = \ |
| 338 | get_real_out_value_shape_df(platform, |
| 339 | mace_out_value, |
| 340 | output_shapes[i], |
| 341 | output_data_formats[i]) |
| 342 | compare_output(output_names[i], mace_out_value, |
| 343 | value, validation_threshold, log_file, |
| 344 | real_output_shape, real_output_data_format) |
| 345 | |
| 346 | |
| 347 | # Remove annoying warning from onnxruntime. |
no test coverage detected