A wrapper around the traced model in caffe2's pb format. Examples: :: model = Caffe2Model.load_protobuf("dir/with/pb/files") inputs = [{"image": img_tensor_CHW}] outputs = model(inputs)
| 185 | |
| 186 | |
| 187 | class Caffe2Model(nn.Module): |
| 188 | """ |
| 189 | A wrapper around the traced model in caffe2's pb format. |
| 190 | |
| 191 | Examples: |
| 192 | :: |
| 193 | model = Caffe2Model.load_protobuf("dir/with/pb/files") |
| 194 | inputs = [{"image": img_tensor_CHW}] |
| 195 | outputs = model(inputs) |
| 196 | |
| 197 | """ |
| 198 | |
| 199 | def __init__(self, predict_net, init_net): |
| 200 | super().__init__() |
| 201 | self.eval() # always in eval mode |
| 202 | self._predict_net = predict_net |
| 203 | self._init_net = init_net |
| 204 | self._predictor = None |
| 205 | |
| 206 | __init__.__HIDE_SPHINX_DOC__ = True |
| 207 | |
| 208 | @property |
| 209 | def predict_net(self): |
| 210 | """ |
| 211 | Returns: |
| 212 | core.Net: the underlying caffe2 predict net |
| 213 | """ |
| 214 | return self._predict_net |
| 215 | |
| 216 | @property |
| 217 | def init_net(self): |
| 218 | """ |
| 219 | Returns: |
| 220 | core.Net: the underlying caffe2 init net |
| 221 | """ |
| 222 | return self._init_net |
| 223 | |
| 224 | def save_protobuf(self, output_dir): |
| 225 | """ |
| 226 | Save the model as caffe2's protobuf format. |
| 227 | |
| 228 | Args: |
| 229 | output_dir (str): the output directory to save protobuf files. |
| 230 | """ |
| 231 | logger = logging.getLogger(__name__) |
| 232 | logger.info("Saving model to {} ...".format(output_dir)) |
| 233 | if not PathManager.exists(output_dir): |
| 234 | PathManager.mkdirs(output_dir) |
| 235 | |
| 236 | with PathManager.open(os.path.join(output_dir, "model.pb"), "wb") as f: |
| 237 | f.write(self._predict_net.SerializeToString()) |
| 238 | with PathManager.open(os.path.join(output_dir, "model.pbtxt"), "w") as f: |
| 239 | f.write(str(self._predict_net)) |
| 240 | with PathManager.open(os.path.join(output_dir, "model_init.pb"), "wb") as f: |
| 241 | f.write(self._init_net.SerializeToString()) |
| 242 | |
| 243 | def save_graph(self, output_file, inputs=None): |
| 244 | """ |
no outgoing calls
no test coverage detected