Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 #1

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open

V2 #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ feat_net_raw: feat_net_raw.cpp $(STATIC_NAME)
align_test: align_test.cpp $(STATIC_NAME)
$(CXX) $< $(CXXFLAGS) -o $@ -L. -lcaffe $(LDFLAGS) $(shell pkg-config --libs opencv)

proto_old2new: proto_old2new.cpp $(STATIC_NAME)
$(CXX) $< $(CXXFLAGS) -o $@ -L. -lcaffe $(LDFLAGS)

clean:
@- $(RM) $(NAME) $(STATIC_NAME)
@- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC) $(PROTO_GEN_PY)
Expand Down
234 changes: 85 additions & 149 deletions align_test.cpp
Original file line number Diff line number Diff line change
@@ -1,175 +1,151 @@
// Copyright 2013 Yangqing Jia
//
// This is a simple script that allows one to quickly test a network whose
// structure is specified by text format protocol buffers, and whose parameter
// are loaded from a pre-trained network.
// Usage:
// test_net net_proto pretrained_net_proto iterations [CPU/GPU]

#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>

#include "caffe/caffe.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#define CROP_WINSIZE 39
#define CROP_PADDING 2.5

using namespace caffe; // NOLINT(build/namespaces)
using namespace cv;
using namespace caffe;

#define square(x) ((x)*(x))

float getMean( float * p )
float getMean( float * p, int width, int height )
{
float ans = 0.0f;
for(int i = 0 ; i < CROP_WINSIZE * CROP_WINSIZE ; i++, p++)
float ans = 0;
for(int i = 0 ; i < width * height ; i++, p++)
ans += *p;
ans = ans/ float( CROP_WINSIZE * CROP_WINSIZE );
ans = ans/ float( width * height );
return ans;
}

float getStd( float * p , float mean)
float getStd( float * p , float mean, int width, int height)
{
float ans = 0.0f;
for(int i = 0 ; i < CROP_WINSIZE * CROP_WINSIZE ; i++, p++)
float ans = 0;
for(int i = 0 ; i < width * height ; i++, p++)
ans += ( (*p-mean) * (*p-mean) );
ans = ans/ float( CROP_WINSIZE * CROP_WINSIZE - 1);
ans = sqrt( ans ) + 1e-5f;
ans = ans/ float( width * height - 1);
ans = sqrt( ans );
return ans;
}
void getZscore( Mat & img, int left, int right, int top, int bottom, float * & score )
void getZscore( cv::Mat & img, int left, int right, int top, int bottom, float * & score, int width, int height )
{
if( img.type()==CV_8UC3 )
{
std::cerr << "warning! a color image input" << std::endl;
cv::cvtColor( img , img , CV_RGB2GRAY );
}
double scale = (right - left) / double( CROP_WINSIZE );

double scale = sqrt((square(right - left) + square(bottom - top))/2) / double( (width + height)/2 );

left -= int( scale * CROP_PADDING );
right += int( scale * CROP_PADDING );
right += int( scale * CROP_PADDING )+1;
top -= int( scale * CROP_PADDING );
bottom+= int( scale * CROP_PADDING );
bottom+= int( scale * CROP_PADDING )+1;

if( top<0 || left < 0 || right >= img.cols || bottom >= img.rows )
if( top<0 || left < 0 || right > img.cols || bottom > img.rows )
{
std::cerr << "warning! invalid bounding box " << std::endl;
return;
}

cv::Mat patch = img( cv::Range( top, bottom ), cv::Range( left, right ) );
cv::resize( patch , patch, cv::Size( height, width ) );
patch.convertTo( patch, CV_32F );

Mat patch = img( Range( top, bottom ), Range( left, right ) );
cv::resize( patch , patch, Size( CROP_WINSIZE, CROP_WINSIZE ) );

patch.convertTo( patch, CV_32F );
//cv::imshow("AA", patch / 255.0);

float mu = getMean( patch.ptr<float>() );
float sigma = getStd( patch.ptr<float>() , mu);
fprintf(stderr, "mu %f, sigma %f\n", mu, sigma);
float mu = getMean( patch.ptr<float>(), width, height);
float sigma = getStd( patch.ptr<float>() , mu, width, height);

score = new( float[ CROP_WINSIZE * CROP_WINSIZE ] );
score = new float [ width*height ];

float * p_patch = patch.ptr<float>();

for(int i = 0 ; i < CROP_WINSIZE * CROP_WINSIZE ; i++)
for(int i = 0 ; i < width*height ; i++)
score[i] = ( p_patch[i] - mu ) / sigma;
}


