Skip to content

Commit

Permalink
Merge pull request #125 from sanyatuning/policy
Browse files Browse the repository at this point in the history
Restructure policy dir, create python package openbot
  • Loading branch information
thias15 committed Dec 10, 2020
2 parents 7982539 + a312e86 commit 4d9bc40
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 39 deletions.
7 changes: 7 additions & 0 deletions policy/openbot/__init__.py
@@ -0,0 +1,7 @@
import os

base_dir = os.path.dirname(os.path.dirname(__file__))
dataset_dir = os.path.join(base_dir, "dataset")

if not (os.path.exists(dataset_dir)):
os.makedirs(dataset_dir)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 5 additions & 6 deletions policy/server/README.md → policy/openbot/server/README.md
Expand Up @@ -8,30 +8,29 @@ If you do not want install the dependencies globally, activate your conda enviro
conda activate openbot
```

Make sure you are in the folder `policy/server`. Now, you can install all the dependencies with the following command:
Make sure you are in the folder `policy`. Now, you can install all the dependencies with the following command:

```
pip install -r requirements.txt
pip install -r openbot/server/requirements.txt
```

## Running the server

You can run the python server with the command:

```
python main.py
python -m openbot.server
```

There is also a developer mode:

```
adev runserver main.py
adev runserver openbot/server
```

When you run the server you should see something like:

```
Dataset dir: <clone-dir>/OpenBot/policy/dataset
Skip address 127.0.0.1 @ interface lo0
Found address 192.168.x.x @ interface en0
Registration of a service, press Ctrl-C to exit...
Expand All @@ -45,7 +44,7 @@ You can now open your browser to visualize the dataset and see incoming uploads
http://localhost:8000/uploaded/
```

<img src="../../docs/images/server.gif" width="100%" alt="server" />
<img src="../../../docs/images/server.gif" width="100%" alt="server" />

## Troubleshooting

Expand Down
5 changes: 5 additions & 0 deletions policy/openbot/server/__main__.py
@@ -0,0 +1,5 @@
from aiohttp import web
from openbot.server.main import app

if __name__ == '__main__':
web.run_app(app, port=8000)
Expand Up @@ -3,7 +3,7 @@

from aiohttp import web

from . import dataset_dir
from .. import dataset_dir


async def handle_api(request):
Expand Down
15 changes: 5 additions & 10 deletions policy/server/main.py → policy/openbot/server/main.py
Expand Up @@ -3,13 +3,10 @@
from aiohttp import web
import aiohttp_jinja2
import jinja2
from openbot.api import dataset_dir, handle_api, get_dir_info
from openbot.preview import handle_preview
from openbot.upload import handle_file_upload
from openbot.zeroconf import register


print("Dataset dir: " + dataset_dir)
from .api import dataset_dir, handle_api, get_dir_info
from .preview import handle_preview
from .upload import handle_file_upload
from .zeroconf import register


@aiohttp_jinja2.template("index.html")
Expand Down Expand Up @@ -40,7 +37,5 @@ async def handle_post(request: web.Request) -> web.Response:
])
app.on_startup.append(register)
aiohttp_jinja2.setup(
app, loader=jinja2.FileSystemLoader(os.path.join(os.getcwd(), "templates"))
app, loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates"))
)
if __name__ == '__main__':
web.run_app(app, port=8000)
Expand Up @@ -4,7 +4,7 @@
from aiohttp import web
import imageio

from . import dataset_dir
from .. import dataset_dir


async def handle_preview(request):
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -4,7 +4,7 @@

from aiohttp import multipart, web

from . import dataset_dir
from .. import dataset_dir


async def handle_file_upload(field: multipart.BodyPartReader) -> web.Response:
Expand Down
File renamed without changes.
13 changes: 6 additions & 7 deletions policy/utils.py → policy/openbot/utils.py
Expand Up @@ -8,7 +8,6 @@
from nbconvert import HTMLExporter
import codecs
import nbformat
import metrics

def save_notebook():
Javascript("IPython.notebook.save_notebook()")
Expand Down Expand Up @@ -51,7 +50,7 @@ def show_train_batch(image_batch, cmd_batch, label_batch,fig_num=1):
plt.imshow(image_batch[n])
plt.title("Cmd: %s, Label: [%.2f %.2f]" \
%(cmd_batch[n],float(label_batch[n][0]),float(label_batch[n][1])))
plt.axis('off')
plt.axis('off')

def show_test_batch(image_batch, cmd_batch, label_batch, pred_batch,fig_num=1):
plt.figure(num=fig_num,figsize=(15,10))
Expand All @@ -60,7 +59,7 @@ def show_test_batch(image_batch, cmd_batch, label_batch, pred_batch,fig_num=1):
plt.imshow(image_batch[n])
plt.title("Cmd: %s, Label: [%.2f %.2f], Pred: [%.2f %.2f]" \
%(cmd_batch[n],float(label_batch[n][0]),float(label_batch[n][1]),float(pred_batch[n][0]),float(pred_batch[n][1])))
plt.axis('off')
plt.axis('off')

def generate_tflite(path, filename):
converter = tf.lite.TFLiteConverter.from_saved_model(os.path.join(path, filename))
Expand All @@ -69,14 +68,14 @@ def generate_tflite(path, filename):
return tflite_model

def save_tflite(tflite_model, path, filename):
open(os.path.join(path, filename + ".tflite"), "wb").write(tflite_model)
open(os.path.join(path, filename + ".tflite"), "wb").write(tflite_model)

def load_model(model_path,loss_fn,metric_list):
model = tf.keras.models.load_model(model_path,
custom_objects=None,
compile=False
)
model.compile(loss=loss_fn,
model.compile(loss=loss_fn,
metrics=metric_list)
return model

Expand All @@ -102,7 +101,7 @@ def compare_tf_tflite(model, tflite_model,img=None,cmd=None):
input_data[input_detail['name']] = (np.array(np.random.random_sample(input_detail['shape']), dtype=np.float32))
interpreter.set_tensor(input_detail['index'], input_data[input_detail['name']])


interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
Expand All @@ -117,4 +116,4 @@ def compare_tf_tflite(model, tflite_model,img=None,cmd=None):
# Compare the result.
for tf_result, tflite_result in zip(tf_results, tflite_results):
print("Almost equal (5% tolerance):", np.allclose(tf_result, tflite_result, rtol=5e-02))
#np.testing.assert_almost_equal(tf_result, tflite_result, decimal=2)
#np.testing.assert_almost_equal(tf_result, tflite_result, decimal=2)
10 changes: 3 additions & 7 deletions policy/policy_learning.ipynb
Expand Up @@ -64,8 +64,7 @@
"metadata": {},
"outputs": [],
"source": [
"import utils\n",
"import dataloader"
"from openbot import dataloader, utils"
]
},
{
Expand Down Expand Up @@ -172,7 +171,7 @@
"metadata": {},
"outputs": [],
"source": [
"import associate_frames\n",
"from openbot import associate_frames\n",
"max_offset = 1e3 #1ms\n",
"train_frames = associate_frames.match_frame_ctrl_cmd(train_data_dir, \n",
" train_datasets, \n",
Expand Down Expand Up @@ -602,10 +601,7 @@
"metadata": {},
"outputs": [],
"source": [
"import models\n",
"import losses\n",
"import metrics\n",
"import callbacks\n",
"from openbot import callbacks, losses, metrics, models\n",
"\n",
"model = models.cil_mobile(NETWORK_IMG_WIDTH,NETWORK_IMG_HEIGHT,BN)\n",
"loss_fn = losses.sq_weighted_mse_angle \n",
Expand Down
6 changes: 0 additions & 6 deletions policy/server/openbot/__init__.py

This file was deleted.

0 comments on commit 4d9bc40

Please sign in to comment.