          if not self._args.redirect_gui and not self._args.colab and sys.stdout.isatty():
              logger.info("  Press 'S' to save model weights immediately")
          logger.info("===================================================")
  
          keypress = KBHit(is_gui=self._args.redirect_gui)
          window_created = False
          err = False
          while True:
              try:
                  if self._args.preview:
                      with self._lock:
                          for name, image in self._preview_buffer.items():
                              if not window_created:
                                  self._create_resizable_window(name, image.shape)
                              cv2.imshow(name, image)  # pylint: disable=no-member
                          if not window_created:
                              window_created = bool(self._preview_buffer)
                      cv_key = cv2.waitKey(1000)  # pylint: disable=no-member
                  else:
                      cv_key = None
  
                  if thread.has_error:
                      logger.debug("Thread error detected")
                      err = True
                      break
                  if self._stop:
                      logger.debug("Stop received")
                      break
  
                  # Preview Monitor
                  if not self._preview_monitor(cv_key):
                      break
  
                  # Console Monitor
                  if keypress.kbhit():
                      console_key = keypress.getch()
                      if console_key in ("\n", "\r"):
                          logger.debug("Exit requested")
                          break
                      if console_key in ("s", "S"):
                          logger.info("Save requested")
                          self._save_now = True
  
                  # GUI Preview trigger update monitor
                  self._process_gui_triggers()
  
                  sleep(1)
              except KeyboardInterrupt:
                  logger.debug("Keyboard Interrupt received")
                  break
          keypress.set_normal_term()
          logger.debug("Closed Monitor")
          return err
  
      @classmethod
      def _create_resizable_window(cls, name: str, image_shape: tuple) -> None:
          """ Create a resizable OpenCV window to hold the preview image.
  
          Parameters
 
</s> remove     def _preview_monitor(self, key_press):
 </s> add     def _do_preview(self, window_created: bool) -> bool:
        """" Display an image preview in a resizable window

        Parameters
        ----------
        window_created: bool
            ``True`` if a preview window has been created otherwise ``False``

        Returns
        -------
        bool
            ``True`` if a preview window has been created otherwise ``False``
        """
        with self._lock:
            for name, image in self._preview_buffer.items():
                if not window_created:
                    self._create_resizable_window(name, image.shape)
                cv2.imshow(name, image)  # pylint: disable=no-member
            window_created = bool(self._preview_buffer) if not window_created else window_created
        return window_created

    def _check_keypress(self, keypress: KBHit) -> bool:
        """ Check if a keypress has been detected.

        Parameters
        ----------
        keypress: :class:`lib.keypress.KBHit`
            The keypress monitor

        Returns
        -------
        bool
            ``True`` if an exit keypress has been detected otherwise ``False``
        """
        retval = False
        if keypress.kbhit():
            console_key = keypress.getch()
            if console_key in ("\n", "\r"):
                logger.debug("Exit requested")
                retval = True
            if console_key in ("s", "S"):
                logger.info("Save requested")
                self._save_now = True
        return retval

    def _preview_monitor(self, key_press: str) -> bool: </s> remove     def _monitor(self, thread):
        """ Monitor the background :func:`_training` thread for key presses and errors.

        Returns
        -------
        bool
            ``True`` if there has been an error in the background thread otherwise ``False``
        """
 </s> add     def _output_startup_info(self):
        """ Print the startup information to the console. """ </s> remove     def _process_gui_triggers(self):
 </s> add     def _process_gui_triggers(self) -> None: </s> remove             logger.verbose("Toggle mask display requested")
 </s> add             logger.verbose("Toggle mask display requested")  # type:ignore </s> remove         model.save()
 </s> add         model.save(is_exit=True)
          if not self._args.redirect_gui and not self._args.colab and sys.stdout.isatty():
              logger.info("  Press 'S' to save model weights immediately")
          logger.info("===================================================")
  


















































      @classmethod
      def _create_resizable_window(cls, name: str, image_shape: tuple) -> None:
          """ Create a resizable OpenCV window to hold the preview image.
  
          Parameters
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 Training: Add setting option to save optimizer weights 
scripts/train.py