Skip to content

Instantly share code, notes, and snippets.

View rmsander's full-sized avatar

Ryan Sander rmsander

View GitHub Profile
@rmsander
rmsander / data_fusion_constructor_sampler_only.py
Last active September 6, 2021 14:18
Module for implementing an Audi Autonomous Driving Dataset (A2D2) DataLoader designed for performing data fusion betweeen 2D RGB images and 3D Lidar point clouds.
"""Module for implementing an Audi Autonomous Driving Dataset (A2D2) DataLoader
designed for performing data fusion betweeen 2D RGB images and 3D Lidar point clouds."""
import numpy as np
from torch.utils.data import Dataset
class A2D2DataLoader(Dataset):
def __init__(self, dataset, rotation=None, \
normalize_xyz=True, normalize_rgb=True, \
take_subset=False, convert_to_tensor=True, \
target_ids=[]):
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import WhiteKernel, DotProduct, RBF
import matplotlib.pyplot as plt
import numpy as np
# Random seeds
np.random.seed(seed=0) # Set seed for NumPy
random_state = 0
# Generate features, and take norm for use with target
import gpytorch
import numpy as np
import matplotlib.pyplot as plt
import torch
# Create features
X = torch.tensor(np.arange(100)).float()
# Create targets as noisy function of features
Y = torch.tensor(np.add(np.sin(X / 10), np.random.normal(loc=0, scale=0.5, size=100))).float()
@rmsander
rmsander / mixup_general.py
Last active July 31, 2021 17:21
Implements Mixup for generalized vector/matrix/tensor combinations using TensorFlow.
import tensorflow as tf
import numpy as np
def mixup_tensorflow(X1, X2, alpha=1.0):
"""Function for implementing Mixup using TensorFlow.
Mixup interpolation occurs between corresponding indices
of X1[i] and X2[i].
Parameters:

If you're using mujoco-py (likely through the MuJoCo suite in gym) on MIT Supercloud, you may encounter issues with POSIX locking if you try to install and setup mujoco-py on the standard shared file system.

To get around this, you can install mujoco-py on a different section (the permanent storage section) of the Supercloud server. Below is a bash script I used to get around issue (you may have to modify some of the commands, such as the conda environment you use):

#!/bin/bash

#SBATCH -c 10
#SBATCH -n 1
#SBATCH --exclusive
@rmsander
rmsander / mixup_tensorflow.py
Last active August 1, 2021 01:11
MIxup TensorFlow
import tensorflow as tf
import numpy as np
def mixup_tensorflow(X1, X2, alpha=1.0):
"""Function for implementing Mixup using TensorFlow.
Mixup interpolation occurs between corresponding indices
of X1[i] and X2[i].
Parameters:
@rmsander
rmsander / uniform_same_class_selection.py
Last active September 6, 2021 14:06
Module for a TF/Keras function for selecting samples of the same class to interpolate (e.g. via Mixup interpolation).
"""Module for a TF/Keras function for selecting samples of the same class to interpolate (e.g. via
Mixup interpolation)."""
import numpy as np
import tensorflow as tf
def vector_same_class_sample(X, Y, y_sample, num_classes=2):
"""Function for generating equal-class Mixup pairs in a vectorized fashion.
Essentially, what we'll do is separate X into different sets according to the
class of the corresponding index in Y. Then, given y_sample, a set of classes
import numpy as np
import tensorflow as tf
def vector_same_class_sample_from_file(Y, y_sample, base_paths_by_class=None, X=None, num_classes=2):
"""Function for generating equal-class Mixup pairs in a vectorized fashion.
Essentially, what we'll do is separate X into different sets according to the
class of the corresponding index in Y. Then, given y_sample, a set of classes
of the first set of images/vectors that we use for Mixup, we pair the same classes
together appropriately by only sampling corresponding same-class images/vectors
from X using appropriate indexing.
"""Adapted from the Keras VAE guide: https://keras.io/examples/generative/vae/."""
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
def make_encoder():
"""Function for making the encoder."""
latent_dim = 10
@rmsander
rmsander / set_mujoco_env_state.py
Created September 6, 2021 13:51
Allows for dynamically changing the state of MuJoCo environments.
"""Module for setting a Gym MuJoCo environment's state to a stored state using
qpos and qvel inputs, which are used for state setting in the MuJoCo simulator.
An example is carried out with the HalfCheetah environment."""
from gym.envs.mujoco.half_cheetah_v3 import HalfCheetahEnv
import numpy as np
def main():
# Initialize and reset the environment to extract initial state