Skip to content

Commit

Permalink
update Tensor & add Layer class
Browse files Browse the repository at this point in the history
  • Loading branch information
yester31 committed Aug 21, 2021
1 parent e0ae71b commit 716587e
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 84 deletions.
2 changes: 1 addition & 1 deletion DL_LAYER/Activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/***************************************************************************
Activateion function algotirhm (Tanh, ReLU, Sigmoid)
****************************************************************************/
#include "Utils.h"
#include "Tensor.h"

enum act_func{Linear, Sigmoid, Tanh, ReLU, Swish, LeakRelu};

Expand Down
5 changes: 3 additions & 2 deletions DL_LAYER/Conv2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
/***************************************************************************
Conventional Convolution algotirhm (without any option)
****************************************************************************/
#include "Utils.h"
#include "Tensor.h"

// layer(functional) class 필요 ***
Tensor convolution(Tensor &inTensor, int KH, int KW, int stride, int OC) {

// 1. weight 존재 유무 체크 없으면 -> 초기화 or 주입(전달)
Tensor wTensor = Tensor(1,2,3,4);
Tensor wTensor1(1,2,3,4);
Tensor wTensor = wTensor1;
// weight 초기화함수 필요 ***


Expand Down
5 changes: 3 additions & 2 deletions DL_LAYER/DL_LAYER.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<ClCompile Include="FullyConnected.cpp" />
<ClCompile Include="Pool2d.cpp" />
<ClCompile Include="SoftMax.cpp" />
<ClCompile Include="Utils.cpp">
<ClCompile Include="Tensor.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="WeightInitializer.cpp">
Expand All @@ -174,7 +174,8 @@
<ClCompile Include="ZeroPad.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Utils.h" />
<ClInclude Include="Layer.h" />
<ClInclude Include="Tensor.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
11 changes: 7 additions & 4 deletions DL_LAYER/DL_LAYER.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
<ClCompile Include="ZeroPad.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="Utils.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="Activation.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
Expand All @@ -45,9 +42,15 @@
<ClCompile Include="DataLoader.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="Tensor.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Utils.h">
<ClInclude Include="Tensor.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="Layer.h">
<Filter>헤더 파일</Filter>
</ClInclude>
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion DL_LAYER/DataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <iostream>
#include <string>
#include <vector>
#include "Utils.h"
#include "Tensor.h"
#include "opencv2/opencv.hpp"

using namespace cv;
Expand Down
3 changes: 1 addition & 2 deletions DL_LAYER/Eltwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
/***************************************************************************
Pooling algotirhm (avg & max)
****************************************************************************/

#include "Utils.h"
#include "Tensor.h"

void eltwiseSum(vector<float>& output, vector<float>& input1, vector<float>& input2)
{
Expand Down
2 changes: 1 addition & 1 deletion DL_LAYER/FullyConnected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/***************************************************************************
FullyConnected algotirhm
****************************************************************************/
#include "Utils.h"
#include "Tensor.h"

void fullyConnected(vector<float>& Output, vector<float>& Input, vector<float>& Weight, int out_features, int input_n, int input_c, int input_h, int input_w) {

Expand Down
119 changes: 119 additions & 0 deletions DL_LAYER/Layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#pragma once
#include "Tensor.h"

class Layer
{
public:

Layer()
{}

Layer(Layer &layer)
{
*this = layer;
}

virtual Tensor calculate(Tensor output, Tensor input);
virtual void initWeight();
~Layer() {}

public:
Tensor weight;
string name;

};

class ConvolutionLayer : public Layer {
public:

ConvolutionLayer(ConvolutionLayer &layer)
{
*this = layer;
}

ConvolutionLayer(int C, int K, int KH, int KW, int Stride, bool bias = false)
: C(C), K(K), KH(KH), KW(KW), stride(Stride)
{
name = "Convolution Layer";
initWeight();
}

Tensor calculate(Tensor output, Tensor input)
{
// 1. weight tensor
float* weight_data = weight.getData();

// 2. input tensor
float* input_data = input.getData();
int IN = input.getShape()[0];
int IC = input.getShape()[1];
int IH = input.getShape()[2];
int IW = input.getShape()[3];

// 3. output tenosr 재설정
int OH = ((IH - KH) / stride) + 1;
int OW = ((IW - KW) / stride) + 1;
output.reConstruct(IN, K, OH, OW);
float* output_data = output.getData();

// 4. 연산 수행
std::cout << "===== Convolution ===== \n" << std::endl;

int C_offset_i, C_offset_o, C_offset_k, H_offset_i, H_offset_o, H_offset_k, W_offset_i, W_offset_o, W_offset_k, ⁠g_idx_i, g_idx_o, g_idx_k;
int N_offset_i = IC * IH * IW;
int N_offset_o = K * OH * OW;
int N_offset_k = IC * KH * KW;
for (int ⁠n_idx = 0; ⁠n_idx < IN; ⁠n_idx++) {
C_offset_i = ⁠n_idx * N_offset_i;
C_offset_o = ⁠n_idx * N_offset_o;
for (int k_idx = 0; k_idx < K; k_idx++) {
C_offset_k = k_idx * N_offset_k;
H_offset_o = k_idx * OH * OW + C_offset_o;
for (int ⁠c_idx = 0; ⁠c_idx < IC; ⁠c_idx++) {
H_offset_i = ⁠c_idx * IH * IW + C_offset_i;
H_offset_k = ⁠c_idx * KH * KW + C_offset_k;
for (int rowStride = 0; rowStride < OH; rowStride++) {
W_offset_o = rowStride * OW + H_offset_o;
for (int colStride = 0; colStride < OW; colStride++) {
float sum = 0;
g_idx_o = colStride + W_offset_o;
for (int y = rowStride * stride; y < rowStride * stride + KH; y++) {
W_offset_i = y * IW + H_offset_i;
W_offset_k = (y - rowStride * stride) * KH + H_offset_k;
for (int x = colStride * stride; x < colStride * stride + KW; x++) {
⁠g_idx_i = x + W_offset_i;
g_idx_k = (x - colStride * stride) + W_offset_k;
sum += input_data[⁠g_idx_i] * weight_data[g_idx_k];
}
}
output_data[g_idx_o] += sum;
}
}
}
}
}
}

// kaiming HE Uniform initialization
void initWeight()
{
vector<int> weightShape{K, C, KH, KW};
weight.setShape(weightShape);
float min = -sqrt(5);
float max = sqrt(5);
mt19937 random((random_device()()));
uniform_real_distribution<float> dist(min, max);

std::cout << "===== Init Weight (kaiming HE Uniform) =====" << std::endl;
for (int i = 0; i < weight.getTotSize(); i++) {
weight.getData()[i] = dist(random);
}
}

public:
int KH;
int KW;
int stride;
int K;
int C;
};
2 changes: 1 addition & 1 deletion DL_LAYER/Pool2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/***************************************************************************
Pooling algotirhm (avg & max)
****************************************************************************/
#include "Utils.h"
#include "Tensor.h"


void avgPooling(vector<float>& avgPoolingOutput, vector<float>& avgPoolingInput, int input_n, int input_c, int input_h, int input_w, int poolingWindow, int poolingStride, int poolingOutputHeight, int poolingOutputWidth) {
Expand Down
2 changes: 1 addition & 1 deletion DL_LAYER/SoftMax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/***************************************************************************
FullyConnected algotirhm
****************************************************************************/
#include "Utils.h"
#include "Tensor.h"

void softMax(vector<float>& softMaxOutput, vector<float>& softMaxInput, int input_n, int lastNodeNumber)
{
Expand Down
2 changes: 1 addition & 1 deletion DL_LAYER/Utils.cpp → DL_LAYER/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <iomanip> // for setw()
#include <fstream>
#include "Utils.h"
#include "Tensor.h"
using namespace std;

void Tensor::valueCheck() {
Expand Down
Loading

0 comments on commit 716587e

Please sign in to comment.