예제 #1
0
def generate(BATCH_SIZE, nice=False):
    generator = generator_model()
    generator.compile(loss="binary_crossentropy", optimizer="SGD")
    generator.load_weights("generator")
    if nice:
        discriminator = discriminator_model()
        discriminator.compile(loss="binary_entropy", optimizer="SGD")
        discriminator.load_weights("discriminator")
        noise = np.zeros((BATCH_SIZE * 20, 100))
        for i in range(BATCH_SIZE * 10):
            noise[i, :] = np.random.uniform(-1, 1, 100)
        generated_images = generator.predict(noise, verbose=1)
        d_pret = discriminator.predict(generated_images, verbose=1)
        index = np.arange(0, BATCH_SIZE * 20)
        index.resize((BATCH_SIZE * 20, 1))
        pre_with_index = list(np.append(d_pret, index, axis=1))
        pre_with_index.sort(key=lambda x: x[0], reverse=True)
        nice_images = np.zeros((BATCH_SIZE, 1) + (generated_images.shape[2:]),
                               dtype=np.float32)
        for i in range(int(BATCH_SIZE)):
            idx = int(pre_with_index[i][1])
            nice_images[i, 0, :, :] = generated_images[idx, 0, :, :]
        image = combine_images(nice_images)
    else:
        noise = np.zeros((BATCH_SIZE, 100))
        for i in range(BATCH_SIZE):
            noise[i, :] = np.random.uniform(-1, 1, 100)
        generated_images = generator.predict(noise, verbose=1)
        image = combine_images(generated_images)
    image = image * 127.5 + 127.5
    Image.fromarray(image.astype(np.uint8)).save("generated_image.png")
def validation_generator(data: pd.DataFrame, batch_size: int,
                         fixed_input_shape: tuple):
    # Create empty arrays to contain batch of features and labels#
    batch_features = np.zeros((batch_size, fixed_input_shape[0],
                               fixed_input_shape[1], fixed_input_shape[2]))
    batch_labels = np.zeros((batch_size, 2))

    # [xmin, xmax, ymin, ymax]
    batch_box = np.zeros((batch_size, 4))
    while True:
        for i in range(batch_size):
            # choose random index in features
            index = random.randint(0, len(data.index) - 1)
            x = cv2.imread(data["filename"].iloc[index])
            x = cv2.resize(x, (fixed_input_shape[1], fixed_input_shape[0]))
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x.astype(np.float64))
            batch_features[i, :, :, :] = x

            # labels: person == [1, 0], background = [0, 1]
            if data['name'].iloc[index] == 'person':
                batch_labels[i, :] = [1, 0]
            elif data['name'].iloc[index] == 'background':
                batch_labels[i, :] = [0, 1]
            else:
                raise ValueError('Unexpected value in "name" column')

            batch_box[i, 0] = data["xmin"].iloc[index]
            batch_box[i, 1] = data["xmax"].iloc[index]
            batch_box[i, 2] = data["ymin"].iloc[index]
            batch_box[i, 3] = data["ymax"].iloc[index]

        yield (batch_features, [batch_labels, batch_box])
