Skip to content

Commit 6b0fb50

Browse files
committed
Adding testbench for an image using PV and LT model of filter module
1 parent cd0c35c commit 6b0fb50

File tree

3 files changed

+172
-29
lines changed

3 files changed

+172
-29
lines changed

modules/filter/Makefile

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Adding stb lib for image handling
2-
INCDIR_STB=lib/stb
3-
INCLUDES_STB := $(wildcard $(INCDIR_STB)/*.h)
2+
CFLAGS_OPENCV := $(shell pkg-config --cflags opencv4)
3+
LFLAGS_OPENCV := $(shell pkg-config --libs opencv4)
44

55
# Include common Makefile
66
include ../Makefile
77

88
# Include stb lib for compilation
9-
INCLUDES += INCLUDES_STB
9+
CFLAGS += $(CFLAGS_OPENCV)
10+
LFLAGS += $(LFLAGS_OPENCV)
1011

1112
# Defining preprocessor directive for debug
1213
ifdef IPS_DEBUG_EN
@@ -20,18 +21,21 @@ ifdef IPS_DUMP_EN
2021
LFLAGS += -DIPS_DUMP_EN
2122
endif # IPS_DUMP_EN
2223

23-
24-
# Defining preprocessor directive for test mode one wildcard normal
24+
# Defining preprocessor directive for test modes
25+
ifdef TEST_MODE_IMAGE
26+
CFLAGS += -DTEST_MODE_IMAGE
27+
LFLAGS += -DTEST_MODE_IMAGE
28+
else
29+
ifdef TEST_MODE_ONE_WINDOW_RANDOM
30+
CFLAGS += -DTEST_MODE_ONE_WINDOW -DTEST_MODE_ONE_WINDOW_RANDOM
31+
LFLAGS += -DTEST_MODE_ONE_WINDOW -DTEST_MODE_ONE_WINDOW_RANDOM
32+
else
2533
ifdef TEST_MODE_ONE_WINDOW_NORMAL
2634
CFLAGS += -DTEST_MODE_ONE_WINDOW -DTEST_MODE_ONE_WINDOW_NORMAL
2735
LFLAGS += -DTEST_MODE_ONE_WINDOW -DTEST_MODE_ONE_WINDOW_NORMAL
2836
endif # TEST_MODE_ONE_WINDOW_NORMAL
29-
30-
# Defining preprocessor directive for test mode one window random
31-
ifdef TEST_MODE_ONE_WINDOW_RANDOM
32-
CFLAGS += -DTEST_MODE_ONE_WINDOW -DTEST_MODE_ONE_WINDOW_RANDOM
33-
LFLAGS += -DTEST_MODE_ONE_WINDOW -DTEST_MODE_ONE_WINDOW_RANDOM
3437
endif # TEST_MODE_ONE_WINDOW_RANDOM
38+
endif # TEST_MODE_IMAGE
3539

3640
# Defining preprocessor directive for model - by default PV model
3741
ifdef IPS_FILTER_LT_EN

modules/filter/include/ips_filter_at_model.hpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,11 @@ void Filter<IN, OUT, N>::exec_filter()
9898
// Getting the image window to filter
9999
IN* img_window_tmp = this->img_window.read();
100100

101-
#ifdef IPS_DEBUG_EN
102-
std::cout << "Performing convolution" << std::endl;
103-
#endif // IPS_DEBUG_EN
104-
105101
// Perform the convolution
106102
for (i = 0; i < N; ++i)
107103
for (j = 0; j < N; ++j)
108-
{
109-
#ifdef IPS_DEBUG_EN
110-
std::cout << "Starting [" << i << "][" << j << "]" << std::endl;
111-
#endif // IPS_DEBUG_EN
112104
result_tmp += this->kernel[i * N + j] * ((OUT) img_window_tmp[i * N + j]);
113-
#ifdef IPS_DEBUG_EN
114-
std::cout << "Done [" << i << "][" << j << "]" << std::endl;
115-
#endif // IPS_DEBUG_EN
116-
}
117105

118-
#ifdef IPS_DEBUG_EN
119-
std::cout << "Convolution result is done" << std::endl;
120-
#endif // IPS_DEBUG_EN
121106
this->result.write(result_tmp);
122107
}
123108
}

modules/filter/src/tb_filter.cpp

Lines changed: 158 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
#define int64 systemc_int64
2+
#define uint64 systemc_uint64
3+
#include <systemc.h>
14
#ifdef TEST_MODE_ONE_WINDOW_RANDOM
25
#include <cstdlib>
3-
#endif // TEST_MODE_ONE_WINDOW_RANDOM
6+
#elif defined(TEST_MODE_IMAGE)
7+
#include <cstdlib>
8+
#undef int64
9+
#undef uint64
10+
#define int64 opencv_int64
11+
#define uint64 opencv_uint64
12+
#include <opencv2/opencv.hpp>
13+
#undef int64
14+
#undef uint64
15+
#define SYSTEMC_TYPES_ALREADY_DEFINED
16+
#endif // TEST_MODE
417
#ifdef IPS_DUMP_EN
518
#include <sstream>
619
#endif // IPS_DUMP_EN
7-
#include <systemc.h>
820

921
#ifndef IPS_FILTER_KERNEL_SIZE
1022
#define IPS_FILTER_KERNEL_SIZE 3
@@ -15,6 +27,9 @@
1527
#ifndef IPS_OUT_TYPE_TB
1628
#define IPS_OUT_TYPE_TB float
1729
#endif // IPS_OUT_TYPE_TB
30+
#ifndef IPS_IMG_PATH_TB
31+
#define IPS_IMG_PATH_TB "../../tools/datagen/src/imgs/car_noisy_image.png"
32+
#endif // IPS_IMG_PATH_TB
1833

1934
#ifndef IPS_FILTER_PV_EN
2035
// N * N * copy_pixel_to_mem_time + mult + redux + copy_pixel_to_mem_time
@@ -33,6 +48,135 @@
3348
#endif // IPS_FILTER_XX_EN
3449

3550

51+
#ifdef TEST_MODE_IMAGE
52+
#ifdef IPS_DUMP_EN
53+
void run_image(sc_trace_file* wf)
54+
#else
55+
void run_image()
56+
#endif // IPS_DUMP_EN
57+
{
58+
#ifdef IPS_DEBUG_EN
59+
SC_REPORT_INFO("TEST_MODE_IMAGE", "Running test");
60+
#endif // IPS_DEBUG_EN
61+
62+
const std::string img_path = IPS_IMG_PATH_TB;
63+
cv::Mat read_image = cv::imread(img_path, cv::IMREAD_GRAYSCALE);
64+
cv::Mat image;
65+
read_image.convertTo(image, CV_32F);
66+
67+
#ifdef IPS_DUMP_EN
68+
std::cout << "Loading image: " << img_path << std::endl;
69+
#endif // IPS_DUMP_EN
70+
71+
// Check if the image is loaded successfully
72+
if (image.empty())
73+
{
74+
std::cerr << "Error: Could not open or find the image!" << std::endl;
75+
exit(EXIT_FAILURE);
76+
}
77+
78+
#ifdef IPS_DUMP_EN
79+
std::cout << "Image info: ";
80+
std::cout << "rows = " << image.rows;
81+
std::cout << " cols = " << image.cols;
82+
std::cout << " channels = " << image.channels() << std::endl;
83+
#endif // IPS_DUMP_EN
84+
85+
// Variables
86+
IPS_IN_TYPE_TB* img_window;
87+
IPS_OUT_TYPE_TB result;
88+
89+
int i, j;
90+
int x, y;
91+
92+
#ifdef IPS_FILTER_AT_EN
93+
sc_signal<IPS_IN_TYPE_TB*> s_img_window;
94+
sc_signal<IPS_OUT_TYPE_TB> s_result;
95+
#endif // IPS_FILTER_AT_EN
96+
97+
// Initialize image window
98+
img_window = new IPS_IN_TYPE_TB[IPS_FILTER_KERNEL_SIZE * IPS_FILTER_KERNEL_SIZE];
99+
100+
// Instantiate filter module and do the connection
101+
#ifdef IPS_DUMP_EN
102+
for (i = 0; i < IPS_FILTER_KERNEL_SIZE; ++i)
103+
{
104+
for (j = 0; j < IPS_FILTER_KERNEL_SIZE; ++j)
105+
{
106+
std::ostringstream var_name;
107+
var_name << "img_window_" << i << "_" << j;
108+
sc_trace(wf, img_window[i * IPS_FILTER_KERNEL_SIZE + j], var_name.str());
109+
}
110+
}
111+
112+
Filter<IPS_IN_TYPE_TB, IPS_OUT_TYPE_TB, IPS_FILTER_KERNEL_SIZE> filter("filter", wf);
113+
sc_trace(wf, result, "result");
114+
#else
115+
Filter<IPS_IN_TYPE_TB, IPS_OUT_TYPE_TB, IPS_FILTER_KERNEL_SIZE> filter("filter");
116+
#endif // IPS_DUMP_EN
117+
118+
sc_start();
119+
120+
#ifdef IPS_DEBUG_EN
121+
std::cout << "Test starting" << std::endl;
122+
std::cout << "@" << sc_time_stamp() << std::endl;
123+
#endif // IPS_DEBUG_EN
124+
125+
// Create each window
126+
for (y = 0; y < image.rows - IPS_FILTER_KERNEL_SIZE; ++y)
127+
{
128+
for (x = 0; x < image.cols - IPS_FILTER_KERNEL_SIZE; ++x)
129+
{
130+
#ifdef IPS_DEBUG_EN
131+
//SC_REPORT_INFO("TEST_MODE_IMAGE", "filtering");
132+
#endif // IPS_DEBUG_EN
133+
134+
// Define the ROI
135+
cv::Rect roi(x, y, IPS_FILTER_KERNEL_SIZE, IPS_FILTER_KERNEL_SIZE);
136+
cv::Mat sub_img = image(roi);
137+
138+
for (i = 0; i < IPS_FILTER_KERNEL_SIZE; ++i)
139+
{
140+
for (j = 0; j < IPS_FILTER_KERNEL_SIZE; ++j)
141+
{
142+
img_window[i * IPS_FILTER_KERNEL_SIZE + j] = sub_img.at<IPS_IN_TYPE_TB>(i, j);
143+
#ifdef IPS_DEBUG_EN
144+
//std::cout << "[" << img_window[i * IPS_FILTER_KERNEL_SIZE + j] << "]";
145+
#endif // IPS_DEBUG_EN
146+
}
147+
148+
#ifdef IPS_DEBUG_EN
149+
//std::cout << std::endl;
150+
#endif // IPS_DEBUG_EN
151+
}
152+
153+
// Apply convolution
154+
#ifdef IPS_FILTER_PV_EN
155+
filter.filter(img_window, result);
156+
#elif defined(IPS_FILTER_LT_EN)
157+
filter.filter(img_window, &result);
158+
sc_start(DELAY_TIME + 10, SC_NS);
159+
#elif defined(IPS_FILTER_AT_EN)
160+
filter.filter();
161+
sc_start(DELAY_TIME + 10, SC_NS);
162+
163+
result = s_result.read();
164+
#endif // IPS_FILTER_XX_EN
165+
166+
#ifdef IPS_DEBUG_EN
167+
//std::cout << "Result[" << x << "][" << y << "] = " << result << std::endl << std::endl;
168+
#endif // IPS_DEBUG_EN
169+
}
170+
}
171+
172+
#ifdef IPS_DUMP_EN
173+
sc_start(1, SC_NS);
174+
#endif // IPS_DUMP_EN
175+
176+
delete [] img_window;
177+
}
178+
#endif // TEST_MODE_IMAGE
179+
36180
#ifdef TEST_MODE_ONE_WINDOW
37181
#ifdef IPS_DUMP_EN
38182
void run_one_window(sc_trace_file* wf)
@@ -52,7 +196,7 @@ void run_one_window()
52196
#endif
53197

54198
// Variables
55-
IPS_IN_TYPE_TB *img_window;
199+
IPS_IN_TYPE_TB* img_window;
56200
IPS_OUT_TYPE_TB result;
57201

58202
#ifdef IPS_FILTER_AT_EN
@@ -155,8 +299,18 @@ int sc_main(int, char*[])
155299
#endif // IPS_DUMP_EN
156300

157301
#ifdef TEST_MODE_ONE_WINDOW
302+
#ifdef IPS_DUMP_EN
158303
run_one_window(wf);
159-
#endif // TEST_MODE_ONE_WINDOW
304+
#else
305+
run_one_window();
306+
#endif // IPS_DUMP_EN
307+
#elif defined(TEST_MODE_IMAGE)
308+
#ifdef IPS_DUMP_EN
309+
run_image(wf);
310+
#else
311+
run_image();
312+
#endif // IPS_DUMP_EN
313+
#endif // TEST_MODE
160314

161315
#ifdef IPS_DUMP_EN
162316
std::cout << "@" << sc_time_stamp() << " Terminating simulation" << std::endl;

0 commit comments

Comments
 (0)