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