예제 #3
0
def decoder_sequence(input_seq):
    src_to_index, tar_to_index = [], []

    index_to_src = dict((i, char) for char, i in src_to_index.items())
    index_to_tar = dict((i, char) for char, i in tar_to_index.items())

    model = load_model('./model/')
    encoder_inputs = model.input[0]  # input_1
    encoder_outputs, state_h_enc, state_c_enc = model.layers[
        2].output  # lstm_1
    encoder_states = [state_h_enc, state_c_enc]
    encoder_model = Model(encoder_inputs, encoder_states)

    states_value = encoder_model.predict(input_seq)

    decoder_inputs = model.input[1]  # input_2
    decoder_state_input_h = Input(shape=(256, ), name='input_3')
    decoder_state_input_c = Input(shape=(256, ), name='input_4')
    decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
    decoder_lstm = model.layers[3]
    decoder_outputs, state_h_dec, state_c_dec = decoder_lstm(
        decoder_inputs, initial_state=decoder_states_inputs)
    decoder_states = [state_h_dec, state_c_dec]
    decoder_dense = model.layers[4]
    decoder_outputs = decoder_dense(decoder_outputs)

    decoder_model = Model([decoder_inputs] + decoder_states_inputs,
                          [decoder_outputs] + decoder_states)

    max_tar_len = decoder_inputs.shape[1]
    tar_vocab_size = decoder_inputs.shape[2]

    target_seq = np.zeros((1, 1, tar_vocab_size))
    target_seq[0, 0, tar_to_index['\t']] = 1

    stop_condition = False
    decoded_sentence = ""
    while not stop_condition:  # stop_condition이 True가 될 때까지 루프 반복
        output_tokens, h, c = decoder_model.predict([target_seq] +
                                                    states_value)
        sampled_token_index = np.argmax(output_tokens[0, -1, :])
        sampled_char = index_to_tar[sampled_token_index]
        decoded_sentence += sampled_char

        # <sos>에 도달하거나 최대 길이를 넘으면 중단.
        if (sampled_char == '\n' or len(decoded_sentence) > max_tar_len):
            stop_condition = True

        # 길이가 1인 타겟 시퀀스를 업데이트 합니다.
        target_seq = np.zeros((1, 1, tar_vocab_size))
        target_seq[0, 0, sampled_token_index] = 1.

        # 상태를 업데이트 합니다.
        states_value = [h, c]

    return decoded_sentence
예제 #4
0
 def reset_states(self, states=None):
     if not self.stateful:
         raise AttributeError('Layer must be stateful.')
     batch_size = self.input_spec[0].shape[0]
     if not batch_size:
         raise ValueError('If a RNN is stateful, it needs to know '
                          'its batch size. Specify the batch size '
                          'of your input tensors: \n'
                          '- If using a Sequential model, '
                          'specify the batch size by passing '
                          'a `batch_input_shape` '
                          'argument to your first layer.\n'
                          '- If using the functional API, specify '
                          'the batch size by passing a '
                          '`batch_shape` argument to your Input layer.')
     # initialize state if None
     if self.states[0] is None:
         if hasattr(self.cell.state_size, '__len__'):
             self.states = [K.zeros((batch_size, dim))
                            for dim in self.cell.state_size]
         else:
             self.states = [K.zeros((batch_size, self.cell.state_size))]
     elif states is None:
         if hasattr(self.cell.state_size, '__len__'):
             for state, dim in zip(self.states, self.cell.state_size):
                 K.set_value(state, np.zeros((batch_size, dim)))
         else:
             K.set_value(self.states[0],
                         np.zeros((batch_size, self.cell.state_size)))
     else:
         states = to_list(states, allow_tuple=True)
         if len(states) != len(self.states):
             raise ValueError('Layer ' + self.name + ' expects ' +
                              str(len(self.states)) + ' states, '
                                                      'but it received ' + str(
                 len(states)) +
                              ' state values. Input received: ' +
                              str(states))
         for index, (value, state) in enumerate(zip(states, self.states)):
             if hasattr(self.cell.state_size, '__len__'):
                 dim = self.cell.state_size[index]
             else:
                 dim = self.cell.state_size
             if value.shape != (batch_size, dim):
                 raise ValueError('State ' + str(index) +
                                  ' is incompatible with layer ' +
                                  self.name + ': expected shape=' +
                                  str((batch_size, dim)) +
                                  ', found shape=' + str(value.shape))
             # TODO: consider batch calls to `set_value`.
             K.set_value(state, value)
    def batch_generator(self, batch_size):
        train = sorted(os.listdir(TRAIN_INPUT_DATA_PATH))
        train_mask = sorted(os.listdir(TRAIN_OUTPUT_DATA_PATH))

        print(train)
        print(train_mask)

        for file in train:
            x = np.zeros((batch_size, IMAGE_SIZE, IMAGE_SIZE, 3))
            y = np.zeros((batch_size, IMAGE_SIZE, IMAGE_SIZE))
            for i in range(batch_size):
              #  random_image = random.randint(0, self.x.shape[0] - 1)
                if file.endswith(IMAGE_FORMAT):
                    x[i] = tiff.imread(TRAIN_INPUT_DATA_PATH + file)
                    y[i] = tiff.imread(TRAIN_OUTPUT_DATA_PATH + file)

            new_y = y.reshape(len(y), len(y[0]), len(y[0][0]), 1)
            yield x, new_y
