Exemplo n.º 1
0
void LocalLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {

  Dtype* x_data = col_buffer_.mutable_cpu_data();
  const Dtype* weight = this->blobs_[0]->cpu_data();
  const Dtype* bottom_data = bottom[0]->cpu_data();
  Dtype* top_data = top[0]->mutable_cpu_data();

  Blob<Dtype> E;
  E.Reshape(1, 1, 1, K_);
  FillerParameter filler_param;
  filler_param.set_value(1);
  ConstantFiller<Dtype> filler(filler_param);
  filler.Fill(&E);

  Blob<Dtype> intermediate;
  intermediate.Reshape(1, 1, K_, N_);
  for (int n=0; n<num_; n++) {
    im2col_cpu(bottom_data + bottom[0]->offset(n), channels_, height_,
               width_, kernel_size_, kernel_size_, pad_, pad_, stride_, stride_, x_data);

    for (int m=0; m<num_output_; m++) { 
      caffe_mul(K_*N_, x_data, weight+this->blobs_[0]->offset(m),
                intermediate.mutable_cpu_data());

      caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, 1, N_, K_,
                            (Dtype)1., E.cpu_data(),
                            intermediate.cpu_data(),
                            (Dtype)0., top_data + top[0]->offset(n, m));
    }

    if (bias_term_) {
      caffe_add(M_ * N_, this->blobs_[1]->cpu_data(),
                top_data + top[0]->offset(n),
                top_data + top[0]->offset(n));
    }
  }
}
Exemplo n.º 2
0
// Usage: caffe_('blob_reshape', hBlob, new_shape)
static void blob_reshape(MEX_ARGS) {
	mxCHECK(nrhs == 2 && mxIsStruct(prhs[0]) && mxIsDouble(prhs[1]),
		"Usage: caffe_('blob_reshape', hBlob, new_shape)");
	Blob<float>* blob = handle_to_ptr<Blob<float> >(prhs[0]);
	const mxArray* mx_shape = prhs[1];
	double* shape_mem_mtr = mxGetPr(mx_shape);
	const int num_axes = mxGetNumberOfElements(mx_shape);
	vector<int> blob_shape(num_axes);
	for (int blob_axis = 0, mat_axis = num_axes - 1; blob_axis < num_axes;
		++blob_axis, --mat_axis) {
		blob_shape[blob_axis] = static_cast<int>(shape_mem_mtr[mat_axis]);
	}
	blob->Reshape(blob_shape);
}
Exemplo n.º 3
0
static void read_blob_from_file(const std::string& file_name,
        Blob<Dtype>& blob) {
  std::ifstream file(file_name.c_str(), std::ifstream::binary);
  if (file.fail()) {
      ASSERT_FALSE(true);
      return;
  }
  vector<int> shape(4, 0);
  file.read(reinterpret_cast<char*>(&shape[0]), 4 * sizeof(int));
  ASSERT_FALSE(file.fail());
  blob.Reshape(shape);
  file.read(reinterpret_cast<char*>(blob.mutable_cpu_data()),
          blob.count() * sizeof(Dtype));
  ASSERT_FALSE(file.fail());
}
Exemplo n.º 4
0
void CaffeMobile::putImage(AndroidBitmapInfo* info, void* pixels, const vector<Blob<float>*>& resImage) {
	Blob<float> * srcBlob = *resImage.data();

	LOG(DEBUG) << "srcBlob received";

	vector<int> shape = {1, 3, (int) info->width, (int) info->height };

	LOG(DEBUG) << "shape configured";

	Blob<float>* imgBlob = new Blob<float>();
	LOG(DEBUG) << "Blob created";

	imgBlob->Reshape(shape);
	LOG(DEBUG) << "imgBlob reshaped";

	imgBlob->CopyFrom(*srcBlob, false, true);
	LOG(DEBUG) << "imgBlob copied";

	int size = imgBlob->count();
	LOG(DEBUG) << "imgBlob size is: " << size;

	/*Partially from https://github.com/ruckus/android-image-filter-ndk*/

	uint32_t* pixelRow;
	int ix, iy, red, green, blue;

	for(iy = 0; iy < (int) info->height; iy++){

		pixelRow = (uint32_t*) pixels;

		for(ix =0; ix < (int) info->width; ix++){
			red = (int) clip(imgBlob->data_at(0,0,iy,ix), 0, 255);
			green = (int) clip(imgBlob->data_at(0,1,iy,ix), 0, 255);
			blue = (int) clip(imgBlob->data_at(0,2,iy,ix), 0, 255);

			pixelRow[ix] =
					((red << 16) & 0x00FF0000) |
					((green << 8) & 0x0000FF00) |
					(blue & 0x000000FF);
		}

		pixels = (char*)pixels + info->stride;
	}

	LOG(DEBUG) << "before return putImage " << size;

	return;
}
Exemplo n.º 5
0
  int NumSequenceMatches(const TransformationParameter transform_param,
      const Datum& datum, Phase phase) {
    // Get crop sequence with Caffe seed 1701.
    DataTransformer<Dtype>* transformer =
        new DataTransformer<Dtype>(transform_param, phase);
    const int crop_size = transform_param.crop_size();
    int crop_h = transform_param.crop_h();
    int crop_w = transform_param.crop_w();
    if (crop_size > 0) {
      crop_h = crop_w = crop_size;
    }
    Caffe::set_random_seed(seed_);
    transformer->InitRand();
    Blob<Dtype>* blob =
        new Blob<Dtype>(1, datum.channels(), datum.height(), datum.width());
    if (crop_h > 0 || crop_w > 0) {
      blob->Reshape(1, datum.channels(), crop_h, crop_w);
    }

    vector<vector<Dtype> > crop_sequence;
    for (int iter = 0; iter < this->num_iter_; ++iter) {
      vector<Dtype> iter_crop_sequence;
      transformer->Transform(datum, blob);
      for (int j = 0; j < blob->count(); ++j) {
        iter_crop_sequence.push_back(blob->cpu_data()[j]);
      }
      crop_sequence.push_back(iter_crop_sequence);
    }
    // Check if the sequence differs from the previous
    int num_sequence_matches = 0;
    for (int iter = 0; iter < this->num_iter_; ++iter) {
      vector<Dtype> iter_crop_sequence = crop_sequence[iter];
      transformer->Transform(datum, blob);
      for (int j = 0; j < blob->count(); ++j) {
        num_sequence_matches +=
            (crop_sequence[iter][j] == blob->cpu_data()[j]);
      }
    }
    return num_sequence_matches;
  }
