示例#1
0
def run(architecture_file, model_file, num_batches=10, pattern="centralModel-*"):
    print "evaluating Q values..."
    redisInstance = Redis(host='localhost', port=6379, db=0)
    model_keys = redisInstance.keys(pattern)
    results = {}

    net = BaristaNet(architecture_file, model_file, None)
    replay_dataset = ReplayDataset("temp-q-converge-dset.hdf5",
                                   net.state[0].shape,
                                   dset_size=1000,
                                   overwrite=True)
    net.add_dataset(replay_dataset)

    game = SnakeGame()
    preprocessor = generate_preprocessor(net.state.shape[2:], gray_scale)
    exp_gain = ExpGain(net, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    print "Generating new experiences..."
    for _ in xrange(100):
        exp_gain.generate_experience(1e5)
    print "Done"

    for key in model_keys:
        print "Evaluating model:", key
        model = dict(redisC.Dict(key=key, redis=redisInstance))
        q_avg = evaluate_model(net, model, num_batches)
        results[key] = q_avg

    for key in sorted(results.keys()):
        print key.ljust(25) + "%0.4f" % results[key]
示例#2
0
def main():
    args = get_args()

    caffe.set_phase_test()
    if args.mode == "cpu":
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()

    if not os.path.isfile(args.model):
        if not args.solver:
            print "Error: Model does not exist. No solver specified."
            sys.exit(1)

        print "Warning: model %s does not exist. Creating..."
        solver = SGDSolver(args.solver)
        solver.net.save(args.model)

    # Initialize objects
    net = BaristaNet(args.architecture, args.model, args.driver,
                     reset_log=True)

    replay_dataset = ReplayDataset(args.dataset, net.state[0].shape,
                                   dset_size=args.dset_size,
                                   overwrite=args.overwrite)
    net.add_dataset(replay_dataset)

    game = SnakeGame()
    preprocessor = generate_preprocessor(net.state.shape[2:], gray_scale)
    exp_gain = ExpGain(net, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    if(args.overwrite):
        for _ in xrange(min(args.initial_replay, args.dset_size)):
            exp_gain.generate_experience(0)

    # Start server loop
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serversocket.bind(('127.0.0.1', args.port))
    serversocket.listen(5)

    print
    print "*"*80
    print "* Starting BARISTA server: listening on port %d." % args.port
    print "*"*80
    # Signal Spark Executor that Barista is ready to receive connections
    issue_ready_signal(args.port)
    while True:
        (clientsocket, address) = serversocket.accept()
        if args.debug:
            handler = debug_process_connection
        else:
            handler = process_connection

        client_thread = threading.Thread(
                            target=handler,
                            args=(clientsocket, net, exp_gain))
        client_thread.run()
def main():
    args = get_args()

    # Instantiate network
    bnet = baristanet.BaristaNet(args.architecture, args.model, None)

    # load parameters from checkpoint into the model
    # params = load_saved_checkpoint(args.checkpoint)
    # netutils.set_net_params(bnet.net, params)

    # Initialize game player
    replay_dataset = ReplayDataset("temp-dset.hdf5", bnet.state[0].shape,
                                   dset_size=300*args.num_games,
                                   overwrite=True)

    game = SnakeGame()
    preprocessor = generate_preprocessor(bnet.state.shape[2:], gray_scale)
    exp_gain = ExpGain(bnet, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    if not os.path.isdir(args.output_dir):
        os.makedirs(args.output_dir)

    # Generate experiences
    frame_index = 0
    num_games_played = 0
    state = exp_gain.get_preprocessed_state()
    while num_games_played < args.num_games:
        # select action
        if random.random() < args.epsilon:
            action = random.choice(exp_gain.actions)
        else:
            idx = bnet.select_action([state])
            action = exp_gain.actions[idx]

        exp_gain.play_action(action)

        # Render frame
        frame = gray_scale(exp_gain.sequence[-1].reshape((1,)+exp_gain.sequence[-1].shape))[-1]

        big_frame = cv2.resize(frame, (0,0), fx=10, fy=10, interpolation=cv2.INTER_NEAREST) 
        cv2.imwrite(os.path.join(args.output_dir, "frame-%d.png" % frame_index), big_frame)
        frame_index += 1
        # cv2.imshow("Game", frame)
        # cv2.waitKey(33)

        # Check if Snake has died
        if exp_gain.game_over:
            print "Game Over"
            exp_gain.reset_game()
            num_games_played += 1

        # Get next state
        state = exp_gain.get_preprocessed_state()
示例#4
0
def main():
    args = get_args()

    # Instantiate network
    bnet = baristanet.BaristaNet(args.architecture, args.model, None)

    # load parameters from checkpoint into the model
    params = load_saved_checkpoint(args.checkpoint)
    netutils.set_net_params(bnet.net, params)

    # Initialize game player
    replay_dataset = ReplayDataset("temp-dset.hdf5", bnet.state[0].shape,
                                   dset_size=300*args.num_games,
                                   overwrite=True)

    game = SnakeGame()
    preprocessor = generate_preprocessor(bnet.state.shape[2:], gray_scale)
    exp_gain = ExpGain(bnet, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    if not os.path.isdir(args.output_dir):
        os.makedirs(args.output_dir)

    # Generate experiences
    frame_index = 0
    num_games_played = 0
    state = exp_gain.get_preprocessed_state()
    while num_games_played < args.num_games:
        # select action
        if random.random() < args.epsilon:
            action = random.choice(exp_gain.actions)
        else:
            idx = bnet.select_action([state])
            action = exp_gain.actions[idx]

        exp_gain.play_action(action)

        # Render frame
        frame = gray_scale(exp_gain.sequence[-1].reshape((1,)+exp_gain.sequence[-1].shape))[-1]

        big_frame = cv2.resize(frame, (0,0), fx=10, fy=10, interpolation=cv2.INTER_NEAREST) 
        cv2.imwrite(os.path.join(args.output_dir, "frame-%d.png" % frame_index), big_frame)
        frame_index += 1
        # cv2.imshow("Game", frame)
        # cv2.waitKey(33)

        # Check if Snake has died
        if exp_gain.game_over:
            print "Game Over"
            exp_gain.reset_game()
            num_games_played += 1

        # Get next state
        state = exp_gain.get_preprocessed_state()
示例#5
0
    def __init__(self, architecture_file, model_file):
        # Initialize objects
        self.net = BaristaNet(architecture_file, model_file, None)
        self.batch_size = self.net.batch_size

        game = SnakeGame()
        preprocessor = generate_preprocessor(self.net.state.shape[2:],
                                             gray_scale)
        self.engines = [ExpGain(self.net, ['w', 'a', 's', 'd'],
                                preprocessor, game.cpu_play,
                                None, game.encode_state())
                        for _ in range(self.batch_size)]
示例#6
0
def main():
    args = get_args()

    caffe.set_phase_test()
    if args.mode == "cpu":
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()

    if not os.path.isfile(args.model):
        if not args.solver:
            print "Error: Model does not exist. No solver specified."
            sys.exit(1)

        print "Warning: model %s does not exist. Creating..."
        solver = SGDSolver(args.solver)
        solver.net.save(args.model)

    # Initialize objects
    net = BaristaNet(args.architecture,
                     args.model,
                     args.driver,
                     reset_log=True)

    replay_dataset = ReplayDataset(args.dataset,
                                   net.state[0].shape,
                                   dset_size=args.dset_size,
                                   overwrite=args.overwrite)
    net.add_dataset(replay_dataset)

    game = SnakeGame()
    preprocessor = generate_preprocessor(net.state.shape[2:], gray_scale)
    exp_gain = ExpGain(net, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    if (args.overwrite):
        for _ in xrange(min(args.initial_replay, args.dset_size)):
            exp_gain.generate_experience(0)

    # Start server loop
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serversocket.bind(('127.0.0.1', args.port))
    serversocket.listen(5)

    print
    print "*" * 80
    print "* Starting BARISTA server: listening on port %d." % args.port
    print "*" * 80
    # Signal Spark Executor that Barista is ready to receive connections
    issue_ready_signal(args.port)
    while True:
        (clientsocket, address) = serversocket.accept()
        if args.debug:
            handler = debug_process_connection
        else:
            handler = process_connection

        client_thread = threading.Thread(target=handler,
                                         args=(clientsocket, net, exp_gain))
        client_thread.run()