Skip to content

Commit df5b222

Browse files
committed
Merge pull request opencv#9829 from pengli:ocl4dnn
2 parents f1fe873 + 937b8e4 commit df5b222

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

modules/dnn/src/layers/softmax_layer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class SoftMaxLayerImpl : public SoftmaxLayer
100100
config.in_shape = shape(*inputs[0]);
101101
config.axis = axisRaw;
102102
config.channels = inputs[0]->size[axisRaw];
103+
config.logsoftmax = logSoftMax;
103104

104105
softmaxOp = Ptr<OCL4DNNSoftmax<float> >(new OCL4DNNSoftmax<float>(config));
105106
}
@@ -108,7 +109,7 @@ class SoftMaxLayerImpl : public SoftmaxLayer
108109
srcMat = inputs[0]->getUMat(ACCESS_READ);
109110
dstMat = outputs[0].getUMat(ACCESS_WRITE);
110111

111-
if (!logSoftMax && softmaxOp->Forward(srcMat, dstMat))
112+
if (softmaxOp->Forward(srcMat, dstMat))
112113
return true;
113114

114115
const Mat &src = *inputs[0];

modules/dnn/src/ocl4dnn/include/ocl4dnn.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,12 @@ class OCL4DNNLRN
445445

446446
struct OCL4DNNSoftmaxConfig
447447
{
448-
OCL4DNNSoftmaxConfig() : axis(0), channels(0)
448+
OCL4DNNSoftmaxConfig() : axis(0), channels(0), logsoftmax(false)
449449
{}
450450
MatShape in_shape;
451451
int axis;
452452
int channels;
453+
bool logsoftmax;
453454
};
454455

455456
template<typename Dtype>
@@ -467,6 +468,7 @@ class OCL4DNNSoftmax
467468
int32_t channels_;
468469
int32_t count_;
469470
bool use_slm_;
471+
bool log_softmax_;
470472
UMat scale_data_;
471473
};
472474
#endif // HAVE_OPENCL

modules/dnn/src/ocl4dnn/src/ocl4dnn_softmax.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ OCL4DNNSoftmax<Dtype>::OCL4DNNSoftmax(OCL4DNNSoftmaxConfig config)
5252
{
5353
softmax_axis_ = config.axis;
5454
channels_ = config.channels;
55+
log_softmax_ = config.logsoftmax;
5556

5657
inner_num_ = 1;
5758
outer_num_ = 1;
@@ -90,6 +91,7 @@ bool OCL4DNNSoftmax<Dtype>::Forward(const UMat& bottom, UMat& top)
9091
String kname;
9192
ocl::Kernel oclk_softmax_forward_kernel;
9293

94+
if (log_softmax_) opts += " -DLOG_SOFTMAX ";
9395
if (use_slm_)
9496
kname = CL_KERNEL_SELECT("softmax_forward_slm");
9597
else

modules/dnn/src/opencl/softmax_loss.cl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ __kernel void TEMPLATE(softmax_forward_slm,Dtype)(const int num, const int chann
112112
for (int index = get_global_id(0); index < channels * spatial_dim;
113113
index += get_global_size(0)) {
114114
int s = index % spatial_dim;
115-
out[n * channels * spatial_dim + index] = out_tmp[index] / scale_tmp[s];
115+
Dtype v = out_tmp[index] / scale_tmp[s];
116+
#ifdef LOG_SOFTMAX
117+
v = log(v);
118+
#endif
119+
out[n * channels * spatial_dim + index] = v;
116120
}
117121
}
118122

@@ -177,6 +181,10 @@ __kernel void TEMPLATE(softmax_forward,Dtype)(const int num, const int channels,
177181
for (int index = get_global_id(0); index < channels * spatial_dim;
178182
index += get_global_size(0)) {
179183
int s = index % spatial_dim;
180-
out[n * channels * spatial_dim + index] /= scale[n * spatial_dim + s];
184+
Dtype v = out[n * channels * spatial_dim + index] / scale[n * spatial_dim + s];
185+
#ifdef LOG_SOFTMAX
186+
v = log(v);
187+
#endif
188+
out[n * channels * spatial_dim + index] = v;
181189
}
182190
}

0 commit comments

Comments
 (0)