Skip to content

Commit

Permalink
iit
Browse files Browse the repository at this point in the history
  • Loading branch information
huiwenn committed Feb 24, 2024
1 parent ca19300 commit ecd38b8
Show file tree
Hide file tree
Showing 17 changed files with 749 additions and 592 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/pylinter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ jobs:
# - name: Checkout repo
# uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install flake8
run: pip install flake8
- name: Python Linter
uses: sunnysid3up/python-linter@master
uses: suo/flake8-github-action@releases/v1
with:
source: "src"
mypy-options: "--ignore-missing-imports --show-error-codes"
black-options: "--exclude **/*"
#pylint-options: "--rcfile=setup.cfg"
isort-options: "-w 100"
django: true
checkName: 'flake8_py3'
path: "src"
args: "--max-line-length=120 --ignore=E402,E731,F541,W291,E122,E127,F401,E266,E241,C901,E741,W293,F811,W503,E203"
#- name: GitHub Action for pylint
# uses: marian-code/python-lint-annotate@v3
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.defaultInterpreterPath": "/Users/sophiasun/miniforge3/envs/py3/bin/python"
}
4 changes: 2 additions & 2 deletions src/experiments/covid_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def experiment(train, valid, test, target_len=50, name="exp"):
encdec_model = lstm.lstm_seq2seq(
input_size=1, output_size=1, embedding_size=128, target_len=target_len
)
models = [rnn_model] # , encdec_model]
models = [rnn_model, encdec_model]

x_train = train.X
y_train = train.Y
Expand Down Expand Up @@ -82,7 +82,7 @@ def experiment(train, valid, test, target_len=50, name="exp"):

epsilon_ls = np.linspace(0.05, 0.50, 10)

for k, uqmethodß in UQ.items():
for k, uqmethod in UQ.items():
print(k)
area = []
coverage = []
Expand Down
180 changes: 104 additions & 76 deletions src/experiments/drone_exp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand All @@ -7,78 +6,98 @@
from src.models import rnn, lstm, bjrnn, cfrnn, copulaCPTS, dplstm, vanila_copula


datapath = '../../robotics_project/PythonRobotics/AerialNavigation/drone_3d_trajectory_following/data/dataset_smallnoise.npy'
datapath = "../../robotics_project/PythonRobotics/AerialNavigation/\
drone_3d_trajectory_following/data/dataset_smallnoise.npy"

data = np.load(datapath, allow_pickle=True)
x_downsample = data[:,::3,:].astype(np.float32)

train_len=60
pred_len=10

x_train = torch.tensor(x_downsample[:600,:train_len,:], dtype=torch.float)
y_train = torch.tensor(x_downsample[:600,train_len:train_len+pred_len,:3], dtype=torch.float)

x_cali = torch.tensor(x_downsample[600:800,:train_len,:], dtype=torch.float)
y_cali = torch.tensor(x_downsample[600:800,train_len:train_len+pred_len,:3], dtype=torch.float)

x_test = torch.tensor(x_downsample[800:,:train_len,:], dtype=torch.float)
y_test = torch.tensor(x_downsample[800:, train_len:train_len+pred_len,:3], dtype=torch.float)

def experiment(x_downsample, train_len=60, pred_len = 10, name='exp',
x_train=x_train,
y_train=y_train,
x_cali=x_cali,
y_cali=y_cali,
x_test=x_test,
y_test=y_test):

rnn_model = rnn.rnn(embedding_size=128, input_size=6, output_size=3, horizon=pred_len)
encdec_model = lstm.lstm_seq2seq(input_size=6, output_size=3, embedding_size=128, target_len = pred_len)
models = [rnn_model,encdec_model]

models[0].train_model(x_train,y_train, n_epochs=500, batch_size=150)
#models[1].train_model(x_train,y_train, n_epochs=500, batch_size=150)

with open('./trained/models_%s.pkl'%name, 'wb') as f:
pickle.dump(models , f)


x_downsample = data[:, ::3, :].astype(np.float32)

train_len = 60
pred_len = 10

x_train = torch.tensor(x_downsample[:600, :train_len, :], dtype=torch.float)
y_train = torch.tensor(
x_downsample[:600, train_len : train_len + pred_len, :3], dtype=torch.float
)

x_cali = torch.tensor(x_downsample[600:800, :train_len, :], dtype=torch.float)
y_cali = torch.tensor(
x_downsample[600:800, train_len : train_len + pred_len, :3], dtype=torch.float
)

x_test = torch.tensor(x_downsample[800:, :train_len, :], dtype=torch.float)
y_test = torch.tensor(
x_downsample[800:, train_len : train_len + pred_len, :3], dtype=torch.float
)


def experiment(
x_downsample,
train_len=60,
pred_len=10,
name="exp",
x_train=x_train,
y_train=y_train,
x_cali=x_cali,
y_cali=y_cali,
x_test=x_test,
y_test=y_test,
):

