Beispiel #1
0
def segnet_predict():
    import cv2
    from src.timer import Timer

    rotation = False
    # set True for running segmentation test
    segmentation_test = True

    jaccard_sum_previous = 0
    jaccard_sum_proposed = 0

    time_previous = [0, 0, 0, 0]
    time_proposed = 0

    if segmentation_test:
        # 指領域抽出実験用
        # for segmentation test
        X_test, file_names = load_x(
            'datasets' + os.sep + 'segmentation_test' + os.sep + 'image',
            rotation)
    else:
        # normal predict
        X_test, file_names = load_x(
            'datasets' + os.sep + 'test' + os.sep + 'image', rotation)

    input_channel_count = 1
    output_channel_count = 1
    first_layer_filter_count = FIRST_LAYER_FILTER_COUNT
    model = segnet(input_channel_count, output_channel_count,
                   first_layer_filter_count)
    model.load_weights('segnet_weights.hdf5')
    model.summary()
    BATCH_SIZE = 8
    timer = Timer()
    Y_pred = model.predict(X_test, BATCH_SIZE)
    #time_proposed += timer.time_elapsed()

    for i, y in enumerate(Y_pred):
        # testDataフォルダ配下にleft_imagesフォルダを置いている
        img = cv2.imread(
            'datasets' + os.sep + 'test' + os.sep + 'image' + os.sep +
            file_names[i], 0)
        #cv2.equalizeHist(img ,img)

        if rotation:
            y = cv2.resize(y, (img.shape[0], img.shape[0]))
        else:
            y = cv2.resize(y, (img.shape[1], img.shape[0]))

        y_dn = denormalize_y(y)
        y_dn = np.uint8(y_dn)
        #ret, mask = cv2.threshold(y_dn, 0, 255, cv2.THRESH_OTSU)
        ret, mask = cv2.threshold(y_dn, 127, 255, cv2.THRESH_BINARY)

        #hist, bins = np.histogram(mask.ravel(), 256, [0, 256])
        mask_binary = normalize_y(mask)

        timer.reset()
        masked = cv2.bitwise_and(img, mask)
        mask_rest = cv2.bitwise_not(mask)
        masked = cv2.bitwise_or(masked, mask_rest)
        #image_user_processed = high_boost_filter(masked)
        cv2.imwrite('prediction' + os.sep + file_names[i], mask)
        time_proposed += timer.time_elapsed()

        if segmentation_test:
            img = cv2.imread(
                'datasets' + os.sep + 'segmentation_test' + os.sep + 'image' +
                os.sep + file_names[i], 0)
            mask_previous, _, time_list_previous = opening_masking(img)
            time_previous[0] += time_list_previous[0]
            time_previous[1] += time_list_previous[1] - time_list_previous[0]
            time_previous[2] += time_list_previous[2] - time_list_previous[1]
            time_previous[3] += time_list_previous[3] - time_list_previous[2]

            mask_previous_binary = normalize_y(mask_previous)
            print(mask_previous_binary.shape)
            label = cv2.imread(
                'datasets' + os.sep + 'segmentation_test' + os.sep + 'label' +
                os.sep + file_names[i], 0)
            label_binary = normalize_y(label)

            jaccard_previous = K.get_value(
                jaccard_coefficient(mask_previous_binary, label_binary))
            jaccard_proposed = K.get_value(
                jaccard_coefficient(mask_binary, label_binary))

            print('previous:', jaccard_previous)
            jaccard_sum_previous += jaccard_previous
            print('proposed:', jaccard_proposed)
            jaccard_sum_proposed += jaccard_proposed

    print('sum_previous:', jaccard_sum_previous)
    print('sum_proposed:', jaccard_sum_proposed)
    print('mean_previous:', jaccard_sum_previous / len(Y_pred))
    print('mean_proposed:', jaccard_sum_proposed / len(Y_pred))
    print('mean_time_previous:',
          list(map(lambda x: x / len(Y_pred), time_previous)))
    print('mean_time_proposed', time_proposed / len(Y_pred))

    return 0
