-
-
Save cfalas/5b289f9219584edff0e6f5721c3960af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from skimage.io import imread, imsave | |
import neuralmate | |
import numpy as np | |
image = imread('boards/board3.png') | |
image = neuralmate.preprocess_image(image) | |
model = tf.keras.models.load_model('model/') | |
model(image) | |
orfen = "r4rk1/ppp2ppp/8/8/8/1P6/PQ3PPP/R4RK1" | |
fens = [] | |
PIECE_MAX = 11 | |
PIECE_MIN = 0 | |
# choose cell | |
# image = np.expand_dims(image[27], 0) | |
print(image.shape) | |
image_tensor = tf.convert_to_tensor(image.astype('float64')) | |
image_tensor = tf.cast(image_tensor, dtype=tf.float64) | |
cnt = 0 | |
while neuralmate.difference(image, image_tensor.numpy()) < 0.06: | |
with tf.GradientTape(persistent=True) as tape: | |
tape.watch(image_tensor) | |
new_labels = model(image_tensor) | |
diff = neuralmate.difference(image, image_tensor.numpy()) | |
print(cnt, 'diff', diff) | |
labels_min = tf.slice(new_labels, [0, PIECE_MIN], [-1, 1]) | |
labels_max = tf.slice(new_labels, [0, PIECE_MAX], [-1, 1]) | |
grads_min = tape.gradient(labels_min, image_tensor) | |
grads_max = tape.gradient(labels_max, image_tensor) | |
mult = 5 | |
# image_tensor = image_tensor + 0.0001 * np.sign(grads_max) | |
image_tensor = image_tensor + 0.001 * np.sign(grads_max) | |
# image_tensor = image_tensor - 0.0001 * np.sign(grads_min) | |
image_tensor = tf.clip_by_value(image_tensor, image-0.058, image+0.058) | |
image_tensor = tf.clip_by_value(image_tensor, 0, 1) | |
# print('certainty min', labels_min.numpy().mean(), labels_min.numpy().max()) | |
print('certainty max', labels_max.numpy().mean(), labels_max.numpy().max()) | |
# print(new_labels.numpy()[:, 3]) | |
# piece = new_labels.numpy().argmax() | |
# print(piece) | |
new_labels = new_labels.numpy().argmax(axis=1).reshape(8, 8) | |
new_fen = neuralmate.labels2fen(new_labels) | |
print('fen', new_fen) | |
print(' ') | |
cnt += 1 | |
if cnt % 100 == 0: | |
full_image = neuralmate.reconstruct_from_blocks(image_tensor) | |
imsave('saved/board3_full_queen.png', full_image) | |
if new_fen not in fens: | |
fens.append(new_fen) | |
print(fens) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment