示例#1
0
文件: blob.hpp 项目: jy9387/mycaffe
 inline int LegacyShape(int index) const {
   CHECK_LE(num_axes(), 4)
       << "Cannot use legacy accessors on Blobs with > 4 axes.";
   CHECK_LT(index, 4);
   CHECK_GE(index, -4);
   if (index >= num_axes() || index < -num_axes()) {
     // Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse
     // indexing) -- this special case simulates the one-padding used to fill
     // extraneous axes of legacy blobs.
     return 1;
   }
   return shape(index);
 }
示例#2
0
文件: blob.hpp 项目: jy9387/mycaffe
 inline int offset(const vector<int>& indices) const {
   CHECK_LE(indices.size(), num_axes());
   int offset = 0;
   for (int i = 0; i < num_axes(); ++i) {
     offset *= shape(i);
     if (indices.size() > i) {
       CHECK_GE(indices[i], 0);
       CHECK_LT(indices[i], shape(i));
       offset += indices[i];
     }
   }
   return offset;
 }
示例#3
0
void ThriftServer::CumulativeFailureInjection::set(
    const FailureInjection& fi) {
  CHECK_GE(fi.errorFraction, 0);
  CHECK_GE(fi.dropFraction, 0);
  CHECK_GE(fi.disconnectFraction, 0);
  CHECK_LE(fi.errorFraction + fi.dropFraction + fi.disconnectFraction, 1);

  std::lock_guard<std::mutex> lock(mutex_);
  errorThreshold_ = fi.errorFraction;
  dropThreshold_ = errorThreshold_ + fi.dropFraction;
  disconnectThreshold_ = dropThreshold_ + fi.disconnectFraction;
  empty_.store((disconnectThreshold_ == 0), std::memory_order_relaxed);
}
示例#4
0
文件: layer.hpp 项目: csuhawk/caffe
 /**
  * Called by the parent Layer's SetUp to check that the number of bottom
  * and top Blobs provided as input match the expected numbers specified by
  * the {ExactNum,Min,Max}{Bottom,Top}Blobs() functions.
  */
 virtual void CheckBlobCounts(const vector<Blob<Dtype>*>& bottom,
                              const vector<Blob<Dtype>*>& top) {
   if (ExactNumBottomBlobs() >= 0) {
     CHECK_EQ(ExactNumBottomBlobs(), bottom.size())
         << type() << " Layer takes " << ExactNumBottomBlobs()
         << " bottom blob(s) as input.";
   }
   if (MinBottomBlobs() >= 0) {
     CHECK_LE(MinBottomBlobs(), bottom.size())
         << type() << " Layer takes at least " << MinBottomBlobs()
         << " bottom blob(s) as input.";
   }
   if (MaxBottomBlobs() >= 0) {
     CHECK_GE(MaxBottomBlobs(), bottom.size())
         << type() << " Layer takes at most " << MaxBottomBlobs()
         << " bottom blob(s) as input.";
   }
   if (ExactNumTopBlobs() >= 0) {
     CHECK_EQ(ExactNumTopBlobs(), top.size())
         << type() << " Layer produces " << ExactNumTopBlobs()
         << " top blob(s) as output.";
   }
   if (MinTopBlobs() >= 0) {
     CHECK_LE(MinTopBlobs(), top.size())
         << type() << " Layer produces at least " << MinTopBlobs()
         << " top blob(s) as output.";
   }
   if (MaxTopBlobs() >= 0) {
     CHECK_GE(MaxTopBlobs(), top.size())
         << type() << " Layer produces at most " << MaxTopBlobs()
         << " top blob(s) as output.";
   }
   if (EqualNumBottomTopBlobs()) {
     CHECK_EQ(bottom.size(), top.size())
         << type() << " Layer produces one top blob as output for each "
         << "bottom blob input.";
   }
 }