rnn_model = rnn.rnn(
embedding_size=128, input_size=6, output_size=3, horizon=pred_len
)
encdec_model = lstm.lstm_seq2seq(
input_size=6, output_size=3, embedding_size=128, target_len=pred_len
)
models = [rnn_model, encdec_model]

models[0].train_model(x_train, y_train, n_epochs=500, batch_size=150)
# models[1].train_model(x_train,y_train, n_epochs=500, batch_size=150)

with open("./trained/models_%s.pkl" % name, "wb") as f:
pickle.dump(models, f)

UQ = {}


bj_class = bjrnn.bj_rnn(models[0], recursion_depth=10)
UQ['bjrnn'] = bj_class

dprnn = dplstm.DPRNN(epochs=150, input_size=6, output_size=3, n_steps=pred_len, batch_size=64, dropout_prob=0.1)
UQ["bjrnn"] = bj_class

dprnn = dplstm.DPRNN(
epochs=150,
input_size=6,
output_size=3,
n_steps=pred_len,
batch_size=64,
dropout_prob=0.1,
)
dprnn.fit(x_train, y_train)
UQ['dprnn'] = dprnn
UQ["dprnn"] = dprnn

cf = cfrnn.CFRNN(models[0], x_cali, y_cali)
cf.calibrate()
UQ['cfrnn'] = cf
UQ["cfrnn"] = cf

cf = cfrnn.CFRNN(models[1], x_cali, y_cali)
cf.calibrate()
UQ['cf-EncDec'] = cf
UQ["cf-EncDec"] = cf

copula = copulaCPTS.copulaCPTS(models[0], x_cali, y_cali)
copula.calibrate()
UQ['copula-rnn'] = copula
UQ["copula-rnn"] = copula

copula = copulaCPTS.copulaCPTS(models[1], x_cali, y_cali)
copula.calibrate()
UQ['copula-EncDec'] = copula



UQ["copula-EncDec"] = copula

areas = {}
coverages = {}

epsilon_ls = np.linspace(0.05, 0.50, 10)

for k, uqmethod in UQ.items():
print(k)

area = []
coverage = []
for eps in epsilon_ls:
Expand All @@ -88,46 +107,55 @@ def experiment(x_downsample, train_len=60, pred_len = 10, name='exp',
coverage.append(uqmethod.calc_coverage_3d(box, pred, y_test))
areas[k] = area
coverages[k] = coverage
with open('./trained/uq_%s.pkl'%name, 'wb') as f:
pickle.dump(UQ , f)
with open('./trained/results_%s.pkl'%name, 'wb') as f:
pickle.dump((areas, coverages) , f)

with open("./trained/uq_%s.pkl" % name, "wb") as f:
pickle.dump(UQ, f)
with open("./trained/results_%s.pkl" % name, "wb") as f:
pickle.dump((areas, coverages), f)

return areas, coverages, (models, UQ)


def main():
lens = [1, 5, 10, 15]
for pred_len in lens:
x_train = torch.tensor(x_downsample[:600,:train_len,:], dtype=torch.float)
y_train = torch.tensor(x_downsample[:600,train_len:train_len+pred_len,:3], dtype=torch.float)

x_cali = torch.tensor(x_downsample[600:800,:train_len,:], dtype=torch.float)
y_cali = torch.tensor(x_downsample[600:800,train_len:train_len+pred_len,:3], dtype=torch.float)

x_test = torch.tensor(x_downsample[800:,:train_len,:], dtype=torch.float)
y_test = torch.tensor(x_downsample[800:, train_len:train_len+pred_len,:3], dtype=torch.float)
x_train = torch.tensor(x_downsample[:600, :train_len, :], dtype=torch.float)
y_train = torch.tensor(
x_downsample[:600, train_len : train_len + pred_len, :3], dtype=torch.float
)

x_cali = torch.tensor(x_downsample[600:800, :train_len, :], dtype=torch.float)
y_cali = torch.tensor(
x_downsample[600:800, train_len : train_len + pred_len, :3],
dtype=torch.float,
)

x_test = torch.tensor(x_downsample[800:, :train_len, :], dtype=torch.float)
y_test = torch.tensor(
x_downsample[800:, train_len : train_len + pred_len, :3], dtype=torch.float
)

for i in range(3):
res = experiment(x_downsample, pred_len= pred_len,
name='drone_split_len_'+str(pred_len)+'_run_'+str(i),
res = experiment(
x_downsample,
pred_len=pred_len,
name="drone_split_len_" + str(pred_len) + "_run_" + str(i),
x_train=x_train,
y_train=y_train,
x_cali=x_cali,
y_cali=y_cali,
x_test=x_test,
y_test=y_test
)
print('finished run ' + str(pred_len))
del res
y_test=y_test,
)
print("finished run " + str(pred_len))
del res


for i in range(3):
res = experiment(x_downsample, name='drone_vanila_'+str(i))
print('finished run ' + str(i))
del res
res = experiment(x_downsample, name="drone_vanila_" + str(i))
print("finished run " + str(i))
del res


if __name__ == '__main__':
if __name__ == "__main__":

main()
Loading

0 comments on commit ecd38b8

Please sign in to comment.