Exemplo n.º 6
0
void EvalDetectionLayer<Dtype>::Forward_cpu(
    const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) 
{
  //const Dtype* input_data = bottom[0]->cpu_data();// 网络输出     N*13*13*125
  const Dtype* label_data = bottom[1]->cpu_data();  // 真实标签数据 N*30*5
  //LOG(INFO) << bottom[0]->data_at(0,0,0,0) << " " << bottom[0]->data_at(0,0,0,1);  
  
  Blob<Dtype> swap;// 网络输出数据 N * 13* 13* 125
  // 变形为    N * (13 * 13) *  5 * 25
 // N*(5*(5+num_class_))*13*13 -> N * (13*13) * 5 * (5+num_class_)
  swap.Reshape(bottom[0]->num(), 
               bottom[0]->height()*bottom[0]->width(), 
               num_object_, 
               bottom[0]->channels()/num_object_);  
  
  Dtype* swap_data = swap.mutable_cpu_data();// cpu上的数据
  caffe_set(swap.count(), Dtype(0.0), swap_data);// 设置为0
  int index = 0;
  for (int b = 0; b < bottom[0]->num(); ++b)// 图片数量
    for (int h = 0; h < bottom[0]->height(); ++h) // 格子 13
      for (int w = 0; w < bottom[0]->width(); ++w)// 格子 13
        for (int c = 0; c < bottom[0]->channels(); ++c)// 5*25=125
	{
	  swap_data[index++] = bottom[0]->data_at(b,c,h,w);
	}  
  //*******************************************************************************//
  //caffe_set(swap.count(), Dtype(0.0), swap_data);// 设置为0
  //int p_index = (7*13+4)*125;
  //swap_data[p_index]=-0.1020;
  //swap_data[p_index+1]=2.0867;
  //swap_data[p_index+2]=1.612;
  //swap_data[p_index+3]=1.0515;
  //swap_data[p_index+4]=1.0;
  //swap_data[p_index+5+11]=100;  

  //*******************************************************************************//  
  Dtype* top_data = top[0]->mutable_cpu_data();// 层输出 cpu数据
  caffe_set(top[0]->count(), Dtype(0), top_data);// 设置为0
  Dtype all_mAP = 0.0;// 总 batch 的 mAP
  for (int i = 0; i < bottom[0]->num(); ++i) 
 {   // N  图片数量
    int input_index = i * bottom[0]->count(1);// 网络输出标签 i * 13*13*125
    int true_index = i * bottom[1]->count(1);//  真实标签     i * 30*5
    int top_index = i * top[0]->count(1);    //  输出数据    //  i * ( 20 + 13*13*5*4 + 1) -> i * (13*13*5*4 + 1)
                                             //  前面20个为 真实标签 物体类别出现的次数
 
 // 获取真实边框 =========================================
    map<int, vector<BoxData> > gt_boxes;
    // 从 对应图片的标签数据中 获取 真实边框 label_ + score_ + box_ 
    // 返回一张图像的 标签 + 多边框数据的 标签
    GetGTBox(side_, label_data + true_index, &gt_boxes);

 
// 在输出数据中  记录 真实标签 物体类别出现的次数=======================
    for (std::map<int, vector<BoxData > >::iterator it = gt_boxes.begin(); it != gt_boxes.end(); ++it) 
	{
      // 遍历 每一个 真实的标签
      //int label = it->first;// 标签 类别
      vector<BoxData>& g_boxes = it->second;// BoxData: label_ + score_ + box_ 
      for (int j = 0; j < g_boxes.size(); ++j) 
	  {// 边框数量
         // 输出数据中的前 20个================================================
// =======================================
         // top_data[top_index + label] += 1;     // 真实标签 物体类别出现的次数
      }
    }
// 获取预测边框 =============================================
    map<int, vector<BoxData> > pred_boxes;
    // 获取预测边框 13*13*5 -> NMS抑制+置信度抑制 ->  pred_boxes(数量少很多)
    //GetPredBox(side_, num_object_, num_class_, input_data + input_index, &pred_boxes, sqrt_, constriant_, score_type_, nms_);
    GetPredBox(side_, num_object_, num_class_, swap_data + input_index, &pred_boxes, score_type_, nms_, biases_);
    
	// 输出数据 后面 的 13*13*5*4 =============================
    // int index = top_index + num_class_ + 1;// 20 + 1 之后为 上面的 (label + score + tp + fp) 参数
//=============================
    int index = top_index + 1;// 1个 map 之后为 上面的 (label + score + tp + fp) 参数
	
    int pred_count(0);// 
    
    Dtype mAP = 0.0;
	int pre_clas_num=0;
	//float AP = 0.0;
	// int tp
    // 遍历预测值的 每一类,与最合适的标签边框计算AP,在计算总类别的mAP============
    for (std::map<int, vector<BoxData> >::iterator it = pred_boxes.begin(); it != pred_boxes.end(); ++it) 
	{
      Dtype AP = 0.0;// 该类AP
	  int tp=0;// 预测正确
	  int fp=0;// 预测错误
	  ++pre_clas_num;// 该图片预测的总类别数量
      int label = it->first;// 预测 边框标签
      vector<BoxData>& p_boxes = it->second;// 多个边框 vector<BoxData>
	  
// 真实标签中 未找到该 类别=======================================
      if (gt_boxes.find(label) == gt_boxes.end()) {
        for (int b = 0; b < p_boxes.size(); ++b) {// 该类别下的每一个预测边框
          top_data[index + pred_count * 4 + 0] = p_boxes[b].label_;// 标签
          top_data[index + pred_count * 4 + 1] = p_boxes[b].score_;// 得分
          top_data[index + pred_count * 4 + 2] = 0; //tp 
          top_data[index + pred_count * 4 + 3] = 1; //fp 错误的预测为正确的 
          ++pred_count;
          ++fp;// 预测错误============================================
        }
		if(tp + fp)  
           AP = tp / (tp + fp);// 计算该类别的 平均准确度  这里等于0
		mAP += AP;// 所有类别的总 AP  这里可以省略 因为 AP为0
        continue;// 跳过预测错误的,只记录fp
      } 
  // 真实标签找到了该预测的类别======================================
      vector<BoxData>& g_boxes = gt_boxes[label];// 真实标签中该 类别的 多个真实边框=====
	  
      vector<bool> records(g_boxes.size(), false);// 记录 真实类别的每一个 物体边框 是否已经被预测过
	  
      for (int k = 0; k < p_boxes.size(); ++k) 
	  { // 遍历 每个 预测边框==============
        top_data[index + pred_count * 4 + 0] = p_boxes[k].label_;// 标签
        top_data[index + pred_count * 4 + 1] = p_boxes[k].score_;// 得分
        Dtype max_iou(-1);// 预测边框最接近的  真实边框 iou
        int idx(-1);// 对应的 真实边框id
		
		// 遍历每个真实边框 找到 预测边框最接近的 真实边框===========
        for (int g = 0; g < g_boxes.size(); ++g) {
          Dtype iou = Calc_iou(p_boxes[k].box_, g_boxes[g].box_);// 计算交并比
          if (iou > max_iou) {
            max_iou = iou;// 记录 每个预测边框 最接近的  真实边框 的 IOU
            idx = g;// 对应的 真实边框id
          }
        }
		// 根据 交并比 确定判断 预测 正确/错误
        if (max_iou >= threshold_) { 
             
            if ( !records[idx] ) {
              records[idx] = true;// 对应的 真实边框id
              top_data[index + pred_count * 4 + 2] = 1; // tp  正->正确
              top_data[index + pred_count * 4 + 3] = 0; // fp
              ++tp;// 预测正确 =================================
            } 
            else 
            {// 同一个位置的物体之前已经被预测过,又一个框来预测,则认为是错误的
              top_data[index + pred_count * 4 + 2] = 0;
              top_data[index + pred_count * 4 + 3] = 1;// 错误 -> 正确
              ++fp;// 预测错误============================================
            }
        }
        ++pred_count;
      }
      if(tp + fp)  AP = tp / (tp + fp);// 计算该类别的 平均准确度
      mAP += AP;// 所有类别的总 AP  
    }
	if(pre_clas_num){
		mAP /= pre_clas_num;
		// mAP /= num_class_; // 计算 mAP 是除以总类别数,还是总预测类别数
	} 
	else mAP = 0.0;
    // 输出对应图片的mAP
    top_data[ index - 1 ] = mAP;
	all_mAP += mAP;
  }
  if(bottom[0]->num()) all_mAP /= (bottom[0]->num());
  top_data[0] = all_mAP;
}
int ex_feature(int argc, char** argv){
	namespace bf=boost::filesystem;
	if (argc < 7){
		LOG(ERROR)<< "Usage: "<<argv[0]<<" pretrained_net_param feature_extraction_proto_file extract_feature_blob_name filelist meanfile mode";
		return 1;
	}
	int mode = atoi(argv[6]);
	if(mode == 1){
		LOG(ERROR) << "Using CPU";
		Caffe::set_mode(Caffe::CPU);
	}else{
		//using gpu
		LOG(ERROR)<< "Using GPU";
		uint device_id = 0;
		LOG(ERROR) << "Using Device_id=" << device_id;
		Caffe::SetDevice(device_id);
		Caffe::set_mode(Caffe::GPU);
	}
	
	Caffe::set_phase(Caffe::TEST);
	string extract_feature_blob_name=argv[3];
	//string svm_model = argv[3];
	string tst_filelist=argv[4];
	string mean_file = argv[5];
	//string save_path = argv[6];
	LOG(ERROR) << "load cnn model";
	shared_ptr<Net<Dtype> > feature_extraction_net(new Net<Dtype>(argv[2]));
	feature_extraction_net->CopyTrainedLayersFrom(argv[1]);
	//shared_ptr<Blob<Dtype> > feature_blob=feature_extraction_net->blob_by_name(extract_feature_blob_name);
	int layerIdx = feature_extraction_net->layerIdx_by_name(extract_feature_blob_name);
	if(layerIdx == -1){
		LOG(ERROR) << "Can't find layer:" << extract_feature_blob_name;
		return 1;
	}else{
		LOG(ERROR) << "LayerIdx:" << layerIdx << " continue...";
	}
	
	vector<vector<Blob<Dtype>*> >& top_vecs = feature_extraction_net->top_vecs();
	shared_ptr<Blob<Dtype> >  feature_blob(top_vecs[layerIdx][0]);
	shared_ptr<Blob<Dtype> > data_blob = feature_extraction_net->blob_by_name("data");
	LOG(ERROR) << "batch size:" << data_blob->num();
	int batch_size = data_blob->num();
	int channels = data_blob->channels();
	int height = data_blob->height();
	int width = data_blob->width();
	CHECK_EQ(height, width);
	int crop_size = height;
	//LOG(ERROR) << 
	//return 1;
	vector<string> images;
	if(!readFromFile(tst_filelist, images)){
		std::cout<< "parse Data Done." << std::endl;
	}else{
		std::cout<<"parse Data failed."<<std::endl;
		return 1;
	}
	Blob<Dtype> data_mean;
	//std::string mean_file = argv[5];
	BlobProto blob_proto;
	std::cout << "reading data_mean from " << mean_file << std::endl;
	ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
	data_mean.FromProto(blob_proto);
	cv::Mat mat_mean = Blob2Mat<Dtype>(data_mean);
	CHECK_EQ(data_mean.num(), 1);
	CHECK_EQ(data_mean.width(), data_mean.height());
	CHECK_EQ(data_mean.channels(), 3);
	std::cout << "prepare parameters" << std::endl;

	
	float scale = 1.0;	
	//bf::path output_path(save_path);
	Blob<Dtype>* bottom = new Blob<Dtype>(batch_size, 3, crop_size, crop_size);
	vector<Blob<Dtype>*> bottomV;
	bottomV.push_back(bottom);
	int numCaches = ceil(float(images.size()) / batch_size);
	Dtype* feature_blob_data;
	Dtype* im_blob_ori;
	int num=0;
	int startIdx = 0;
	//bf::path ftrfile = output_path;
	//ftrfile.replace_extension(".ftr");
	//std::ofstream fo(ftrfile.string().c_str());
	bool multivew = false;
	LOG(ERROR) << "cachesize:" << batch_size << " numCaches:" << numCaches;
	clock_t start_processing, end_processing;
	start_processing = clock();
	for(int cacheIdx = 0;cacheIdx < numCaches;cacheIdx++){
		LOG(ERROR) << "processing:" << cacheIdx << "/" << numCaches;
		vector< vector<Dtype> > cache;
		//vector< vector<Dtype> > resultcache;
		clock_t start_cache, end_cache;
		start_cache = clock();
		vector<vector<int> > img_size;
		readImagesToCache(cache, images, crop_size, mat_mean, batch_size, &startIdx, &num, scale, multivew, img_size);
		end_cache = clock();
		LOG(ERROR) << "readImageToCache:" << (end_cache-start_cache) << "ms";
		start_cache = clock();
		int nBatches = ceil(float(cache.size()) / batch_size);
		//LOG(ERROR) << "nBatches:"<< nBatches << " cache:" << cache.size();
		assert(img_size.size() == nBatches);
		for(int batchIdx = 0;batchIdx < nBatches; batchIdx++){
			time_t start_epoch, end_epoch;
			start_epoch = time(NULL);
			LOG(ERROR) << "ResetLayer: height" <<img_size[batchIdx][0] << " width:" << img_size[batchIdx][1] ;
			bottom->Reshape(bottom->num(), bottom->channels(), img_size[batchIdx][0], img_size[batchIdx][1]);
			feature_extraction_net->ResetLayers(layerIdx, img_size[batchIdx][0], img_size[batchIdx][1]);
			int n=readImagesToBlob(*bottom, cache, batch_size, batchIdx);
			float loss = 0.0;
			LOG(ERROR) << "forward";
			const vector<Blob<Dtype>*>& result =  feature_extraction_net->Forward(bottomV, layerIdx, &loss);
			//SetTopAct<Dtype>(feature_blob);
			//int height_t = feature_blob->height();
			//int width_t = feature_blob->width();
			//LOG(ERROR) << "feature_blob:" << height_t << " " << width_t;
			//for(int hh = 0;hh < 3;hh++){
			//	for(int ww = 0;ww<3;++ww){
			//int hh=0, ww=0;
			LOG(ERROR) << "Enter coordinate to reconstruct and -1 to processing next picture";
			/*while(1){
					std::cout << "Input the position to be reconstruct (x y):";
					std::cin >> hh;
					if (hh == -1){
						break;
					}
					std::cin >> ww;
					feature_extraction_net->ReSetActions(layerIdx);
					SetTop(feature_blob, hh, ww);
					//SetTopAct<Dtype>(feature_blob);
					feature_extraction_net->Deconv(layerIdx);
					//return 1;
					int feature_num = feature_blob->num();
					int feature_dim = feature_blob->count() / feature_num;
					int start_idx=batch_size*batchIdx;
					for(int s=0;s<n;++s){
						feature_blob_data = data_blob->mutable_cpu_diff()+data_blob->offset(s);
						im_blob_ori = data_blob->mutable_cpu_data() + data_blob->offset(s);
						//vector<Dtype> result_t;
						show_reconstruct_image<Dtype>(feature_blob_data, im_blob_ori, data_blob->count() / data_blob->num(), data_blob->height(), data_blob->width(), data_blob->channels());
						for(int d=0;d<feature_dim;++d){
							result_t.push_back(feature_blob_data[d]);
						}
						resultcache.push_back(result_t);
					}
			}*/

				//}
			//}
			show_reconstruct_image_for_position<Dtype>(feature_extraction_net, feature_blob, data_blob, layerIdx, n);
			end_epoch = time(NULL);
			LOG(ERROR) << "forward batch(" << batch_size << "):" << difftime(end_epoch,start_epoch) << "s";
			//LOG(ERROR) << "BatchIdx:" << batchIdx << " n:" << n << " resultcache:" << resultcache.size();
		}
		end_cache = clock();
		LOG(ERROR) << "forward cache:" << end_cache-start_cache << "ms";

		//return 1;
		/*
		int imgIdx = startIdx - num;
		for(int s=0;s<num;++s){
			if(multivew){
				fo << images[imgIdx+s] << " " << 9*2 << " " << resultcache[0].size() << std::endl;
				for(int m=0;m<9*2;++m){
					vector<Dtype>& ftr=resultcache[s*9*2+m];
					for(int d=0;d<ftr.size()-1;++d){
						fo << ftr[d] << " ";
					}
					fo << ftr[ftr.size()-1] << std::endl;
				}
			}else{
				fo << images[imgIdx+s] << " " << 1 << " " << resultcache[0].size() << std::endl;
				vector<Dtype>& ftr=resultcache[s];
				for(int d=0;d<ftr.size()-1;++d){
						fo << ftr[d] << " ";
					//show_reconstruct_image(ftr, 
				}
				fo << ftr[ftr.size()-1] << std::endl;
			}
		}*/
	}
	//fo.close();
	end_processing = clock();
	LOG(ERROR) << "total time:" << float(end_processing-start_processing)/CLOCKS_PER_SEC << "s";
	delete bottom;
	return 0;
}
Exemplo n.º 8
0
void LocalLayer<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* bottom_data = bottom[0]->cpu_data();
  Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
  Dtype* x_data = col_buffer_.mutable_cpu_data();
  Dtype* x_diff = col_buffer_.mutable_cpu_diff();
  const Dtype* weight = this->blobs_[0]->cpu_data();
  Dtype* weight_diff = this->blobs_[0]->mutable_cpu_diff();
  Dtype* bias_diff = NULL;

  Blob<Dtype> intermediate;
  intermediate.Reshape(1, 1, 1, N_);

  Blob<Dtype> xt;
  xt.Reshape(1, 1, K_, N_);
  Dtype* xt_data = xt.mutable_cpu_data();

  if (bias_term_) {
    bias_diff = this->blobs_[1]->mutable_cpu_diff();
    memset(bias_diff, 0, sizeof(Dtype) * this->blobs_[1]->count());
    for (int n = 0; n < num_; ++n) {
      caffe_add(M_ * N_, bias_diff,
                top_diff + top[0]->offset(n),
                bias_diff);
    }
  }

  memset(weight_diff, 0, sizeof(Dtype) * this->blobs_[0]->count());
  for (int n=0; n<num_; n++) {
    im2col_cpu(bottom_data + bottom[0]->offset(n), channels_, height_,
               width_, kernel_size_, kernel_size_, pad_, pad_, stride_, stride_, x_data);

    // gradient wrt weight
    for (int m=0; m<num_output_; m++) {
      Dtype* filter_weight_diff = weight_diff+this->blobs_[0]->offset(m);
      for (int k=0; k<K_; k++) {
        caffe_mul(N_, top_diff+top[0]->offset(n, m),  
                  x_data+col_buffer_.offset(0,k), xt_data+xt.offset(0,0,k));
      }
      caffe_cpu_axpby(K_*N_, Dtype(1.0), xt_data, Dtype(1.0), filter_weight_diff);
    }
      
    // gradient wrt bottom data
    if (propagate_down[0]) {
      memset(x_diff, 0, col_buffer_.count() * sizeof(Dtype));
      for (int m=0; m<num_output_; m++) {
        for (int k=0; k<K_; k++) {
          caffe_mul(N_, top_diff+top[0]->offset(n, m),
                    weight+this->blobs_[0]->offset(m,0,k),
                    intermediate.mutable_cpu_data());

          caffe_cpu_axpby(N_, Dtype(1.0),
                          intermediate.cpu_data(), Dtype(1.0),
                          x_diff+col_buffer_.offset(0,k));
        }
      }

      // col2im back to the data
      col2im_cpu(x_diff, channels_, height_, width_, kernel_size_, kernel_size_,
                 pad_, pad_, stride_, stride_, bottom_diff + bottom[0]->offset(n));

    }
  }

}
Exemplo n.º 9
0
	void FindOutlier( const Dtype* top_diff, const Dtype* bottom_data, int &M, int &N, int &K)
	{
		if (!ACTIVATE) return;

//		ofstream F_top_diff("top_diff.txt");
//		ofstream F_bottom_data("bottom_data.txt");
//		ofstream F_gradient("gradient.txt");
//		ofstream F_mean_gradient("mean_gradient.txt");
//		ofstream F_abs_diff("abs_diff.txt");


//		for (int i = 0; i<N; i++)
//		{
//			for (int j = 0; j<M; j++)
//			{
//				F_top_diff << *(top_diff+i*M+j) << " ";
//			}
//			F_top_diff << endl;
//		}
//		F_top_diff.close();
//
//		for (int i = 0; i<N; i++)
//		{
//			for (int j = 0; j<K; j++)
//			{
//				F_bottom_data << *(bottom_data+i*K+j) << " ";
//			}
//			F_bottom_data << endl;
//		}
//		F_bottom_data.close();



		if ( !IdentifyOutlier )
		{
			Blob<Dtype> GradMean;
	  		Blob<Dtype> Grad;

			Grad.Reshape( N, 1, M, K);
			GradMean.Reshape(1, 1, M, K);

			int top_offset = M;
			int bottom_offset = K;
			int grad_offset = M*K;

			Dtype* AllGrad = Grad.mutable_cpu_data();

			for (int i = 0; i < N; ++i)
			{
				caffe_cpu_gemm<Dtype>(CblasTrans, CblasNoTrans, M, K, 1, (Dtype)1.,
					top_diff + top_offset * i, bottom_data + bottom_offset * i, (Dtype)0.,
					AllGrad + grad_offset * i);
			}

//			for (int i = 0; i<N; i++)
//			{
//				for (int j = 0; j<grad_offset; j++)
//				{
//					F_gradient << *(AllGrad+i*grad_offset+j) << " ";
//				}
//				F_gradient << endl;
//			}
//			F_gradient.close();

			const Dtype* element_r = Grad.cpu_data();
			Dtype* eleMean_w = GradMean.mutable_cpu_data();
			caffe_set(grad_offset, (Dtype)0., eleMean_w);

			int PositiveNum = 0;
			for (int i = 0; i < N; ++i)
			{
				if (!MiniBatchIsneg[i])
				{
					caffe_axpy<Dtype>(grad_offset, (Dtype)1., element_r+i*grad_offset, eleMean_w);
					PositiveNum ++;
				}
			}
			caffe_scal<Dtype>( grad_offset, (Dtype)(1.0/PositiveNum), eleMean_w);

//			for (int i = 0; i<1; i++)
//			{
//				for (int j = 0; j<grad_offset; j++)
//				{
//					F_mean_gradient << *(eleMean_w+i*grad_offset+j) << " ";
//				}
//				F_mean_gradient << endl;
//			}
//			F_mean_gradient.close();

			Dtype* element_w = Grad.mutable_cpu_data();
			const Dtype* eleMean_r = GradMean.cpu_data();

			for (int i = 0; i < N; ++i)
			{
				if (!MiniBatchIsneg[i])
				{
					caffe_axpy<Dtype>(grad_offset, (Dtype)(-1.), eleMean_r, element_w+i*grad_offset);
					caffe_abs<Dtype>(grad_offset, Grad.cpu_data() + i * grad_offset, element_w + i*grad_offset);
				}

			}

//			for (int i = 0; i<N; i++)
//			{
//				for (int j = 0; j<grad_offset; j++)
//				{
//					F_abs_diff << *(element_w+i*grad_offset+j) << " ";
//				}
//				F_abs_diff << endl;
//			}
//			F_abs_diff.close();

			Blob<Dtype> Ones;
			Ones.Reshape( 1, 1, grad_offset, 1);
			caffe_set(grad_offset, (Dtype)1., Ones.mutable_cpu_data());

			Blob<Dtype> OutIndicator;
			OutIndicator.Reshape( N, 1, 1, 1);
			caffe_cpu_gemv<Dtype>(CblasNoTrans, N, grad_offset, (Dtype) 1., Grad.cpu_data(), Ones.cpu_data(),
	    	(Dtype)0., OutIndicator.mutable_cpu_data());

			const Dtype* diff = OutIndicator.cpu_data();
			std::vector<mypair> scores;
			scores.clear();
			for (int i = 0; i < N; ++i)
			{
				if (!MiniBatchIsneg[i])
				{
					scores.push_back(mypair(*(diff+i), i));
				}
			}
			std::sort(scores.begin(), scores.end(), DataProcess::comparator_pair_index);

			int KillNumber = (int) (PositiveNum * NoiseRate);
			IsOutlier.assign(N, false);
			for (int i = 0; i < KillNumber; ++i)
			{
				IsOutlier[scores[PositiveNum-i-1].second] = true;
				Weight[MiniBatchDataID[scores[PositiveNum-i-1].second]]++;
			}

			IdentifyOutlier = true;
		}
	};
