Skip to content

Commit

Permalink
for GMP2019
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlyqing00 committed Dec 30, 2018
1 parent a9cffc7 commit 97b1e71
Show file tree
Hide file tree
Showing 40 changed files with 479 additions and 149 deletions.
38 changes: 29 additions & 9 deletions benchmark.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
#!/bin/bash

generator_n=3
generator_nums=(20 30 40 60)

test_n=4
nums=(20 30 40 50)
samples=(30 50 70 100)
nums=(20 30 40 60 60)
samples=(100 300 800 3200 4000)
metrics=(0 2)
compositions=(0 1)
compositions=(2)

if [ $# == 0 ]; then
echo "Enter a test case name."
echo "End."
exit
fi

echo "Test case: "$var
for i in $(seq 0 ${test_n}); do
./bin/release/generator -t $1 -n ${nums[i]}
if [ "$1" == "GA" ]; then
echo "Add seams on GA solutions."

test=("doc0" "doc3" "doc7" "doc10" "doc11" "doc12" "doc13" "doc14" "doc15" "doc16" "doc17" "doc18" "doc19" "doc20" "doc21" "doc23" "doc24")
# test=("doc7")
for var in ${test[@]}; do
for i in $(seq 0 ${test_n}); do
./bin/release/debug-tool -d 2 -o ${var}_${nums[i]}
done
done

exit
fi

echo "Test case: "$1
for i in $(seq 0 ${generator_n}); do
./bin/release/generator -t $1 -n ${generator_nums[i]}
echo " "
done

for metric in ${metrics[@]}; do
for comp in ${compositions[@]}; do
for metric in ${metrics[@]}; do
for comp in ${compositions[@]}; do
for i in $(seq 0 ${test_n}); do
./bin/release/solver \
-t $1 \
-n ${nums[i]} \
-m ${metric} \
-c ${comp} \
-s ${samples[i]}
-s ${samples[i]} \
-b
echo " "
done
done
Expand Down
72 changes: 72 additions & 0 deletions calc_scores.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import numpy as np
import matplotlib.pyplot as plt

folder_name = 'data/scores/'
scores_list = os.listdir(folder_name)
nums = [20, 30, 40, 60]
method_names = ['Pixel + Greedy', 'Pixel + GCOM', 'Word + Greedy', 'Word + GCOM', 'GA']
method_colors = ['teal', 'olive', 'aqua', 'orchid', 'orange']
order = [4, 0, 1, 2, 3]
# order = [0, 1, 2, 3]

adjust_flag = True
test_cnt = 0
test_n = len(nums)
method_n = len(method_names)
# method_n = 4
data = np.zeros(method_n * test_n)

our_data = None
for file_name in scores_list:

if (file_name == 'doc11.txt' or file_name == 'doc19.txt'):
continue

print(folder_name + file_name)
test_cnt += 1

score_file = open(folder_name + file_name, 'r')
for i in range(method_n):
x = score_file.readline()
scores = x.split()
if (len(scores) == 0):
continue

for j in range(test_n):
data[i * test_n + j] += float(scores[j])

if i == 3:
if our_data is None:
our_data = np.array(scores)
else:
our_data = np.vstack([our_data, np.array(scores)])

score_file.close()

data /= test_cnt
# print(data)
print(our_data)

for j in range(test_n):
x = np.array(our_data[:,j]).astype(np.float)
print(x.min(), x.max(), x.mean(), x.std())
# print(np.min(x), np.max(x))

x = list(range(len(nums)))
total_width = 0.8
width = total_width / method_n

for i in range(method_n):
order_idx = order[i]
if i > 0:
for j in range(test_n):
x[j] += width
plt.bar(x, data[order_idx * test_n: (order_idx+1) * test_n], width=width, label=method_names[order_idx], facecolor=method_colors[order_idx])

ticks = [str(x) for x in nums]
plt.xticks(np.arange(test_n), ticks)
plt.legend(loc='lower left', prop={'size': 8})
plt.xlabel('Stripes Number')
plt.ylabel('Accuracy')
plt.show()
Binary file modified data/gt/doc0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed data/gt/doc0_noise_100.png
Binary file not shown.
Binary file added data/gt/doc10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed data/gt/doc1_noise_100.png
Binary file not shown.
Binary file added data/gt/doc20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified data/gt/doc3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed data/gt/doc3_noise0.png
Binary file not shown.
Binary file removed data/gt/doc3_noise1.png
Binary file not shown.
Binary file added data/gt/doc3_noise_20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/gt/doc7.png
39 changes: 39 additions & 0 deletions format_scores.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import numpy as np

folder_name = 'data/scores_ori/'
dst_folder_name = 'data/scores/'
scores_list = os.listdir(folder_name)
nums_len = 6
method_n = 4
data = np.zeros([4, 6])

for file_name in scores_list:

print(folder_name + file_name)

score_file = open(folder_name + file_name, 'r')
for i in range(int(method_n / 2)):
x = score_file.readline()
scores = x.split()
if (len(scores) == 0):
continue

for j in range(nums_len):
data[i * 2][j] = float(scores[j * 2])
data[i * 2 + 1][j] = float(scores[j * 2 + 1])

score_file.close()

score_file = open(dst_folder_name + file_name, 'w')
for i in range(method_n):
for j in range(nums_len):
if j == 2 or j == 3 or j == 5:
continue
score_file.write(str(data[i][j]) + ' ')
score_file.write('\n')

score_file.close()


print(data)
3 changes: 1 addition & 2 deletions include/path_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class PathManager {

PathManager(int _vertices_n, int _sols_n);

void add_sol_words( const map< vector<int>, int > & sol_words,
int sol_cnt=1);
void add_sol_words( const map< vector<int>, int > & sol_words);
void print_sol_paths();

void build_path_graph();
Expand Down
3 changes: 2 additions & 1 deletion include/stripes_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ class StripesGenerator {
cv::Mat ori_img;
int stripes_n {0};
cv::Size ori_img_size;
bool updown_flag = true;

vector<cv::Mat> stripes;
vector<int> access_idx;

StripesGenerator(const string & img_path, int stripes_n);
StripesGenerator(const string & img_path, int stripes_n, bool _updown_flag=false);

cv::Mat get_puzzle_img(int gap);

Expand Down
19 changes: 13 additions & 6 deletions include/stripes_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class StripesSolver {
enum Composition {
GREEDY,
GCOM,
GREEDY_GCOM,
};

const string puzzle_folder;
Expand All @@ -55,11 +56,13 @@ class StripesSolver {
~StripesSolver();

void m_metric();
bool reassemble(Metric _metric_mode, Composition _composition_mode);
bool reassemble(Metric _metric_mode,
Composition _composition_mode,
const string & case_name,
bool benchmark_flag);

cv::Mat compose_img(const vector<int> & composition_order);
cv::Mat add_seams(const cv::Mat & img, const vector<int> & composition_order);
void save_result(const string & case_name);

private:

Expand All @@ -69,6 +72,8 @@ class StripesSolver {
vector<StripePair> stripe_pairs;
vector<StripePair> stripe_pairs_pixel;

void save_result(const string & case_name, bool benchmark_flag);

// Tesseract
const string tesseract_model_path {"data/tesseract_model/"};
tesseract::TessBaseAPI * ocr;
Expand All @@ -86,18 +91,20 @@ class StripesSolver {
Device device {kCPU};

// Metric word-path
int sols_n = 10;
int sols_n {10};
int candidate_len {10};
vector< vector<double> > pixel_graph;
vector< vector<double> > pixel_graph2;

double m_metric_char(const cv::Mat & piece0, const cv::Mat & piece1);
double m_metric_comp_eva(const cv::Mat & piece0, const cv::Mat & piece1);
void m_metric_word();

vector< vector<int> > reassemble_greedy(bool probability_flag=false);
void reassemble_GCOM();

cv::Mat word_detection( const cv::Mat & img,
const vector<int> & sol,
int sol_cnt=1);
void stochastic_search( vector<int> & sol, const vector< vector<StripePair> > & compose_next);
cv::Mat word_detection( const cv::Mat & img, const vector<int> & sol);
void merge_single_sol(vector< vector<int> > & fragments);
void finetune_sols(const vector< vector<int> > & fragments);

Expand Down
2 changes: 1 addition & 1 deletion include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum class PuzzleType {
};

const double eps = 1e-8;
const int U_a = 5;
const int U_a = 3;
const cv::Scalar seam_color_red(100, 100, 200);
const cv::Scalar seam_color_green(100, 200, 100);

Expand Down
12 changes: 12 additions & 0 deletions src/debug_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ void add_seams(const string & puzzle_name) {
cout << i << " " << sol_order[i] << endl;
}
cv::Scalar seam_color;
int correct_cnt = 0;
for (int i = 1; i < n; i++) {

for (int j = 0; j < n; j++) {
Expand All @@ -349,6 +350,7 @@ void add_seams(const string & puzzle_name) {
seam_color = seam_color_red;
} else {
seam_color = seam_color_green;
correct_cnt++;
}
break;
}
Expand All @@ -357,6 +359,16 @@ void add_seams(const string & puzzle_name) {

}

double score = (double)correct_cnt / (n - 1);
printf("Score: %.3lf\n", score);
size_t pos = puzzle_name.find_last_of('_');
ofstream fout("data/scores/" + puzzle_name.substr(0, pos) + ".txt", ios::app);
fout << score << " ";
if (n == 80) {
fout << endl;
}
fout.close();

cv::imwrite("data/results/" + puzzle_name + "_GA_sol_seams.png", sol_img);

}
Expand Down
15 changes: 12 additions & 3 deletions src/generator/generate_puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ int main(int argc, char ** argv) {
string case_name = "test0";
int vertical_n = 4;
PuzzleType puzzle_type = PuzzleType::STRIPES;
bool updown_flag = false;

// Parse command line parameters
const string opt_str = "t:T:n:N:gGsS";
const string opt_str = "t:T:n:N:gGsSU";
int opt = getopt(argc, argv, opt_str.c_str());

while (opt != -1) {
Expand All @@ -22,13 +23,17 @@ int main(int argc, char ** argv) {
case 's': case 'S':
puzzle_type = PuzzleType::SQUARES;
break;
case 'U':
updown_flag = true;
break;
}

opt = getopt(argc, argv, opt_str.c_str());
}

cout << "Test case name: \t" << case_name << endl;
cout << "Vertical cut num: \t" << vertical_n << endl;
cout << "Up down flag: \t" << updown_flag << endl;
cout << "Puzzle type: \t" << (puzzle_type == PuzzleType::SQUARES ? "Squares": "Stripes") << endl;

// Generate new puzzle
Expand All @@ -43,7 +48,11 @@ int main(int argc, char ** argv) {
mkdir(puzzle_folder.c_str(), S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
}

puzzle_folder += case_name + "_" + to_string(vertical_n) + "/";
if (updown_flag) {
puzzle_folder += case_name + "_" + to_string(vertical_n * 2) + "/";
} else {
puzzle_folder += case_name + "_" + to_string(vertical_n) + "/";
}

const string gt_folder = "data/gt/";
const string gt_img_path = gt_folder + case_name + ".png";
Expand All @@ -55,7 +64,7 @@ int main(int argc, char ** argv) {
}

if (puzzle_type == PuzzleType::STRIPES) {
StripesGenerator stripes_generator(gt_img_path, vertical_n);
StripesGenerator stripes_generator(gt_img_path, vertical_n, updown_flag);
stripes_generator.save_puzzle(puzzle_folder);
} else {
SquaresGenerator squares_generator(gt_img_path, vertical_n);
Expand Down
Loading

0 comments on commit 97b1e71

Please sign in to comment.