Skip to content

Commit

Permalink
Merge pull request #29 from WICWIU/GAN
Browse files Browse the repository at this point in the history
Debug GAN tutorials
  • Loading branch information
DonghweeYoon committed Jan 23, 2020
2 parents 13c97c8 + 5117e14 commit 317fb41
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 149 deletions.
46 changes: 25 additions & 21 deletions WICWIU_src/BEGAN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ template<typename DTYPE> class BEGAN : public GAN<DTYPE> {
int MultiplyKOnGradient();

float UpdateK();
float GetK();

float ComputeConvergenceMeasure();
float GetConvergenceMeasure();
};

template<typename DTYPE> BEGAN<DTYPE>::BEGAN() : GAN<DTYPE>() {
Expand Down Expand Up @@ -76,9 +78,9 @@ template<typename DTYPE> int BEGAN<DTYPE>::TrainGeneratorOnCPU(){

this->GetSwitch()->SetSwitchNumber(FAKE);
this->ForwardPropagate();
// std::cout << ", G : D(G(z)) = " << (*this->GetDiscriminator()->GetResult()->GetResult())[Index5D(this->GetDiscriminator()->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)] << '\n';
this->GetGeneratorLossFunction()->ForwardPropagate();
this->SaveLossG();

this->GetGeneratorLossFunction()->BackPropagate();
this->BackPropagate();

Expand All @@ -97,9 +99,8 @@ template<typename DTYPE> int BEGAN<DTYPE>::ComputeGradientOfDiscriminatorAboutRe
this->GetSwitch()->SetSwitchNumber(REAL);
this->GetSwitch()->ForwardPropagate();
this->GetDiscriminator()->ForwardPropagate();
// std::cout << "D: D(x) = " << (*this->GetDiscriminator()->GetResult()->GetResult())[Index5D(this->GetDiscriminator()->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
this->GetDiscriminatorLossFunction()->ForwardPropagate();
this->SaveLossX();

this->GetDiscriminatorLossFunction()->BackPropagate();
this->GetDiscriminator()->BackPropagate();
}
Expand All @@ -113,10 +114,11 @@ template<typename DTYPE> int BEGAN<DTYPE>::ComputeGradientOfDiscriminatorAboutFa
this->AllocLabel(FAKE);
this->GetSwitch()->SetSwitchNumber(FAKE);
this->ForwardPropagate();
// std::cout << ", D : D(G(z)) = " << (*this->GetDiscriminator()->GetResult()->GetResult())[Index5D(this->GetDiscriminator()->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];

// this->MultiplyKOnOutput();
this->MultiplyKOnOutput();
this->GetDiscriminatorLossFunction()->ForwardPropagate();
this->SaveLossX();

this->GetDiscriminatorLossFunction()->BackPropagate();
this->MultiplyKOnGradient();

Expand All @@ -132,9 +134,9 @@ template<typename DTYPE> int BEGAN<DTYPE>::TrainGeneratorOnGPU(){

this->GetSwitch()->SetSwitchNumber(FAKE);
this->ForwardPropagateOnGPU();
// std::cout << ", G : D(G(z)) = " << (*this->GetDiscriminator()->GetResult()->GetResult())[Index5D(this->GetDiscriminator()->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)] << '\n';
this->GetGeneratorLossFunction()->ForwardPropagateOnGPU();
this->SaveLossG();

this->GetGeneratorLossFunction()->BackPropagateOnGPU();
this->BackPropagateOnGPU();

Expand All @@ -150,19 +152,16 @@ template<typename DTYPE> int BEGAN<DTYPE>::TrainGeneratorOnGPU(){

template<typename DTYPE> int BEGAN<DTYPE>::ComputeGradientOfDiscriminatorAboutRealOnGPU(){
this->ResetResult();
this->ResetGradient();
// this->GetDiscriminator()->ResetGradient();
this->GetDiscriminator()->ResetGradient();
this->ResetDiscriminatorLossFunctionResult();
this->ResetDiscriminatorLossFunctionGradient();

this->AllocLabel(REAL);
this->GetSwitch()->SetSwitchNumber(REAL);
this->GetSwitch()->ForwardPropagateOnGPU();
this->GetDiscriminator()->ForwardPropagateOnGPU();
// std::cout << "\nD: D(x) = " << (*this->GetDiscriminator()->GetResult()->GetResult())[Index5D(this->GetDiscriminator()->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];

this->GetDiscriminatorLossFunction()->ForwardPropagateOnGPU();
this->SaveLossX();

this->GetDiscriminatorLossFunction()->BackPropagateOnGPU();
this->GetDiscriminator()->BackPropagateOnGPU();

Expand All @@ -171,18 +170,17 @@ template<typename DTYPE> int BEGAN<DTYPE>::ComputeGradientOfDiscriminatorAboutRe

template<typename DTYPE> int BEGAN<DTYPE>::ComputeGradientOfDiscriminatorAboutFakeOnGPU(){
this->ResetResult();
this->ResetGradient();
// this->GetDiscriminator()->ResetGradient();
this->ResetDiscriminatorLossFunctionResult();
this->GetDiscriminator()->ResetGradient();
// this->ResetDiscriminatorLossFunctionResult();
this->ResetDiscriminatorLossFunctionGradient();

this->AllocLabel(FAKE);
this->GetSwitch()->SetSwitchNumber(FAKE);
this->ForwardPropagateOnGPU();
// std::cout << ", D : D(G(z)) = " << (*this->GetDiscriminator()->GetResult()->GetResult())[Index5D(this->GetDiscriminator()->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];

// this->MultiplyKOnOutput();
this->MultiplyKOnOutput();
this->GetDiscriminatorLossFunction()->ForwardPropagateOnGPU();
this->SaveLossX();

this->GetDiscriminatorLossFunction()->BackPropagateOnGPU();
this->MultiplyKOnGradient();
this->GetDiscriminator()->BackPropagateOnGPU();
Expand All @@ -199,30 +197,36 @@ template<typename DTYPE> float BEGAN<DTYPE>::SaveLossG(){
}

template<typename DTYPE> int BEGAN<DTYPE>::MultiplyKOnOutput(){
this->GetDiscriminator()->GetResult()->GetResult()->MultiplyScalar(0, m_k);
this->GetDiscriminator()->GetResultOperator()->GetResult()->MultiplyScalar(0, m_k);
return TRUE;
}

template<typename DTYPE> int BEGAN<DTYPE>::MultiplyKOnGradient(){
this->GetDiscriminator()->GetResult()->GetGradient()->MultiplyScalar(0, m_k);
this->GetDiscriminator()->GetResultOperator()->GetGradient()->MultiplyScalar(0, m_k);
return TRUE;
}

template<typename DTYPE> float BEGAN<DTYPE>::UpdateK(){
printf("m_k = %f, m_LossX = %f, m_LossG = %f, ", m_k, m_LossX, m_LossG);
m_k = m_k + m_lamda * (m_gamma * m_LossX - m_LossG);
if(m_k > 1.f) m_k = 1.f;
if(m_k < 0.f) m_k = 0.f;

return m_k;
}

template<typename DTYPE> float BEGAN<DTYPE>::GetK(){
return m_k;
}

template<typename DTYPE> float BEGAN<DTYPE>::ComputeConvergenceMeasure(){
m_ConvergenceMeasure = m_LossX + std::abs(m_gamma * m_LossX - m_LossG);
printf("m_ConvergenceMeasure = %f\n", m_ConvergenceMeasure);
return m_ConvergenceMeasure;
}

template<typename DTYPE> float BEGAN<DTYPE>::GetConvergenceMeasure(){
return m_ConvergenceMeasure;
}

#endif // BEGAN_H_

//////////////////////////////////////////
22 changes: 8 additions & 14 deletions WICWIU_src/GAN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ template<typename DTYPE> class GAN : public NeuralNetwork<DTYPE> {
int TrainGenerator();
int TrainDiscriminator();

int Test();
int Generate();


virtual int TrainGeneratorOnCPU();
Expand All @@ -67,7 +67,7 @@ template<typename DTYPE> class GAN : public NeuralNetwork<DTYPE> {
virtual int ComputeGradientOfDiscriminatorAboutRealOnCPU();
virtual int ComputeGradientOfDiscriminatorAboutFakeOnCPU();

int TestOnCPU();
int GenerateOnCPU();


virtual int TrainGeneratorOnGPU();
Expand All @@ -76,7 +76,7 @@ template<typename DTYPE> class GAN : public NeuralNetwork<DTYPE> {
virtual int ComputeGradientOfDiscriminatorAboutRealOnGPU();
virtual int ComputeGradientOfDiscriminatorAboutFakeOnGPU();

int TestOnGPU();
int GenerateOnGPU();


int ResetParameterGradient();
Expand Down Expand Up @@ -258,11 +258,11 @@ template<typename DTYPE> int GAN<DTYPE>::TrainDiscriminator(){
} else return FALSE;
}

template<typename DTYPE> int GAN<DTYPE>::Test() {
template<typename DTYPE> int GAN<DTYPE>::Generate() {
if(this->GetDevice() == CPU) {
this->TestOnCPU();
this->GenerateOnCPU();
} else if(this->GetDevice() == GPU) {
this->TestOnGPU();
this->GenerateOnGPU();
} else return FALSE;
}

Expand All @@ -274,7 +274,6 @@ template<typename DTYPE> int GAN<DTYPE>::TrainGeneratorOnCPU(){

m_pSwitch->SetSwitchNumber(FAKE);
this->ForwardPropagate();
std::cout << ", G : D(G(z)) = " << (*m_pDiscriminator->GetResult()->GetResult())[Index5D(m_pDiscriminator->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)] << '\n';
m_pGeneratorLossFunction->ForwardPropagate();
m_pGeneratorLossFunction->BackPropagate();
this->BackPropagate();
Expand Down Expand Up @@ -303,7 +302,6 @@ template<typename DTYPE> int GAN<DTYPE>::ComputeGradientOfDiscriminatorAboutReal
m_pSwitch->SetSwitchNumber(REAL);
m_pSwitch->ForwardPropagate();
m_pDiscriminator->ForwardPropagate();
std::cout << "\r\rD: D(x) = " << (*m_pDiscriminator->GetResult()->GetResult())[Index5D(m_pDiscriminator->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
m_pDiscriminatorLossFunction->ForwardPropagate();
m_pDiscriminatorLossFunction->BackPropagate();
m_pDiscriminator->BackPropagate();
Expand All @@ -320,15 +318,14 @@ template<typename DTYPE> int GAN<DTYPE>::ComputeGradientOfDiscriminatorAboutFake
this->AllocLabel(FAKE);
m_pSwitch->SetSwitchNumber(FAKE);
this->ForwardPropagate();
std::cout << ", D : D(G(z)) = " << (*m_pDiscriminator->GetResult()->GetResult())[Index5D(m_pDiscriminator->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
m_pDiscriminatorLossFunction->ForwardPropagate();
m_pDiscriminatorLossFunction->BackPropagate();
m_pDiscriminator->BackPropagate();

return TRUE;
}

template<typename DTYPE> int GAN<DTYPE>::TestOnCPU(){
template<typename DTYPE> int GAN<DTYPE>::GenerateOnCPU(){
m_pGenerator->ResetResult();
m_pGenerator->ForwardPropagate();

Expand All @@ -345,7 +342,6 @@ template<typename DTYPE> int GAN<DTYPE>::TrainGeneratorOnGPU(){

m_pSwitch->SetSwitchNumber(FAKE);
this->ForwardPropagateOnGPU();
std::cout << ", G : D(G(z)) = " << (*m_pDiscriminator->GetResult()->GetResult())[Index5D(m_pDiscriminator->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
m_pGeneratorLossFunction->ForwardPropagateOnGPU();
m_pGeneratorLossFunction->BackPropagateOnGPU();
this->BackPropagateOnGPU();
Expand Down Expand Up @@ -385,7 +381,6 @@ template<typename DTYPE> int GAN<DTYPE>::ComputeGradientOfDiscriminatorAboutReal
m_pSwitch->SetSwitchNumber(REAL);
m_pSwitch->ForwardPropagateOnGPU();
m_pDiscriminator->ForwardPropagateOnGPU();
std::cout << "\r\rD: D(x) = " << (*m_pDiscriminator->GetResult()->GetResult())[Index5D(m_pDiscriminator->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
m_pDiscriminatorLossFunction->ForwardPropagateOnGPU();
m_pDiscriminatorLossFunction->BackPropagateOnGPU();
m_pDiscriminator->BackPropagateOnGPU();
Expand All @@ -402,15 +397,14 @@ template<typename DTYPE> int GAN<DTYPE>::ComputeGradientOfDiscriminatorAboutFake
this->AllocLabel(FAKE);
m_pSwitch->SetSwitchNumber(FAKE);
this->ForwardPropagateOnGPU();
std::cout << ", D : D(G(z)) = " << (*m_pDiscriminator->GetResult()->GetResult())[Index5D(m_pDiscriminator->GetResult()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
m_pDiscriminatorLossFunction->ForwardPropagateOnGPU();
m_pDiscriminatorLossFunction->BackPropagateOnGPU();
m_pDiscriminator->BackPropagateOnGPU();

return TRUE;
}

template<typename DTYPE> int GAN<DTYPE>::TestOnGPU(){
template<typename DTYPE> int GAN<DTYPE>::GenerateOnGPU(){
#ifdef __CUDNN__
this->ResetResult();
this->ForwardPropagateOnGPU();
Expand Down
Empty file.
64 changes: 44 additions & 20 deletions tutorials/GAN/BEGAN/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define BATCH 64
#define EPOCH 10000
#define LOOP_FOR_TRAIN (202599 / BATCH)
#define LOOP_FOR_TEST 10//(10000 / BATCH)
#define LOOP_FOR_GENERATE (10000 / BATCH)
#define LOOP_FOR_TRAIN_DISC 1
#define GPUID 2

Expand Down Expand Up @@ -47,17 +47,11 @@ int main(int argc, char const *argv[]) {

net->PrintGraphInformation();

float bestGenLoss = 0.f;
float bestDiscLoss = 0.f;
int epoch = 0;

// @ When load parameters
// net->Load(filename);

std::cout << "bestGenLoss : " << bestGenLoss << '\n';
std::cout << "bestDiscLoss : " << bestDiscLoss << '\n';
std::cout << "epoch : " << epoch << '\n';

//Start make Noise
Gnoise->StartProduce();

Expand Down Expand Up @@ -95,26 +89,56 @@ int main(int argc, char const *argv[]) {
net->UpdateK();
net->ComputeConvergenceMeasure();

genLoss = (*net->GetGeneratorLossFunction()->GetResult())[Index5D(net->GetGeneratorLossFunction()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
discLoss = (*net->GetDiscriminatorLossFunction()->GetResult())[Index5D(net->GetDiscriminatorLossFunction()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];

printf("Train complete percentage is %d / %d -> Generator Loss : %f, Discriminator Loss : %f\n",
j + 1,
LOOP_FOR_TRAIN,
genLoss,
discLoss);
// fflush(stdout);
if(j % 50 == 0){
string filePath = "generated/epoch" + std::to_string(i) + "_" + std::to_string(j) + ".jpg";
const char *cstr = filePath.c_str();
Tensor2Image<float>(net->GetGenerator()->GetResult(), cstr, 3, 64, 64);
if((j + 1) % 50 == 0){
genLoss = (*net->GetGeneratorLossFunction()->GetResult())[Index5D(net->GetGeneratorLossFunction()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];
discLoss = (*net->GetDiscriminatorLossFunction()->GetResult())[Index5D(net->GetDiscriminatorLossFunction()->GetResult()->GetShape(), 0, 0, 0, 0, 0)];

printf("\rTrain complete percentage is %d / %d -> Generator Loss: %f, Discriminator Loss: %f, K: %f, Convergence measure: %f",
j + 1,
LOOP_FOR_TRAIN,
genLoss,
discLoss,
net->GetK(),
net->GetConvergenceMeasure());
fflush(stdout);

string filePath = "trained/epoch" + std::to_string(i) + "_" + std::to_string(j + 1) + ".jpg";
const char *cstr = filePath.c_str();
Tensor2Image<float>(net->GetGenerator()->GetResult(), cstr, 3, 64, 64);
}
}

endTime = clock();
nProcessExcuteTime = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
printf("\n(excution time per epoch : %f)\n\n", nProcessExcuteTime);

// ======================= Generate(Save Generated Image)======================
float generateGenLoss = 0.f;
float generateDiscLoss = 0.f;

net->SetModeInference();

for (int j = 0; j < (int)LOOP_FOR_GENERATE; j++) {
Tensor<float> *z_t = Gnoise->GetNoiseFromBuffer();

#ifdef __CUDNN__
z_t->SetDeviceGPU(GPUID);
#endif // __CUDNN__

net->FeedInputTensor(1, z_t);
net->Generate();

string filePath = "generated/epoch" + std::to_string(i) + "_" + std::to_string(j + 1) + ".jpg";
const char *cstr = filePath.c_str();
Tensor2Image<float>(net->GetGenerator()->GetResult(), cstr, 3, 64, 64);

printf("\rGenerate complete percentage is %d / %d",
j + 1,
LOOP_FOR_GENERATE);
fflush(stdout);
}
std::cout << "\n\n";

net->Save(filename);
}

Expand Down
2 changes: 1 addition & 1 deletion tutorials/GAN/BEGAN/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ clean:
rm -rf *.o main

clean_img:
rm trained/*
rm generated/*
rm evaluated/*

clean_all:
rm -rf *.o main
Expand Down
Empty file.
Loading

0 comments on commit 317fb41

Please sign in to comment.