def get_model(args):
    """
    get models
    """
    if args.mode == "cnn":
        return get_cnn(args)
    elif args.mode == "lstm":
        return get_lstm(args)
Esempio n. 2
0
    def process(self, args, bg_color=0):
        """
        Scale image to width 1024, convert to grayscale and than slice by tiles.
        It's possible to slice image with padding and each tile will contain pixels from surrounding tiles
        """
        configure_backend(args)
        cnn = get_cnn(args)

        tile_size = cnn.tile_size
        img = cv2.imread(args.input_file, cv2.IMREAD_GRAYSCALE)
        assert img is not None, f'No file: {args.input_file}'

        h, w = img.shape

        if args.scale:
            width = args.scale
            height = int(width * h / w)
        else:
            width, height = w, h
        img = cv2.resize(img, dsize=(width, height), interpolation=cv2.INTER_AREA)

        output_img = np.zeros(img.shape)

        i = 0
        j = 0

        while tile_size * (i * 1) < (width + tile_size):
            while tile_size * (j + 1) < (height + tile_size):
                tile, orig_size = slice_tile(img, i, j, tile_size, args.padding, bg_color=bg_color)
                if not orig_size[0] or not orig_size[1]:
                    j += 1
                    continue

                # convert to CNN format
                cnn_tile = cnn.input_img_to_cnn(tile, tile_size, args.padding)

                # process output
                print('processing tile {}, {}'.format(i, j))
                # TODO: fix this, we should be able to batch processing
                out_arr = cnn.process_tile(cnn_tile)

                # convert to img format
                out_tile = cnn.cnn_output_to_img(out_arr, tile_size)

                output_img[
                    j * tile_size : (j + 1) * tile_size, i * tile_size : (i + 1) * tile_size
                ] = out_tile[: orig_size[0], : orig_size[1]]

                j += 1
            i += 1
            j = 0

        cv2.imwrite(args.output_file, output_img)
        if args.display:
            display(output_img)
Esempio n. 3
0
    def __init__(self,
                 num_actions,
                 exploration,
                 exploration_decay,
                 exploration_min,
                 reward_decay,
                 deque_size,
                 batch_size,
                 model=None):
        """
        Initializes a Deep Q network.

        :param num_actions: The number of actions that are possible in the
        environment this Deep Q network will be working with.
        :param exploration: The exploration rate.
        :param exploration_decay: The decay rate of the exploration rate.
        :param exploration_min: The minimum possible exploration rate.
        :param reward_decay: The rate at which reward decays at each time step.
        :param deque_size: The size of the deque that will hold past experiences
        for replay.
        :param batch_size: The batch size to use when training the NN.
        :param model: The NN to use to approximate the Q function.
        """

        self.exploration_min = exploration_min
        self.exploration = exploration
        self.exploration_decay = exploration_decay
        self.transitions = deque(maxlen=deque_size)
        self.actions = list(range(num_actions))
        self.num_actions = num_actions
        self.exploration = exploration
        self.reward_decay = reward_decay
        self.batch_size = batch_size

        if model is None:
            self.model = get_cnn(self.num_actions)

        return
Esempio n. 4
0
def train(args):
    configure_backend(args)

    # np.random.seed(123)  # for reproducibility
    print('Creating CNN')

    cnn = get_cnn(args)
    model_checkpoint = ModelCheckpoint(
        args.weights_file,
        monitor=args.monitor,
        verbose=1,
        save_best_only=args.best,
        period=args.period,
    )
    if args.comment:
        uniq_part = args.comment
    else:
        uniq_part = f'lr_{args.learning_rate}'
    run_name = datetime.datetime.now().strftime(f'%Y/%m/%d/%H_%M_{uniq_part}')
    callbacks = [
        model_checkpoint,
        TensorBoard(log_dir=f'tensorboard_log/{run_name}')
    ]
    if args.early_stopping:
        callbacks.append(
            EarlyStopping(monitor='val_loss', verbose=1, patience=50))
    data_source = DataSource(args, cnn)
    steps = args.epoch_steps if args.epoch_steps else data_source.ideal_steps
    print(f'Number of Steps: {steps} / {max(int(steps / args.batch_size), 1)}')
    cnn.model.fit_generator(
        data_source.data_generator(),
        validation_data=data_source.validation_generator(),
        validation_steps=max(int(steps / args.batch_size), 1),
        steps_per_epoch=steps,
        epochs=args.epochs,
        verbose=1,
        callbacks=callbacks,
    )
Esempio n. 5
0
from vis.utils import utils
from matplotlib import pyplot as plt

from train_cnn import add_common_arguments


def parse_args():
    parser = argparse.ArgumentParser()
    add_common_arguments(parser)
    return parser.parse_args()


if __name__ == '__main__':
    args = parse_args()

    cnn = get_cnn(args)
    cnn.load(args.weights_file)

    idx = -1

    model = cnn.model
    model.layers[idx].activation = activations.linear
    model = utils.apply_modifications(model)

    img = cv2.imread('tile.png', cv2.IMREAD_GRAYSCALE)
    img = img.reshape(img.shape + (1, ))
    img = img.astype('float32')
    img /= 255

    f, ax = plt.subplots(4, 4)
    for i in range(4):
                           translate=0.2,
                           shuffle=True)
test_scaled_gen = get_gen('test',
                          batch_size=batch_size,
                          scale=(1.0, 2.5),
                          translate=0.2,
                          shuffle=False)

# (X_train, y_train), (X_test, y_test) = mnist.load_data()
# print(X_train.shape)
# exit()

# ---
# Normal CNN

inputs, outputs = get_cnn()
model = Model(inputs=inputs, outputs=outputs)
model.summary()
optim = Adam(1e-3)
# optim = SGD(1e-3, momentum=0.99, nesterov=True)
loss = categorical_crossentropy
model.compile(optim, loss, metrics=['accuracy'])

# model.fit_generator(
#     train_gen, steps_per_epoch=steps_per_epoch,
#     epochs=10, verbose=1,
#     validation_data=test_gen, validation_steps=validation_steps
# )
# model.save_weights('mnist_cnn.h5')
# 1875/1875 [==============================] - 24s - loss: 0.0090 - acc: 0.9969 - val_loss: 0.0528 - val_acc: 0.9858