示例#5
0
    TEST(Logging, CheckOpPass)
    {
        int i1 = 1;
        int i2 = 2;
        unsigned u1 = 3;
        unsigned u2 = 4;
        float f1 = 5.5f;
        float f2 = 6.6f;
        int* p1 = &i1;
        int* p2 = &i2;
        char const * message = "message";

        CHECK_EQ(i1, i1) << message;
        CHECK_EQ(u1, u1) << message;
        CHECK_EQ(f1, f1) << message;
        CHECK_EQ(p1, p1) << message;

        CHECK_NE(i1, i2) << message;
        CHECK_NE(u1, u2) << message;
        CHECK_NE(f1, f2) << message;
        CHECK_NE(p1, p2) << message;

        CHECK_LT(i1, i2) << message;
        CHECK_LT(u1, u2) << message;
        CHECK_LT(f1, f2) << message;

        CHECK_LE(i1, i1) << message;
        CHECK_LE(u1, u2) << message;
        CHECK_LE(f1, f1) << message;

        CHECK_GT(i2, i1) << message;
        CHECK_GT(u2, u1) << message;
        CHECK_GT(f2, f1) << message;

        CHECK_GE(i1, i1) << message;
        CHECK_GE(u2, u1) << message;
        CHECK_GE(f1, f1) << message;
    }
