diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/dual_main.py b/dual_main.py index abef7cc..1c62188 100755 --- a/dual_main.py +++ b/dual_main.py @@ -1,4 +1,6 @@ import os +import time + import numpy as np from models import SpecDetector, PixelWisedDetector from root_dir import ROOT_DIR @@ -39,7 +41,7 @@ def main(pxl_model_path=pxl_model_path, blk_model_path=blk_model_path): data_total = data data = b'' os.close(fd_img) - + t1 = time.time() img = np.frombuffer(data_total, dtype=np.float32).reshape((nrows, nbands, -1)).transpose(0, 2, 1) print(f"get img shape {img.shape}") pxl_img_queue.put(img) @@ -48,6 +50,7 @@ def main(pxl_model_path=pxl_model_path, blk_model_path=blk_model_path): blk_msk = blk_msk_queue.get() mask = pxl_msk & blk_msk print(f"predict success get mask shape: {mask.shape}") + print(f"Time: {time.time() - t1}") # 写出 fd_mask = os.open(mask_fifo_path, os.O_WRONLY) os.write(fd_mask, mask.tobytes()) diff --git a/dual_main_fifo.py b/dual_main_fifo.py new file mode 100755 index 0000000..bd6d3f5 --- /dev/null +++ b/dual_main_fifo.py @@ -0,0 +1,149 @@ +import os +import time + +import numpy as np +from models import SpecDetector, PixelWisedDetector +from root_dir import ROOT_DIR +from multiprocessing import Process, Pipe + +nrows, ncols, nbands = 256, 1024, 4 +img_fifo_path = "/tmp/dkimg.fifo" +mask_fifo_path = "/tmp/dkmask.fifo" +cmd_fifo_path = '/tmp/tobacco_cmd.fifo' + +pxl_model_path = "rf_1x1_c4_1_sen1_4.model" +blk_model_path = "rf_8x8_c4_185_sen32_4.model" + + +def main(pxl_model_path=pxl_model_path, blk_model_path=blk_model_path): + # 启动两个模型线程 + blk_img_pipe_parent, blk_img_pipe_child = Pipe() + pxl_img_pipe_parent, pxl_msk_pipe_child = Pipe() + blk_cmd_pipe_parent, blk_cmd_pipe_child = Pipe() + pxl_cmd_pipe_parent, pxl_cmd_pipe_child = Pipe() + blk_process = Process(target=block_model, args=(blk_cmd_queue, blk_img_queue, blk_msk_queue, blk_model_path, )) + pxl_process = Process(target=pixel_model, args=(pxl_cmd_queue, pxl_img_queue, pxl_msk_queue, pxl_model_path, )) + blk_process.start() + pxl_process.start() + img = np.ones((nrows, ncols, nbands)) + t1 = time.time() + pxl_img_queue.put(img) + blk_img_queue.put(img) + t2 = time.time() + while pxl_msk_queue.empty(): + pass + pxl_msk = pxl_msk_queue.get() + while blk_msk_queue.empty(): + pass + blk_msk = blk_msk_queue.get() + t3 = time.time() + mask = pxl_msk & blk_msk + t4 = time.time() + print(f"spent {(t2-t1)*1000:.2f} ms to put data") + print(f"spent {(t3-t2)*1000:.2f} ms to get data") + print(f"spent {(t4-t3)*1000:.2f} ms to perform 'and' operation") + print(f"predict success get mask shape: {mask.shape}") + print(f"Total Time: {(t4 - t1)*1000:.2f} ms") + total_len = nrows * ncols * nbands * 4 + if not os.access(img_fifo_path, os.F_OK): + os.mkfifo(img_fifo_path, 0o777) + if not os.access(mask_fifo_path, os.F_OK): + os.mkfifo(mask_fifo_path, 0o777) + data = b'' + while True: + fd_img = os.open(img_fifo_path, os.O_RDONLY) + while len(data) < total_len: + data += os.read(fd_img, total_len) + if len(data) > total_len: + data_total = data[:total_len] + data = data[total_len:] + else: + data_total = data + data = b'' + os.close(fd_img) + + img = np.frombuffer(data_total, dtype=np.float32).reshape((nrows, nbands, -1)).transpose(0, 2, 1) + print(f"get img shape {img.shape}") + t1 = time.time() + pxl_img_queue.put(img) + blk_img_queue.put(img) + pxl_msk = pxl_msk_queue.get() + blk_msk = blk_msk_queue.get() + mask = pxl_msk & blk_msk + print(f"predict success get mask shape: {mask.shape}") + print(f"Time: {time.time() - t1}") + # 写出 + fd_mask = os.open(mask_fifo_path, os.O_WRONLY) + os.write(fd_mask, mask.tobytes()) + os.close(fd_mask) + + +def block_model(cmd_pipe:Pipe, img_pipe:Pipe, blk_model_path=blk_model_path): + blk_model = SpecDetector(os.path.join(ROOT_DIR, "models", blk_model_path), blk_sz=8, channel_num=4) + _ = blk_model.predict(np.ones((nrows, ncols, nbands))) + rigor_rate = 100 + while True: + # deal with the cmd if cmd_queue is not empty + if not cmd_queue.empty(): + cmd = cmd_queue.get() + if isinstance(cmd, int): + rigor_rate = cmd + elif isinstance(cmd, str): + if cmd == 'stop': + break + else: + try: + blk_model_path = SpecDetector(os.path.join(ROOT_DIR, "models", blk_model_path), + blk_sz=8, channel_num=4) + except Exception as e: + print(f"Load Model Failed! {e}") + # deal with the img if img_queue is not empty + if not img_queue.empty(): + t1 = time.time() + img = img_queue.get() + t2 = time.time() + mask = blk_model.predict(img, rigor_rate) + t3 = time.time() + mask_queue.put(mask) + t4 = time.time() + print(f"block model spent:{(t2-t1)*1000:.2f}ms to get img") + print(f"block model spent:{(t3-t2)*1000:.2f}ms to run model") + print(f"block model spent:{(t4-t3)*1000:.2f}ms to put img") + print(f"block model spent:{(t4 - t1)*1000:.2f}ms") + + +def pixel_model(cmd_queue: Queue, img_queue: Queue, mask_queue: Queue, pixel_model_path=pxl_model_path): + pixel_model = PixelWisedDetector(os.path.join(ROOT_DIR, "models", pixel_model_path), blk_sz=1, channel_num=4) + _ = pixel_model.predict(np.ones((nrows, ncols, nbands))) + rigor_rate = 100 + while True: + # deal with the cmd if cmd_queue is not empty + if not cmd_queue.empty(): + cmd = cmd_queue.get() + if isinstance(cmd, int): + rigor_rate = cmd + elif isinstance(cmd, str): + if cmd == 'stop': + break + else: + try: + pixel_model = PixelWisedDetector(os.path.join(ROOT_DIR, "models", pixel_model_path), + blk_sz=1, channel_num=4) + except Exception as e: + print(f"Load Model Failed! {e}") + # deal with the img if img_queue is not empty + if not img_queue.empty(): + t1 = time.time() + img = img_queue.get() + t2 = time.time() + mask = pixel_model.predict(img, rigor_rate) + t3 = time.time() + mask_queue.put(mask) + t4 = time.time() + print(f"pixel model spent:{(t2-t1)*1000:.2f}ms to get img") + print(f"pixel model spent:{(t3-t2)*1000:.2f}ms to run model") + print(f"pixel model spent:{(t4-t3)*1000:.2f}ms to put img") + print(f"pixel model spent:{(t4 - t1)*1000:.2f}ms") + +if __name__ == '__main__': + main() diff --git a/dual_main_one_by_one.py b/dual_main_one_by_one.py new file mode 100755 index 0000000..c346051 --- /dev/null +++ b/dual_main_one_by_one.py @@ -0,0 +1,65 @@ +import os +import time +import cv2 +import numpy as np +from models import SpecDetector, PixelWisedDetector +from root_dir import ROOT_DIR + +nrows, ncols, nbands = 256, 1024, 4 +img_fifo_path = "/tmp/dkimg.fifo" +mask_fifo_path = "/tmp/dkmask.fifo" +selected_model = "rf_8x8_c4_185_sen32_4.model" +pxl_model_path = "rf_1x1_c4_1_sen1_4.model" + + +def main(): + model_path = os.path.join(ROOT_DIR, "models", selected_model) + detector = SpecDetector(model_path, blk_sz=8, channel_num=4) + model_path = os.path.join(ROOT_DIR, "models", pxl_model_path) + detector2 = PixelWisedDetector(model_path, blk_sz=1, channel_num=4) + _ = detector.predict(np.ones((nrows, ncols, nbands))) + _ = detector2.predict(np.ones((nrows, ncols, nbands))) + total_len = nrows * ncols * nbands * 4 + + if not os.access(img_fifo_path, os.F_OK): + os.mkfifo(img_fifo_path, 0o777) + if not os.access(mask_fifo_path, os.F_OK): + os.mkfifo(mask_fifo_path, 0o777) + data = b'' + while True: + # 读取 + fd_img = os.open(img_fifo_path, os.O_RDONLY) + while len(data) < total_len: + data += os.read(fd_img, total_len) + if len(data) > total_len: + data_total = data[:total_len] + data = data[total_len:] + else: + data_total = data + data = b'' + + os.close(fd_img) + # 识别 + img = np.frombuffer(data_total, dtype=np.float32).reshape((nrows, nbands, -1)).transpose(0, 2, 1) + t1 = time.time() + mask1 = detector.predict(img) + t2 = time.time() + mask2 = detector2.predict(img) + t3 = time.time() + mask = mask2 & mask1 + t4 = time.time() + print("="*40) + print(f"block: {(t2 - t1)*1000:.2f}ms \n," + f"pixel: {(t3 - t2)*1000:.2f}ms \n" + f"mask: {(t4 -t3)*1000:.2f}ms \n" + f"Total: {(t4 - t1)*1000:.2f}ms") + print("="*40) + print() + # 写出 + fd_mask = os.open(mask_fifo_path, os.O_WRONLY) + os.write(fd_mask, mask.tobytes()) + os.close(fd_mask) + + +if __name__ == '__main__': + main() diff --git a/dual_main_pipe.py b/dual_main_pipe.py new file mode 100755 index 0000000..e7b26e3 --- /dev/null +++ b/dual_main_pipe.py @@ -0,0 +1,115 @@ +import os +import time + +import numpy as np +from models import SpecDetector, PixelWisedDetector +from root_dir import ROOT_DIR +from multiprocessing import Process, Pipe +from multiprocessing.connection import Connection + +nrows, ncols, nbands = 256, 1024, 4 + +img_fifo_path = "/tmp/dkimg.fifo" +mask_fifo_path = "/tmp/dkmask.fifo" +cmd_fifo_path = '/tmp/tobacco_cmd.fifo' + +pxl_model_path = "rf_1x1_c4_1_sen1_4.model" +blk_model_path = "rf_8x8_c4_185_sen32_4.model" + + +def main(pxl_model_path=pxl_model_path, blk_model_path=blk_model_path): + # make fifos to communicate with the child model processes + blk_img_pipe_parent, blk_img_img_pipe_child = Pipe() + blk_msk_pipe_parent, blk_msk_pipe_child = Pipe() + blk_cmd_pipe_parent, blk_cmd_pipe_child = Pipe() + blk_process = Process(target=model_process_func, + args=(blk_cmd_pipe_child, blk_img_img_pipe_child, + blk_msk_pipe_child, "blk", blk_model_path, )) + pxl_img_pipe_parent, pxl_img_img_pipe_child = Pipe() + pxl_msk_pipe_parent, pxl_msk_pipe_child = Pipe() + pxl_cmd_pipe_parent, pxl_cmd_pipe_child = Pipe() + pxl_process = Process(target=model_process_func, + args=(pxl_cmd_pipe_child, pxl_img_img_pipe_child, + pxl_cmd_pipe_child, "pxl", blk_model_path, )) + + blk_process.start() + pxl_process.start() + total_len = nrows * ncols * nbands * 4 + if not os.access(img_fifo_path, os.F_OK): + os.mkfifo(img_fifo_path, 0o777) + if not os.access(mask_fifo_path, os.F_OK): + os.mkfifo(mask_fifo_path, 0o777) + data = b'' + while True: + fd_img = os.open(img_fifo_path, os.O_RDONLY) + while len(data) < total_len: + data += os.read(fd_img, total_len) + if len(data) > total_len: + data_total = data[:total_len] + data = data[total_len:] + else: + data_total = data + data = b'' + os.close(fd_img) + t1 = time.time() + img = np.frombuffer(data_total, dtype=np.float32).reshape((nrows, nbands, -1)).transpose(0, 2, 1) + print(f"get img shape {img.shape}") + pxl_img_queue.put(img) + blk_img_queue.put(img) + pxl_msk = pxl_msk_queue.get() + blk_msk = blk_msk_queue.get() + mask = pxl_msk & blk_msk + print(f"predict success get mask shape: {mask.shape}") + print(f"Time: {time.time() - t1}") + # 写出 + fd_mask = os.open(mask_fifo_path, os.O_WRONLY) + os.write(fd_mask, mask.tobytes()) + os.close(fd_mask) + + +def model_process_func(cmd_pipe: Connection, img_pipe: Connection, msk_pipe: Connection, + model_cls: str, model_path=pxl_model_path): + assert model_cls in ['pxl', 'blk'] + if model_cls == 'pxl': + model = PixelWisedDetector(os.path.join(ROOT_DIR, "models", model_path), + blk_sz=1, channel_num=4) + else: + model = SpecDetector(os.path.join(ROOT_DIR, "models", model_path), + blk_sz=8, channel_num=4) + _ = model.predict(np.ones((nrows, ncols, nbands))) + rigor_rate = 70 + while True: + # deal with the cmd if cmd_queue is not empty + if not cmd_pipe.poll(): + cmd = cmd_pipe.recv() + if isinstance(cmd, int): + rigor_rate = cmd + elif isinstance(cmd, str): + if cmd == 'stop': + break + else: + try: + if model_cls == 'pxl': + model = PixelWisedDetector(os.path.join(ROOT_DIR, "models", model_path), + blk_sz=1, channel_num=4) + else: + model = SpecDetector(os.path.join(ROOT_DIR, "models", model_path), + blk_sz=8, channel_num=4) + except Exception as e: + print(f"Load Model Failed! {e}") + # deal with the img if img_queue is not empty + if not img_pipe.poll(): + t1 = time.time() + img = img_pipe.recv() + t2 = time.time() + mask = model.predict(img, rigor_rate) + t3 = time.time() + msk_pipe.send(mask) + t4 = time.time() + print(f"{model_cls} model recv time: {(t2 - t1) * 1000}ms\n" + f"{model_cls} model predict time: {(t3 - t2) * 1000}ms\n" + f"{model_cls} model send time: {(t4 - t3) * 1000}ms") + + +if __name__ == '__main__': + main() diff --git a/dual_main_queue.py b/dual_main_queue.py new file mode 100755 index 0000000..b6425fb --- /dev/null +++ b/dual_main_queue.py @@ -0,0 +1,148 @@ +import os +import time + +import numpy as np +from models import SpecDetector, PixelWisedDetector +from root_dir import ROOT_DIR +from multiprocessing import Process, Queue + +nrows, ncols, nbands = 256, 1024, 4 +img_fifo_path = "/tmp/dkimg.fifo" +mask_fifo_path = "/tmp/dkmask.fifo" +cmd_fifo_path = '/tmp/tobacco_cmd.fifo' + +pxl_model_path = "rf_1x1_c4_1_sen1_4.model" +blk_model_path = "rf_8x8_c4_185_sen32_4.model" + + +def main(pxl_model_path=pxl_model_path, blk_model_path=blk_model_path): + # 启动两个模型线程 + blk_cmd_queue, pxl_cmd_queue = Queue(maxsize=100), Queue(maxsize=100) + blk_img_queue, pxl_img_queue = Queue(maxsize=100), Queue(maxsize=100) + blk_msk_queue, pxl_msk_queue = Queue(maxsize=100), Queue(maxsize=100) + blk_process = Process(target=block_model, args=(blk_cmd_queue, blk_img_queue, blk_msk_queue, blk_model_path, )) + pxl_process = Process(target=pixel_model, args=(pxl_cmd_queue, pxl_img_queue, pxl_msk_queue, pxl_model_path, )) + blk_process.start() + pxl_process.start() + img = np.ones((nrows, ncols, nbands)) + t1 = time.time() + pxl_img_queue.put(img) + blk_img_queue.put(img) + t2 = time.time() + while pxl_msk_queue.empty(): + pass + pxl_msk = pxl_msk_queue.get() + while blk_msk_queue.empty(): + pass + blk_msk = blk_msk_queue.get() + t3 = time.time() + mask = pxl_msk & blk_msk + t4 = time.time() + print(f"spent {(t2-t1)*1000:.2f} ms to put data") + print(f"spent {(t3-t2)*1000:.2f} ms to get data") + print(f"spent {(t4-t3)*1000:.2f} ms to perform 'and' operation") + print(f"predict success get mask shape: {mask.shape}") + print(f"Total Time: {(t4 - t1)*1000:.2f} ms") + total_len = nrows * ncols * nbands * 4 + if not os.access(img_fifo_path, os.F_OK): + os.mkfifo(img_fifo_path, 0o777) + if not os.access(mask_fifo_path, os.F_OK): + os.mkfifo(mask_fifo_path, 0o777) + data = b'' + while True: + fd_img = os.open(img_fifo_path, os.O_RDONLY) + while len(data) < total_len: + data += os.read(fd_img, total_len) + if len(data) > total_len: + data_total = data[:total_len] + data = data[total_len:] + else: + data_total = data + data = b'' + os.close(fd_img) + + img = np.frombuffer(data_total, dtype=np.float32).reshape((nrows, nbands, -1)).transpose(0, 2, 1) + print(f"get img shape {img.shape}") + t1 = time.time() + pxl_img_queue.put(img) + blk_img_queue.put(img) + pxl_msk = pxl_msk_queue.get() + blk_msk = blk_msk_queue.get() + mask = pxl_msk & blk_msk + print(f"predict success get mask shape: {mask.shape}") + print(f"Time: {time.time() - t1}") + # 写出 + fd_mask = os.open(mask_fifo_path, os.O_WRONLY) + os.write(fd_mask, mask.tobytes()) + os.close(fd_mask) + + +def block_model(cmd_queue: Queue, img_queue: Queue, mask_queue: Queue, blk_model_path=blk_model_path): + blk_model = SpecDetector(os.path.join(ROOT_DIR, "models", blk_model_path), blk_sz=8, channel_num=4) + _ = blk_model.predict(np.ones((nrows, ncols, nbands))) + rigor_rate = 100 + while True: + # deal with the cmd if cmd_queue is not empty + if not cmd_queue.empty(): + cmd = cmd_queue.get() + if isinstance(cmd, int): + rigor_rate = cmd + elif isinstance(cmd, str): + if cmd == 'stop': + break + else: + try: + blk_model_path = SpecDetector(os.path.join(ROOT_DIR, "models", blk_model_path), + blk_sz=8, channel_num=4) + except Exception as e: + print(f"Load Model Failed! {e}") + # deal with the img if img_queue is not empty + if not img_queue.empty(): + t1 = time.time() + img = img_queue.get() + t2 = time.time() + mask = blk_model.predict(img, rigor_rate) + t3 = time.time() + mask_queue.put(mask) + t4 = time.time() + print(f"block model spent:{(t2-t1)*1000:.2f}ms to get img") + print(f"block model spent:{(t3-t2)*1000:.2f}ms to run model") + print(f"block model spent:{(t4-t3)*1000:.2f}ms to put img") + print(f"block model spent:{(t4 - t1)*1000:.2f}ms") + + +def pixel_model(cmd_queue: Queue, img_queue: Queue, mask_queue: Queue, pixel_model_path=pxl_model_path): + pixel_model = PixelWisedDetector(os.path.join(ROOT_DIR, "models", pixel_model_path), blk_sz=1, channel_num=4) + _ = pixel_model.predict(np.ones((nrows, ncols, nbands))) + rigor_rate = 100 + while True: + # deal with the cmd if cmd_queue is not empty + if not cmd_queue.empty(): + cmd = cmd_queue.get() + if isinstance(cmd, int): + rigor_rate = cmd + elif isinstance(cmd, str): + if cmd == 'stop': + break + else: + try: + pixel_model = PixelWisedDetector(os.path.join(ROOT_DIR, "models", pixel_model_path), + blk_sz=1, channel_num=4) + except Exception as e: + print(f"Load Model Failed! {e}") + # deal with the img if img_queue is not empty + if not img_queue.empty(): + t1 = time.time() + img = img_queue.get() + t2 = time.time() + mask = pixel_model.predict(img, rigor_rate) + t3 = time.time() + mask_queue.put(mask) + t4 = time.time() + print(f"pixel model spent:{(t2-t1)*1000:.2f}ms to get img") + print(f"pixel model spent:{(t3-t2)*1000:.2f}ms to run model") + print(f"pixel model spent:{(t4-t3)*1000:.2f}ms to put img") + print(f"pixel model spent:{(t4 - t1)*1000:.2f}ms") + +if __name__ == '__main__': + main() diff --git a/main.py b/main.py index 5015319..bd9dca9 100755 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import os +import time import cv2 import numpy as np @@ -14,7 +15,16 @@ selected_model = "rf_8x8_c4_185_sen32_4.model" def main(): model_path = os.path.join(ROOT_DIR, "models", selected_model) detector = SpecDetector(model_path, blk_sz=8, channel_num=4) - _ = detector.predict(np.ones((256, 1024, 4))) + t1 = time.time() + _ = detector.predict(np.zeros((nrows, ncols, nbands))) + t2 = time.time() + _ = detector.predict(np.zeros((nrows, ncols, nbands))) + t3 = time.time() + _ = detector.predict(np.ones((nrows, ncols, nbands))) + t4 = time.time() + print(f"First time test {(t2 -t1)*1000:.2f}ms\n" + f"Second time test {(t3 - t2)*1000:.2f}ms\n" + f"Third time test {(t4 - t3)*1000:.2f}ms\n") total_len = nrows * ncols * nbands * 4 if not os.access(img_fifo_path, os.F_OK): @@ -22,6 +32,8 @@ def main(): if not os.access(mask_fifo_path, os.F_OK): os.mkfifo(mask_fifo_path, 0o777) data = b'' + print("Start the serveice.") + print("="*20) while True: # 读取 fd_img = os.open(img_fifo_path, os.O_RDONLY) @@ -37,7 +49,9 @@ def main(): os.close(fd_img) # 识别 img = np.frombuffer(data_total, dtype=np.float32).reshape((nrows, nbands, -1)).transpose(0, 2, 1) + t1 = time.time() mask = detector.predict(img) + print(f"time spent {(time.time() - t1)*1000:.2f}ms") # 写出 fd_mask = os.open(mask_fifo_path, os.O_WRONLY) os.write(fd_mask, mask.tobytes()) diff --git a/models.py b/models.py index 3ef77cd..3da1fd7 100755 --- a/models.py +++ b/models.py @@ -145,16 +145,16 @@ class SpecDetector(object): else: raise FileNotFoundError("Model File not found") - def predict(self, data, rigor_rate=70): + def predict(self, data, rigor_rate=100): blocks = split_x(data, blk_sz=self.blk_sz) blocks = np.array(blocks) features = feature(np.array(blocks)) - print("Spec Detector", rigor_rate) + # print("Spec Detector", rigor_rate) y_pred = self.clf.predict_proba(features) y_pred, y_prob = np.argmax(y_pred, axis=1), np.max(y_pred, axis=1) y_pred_binary = np.zeros_like(y_pred) # classes merge - y_pred_binary[((y_pred == 2) | (y_pred > 3)) & (y_prob > (100 - rigor_rate) / 100.0)] = 1 + y_pred_binary[((y_pred == 2) | (y_pred >= 3)) & (y_prob > (100 - rigor_rate) / 100.0)] = 1 # transform to mask mask = self.mask_transform(y_pred_binary, (ncols, nrows)) return mask @@ -182,7 +182,7 @@ class PixelWisedDetector(object): features = data.reshape((-1, self.channel_num)) y_pred = self.clf.predict(features, rigor_rate) y_pred_binary = np.ones_like(y_pred, dtype=np.uint8) - print("pixel detector", rigor_rate) + # print("pixel detector", rigor_rate) # classes merge y_pred_binary[(y_pred == 0) | (y_pred == 1) | (y_pred == 3)] = 0 # transform to mask