Example #1
0
void cal_add_item_cpu<float>(const float co, const int N, const int K, const float* W, const float* x1, float* tempX1,
                             const float* x2, float* tempX2, const float gamma, float* KK) {

    memset(tempX1, 0, sizeof(float) * K);
    memset(tempX2, 0, sizeof(float) * K);

    caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x1, 0.0, tempX1);
    caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x2, 0.0, tempX2);

    float square_sum = 0;

    caffe_cpu_axpby(K, -1.0f, tempX2, 1.0f, tempX1);
    caffe_cpu_axpby(K, 1.0f, x1, 0.0f, tempX2);
    caffe_cpu_axpby(K, -1.0f, x2, 1.0f, tempX2);
    square_sum = caffe_cpu_dot<float>(K, tempX1, tempX1);

    //calculate 2 * \gamma * kernel
    float kernel = 0.0f;
    float tempGamma = gamma / 4.0f;
    for(int i = 0; i < 5; ++i) {
        float temp = (0.0 - tempGamma) * square_sum;
        temp = exp(temp);
        kernel += 2 * tempGamma* temp;
        tempGamma = tempGamma * 2;
    }
    /*float kernel = (0.0 - gamma) * square_sum;
    kernel = exp(kernel);
    kernel = 2 * gamma * kernel;
    */
    //calculate KK <- co * kernel * X^T * W + 1 * KK
    caffe_cpu_gemm<float>(CblasNoTrans, CblasTrans, 1, N, K, co*kernel, tempX2, W, 0.0, KK);
}
void TripletClsLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom){
         
  int num = bottom[0]->num();  
  int channels = bottom[0]->channels();

  for (int n = 0; n < num; ++n) {
    if (propagate_down[0]) {                                           
	    if(const_.cpu_data()[n] > Dtype(0.0)){
		
               //derivative in relation to image                
                 caffe_cpu_axpby(channels, Dtype( 2./num), diff_impostor_target.cpu_data() + bottom[0]->offset(n), Dtype(0.0),
                                bottom[0]->mutable_cpu_diff() + bottom[0]->offset(n));

		//derivative in relation to target                
                caffe_cpu_axpby(channels, Dtype( 2./num), diff_sample_target.cpu_data() + bottom[1]->offset(n), Dtype(0.0),
                                bottom[1]->mutable_cpu_diff() + bottom[1]->offset(n));                 

		//derivative in relation to impostor
                caffe_cpu_axpby(channels, Dtype( -2./num), diff_sample_impostor.cpu_data() + bottom[2]->offset(n), Dtype(0.0),
                                bottom[2]->mutable_cpu_diff() + bottom[2]->offset(n));     

		           
            }else{

		caffe_set(channels, Dtype(0.0), bottom[0]->mutable_cpu_diff() + bottom[0]->offset(n));
                caffe_set(channels, Dtype(0.0), bottom[1]->mutable_cpu_diff() + bottom[1]->offset(n));
                caffe_set(channels, Dtype(0.0), bottom[2]->mutable_cpu_diff() + bottom[2]->offset(n));

	    }
    }           
 }
}
void ContrastiveLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  Dtype margin = this->layer_param_.contrastive_loss_param().margin();
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      const Dtype alpha = sign * top[0]->cpu_diff()[0] /
          static_cast<Dtype>(bottom[i]->num());
      int num = bottom[i]->num();
      int channels = bottom[i]->channels();
      for (int j = 0; j < num; ++j) {
        Dtype* bout = bottom[i]->mutable_cpu_diff();
        if (static_cast<int>(bottom[2]->cpu_data()[j])) {  // similar pairs
          caffe_cpu_axpby(
              channels,
              alpha,
              diff_.cpu_data() + (j*channels),
              Dtype(0.0),
              bout + (j*channels));
        } else {  // dissimilar pairs
          if ((margin-dist_sq_.cpu_data()[j]) > Dtype(0.0)) {
            caffe_cpu_axpby(
                channels,
                -alpha,
                diff_.cpu_data() + (j*channels),
                Dtype(0.0),
                bout + (j*channels));
          } else {
            caffe_set(channels, Dtype(0), bout + (j*channels));
          }
        }
      }
    }
  }
}
Example #4
0
void NesterovSolver<Dtype>::ComputeUpdateValue(int param_id, Dtype rate) {
  CHECK(Caffe::root_solver());
  const vector<Blob<Dtype>*>& net_params = this->net_->learnable_params();
  const vector<float>& net_params_lr = this->net_->params_lr();
  Dtype momentum = this->param_.momentum();
  Dtype local_rate = rate * net_params_lr[param_id];
  switch (Caffe::mode()) {
  case Caffe::CPU: {
    // save history momentum for stepping back
    caffe_copy(net_params[param_id]->count(),
        this->history_[param_id]->cpu_data(),
        this->update_[param_id]->mutable_cpu_data());

    // update history
    caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
              net_params[param_id]->cpu_diff(), momentum,
              this->history_[param_id]->mutable_cpu_data());

    // compute update: step back then over step
    caffe_cpu_axpby(net_params[param_id]->count(), Dtype(1) + momentum,
        this->history_[param_id]->cpu_data(), -momentum,
        this->update_[param_id]->mutable_cpu_data());

    // copy
    caffe_copy(net_params[param_id]->count(),
        this->update_[param_id]->cpu_data(),
        net_params[param_id]->mutable_cpu_diff());
    break;
  }
  case Caffe::GPU: {
#ifndef CPU_ONLY
    // save history momentum for stepping back
    caffe_copy(net_params[param_id]->count(),
        this->history_[param_id]->gpu_data(),
        this->update_[param_id]->mutable_gpu_data());

    // update history
    caffe_gpu_axpby(net_params[param_id]->count(), local_rate,
              net_params[param_id]->gpu_diff(), momentum,
              this->history_[param_id]->mutable_gpu_data());

    // compute update: step back then over step
    caffe_gpu_axpby(net_params[param_id]->count(), Dtype(1) + momentum,
        this->history_[param_id]->gpu_data(), -momentum,
        this->update_[param_id]->mutable_gpu_data());

    // copy
    caffe_copy(net_params[param_id]->count(),
        this->update_[param_id]->gpu_data(),
        net_params[param_id]->mutable_gpu_diff());
#else
    NO_GPU;
#endif
    break;
  }
  default:
    LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
  }
}
void BinomialDevianceLossLayer<Dtype>::Forward_cpu(
    const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  n1 = 0;
  n2 = 0; 
  for (int i = 0; i < bottom[1]->num(); ++i){
    if (static_cast<int>(bottom[1]->cpu_data()[i]) == 1){
      n1++;
    }	
    else if (static_cast<int>(bottom[1]->cpu_data()[i]) == -1)	{
      n2++;
    }

  }


 // LOG(INFO) << n1 << "  " << n2;
  Dtype c = this->layer_param_.binomial_deviance_loss_param().c(); 
  for (int i = 0; i < bottom[1]->num(); ++i){
    M_.mutable_cpu_data()[i] = static_cast<int>(bottom[1]->cpu_data()[i]);
  
    if (static_cast<int>(bottom[1]->cpu_data()[i]) == 1)
      W_.mutable_cpu_data()[i] = 1.0/n1;
    else if (static_cast<int>(bottom[1]->cpu_data()[i]) == -1)	{
      W_.mutable_cpu_data()[i] = 1.0/n2;
      M_.mutable_cpu_data()[i] = -c;
    }
    else W_.mutable_cpu_data()[i] = 0.0;
  } 
  summer_vec_.Reshape(bottom[0]->num(), 1, 1, 1);
  for (int i = 0; i < bottom[0]->num(); ++i){
    exp_.mutable_cpu_data()[i] = Dtype(1);
    summer_vec_.mutable_cpu_data()[i] = Dtype(1);
  }

  Dtype alpha = this->layer_param_.binomial_deviance_loss_param().alpha(); 
  Dtype beta = this->layer_param_.binomial_deviance_loss_param().beta(); 

  caffe_cpu_axpby(
              bottom[1]->num(),
              Dtype(-alpha),
              bottom[0]->cpu_data(),
              Dtype(alpha * beta),
              exp_.mutable_cpu_data());

  caffe_mul(bottom[1]->num(), M_.cpu_data(), exp_.cpu_data(), exp_.mutable_cpu_data());
  caffe_exp(bottom[1]->num(), exp_.cpu_data(), exp_.mutable_cpu_data());
 
  caffe_cpu_axpby(bottom[1]->num(), Dtype(1), exp_.cpu_data(), Dtype(1), summer_vec_.mutable_cpu_data());
  for (int i = 0; i < bottom[0]->num(); ++i){
    summer_vec_.mutable_cpu_data()[i] = log(summer_vec_.cpu_data()[i]);
  }
//// multiply by elimination array
  caffe_mul(bottom[2]->num(), bottom[2]->cpu_data(), summer_vec_.cpu_data(), summer_vec_.mutable_cpu_data());
////
  Dtype loss = caffe_cpu_dot(bottom[1]->num(), W_.cpu_data(), summer_vec_.cpu_data());
  top[0]->mutable_cpu_data()[0] = loss;
}
void TripletLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
  const vector<Blob<Dtype>*>& top) {
  Dtype eps = this->layer_param_.triplet_loss_param().eps();
  Dtype loss = 0;
  Dtype margin = this->layer_param_.triplet_loss_param().margin();
  caffe_cpu_gemm(CblasNoTrans, CblasTrans, sample_num_, sample_num_,
      feature_dim_, Dtype(1), bottom[0]->cpu_data(),
      bottom[0]->cpu_data(), Dtype(0),
      inner_matrix_.mutable_cpu_data());

  for (int i = 0; i < triplet_num_; ++i) {
    int a_idx = bottom[1]->cpu_data()[i * 3];
    int p_idx = bottom[1]->cpu_data()[i * 3 + 1];
    int n_idx = bottom[1]->cpu_data()[i * 3 + 2];
    const Dtype *a_pointer = bottom[0]->cpu_data() + a_idx * feature_dim_;
    const Dtype *p_pointer = bottom[0]->cpu_data() + p_idx * feature_dim_;
    const Dtype *n_pointer = bottom[0]->cpu_data() + n_idx * feature_dim_;
    const Dtype *inner_matrix_data = inner_matrix_.cpu_data();
    Dtype a_norm = sqrt(inner_matrix_data[a_idx * sample_num_ + a_idx] + eps);
    Dtype p_norm = sqrt(inner_matrix_data[p_idx * sample_num_ + p_idx] + eps);
    Dtype n_norm = sqrt(inner_matrix_data[n_idx * sample_num_ + n_idx] + eps);
    Dtype inner_ap = inner_matrix_data[a_idx * sample_num_ + p_idx];
    Dtype inner_an = inner_matrix_data[a_idx * sample_num_ + n_idx];
    Dtype dist_ap = inner_ap / (a_norm * p_norm);
    Dtype dist_an = inner_an / (a_norm * n_norm);
    if (dist_ap - dist_an - margin < 0) {
      ComputeDiff_cpu(a_pointer, p_pointer, a_norm,
          p_norm, inner_ap, diff_ap_.mutable_cpu_data());
      ComputeDiff_cpu(a_pointer, n_pointer, a_norm,
          n_norm, inner_an, diff_an_.mutable_cpu_data());
      ComputeDiff_cpu(p_pointer, a_pointer, p_norm,
          a_norm, inner_ap, diff_pa_.mutable_cpu_data());
      ComputeDiff_cpu(n_pointer, a_pointer, n_norm,
          a_norm, inner_an, diff_na_.mutable_cpu_data());

      caffe_cpu_axpby(feature_dim_, Dtype(1),
          diff_an_.cpu_data(), Dtype(1),
          bottom_diff_.mutable_cpu_data() + (a_idx * feature_dim_));
      caffe_cpu_axpby(feature_dim_, Dtype(-1),
          diff_ap_.cpu_data(), Dtype(1),
          bottom_diff_.mutable_cpu_data() + (a_idx * feature_dim_));
      caffe_cpu_axpby(feature_dim_, Dtype(-1),
          diff_pa_.cpu_data(), Dtype(1),
          bottom_diff_.mutable_cpu_data() + (p_idx * feature_dim_));
      caffe_cpu_axpby(feature_dim_, Dtype(1),
          diff_na_.cpu_data(), Dtype(1),
          bottom_diff_.mutable_cpu_data() + (n_idx * feature_dim_));

      loss += dist_an + margin - dist_ap;
    }
  }
  //Dtype scalar = Dtype(1) / triplet_num_;
  Dtype scalar = Dtype(1) / sample_num_;
  top[0]->mutable_cpu_data()[0] = loss * scalar;
}
Example #7
0
void RMSPropSolver<Dtype>::ComputeUpdateValue(int param_id, Dtype rate) {
  const vector<Blob<Dtype>*>& net_params = this->net_->learnable_params();
  const vector<float>& net_params_lr = this->net_->params_lr();

  // get the learning rate
  Dtype delta = this->param_.delta();
  Dtype rms_decay = this->param_.rms_decay();
  Dtype local_rate = rate * net_params_lr[param_id];

  switch (Caffe::mode()) {
  case Caffe::CPU:
    // compute square of gradient in update
    caffe_powx(net_params[param_id]->count(),
        net_params[param_id]->cpu_diff(), Dtype(2),
        this->update_[param_id]->mutable_cpu_data());

    // update history
    caffe_cpu_axpby(net_params[param_id] -> count(),
        Dtype(1-rms_decay), this->update_[param_id]->cpu_data(),
        rms_decay, this->history_[param_id]-> mutable_cpu_data());

    // prepare update
    caffe_powx(net_params[param_id]->count(),
        this->history_[param_id]->cpu_data(), Dtype(0.5),
        this->update_[param_id]->mutable_cpu_data());

    caffe_add_scalar(net_params[param_id]->count(),
        delta, this->update_[param_id]->mutable_cpu_data());

    caffe_div(net_params[param_id]->count(),
        net_params[param_id]->cpu_diff(), this->update_[param_id]->cpu_data(),
        this->update_[param_id]->mutable_cpu_data());

    // scale and copy
    caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
        this->update_[param_id]->cpu_data(), Dtype(0),
        net_params[param_id]->mutable_cpu_diff());
    break;
  case Caffe::GPU:
#ifndef CPU_ONLY
    rmsprop_update_gpu(net_params[param_id]->count(),
        net_params[param_id]->mutable_gpu_diff(),
        this->history_[param_id]->mutable_gpu_data(),
        rms_decay, delta, local_rate);
#else
    NO_GPU;
#endif
    break;
  default:
    LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
  }
}
void TripletLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,  
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {  
  //Dtype margin = this->layer_param_.contrastive_loss_param().margin();  
  const Dtype* sampleW = bottom[3]->cpu_data();  
  for (int i = 0; i < 3; ++i) {  
    if (propagate_down[i]) {  
      const Dtype sign = (i < 2) ? -1 : 1;  
      const Dtype alpha = sign * top[0]->cpu_diff()[0] /  
          static_cast<Dtype>(bottom[i]->num());  
      int num = bottom[i]->num();  
      int channels = bottom[i]->channels();  
      for (int j = 0; j < num; ++j) {  
        Dtype* bout = bottom[i]->mutable_cpu_diff();  
        if (i==0) {  // a  
          //if(dist_binary_.cpu_data()[j]>Dtype(0)){  
              caffe_cpu_axpby(  
                  channels,  
                  alpha*sampleW[j],  
                  diff_pn_.cpu_data() + (j*channels),  
                  Dtype(0.0),  
                  bout + (j*channels));  
          //}else{  
          //  caffe_set(channels, Dtype(0), bout + (j*channels));  
          //}  
        } else if (i==1) {  // p  
          //if(dist_binary_.cpu_data()[j]>Dtype(0)){  
              caffe_cpu_axpby(  
                  channels,  
                  alpha*sampleW[j],  
                  diff_ap_.cpu_data() + (j*channels),  
                  Dtype(0.0),  
                  bout + (j*channels));  
          //}else{  
          //      caffe_set(channels, Dtype(0), bout + (j*channels));  
          //}  
        } else if (i==2) {  // n  
          //if(dist_binary_.cpu_data()[j]>Dtype(0)){  
              caffe_cpu_axpby(  
                  channels,  
                  alpha*sampleW[j],  
                  diff_an_.cpu_data() + (j*channels),  
                  Dtype(0.0),  
                  bout + (j*channels));  
           //}else{  
           //   caffe_set(channels, Dtype(0), bout + (j*channels));  
           //}  
        }  
      } // for num  
    } //if propagate_down[i]  
  } //for i  
} 
void CosineSimilarityLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      int num = bottom[i]->num();
      int channels = bottom[i]->channels();
      for (int j = 0; j < num; ++j) {
            Dtype denominator = pow(xx_.cpu_data()[j]*yy_.cpu_data()[j], 0.5);  
            Dtype* bout = bottom[i]->mutable_cpu_diff();
            if (i == 0){
	      caffe_cpu_axpby(
	        channels,
                Dtype(-xy_.cpu_data()[j] /(denominator* xx_.cpu_data()[j])),
                bottom[0]->cpu_data() + (j*channels),
                Dtype(0.0),
                bout + (j*channels));

              caffe_cpu_axpby(
              channels,
              Dtype(1.0/denominator),
              bottom[1]->cpu_data() + (j*channels),
              Dtype(1.0),
              bout + (j*channels));
            } else if (i == 1){
	      caffe_cpu_axpby(
	        channels,
                Dtype(-xy_.cpu_data()[j] /(denominator*yy_.cpu_data()[j])),
	        bottom[1]->cpu_data() + (j*channels),
  	        Dtype(0.0),
	        bout + (j*channels));
              caffe_cpu_axpby(
	        channels,
	        Dtype(1.0/denominator),
	        bottom[0]->cpu_data() + (j*channels),
	        Dtype(1.0),
	        bout + (j*channels));	
            }
    
            caffe_cpu_axpby(
              channels,
              Dtype(0.0),
              bout + (j*channels),
              top[0]->cpu_diff()[j],
              bout + (j*channels)
            );
      }
    }
  }

}
Example #10
0
float cal_gamma_cpu<float>(const int K, const int S, const int N, const float* W, const float* X, float* tempX1, float* tempX2) {
    srand((unsigned int)time(0));
    float gamma = 0;
    float temp = 0;

    //random sample S pair to calculate gamma
    for(int i = 0; i < S; ++i) {
        memset(tempX1, 0, sizeof(float) * K);
        memset(tempX2, 0, sizeof(float) * K);
        int s1 = rand() % S;
        int s2 = rand() % S;
        s2 = (s1 != s2) ? s2 : (s2 + 1) % S;

        const float* x1 = X + s1 * K;
        const float* x2 = X + s2 * K;

        caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x1, 0.0, tempX1);
        caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x2, 0.0, tempX2);

        //caffe_cpu_sub<float>(K, tempX1, tempX2, tempX2);
        caffe_cpu_axpby(K, 1.0f, tempX1, -1.0f, tempX2);
        temp = caffe_cpu_dot<float>(K, tempX2, tempX2);
        gamma += temp;
    }
    return S / gamma;
}
Example #11
0
void SGDSolver<Dtype>::ComputeUpdateValue(int param_id, Dtype rate) {
  const vector<Blob<Dtype>*>& net_params = this->net_->learnable_params();
  const vector<float>& net_params_lr = this->net_->params_lr();
  Dtype momentum = this->param_.momentum();
  Dtype local_rate = rate * net_params_lr[param_id];
  // Compute the update to history, then copy it to the parameter diff.
  switch (Caffe::mode()) {
  case Caffe::CPU: {
    caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
              net_params[param_id]->cpu_diff(), momentum,
              history_[param_id]->mutable_cpu_data());
    caffe_copy(net_params[param_id]->count(),
        history_[param_id]->cpu_data(),
        net_params[param_id]->mutable_cpu_diff());
    break;
  }
  case Caffe::GPU: {
#ifndef CPU_ONLY
    sgd_update_gpu(net_params[param_id]->count(),
        net_params[param_id]->mutable_gpu_diff(),
        history_[param_id]->mutable_gpu_data(),
        momentum, local_rate);
#else
    NO_GPU;
#endif
    break;
  }
  default:
    LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
  }
}
void ContrastiveLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  Dtype margin = this->layer_param_.contrastive_loss_param().margin();
  bool legacy_version =
      this->layer_param_.contrastive_loss_param().legacy_version();
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      const Dtype alpha = sign * top[0]->cpu_diff()[0] /
          static_cast<Dtype>(bottom[i]->num());
      int num = bottom[i]->num();
      int channels = bottom[i]->channels();
      for (int j = 0; j < num; ++j) {
        Dtype* bout = bottom[i]->mutable_cpu_diff();
        if (static_cast<int>(bottom[2]->cpu_data()[j]) == static_cast<int>(bottom[3]->cpu_data()[j])) {  // similar pairs
          caffe_cpu_axpby(
              channels,
              alpha,
              diff_.cpu_data() + (j*channels),
              Dtype(0.0),
              bout + (j*channels));
        } else {  // dissimilar pairs
          Dtype mdist(0.0);
          Dtype beta(0.0);
          if (legacy_version) {
            mdist = margin - dist_sq_.cpu_data()[j];
            beta = -alpha;
          } else {
            Dtype dist = sqrt(dist_sq_.cpu_data()[j]);
            mdist = margin - dist;
            beta = -alpha * mdist / (dist + Dtype(1e-4));
          }
          if (mdist > Dtype(0.0)) {
            caffe_cpu_axpby(
                channels,
                beta,
                diff_.cpu_data() + (j*channels),
                Dtype(0.0),
                bout + (j*channels));
          } else {
            caffe_set(channels, Dtype(0), bout + (j*channels));
          }
        }
      }
    }
  }
}
void EuclideanLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
  int count = (*bottom)[0]->count();
  int num = (*bottom)[0]->num();
  // Compute the gradient
  caffe_cpu_axpby(count, Dtype(1) / num, difference_.cpu_data(), Dtype(0),
      (*bottom)[0]->mutable_cpu_diff());
}
void SGDFeedbackSolver<Dtype>::ComputeUpdateValue() {
  vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
  vector<float>& net_params_lr = this->net_->params_lr();
  vector<float>& net_params_weight_decay = this->net_->params_weight_decay();
  // get the learning rate
  Dtype rate = GetLearningRate();
  if (this->param_.display() && this->iter_ % this->param_.display() == 0) {
    LOG(INFO) << "Iteration " << this->iter_ << ", lr = " << rate;
  }
  Dtype momentum = this->param_.momentum();
  Dtype weight_decay = this->param_.weight_decay();
  switch (Caffe::mode()) {
  case Caffe::CPU:
    for (int param_id = 0; param_id < net_params.size(); ++param_id) {
      // Compute the value to history, and then copy them to the blob's diff.
      Dtype local_rate = rate * net_params_lr[param_id];
      Dtype local_decay = weight_decay * net_params_weight_decay[param_id];
      caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
          net_params[param_id]->cpu_diff(), momentum,
          history_[param_id]->mutable_cpu_data());
      if (local_decay) {
        // add weight decay
        caffe_axpy(net_params[param_id]->count(),
            local_decay * local_rate,
            net_params[param_id]->cpu_data(),
            history_[param_id]->mutable_cpu_data());
      }
      // copy
      caffe_copy(net_params[param_id]->count(),
          history_[param_id]->cpu_data(),
          net_params[param_id]->mutable_cpu_diff());
    }
    break;
  case Caffe::GPU:
    for (int param_id = 0; param_id < net_params.size(); ++param_id) {
      // Compute the value to history, and then copy them to the blob's diff.
      Dtype local_rate = rate * net_params_lr[param_id];
      Dtype local_decay = weight_decay * net_params_weight_decay[param_id];
      caffe_gpu_axpby(net_params[param_id]->count(), local_rate,
          net_params[param_id]->gpu_diff(), momentum,
          history_[param_id]->mutable_gpu_data());
      if (local_decay) {
        // add weight decay
        caffe_gpu_axpy(net_params[param_id]->count(),
            local_decay * local_rate,
            net_params[param_id]->gpu_data(),
            history_[param_id]->mutable_gpu_data());
      }
      // copy
      caffe_gpu_copy(net_params[param_id]->count(),
          history_[param_id]->gpu_data(),
          net_params[param_id]->mutable_gpu_diff());
    }
    break;
  default:
    LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
  }
}
Example #15
0
void VoxelCustomLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
        const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
    caffe_cpu_axpby(
        (*bottom)[0]->count(),              // count
        Dtype(1) / (*bottom)[0]->num(),     // alpha
        diff_.cpu_data(),                   // a
        Dtype(0),                           // beta
        (*bottom)[0]->mutable_cpu_diff());  // b
}
void TripletLossLayer<Dtype>::ComputeDiff_cpu(const Dtype *x_1,
  const Dtype *x_2, const Dtype x_1_norm, const Dtype x_2_norm,
  const Dtype inner_val, Dtype *x_1_diff) {
  caffe_cpu_scale(feature_dim_, Dtype(1) / (x_1_norm * x_2_norm),
      x_2, x_1_diff);
  Dtype x_1_norm_cubic = x_1_norm * x_1_norm * x_1_norm;
  caffe_cpu_axpby(feature_dim_, -inner_val / (x_1_norm_cubic * x_2_norm),
      x_1, Dtype(1), x_1_diff);
}
Example #17
0
void PowerLayer<Dtype>::Backward_cpu(
    const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down,
    const vector<Blob<Dtype>*>& bottom) {
  if (propagate_down[0]) {
    Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
    const int count = bottom[0]->count();
    const Dtype* top_diff = top[0]->cpu_diff();
    if (diff_scale_ == Dtype(0) || power_ == Dtype(1)) {
      caffe_set(count, diff_scale_, bottom_diff);
    } else {
      const Dtype* bottom_data = bottom[0]->cpu_data();
      // Compute dy/dx = scale * power * (shift + scale * x)^(power - 1)
      //               = diff_scale * y / (shift + scale * x)
      if (power_ == Dtype(2)) {
        // Special case for y = (shift + scale * x)^2
        //     -> dy/dx = 2 * scale * (shift + scale * x)
        //              = diff_scale * shift + diff_scale * scale * x
        caffe_cpu_axpby(
            count,
            diff_scale_ * scale_,
            bottom_data,
            Dtype(0),
            bottom_diff);

        if (shift_ != Dtype(0)) {
          caffe_add_scalar(count, diff_scale_ * shift_, bottom_diff);
        }
      } else if (shift_ == Dtype(0)) {
        // Special case for y = (scale * x)^power
        //     -> dy/dx = scale * power * (scale * x)^(power - 1)
        //              = scale * power * (scale * x)^power * (scale * x)^(-1)
        //              = power * y / x
        const Dtype* top_data = top[0]->cpu_data();
        caffe_div(count, top_data, bottom_data, bottom_diff);
        caffe_scal(count, power_, bottom_diff);
      } else {
        caffe_copy(count, bottom_data, bottom_diff);
        if (scale_ != Dtype(1)) {
          caffe_scal(count, scale_, bottom_diff);
        }
        if (shift_ != Dtype(0)) {
          caffe_add_scalar(count, shift_, bottom_diff);
        }
        const Dtype* top_data = top[0]->cpu_data();
        caffe_div<Dtype>(count, top_data, bottom_diff, bottom_diff);
        if (diff_scale_ != Dtype(1)) {
          caffe_scal(count, diff_scale_, bottom_diff);
        }
      }
    }
    if (diff_scale_ != Dtype(0)) {
      caffe_mul(count, top_diff, bottom_diff, bottom_diff);
    }
  }
}
Example #18
0
	void TripletLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, 
		const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
