def main(): tf.enable_eager_execution() vertices, faces = meshzoo.iso_sphere(2) faces = point_normals_outward(vertices, faces) vertices[:, 0] *= 0.5 vertices = tf.constant(vertices, dtype=tf.float32) faces = tf.constant(faces, dtype=tf.int32) visualise_mesh(vertices, faces) offset_direction = tf.linalg.l2_normalize([-1, 0.05, 0.]) offset_magnitudes = tf.where(tf.greater(vertices[:, 0], 0.45), tf.ones([len(vertices)]), tf.zeros([len(vertices)])) vertices = apply_offsets_with_pushing(vertices[None], faces, offset_direction[None], offset_magnitudes[None])[0] visualise_mesh(vertices, faces) offset_direction = tf.linalg.l2_normalize([0., 0.01, -1.]) offset_magnitudes = tf.where(tf.greater(vertices[:, 2], 0.9), tf.ones([len(vertices)]), tf.zeros([len(vertices)])) vertices = apply_offsets_with_pushing(vertices[None], faces, offset_direction[None], offset_magnitudes[None])[0] visualise_mesh(vertices, faces)
def write(filename, f): '''Write a function `f` defined in terms of spherical coordinates to a file. ''' import meshio import meshzoo points, cells = meshzoo.iso_sphere(5) # get spherical coordinates from points polar = numpy.arccos(points[:, 2]) azimuthal = numpy.arctan2(points[:, 1], points[:, 0]) vals = f(polar, azimuthal) meshio.write(filename, points, {'triangle': cells}, point_data={'f': vals}) return
def create_sphere(n_subdivide=3, fix_normals=True): # 3 makes 642 verts, 1280 faces, # 4 makes 2562 verts, 5120 faces verts, faces = meshzoo.iso_sphere(n_subdivide) if fix_normals: v0 = verts[faces[:,0], :] v1 = verts[faces[:,1], :] v2 = verts[faces[:,2], :] v_m = (v0+v1+v2)/3 e0 = v1-v0 e1 = v2-v0 p = np.cross(e0,e1) outwards = (v_m*p).sum(1) faces = np.where(outwards[:,None] > 0, faces[:,[0,1,2]], faces[:,[0,2,1]]) return verts, faces
self.encoder = models.resnet18(pretrained=self.use_pretrained_encoder, num_classes=self.bottleneck_size) self.decoder = PointGenCon_SkipConection( bottleneck_size=self.bottleneck_size) def forward(self, x, grid): x = x[:, :3, :, :].contiguous() x = self.encoder(x) grid = grid.contiguous() y = self.decoder(x, grid) return y.transpose(2, 1).contiguous() if __name__ == '__main__': import meshzoo x = torch.randn(8, 3, 256, 256).cuda() vert, face = meshzoo.iso_sphere(3) vert = torch.FloatTensor(vert).transpose(0, 1)[None, :, :].repeat(8, 1, 1).cuda() print(vert.shape) model = SVR_AtlasNet_SPHERE_Plus().cuda() print(model(x, vert).shape)
def create_sphere(n_subdivide=3): # 3 makes 642 verts, 1280 faces, # 4 makes 2562 verts, 5120 faces verts, faces = meshzoo.iso_sphere(n_subdivide) return verts, faces
print("previous weight loaded") # a = sio.loadmat(os.path.join('data','points_sphere.mat')) # points_sphere = np.array(a['p']) # points_sphere = torch.FloatTensor(points_sphere).transpose(0,1).contiguous() # print(points_sphere.shape) # torch.Size([3, 7446]) n_subdivide = 4 # inputs of sr.Mesh class should be like following. # verts: (#batch, #vertices, 3) # faces: (#batch, #faces, 3) verts, faces = [ elem.transpose(0, 1)[np.newaxis, ...] for elem in meshzoo.iso_sphere(n_subdivide) ] # print(verts.shape) # (1, 2562, 3) # print(faces.shape) # (1, 5120, 3) # logger test_loss = AverageValueMeter() test_curve = [] test_loss.reset() model.eval() with torch.no_grad(): for i, data in enumerate(tqdm.tqdm(dataloader), 0): img, points, label, _, _ = data
self.avg = 0 self.sum = 0 self.count = 0.0 def update(self, val, n=1): self.val = val self.sum += val * n self.count += n self.avg = self.sum / self.count if __name__ == "__main__": import numpy as np import meshzoo verts, faces = meshzoo.iso_sphere(3) verts = verts[np.newaxis, ...] faces = faces[np.newaxis, ...] output_path = 'logs/test_gif_01.gif' video = save_as_gif(verts, faces, output_path, input_img=None, verbose=True) print(video.shape) output_path = 'logs/test_gif_02' save_as_gif(verts, faces, output_path, input_img=None, verbose=False) save_as_obj(verts, faces, output_path, verbose=True)
def test_iso_sphere(): points, cells = meshzoo.iso_sphere() assert len(points) == 2562 assert _near_equal(numpy.sum(points, axis=0), [0.0, 0.0, 0.0]) assert len(cells) == 5120 return