예제 #6
0
 def print_policy(self):
     array_1d = np.zeros(self.grid_size)
     tabs = 0
     for i in range(0, self.grid_size):
         array_1d[i] = 1
         sep = ' '
         if not ((i + 1) % self.game.positions_space[1]):
             # tabs += 1
             sep = '\n'
         print(self.model.predict(array_1d.reshape((1, -1))), end=sep)
         array_1d[i] = 0
예제 #7
0
        def getCapsulesMatrix(state):
            """ Return matrix with capsule coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            capsules = state.data.layout.capsules
            matrix = np.zeros((height, width), dtype=np.int8)

            for i in capsules:
                # Insert capsule cells vertically reversed into matrix
                matrix[-1 - i[1], i[0]] = 1

            return matrix
예제 #8
0
 def getWallMatrix(state):
     """ Return matrix with wall coordinates set to 1 """
     width, height = state.data.layout.width, state.data.layout.height
     grid = state.data.layout.walls
     matrix = np.zeros((height, width), dtype=np.int8)
     for i in range(grid.height):
         for j in range(grid.width):
             # Put cell vertically reversed in matrix
             cell = 1 if grid[j][i] else 0
             matrix[-1 - i][j] = cell
     return matrix
예제 #9
0
        def getPacmanMatrix(state):
            """ Return matrix with pacman coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            matrix = np.zeros((height, width), dtype=np.int8)

            for agentState in state.data.agentStates:
                if agentState.isPacman:
                    pos = agentState.configuration.getPosition()
                    cell = 1
                    matrix[-1 - int(pos[1])][int(pos[0])] = cell

            return matrix
    def predict(self, image):
        image_h, image_w, _ = image.shape
        image = cv2.resize(image, (416, 416))
        image = self.normalize(image)

        input_image = image[:, :, ::-1]
        input_image = np.expand_dims(input_image, 0)
        dummy_array = np.zeros((1, 1, 1, 1, self.config['model']['max_obj'], 4))

        netout = self.model.predict([input_image, dummy_array])[0]
        boxes = decode_netout(netout, self.config['model']['anchors'], self.config['model']['nb_class'])

        return boxes
예제 #11
0
def get_filters_for_layer(layer_number, layer_outputs):
    x_max = layer_outputs[layer_number].shape[0]
    y_max = layer_outputs[layer_number].shape[1]
    n = layer_outputs[layer_number].shape[2]

    L = []
    for i in range(n):
        L.append(np.zeros((x_max, y_max)))

    for i in range(n):
        for x in range(x_max):
            for y in range(y_max):
                L[i][x][y] = layer_outputs[layer_number][x][y][i]

    return L
