2017-11-04 01:45:55 +08:00
|
|
|
import numpy as np
|
|
|
|
import os
|
2017-11-10 14:40:23 +08:00
|
|
|
import time
|
2017-11-04 01:45:55 +08:00
|
|
|
|
|
|
|
def hex2board(hex):
|
|
|
|
scale = 16
|
|
|
|
num_of_bits = 360
|
|
|
|
binary = bin(int(hex[:-2], scale))[2:].zfill(num_of_bits) + hex[-2]
|
2017-11-09 19:23:40 +08:00
|
|
|
board = np.zeros([361], dtype='int8')
|
2017-11-04 01:45:55 +08:00
|
|
|
for i in range(361):
|
|
|
|
board[i] = int(binary[i])
|
|
|
|
board = board.reshape(1,19,19,1)
|
|
|
|
return board
|
|
|
|
|
|
|
|
def str2prob(str):
|
|
|
|
p = str.split()
|
|
|
|
prob = np.zeros([362])
|
|
|
|
for i in range(362):
|
|
|
|
prob[i] = float(p[i])
|
|
|
|
prob = prob.reshape(1,362)
|
2017-11-09 19:23:40 +08:00
|
|
|
if np.sum(np.isnan(prob))==0:
|
|
|
|
return prob, True
|
|
|
|
else:
|
2017-11-10 14:40:23 +08:00
|
|
|
return 0, False
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-11-09 19:23:40 +08:00
|
|
|
dir = "/home/yama/leela-zero/data/sgf-txt-files/"
|
2017-11-04 01:45:55 +08:00
|
|
|
name = os.listdir(dir)
|
|
|
|
text = []
|
|
|
|
for n in name:
|
|
|
|
if n[-4:]==".txt":
|
|
|
|
text.append(n)
|
|
|
|
print(text)
|
2017-11-10 14:40:23 +08:00
|
|
|
total_start = -time.time()
|
2017-11-04 01:45:55 +08:00
|
|
|
for t in text:
|
2017-11-10 14:40:23 +08:00
|
|
|
start = -time.time()
|
2017-11-04 01:45:55 +08:00
|
|
|
num = 0
|
2017-11-10 14:40:23 +08:00
|
|
|
boards = []
|
|
|
|
board = []
|
|
|
|
win = []
|
|
|
|
p = []
|
2017-11-09 19:23:40 +08:00
|
|
|
flag = False
|
2017-11-04 01:45:55 +08:00
|
|
|
for line in open(dir + t):
|
2017-11-10 14:40:23 +08:00
|
|
|
if num % 19 == 0:
|
|
|
|
flag = False
|
2017-11-04 01:45:55 +08:00
|
|
|
if num % 19 < 16:
|
|
|
|
new_board = hex2board(line)
|
2017-11-10 14:40:23 +08:00
|
|
|
board.append(new_board)
|
2017-11-04 01:45:55 +08:00
|
|
|
if num % 19 == 16:
|
2017-11-09 22:40:44 +08:00
|
|
|
if int(line) == 0:
|
2017-11-09 19:23:40 +08:00
|
|
|
new_board = np.ones([1, 19 ,19 ,1], dtype='int8')
|
2017-11-09 22:40:44 +08:00
|
|
|
if int(line) == 1:
|
2017-11-09 19:23:40 +08:00
|
|
|
new_board = np.zeros([1, 19, 19, 1], dtype='int8')
|
2017-11-10 14:40:23 +08:00
|
|
|
board.append(new_board)
|
|
|
|
board = np.concatenate(board, axis=3)
|
|
|
|
boards.append(board)
|
|
|
|
board = []
|
2017-11-04 01:45:55 +08:00
|
|
|
if num % 19 == 17:
|
2017-11-10 14:40:23 +08:00
|
|
|
if str2prob(line)[1] == True:
|
|
|
|
p.append(str2prob(line)[0])
|
|
|
|
else:
|
|
|
|
flag = True
|
|
|
|
boards = boards[:-1]
|
2017-11-04 01:45:55 +08:00
|
|
|
if num % 19 == 18:
|
2017-11-10 14:40:23 +08:00
|
|
|
if flag == False:
|
|
|
|
win.append(np.array(int(line), dtype='int8').reshape(1,1))
|
2017-11-04 01:45:55 +08:00
|
|
|
num=num+1
|
2017-11-10 14:40:23 +08:00
|
|
|
boards = np.concatenate(boards, axis=0)
|
|
|
|
win = np.concatenate(win, axis=0)
|
|
|
|
p = np.concatenate(p, axis=0)
|
2017-11-09 19:23:40 +08:00
|
|
|
print("Boards Shape: {}, Wins Shape: {}, Probs Shape : {}".format(boards.shape[0], win.shape[0], p.shape[0]))
|
2017-11-10 14:40:23 +08:00
|
|
|
print("Finished {} Time {}".format(t, time.time()+start))
|
2017-11-09 19:23:40 +08:00
|
|
|
np.savez("/home/tongzheng/meta-data/"+t[:-4], boards=boards, win=win, p=p)
|
2017-11-10 14:40:23 +08:00
|
|
|
del boards, board, win, p
|
|
|
|
print("All finished! Time {}".format(time.time()+total_start))
|