Skip to content

Caffe:Develop

Caffe 개발을 위한 소스코드 분석을 포함한다.

Blob

#Layer, #Net, #Solver 객체와 상호작용하는 #SyncedMemory의 Wrapper class.

  • Header: include/caffe/blob.hpp
  • Source: src/caffe/blob.cpp

이 객체를 알기 위해서, 우선 #SyncedMemory를 알아야 한다. 특징은 아래와 같다.

  • 4차원 배열.
  • 데이터를 저장하고 모듈간 통신에 사용됨.
  • 메모리 인터페이스를 제공하며, CPU/GPU간 동기화 및 이종 연산에 대한 고민을 하지 않도록 한다.
  • 모델은 Google Protocol Buffers 에 의해 저장됨.
  • 대용량 데이터셋은 LevelDB에 저장됨.

Member variables

protected:
shared_ptr<SyncedMemory> data_;
shared_ptr<SyncedMemory> diff_;
shared_ptr<SyncedMemory> shape_data_;
vector<int> shape_;
int count_;
int capacity_;

Layer

특징은 아래와 같다.

  • 하나 또는 다수의 #Blob을 입력으로 하고, 하나 또는 다수의 #Blob을 출력으로 하는 네트워크(#Net)의 구성 단위.
  • Forward Pass, Backward Pass를 갖고 있고, Layer 파라미터들의 증감(Gradient)을 계산, 최종 출력의 오차를 역전파(Back probagation)한다.
  • Convolution, Pooling, Inner products, Rectified linear, Logistic, Local response normalization, Element-wise operations, Losses like softmax and hinge 등의 형태를 가지는 Layer제공.

Net

Solver

SyncedMemory

Manages memory allocation and synchronization between the host (CPU) and device (GPU).

  • Header: include/caffe/syncedmem.hpp
  • Source: src/caffe/syncedmem.cpp

CPU메모리와 GPU메모리에 대한 할당/해제 연산이 정의되어있다.

Member variables

  • void* cpu_ptr_;: CPU를 위한 메모리 포인터.
  • void* gpu_ptr_;: GPU를 위한 메모리 포인터.
  • size_t size_;: 할당된 메모리 크기.

util/math_functions

주로 아래와 같이 GPU(CUDA)를 위한 API Wrapper가 존재한다.

inline void caffe_gpu_memset(const size_t N, const int alpha, void* X) {
#ifndef CPU_ONLY
    CUDA_CHECK(cudaMemset(X, alpha, N));  // NOLINT(caffe/alt_fn)
#else
    NO_GPU;
#endif
}

See also

Favorite site