فهرست منبع

feat(Hyakkiyakou): Save useful pictures for training

ryland 2 سال پیش
والد
کامیت
1957c2a43e
5فایلهای تغییر یافته به همراه76 افزوده شده و 19 حذف شده
  1. 10 6
      tasks/Hyakkiyakou/agent/agent.py
  2. 1 1
      tasks/Hyakkiyakou/agent/focus.py
  3. 5 5
      tasks/Hyakkiyakou/config.py
  4. 49 2
      tasks/Hyakkiyakou/debugger.py
  5. 11 5
      tasks/Hyakkiyakou/script_task.py

+ 10 - 6
tasks/Hyakkiyakou/agent/agent.py

@@ -27,19 +27,23 @@ def generate_gaussian_patch(size=(300, 300), mean=0, std_dev=60):
 def embed_patch_in_canvas(canvas, patch, position=(0, 0), patch_size=(300, 300)):
 def embed_patch_in_canvas(canvas, patch, position=(0, 0), patch_size=(300, 300)):
     canvas_height, canvas_width = 720, 1280
     canvas_height, canvas_width = 720, 1280
     patch_height, patch_width = patch_size
     patch_height, patch_width = patch_size
-    patch_height = max(0, min(patch_height, canvas_height))
-    patch_width = max(0, min(patch_width, canvas_width))
+    pos_x, pos_y = position
+    pos_x = max(0, min(pos_x, canvas_width))
+    pos_y = max(0, min(pos_y, canvas_width))
 
 
-    x1 = position[0] - patch_width // 2
-    y1 = position[1] - patch_height // 2
-    x2 = position[0] + patch_width // 2
-    y2 = position[1] + patch_height // 2
+    x1 = pos_x - patch_width // 2
+    y1 = pos_y - patch_height // 2
+    x2 = pos_x + patch_width // 2
+    y2 = pos_y + patch_height // 2
     x1 = max(0, x1)
     x1 = max(0, x1)
     y1 = max(0, y1)
     y1 = max(0, y1)
     x2 = min(canvas_width, x2)
     x2 = min(canvas_width, x2)
     y2 = min(canvas_height, y2)
     y2 = min(canvas_height, y2)
     embed_w = x2 - x1
     embed_w = x2 - x1
     embed_h = y2 - y1
     embed_h = y2 - y1
+    if embed_w <= 0 or embed_h <= 0:
+        logger.warning(f'Cannot embed patch in canvas: ({position})')
+        return canvas
     if embed_w < patch_width or embed_h < patch_height:
     if embed_w < patch_width or embed_h < patch_height:
         _patch = patch[:embed_h, :embed_w]
         _patch = patch[:embed_h, :embed_w]
     else:
     else:

+ 1 - 1
tasks/Hyakkiyakou/agent/focus.py