//		const Dtype* sampleW = bottom[3]->cpu_data();
		//for 3 bottom data
		for(int i = 0; i < 3; ++i)
		{
			if(propagate_down[i]){
				const Dtype sign = (i < 2) ? -1: 1;
				const Dtype alpha = sign * top[0]->cpu_diff()[0] / static_cast<Dtype>(bottom[i]->num());
				int num = bottom[i]->num();
				int channels = bottom[i]->channels();
				for(int j = 0; j < num; ++j)//for each image
				{
					Dtype* bout = bottom[i]->mutable_cpu_diff();
					if(i == 0){//anchor
						caffe_cpu_axpby(
							channels, 
							alpha,// * sampleW[j], 
							diff_pn_.cpu_data() + (j * channels),
							Dtype(0.0),
							bout + (j*channels)
							);	
					}else if(i == 1) { //positive
						caffe_cpu_axpby(
							channels,
							alpha, //*sampleW[j],
							diff_ap_.cpu_data() + (j*channels),
							Dtype(0.0),
							bout + (j*channels)
							);
					}else if(i==2){ //negative
						caffe_cpu_axpby(
							channels, 
							alpha, // * sampleW[j],
							diff_an_.cpu_data() + (j*channels),
							Dtype(0.0),
							bout + (j*channels)
							);
					}
				}
			}
		}
	}
void EuclideanLossWithIgnoreLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
	int count  = bottom[0]->count();
	//Number of elements in the batch
	int bCount  = bottom[0]->count(1, bottom[0]->num_axes());
	int lbCount = bottom[1]->count(1, bottom[1]->num_axes());
	int N      = bottom[0]->shape(0);
	if (lCount_ == Dtype(0)){
		LOG(INFO) << "EuclideanLossWithIgnore was Silent for this batch";
		return;
	}
	//Compute the gradients
	for (int i = 0; i < 2; ++i) {
		const Dtype sign = (i == 0) ? 1 : -1;
		const Dtype alpha = sign * top[0]->cpu_diff()[0] / lCount_;
		Dtype Z;
		const Dtype* botZData = bottom[nc_]->cpu_data();
		Dtype* botDiff       = bottom[i]->mutable_cpu_diff(); 
		const Dtype* labels  = bottom[1]->cpu_data();       
		Dtype* diff          = diff_.mutable_cpu_data();
		const Dtype* diffC   = diff_.cpu_data();
		if (propagate_down[i]) {
			for (int n=0; n < N; ++n){
				if (labels[bCount] == Dtype(1)){ 
					if (is_normalize_){
						Z = caffe_cpu_dot(bCount, botZData, botZData);
						if (Z>0){
							caffe_scal(count, Z, diff);
						}
					}

				caffe_cpu_axpby(
						bCount,              // count
						alpha,               // alpha
						diffC,               // a
						Dtype(0),                           // beta
						botDiff);  // b
				}
				labels += lbCount;
				diff   += bCount;
				diffC  += bCount;
				if (nc_==0){
					botZData += bCount;
				}else{
					botZData += lbCount;
				}
				if (i==0){
					botDiff  += bCount;
				}else {
					botDiff  += lbCount; 
				} 
			}
		}
	}
}
Example #20
0
void SumLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  const Dtype* bottom_data = bottom[0]->cpu_data();
  Dtype* top_data = top[0]->mutable_cpu_data();
  caffe_copy(bottom[0]->count(), bottom_data, top_data);
  for (int i = 1; i < bottom.size(); ++i) {
    const Dtype* bottom_data_i = bottom[i]->cpu_data();
    caffe_cpu_axpby(bottom[0]->count(), Dtype(1.0), bottom_data_i,
       Dtype(1.0), top_data); 
  }
}
void CoupledClusterLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
        const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
    /* loss_weight */
    const Dtype alpha = top[0]->cpu_diff()[0]/group_num;
    CHECK_EQ(feat_len, bottom[0]->channels());
    CHECK_EQ(N*group_num*feat_len, bottom[0]->count());
    if(propagate_down[0]) {
        Dtype *bottom_diff = bottom[0]->mutable_cpu_diff();
        caffe_set(N*group_num*feat_len, Dtype(0), bottom_diff);
        for(int i=0; i<group_num; ++i) {
            for(int j=0; j<N; ++j) {
                if(pos_backward[i*N+j])
                    /* for positive samples */
                    caffe_cpu_axpby(feat_len, scale*alpha, diff_.cpu_data()+feat_len*(i*N+j), Dtype(0), bottom_diff+feat_len*(i*N+j));
                else if(neg_backward[i*N+j]) {
                    /* for hard negative sample */
                    caffe_cpu_axpby(feat_len, -scale*alpha, diff_.cpu_data()+feat_len*(i*N+j), Dtype(0), bottom_diff+feat_len*(i*N+j));
                }
            }
        }
    }
}
Example #22
0
void MVNLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down,
    const vector<Blob<Dtype>*>& bottom) {
  const Dtype* top_diff = top[0]->cpu_diff();
  const Dtype* top_data = top[0]->cpu_data();
  const Dtype* bottom_data = bottom[0]->cpu_data();
  Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();

  int num;
  if (this->layer_param_.mvn_param().across_channels())
    num = bottom[0]->num();
  else
    num = bottom[0]->num() * bottom[0]->channels();

  int dim = bottom[0]->count() / num;

  if (this->layer_param_.mvn_param().normalize_variance()) {
    caffe_mul(temp_.count(), top_data, top_diff, bottom_diff);
    caffe_cpu_gemv<Dtype>(CblasNoTrans, num, dim, 1., bottom_diff,
          sum_multiplier_.cpu_data(), 0., mean_.mutable_cpu_data());
    caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, 1.,
          mean_.cpu_data(), sum_multiplier_.cpu_data(), 0.,
          bottom_diff);
    caffe_mul(temp_.count(), top_data, bottom_diff, bottom_diff);

    caffe_cpu_gemv<Dtype>(CblasNoTrans, num, dim, 1., top_diff,
            sum_multiplier_.cpu_data(), 0., mean_.mutable_cpu_data());
    caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, 1.,
            mean_.cpu_data(), sum_multiplier_.cpu_data(), 1.,
            bottom_diff);

    caffe_cpu_axpby(temp_.count(), Dtype(1), top_diff, Dtype(-1. / dim),
        bottom_diff);

    // put the squares of bottom into temp_
    caffe_powx(temp_.count(), bottom_data, Dtype(2),
        temp_.mutable_cpu_data());
    caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, 1.,
        variance_.cpu_data(), sum_multiplier_.cpu_data(), 0.,
        temp_.mutable_cpu_data());

    caffe_div(temp_.count(), bottom_diff, temp_.cpu_data(), bottom_diff);
  } else {
    caffe_cpu_gemv<Dtype>(CblasNoTrans, num, dim, 1. / dim, top_diff,
      sum_multiplier_.cpu_data(), 0., mean_.mutable_cpu_data());
    caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, -1.,
      mean_.cpu_data(), sum_multiplier_.cpu_data(), 0.,
      temp_.mutable_cpu_data());
    caffe_add(temp_.count(), top_diff, temp_.cpu_data(), bottom_diff);
  }
}
void BinomialDevianceLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {

  Dtype alpha = this->layer_param_.binomial_deviance_loss_param().alpha(); 
 
  if (propagate_down[0]) { 
    Dtype* bout = bottom[0]->mutable_cpu_diff();

    int num = bottom[0]->num();
    for (int i = 0 ; i < num; i++){
      bout[i] = Dtype(1.0); 
    }
    caffe_cpu_axpby(bottom[0]->num(), Dtype(1), exp_.cpu_data(), Dtype(1), bout);
    caffe_div(bottom[0]->num(), exp_.cpu_data(), bout, bout);
    caffe_mul(bottom[0]->num(), M_.cpu_data(), bout, bout);
    caffe_mul(bottom[0]->num(), W_.cpu_data(), bout, bout);   
    caffe_cpu_axpby(bottom[0]->num(), Dtype(0.0), bout, Dtype(-alpha * top[0]->cpu_diff()[0]), bout);

    //// multiply by elimination array
    caffe_mul(bottom[2]->num(), bottom[2]->cpu_data(), bottom[0]->cpu_diff(), bout);
////
  }
}
Example #24
0
void L2NormLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  const Dtype* top_diff = top[0]->cpu_diff();
  const Dtype* top_data = top[0]->cpu_data();
  const Dtype* norm_scale = norm_.cpu_data();
  Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
  const int n = top[0]->num();
  const int d = top[0]->count() / n;
  caffe_copy(bottom[0]->count(), top_diff, bottom_diff);
  for (int i=0; i<n; ++i) {
    Dtype a = caffe_cpu_dot(d, top_data+i*d, top_diff+i*d);
    caffe_cpu_axpby(d, Dtype(-1) * a * norm_scale[i], top_data + i*d, norm_scale[i], bottom_diff + i*d);
  }
}
void EuclideanLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom) {
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      caffe_cpu_axpby(
          (*bottom)[i]->count(),              // count
          sign / (*bottom)[i]->num(),         // alpha
          diff_.cpu_data(),                   // a
          Dtype(0),                           // beta
          (*bottom)[i]->mutable_cpu_diff());  // b
    }
  }
}
Example #26
0
void AbsdistLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num();
      caffe_cpu_axpby(
          bottom[i]->count(),              // count
          alpha,                              // alpha
          diff_.cpu_data(),                   // a
          Dtype(0),                           // beta
          bottom[i]->mutable_cpu_diff());  // b
    }
  }
}
void EuclideanDistLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  int count = bottom[0]->count();
  int num = bottom[0]->num();
  int dim = sum_multiplier_.count();
  caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1,
      1., top[0]->cpu_diff(), sum_multiplier_.cpu_data(),
      0., temp_.mutable_cpu_data());
  caffe_mul(count, temp_.cpu_data(), diff_.cpu_data(), diff_.mutable_cpu_data());
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      caffe_cpu_axpby(
          bottom[i]->count(),              // count
          sign,                              // alpha
          diff_.cpu_data(),                   // a
          Dtype(0),                           // beta
          bottom[i]->mutable_cpu_diff());  // b
    }
  }
}
Example #28
0
void RmseLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom) {
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      const Dtype alpha = sign * top[0]->cpu_diff()[0] / num_rating_;  // top[0]->cpu_diff()[0] is set to 1 by net that's the total loss of all instance.
      caffe_cpu_axpby(
          num_rating_,                        // num_rating_
          alpha,                              // alpha
          diff_.cpu_data(),                   // a
          Dtype(0),                           // beta
          (*bottom)[i]->mutable_cpu_diff());  // b

      
      // const Dtype* diff_cpu = (*bottom)[i]->cpu_diff();
      // std::cout << "diff in loss" << std::endl;
      // for (int i = 0; i < 10; i++){
      //   std::cout << diff_cpu[i] << "\t";
      // }
      // std::cout << std::endl;
    } // ~ propagate_down[i]
  }
}
void EuclideanLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      const Dtype alpha = sign * top[0]->cpu_diff()[0] / (bottom)[i]->num();
			Dtype nrmlz       = 1.0;      
			int count = (bottom)[0]->count();
			if (is_normalize_){
				nrmlz = caffe_cpu_dot(count, (bottom)[nc_]->cpu_data(), (bottom)[nc_]->cpu_data());
				if (nrmlz>0){
					caffe_scal(count, Dtype(1)/nrmlz, diff_.mutable_cpu_data());
				}
			}

			caffe_cpu_axpby(
          (bottom)[i]->count(),              // count
          alpha,                              // alpha
          diff_.cpu_data(),                    // a
          Dtype(0),                           // beta
          bottom[i]->mutable_cpu_diff());  // b
    }
  }
}
Example #30
0
void MaxMarginLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
		const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
	Dtype margin = this->layer_param_.max_margin_loss_param().margin();
	for (int i = 0; i < 2; ++i) {
		if (propagate_down[i]) {
			const Dtype sign = (i == 0) ? 1 : -1;
			const Dtype alpha = sign * top[0]->cpu_diff()[0] /
					static_cast<Dtype>(bottom[i]->num());
			int num = bottom[i]->num();
			int channels = bottom[i]->channels();
			for (int j = 0; j < num; ++j) {
				Dtype* bout = bottom[i]->mutable_cpu_diff();
				Dtype yij;
				if (static_cast<int>(bottom[2]->cpu_data()[j])) {  // similar pairs
					yij = 1;
				}
				else {  // dissimilar pairs
					yij = -1;
				}
				if (yij * (b - dist_sq_.cpu_data()[j]) <= margin) {
					Dtype scalar = alpha * (2.0) * yij;
					b += alpha * yij * c;
					caffe_cpu_axpby(
							channels,
							scalar,
							diff_.cpu_data() + (j*channels),
							Dtype(0.0),
							bout + (j*channels));
				}
				else {
					caffe_set(channels, Dtype(0), bout + (j*channels));
				}
			}
		}
	}
}