Skip to content

Commit

Permalink
Server to serve the network results
Browse files Browse the repository at this point in the history
  • Loading branch information
tasuki committed Aug 3, 2018
1 parent 26e5de6 commit 2c45d8b
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 14 deletions.
Empty file added __init__.py
Empty file.
2 changes: 1 addition & 1 deletion munch/convert_records.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mutable import Converter
from .mutable import Converter

import numpy as np
import h5py
Expand Down
54 changes: 42 additions & 12 deletions munch/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import textwrap
from pprint import pprint

import numpy as np

columns = list('abcdefghi')

def square(dim):
Expand Down Expand Up @@ -73,7 +75,7 @@ def rotate(square, should_rotate = True):
lst = [row[::-1] for row in square]
return lst[::-1]

def convert_one(moves, expected):
def convert_in(moves, should_rotate):
horizontal_walls = out_walls(moves, 'h')
vertical_walls = out_walls(moves, 'v')

Expand All @@ -89,12 +91,6 @@ def convert_one(moves, expected):
x_walls_left = 10 - len(get_walls(x_moves))
o_walls_left = 10 - len(get_walls(o_moves))

expected_horizontal, expected_vertical, expected_pawn = out_expected(expected)

should_rotate = False
if len(moves) % 2 == 1:
should_rotate = True

if should_rotate:
onturn_pawn = rotate(o_pawn)
onturn_walls = o_walls_left
Expand All @@ -106,18 +102,52 @@ def convert_one(moves, expected):
other_pawn = o_pawn
other_walls = o_walls_left

return "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n-\n\n%s\n\n%s\n\n%s\n" % (
out_square(rotate(horizontal_walls, should_rotate)),
out_square(rotate(vertical_walls, should_rotate)),
out_square(onturn_pawn),
return (
rotate(horizontal_walls, should_rotate),
rotate(vertical_walls, should_rotate),
onturn_pawn,
onturn_walls,
out_square(other_pawn),
other_pawn,
other_walls,
)

def convert_one(moves, expected):
should_rotate = len(moves) % 2 == 1
hw, vw, tp, tw, op, ow = convert_in(moves, should_rotate)

expected_horizontal, expected_vertical, expected_pawn = out_expected(expected)

return "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n-\n\n%s\n\n%s\n\n%s\n" % (
out_square(hw),
out_square(vw),
out_square(tp),
tw,
out_square(op),
ow,
out_square(rotate(expected_horizontal, should_rotate)),
out_square(rotate(expected_vertical, should_rotate)),
out_square(rotate(expected_pawn, should_rotate)),
)

def pad_walls(walls):
return np.pad(np.array(walls), (0, 1), 'constant')

def pad_wallcount(wallcount):
return np.full((9, 9), wallcount)

def convert_record(moves):
should_rotate = len(moves) % 2 == 1
hw, vw, tp, tw, op, ow = convert_in(moves, should_rotate)

return np.array([
pad_walls(hw),
pad_walls(vw),
tp,
pad_wallcount(tw),
op,
pad_wallcount(ow),
])

def convert(record):
split = record.split(";")

Expand Down
2 changes: 1 addition & 1 deletion munch/mutable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from converter import *
from .converter import *
from collections import namedtuple

import numpy as np
Expand Down
3 changes: 3 additions & 0 deletions run_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from server.serve import run

run(port=8008)
21 changes: 21 additions & 0 deletions server/predictor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import h5py
import numpy as np

from keras.models import load_model
from keras_resnet import custom_objects

from .results import order, rotate
from munch.converter import convert_record

network = 'nn/01_first_nn.h5'
model = load_model(network)

def predict(game_record):
moves = game_record.split(';')
nparr = np.array([convert_record(moves)])
predictions = order(model.predict(nparr)[0].tolist())

if len(moves) % 2 == 1:
predictions = [rotate(p) for p in predictions]

return predictions
63 changes: 63 additions & 0 deletions server/results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
hp = list("abcdefghi")
vp = list("987654321")

hw = list("abcdefghx")
vw = list("87654321x")


available_moves = []
for v in vw:
for h in hw:
available_moves.append(h + v + "h")

for v in vw:
for h in hw:
available_moves.append(h + v + "v")

for v in vp:
for h in hp:
available_moves.append(h + v)


p_rotations = {}
for i, val in enumerate(hp):
p_rotations[val] = hp[8 - i]
for i, val in enumerate(vp):
p_rotations[val] = vp[8 - i]

w_rotations = {}
for i, val in enumerate(hw[:8]):
w_rotations[val] = hw[7 - i]
for i, val in enumerate(vw[:8]):
w_rotations[val] = vw[7 - i]


def convert(pred):
prob, move = pred
return (move, int(prob * 1000))

def nonzero(pred):
prob, move = pred
return prob != 0

def order(predictions):
preds = sorted(list(zip(predictions, available_moves)), reverse=True)
filtered = filter(nonzero, map(convert, preds))

return list(filtered)[:20]


def rotate(pred):
move, prob = pred

col = move[0]
row = move[1]

if len(move) == 3:
newmove = w_rotations[col] + w_rotations[row] + move[2]
elif len(move) == 2:
newmove = p_rotations[col] + p_rotations[row]
else:
raise "oops"

return (newmove, prob)
25 changes: 25 additions & 0 deletions server/serve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .predictor import predict

from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
import json

class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()

def do_GET(self):
self._set_headers()
url = urlparse(self.path)
game = url.query.replace("game=", "")

encoded = json.dumps(predict(game))
self.wfile.write((encoded + "\n").encode('utf-8'))

def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting httpd...')
httpd.serve_forever()
13 changes: 13 additions & 0 deletions server/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#curl "http://localhost:8008?game="

#curl "http://localhost:8008?game=d1;d9;d2;d8;d3;d7;a1h;d6;c1h;d5;d4v;d8v;d2v;f5h;c5h;h5h;d4;e4v;b6v;a8h;c8v;c5;c4;b5;c5;b4v"
#curl "http://localhost:8008?game=d1;d9;d2;d8;d3;d7;a1h;d6;c1h;d5;d4v;d8v;d2v;f5h;c5h;h5h;d4;e4v;b6v;a8h;c8v;c5;c4;b5;c5"

curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4"
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h"
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h;e1v"
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h;e1v;e7"
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h;e1v;e7;a4"

# clash in the middle
# curl "http://localhost:8008?game=e2;e8;e3;e7;e4;e6;e5"

0 comments on commit 2c45d8b

Please sign in to comment.