コード例 #1
0
 def __init__(self):
     super().__init__()
     self.ui = Ui_AIOptimization()
     self.ui.setupUi(self)
     self.ui.tab_widget.currentChanged.connect(self._on_changed_current_tab)
     LearningModel.default().training_finished.connect(
         self._on_finished_training)
     LearningModel.default().test_finished.connect(self._on_finished_test)
コード例 #2
0
 def test_object_instance(self):
     for i in range(1, 100):
         w = np.random.rand(1, 2) + 1
         d = random.randint(-5, 5)
         learning_model = LearningModel(w, d)
         x = np.random.rand(1, 2)
         value = learning_model.decide(x)
         print(f'value {value}')
コード例 #3
0
def generate_models(num_models: int,
                    specie='a',
                    generation=1) -> List[LearningModel]:
    to_return = list()
    size = (1, 3)

    for i in range(0, num_models):
        if specie == 'a':
            w_vector = list(
                i for i in (np.random.uniform(-2, 2, size)).flatten())
            d = random.randrange(-3, 3)
            model = LearningModel(w_vector, d)
            model.generation = generation
            to_return.append(model)
        elif specie == 'b':
            w_vector = list(
                i for i in (np.random.uniform(-5, 5, size)).flatten())
            d = random.randrange(-1, 1)
            model = LearningModel(w_vector, d)
            model.generation = generation
            to_return.append(model)
        elif specie == 'c_rare':
            w_vector = list(
                i for i in (np.random.uniform(-10, 10, size)).flatten())
            d = random.randrange(-10, 10)
            model = LearningModel(w_vector, d)
            model.generation = generation
            to_return.append(model)

    return to_return
コード例 #4
0
ファイル: dataset.py プロジェクト: akiphumi/SDTest
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dataset()
        self.ui.setupUi(self)

        self.all_thumbnails = []
        self.selected_thumbnails: Set[Thumbnail] = set()

        self.ui.image_list_widget.itemSelectionChanged.connect(
            self.on_changed_image_list_selection)
        self.ui.delete_images_button.clicked.connect(
            self.on_clicked_delete_images_button)
        self.ui.train_button.clicked.connect(self.on_clicked_train_button)

        self.ui.camera_and_images_menu = QMenu()
        self.ui.camera_and_images_menu.addAction(self.ui.select_images_action)
        self.ui.camera_and_images_menu.addAction(self.ui.camera_action)
        self.ui.camera_and_images_button.setMenu(
            self.ui.camera_and_images_menu)

        self.ui.select_images_action.triggered.connect(
            self.on_clicked_select_images_button)
        self.ui.camera_action.triggered.connect(self.on_clicked_camera_button)

        self.ui.image_list_widget.setCurrentItem(
            self.ui.image_list_widget.topLevelItem(0).child(
                0))  # FIXME: refactor
        self.ui.image_list_widget.expandAll()

        self._reload_images(Dataset.Category.TRAINING_OK)
        self.__reload_recent_training_date()

        self.capture_dialog: Optional[ImageCaptureDialog] = None

        self.preview_window = PreviewWindow()

        self.watcher = QFileSystemWatcher(self)
        self.watcher.addPaths([
            str(Dataset.images_path(Dataset.Category.TRAINING_OK)),
            str(Dataset.images_path(Dataset.Category.TEST_OK)),
            str(Dataset.images_path(Dataset.Category.TEST_NG))
        ])
        self.watcher.directoryChanged.connect(
            self.on_dataset_directory_changed)

        self.select_area_dialog = None
        self.msgBox = None

        LearningModel.default().training_finished.connect(
            self.on_finished_training)
コード例 #5
0
def from_bson_to_model(bson):
    model_id = bson['model_id']
    processed = bson['processed']
    w_vector = bson['w_vector']
    d = bson['d']
    if processed:
        score = bson['score']
    else:
        score = None
    generation = bson['generation']

    model = LearningModel(w_vector, d, model_id)
    model.generation = generation
    model.processed = processed
    model.score = score
    return model