Beispiel #2
0
def cross_validation_segnet():
    from sklearn.model_selection import KFold
    from sklearn.model_selection import train_test_split
    from src.plot import plot_loss_accuracy, plot_dice_coefficient_cv
    from src.timer import Timer

    segmentation_test = True
    rotation = False
    jaccards_previous = []
    jaccards_proposed = []

    # training_validation dataset path
    training_validation_path = 'datasets' + os.sep + 'training_validation'

    # 学習・検証用データセットのロード
    # load dataset for training, validation
    x_all, file_names = load_x(training_validation_path + os.sep + 'image')
    y_all = load_y(training_validation_path + os.sep + 'label')
    # testデータの分割だがこれは今回あらかじめ分けておくので無視する
    #x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=7)
    # 指領域抽出実験用
    X_test, file_names = load_x(
        'datasets' + os.sep + 'segmentation_test' + os.sep + 'image', rotation)

    # 入力はグレースケール1チャンネル
    input_channel_count = 1
    # 出力はグレースケール1チャンネル
    output_channel_count = 1
    # hyper parameters
    BATCH_SIZE = 8
    NUM_EPOCH = 300

    # dice係数の最終値を記憶するlist
    # lists for get last dice coefficient values
    final_dices = []
    final_val_dices = []
    # dice係数のlistを記憶するlist
    # list for get dice coefficient
    dice_lists = []

    training_times = []

    label_names = ['1st', '2nd', '3rd', '4th']

    kf = KFold(n_splits=4, shuffle=True)
    timer = Timer()
    # kFoldループを行う(36データを4つに分割)
    # kFold loop (split 36 data to 4 data)
    for train_index, val_index in kf.split(x_all, y_all):
        print(train_index, val_index)
        x_train = x_all[train_index]
        y_train = y_all[train_index]
        x_validation = x_all[val_index]
        y_validation = y_all[val_index]

        print(x_train.shape, x_validation.shape)

        # SegNetの定義
        model = segnet(input_channel_count, output_channel_count,
                       FIRST_LAYER_FILTER_COUNT)
        model.compile(loss=dice_coefficient_loss,
                      optimizer=Adam(lr=1e-3),
                      metrics=[dice_coefficient, 'accuracy'])
        timer.reset()
        history = model.fit(x_train,
                            y_train,
                            batch_size=BATCH_SIZE,
                            epochs=NUM_EPOCH,
                            verbose=1,
                            validation_data=(x_validation, y_validation))
        training_times.append(timer.time_elapsed())

        # dice_coefficientの最終値を記録
        # get last dice coefficient value
        final_dices.append(history.history['dice_coefficient'][-1])
        final_val_dices.append(history.history['val_dice_coefficient'][-1])
        dice_lists.append(history.history['dice_coefficient'])
        plot_loss_accuracy(history)
        #Y_pred = model.predict(X_test, BATCH_SIZE)

        if segmentation_test:
            Y_pred = model.predict(X_test, BATCH_SIZE)

            for i, y in enumerate(Y_pred):
                # testDataフォルダ配下にleft_imagesフォルダを置いている
                img = cv2.imread(
                    'datasets' + os.sep + 'segmentation_test' + os.sep +
                    'image' + os.sep + file_names[i], 0)
                if rotation:
                    y = cv2.resize(y, (img.shape[0], img.shape[0]))
                else:
                    y = cv2.resize(y, (img.shape[1], img.shape[0]))

                # 提案手法
                # proposed method
                y_dn = denormalize_y(y)
                y_dn = np.uint8(y_dn)
                # ret, mask = cv2.threshold(y_dn, 0, 255, cv2.THRESH_OTSU)
                ret, mask = cv2.threshold(y_dn, 127, 255, cv2.THRESH_BINARY)
                # hist, bins = np.histogram(mask.ravel(), 256, [0, 256])
                mask_binary = normalize_y(mask)
                # 従来手法
                # previous method
                mask_previous, _ = opening_masking(img)
                mask_previous_binary = normalize_y(mask_previous)
                # ラベル読み込み
                # load label images
                label = cv2.imread(
                    'datasets' + os.sep + 'segmentation_test' + os.sep +
                    'label' + os.sep + file_names[i], 0)
                label_binary = normalize_y(label)

                dice_previous = K.get_value(
                    jaccard_coefficient(mask_previous_binary, label_binary))
                dice_proposed = K.get_value(
                    jaccard_coefficient(mask_binary, label_binary))

                print('previous:', dice_previous)
                jaccards_previous.append(dice_previous)
                print('proposed:', dice_proposed)
                jaccards_proposed.append(dice_proposed)

    print(final_dices)
    print('平均訓練精度', np.mean(final_dices))  # mean training precision
    print(final_val_dices)
    print('平均検証精度', np.mean(final_val_dices))  # mean validation precision
    print(training_times)
    print('平均学習時間', np.mean(training_times))  # mean training time
    print(jaccards_previous)
    print('従来手法精度',
          np.mean(jaccards_previous))  # extract-precision by previous method
    print(jaccards_proposed)
    print('提案手法精度',
          np.mean(jaccards_proposed))  # extract-precision by proposed method

    plot_dice_coefficient_cv(dice_lists, label_names)

    return 0