Exemplo n.º 10
0
		void FindOutlier_gpu( const Dtype* top_diff, const Dtype* bottom_data, int &M, int &N, int &K)
		{
			if (!ACTIVATE) return;

			if ( !IdentifyOutlier )
			{
				Blob<Dtype> GradMean;
		  		Blob<Dtype> Grad;
		  		//LOG(INFO) << "1";

				Grad.Reshape( N, 1, M, K);
				GradMean.Reshape(1, 1, M, K);
				//LOG(INFO) << "2";

				int top_offset = M;
				int bottom_offset = K;
				int grad_offset = M*K;

				Dtype* AllGrad = Grad.mutable_gpu_data();

				for (int i = 0; i < N; ++i)
				{
					caffe_gpu_gemm<Dtype>(CblasTrans, CblasNoTrans, M, K, 1, (Dtype)1.,
						top_diff + top_offset * i, bottom_data + bottom_offset * i, (Dtype)0.,
						AllGrad + grad_offset * i);
				}
				//LOG(INFO) << "3";

				const Dtype* element_r = Grad.gpu_data();
				Dtype* eleMean_w = GradMean.mutable_gpu_data();
				caffe_gpu_set(grad_offset, (Dtype)0., eleMean_w);

				int PositiveNum = 0;
				for (int i = 0; i < N; ++i)
				{
					if (!MiniBatchIsneg[i])
					{
						caffe_gpu_axpy<Dtype>(grad_offset, (Dtype)1., element_r+i*grad_offset, eleMean_w);
						PositiveNum ++;
					}
				}
				caffe_gpu_scal<Dtype>( grad_offset, (Dtype)(1.0/PositiveNum), eleMean_w);
				//LOG(INFO) << "4";

				Dtype* element_w = Grad.mutable_gpu_data();
				const Dtype* eleMean_r = GradMean.gpu_data();

				for (int i = 0; i < N; ++i)
				{
					if (!MiniBatchIsneg[i])
					{
						caffe_gpu_axpy<Dtype>(grad_offset, (Dtype)(-1.), eleMean_r, element_w+i*grad_offset);
						caffe_gpu_abs<Dtype>(grad_offset, Grad.gpu_data() + i * grad_offset, element_w + i*grad_offset);
					}

				}
				//LOG(INFO) << "5";

				Blob<Dtype> Ones;
				Ones.Reshape( 1, 1, grad_offset, 1);
				caffe_gpu_set(grad_offset, (Dtype)1., Ones.mutable_gpu_data());

				//LOG(INFO) <<"5.1";

				Blob<Dtype> OutIndicator;
				OutIndicator.Reshape( N, 1, 1, 1);
				caffe_gpu_gemv<Dtype>(CblasNoTrans, N, grad_offset, (Dtype) 1., Grad.gpu_data(), Ones.gpu_data(),
		    	(Dtype)0., OutIndicator.mutable_gpu_data());

				//LOG(INFO) <<"5.2";

				const Dtype* diff = OutIndicator.cpu_data();
				std::vector<mypair> scores;
				scores.clear();
				for (int i = 0; i < N; ++i)
				{
					if (!MiniBatchIsneg[i])
					{
						//LOG(INFO) << i << " " << *(diff+i);
						scores.push_back(mypair(*(diff+i), i));
					}
				}
				std::sort(scores.begin(), scores.end(), DataProcess::comparator_pair_index);

				//LOG(INFO) <<"5.3";

				int KillNumber = (int) (PositiveNum * NoiseRate);
				IsOutlier.assign(N, false);
				//LOG(INFO) << KillNumber << " " << N <<;
				for (int i = 0; i < KillNumber; ++i)
				{
					IsOutlier[scores[PositiveNum-i-1].second] = true;
					Weight[MiniBatchDataID[scores[PositiveNum-i-1].second]]++;
				}
				//LOG(INFO) << "6";

				IdentifyOutlier = true;
			}
		};