def load_model(self, clicked): s_m = str(self.cb_model.currentText()) # Fill up settings from model string if 'Mnist' in s_m: self.dataset = MNIST() self.z_dim = 5 self.y_dim = 10 self.image_size = 28 self.image_channels = 1 elif 'CelebBig' in s_m: self.dataset = CelebBig() self.z_dim = 128 self.y_dim = 40 self.image_size = 128 self.image_channels = 3 elif 'Celeb' in s_m: self.dataset = CelebA() self.z_dim = 50 self.y_dim = 40 self.image_size = 32 self.image_channels = 3 elif 'Cell' in s_m: self.dataset = Cell() self.z_dim = 50 self.y_dim = None self.image_size = 64 self.image_channels = 1 if 'noy' in s_m: self.y_dim = None if 'Mnist_Dense' in s_m: model_class = ModelDenseMnist elif 'Mnist_Conv' in s_m: model_class = ModelConvMnist elif 'Celeb_Conv' in s_m: model_class = ModelConv32 elif 'Celeb_Subpix' in s_m: model_class = ModelSubpix32 self.y_dim = None elif 'CelebBig' in s_m: model_class = ModelConv128 elif 'CelebBig' in s_m: model_class = ModelConv128 elif 'Cell' in s_m: model_class = ModelConv64 model = model_class(batch_size=self.batch_size, z_dim=self.z_dim, y_dim=self.y_dim, is_training=False) # # if 'WGan' in s_m: # self.solver = AaeWGanSolver(model=model) # elif 'Gan' in s_m: # self.solver = AaeGanSolver(model=model) # else: self.solver = AaeSolver(model=model) self.sess.run(tf.global_variables_initializer()) # Saver saver = tf.train.Saver() # Restore previous saver.restore(self.sess, s_m) self.build_interface()
def manifold_images(model, name, gan, y=False): # Solver if gan == 'WGan': print("WGan Solver") solver = AaeWGanSolver(model=model) gan = 'WGan_' elif gan == 'Gan': print("Gan Solver") solver = AaeGanSolver(model=model) gan = 'Gan_' else: solver = AaeSolver(model=model) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() # To restore previous print("Restoring model") saver.restore(sess, 'models/model_%s%s.ckpt' % (gan, name)) print("Model restored") r = 1.8 # Generate grid of z ls = np.linspace(-r, r, samples) lf = np.linspace(-3, 3, frames) z = np.array(list(product(lf, ls, ls, ls, ls))) if not os.path.exists('output/z_tmp'): os.makedirs('output/z_tmp') if y: rng = 10 else: rng = 1 for z_i in range(5): z_t = np.roll(z, z_i, axis=1) for code_v in range(rng): code = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] code[0][code_v] = 1 size = (samples**4 * frames) y_lab = np.reshape(code * size, [size, 10]) img = sess.run(solver.x_from_z, feed_dict={ solver.z_provided: z_t, solver.y_labels: y_lab }) files = [] w = 28 * samples**2 + (samples - 1) * 10 h = (28 * samples**2 + (samples - 1) * 10) * frames b_i_canv = Image.new('L', (w, h), color=1) b_i_apng = Image.new('L', (w, w), color=1) ls = range(samples) lf = range(frames) prod = list(product(lf, ls, ls, ls, ls)) for i, p in enumerate(prod): index = samples ** 4 * p[0] + samples ** 3 * p[1] + \ samples ** 2 * p[2] + samples * p[3] + p[4] im = img[index].reshape([28, 28]) cimg = Image.fromarray(np.uint8(im * 255)) x = p[4] * 28 + p[2] * (10 + 28 * samples) y = p[3] * 28 + p[1] * (10 + 28 * samples) b_i_apng.paste(cimg, (x, y)) y_p = y + p[0] * (28 * samples**2 + (samples - 1) * 10) b_i_canv.paste(cimg, (x, y_p)) if (i + 1) % samples**4 == 0: file = "output/z_tmp/Res_%d.png" % (i // samples**4) files.append(file) b_i_apng.save(file) ap = APNG() # Create animated png for file in files: ap.append(file, delay=100) for file in files[::-1]: ap.append(file, delay=100) if not os.path.exists('output/z_%d' % z_i): os.makedirs('output/z_%d' % z_i) ap.save("output/z_%d/%s_%d.apng" % (z_i, name, code_v)) b_i_canv.save("output/z_%d/%s_%d.png" % (z_i, name, code_v))
def compare_style(model, name, gan, y=False): # Solver if gan == 'WGan': print("WGan Solver") solver = AaeWGanSolver(model=model) gan = 'WGan_' elif gan == 'Gan': print("Gan Solver") solver = AaeGanSolver(model=model) gan = 'Gan_' else: solver = AaeSolver(model=model) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() # To restore previous print("Restoring model") saver.restore(sess, 'models/model_%s%s.ckpt' % (gan, name)) print("Model restored") b_i = [] for i in range(91): b_i.append(Image.new('L', (28 * 5, 28 * 5))) ys = [] for i in range(9): y = [0] * 10 y[i] = 1.0 ys.append(np.array(y).reshape([1, 10])) for j in range(1, 10): y = [0] * 10 y[i] = 1 - np.sin(np.pi / 20 * j) y[i + 1] = np.sin(np.pi / 20 * j) ys.append(np.array(y).reshape([1, 10])) y = [0] * 10 y[9] = 1.0 ys.append(np.array(y).reshape([1, 10])) y = np.concatenate(ys, axis=0) for i in range(25): z = np.random.uniform(-1.5, 1.5, size=[1, 5]) z = np.tile(z, [91, 1]) img = sess.run(solver.x_from_z, feed_dict={ solver.z_provided: z, solver.y_labels: y }) for j in range(91): im = img[j].reshape([28, 28]) cimg = Image.fromarray(np.uint8(im * 255)) x = 28 * (i % 5) x2 = 28 * (i // 5) b_i[j].paste(cimg, (x, x2)) if not os.path.exists('output/z_tmp'): os.makedirs('output/z_tmp') if not os.path.exists('output/style'): os.makedirs('output/style') ap = APNG() files = [] for i in range(91): file = "output/z_tmp/Res_%d.png" % i b_i[i].save(file) files.append(file) # Create animated png for file in files: ap.append(file, delay=200) ap.save("output/style/%s.apng" % name)
# Return solution return Y if __name__ == "__main__": print( "Run Y = tsne.tsne(X, no_dims, perplexity) to perform t-SNE on your dataset." ) print("Running example on 2,500 MNIST digits...") print("Draw Mnist Dense no y") model = ModelDenseMnist(batch_size=128, z_dim=5, y_dim=None, is_training=False) solver = AaeSolver(model) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() # To restore previous print("Restoring model") saver.restore(sess, 'models/model_Mnist_Dense_noy.ckpt') print("Model restored") z = [] y = [] pred_out = [] data = MNIST() i = 100
def plot_samples(model, data, name): # Solver solver = AaeSolver(model=model) # Session config = tf.ConfigProto(device_count={'GPU': 0}) sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) # Saver saver = tf.train.Saver() # To restore previous print("Restoring model") saver.restore(sess, 'models/model_%s.ckpt' % name) print("Model restored") z = [] y = [] pred_out = [] i = 500 for batch_x, batch_y in data.iterate_minibatches(model.batch_size, shuffle=True): z_enc, y_enc = sess.run([solver.z_encoded, solver.y_pred_enc], feed_dict={ solver.x_image: batch_x, solver.y_labels: batch_y }) z.append(z_enc) pred_out.extend(y_enc) y.extend(batch_y) if i == 0: break i -= 1 z = np.concatenate(z, axis=0) f, axarr = plt.subplots(model.z_dim, model.z_dim) for j in range(model.z_dim): for i in range(model.z_dim): ax = axarr[i, j] ax.set_title('Axis [%d, %d]' % (i, j)) ax.scatter(z[:, j], z[:, i], c=pred_out) if i != model.z_dim - 1: plt.setp(ax.get_xticklabels(), visible=False) if j != 0: plt.setp(ax.get_yticklabels(), visible=False) plt.show() y = np.argmax(y, axis=1) f, axarr = plt.subplots(model.z_dim, model.z_dim) for i in range(model.z_dim): for j in range(model.z_dim): ax = axarr[i, j] ax.set_title('Axis [%d, %d]' % (i, j)) ax.scatter(z[:, i], z[:, j], c=y, s=1) if i != model.z_dim - 1: plt.setp(ax.get_xticklabels(), visible=False) if j != 0: plt.setp(ax.get_yticklabels(), visible=False) ax.set_ylim([-3, 3]) ax.set_xlim([-3, 3]) ax.set_autoscalex_on(False) ax.set_autoscaley_on(False) plt.show()
def draw_images(model, name, systematic=True, gan=''): # Solver if gan == 'WGan': print("WGan Solver") solver = AaeWGanSolver(model=model) gan = 'WGan_' elif gan == 'Gan': print("Gan Solver") solver = AaeGanSolver(model=model) gan = 'Gan_' else: solver = AaeSolver(model=model) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() # To restore previous print("Restoring model") saver.restore(sess, 'models/model_%s%s.ckpt' % (gan, name)) print("Model restored") z = [] # If systematic then all animations pass through origin # If random then they pass through respective axis if systematic: name += 'sys' a = np.zeros([50, 50]) else: name += 'rand' a = np.random.uniform(-1, 1, [50, 50]) # We use sinus so that we can see edge images for longer for v in np.linspace(-np.pi / 2, np.pi / 2, frames): for i in range(model.z_dim): b = np.reshape(np.copy(a[i, :]), [1, 50]) b[0][i] = 2.5 * np.sin(v) z.append(b) z = np.concatenate(z) y = np.zeros([model.z_dim * frames, 1]) img = sess.run(solver.x_from_z, feed_dict={ solver.z_provided: z, solver.y_labels: y }) files = [] w = 32 * 10 h = 32 * 5 b_i_apng = Image.new('RGB', (w, h), color=1) b_i_canv = Image.new('RGB', (w, h * frames), color=1) for f in range(frames): for x in range(10): for y in range(5): index = f * 50 + x + y * 10 im = CelebA.transform2display(img[index]) cimg = Image.fromarray(np.uint8(1 + im * 254)) b_i_apng.paste(cimg, (x * 32, y * 32)) b_i_canv.paste(cimg, (x * 32, y * 32 + f * 32 * 5)) file = "output/celeb/Res_%d.png" % f files.append(file) # blank_image = blank_image.resize((128*10, 128*5)) b_i_apng.save(file) ap = APNG() # Create animated png for file in files: ap.append(file, delay=50) for file in files[::-1]: ap.append(file, delay=50) if not os.path.exists('output/celeb'): os.makedirs('output/celeb') ap.save("output/celeb/%s.apng" % name) b_i_canv.save("output/celeb/%s.png" % name)