void EltwiseAccuracyLayer<Dtype>::Reshape(
  const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
  CHECK_EQ(bottom[0]->num(), bottom[1]->num())
      << "The data and label should have the same number.";
  CHECK_LE(top_k_, bottom[0]->count() / bottom[0]->num())
      << "top_k must be less than or equal to the number of classes.";
  CHECK_EQ(bottom[1]->channels(), 1)
      << "Label data should have channel 1.";
  CHECK_EQ(bottom[0]->height(), bottom[1]->height())
      << "The data and label should have the same height.";
  CHECK_EQ(bottom[0]->width(), bottom[1]->width())
      << "the data and label should have the same width.";
  top[0]->Reshape(1, 1, 1, 1);
}
示例#7
0
bool CoverageSet::covers(size_t begin, size_t end) const noexcept {
  CHECK_LE(begin, end)
      << "End of interval must be greater than or equal to begin";
  if (begin == end) {
    return true;
  }

  auto right = set_.upper_bound(Interval{begin, end});
  if (right == set_.begin()) {
    return false;
  }
  auto left = std::prev(right);
  return left->begin <= begin && end <= left->end;
}
示例#8
0
void Blob<Dtype>::Reshape(const vector<int>& shape) {
  CHECK_LE(shape.size(), kMaxBlobAxes);
  count_ = 1;
  shape_.resize(shape.size());
  if (!shape_data_ || shape_data_->size() < shape.size() * sizeof(int)) {
    shape_data_.reset(new SyncedMemory(shape.size() * sizeof(int)));
  }
  int* shape_data = static_cast<int*>(shape_data_->mutable_cpu_data());
  for (int i = 0; i < shape.size(); ++i) {
    CHECK_GE(shape[i], 0);
    if (count_ != 0) {
      CHECK_LE(shape[i], INT_MAX / count_) << "blob size exceeds INT_MAX";
    }
    count_ *= shape[i];
    shape_[i] = shape[i];
    shape_data[i] = shape[i];
  }
  if (count_ > capacity_) {
    capacity_ = count_;
    data_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
    diff_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
  }
}
示例#9
0
std::unique_ptr<IOBuf> SnappyCodec::doCompress(const IOBuf* data) {
  IOBufSnappySource source(data);
  auto out =
    IOBuf::create(snappy::MaxCompressedLength(source.Available()));

  snappy::UncheckedByteArraySink sink(reinterpret_cast<char*>(
      out->writableTail()));

  size_t n = snappy::Compress(&source, &sink);

  CHECK_LE(n, out->capacity());
  out->append(n);
  return out;
}
示例#10
0
// Asynchronous read on a overlapped int_fd. Returns `Error` on fatal errors,
// `None()` on a successful pending IO operation or number of bytes read on a
// successful IO operation that finished immediately.
inline Result<size_t> read_async(
    const int_fd& fd,
    void* data,
    size_t size,
    OVERLAPPED* overlapped)
{
  CHECK_LE(size, UINT_MAX);

  switch (fd.type()) {
    case WindowsFD::Type::HANDLE: {
      DWORD bytes;
      const bool success =
        ::ReadFile(fd, data, static_cast<DWORD>(size), &bytes, overlapped);

      // On failure, there are two EOF cases for reads:
      //   1) ERROR_BROKEN_PIPE: The write end is closed and there is no data.
      //   2) ERROR_HANDLE_EOF: We hit the EOF for an asynchronous file handle.
      const DWORD errorCode = ::GetLastError();
      if (success == FALSE &&
          (errorCode == ERROR_BROKEN_PIPE || errorCode == ERROR_HANDLE_EOF)) {
        return 0;
      }

      return ::internal::windows::process_async_io_result(success, bytes);
    }
    case WindowsFD::Type::SOCKET: {
      static_assert(
          std::is_same<OVERLAPPED, WSAOVERLAPPED>::value,
          "Expected `WSAOVERLAPPED` to be of type `OVERLAPPED`.");

      // Note that it's okay to allocate this on the stack, since the WinSock
      // providers must copy the WSABUF to their internal buffers. See
      // https://msdn.microsoft.com/en-us/library/windows/desktop/ms741688(v=vs.85).aspx // NOLINT(whitespace/line_length)
      WSABUF buf = {
        static_cast<u_long>(size),
        static_cast<char*>(data)
      };

      DWORD bytes;
      DWORD flags = 0;
      const int result =
        ::WSARecv(fd, &buf, 1, &bytes, &flags, overlapped, nullptr);

      return ::internal::windows::process_async_io_result(result == 0, bytes);
    }
  }

  UNREACHABLE();
}
void ArgMaxLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) {
  out_max_val_ = this->layer_param_.argmax_param().out_max_val();
  top_k_ = this->layer_param_.argmax_param().top_k();
  CHECK_GE(top_k_, 1) << " top k must not be less than 1.";
  CHECK_LE(top_k_, bottom[0]->count() / bottom[0]->num())
      << "top_k must be less than or equal to the number of classes.";
  if (out_max_val_) {
    // Produces max_ind and max_val
    (*top)[0]->Reshape(bottom[0]->num(), 2, top_k_, 1);
  } else {
    // Produces only max_ind
    (*top)[0]->Reshape(bottom[0]->num(), 1, top_k_, 1);
  }
}
Vector ЧебышёвSeries<Vector>::Evaluate(Instant const& t) const {
    // This formula ensures continuity at the edges by producing -1 or +1 within
    // 2 ulps for |t_min_| and |t_max_|.
    double const scaled_t = ((t - t_max_) + (t - t_min_)) * one_over_duration_;
    // We have to allow |scaled_t| to go slightly out of [-1, 1] because of
    // computation errors.  But if it goes too far, something is broken.
    // TODO(phl): This should use DCHECK but these macros don't work because the
    // Principia projects don't define NDEBUG.
#ifdef _DEBUG
    CHECK_LE(scaled_t, 1.1);
    CHECK_GE(scaled_t, -1.1);
#endif

    return helper_.EvaluateImplementation(scaled_t);
}
示例#13
0
void Blob<Dtype>::Reshape(const vector<int>& shape) {
  CHECK_LE(shape.size(), kMaxBlobAxes);
  count_ = 1;
  shape_.resize(shape.size());
  for (int i = 0; i < shape.size(); ++i) {
    CHECK_GE(shape[i], 0);
    count_ *= shape[i];
    shape_[i] = shape[i];
  }
  if (count_ > capacity_) {
    capacity_ = count_;
    data_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
    diff_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
  }
}
示例#14
0
void SegAccuracyLayer<Dtype>::Reshape(
  const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
  CHECK_LE(1, bottom[0]->channels())
      << "top_k must be less than or equal to the number of channels (classes).";
  CHECK_EQ(bottom[0]->num(), bottom[1]->num())
    << "The data and label should have the same number.";
  CHECK_EQ(bottom[1]->channels(), 1)
    << "The label should have one channel.";
  CHECK_EQ(bottom[0]->height(), bottom[1]->height())
    << "The data should have the same height as label.";
  CHECK_EQ(bottom[0]->width(), bottom[1]->width())
    << "The data should have the same width as label.";
 
  top[0]->Reshape(1, 1, 1, 5);
}
ReturnValue BleApiTest_TestEncodingLongDataExactLength(pBleDevice dev)
{
	ReturnValue retval;

	U2F_AUTHENTICATE_REQ authReq;
	unsigned char reply[2048];
	unsigned int replyLength = sizeof(reply);
	unsigned char request[256];
	unsigned int requestlen;
	unsigned char replyCmd;

	// pick random challenge and use registered appId.
	for (size_t i = 0; i < sizeof(authReq.nonce); ++i)
		authReq.nonce[i] = rand();
	memcpy(authReq.appId, regReq.appId, sizeof(authReq.appId));
	authReq.keyHandleLen = regRsp.keyHandleLen;
	memcpy(authReq.keyHandle, regRsp.keyHandleCertSig,
	       authReq.keyHandleLen);

	uint64_t t = dev->TimeMs();

	/* prepare register request */
	request[0] = 0x00;
	request[1] = U2F_INS_AUTHENTICATE;
	request[2] = U2F_AUTH_ENFORCE;
	request[3] = 0x00;
	request[4] = 0x00;
	request[5] = 0x00;
	request[6] = U2F_NONCE_SIZE + U2F_APPID_SIZE + 1 + authReq.keyHandleLen;
	memcpy(request + 7, reinterpret_cast < char *>(&authReq), request[6]);
	requestlen = 7 + request[6];
	request[requestlen++] = 0x00;
	/* 1 byte user presence + 4 bytes counter + upto (6 + 33 + 33) bytes signature */
	request[requestlen++] = 0x01 + 0x04 + 0x48;

	/* write command */
	retval =
	    dev->CommandWrite(FIDO_BLE_CMD_MSG, request, requestlen, &replyCmd,
			      reply, &replyLength);
	CHECK_EQ(retval, ReturnValue::BLEAPI_ERROR_SUCCESS);

  CHECK_EQ(replyCmd, FIDO_BLE_CMD_MSG, "Reply is not a FIDO_BLE_CMD_MSG (0x83)");
	CHECK_EQ(FIDO_RESP_SUCCESS, bytes2short(reply, replyLength - 2), "expects FIDO_RESP_SUCCESS (0x9000)");
	CHECK_NE(replyLength, 2);
  CHECK_LE(replyLength - 2, sizeof(U2F_AUTHENTICATE_RESP), "Returned authentication response does not match expected length.");

	return ReturnValue::BLEAPI_ERROR_SUCCESS;
}
示例#16
0
void AccuracyLayer<Dtype>::Reshape(
  const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
  CHECK_LE(top_k_, bottom[0]->count() / bottom[1]->count())
      << "top_k must be less than or equal to the number of classes.";
  label_axis_ =
      bottom[0]->CanonicalAxisIndex(this->layer_param_.accuracy_param().axis());
  outer_num_ = bottom[0]->count(0, label_axis_);
  inner_num_ = bottom[0]->count(label_axis_ + 1);
  CHECK_EQ(outer_num_ * inner_num_, bottom[1]->count())
      << "Number of labels must match number of predictions; "
      << "e.g., if label axis == 1 and prediction shape is (N, C, H, W), "
      << "label count (number of labels) must be N*H*W, "
      << "with integer values in {0, 1, ..., C-1}.";
  vector<int> top_shape(0);  // Accuracy is a scalar; 0 axes.
  top[0]->Reshape(top_shape);
}
示例#17
0
inline std::string DebugString(double const number, int const precision) {
  char result[50];
#if OS_WIN && PRINCIPIA_COMPILER_MSVC && (_MSC_VER < 1900)
  unsigned int old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
  int const size = sprintf_s(result,
                             ("%+." + std::to_string(precision) + "e").c_str(),
                             number);
  _set_output_format(old_exponent_format);
#else
  int const size = snprintf(result, sizeof(result),
                            ("%+." + std::to_string(precision) + "e").c_str(),
                            number);
#endif
  CHECK_LE(0, size);
  return std::string(result, size);
}
示例#18
0
void caffe_rng_bernoulli(const int n, const Dtype p, unsigned int* r) {
  CHECK_GE(n, 0);
  CHECK(r);
  CHECK_GE(p, 0);
  CHECK_LE(p, 1);
#ifdef USE_MKL
  bernoulli_generate(n, p, reinterpret_cast<int *>(r));
#else
  boost::bernoulli_distribution<Dtype> random_distribution(p);
  boost::variate_generator<caffe::rng_t*, boost::bernoulli_distribution<Dtype> >
      variate_generator(caffe_rng(), random_distribution);
  for (int i = 0; i < n; ++i) {
    r[i] = static_cast<unsigned int>(variate_generator());
  }
#endif
}
示例#19
0
 real calc(int64_t num) {
   // We assume that num never decreases.
   CHECK_LE(lastNum_, num);
   lastNum_ = num;
   while (currentSegment_ < rates_.size()) {
     if (num <= segments_[currentSegment_]) {
       return learningRate_ * rates_[currentSegment_];
     }
     ++currentSegment_;
     if (currentSegment_ < rates_.size()) {
       LOG(INFO) << " learning_rate changes to "
                 << learningRate_ * rates_[currentSegment_];
     }
   }
   return learningRate_ * rates_.back();
 }
