def objective(v): x_v, parts_v = (v[:T * o.dxA], v[T * o.dxA:]) omega_v, theta_v = (parts_v[:K * o.dxA], parts_v[K * o.dxA:]) # build omega and compute omega cost for k in range(K): omega_vk = omega_v[k * o.dxA:(k + 1) * o.dxA] omega[k] = matmul(omega0, ex(alg(G, omega_vk))) omegaCost[k] = tf.reduce_sum( mahalanobis2_tf(tf.expand_dims(omega_vk, 0), Wi)) # iterate through each time xPrev, QPrev_i = (x0, Sigma_x0i) thetaPrev = theta0 for t in range(T): # Make x_t x_vt = x_v[t * o.dxA:(t + 1) * o.dxA] x[t] = matmul(xPrev, ex(alg(G, x_vt))) # x_t cost xCost[t] = tf.reduce_sum( mahalanobis2_tf(tf.expand_dims(x_vt, 0), QPrev_i)) theta_vt = theta_v[t * K * o.dxA:(t + 1) * K * o.dxA] for k in range(K): # Make theta_tk s_tk = theta_vt[k * o.dxA:(k + 1) * o.dxA] s_tkRot = s_tk[o.dy:] s_tkTrans = s_tk[:o.dy] R_thetaPrev_k, d_thetaPrev_k = SED_tf.Rt(np2tf(thetaPrev[k])) R_theta_tk = matmul(R_thetaPrev_k, ex(alg(GRot, s_tkRot))) theta[t][k] = SED_tf.MakeRd(o, R_theta_tk, s_tkTrans) # theta_tk cost m_tk = matvec(Bi, (s_tkTrans - matvec(A, d_thetaPrev_k))) val = tf.concat([m_tk, s_tkRot], axis=0) thetaCost[t][k] = tf.reduce_sum( mahalanobis2_tf(tf.expand_dims(val, 0), Si[k])) lhs = SED_tf.inv(o, matmul(matmul(x[t], omega[k]), theta[t][k])) yPart = SED_tf.TransformPointsNonHomog(lhs, y_[t]) negDists[k] = -mahalanobis2_tf(yPart, Ei[k]) thetaCost_t[t] = tf.reduce_sum(thetaCost[t]) negDistsStacked = tf.stack(negDists) smoothMins = -tf.math.reduce_logsumexp(negDistsStacked, axis=0) obsCost[t] = tf.reduce_sum(smoothMins) # Set prevs xPrev = x[t] thetaPrev = theta[t] QPrev_i = Qi ## end time t totalCost = tf.reduce_sum(xCost) + tf.reduce_sum(omegaCost) + \ tf.reduce_sum(thetaCost_t) + tf.reduce_sum(obsCost) return totalCost
def compute_inception_score(model, d): mean, logvar = model.encode(test_images) r_m = np.identity(model.latent_dim) z = model.reparameterize(mean, logvar) r_x = rotate(test_images, d) c, s = np.cos(d), np.sin(d) r_m[0, [0, 1]], r_m[1, [0, 1]] = [c, s], [-s, c] rota_z = matvec(tf.cast(r_m, dtype=tf.float32), z) phi_x = model.sample(rota_z) return inception_model.compute_score(r_x, phi_x)
def compute_mnist_score(model, classifier, z=0, d=0, r_m=0, initial=False): if (initial == True): mean, logvar = model.encode(test_images) r_m = np.identity(model.latent_dim) z = model.reparameterize(mean, logvar) d = np.radians(random.randint(0, 90)) c, s = np.cos(d), np.sin(d) r_m[0, [0, 1]], r_m[1, [0, 1]] = [c, s], [-s, c] rota_z = matvec(tf.cast(r_m, dtype=tf.float32), z) phi_z = model.sample(rota_z) scores = classifier.mnist_score(phi_z) return scores
def objective(qs_t): q_t, s_t = (qs_t[:o.dxA], qs_t[o.dxA:]) # make x_t, theta_tk for each k x_t = matmul(xPrev_, ex(alg(G, q_t))) theta_t = [[] for k in range(K)] for k in range(K): s_tk = s_t[k * o.dxA:(k + 1) * o.dxA] s_tkRot = s_tk[o.dy:] s_tkTrans = s_tk[:o.dy] R_theta_t = matmul(R_thetaPrev[k], ex(alg(GRot, s_tkRot))) theta_t[k] = SED_tf.MakeRd(o, R_theta_t, s_tkTrans) lhs = [ SED_tf.inv(o, matmul(matmul(x_t, omega_[k]), theta_t[k])) for k in range(K) ] yPart = [SED_tf.TransformPointsNonHomog(lhs[k], yt_) for k in range(K)] negDists = tf.stack( [-mahalanobis2_tf(yPart[k], Ei[k]) for k in range(K)]) smooth_mins = -tf.math.reduce_logsumexp(negDists, axis=0) cost = tf.reduce_sum(smooth_mins) # x dynamics cost_xDyn = tf.reduce_sum(mahalanobis2_tf(tf.expand_dims(q_t, 0), Qi)) # theta dynamics cost_thetaDyn = [[] for k in range(K)] for k in range(K): s_tk = s_t[k * o.dxA:(k + 1) * o.dxA] s_tkRot = s_tk[o.dy:] s_tkTrans = s_tk[:o.dy] m_tk = matvec(Bi, (s_tkTrans - matvec(A, d_thetaPrev[k]))) val = tf.concat([m_tk, s_tkRot], axis=0) cost_thetaDyn[k] = tf.reduce_sum( mahalanobis2_tf(tf.expand_dims(val, 0), Si[k])) return cost + cost_xDyn + tf.reduce_sum(cost_thetaDyn)
def inv(o, x): R, d = Rt(x) Rnew = tf.transpose(R) dnew = matvec(-Rnew, d) return MakeRd(o, Rnew, dnew)
def rotate_vector(vector, matrix): matrix = tf.cast(matrix, tf.float32) test = matvec(matrix, vector) return test
for i in range(predictions.shape[0]): plt.subplot(4, 4, i + 1) plt.imshow(predictions[i, :, :, 0], cmap='gray') plt.axis('off') file_dir = './image/'+ dire if not os.path.exists(file_dir): os.makedirs(file_dir) plt.savefig(file_dir +'/image_at_epoch_{:04d}.png'.format(degree)) plt.close() def generate_images(model, data): fig = plt.figure(figsize=(4, 4)) for i in range(data.shape[0]): plt.subplot(4, 4, i + 1) plt.imshow(data[i, :, :, 0], cmap='gray') plt.axis('off') plt.show() for i in range(1, 6): model = CVAE(latent_dim=16, beta=i) checkpoint = tf.train.Checkpoint(model=model) checkpoint.restore("checkpoints/2_20method" + str(i) + "/ckpt-10") mean, logvar = model.encode(test_sample) r_m = np.identity(model.latent_dim) z = model.reparameterize(mean, logvar) theta = np.radians(60) c, s = np.cos(theta), np.sin(theta) r_m[0, [0, 1]], r_m[1, [0, 1]] = [c, s], [-s, c] rota_z = matvec(tf.cast(r_m, dtype=tf.float32), z) phi_z = model.decode(rota_z) generate_and_save_images(phi_z, 1, 'test3' + "/beta_test" + str(i))