예제 #12
0
def train(BATCH_SIZE):
    (X_train, y_train), (X_test, y_test) = mnist.load_data()
    X_train = (X_train.astype(np.float32) - 127.5) / 127.5
    X_train = X_train.reshape((X_train.shape[0], 1) + X_train.shape[1:])
    discriminator = discriminator_model()
    generator = generator_model()
    discriminator_on_generator = generator_containing_discriminator(
        generator, discriminator)
    d_optim = SGD(lr=0.0005, momentum=0.9, nesterov=True)
    g_optim = SGD(lr=0.0005, momentum=0.9, nesterov=True)
    generator.compile(loss="binary_crossentropy", optimizer=d_optim)
    discriminator_on_generator.compile(loss="binary_crossentropy",
                                       optimizer=g_optim)
    discriminator.trainable = True
    discriminator.compile(loss="binary_crossentropy", optimizer=d_optim)
    noise = np.zeros((BATCH_SIZE, 100))
    for epochs in range(100):
        print("Epoch is", epochs)
        print("Number of batches", int(X_train.shape[0] / BATCH_SIZE))
        for index in range(int(X_train.shape[0] / BATCH_SIZE)):
            for i in range(BATCH_SIZE):
                noise[i, :] = np.random.uniform(-1, 1, 100)
            image_batch = X_train[index * BATCH_SIZE:(index + 1) * BATCH_SIZE]
            image_batch = image_batch.reshape(image_batch.shape[0],
                                              image_batch.shape[2],
                                              image_batch.shape[3],
                                              image_batch.shape[1])
            generated_images = generator.predict(noise, verbose=0)
            if index % 20 == 0:
                image = combine_images(generated_images)
                image = image * 127.5 + 127.5
                Image.fromarray(image.astype(
                    np.uint8)).save(str(epochs) + "_" + str(index) + ".png")
            X = np.concatenate((image_batch, generated_images))
            y = [1] * BATCH_SIZE + [0] * BATCH_SIZE
            d_loss = discriminator.train_on_batch(X, y)
            print("batch %d d_loss : %f" % (index, d_loss))
            for i in range(BATCH_SIZE):
                noise[i, :] = np.random.uniform(-1, 1, 100)
            discriminator.trainable = False
            g_loss = discriminator_on_generator.train_on_batch(
                noise, [1] * BATCH_SIZE)
            discriminator.trainable = True
            print("batch %d g_loss : %f" % (index, g_loss))
            if index % 10 == 9:
                generator.save_weights("generator", True)
                discriminator.save_weights("discriminator", True)
예제 #13
0
def combine_images(generated_images):
    generated_images = generated_images.reshape(generated_images.shape[0],
                                                generated_images.shape[3],
                                                generated_images.shape[1],
                                                generated_images.shape[2])
    num = generated_images.shape[0]
    width = int(math.sqrt(num))
    height = int(math.ceil(float(num) / width))
    shape = generated_images.shape[2:]
    image = np.zeros((height * shape[0], width * shape[1]),
                     dtype=generated_images.dtype)
    for index, img in enumerate(generated_images):
        i = int(index / width)
        j = index % width
        image[i * shape[0]:(i + 1) * shape[0],
              j * shape[1]:(j + 1) * shape[1]] = img[0, :, :]
    return image
예제 #14
0
 def action_to_output(self, action):
     ret = np.zeros(self.action_dim)
     for idx, move in enumerate(self.possible_actions[action]):
         {
             0: lambda: ret[:self.action_dim // 2][
                 0::3],  # first half upper mussels
             1: lambda: ret[:self.action_dim // 2][
                 1::3],  # first half transverse mussels
             2: lambda: ret[:self.action_dim // 2][
                 2::3],  # first half downer mussels
             3: lambda: ret[self.action_dim // 2:][
                 0::3],  # second half upper mussels
             4: lambda: ret[self.action_dim // 2:][
                 1::3],  # second half transverse mussels
             5: lambda: ret[self.action_dim // 2:][
                 2::3],  # second half downer mussels
         }.get(idx)().fill(move)
     return ret
예제 #15
0
 def __init__(self, _, action_dim, agent_params):
     """Initialize agent assuming floating point state and action"""
     self.is_learning = agent_params.get(self.IS_LEARNING, True)
     self.end_point = [9, -1]
     self.state_dim = len(self.get_all_features2(list(np.arange(0, 82))))
     self.action_dim = action_dim
     self.action = np.zeros(action_dim)
     self.gamma = 0.9
     self.memory = deque(maxlen=20000)
     self.epsilon_start = 0.8
     if not self.is_learning:
         self.epsilon_start = 0.0
     self.epsilon = self.epsilon_start  # exploration rate
     self.epsilon_min = 0.01
     self.epsilon_decay = 0.995
     self.learning_rate = 0.001
     self.steps = 0
     self.last_state = None
     self.last_action = None
     self.possible_actions = [
         [0, 0, 0, 0, 0, 0],  # do nothing
         [1, 0, 0, 1, 0, 0],  # ∏
         [0, 0, 1, 0, 0, 1],  # ∐
         [1, 1, 1, 0.5, 0.5, 0.5],  # --
         [0.5, 0, 0, 0, 0, 1],  # Ƨ
         [0, 0, 0.5, 1, 0, 0],  # S
         [0, 0, 1, 0, 1, 0],  # ⊂_
         [1, 0, 0, 0, 1, 0],  # ∩_
     ]
     self.action_size = len(
         self.possible_actions)  # number of possible actions ZXC + IOP
     self.model = self._build_model()
     self.begin_point = [0, 0]
     self.dist_to_end = self.distance(self.begin_point[0],
                                      self.begin_point[1],
                                      self.end_point[0], self.end_point[1])
     self.max_repeat = 10
     self.repeat_count = self.max_repeat
     random.seed()
