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