コード例 #6
0
ファイル: dataset.py プロジェクト: akiphumi/SDTest
    def on_finished_selecting_area(self, data: TrimmingData):
        categories = [
            Dataset.Category.TRAINING_OK, Dataset.Category.TEST_OK,
            Dataset.Category.TEST_NG
        ]
        truncated_image_paths = []
        for category in categories:
            dir_path = Dataset.images_path(category)
            save_path = Dataset.trimmed_path(category)
            if os.path.exists(save_path):
                shutil.rmtree(save_path)
            os.mkdir(save_path)
            if not data.needs_trimming:
                copy_tree(str(dir_path), str(save_path))
            else:
                file_list = os.listdir(dir_path)
                file_list = [
                    img for img in file_list if Path(img).suffix in
                    ['.jpg', '.jpeg', '.png', '.gif', '.bmp']
                ]
                for file_name in file_list:
                    truncated_image_path = Dataset.trim_image(
                        os.path.join(dir_path, file_name), save_path, data)
                    if truncated_image_path:
                        file_name = os.path.basename(truncated_image_path)
                        shutil.move(
                            truncated_image_path,
                            os.path.join(
                                Dataset.images_path(
                                    Dataset.Category.TRUNCATED), file_name))
                        truncated_image_paths.append(truncated_image_path)
            Project.save_latest_trimming_data(data)

        # alert for moving truncated images
        if truncated_image_paths:
            self.msgBox = QMessageBox()
            self.msgBox.setText(str(len(truncated_image_paths))+'枚の画像を読み込めませんでした. これらの画像はtruncatedフォルダに移動されました.\n\n'\
                                + 'このままトレーニングを開始しますか?')
            self.msgBox.setStandardButtons(self.msgBox.Yes | self.msgBox.No)
            self.msgBox.setDefaultButton(self.msgBox.Yes)
            reply = self.msgBox.exec()
            if reply == self.msgBox.No:
                return

        # start training
        LearningModel.default().start_training()
コード例 #7
0
ファイル: main_window.py プロジェクト: akiphumi/SDTest
    def setup_tool_bar(self):
        self.inspection_widget_id = self.ui.main_stacked_widget.addWidget(InspectionWidget())
        self.ai_optimization_widget_id = self.ui.main_stacked_widget.addWidget(AIOptimizationWidget())
        self.past_result_widget_id = self.ui.main_stacked_widget.addWidget(PastResultWidget())
        self.ui.optimization_action.triggered.connect(self.on_clicked_optimization_button)
        self.ui.inspection_action.triggered.connect(self.on_clicked_inspection_button)
        self.ui.past_result_action.triggered.connect(self.on_clicked_past_result_button)
        self.ui.action_group = QActionGroup(self)
        self.ui.action_group.addAction(self.ui.optimization_action)
        self.ui.action_group.addAction(self.ui.inspection_action)
        self.ui.action_group.addAction(self.ui.past_result_action)
        self.ui.inspection_action.setChecked(True)
        self.ui.action_group.setExclusive(True)

        try:
            self.on_clicked_inspection_button()
            self.ui.inspection_action.setChecked(True)
            LearningModel.default().load_weights()
        except FileNotFoundError:
            self.on_clicked_optimization_button()
            self.ui.optimization_action.setChecked(True)

        appctxt = ApplicationContext()
        loader_gif_path = appctxt.get_resource('images/loader.gif')
        self.loader = QMovie(loader_gif_path)
        self.loader.start()
        self.loader_label = QLabel()
        self.loader_label.setMovie(self.loader)
        self.loader_label.hide()
        self.training_message = QLabel()

        spacer = QWidget()
        spacer.setFixedWidth(2)

        self.statusBar().addPermanentWidget(self.training_message)
        self.statusBar().addPermanentWidget(self.loader_label)
        self.statusBar().addPermanentWidget(spacer)

        self.statusBar().setSizeGripEnabled(False)
コード例 #8
0
    def __init__(self):
        QWidget.__init__(self)
        self.ui = Ui_inspection()
        self.ui.setupUi(self)
        # camera preview settings
        self.camera_model = CameraModel.default()
        self.camera_model.set_selected_camera_to_view_finder(
            self.ui.camera_preview)
        self.camera_model.image_saved.connect(self.on_image_saved)

        self.ui.camera_preview.setFixedSize(self.__VIEW_FINDER)

        self.learning_model = LearningModel.default()
        self.learning_model.predicting_finished.connect(
            self.on_finished_predicting)

        self.select_camera_widget = CameraList()
        self.select_camera_widget.clicked.connect(self.on_clicked_camera_list)
        self.select_camera_widget.closed.connect(self.on_closed_camera_list)

        self.ui.select_camera_button.clicked.connect(
            self.on_clicked_select_camera_button)
        self.ui.inspect_button.clicked.connect(self.on_clicked_inspect_button)

        self.ui.inspect_existing_image_button.clicked.connect(
            self.on_clicked_inspection_existing_image_button)

        self.ui.result.setCurrentWidget(self.ui.default_result)

        appctxt = ApplicationContext()
        loader_gif_path = appctxt.get_resource('images/loader.gif')
        self.loader_movie = QMovie(loader_gif_path)
        self.loader_movie.setScaledSize(QSize(30, 8))
        self.loader_movie.start()

        self.__ng_counter = 0
        self.__ok_counter = 0
        self.ui.OK_counter_label.setText(str(self.ok_counter))
        self.ui.NG_counter_label.setText(str(self.ng_counter))

        self.msgBox = None
