Skip to content

Instantly share code, notes, and snippets.

@justinhchae
justinhchae / save_obj_multitexture.py
Last active June 22, 2023 03:49
Example Usage for save_obj with multiple textures in PyTorch3D
# install direct from our forked repository: https://github.com/Esri/pytorch3d/tree/multitexture-obj-high-precision
!pip install 'git+https://github.com/Esri/pytorch3d.git@multitexture-obj-high-precision'
from pytorch3d.io import save_obj
# save_obj now allows for saving objs with multiple textures and control over the texture outputs
save_obj(
f=obj_filename,
verts=obj[0],
faces=obj[1].verts_idx,
@justinhchae
justinhchae / subset_obj_pytorch3d.py
Last active June 22, 2023 03:50
Example Usage to Subset an Obj with PyTorch3D
# install direct from our forked repository: https://github.com/Esri/pytorch3d/tree/multitexture-obj-high-precision
!pip install 'git+https://github.com/Esri/pytorch3d.git@multitexture-obj-high-precision'
from pytorch3d.io import subset_obj
faces_to_subset = <a torch tensor of face indices to subset>
# all obj objects are subset to the selected face indices
obj_subset = subset_obj(
obj=obj,
@justinhchae
justinhchae / sample_points_from_obj_pytorch3d.py
Last active June 22, 2023 03:50
Sampling points with PyTorch3D
# install direct from our forked repository: https://github.com/Esri/pytorch3d/tree/multitexture-obj-high-precision
!pip install 'git+https://github.com/Esri/pytorch3d.git@multitexture-obj-high-precision'
from pytorch3d.ops import sample_points_from_obj
(
points, # points sampled proportional to face area
normals, # point normals based on mesh verts
textures, # point textures sampled from faces
mappers # an index to each points origin face
@justinhchae
justinhchae / load_obj_pytorch3d_high_precision.py
Last active June 22, 2023 03:49
Example Usage for PyTorch3D Load Obj with High Precision
# install direct from our forked repository: https://github.com/Esri/pytorch3d/tree/multitexture-obj-high-precision
!pip install 'git+https://github.com/Esri/pytorch3d.git@multitexture-obj-high-precision'
from pytorch3d.io import load_obj
# the current pytorch3d API for load_obj
obj = load_obj(
path_to_obj_file,
load_textures=True,
)
@justinhchae
justinhchae / install_import_pytorch3d_high_precision.py
Last active June 22, 2023 13:44
Example Install and Import for Esri PyTorch3D Fork
# install direct from our forked repository: https://github.com/Esri/pytorch3d/tree/multitexture-obj-high-precision
!pip install 'git+https://github.com/Esri/pytorch3d.git@multitexture-obj-high-precision'
# import the forked pytorch3d library and new functions
from pytorch3d.io import load_obj
from pytorch3d.ops import sample_points_from_obj
from pytorch3d.io import subset_obj, save_obj
@justinhchae
justinhchae / Numpy4PointClouds.py
Created May 3, 2022 23:25
A simple working example of how to use compressed numpy storage for data such as point clouds and classes.
'''https://numpy.org/doc/stable/reference/generated/numpy.savez_compressed.html#numpy.savez_compressed
'''
import numpy as np
import os
# number of points in the point cloud
num_points = 10
@justinhchae
justinhchae / sample_schema.py
Created March 14, 2021 17:53
sample_schema.py
class Budget2021(BudgetDB.base):
__tablename__ = "budget2021"
# uncomment the following line to print out this class as text
# df = get_vars_main('budget_main.csv')
id = Column(Integer, primary_key=True)
fund_type = Column(String)
# the code is a value like 4355 that means "Department A" -> F key to the table the name of the dept.
department_code = Column(Integer, ForeignKey("departmentDescription.department_code"))
fund_code = Column(String, ForeignKey("fundDescription.fund_code"))
organization_code = Column(Integer)
@justinhchae
justinhchae / config.py
Created March 14, 2021 17:49
schema generator for alchemy
def get_vars_main(filename):
"""
a helper function for creating the code for the main table of an sql alchemy schema
bases on a csv file, extract necessary column names and tables and print the code to terminal
copy and paste the code into the schema section
"""
logging.info('get_vars_main() Producing code to make main data table in alchemy.')
class_name = f'class {db_table_name.title()}({db_class_name}):'
full_path = os.sep.join([data_folder, filename])
@justinhchae
justinhchae / main.py
Created March 4, 2021 22:05
A made up function that calls another function to demonstrate multiprocessing with tqdm progress bar.
# given a function from fc.py
# https://gist.github.com/justinhchae/a2efee420330a985286e7b88fd049ce5
# in a file called main.py
import pandas as pd
# using the pytorch version of mp
import torch.multiprocessing as mp
# we need partial
from functools import partial
# progress bar
@justinhchae
justinhchae / fc.py
Last active March 4, 2021 22:06
A do nothing functio to demonstrate a function call to a function that does a progress bar with tqdm.
# solution reference:
# https://stackoverflow.com/questions/41707229/tqdm-printing-to-newline
def fc(x, param):
# inject some waiting to see progress unfold
time.sleep(1)
# set up pbar
pbar = tqdm(x.items(), desc='Exp:', position=0)
# init a variable for output
yhat = 0