示例#20
0
/// Input format: from_v_id \t to_v_id
void MMSBModel::ReadData() { //TODO
  /// training data
  const string& train_data_path = Context::get_string("train_data");
  LOG(INFO) << "Read train data from " << train_data_path;
  fstream train_data_file(train_data_path.c_str(), ios::in);
  CHECK(train_data_file.is_open()) << "Fail to open " << train_data_path; 
  // header: #v #e
  Count num_vertices = 0;
  train_data_file >> num_vertices;
  LOG(INFO) << "#Vertices\t" << num_vertices;
  CHECK_LE(train_batch_size_, num_vertices);
  vertices_.resize(num_vertices);
  for (VIndex i = 0; i < num_vertices; ++i) {
    vertices_[i] = new Vertex();
  }
  // links
  VIndex i, j;
  while (train_data_file >> i >> j) {
    if (i != j) { // avoid self-loop
      vertices_[i]->AddLink(j);
      vertices_[j]->AddLink(i);
    }
  }
  train_data_file.close();

  /// test data
  const string& test_data_path = Context::get_string("test_data");
  LOG(INFO) << "Read test data from " << test_data_path;
  fstream test_data_file(test_data_path.c_str(), ios::in);
  CHECK(test_data_file.is_open()) << "Fail to open " << test_data_path; 
  int value = 0;
  WIndex i_worker, j_worker;
  while (test_data_file >> i >> j >> value >> i_worker >> j_worker) {
#ifdef DEBUG
    CHECK(vertices_.find(i) != vertices_.end());
    CHECK_EQ(i_worker, worker_id_);
#endif
    test_links_.push_back(make_pair(i, j));
    test_link_values_.push_back(value);
    if (j_worker != worker_id_) {
      test_neighbor_worker_[j] = j_worker;
    }
  }
  LOG(INFO) << "#Test links (pos and neg links)\t"
      << test_links_.size();
  test_data_file.close();
}
SymplecticRungeKuttaNyströmIntegrator<Position, order, time_reversible,
                                      evaluations, composition>::