template <typename Dtype>
static void save_blob(const string& fn, Blob<Dtype> *b){
LOG(INFO) << "Saving " << fn;
FILE *f = fopen(fn.c_str(), "wb");
CHECK(f != NULL);
fwrite(b->cpu_data(), sizeof(Dtype), b->count(), f);
fclose(f);
}

static void draw(const float *buf, const float *pt){
const int ph = 39, pw = 39;
void draw(Blob<float>* data_blob, const Blob<float>* feature_blob)
{
CHECK_EQ(data_blob->num(), feature_blob->num());
int batch_size = data_blob->num();
int img_length = data_blob->height()*data_blob->width();
int pt_length = feature_blob->channels()/2;
const float scale = 4.0f;
cv::Mat m = cv::Mat::zeros(ph, pw, CV_32FC1);
memcpy(m.data, buf, sizeof(float)*pw*ph);
cv::Mat dsp;
cv::normalize(m, dsp, 0, 255, cv::NORM_MINMAX, CV_8UC1);
cv::resize(dsp, dsp, cv::Size(), scale, scale);
cv::cvtColor(dsp, dsp, CV_GRAY2BGR);

#if 1
for(int i=0;i<5;i++){
const float * pt = feature_blob->cpu_data();
for (int i=0; i<batch_size; i++)
{
cv::Mat img(data_blob->height(), data_blob->width(), CV_32FC1);
img.data = (uchar *)(data_blob->mutable_cpu_data() + i*img_length);
cv::Mat dsp;
cv::normalize(img, dsp, 0, 255, cv::NORM_MINMAX, CV_8UC1);
cv::resize(dsp, dsp, cv::Size(), scale, scale);
cv::cvtColor(dsp, dsp, CV_GRAY2BGR);
for(int i=0;i<pt_length;i++){
const float *t = pt + 2*i;
cv::circle(dsp, cv::Point(t[0]*scale, t[1]*scale), 2, cv::Scalar(255,0,0), 2);
}
#endif
cv::imshow("A", dsp);
}
pt += pt_length*2;
cv::imshow("image", dsp);
cv::waitKey(0);
}
}


int main(int argc, char** argv) {
LogMessage::Enable(true);
if (argc < 3) {
LOG(ERROR) << "test_net net_proto pretrained_net_proto iterations inputbin output_dir"
<< " [CPU/GPU]";
//check input
if (argc != 4) {
LOG(ERROR) << "test_align test_net_proto pretrained_net_proto imagelist";
return 0;
}

//enable screen output
LogMessage::Enable(true);
//set CPU mode
Caffe::set_mode(Caffe::CPU);

//initial test net
NetParameter test_net_param;
ReadNetParamsFromTextFileOrDie(argv[1], &test_net_param);

Net<float> caffe_test_net(test_net_param);
//read in pretrained net
NetParameter trained_net_param;
ReadNetParamsFromBinaryFileOrDie(argv[2], &trained_net_param);
caffe_test_net.CopyTrainedLayersFrom(trained_net_param);

#if 0
SolverState state;
std::string state_file = std::string(argv[2]) + ".solverstate";
ReadProtoFromBinaryFile(state_file, &state);
#endif

vector<Blob<float>*> dummy_blob_input_vec;

//save layer
//find layer ID
int feature_layer_idx = -1;
int data_layer_idx = -1;
for(int i=0;i<caffe_test_net.layer_names().size();i++)
if(caffe_test_net.layer_names()[i] == "ip2"){
for(int i=0;i<caffe_test_net.layer_names().size();i++){
if(caffe_test_net.layer_names()[i] == "ip2")
feature_layer_idx = i;
break;
}
for(int i=0;i<caffe_test_net.layer_names().size();i++)
if(caffe_test_net.layer_names()[i] == "image_input"){
else if (caffe_test_net.layer_names()[i] == "image_input")
data_layer_idx = i;
break;
}

}
//check layer ID and log info
CHECK_NE(feature_layer_idx, -1);
CHECK_NE(data_layer_idx, -1);
LOG(INFO) << "Data layer: " << data_layer_idx;
LOG(INFO) << "Feature layer: " << feature_layer_idx;

Blob<float>* output = caffe_test_net.top_vecs()[feature_layer_idx][0],
//get pointer of the outputs of these layers
Blob<float>* feature_blob = caffe_test_net.top_vecs()[feature_layer_idx][0],
*data_blob = caffe_test_net.top_vecs()[data_layer_idx][0];
//log info
LOG(INFO) << "DATA BLOB dim: " << data_blob->num() << ' '
<< data_blob->channels() << ' '
<< data_blob->width() << ' '
<< data_blob->height();
LOG(INFO) << "OUTPUT BLOB dim: " << feature_blob->num() << ' '
<< feature_blob->channels() << ' '
<< feature_blob->width() << ' '
<< feature_blob->height();
//read image list
FILE *finput = fopen(argv[3], "r");
CHECK(finput != NULL)<<"read file error "<<argv[3];
//setup data layer
MemoryDataLayer<float> *data_layer = dynamic_cast<MemoryDataLayer<float>* >(caffe_test_net.layers()[data_layer_idx].get());
CHECK(data_layer != 0);

LOG(INFO) << "OUTPUT BLOB dim: " << output->num() << ' '
<< output->channels() << ' '
<< output->width() << ' '
<< output->height();
const int ih = data_blob->height(), iw = data_blob->width(), ic = data_blob->channels();
//double buf[ih*iw*ic];
FILE *finput = fopen(argv[3], "r");
CHECK(finput != NULL);
for (;;) {
//creat a dummy vector
vector<Blob<float>*> dummy_blob_input_vec;
CHECK_EQ(data_blob->num(), 1)<<"batch size should be 1";
int input_size = data_blob->channels()*data_blob->width()*data_blob->height();
while(1){
char fn[1024];
int l,r,t,b;
int nread = fscanf(finput, "%s%d%d%d%d", fn, &l, &r, &t, &b);
Expand All @@ -181,56 +157,16 @@ int main(int argc, char** argv) {
continue;
}
cv::cvtColor(mat, mat, CV_BGR2GRAY);
float * p = 0;
getZscore( mat, l, r, t, b, p);
if(!p)
continue;

//float *d = data_blob->mutable_cpu_data();
size_t len = ih * iw * ic;
//XXX
float * p = NULL;
getZscore( mat, l, r, t, b, p, data_blob->width(), data_blob->height());
CHECK(p != NULL);
float dummy_label[1] = {0};
float tmp[32] = {0};
//draw(p, tmp);
#if 1
data_layer->Reset(p, dummy_label, 1);
for(int j = 0; j < data_blob->num(); j++){
//memcpy(d, p, sizeof(float)*CROP_WINSIZE*CROP_WINSIZE);
/*
size_t nread = fread(buf, sizeof(double), len, finput);
CHECK_EQ(nread, len);
for(int k=0;k<len;k++){
d[k] = buf[k];
}
d += len;
*/
}
const vector<Blob<float>*>& result =
caffe_test_net.Forward(dummy_blob_input_vec);

printf("%s %d %d %d %d ", fn, l, r, t, b);
const float *pt = output->cpu_data();
for(int i=0;i<output->num();i++){
for(int j=0;j<output->channels();j++)
printf("%f\t", pt[j]);
printf("\n");
}
fflush(stdout);

draw(p, pt);
#endif
delete [] p;

//sprintf(output_dir, "%s/feat_%05d", argv[4], i);
//save_blob(output_dir, output);

//test_accuracy += result[0]->cpu_data()[0];
//LOG(ERROR) << "Batch " << i << ", accuracy: " << result[0]->cpu_data()[0];
caffe_test_net.Forward(dummy_blob_input_vec);
draw(data_blob, feature_blob);
delete p;
}
fclose(finput);
//test_accuracy /= total_iter;
//LOG(ERROR) << "Test accuracy:" << test_accuracy;

return 0;
}

27 changes: 27 additions & 0 deletions proto_old2new.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "caffe/caffe.hpp"

using namespace caffe;

int main(int argc, char** argv) {
//enable screen output
LogMessage::Enable(true);
//check input
if (argc != 4) {
LOG(ERROR) << "proto_old2new old_proto new_proto mode(0:bin2txt 1:txt2txt)";
return 0;
}
//set CPU mode
Caffe::set_mode(Caffe::CPU);
//initial test net
NetParameter net_param;
if(argv[3][0] == '0'){
ReadNetParamsFromBinaryFileOrDie(argv[1], &net_param);
LOG(INFO)<<"bin2txt: "<<argv[1]<<" to "<<argv[2];
}else {
ReadNetParamsFromTextFileOrDie(argv[1], &net_param);
LOG(INFO)<<"txt2txt: "<<argv[1]<<" to "<<argv[2];
}
WriteProtoToTextFile(net_param, argv[2]);
return 0;
}

Loading