コード例 #9
0
ファイル: main_window.py プロジェクト: akiphumi/SDTest
    def __init__(self):
        super().__init__()

        # Disable maximizing window
        self.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint | Qt.CustomizeWindowHint)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.inspection_widget_id = None
        self.ai_optimization_widget_id = None
        self.past_result_widget_id = None

        self.msgBox = None

        self.inspection_mainwindow_size = QSize(780, 550)
        self.optimization_mainwindow_size = QSize(864, 730)
        self.past_result_mainwindow_size = QSize(780, 600)

        self.inspection_widget_size = QSize(740, 420)
        self.optimization_widget_size = QSize(840, 600)
        self.past_result_widget_size = QSize(740, 400)

        self.latest_cpos = None
        self.move_window_position()

        self.setup_tool_bar()
        self.setup_menu_bar()

        # 一旦レポート機能なし
        self.ui.past_result_action.setEnabled(False)
        self.ui.past_result_action.setVisible(False)

        LearningModel.default().predicting_start.connect(self.on_start_predicting)
        LearningModel.default().predicting_finished.connect(self.on_finished_predicting)
        LearningModel.default().training_start.connect(self.on_start_training)
        LearningModel.default().training_finished.connect(self.on_finished_training)
コード例 #10
0
def test_model(model: LearningModel, enemies_rectangle: List[Rectangle],
               img_shape):
    if len(enemies_rectangle) == 0:
        return 0

    p_bot_left_img = (0, img_shape[0])

    enemies_rectangle.sort(key=lambda x: x.x1)
    enemy_rectangle = enemies_rectangle[0]
    p_top_left = (enemy_rectangle.x1, enemy_rectangle.y1)
    p_top_right = (enemy_rectangle.x2, enemy_rectangle.y1)
    p_bot_left = (enemy_rectangle.x1, enemy_rectangle.y2)

    total_distance = distance.euclidean((0, 0), (img_shape[0], img_shape[1]))

    x1 = distance.euclidean(p_bot_left_img, p_top_left) / total_distance
    x2 = distance.euclidean(p_bot_left_img, p_top_right) / total_distance
    x3 = (_get_angle(p_bot_left_img, p_bot_left) / 90) + 1

    x_vector = np.array([[x1, x2, x3]])
    to_return = model.apply(x_vector)
    return to_return
コード例 #11
0
ファイル: test.py プロジェクト: akiphumi/SDTest
    def __init__(self):
        self.learning_model = LearningModel.default()  # initの引数で渡したい…

        super().__init__()
        self.ui = Ui_Test()
        self.ui.setupUi(self)

        appctxt = ApplicationContext()
        loader_gif_path = appctxt.get_resource('images/loader.gif')
        self.loader = QMovie(loader_gif_path)
        self.loader.setScaledSize(QSize(30, 8))
        self.ui.loading_gif_label.setMovie(self.loader)
        self.loader.start()

        self.ui.about_threshold_button.clicked.connect(
            self.on_clicked_about_threshold_button)
        self.ui.threshold_slider.valueChanged.connect(
            self.on_threshold_changed)
        self.ui.details_button.clicked.connect(self.on_clicked_details_button)

        self.distance_figure: Figure = plt.figure(figsize=(4, 3))
        self.distance_canvas = FigureCanvas(self.distance_figure)
        self.distance_canvas.setParent(self.ui.distance_chart_widget)
        # sns.set_palette(['#3FDA68', '#E66643'])

        performance_figure = Figure(figsize=(3.5, 3.5))
        self.performance_axes: Axes = performance_figure.add_subplot(111)
        performance_figure.patch.set_alpha(0)
        self.performance_axes.set_position(
            pos=[-0.1, 0.1, 1, 1])  # FIXME: adjust position automatically
        self.performance_center_circle = plt.Circle(xy=(0, 0),
                                                    radius=0.75,
                                                    fc='#F5F5F5',
                                                    linewidth=1.25)
        self.performance_canvas = FigureCanvas(performance_figure)
        self.performance_canvas.setParent(self.ui.performance_chart_widget)

        self.test_report_widget = TestReportWidget()
コード例 #12
0
 def _on_changed_current_tab(self):
     if self.ui.tab_widget.currentIndex() == 1:  # FIXME: refactor condition
         self.ui.test_tab.show_loading()
         LearningModel.default().test_if_needed(predict_training=True)
コード例 #13
0
def create_model(w_array, d):
    w_array_mut = [i for i in w_array.ravel()]
    model = LearningModel(w_array_mut, d)
    model.processed = False
    return model
コード例 #14
0
ファイル: test_report_model.py プロジェクト: akiphumi/SDTest
 def __init__(self):
     appctxt = ApplicationContext()
     env = Environment(
         loader=FileSystemLoader(appctxt.get_resource('html/')))
     self.template = env.get_template('test_report_template.html')
     self.test_results = LearningModel.default().test_results