SymplecticRungeKuttaNyströmIntegrator(
    serialization::FixedStepSizeIntegrator::Kind const kind,
    FixedVector<double, stages_> const& a,
    FixedVector<double, stages_> const& b)
    : FixedStepSizeIntegrator<
          SpecialSecondOrderDifferentialEquation<Position>>(kind),
      a_(a),
      b_(b) {
  DoublePrecision<double> c_i = 0.0;
  for (int i = 0; i < stages_; ++i) {
    c_[i] = c_i.value;
    c_i.Increment(a_[i]);
  }
  CHECK_LE(ULPDistance(1.0, c_i.value), 2);
  if (composition == kABA) {
    CHECK_EQ(0.0, b_[0]);
  } else if (composition == kBAB) {
    CHECK_EQ(0.0, a_[stages_ - 1]);
  }
  if (time_reversible) {
    switch (composition) {
      case kABA:
        for (int i = 0; i < stages_; ++i) {
          CHECK_EQ(a_[i], a_[stages_ - 1 - i]);
        }
        for (int i = 0; i < stages_ - 1; ++i) {
          CHECK_EQ(b_[i + 1], b_[stages_ - 1 - i]);
        }
        break;
      case kBAB:
        for (int i = 0; i < stages_ - 1; ++i) {
          CHECK_EQ(a_[i], a_[stages_ - 2 - i]);
        }
        for (int i = 0; i < stages_; ++i) {
          CHECK_EQ(b_[i], b_[stages_ - 1 - i]);
        }
        break;
      case kBA:
        LOG(FATAL) << "Time-reversible compositions have the FSAL property";
        break;
      default:
        LOG(FATAL) << "Invalid CompositionMethod";
    }
  }
}
示例#22
0
// Not all entries in the buffer represent a valid row. Advance the internal cursor
// used for the getNextRow method to the next row which is valid.
size_t ResultSet::advanceCursorToNextEntry() const {
  while (crt_row_buff_idx_ < entryCount()) {
    const auto entry_idx = permutation_.empty() ? crt_row_buff_idx_ : permutation_[crt_row_buff_idx_];
    const auto storage_lookup_result = findStorage(entry_idx);
    const auto storage = storage_lookup_result.storage_ptr;
    const auto fixedup_entry_idx = storage_lookup_result.fixedup_entry_idx;
    if (!storage->isEmptyEntry(fixedup_entry_idx)) {
      break;
    }
    ++crt_row_buff_idx_;
  }
  if (permutation_.empty()) {
    return crt_row_buff_idx_;
  }
  CHECK_LE(crt_row_buff_idx_, permutation_.size());
  return crt_row_buff_idx_ == permutation_.size() ? crt_row_buff_idx_ : permutation_[crt_row_buff_idx_];
}
void EuclideanLossWithIgnoreLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
				const vector<Blob<Dtype>*>& top){
		LossLayer<Dtype>::LayerSetUp(bottom, top);
		EuclideanLossParameter euclid_loss = this->layer_param_.euclideanloss_param();
		is_normalize_     = euclid_loss.is_normalized();
		nc_               = euclid_loss.normalize_choice(); 
		CHECK_GE(nc_, 0) << "normalize_choice can be 0 or 1";
		CHECK_LE(nc_, 1) << "normalize_choice can be 0 or 1"; 
		
		/*
		LOG(INFO) << "Setup Euclidean " << "Normalize: " << is_normalize_ << " nc_: " << nc_;
		if (is_normalize_)
			LOG(INFO) << "normalization is ON";
		else
			LOG(INFO) << "normalization is OFF";
		*/
}
示例#24
0
void RmseLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
    vector<Blob<Dtype>*>* top) {
  num_rating_ = bottom[2]->cpu_data()[0];
  int count = bottom[0]->count();
  CHECK_LE(num_rating_, count) << "assigned rating length exceed boundary.";

  // const Dtype* data = bottom[0]->cpu_data();
  // std::cout << "data" << std::endl;
  // for (int i = 0; i < 10; i++){
  //   std::cout << data[i] << "\t";
  // }
  // std::cout << std::endl;
  // const Dtype* label = bottom[1]->cpu_data();
  // std::cout << "label" << std::endl;
  // for (int i = 0; i < 10; i++){
  //   std::cout << label[i] << "\t";
  // }
  // std::cout << std::endl;

  caffe_sub(
      num_rating_,
      bottom[0]->cpu_data(),
      bottom[1]->cpu_data(),
      diff_.mutable_cpu_data());

  if (bias_!=0) {
    caffe_add_scalar(num_rating_, bias_, diff_.mutable_cpu_data());
  }

  // const Dtype* diff_cpu = diff_.cpu_data();
  // std::cout << "diff_cpu" << std::endl;
  // for (int i = 0; i < 10; i++){
  //   std::cout << diff_cpu[i] << "\t";
  // }
  // std::cout << std::endl;

  Dtype dot = caffe_cpu_dot(num_rating_, diff_.cpu_data(), diff_.cpu_data());

  // std::cout << "dot:" << dot << std::endl;

  // Dtype loss = dot / bottom[0]->num() / Dtype(2);
  Dtype loss = sqrt(dot / num_rating_); // rmse, temp for movielens.
  (*top)[0]->mutable_cpu_data()[0] = loss;
  // LOG(INFO) << "loss:" << loss << " num_rating_:" << num_rating_;
}
示例#25
0
// Constructor from OpenCV image.
ImageData::ImageData(const cv::Mat& image) {
  // Make sure all pixels are within some valid range.
  double min_pixel_value, max_pixel_value;
  cv::minMaxLoc(image, &min_pixel_value, &max_pixel_value);
  CHECK_GE(min_pixel_value, 0)
      << "Invalid pixel range in given image: values cannot be negative. "
      << "Use ImageData(cv::Mat&, false) to avoid normalization, where any "
      << "image values are okay.";
  CHECK_LE(max_pixel_value, 255)
      << "Invalid pixel range in given image: values cannot exceed 255."
      << "Use ImageData(cv::Mat&, false) to avoid normalization, where any "
      << "image values are okay.";

  const ImageNormalizeMode normalize_mode =
      (max_pixel_value > 1.0) ? NORMALIZE_IMAGE : DO_NOT_NORMALIZE_IMAGE;
  InitializeFromImage(image, normalize_mode, &image_size_, &channels_);
  spectral_mode_ = GetDefaultSpectralMode(channels_.size());
}
示例#26
0
void DataTransformer<Dtype>::Transform(const vector<Datum> & datum_vector,
                                       Blob<Dtype>* transformed_blob) {
  const int datum_num = datum_vector.size();
  const int num = transformed_blob->num();
  const int channels = transformed_blob->channels();
  const int height = transformed_blob->height();
  const int width = transformed_blob->width();

  CHECK_GT(datum_num, 0) << "There is no datum to add";
  CHECK_LE(datum_num, num) <<
    "The size of datum_vector must be no greater than transformed_blob->num()";
  Blob<Dtype> uni_blob(1, channels, height, width);
  for (int item_id = 0; item_id < datum_num; ++item_id) {
    int offset = transformed_blob->offset(item_id);
    uni_blob.set_cpu_data(transformed_blob->mutable_cpu_data() + offset);
    Transform(datum_vector[item_id], &uni_blob);
  }
}
示例#27
0
void Solver::InitTrainNet() {
  const int num_train_nets = param_.has_net() + param_.has_net_param() +
      param_.has_train_net() + param_.has_train_net_param();
  const string& field_names = "net, net_param, train_net, train_net_param";
  CHECK_GE(num_train_nets, 1) << "SolverParameter must specify a train net "
      << "using one of these fields: " << field_names;
  CHECK_LE(num_train_nets, 1) << "SolverParameter must not contain more than "
      << "one of these fields specifying a train_net: " << field_names;
  NetParameter net_param;
  if (param_.has_train_net_param()) {
    LOG_IF(INFO, Caffe::root_solver())
        << "Creating training net specified in train_net_param.";
    net_param.CopyFrom(param_.train_net_param());
  } else if (param_.has_train_net()) {
    LOG_IF(INFO, Caffe::root_solver())
        << "Creating training net from train_net file: " << param_.train_net();
    ReadNetParamsFromTextFileOrDie(param_.train_net(), &net_param);
  }
  if (param_.has_net_param()) {
    LOG_IF(INFO, Caffe::root_solver())
        << "Creating training net specified in net_param.";
    net_param.CopyFrom(param_.net_param());
  }
  if (param_.has_net()) {
    LOG_IF(INFO, Caffe::root_solver())
        << "Creating training net from net file: " << param_.net();
    ReadNetParamsFromTextFileOrDie(param_.net(), &net_param);
  }
  // Set the correct NetState.  We start with the solver defaults (lowest
  // precedence); then, merge in any NetState specified by the net_param itself;
  // finally, merge in any NetState specified by the train_state (highest
  // precedence).
  NetState net_state;
  net_state.set_phase(TRAIN);
  net_state.MergeFrom(net_param.state());
  net_state.MergeFrom(param_.train_state());
  net_param.mutable_state()->CopyFrom(net_state);
  if (Caffe::root_solver()) {
    net_.reset(new Net(net_param, rank_, &init_flag_, &iter0_flag_));
  } else {
    net_.reset(new Net(net_param, rank_, &init_flag_, &iter0_flag_,
        root_solver_->net_.get()));
  }
}
示例#28
0
void AString::insert(const char *from, size_t size, size_t insertionPos) {
    CHECK_GE(insertionPos, 0u);
    CHECK_LE(insertionPos, mSize);

    makeMutable();

    if (mSize + size + 1 > mAllocSize) {
        mAllocSize = (mAllocSize + size + 31) & -32;
        mData = (char *)realloc(mData, mAllocSize);
        CHECK(mData != NULL);
    }

    memmove(&mData[insertionPos + size],
            &mData[insertionPos], mSize - insertionPos + 1);

    memcpy(&mData[insertionPos], from, size);

    mSize += size;
}
void ConcatLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) {
  CHECK_GT(bottom.size(), 1) <<
    "Concat Layer takes at least two blobs as input.";
  CHECK_EQ(top->size(), 1) <<
    "Concat Layer takes a single blob as output.";
  concat_dim_ = this->layer_param_.concat_param().concat_dim();
  CHECK_GE(concat_dim_, 0) << "concat_dim should be >= 0";
  //lipengyu delete
  /*CHECK_LE(concat_dim_, 1) <<
    "For now concat_dim <=1, it can only concat num and channels";*/
  //lipengyu add
  CHECK_LE(concat_dim_, 4) <<
    "For now concat_dim <=1, it can only concat num and channels";

  // Intialize with the first blob
  count_ = bottom[0]->count();
  num_ = bottom[0]->num();
  channels_ = bottom[0]->channels();
  height_ = bottom[0]->height();
  width_ = bottom[0]->width();
  for (int i = 1; i < bottom.size(); ++i) {
    count_ += bottom[i]->count();
    if (concat_dim_== 0) {
      num_ += bottom[i]->num();
    } else if (concat_dim_ == 1) {
      channels_ += bottom[i]->channels();
    } else if (concat_dim_ == 2) {
      height_ += bottom[i]->height();
    } else if (concat_dim_ == 3) {
      width_ += bottom[i]->width();
    }
  }
  if(concat_dim_ == 4)//lipengyu add
  {
	  channels_ = count_ / num_;
	  (*top)[0]->Reshape(num_, channels_, 1, 1);// lipengyu add
  }
  else
	(*top)[0]->Reshape(num_, channels_, height_, width_);
  CHECK_EQ(count_, (*top)[0]->count());
}
示例#30
0
void RandomRotateImage(const cv::Mat& src_img, const int rotation_range,
    const float rescale_factor, const vector<int> & border_value,
    cv::Mat* dst_img) {
  CHECK_GT(rescale_factor, 0);
  CHECK_GE(rotation_range, 0);
  CHECK_LE(rotation_range, 360);
  CHECK(border_value.size() ==  src_img.channels() ||
      border_value.size() == 1 || border_value.size() == 0)
      << "border value can either has the dimension"
      << "of 1 or euqals to the channels dimension of the input image";
  cv::Scalar scalar;
  switch (border_value.size()) {
    case 0:
      scalar = cv::Scalar(0);
      break;
    case 1:
      scalar = cv::Scalar(border_value[0]);
      break;
    case 2:
      scalar = cv::Scalar(border_value[0], border_value[1]);
      break;
    case 3:
      scalar = cv::Scalar(border_value[0], border_value[1], border_value[2]);
      break;
    case 4:
      scalar = cv::Scalar(border_value[0], border_value[1],
          border_value[3], border_value[4]);
      break;
    default:
      LOG(FATAL) << "border value can only be equal or less then 4";
  }
  cv::Point2f pt(src_img.cols/2, src_img.rows/2);
  cv::Mat r;
  if (rotation_range != 0) {
    r = cv::getRotationMatrix2D(pt,
        caffe_rng_rand()%rotation_range-rotation_range/2, rescale_factor);
  } else {
    r = cv::getRotationMatrix2D(pt, 0, rescale_factor);
  }
  cv::warpAffine(src_img, *dst_img, r, cv::Size(src_img.cols, src_img.rows),
      cv::INTER_LINEAR, cv::BORDER_CONSTANT, scalar);
}