Skip to content

Commit ba93174

Browse files
authored
Merge pull request #30 from koba-jon/develop/v2.0.1
Develop/v2.0.1
2 parents 4265a19 + c9481cf commit ba93174

File tree

23 files changed

+147
-61
lines changed

23 files changed

+147
-61
lines changed

Anomaly_Detection/AnoGAN2d/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ If you want to view specific examples of command line arguments, please view "sr
9898
#!/bin/bash
9999
100100
DATA='MVTecAD'
101+
HEATMAP_MAX=0.1
101102
102103
./AnoGAN2d \
103104
--test true \
104105
--dataset ${DATA} \
105106
--test_dir "test_anomaly" \
106107
--test_result_dir "test_result_anomaly" \
108+
--heatmap_max ${HEATMAP_MAX} \
107109
--size 256 \
108110
--gpu_id 0 \
109111
--nc 3
@@ -113,6 +115,7 @@ DATA='MVTecAD'
113115
--dataset ${DATA} \
114116
--test_dir "test_normal" \
115117
--test_result_dir "test_result_normal" \
118+
--heatmap_max ${HEATMAP_MAX} \
116119
--size 256 \
117120
--gpu_id 0 \
118121
--nc 3

Anomaly_Detection/AnoGAN2d/scripts/test.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
22

33
DATA='MVTecAD'
4+
HEATMAP_MAX=0.1
45

56
./AnoGAN2d \
67
--test true \
78
--dataset ${DATA} \
89
--test_dir "test_anomaly" \
910
--test_result_dir "test_result_anomaly" \
11+
--heatmap_max ${HEATMAP_MAX} \
1012
--size 256 \
1113
--gpu_id 0 \
1214
--nc 3
@@ -16,6 +18,7 @@ DATA='MVTecAD'
1618
--dataset ${DATA} \
1719
--test_dir "test_normal" \
1820
--test_result_dir "test_result_normal" \
21+
--heatmap_max ${HEATMAP_MAX} \
1922
--size 256 \
2023
--gpu_id 0 \
2124
--nc 3