@@ -116,6 +116,6 @@ velocity: {self._v}"""
         upsilon = (vector[1] / 250 - vector[2] / 35)
         upsilon = (vector[1] / 250 - vector[2] / 35)
         upsilon = 100 * (upsilon**2 if upsilon > 0 else - upsilon**2)
         upsilon = 100 * (upsilon**2 if upsilon > 0 else - upsilon**2)
         result = _omega + tau + upsilon - 0.6
         result = _omega + tau + upsilon - 0.6
-        logger.info(f"total: {result:.4f} | {_omega:.4f} | {tau:.4f} | {upsilon:.4f}")
+        # logger.info(f"total: {result:.4f} | {_omega:.4f} | {tau:.4f} | {upsilon:.4f}")
         return result
         return result
 
 

+ 5 - 5
tasks/Hyakkiyakou/config.py

@@ -23,11 +23,11 @@ class HyakkiyakouConfig(ConfigBase):
     hya_priorities: str = Field(default='', description='hya_priorities_help')
     hya_priorities: str = Field(default='', description='hya_priorities_help')
     hya_sp: float = Field(default=1., description='hya_sp_help')
     hya_sp: float = Field(default=1., description='hya_sp_help')
     hya_ssr: float = Field(default=1., description='hya_ssr_help')
     hya_ssr: float = Field(default=1., description='hya_ssr_help')
-    hya_sr: float = Field(default=0.5, description='hya_sr_help')
-    hya_r: float = Field(default=0, description='hya_r_help')
-    hya_n: float = Field(default=0, description='hya_n_help')
+    hya_sr: float = Field(default=0.7, description='hya_sr_help')
+    hya_r: float = Field(default=0.3, description='hya_r_help')
+    hya_n: float = Field(default=0.0, description='hya_n_help')
     # 呱太和N卡不是一个东西
     # 呱太和N卡不是一个东西
-    hya_g: float = Field(default=0, description='hya_g_help')
+    hya_g: float = Field(default=0.0, description='hya_g_help')
 
 
 
 
 class HyakkiyakouModels(ConfigBase):
 class HyakkiyakouModels(ConfigBase):
@@ -49,7 +49,7 @@ class DebugConfig(ConfigBase):
     # 保存图片,拿去回喂给模型
     # 保存图片,拿去回喂给模型
     continuous_learning: bool = Field(default=False, description='continuous_learning_help')
     continuous_learning: bool = Field(default=False, description='continuous_learning_help')
     # 单独的设定截屏间隔, 单位ms
     # 单独的设定截屏间隔, 单位ms
-    screenshot_interval: float = Field(default=300, description='screenshot_interval_help')
+    hya_interval: float = Field(default=300, description='hya_interval_help')
 
 
 
 
 class Hyakkiyakou(ConfigBase):
 class Hyakkiyakou(ConfigBase):

+ 49 - 2
tasks/Hyakkiyakou/debugger.py

@@ -5,10 +5,11 @@ import time
 import numpy as np
 import numpy as np
 
 
 from datetime import datetime
 from datetime import datetime
+from cached_property import cached_property
 from pathlib import Path
 from pathlib import Path
 from numpy import uint8, fromfile
 from numpy import uint8, fromfile
 from threading import Event, Lock, Thread
 from threading import Event, Lock, Thread
-from oashya.labels import id2label, CLASSIFY
+from oashya.labels import id2label, CLASSIFY, CLASSINDEX
 from oashya.utils import draw_tracks
 from oashya.utils import draw_tracks
 from oashya.tracker import Tracker
 from oashya.tracker import Tracker
 
 
@@ -73,9 +74,28 @@ class Debugger:
     sync_thread = Thread(target=show_track, daemon=True, name='OAS Track Debugger',
     sync_thread = Thread(target=show_track, daemon=True, name='OAS Track Debugger',
                          args=(sync_image, sync_lock, stop_event))
                          args=(sync_image, sync_lock, stop_event))
     # sync_event = Event()  # reserved for future use
     # sync_event = Event()  # reserved for future use
+    # -------------------------------------------------------------------------
 
 
-    def __init__(self, info_enable: bool = False):
+    def __init__(self, info_enable: bool = False, continuous_learning: bool = False):
         Debugger.info_enable = info_enable
         Debugger.info_enable = info_enable
+        self.images_cache: dict = {}
+        self.continuous_learning = continuous_learning
+        if continuous_learning:
+            logger.info('Continuous Learning Mode Enabled')
+            save_time = datetime.now().strftime('%Y%m%dT%H')
+            self.hya_save_folder: Path = Path(f'./log/hya/{save_time}')
+            self.hya_save_folder.mkdir(parents=True, exist_ok=True)
+
+    @cached_property
+    def save_class(self) -> list[int]:
+        """
+        需要保存的类别
+        @return:
+        """
+        sp = [i for i in range(CLASSINDEX.MIN_SP, CLASSINDEX.MAX_SP + 1)]
+        ssr = [i for i in range(CLASSINDEX.MIN_SSR, CLASSINDEX.MAX_SSR + 1)]
+        g = [i for i in range(CLASSINDEX.MIN_G, CLASSINDEX.MAX_G + 1)]
+        return sp + ssr + g
 
 
     def show(self, results):
     def show(self, results):
         image = draw_tracks(self.device.image, results)
         image = draw_tracks(self.device.image, results)
@@ -98,6 +118,31 @@ class Debugger:
         self.stop_event.set()
         self.stop_event.set()
         self.sync_thread.join()
         self.sync_thread.join()
 
 
+    def check_class(self, _class: int) -> bool:
+        return _class in self.save_class
+
+    def deal_learning(self, image, tracks: list):
+        save_flag: bool = False
+        for _id, _class, _conf, _cx, _cy, _w, _h, _v in tracks:
+            if self.check_class(_class):
+                save_flag = True
+                break
+        if not save_flag:
+            return
+        time_now_image_name = f'hya_{int(time.time() * 1000)}'
+        self.images_cache[time_now_image_name] = image
+
+    def save_images(self):
+        if not self.images_cache:
+            self.images_cache: dict = {}
+            return
+        logger.info('OAS Track Debugger save images to train model')
+        for image_name, image in self.images_cache.items():
+            cv2.imwrite(str(self.hya_save_folder / image_name), image)
+        self.images_cache.clear()
+
+
+
 
 
 def test_debugger():
 def test_debugger():
     debugger = Debugger()
     debugger = Debugger()
@@ -116,4 +161,6 @@ def test_debugger():
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
     # test_track()
     # test_track()
+    sp = datetime.now().strftime('%Y%m%d')
+    print(sp)
     pass
     pass

+ 11 - 5
tasks/Hyakkiyakou/script_task.py

@@ -94,11 +94,11 @@ class ScriptTask(GameUi, HyaSlave):
     @cached_property
     @cached_property
     def debugger(self) -> Debugger:
     def debugger(self) -> Debugger:
         debug_config = self._config.debug_config
         debug_config = self._config.debug_config
-        fast_screenshot_interval = debug_config.screenshot_interval
-        if fast_screenshot_interval <= 100 or fast_screenshot_interval >= 1000:
+        hya_interval = debug_config.hya_interval
+        if hya_interval <= 100 or hya_interval >= 1000:
             raise RequestHumanTakeover('screenshot_interval must be between 1000 and 10000')
             raise RequestHumanTakeover('screenshot_interval must be between 1000 and 10000')
-        self.set_fast_screenshot_interval(fast_screenshot_interval)
-        return Debugger(info_enable=debug_config.hya_info)
+        self.set_fast_screenshot_interval(hya_interval)
+        return Debugger(info_enable=debug_config.hya_info, continuous_learning=debug_config.continuous_learning)
 
 
     def run(self):
     def run(self):
         hya_count: int = 0
         hya_count: int = 0
@@ -172,15 +172,21 @@ class ScriptTask(GameUi, HyaSlave):
                 tracks = self.tracker(image=self.device.image, response=last_action)
                 tracks = self.tracker(image=self.device.image, response=last_action)
                 last_action = self.agent.decision(tracks=tracks, state=slave_state)
                 last_action = self.agent.decision(tracks=tracks, state=slave_state)
                 self.do_action(last_action, state=slave_state)
                 self.do_action(last_action, state=slave_state)
+            else:
+                # TODO freeze state
+                tracks = []
             # debug
             # debug
             if self._config.debug_config.hya_show:
             if self._config.debug_config.hya_show:
-                draw_image = draw_tracks(self.device.image, tracks)
+                draw_image = draw_tracks(self.device.image, tracks=tracks)
                 self.debugger.show_sync(image=draw_image)
                 self.debugger.show_sync(image=draw_image)
+            if self._config.debug_config.continuous_learning:
+                self.debugger.deal_learning(image=self.device.image, tracks=tracks)
 
 
         logger.info('Hyakkiyakou End')
         logger.info('Hyakkiyakou End')
         if self._config.debug_config.hya_show:
         if self._config.debug_config.hya_show:
             self.debugger.show_stop()
             self.debugger.show_stop()
         self.ui_click(self.I_HEND, self.I_HACCESS)
         self.ui_click(self.I_HEND, self.I_HACCESS)
+        del self.debugger
 
 
     def do_action(self, action: list, state):
     def do_action(self, action: list, state):
         x, y, throw, bean = action
         x, y, throw, bean = action