def print_for_model_digit(model, tag): sess = tf.Session() new_saver = tf.train.import_meta_graph(model + ".meta") new_saver.restore(sess, model) graph = tf.get_default_graph() input_rec = graph.get_tensor_by_name('Reconstructor/input:0') output_rec = graph.get_tensor_by_name('Reconstructor/output:0') input_enc = graph.get_tensor_by_name('Encoder/input:0') output_enc = graph.get_tensor_by_name('Encoder/output:0') # r_cost = tf.losses.mean_squared_error(real_data_rot, output_enc) for digit in range(10): data = X_test_digits[digit] z = sess.run([output_enc], feed_dict={input_enc: data}) z = z[0] outf = open("score_%s_%i.txt" % (tag, digit), 'w') for a in np.arange(-1, 1, 0.01): angles = np.ones((len(data), 1)) * a Xb_rot = rotatepair_batch.rotate_batch(data, angles[:, 0] * 180) aco = np.cos(angles * math.pi) asi = np.sin(angles * math.pi) za = np.concatenate((z, aco, asi), axis=1) samples = sess.run([output_rec], feed_dict={input_rec: za}) score = np.mean(np.square(Xb_rot - samples)) outf.write("%g %g\n" % (a, score)) print(tag, digit, a, score) sess.close()
def get_batch(X, Y): a = np.random.uniform(-1, 1, size=(BATCH_SIZE, )) Xb, Yb = get_batch_only_Xb(X, Y) sel_idx = (Yb == 4) | (Yb == 9) a[sel_idx] /= 4 Xb_rot = rotatepair_batch.rotate_batch(Xb, a * 180) return a, Xb, Xb_rot
def plot_for_model(model, tag): sess = tf.Session() new_saver = tf.train.import_meta_graph(model + ".meta") new_saver.restore(sess, model) graph = tf.get_default_graph() input_img = graph.get_tensor_by_name('input_img:0') output_img = graph.get_tensor_by_name('output_img:0') for digit in range(10): Xb = get_sample_test_digit(digit, BATCH_SIZE) Xb = np.array(Xb) csamples = [] creal = [] for a in np.arange(-1, 1, 0.1): angles = np.ones(BATCH_SIZE) * a Xb_rot = rotatepair_batch.rotate_batch(Xb, angles) samples = sess.run(output_img, feed_dict={input_img: Xb_rot}) csamples.append(np.squeeze(samples)) creal.append(Xb_rot) csamples = np.squeeze(csamples) creal = np.squeeze(creal) sh = list(csamples.shape) sh[1] *= 2 cmerge = np.zeros(sh) cmerge[:, 0::2] = creal cmerge[:, 1::2] = csamples save_plot_matrix(cmerge, 'rec_%s_%i.png' % (tag, digit)) sess.close()
def get_batch(X, Y): a = np.random.uniform(-1, 1, size=(BATCH_SIZE, )) #uncomment to learn the model on discrete angles to see it's interpolation abilities #a = np.random.randint(-5, 5, size=(BATCH_SIZE,)) #a = np.divide(a, 5) Xb, Yb = get_batch_only_Xb(X, Y) sel_idx = (Yb == 4) | (Yb == 9) a[sel_idx] /= 4 Xb_rot = rotatepair_batch.rotate_batch(Xb, a * 180) return a, Xb, Xb_rot
def plot_for_model(model, tag): sess = tf.Session() new_saver = tf.train.import_meta_graph(model + ".meta") new_saver.restore(sess, model) graph = tf.get_default_graph() input_rec = graph.get_tensor_by_name('Reconstructor/input:0') output_rec = graph.get_tensor_by_name('Reconstructor/output:0') input_enc = graph.get_tensor_by_name('Encoder/input:0') output_enc = graph.get_tensor_by_name('Encoder/output:0') for digit in range(10): data = get_sample_test_digit(digit, BATCH_SIZE) data = np.array(data) z = sess.run([output_enc], feed_dict={input_enc: data}) z = z[0] csamples = [] creal = [] for a in np.arange(-1, 1, 0.1): angles = np.ones((BATCH_SIZE, 1)) * a Xb_rot = rotatepair_batch.rotate_batch(data, angles[:, 0] * 180) aco = np.cos(angles * math.pi) asi = np.sin(angles * math.pi) za = np.concatenate((z, aco, asi), axis=1) samples = sess.run([output_rec], feed_dict={input_rec: za}) csamples.append(np.squeeze(samples)) creal.append(Xb_rot) csamples = np.squeeze(csamples) creal = np.squeeze(creal) sh = list(csamples.shape) sh[1] *= 2 cmerge = np.zeros(sh) cmerge[:, 0::2] = creal cmerge[:, 1::2] = csamples save_plot_matrix(cmerge, 'rec_%s_%i.png' % (tag, digit)) sess.close()
def get_batch(X,Y): a = np.random.uniform(-0.5, 0.5, size=(BATCH_SIZE,)) Xb,Yb = get_batch_only_Xb(X, Y) Xb_rot = rotatepair_batch.rotate_batch(Xb, a*180) return a, Xb, Xb_rot