Caffe2 - C++ API
A deep learning, cross platform ML framework
image_input_op.cc
1 #include "caffe2/image/image_input_op.h"
2 
3 #ifdef CAFFE2_USE_MKLDNN
4 #include <caffe2/ideep/operators/operator_fallback_ideep.h>
5 #include <caffe2/ideep/utils/ideep_operator.h>
6 #endif
7 
8 namespace caffe2 {
9 
10 template <>
11 bool ImageInputOp<CPUContext>::ApplyTransformOnGPU(
12  const std::vector<std::int64_t>&,
13  const c10::Device&) {
14  return false;
15 }
16 
17 REGISTER_CPU_OPERATOR(ImageInput, ImageInputOp<CPUContext>);
18 
19 OPERATOR_SCHEMA(ImageInput)
20  .NumInputs(0, 1)
21  .NumOutputs(2, INT_MAX)
22  .TensorInferenceFunction(
23  [](const OperatorDef& def, const vector<TensorShape>& /* unused */ ) {
24  vector<TensorShape> out(2);
25  ArgumentHelper helper(def);
26  int batch_size = helper.GetSingleArgument<int>("batch_size", 0);
27  int crop = helper.GetSingleArgument<int>("crop", -1);
28  int color = helper.GetSingleArgument<int>("color", 1);
29  CHECK_GT(crop, 0);
30  out[0] = CreateTensorShape(
31  vector<int>{batch_size, crop, crop, color ? 3 : 1},
32  TensorProto::FLOAT);
33  out[1] =
34  CreateTensorShape(vector<int>{1, batch_size}, TensorProto::INT32);
35  return out;
36  })
37  .SetDoc(R"DOC(
38 Imports and processes images from a database. For each run of the operator,
39 batch_size images will be processed. GPUs can optionally be used for
40 part of the processing.
41 
42 The following transformations are applied to the image
43  - A bounding box is applied to the initial image (optional)
44  - The image is rescaled either up or down (with the scale argument) or
45  just up (with the minsize argument)
46  - The image is randomly cropped (crop size is passed as an argument but
47  the location of the crop is random except if is_test is passed in which case
48  the image in cropped at the center)
49  - The image is normalized. Each of its color channels can have separate
50  normalization values
51 
52 The dimension of the output image will always be cropxcrop
53 )DOC")
54  .Arg("batch_size", "Number of images to output for each run of the operator"
55  ". Must be 1 or greater")
56  .Arg("color", "Number of color channels (1 or 3). Defaults to 1")
57  .Arg("color_jitter", "Whether or not to do color jitter. Defaults to 0")
58  .Arg("img_saturation", "Image saturation scale used in color jittering. "
59  "Defaults to 0.4")
60  .Arg("img_brightness", "Image brightness scale used in color jittering. "
61  "Defaults to 0.4")
62  .Arg("img_contrast", "Image contrast scale used in color jittering. "
63  "Defaults to 0.4")
64  .Arg("color_lighting", "Whether or not to do color lighting."
65  " Defaults to 0")
66  .Arg("color_lighting_std", "Std of normal distribution where color lighting"
67  " scaling factor is sampled. Defaults to 0.1")
68  .Arg("scale_jitter_type", "Type 0: No scale jittering "
69  "Type 1: Inception-style scale jittering")
70  .Arg("label_type", "Type 0: single integer label for multi-class "
71  "classification. Type 1: sparse active label indices for multi-label "
72  "classification. Type 2: dense label embedding vector for label "
73  "embedding regression")
74  .Arg("scale", "Scale the size of the smallest dimension of the image to"
75  " this. Scale and minsize are mutually exclusive."
76  " Must be larger than crop")
77  .Arg("minsize", "Scale the size of the smallest dimension of the image to"
78  " this only if the size is initially smaller. Scale and minsize are"
79  " mutually exclusive. Must be larger than crop.")
80  .Arg("warp", "If 1, both dimensions of the image will be set to minsize or"
81  " scale; otherwise, the other dimension is proportionally scaled."
82  " Defaults to 0")
83  .Arg("crop", "Size to crop the image to. Must be provided")
84  .Arg("mirror", "Whether or not to mirror the image. Defaults to 0")
85  .Arg("mean", "Mean by which to normalize color channels."
86  " Defaults to 0.")
87  .Arg("mean_per_channel", "Vector of means per color channel "
88  " (1 or 3 elements). Defaults to mean argument. Channel order BGR")
89  .Arg("std", "Standard deviation by which to normalize color channels."
90  " Defaults to 1.")
91  .Arg("std_per_channel", "Vector of standard dev. per color channel "
92  " (1 or 3 elements). Defaults to std argument. Channel order is BGR")
93  .Arg("bounding_ymin", "Bounding box coordinate. Defaults to -1 (none)")
94  .Arg("bounding_xmin", "Bounding box coordinate. Defaults to -1 (none)")
95  .Arg("bounding_height", "Bounding box coordinate. Defaults to -1 (none)")
96  .Arg("bounding_width", "Bounding box coordinate. Defaults to -1 (none)")
97  .ArgIsTest("Set to 1 to do deterministic cropping. Defaults to 0")
98  .Arg("use_caffe_datum", "1 if the input is in Caffe format. Defaults to 0")
99  .Arg("use_gpu_transform", "1 if GPU acceleration should be used."
100  " Defaults to 0. Can only be 1 in a CUDAContext")
101  .Arg("decode_threads", "Number of CPU decode/transform threads."
102  " Defaults to 4")
103  .Arg("output_type", "If gpu_transform, can set to FLOAT or FLOAT16.")
104  .Arg("db", "Name of the database (if not passed as input)")
105  .Arg("db_type", "Type of database (if not passed as input)."
106  " Defaults to leveldb")
107  .Arg("output_sizes", "The sizes of any outputs besides the data and label "
108  "(should have a number of elements equal to the number of additional "
109  "outputs)")
110  .Arg("random_scale", "[min, max] shortest-side desired for image resize. "
111  "Defaults to [-1, -1] or no random resize desired.")
112  .Input(0, "reader", "The input reader (a db::DBReader)")
113  .Output(0, "data", "Tensor containing the images")
114  .Output(1, "label", "Tensor containing the labels")
115  .Output(2, "additional outputs", "Any outputs after the first 2 will be "
116  "Tensors read from the input TensorProtos");
117 
118 NO_GRADIENT(ImageInput);
119 
120 #ifdef CAFFE2_USE_MKLDNN
121 REGISTER_IDEEP_OPERATOR(
122  ImageInput,
123  IDEEPFallbackOp<ImageInputOp<CPUContext>>);
124 #endif
125 
126 } // namespace caffe2
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13