예제 #16
0
 def prepare_state(self, state):  # 2d to list
     array_1d = np.zeros(self.grid_size)
     idx = state[0] * self.game.positions_space[0] + state[1]
     array_1d[idx] = 1
     return (array_1d.reshape((1, -1)))
예제 #17
0
    def getStateMatrices(self, state):
        """ Return wall, ghosts, food, capsules matrices """
        def getWallMatrix(state):
            """ Return matrix with wall coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            grid = state.data.layout.walls
            matrix = np.zeros((height, width), dtype=np.int8)
            for i in range(grid.height):
                for j in range(grid.width):
                    # Put cell vertically reversed in matrix
                    cell = 1 if grid[j][i] else 0
                    matrix[-1 - i][j] = cell
            return matrix

        def getPacmanMatrix(state):
            """ Return matrix with pacman coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            matrix = np.zeros((height, width), dtype=np.int8)

            for agentState in state.data.agentStates:
                if agentState.isPacman:
                    pos = agentState.configuration.getPosition()
                    cell = 1
                    matrix[-1 - int(pos[1])][int(pos[0])] = cell

            return matrix

        def getGhostMatrix(state):
            """ Return matrix with ghost coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            matrix = np.zeros((height, width), dtype=np.int8)

            for agentState in state.data.agentStates:
                if not agentState.isPacman:
                    if not agentState.scaredTimer > 0:
                        pos = agentState.configuration.getPosition()
                        cell = 1
                        matrix[-1 - int(pos[1])][int(pos[0])] = cell

            return matrix

        def getScaredGhostMatrix(state):
            """ Return matrix with ghost coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            matrix = np.zeros((height, width), dtype=np.int8)

            for agentState in state.data.agentStates:
                if not agentState.isPacman:
                    if agentState.scaredTimer > 0:
                        pos = agentState.configuration.getPosition()
                        cell = 1
                        matrix[-1 - int(pos[1])][int(pos[0])] = cell

            return matrix

        def getFoodMatrix(state):
            """ Return matrix with food coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            grid = state.data.food
            matrix = np.zeros((height, width), dtype=np.int8)

            for i in range(grid.height):
                for j in range(grid.width):
                    # Put cell vertically reversed in matrix
                    cell = 1 if grid[j][i] else 0
                    matrix[-1 - i][j] = cell

            return matrix

        def getCapsulesMatrix(state):
            """ Return matrix with capsule coordinates set to 1 """
            width, height = state.data.layout.width, state.data.layout.height
            capsules = state.data.layout.capsules
            matrix = np.zeros((height, width), dtype=np.int8)

            for i in capsules:
                # Insert capsule cells vertically reversed into matrix
                matrix[-1 - i[1], i[0]] = 1

            return matrix

        # Create observation matrix as a combination of
        # wall, pacman, ghost, food and capsule matrices
        # width, height = state.data.layout.width, state.data.layout.height
        width, height = state.data.layout.width, state.data.layout.height
        observation = np.zeros((6, height, width))

        observation[0] = getWallMatrix(state)
        observation[1] = getPacmanMatrix(state)
        observation[2] = getGhostMatrix(state)
        observation[3] = getScaredGhostMatrix(state)
        observation[4] = getFoodMatrix(state)
        observation[5] = getCapsulesMatrix(state)
        #print 'observation: ', observation
        #observation = np.swapaxes(observation, 0, 2)

        #print 'observation: ', observation
        return observation
    def evaluate(self,
                 generator,
                 iou_threshold=0.3,
                 score_threshold=0.3,
                 max_detections=100,
                 save_path=None):
        """ Evaluate a given dataset using a given model.
        code originally from https://github.com/fizyr/keras-retinanet

        # Arguments
            generator       : The generator that represents the dataset to evaluate.
            model           : The model to evaluate.
            iou_threshold   : The threshold used to consider when a detection is positive or negative.
            score_threshold : The score confidence threshold to use for detections.
            max_detections  : The maximum number of detections to use per image.
            save_path       : The path to save images with visualized detections to.
        # Returns
            A dict mapping class names to mAP scores.
        """
        # gather all detections and annotations
        all_detections = [[None for i in range(generator.num_classes())] for j in range(generator.size())]
        all_annotations = [[None for i in range(generator.num_classes())] for j in range(generator.size())]

        for i in range(generator.size()):
            raw_image = generator.load_image(i)
            raw_height, raw_width, raw_channels = raw_image.shape

            # make the boxes and the labels
            pred_boxes = self.predict(raw_image)

            score = np.array([box.score for box in pred_boxes])
            pred_labels = np.array([box.label for box in pred_boxes])

            if len(pred_boxes) > 0:
                pred_boxes = np.array([[box.xmin * raw_width, box.ymin * raw_height, box.xmax * raw_width,
                                        box.ymax * raw_height, box.score] for box in pred_boxes])
            else:
                pred_boxes = np.array([[]])

                # sort the boxes and the labels according to scores
            score_sort = np.argsort(-score)
            pred_labels = pred_labels[score_sort]
            pred_boxes = pred_boxes[score_sort]

            # copy detections to all_detections
            for label in range(generator.num_classes()):
                all_detections[i][label] = pred_boxes[pred_labels == label, :]

            annotations = generator.load_annotation(i)

            # copy detections to all_annotations
            for label in range(generator.num_classes()):
                all_annotations[i][label] = annotations[annotations[:, 4] == label, :4].copy()

        # compute mAP by comparing all detections and all annotations
        average_precisions = {}

        for label in range(generator.num_classes()):
            false_positives = np.zeros((0,))
            true_positives = np.zeros((0,))
            scores = np.zeros((0,))
            num_annotations = 0.0

            for i in range(generator.size()):
                detections = all_detections[i][label]
                annotations = all_annotations[i][label]
                num_annotations += annotations.shape[0]
                detected_annotations = []

                for d in detections:
                    scores = np.append(scores, d[4])

                    if annotations.shape[0] == 0:
                        false_positives = np.append(false_positives, 1)
                        true_positives = np.append(true_positives, 0)
                        continue

                    overlaps = compute_overlap(np.expand_dims(d, axis=0), annotations)
                    assigned_annotation = np.argmax(overlaps, axis=1)
                    max_overlap = overlaps[0, assigned_annotation]

                    if max_overlap >= iou_threshold and assigned_annotation not in detected_annotations:
                        false_positives = np.append(false_positives, 0)
                        true_positives = np.append(true_positives, 1)
                        detected_annotations.append(assigned_annotation)
                    else:
                        false_positives = np.append(false_positives, 1)
                        true_positives = np.append(true_positives, 0)

            # no annotations -> AP for this class is 0 (is this correct?)
            if num_annotations == 0:
                average_precisions[label] = 0
                continue

            # sort by score
            indices = np.argsort(-scores)
            false_positives = false_positives[indices]
            true_positives = true_positives[indices]

            # compute false positives and true positives
            false_positives = np.cumsum(false_positives)
            true_positives = np.cumsum(true_positives)

            # compute recall and precision
            recall = true_positives / num_annotations
            precision = true_positives / np.maximum(true_positives + false_positives, np.finfo(np.float64).eps)

            # compute average precision
            average_precision = compute_ap(recall, precision)
            average_precisions[label] = average_precision

        return average_precisions
예제 #19
0
    def train_agent(self,
                    enforce_play=False,
                    save_every=100000,
                    total_episodes=100000):

        env = NeuralNetPokerEnv(self.nc)
        neural_npc = NeuralNetworkNPC()

        # exploration vs exploitation
        eps = 0.5
        # future reward factor
        gamma = 0.5

        all_rewards = [0, 0]

        for i in range(1, total_episodes + 1):
            s = env.reset(self.nc, rand_bank_dist=True)

            # saving the model for keras and saving q-table like results in readable format
            if save_every and (i % save_every == 0 or i == 1):
                print("Episode {} of {}".format(i, total_episodes))
                save_poker_model(self.model, all_rewards)

            # episode flags
            done = False
            post_river = False

            # iterations over single episode
            while not done:

                # getting player objects from state, either after reset of environment or next step
                player1 = s[0]
                player2 = s[1]

                # separating the player hand to hole and community cards
                # hole cards - player's unique cards
                # community cards - mutual cards for players
                hole_cards1 = player1.hand.cards[0:2]
                hole_cards2 = player2.hand.cards[0:2]
                community_cards = player1.hand.cards[2:]

                # state encoding to consumable format for neural network
                state1, _ = encode_to_vector(hole_cards1, community_cards, 0,
                                             player1.bank, self.nc)
                state2, _ = encode_to_vector(hole_cards2, community_cards, 0,
                                             player2.bank, self.nc)

                # choosing the action via epsilon exploration vs exploitation
                a1 = self.epsilon_choice_action(eps, neural_npc, player1,
                                                post_river)
                a2 = self.epsilon_choice_action(eps, neural_npc, player2,
                                                post_river)

                # checking whether the state action is to be enforced
                if enforce_play or post_river:
                    a1, a2 = 1, 1

                # moving to next in accordance to action and states
                new_s, r, done, _ = env.step([a1, a2])

                # reward for both players
                reward1 = r[0]
                reward2 = r[1]
                all_rewards[0] += reward1 + reward2
                all_rewards[1] += 2

                # predicted future reward
                next_reward1 = np.zeros((2, ))
                next_reward2 = np.zeros((2, ))
                if new_s:
                    # separating the player hand to hole and community cards
                    # hole cards - player's unique cards
                    # community cards - mutual cards for players
                    hole_cards1 = new_s[0].hand.cards[0:2]
                    hole_cards2 = new_s[1].hand.cards[0:2]
                    community_cards = player1.hand.cards[2:]
                    # encoding and predicting future reward
                    next_state1, _ = encode_to_vector(hole_cards1,
                                                      community_cards, 0,
                                                      player1.bank, self.nc)
                    next_state2, _ = encode_to_vector(hole_cards2,
                                                      community_cards, 0,
                                                      player2.bank, self.nc)
                    next_reward1[1] = self.model.predict(next_state1)[0, 1]
                    next_reward2[1] = self.model.predict(next_state2)[0, 1]

                # deep-q learning combining immediate and future reward
                target1 = reward1 + gamma * next_reward1[a1]
                target2 = reward2 + gamma * next_reward2[a2]
                # in case of future reward - only call is relevant
                target_vec1 = np.zeros((
                    1,
                    2,
                ))
                target_vec2 = np.zeros((
                    1,
                    2,
                ))

                target_vec1[0, a1] = target1
                target_vec2[0, a2] = target2

                # fitting the model to combined reward
                self.model.fit(state1, target_vec1, epochs=1, verbose=0)
                if a1:
                    # takes place only if SB called
                    self.model.fit(state2, target_vec2, epochs=1, verbose=0)

                # advancing to next step
                s = new_s
예제 #20
0
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

# let's train the model using SGD + momentum (how original).
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
model.summary()

model.load_weights('weights.hdf5')

# Let us just apply the method to a single image and see what are the results


img = plt.imread('trial_image.jpeg').transpose((2, 0, 1))
channels, rows, cols = img.shape

enlarged = np.lib.pad(img, ((0, 0), (16, 16), (16, 16)), mode='mean')
# print (enlarged.shape)
heat_map = np.zeros((rows, cols))
t1 = time.clock()
for r in np.arange(rows - 32):
    for c in np.arange(cols - 32):
        temp = np.expand_dims(enlarged[:, r:r + 32, c:c + 32], axis=0)
        heat_map[r, c] = model.predict(temp)[0]
print("Time taken for one image: ", time.clock() - t1)