in containers/Shoot/CNN/host.py [0:0]
def transform_fn(model, request_body, content_type, accept_type):
try:
input_object=json.loads(request_body)
board=input_object["board"]
count=input_object["session"].get("count",0)
input_object["session"]["count"]=count+1
if count>10:
board=nd.array(board)
board=nd.concat(
(board==1).expand_dims(axis=0),
(board==2).expand_dims(axis=0),dim=0
)
board=board.expand_dims(axis=0)
mask=board.clip(0,1)
mask=-(mask-1)
mask=mask.reshape((2,-1))
p=nd.softmax(model(board).reshape((-1,)))
p=p*mask[0]*mask[1]
while True:
loc=int(p.argmax(axis=0).asscalar())
y=loc//board.shape[2]
x=loc%board.shape[2]
if input_object["board"][y][x]==0:
break
else:
p[loc]=0
else:
while True:
x=random.randint(0,len(input_object["board"][0])-1)
y=random.randint(0,len(input_object["board"])-1)
if input_object["board"][y][x]==0:
break
input_object["session"]["shootType"]="CNNNet"
return bytearray(json.dumps({
"shot":{
"x":x,
"y":y
},
"session":input_object["session"]
}),'utf-8'),accept_type
except Exception as e:
print(traceback.format_exc())
print(e)