Run the caffe2 model on given inputs, recording the shape and draw the graph. predict_net/init_net: caffe2 model. tensor_inputs: a list of tensors that caffe2 model takes as input. graph_save_path: path for saving graph of exported model.
(predict_net, init_net, tensor_inputs, graph_save_path)
| 173 | |
| 174 | |
| 175 | def run_and_save_graph(predict_net, init_net, tensor_inputs, graph_save_path): |
| 176 | """ |
| 177 | Run the caffe2 model on given inputs, recording the shape and draw the graph. |
| 178 | |
| 179 | predict_net/init_net: caffe2 model. |
| 180 | tensor_inputs: a list of tensors that caffe2 model takes as input. |
| 181 | graph_save_path: path for saving graph of exported model. |
| 182 | """ |
| 183 | |
| 184 | logger.info("Saving graph of ONNX exported model to {} ...".format(graph_save_path)) |
| 185 | save_graph(predict_net, graph_save_path, op_only=False) |
| 186 | |
| 187 | # Run the exported Caffe2 net |
| 188 | logger.info("Running ONNX exported model ...") |
| 189 | with ScopedWS("__ws_tmp__", True) as ws: |
| 190 | ws.RunNetOnce(init_net) |
| 191 | initialized_blobs = set(ws.Blobs()) |
| 192 | uninitialized = [inp for inp in predict_net.external_input if inp not in initialized_blobs] |
| 193 | for name, blob in zip(uninitialized, tensor_inputs): |
| 194 | ws.FeedBlob(name, blob) |
| 195 | |
| 196 | try: |
| 197 | ws.RunNetOnce(predict_net) |
| 198 | except RuntimeError as e: |
| 199 | logger.warning("Encountered RuntimeError: \n{}".format(str(e))) |
| 200 | |
| 201 | ws_blobs = {b: ws.FetchBlob(b) for b in ws.Blobs()} |
| 202 | blob_sizes = {b: ws_blobs[b].shape for b in ws_blobs if isinstance(ws_blobs[b], np.ndarray)} |
| 203 | |
| 204 | logger.info("Saving graph with blob shapes to {} ...".format(graph_save_path)) |
| 205 | save_graph(predict_net, graph_save_path, op_only=False, blob_sizes=blob_sizes) |
| 206 | |
| 207 | return ws_blobs |
no test coverage detected