6 SpatialSoftmaxWithLoss,
9 SpatialSoftmaxWithLossGradient,
16 .TensorInferenceFunction([](
const OperatorDef& def,
37Combined Spatial Softmax and Cross-Entropy loss operator.
38Similar to SoftmaxWithLoss, this operator computes the spatial softmax
39normalized values for each layer in the batch of the given input, after which
40cross-entropy loss is computed. This operator is numerically more stable than
41separate Softmax and CrossEntropy ops. The inputs are a 2-D tensor
42(Tensor) of size (batch_size x input_feature_dimensions) and tensor of
44Output is tensor with the probability for each label in a pixel for each example
45(N x D x W x H) and averaged loss (scalar).
46For spatial softmax, weighting is by x,y position of the input.
48 .Input(0, "logits",
"Unscaled log probabilities")
49 .Input(1,
"labels",
"Ground truth")
53 "Optional blob to be used to weight the samples for the loss. With\
54 spatial set, weighting is by x,y of the input")
55 .Output(0,
"softmax",
"Tensor with softmax cross entropy loss")
56 .Output(1,
"loss",
"Average loss");
72 Output(0,
X.sizes(), at::dtype<float>());
74 if (!sum_multiplier_.defined()) {
77 D, 1.f, sum_multiplier_.mutable_data<
float>(), &
context_);
78 }
else if (sum_multiplier_.numel() !=
D) {
79 sum_multiplier_.Resize(
D);
81 D, 1.f, sum_multiplier_.mutable_data<
float>(), &
context_);
84 float* Pdata = P->template mutable_data<float>();
85 const float*
weights = (InputSize() > 2 ?
Input(2).data<
float>() :
nullptr);
93 const float* Xdata =
X.data<
float>();
95 for (
int i = 0; i <
N; ++i) {
96 for (
int y = 0;
y <
H; ++
y) {
97 for (
int x = 0;
x <
W; ++
x) {
99 float max_val = (-1e20f);
100 for (
int c = 0;
c <
D; ++
c) {
108 for (
int c = 0;
c <
D; ++
c) {
110 float expx =
exp(Xdata[
idx] - max_val);
116 for (
int c = 0;
c <
D; ++
c) {
118 Pdata[
idx] /= expsum;
126 Output(1, vector<int64_t>(), at::dtype<float>());
127 float* avg_loss_data = avg_loss->template mutable_data<float>();
128 const int* label_data =
T.data<
int>();
130 float sum_label_xent = 0.0f;
131 float total_weight = 0.0;
133 for (
int y = 0;
y <
H;
y++) {
134 for (
int x = 0;
x <
W;
x++) {
135 for (
int i = 0; i <
N; i++) {
136 int label_idx = i *
H *
W +
y *
W +
x;
137 int label = label_data[label_idx];
140 label < D && label >= 0,
141 "Label seems incorrect:label value larger than number of classes",
153 if (total_weight != 0.0) {
154 *avg_loss_data = sum_label_xent / total_weight;
156 *avg_loss_data = 0.0;
166 auto& P =
Input(InputSize() - 2);
167 auto& d_avg_loss =
Input(InputSize() - 1);
169 const float*
weights = (InputSize() > 4 ?
Input(2).data<
float>() :
nullptr);
173 auto*
dX =
Output(0,
X.sizes(), at::dtype<float>());
181 const float* Pdata = P.data<
float>();
182 float* dX_data =
dX->template mutable_data<float>();
183 const int* label_data =
T.data<
int>();
190 float total_weight = 0.0f;
191 for (
int y = 0;
y <
H; ++
y) {
192 for (
int x = 0;
x <
W; ++
x) {
193 for (
int i = 0;
i <
N; ++
i) {
194 int label_idx =
i *
H *
W +
y *
W +
x;
195 int label = label_data[label_idx];
200 dX_data[
idx] = (dX_data[
idx] - 1.0);
204 for (
int c = 0;
c <
D; ++
c) {
205 int k =
i * (
H *
W *
D) +
c * (
H *
W) +
y *
W +
x;
214 for (
int c = 0;
c <
D; ++
c) {
223 if (total_weight > 0) {
226 scale_ / total_weight,
233 d_avg_loss.data<
float>(),
235 dX->template mutable_data<float>(),
241class GetSoftmaxWithLossGradient :
public GradientMakerBase {
243 vector<OperatorDef> GetGradientDefs()
override {
245 {I(0), I(1), O(0),
GO(1)},
249 if (
def_.input_size() == 3) {
252 return SingleGradientDef(
253 "SpatialSoftmaxWithLossGradient",
256 vector<string>{GI(0)});
void CopyFromCPU(size_t n, const T *src, T *dst)
A helper class to index into arguments.
GradientMakerBase(const OperatorDef &def, const vector< GradientWrapper > &g_output)
bool RunOnDevice() override
bool RunOnDevice() override
Tensor exp(const Tensor &self)
constexpr Symbol idx(static_cast< unique_t >(_keys::attr_idx))
C10_EXPORT void Set< float, CPUContext >(const std::int64_t N, const float alpha, float *Y, CPUContext *)
C10_EXPORT void Scale< float, float, CPUContext >(const std::int64_t N, const float alpha, const float *X, float *Y, CPUContext *)
Copyright (c) 2016-present, Facebook, Inc.
REGISTER_CPU_OPERATOR(ATen, ATenOp< CPUContext >)
matrix of logits for each example and class weights
CAFFE_ENFORCE_EQ(in.size(), 1, "New shape must not be specified by the input blob and the " "argument `shape` at the same time.")
return vector< TensorShape >
ArgumentHelper helper(def)
and label is applied to the tensor elementwise If y
SparseLengths8BitsRowwiseOp< CPUContext, 0, 1 >::LENGTHS uint8 tensor obtained with Vector with the same sum of elements as the first dimension of DATA Input(3, "scale_bias", "Matrix of floats, each row r_i of which stores a pair " "s_i, b_i -- scale and bias for i-th row") .Output(0
const vector< TensorShape > & in
multiply the weights by a constant w
Unscaled log probabilities Optional blob to be used to weight the samples for the loss With spatial weighting is by x
Output tensor quantization scale X
*and produces a single output tensor *expanded *The op also takes an argument *dims *with a list of dimensions for where to add the single dimensional entries If the same blob is provided as input and the operation is copy free This is the exact inverse operation of *Squeeze *Github dims
Tensor empty(at::IntArrayRef dims, at::TensorOptions options)
See RoIPoolF Gradient of forward dX
Prefix string to prepend extracted blobs blob_names
SparseLengths8BitsRowwiseOp< CPUContext, 0, 1 >::LENGTHS SetDoc(R"DOC(
Variation of SparseLengthsMean operator, where DATA is
stored using 8bits. DATA was quantized with 8Bit row-wise
quantization (see doc to FloatToRowwiseQuantized8Bits operator). To
restore DATA from 8Bit, we use additional input that stores scales
and biases.
)DOC") .Input(0
REGISTER_GRADIENT(CTC, GetCTCGradient)
SparseLengths8BitsRowwiseOp< CPUContext, 1, 1 >::LENGTHS uint8 tensor obtained with Integer vector containing indices of the first dimension of DATA for the slices that are being aggregated Matrix of each row r_i of which stores a pair b_i scale and bias for i th row Output(0, "output", "output")
the other dimension is proportionally scaled Defaults to Whether or not to mirror the image Defaults to Vector of means per color Standard deviation by which to normalize color channels Defaults to Bounding box coordinate Defaults Bounding box coordinate Defaults Bounding box coordinate Defaults Bounding box coordinate Defaults if the input is in Caffe format Defaults to Number of CPU decode transform threads Defaults to Name of the Type of The sizes of any outputs besides the data and label(should have a number of elements equal to the number of additional "
"outputs)") .Arg( "random_scale"
CAFFE_ENFORCE(dims.front() >=0, "Dimension ids must be non-negative.")
c10::BFloat16 max(c10::BFloat16 a, c10::BFloat16 b)