Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check labels in SoftmaxWithLoss #3043

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/caffe/loss_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,12 @@ class SoftmaxWithLossLayer : public LossLayer<Dtype> {
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

/**
* @brief Checks that all labels are within the correct range.
*
* @param label CPU pointer to labels
*/
void CheckLabels(const Dtype* label);

/// The internal SoftmaxLayer used to map predictions to a distribution.
shared_ptr<Layer<Dtype> > softmax_layer_;
Expand Down
22 changes: 20 additions & 2 deletions src/caffe/layers/softmax_loss_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void SoftmaxWithLossLayer<Dtype>::Reshape(
template <typename Dtype>
void SoftmaxWithLossLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
CheckLabels(bottom[1]->cpu_data());
// The forward pass computes the softmax prob values.
softmax_layer_->Forward(softmax_bottom_vec_, softmax_top_vec_);
const Dtype* prob_data = prob_.cpu_data();
Expand All @@ -66,8 +67,6 @@ void SoftmaxWithLossLayer<Dtype>::Forward_cpu(
if (has_ignore_label_ && label_value == ignore_label_) {
continue;
}
DCHECK_GE(label_value, 0);
DCHECK_LT(label_value, prob_.shape(softmax_axis_));
loss -= log(std::max(prob_data[i * dim + label_value * inner_num_ + j],
Dtype(FLT_MIN)));
++count;
Expand Down Expand Up @@ -120,6 +119,25 @@ void SoftmaxWithLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
}
}

template <typename Dtype>
void SoftmaxWithLossLayer<Dtype>::CheckLabels(const Dtype* label) {
const int nlabels = prob_.shape(softmax_axis_);
for (int i = 0; i < outer_num_; ++i) {
for (int j = 0; j < inner_num_; j++) {
const int label_value = static_cast<int>(label[i * inner_num_ + j]);
if (has_ignore_label_ && label_value == ignore_label_) {
continue;
}
CHECK_GE(label_value, 0);
CHECK_LT(label_value, nlabels) << "Invalid label value: the ground "
<< "truth labels contain the value " << label_value << ", but "
<< "the prediction has only " << nlabels << " channels (axis "
<< softmax_axis_ << "). Either your labels are incorrect, "
<< "or you have too many channels in your prediction.";
}
}
}

#ifdef CPU_ONLY
STUB_GPU(SoftmaxWithLossLayer);
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/caffe/layers/softmax_loss_layer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ __global__ void SoftmaxLossForwardGPU(const int nthreads,
template <typename Dtype>
void SoftmaxWithLossLayer<Dtype>::Forward_gpu(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
// Since the labels are already on the CPU, there is no transfer by calling
// cpu_data here.
CheckLabels(bottom[1]->cpu_data());
softmax_layer_->Forward(softmax_bottom_vec_, softmax_top_vec_);
const Dtype* prob_data = prob_.gpu_data();
const Dtype* label = bottom[1]->gpu_data();
Expand Down