class Enviroment:

    # Variables de pygame
    screen: pygame.Surface
    screen_width: int
    screen_height: int

    # Variables del entorno
    done: bool
    winner: bool
    the_winner: int
    play_number: int

    # Pantallas cuando ganan
    player1_win: pygame.image
    player2_win: pygame.image
    empate: pygame.image
    player_win: pygame.image

    # Jugadores
    player1: Player
    player2: Player

    # Turno del jugador
    player1_turn: bool

    # Constantes
    play_count: int
    PLAY_LIMIT = 100

    # Tablero de ajedrez
    chessboard: Chessboard

    # Booleana de configuracion para identificar si se grafica el tablero o no
    graph_mode: bool

    def __init__(self,
                 screen_width: int,
                 screen_height: int,
                 player1_command: str,
                 player2_command: str,
                 graph_mode=True):
        self.graph_mode = graph_mode

        # Inicializando pygame
        pygame.init()

        if self.graph_mode:
            self.screen = pygame.display.set_mode(
                (screen_width, screen_height))
            pygame.display.set_caption("Ajedrez")

        self.screen_height = screen_height
        self.screen_width = screen_width
        s_size = (screen_width, screen_height)

        # Limite de jugadas
        self.play_count = 0

        # Obtienendo directorio para la carga de recursos
        dirname = os.path.dirname(__file__)

        # Creando tablero
        path = os.path.join(dirname, "assets/Tablero.jpg")
        skin = pygame.image.load(path)
        self.chessboard = Chessboard(skin, screen_width, screen_height)

        # Cargando imagenes de victoria
        path = os.path.join(dirname, "assets/Player 1 win.png")
        self.player1_win = pygame.image.load(path)
        self.player1_win = pygame.transform.scale(self.player1_win, s_size)
        path = os.path.join(dirname, "assets/Player 2 win.png")
        self.player2_win = pygame.image.load(path)
        self.player2_win = pygame.transform.scale(self.player2_win, s_size)
        path = os.path.join(dirname, "assets/Empate.png")
        self.empate = pygame.image.load(path)
        self.empate = pygame.transform.scale(self.empate, s_size)

        # Creando jugadores
        self.player1 = Player(Player.PLAYER_ONE, Controller(player1_command))
        self.player2 = Player(Player.PLAYER_TWO, Controller(player2_command))
        self._add_knights()
        self.player1_turn = True

        self.done = False
        self.winner = False
        self.play_number = 0

        self.timer = Timer()

    def _add_knights(self):
        """
        Agrega los caballos iniciales a los jugadores y al tablero
        """
        # Altura y anchura de los caballos
        width = self.screen_width / 8
        height = self.screen_height / 8

        # Obteniendo Path de los caballos
        dirname = os.path.dirname(__file__)
        caballo_blanco = os.path.join(dirname, "assets/Caballo blanco.png")
        caballo_negro = os.path.join(dirname, "assets/Caballo negro.png")

        for player_number in [Player.PLAYER_ONE, Player.PLAYER_TWO]:
            # Obteniendo player
            player = self.player1 if player_number == Player.PLAYER_ONE \
                else self.player2

            # Cargando imagen del caballo
            file_path = caballo_blanco if player_number == Player.PLAYER_ONE \
                else caballo_negro
            skin = pygame.image.load(file_path)

            # Creando caballos del jugador
            for nc in range(16):
                id_caballo = player_number * 100 + nc

                # Calculando posicion del caballo
                x = nc % 8
                desp_y = 0 if player_number == Player.PLAYER_ONE else 6
                y = int(nc / 8) + desp_y

                # Creando caballo
                knight = Knight(x, y, skin, width, height, id_caballo)

                # Agregando caballo
                player.add_knight(knight)
                self.chessboard.add_knight(knight)

    def launch_turns(self):
        play_limit = self.PLAY_LIMIT * 2

        while not self.winner and not self.done and self.play_count < play_limit:
            self.play_count += 1

            # Obteniendo jugador en turno
            self.player1_turn = not self.player1_turn
            player = self.player1 if self.player1_turn else self.player2
            other_player = self.player1 if not self.player1_turn \
                else self.player2

            # Obteniendo accion del jugador
            state = self.chessboard.get_state(player, other_player)

            self.timer.reset()
            good_movement = False
            time_out = False
            while not good_movement:
                try:
                    action = player.get_action(state,
                                               self.timer.current_time())
                    # print("Action: ")
                    # print(action.knight_id)
                    # print(action.knight_movement)

                    # Aplicando accion
                    self.chessboard.move(player, action.knight_id,
                                         action.knight_movement)
                    good_movement = True
                except NameError as e:
                    print(f"Player {player.player_number} error: {e}")

                    # Si el controlador se demora mas de 5 segundos queda
                    # eliminado
                    if e.__str__() == "TimeOut":
                        time_out = True
                        break

                except Exception as e:
                    print("Error raro")
                    print(e)

            # Registrando eliminacion por time out
            if time_out or not good_movement:
                self.winner = True
                self.the_winner = other_player.player_number

                # Actualizando jugador ganador
                self.player_win = self.player1_win if self.the_winner == \
                    Player.PLAYER_ONE else self.player2_win

                print(f"Player {player.player_number} pierde por time out")

            # Registrando ganador
            if player.point == 16:
                self.winner = True
                self.the_winner = player.player_number

                # Actualizando jugador ganador
                self.player_win = self.player1_win if self.the_winner == \
                    Player.PLAYER_ONE else self.player2_win

                print(f"Player {player.player_number} win")
            # else:
            #     time.sleep(0.1)

        # Obteniendo ganador por limite de jugadas
        if self.play_count == play_limit:
            self.winner = True

            if self.player1.point > self.player2.point:
                self.the_winner = Player.PLAYER_ONE
                self.player_win = self.player1_win

            elif self.player1.point < self.player2.point:
                self.the_winner = Player.PLAYER_TWO
                self.player_win = self.player2_win

            else:
                self.the_winner = Player.PLAYER_PAR
                self.player_win = self.empate

    def run(self):
        """
        Comprende el ciclo del juego. Este finaliza cuando alguno de los
        jugadores gana.
        """
        t = threading.Thread(target=self.launch_turns)
        t.start()

        while not self.done and self.graph_mode:
            # Actualizando eventos
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.done = True
                    continue

            player = self.player1 if self.player1_turn else self.player2
            other_player = self.player1 if not self.player1_turn \
                else self.player2

            # Limpiando ventana
            self.screen.fill((33, 33, 33))

            # Dibujando tablero y jugadores
            self.chessboard.draw(self.screen)
            other_player.draw(self.screen)
            player.draw(self.screen)

            # Dibujando timer
            self.timer.draw(self.screen)

            # Dibujando ganador de la partida
            if self.winner:
                self.screen.blit(self.player_win, (0, 0))

            # Actualizando tablero
            pygame.display.update()

        print("Esperando hilos")
        t.join()
        print("Juego finalizado")
        print("Puntaje del jugador 1: ", self.player1.point)
        print("Puntaje del jugador 2: ", self.player2.point)