Anomaly_Detection/AnoGAN2d/src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ po::options_description parse_arguments(){
6969
("test_result_dir", po::value<std::string>()->default_value("test_result"), "test result directory : ./<test_result_dir>")
7070
("test_search_epoch", po::value<size_t>()->default_value(100), "epoch to search latent variable in test")
7171
("test_Lambda", po::value<float>()->default_value(0.1), "anomaly score rate between reconstruction and feature matching in test")
72+
("heatmap_max", po::value<float>()->default_value(1.0), "heatmap maximum-value in test")
7273

7374
// (5) Define for Anomaly Detection
7475
("AD", po::value<bool>()->default_value(false), "anomaly detection mode on/off")

Anomaly_Detection/AnoGAN2d/src/test.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ void test(po::variables_map &vm, torch::Device &device, GAN_Generator &gen, GAN_
3535
size_t search_epoch;
3636
float ave_anomaly_score, ave_res_loss, ave_dis_loss;
3737
double seconds, ave_time;
38-
std::string path, result_dir, fname;
38+
std::string path, result_dir, output_dir, heatmap_dir, fname;
3939
std::string dataroot;
4040
std::ofstream ofs, ofs_score;
4141
std::chrono::system_clock::time_point start, end;
4242
std::tuple<torch::Tensor, std::vector<std::string>> data;
4343
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor> anomaly_score_with_alpha;
44-
torch::Tensor image, z, output;
44+
torch::Tensor image, z, output, heatmap;
4545
torch::Tensor loss, anomaly_score, res_loss, dis_loss;
4646
datasets::ImageFolderWithPaths dataset;
4747
DataLoader::ImageFolderWithPaths dataloader;
@@ -73,6 +73,8 @@ void test(po::variables_map &vm, torch::Device &device, GAN_Generator &gen, GAN_
7373
gen->eval();
7474
dis->eval();
7575
result_dir = vm["test_result_dir"].as<std::string>(); fs::create_directories(result_dir);
76+
output_dir = result_dir + "/output"; fs::create_directories(output_dir);
77+
heatmap_dir = result_dir + "/heatmap"; fs::create_directories(heatmap_dir);
7678
ofs.open(result_dir + "/loss.txt", std::ios::out);
7779
ofs_score.open(result_dir + "/anomaly_score.txt", std::ios::out);
7880
while (dataloader(data)){
@@ -113,9 +115,13 @@ void test(po::variables_map &vm, torch::Device &device, GAN_Generator &gen, GAN_
113115
ofs << '<' << std::get<1>(data).at(0) << "> anomaly_score:" << anomaly_score.item<float>() << " res:" << res_loss.item<float>() << " dis:" << dis_loss.item<float>() << " (time:" << ave_time << ')' << std::endl;
114116
ofs_score << anomaly_score.item<float>() << std::endl;
115117

116-
fname = result_dir + '/' + std::get<1>(data).at(0);
118+
fname = output_dir + '/' + std::get<1>(data).at(0);
117119
visualizer::save_image(output.detach(), fname, /*range=*/output_range, /*cols=*/1, /*padding=*/0);
118120

121+
fname = heatmap_dir + '/' + std::get<1>(data).at(0);
122+
heatmap = visualizer::create_heatmap(torch::abs(image - output).mean(/*dim=*/1, /*keepdim=*/true), /*range=*/{0, (output_range.second - output_range.first) * vm["heatmap_max"].as<float>()});
123+
visualizer::save_image(heatmap.detach(), fname, /*range=*/{0.0, 1.0}, /*cols=*/1, /*padding=*/0);
124+
119125
}
120126

121127
// (6) Calculate Average

Anomaly_Detection/DAGMM2d/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ If you want to view specific examples of command line arguments, please view "sr
9898
#!/bin/bash
9999
100100
DATA='MVTecAD'
101+
HEATMAP_MAX=0.1
101102
102103
./DAGMM2d \
103104
--test true \
104105
--dataset ${DATA} \
105106
--test_dir "test_anomaly" \
106107
--test_result_dir "test_result_anomaly" \
108+
--heatmap_max ${HEATMAP_MAX} \
107109
--size 256 \
108110
--gpu_id 0 \
109111
--nc 3
@@ -113,6 +115,7 @@ DATA='MVTecAD'
113115
--dataset ${DATA} \
114116
--test_dir "test_normal" \
115117
--test_result_dir "test_result_normal" \
118+
--heatmap_max ${HEATMAP_MAX} \
116119
--size 256 \
117120
--gpu_id 0 \
118121
--nc 3

Anomaly_Detection/DAGMM2d/scripts/test.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
22

33
DATA='MVTecAD'
4+
HEATMAP_MAX=0.1
45

56
./DAGMM2d \
67
--test true \
78
--dataset ${DATA} \
89
--test_dir "test_anomaly" \
910
--test_result_dir "test_result_anomaly" \
11+
--heatmap_max ${HEATMAP_MAX} \
1012
--size 256 \
1113
--gpu_id 0 \
1214
--nc 3
@@ -16,6 +18,7 @@ DATA='MVTecAD'
1618
--dataset ${DATA} \
1719
--test_dir "test_normal" \
1820
--test_result_dir "test_result_normal" \
21+
--heatmap_max ${HEATMAP_MAX} \
1922
--size 256 \
2023
--gpu_id 0 \
2124
--nc 3

Anomaly_Detection/DAGMM2d/src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ po::options_description parse_arguments(){
7171
("test_dir", po::value<std::string>()->default_value("test"), "test image directory : ./datasets/<dataset>/<test_dir>/<image files>")
7272
("test_load_epoch", po::value<std::string>()->default_value("latest"), "training epoch used for testing")
7373
("test_result_dir", po::value<std::string>()->default_value("test_result"), "test result directory : ./<test_result_dir>")
74+
("heatmap_max", po::value<float>()->default_value(1.0), "heatmap maximum-value in test")
7475

7576
// (5) Define for Anomaly Detection
7677
("AD", po::value<bool>()->default_value(false), "anomaly detection mode on/off")

Anomaly_Detection/DAGMM2d/src/test.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ void test(po::variables_map &vm, torch::Device &device, Encoder &enc, Decoder &d
3131
// (0) Initialization and Declaration
3232
float ave_loss, ave_anomaly_score, score;
3333
double seconds, ave_time;
34-
std::string path, result_dir, fname;
34+
std::string path, result_dir, output_dir, heatmap_dir, fname;
3535
std::string dataroot;
3636
std::ofstream ofs, ofs_loss, ofs_score;
3737
std::chrono::system_clock::time_point start, end;
3838
std::tuple<torch::Tensor, std::vector<std::string>> data;
39-
torch::Tensor image, output;
39+
torch::Tensor image, output, heatmap;
4040
torch::Tensor z, z_c, z_r, z_r1, z_r2;
4141
torch::Tensor mu, sigma, phi;
4242
torch::Tensor loss, anomaly_score;
@@ -72,6 +72,8 @@ void test(po::variables_map &vm, torch::Device &device, Encoder &enc, Decoder &d
7272
dec->eval();
7373
est->eval();
7474
result_dir = vm["test_result_dir"].as<std::string>(); fs::create_directories(result_dir);
75+
output_dir = result_dir + "/output"; fs::create_directories(output_dir);
76+
heatmap_dir = result_dir + "/heatmap"; fs::create_directories(heatmap_dir);
7577
ofs.open(result_dir + "/loss.txt", std::ios::out);
7678
ofs_loss.open(result_dir + "/reconstruction_error.txt", std::ios::out);
7779
ofs_score.open(result_dir + "/anomaly_score.txt", std::ios::out);
@@ -118,9 +120,13 @@ void test(po::variables_map &vm, torch::Device &device, Encoder &enc, Decoder &d
118120
ofs_loss << loss.item<float>() << std::endl;
119121
ofs_score << score << std::endl;
120122

121-
fname = result_dir + '/' + std::get<1>(data).at(0);
123+
fname = output_dir + '/' + std::get<1>(data).at(0);
122124
visualizer::save_image(output.detach(), fname, /*range=*/output_range, /*cols=*/1, /*padding=*/0);
123125

126+
fname = heatmap_dir + '/' + std::get<1>(data).at(0);
127+
heatmap = visualizer::create_heatmap(torch::abs(image - output).mean(/*dim=*/1, /*keepdim=*/true), /*range=*/{0, (output_range.second - output_range.first) * vm["heatmap_max"].as<float>()});
128+
visualizer::save_image(heatmap.detach(), fname, /*range=*/{0.0, 1.0}, /*cols=*/1, /*padding=*/0);
129+
124130
}
125131

126132
// (6) Calculate Average

Anomaly_Detection/EGBAD2d/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ If you want to view specific examples of command line arguments, please view "sr
9898
#!/bin/bash
9999
100100
DATA='MVTecAD'
101+
HEATMAP_MAX=0.1
101102
102103
./EGBAD2d \
103104
--test true \
104105
--dataset ${DATA} \
105106
--test_dir "test_anomaly" \
106107
--test_result_dir "test_result_anomaly" \
108+
--heatmap_max ${HEATMAP_MAX} \
107109
--size 256 \
108110
--gpu_id 0 \
109111
--nc 3
@@ -113,6 +115,7 @@ DATA='MVTecAD'
113115
--dataset ${DATA} \
114116
--test_dir "test_normal" \
115117
--test_result_dir "test_result_normal" \
118+
--heatmap_max ${HEATMAP_MAX} \
116119
--size 256 \
117120
--gpu_id 0 \
118121
--nc 3

Anomaly_Detection/EGBAD2d/scripts/test.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
22

33
DATA='MVTecAD'
4+
HEATMAP_MAX=0.1
45

56
./EGBAD2d \
67
--test true \
78
--dataset ${DATA} \
89
--test_dir "test_anomaly" \
910
--test_result_dir "test_result_anomaly" \
11+
--heatmap_max ${HEATMAP_MAX} \
1012
--size 256 \
1113
--gpu_id 0 \
1214
--nc 3
@@ -16,6 +18,7 @@ DATA='MVTecAD'
1618
--dataset ${DATA} \
1719
--test_dir "test_normal" \
1820
--test_result_dir "test_result_normal" \
21+
--heatmap_max ${HEATMAP_MAX} \
1922
--size 256 \
2023
--gpu_id 0 \
2124
--nc 3

Anomaly_Detection/EGBAD2d/src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ po::options_description parse_arguments(){
6868
("test_load_epoch", po::value<std::string>()->default_value("latest"), "training epoch used for testing")
6969
("test_result_dir", po::value<std::string>()->default_value("test_result"), "test result directory : ./<test_result_dir>")
7070
("test_Lambda", po::value<float>()->default_value(0.1), "anomaly score rate between reconstruction and feature matching in test")
71+
("heatmap_max", po::value<float>()->default_value(1.0), "heatmap maximum-value in test")
7172

7273
// (5) Define for Anomaly Detection
7374
("AD", po::value<bool>()->default_value(false), "anomaly detection mode on/off")

Anomaly_Detection/EGBAD2d/src/test.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ void test(po::variables_map &vm, torch::Device &device, GAN_Encoder &enc, GAN_Ge
3333
// (0) Initialization and Declaration
3434
float ave_anomaly_score, ave_res_loss, ave_dis_loss;
3535
double seconds, ave_time;
36-
std::string path, result_dir, fname;
36+
std::string path, result_dir, output_dir, heatmap_dir, fname;
3737
std::string dataroot;
3838
std::ofstream ofs, ofs_score;
3939
std::chrono::system_clock::time_point start, end;
4040
std::tuple<torch::Tensor, std::vector<std::string>> data;
4141
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor> anomaly_score_with_alpha;
42-
torch::Tensor image, z, output;
42+
torch::Tensor image, z, output, heatmap;
4343
torch::Tensor anomaly_score, res_loss, dis_loss;
4444
datasets::ImageFolderWithPaths dataset;
4545
DataLoader::ImageFolderWithPaths dataloader;
@@ -67,6 +67,8 @@ void test(po::variables_map &vm, torch::Device &device, GAN_Encoder &enc, GAN_Ge
6767
gen->eval();
6868
dis->eval();
6969
result_dir = vm["test_result_dir"].as<std::string>(); fs::create_directories(result_dir);
70+
output_dir = result_dir + "/output"; fs::create_directories(output_dir);
71+
heatmap_dir = result_dir + "/heatmap"; fs::create_directories(heatmap_dir);
7072
ofs.open(result_dir + "/loss.txt", std::ios::out);
7173
ofs_score.open(result_dir + "/anomaly_score.txt", std::ios::out);
7274
while (dataloader(data)){
@@ -96,9 +98,13 @@ void test(po::variables_map &vm, torch::Device &device, GAN_Encoder &enc, GAN_Ge
9698
ofs << '<' << std::get<1>(data).at(0) << "> anomaly_score:" << anomaly_score.item<float>() << " res:" << res_loss.item<float>() << " dis:" << dis_loss.item<float>() << std::endl;
9799
ofs_score << anomaly_score.item<float>() << std::endl;
98100

99-
fname = result_dir + '/' + std::get<1>(data).at(0);
101+
fname = output_dir + '/' + std::get<1>(data).at(0);
100102
visualizer::save_image(output.detach(), fname, /*range=*/output_range, /*cols=*/1, /*padding=*/0);
101103

104+
fname = heatmap_dir + '/' + std::get<1>(data).at(0);
105+
heatmap = visualizer::create_heatmap(torch::abs(image - output).mean(/*dim=*/1, /*keepdim=*/true), /*range=*/{0, (output_range.second - output_range.first) * vm["heatmap_max"].as<float>()});
106+
visualizer::save_image(heatmap.detach(), fname, /*range=*/{0.0, 1.0}, /*cols=*/1, /*padding=*/0);
107+
102108
}
103109

104110
// (5) Calculate Average

Anomaly_Detection/GANomaly2d/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ If you want to view specific examples of command line arguments, please view "sr
9898
#!/bin/bash
9999
100100
DATA='MVTecAD'
101+
HEATMAP_MAX=0.1
101102
102103
./GANomaly2d \
103104
--test true \
104105
--dataset ${DATA} \
105106
--test_dir "test_anomaly" \
106107
--test_result_dir "test_result_anomaly" \
108+
--heatmap_max ${HEATMAP_MAX} \
107109
--size 256 \
108110
--gpu_id 0 \
109111
--nc 3
@@ -113,6 +115,7 @@ DATA='MVTecAD'
113115
--dataset ${DATA} \
114116
--test_dir "test_normal" \
115117
--test_result_dir "test_result_normal" \
118+
--heatmap_max ${HEATMAP_MAX} \
116119
--size 256 \
117120
--gpu_id 0 \
118121
--nc 3

Anomaly_Detection/GANomaly2d/scripts/test.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
22

33
DATA='MVTecAD'
4+
HEATMAP_MAX=0.1
45

56
./GANomaly2d \
67
--test true \
78
--dataset ${DATA} \
89
--test_dir "test_anomaly" \
910
--test_result_dir "test_result_anomaly" \
11+
--heatmap_max ${HEATMAP_MAX} \
1012
--size 256 \
1113
--gpu_id 0 \
1214
--nc 3
@@ -16,6 +18,7 @@ DATA='MVTecAD'
1618
--dataset ${DATA} \
1719
--test_dir "test_normal" \
1820
--test_result_dir "test_result_normal" \
21+
--heatmap_max ${HEATMAP_MAX} \
1922
--size 256 \
2023
--gpu_id 0 \
2124
--nc 3

Anomaly_Detection/GANomaly2d/src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ po::options_description parse_arguments(){
6767
("test_dir", po::value<std::string>()->default_value("test"), "test image directory : ./datasets/<dataset>/<test_dir>/<image files>")
6868
("test_load_epoch", po::value<std::string>()->default_value("latest"), "training epoch used for testing")
6969
("test_result_dir", po::value<std::string>()->default_value("test_result"), "test result directory : ./<test_result_dir>")
70+
("heatmap_max", po::value<float>()->default_value(1.0), "heatmap maximum-value in test")
7071

7172
// (5) Define for Anomaly Detection
7273
("AD", po::value<bool>()->default_value(false), "anomaly detection mode on/off")

Anomaly_Detection/GANomaly2d/src/test.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ void test(po::variables_map &vm, torch::Device &device, Encoder &enc1, Encoder &
3030
// (0) Initialization and Declaration
3131
float ave_con_loss, ave_enc_loss, ave_anomaly_score;
3232
double seconds, ave_time;
33-
std::string path, result_dir, fname;
33+
std::string path, result_dir, output_dir, heatmap_dir, fname;
3434
std::string dataroot;
3535
std::ofstream ofs, ofs_score;
3636
std::chrono::system_clock::time_point start, end;
3737
std::tuple<torch::Tensor, std::vector<std::string>> data;
38-
torch::Tensor image, z, output, z_rec;
38+
torch::Tensor image, z, output, z_rec, heatmap;
3939
torch::Tensor con_loss, enc_loss, anomaly_score;
4040
datasets::ImageFolderWithPaths dataset;
4141
DataLoader::ImageFolderWithPaths dataloader;
@@ -67,6 +67,8 @@ void test(po::variables_map &vm, torch::Device &device, Encoder &enc1, Encoder &
6767
enc2->eval();
6868
dec->eval();
6969
result_dir = vm["test_result_dir"].as<std::string>(); fs::create_directories(result_dir);
70+
output_dir = result_dir + "/output"; fs::create_directories(output_dir);
71+
heatmap_dir = result_dir + "/heatmap"; fs::create_directories(heatmap_dir);
7072
ofs.open(result_dir + "/loss.txt", std::ios::out);
7173
ofs_score.open(result_dir + "/anomaly_score.txt", std::ios::out);
7274
while (dataloader(data)){
@@ -96,9 +98,13 @@ void test(po::variables_map &vm, torch::Device &device, Encoder &enc1, Encoder &
9698
ofs << '<' << std::get<1>(data).at(0) << "> con_" << vm["loss_con"].as<std::string>() << ':' << con_loss.item<float>() << " enc_" << vm["loss_enc"].as<std::string>() << ':' << enc_loss.item<float>() << " anomaly_score:" << anomaly_score.item<float>() << std::endl;
9799
ofs_score << anomaly_score.item<float>() << std::endl;
98100

99-
fname = result_dir + '/' + std::get<1>(data).at(0);
101+
fname = output_dir + '/' + std::get<1>(data).at(0);
100102
visualizer::save_image(output.detach(), fname, /*range=*/output_range, /*cols=*/1, /*padding=*/0);
101103

104+
fname = heatmap_dir + '/' + std::get<1>(data).at(0);
105+
heatmap = visualizer::create_heatmap(torch::abs(image - output).mean(/*dim=*/1, /*keepdim=*/true), /*range=*/{0, (output_range.second - output_range.first) * vm["heatmap_max"].as<float>()});
106+
visualizer::save_image(heatmap.detach(), fname, /*range=*/{0.0, 1.0}, /*cols=*/1, /*padding=*/0);
107+
102108
}
103109

104110
// (6) Calculate Average

Anomaly_Detection/Skip-GANomaly2d/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ If you want to view specific examples of command line arguments, please view "sr
9898
#!/bin/bash
9999
100100
DATA='MVTecAD'
101+
HEATMAP_MAX=0.1
101102
102103
./Skip-GANomaly2d \
103104
--test true \
104105
--dataset ${DATA} \
105106
--test_dir "test_anomaly" \
106107
--test_result_dir "test_result_anomaly" \
108+
--heatmap_max ${HEATMAP_MAX} \
107109
--size 256 \
108110
--gpu_id 0 \
109111
--nc 3
@@ -113,6 +115,7 @@ DATA='MVTecAD'
113115
--dataset ${DATA} \
114116
--test_dir "test_normal" \
115117
--test_result_dir "test_result_normal" \
118+
--heatmap_max ${HEATMAP_MAX} \
116119
--size 256 \
117120
--gpu_id 0 \
118121
--nc 3

0 commit comments

Comments
 (0)