Caffe2 - Python API
A deep learning, cross platform ML framework
tools.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 ## @package tools
17 # Module caffe2.python.helpers.tools
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 
23 
24 def image_input(
25  model, blob_in, blob_out, order="NCHW", use_gpu_transform=False, **kwargs
26 ):
27  assert 'is_test' in kwargs, "Argument 'is_test' is required"
28  if order == "NCHW":
29  if (use_gpu_transform):
30  kwargs['use_gpu_transform'] = 1 if use_gpu_transform else 0
31  # GPU transform will handle NHWC -> NCHW
32  outputs = model.net.ImageInput(blob_in, blob_out, **kwargs)
33  pass
34  else:
35  outputs = model.net.ImageInput(
36  blob_in, [blob_out[0] + '_nhwc'] + blob_out[1:], **kwargs
37  )
38  outputs_list = list(outputs)
39  outputs_list[0] = model.net.NHWC2NCHW(outputs_list[0], blob_out[0])
40  outputs = tuple(outputs_list)
41  else:
42  outputs = model.net.ImageInput(blob_in, blob_out, **kwargs)
43  return outputs
44 
45 
46 def video_input(model, blob_in, blob_out, **kwargs):
47  # size of outputs can vary depending on kwargs
48  outputs = model.net.VideoInput(blob_in, blob_out, **kwargs)
49  return outputs