Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logs #5

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*/__pycache__/
*.py[cod]
*$py.class

Expand Down
99 changes: 99 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from typing import Optional
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import json
import numpy as np

import glob
import os



def collect_first_best(pattern: str, neg: Optional[str] = None):
files = []
for file in sorted(glob.glob(pattern), reverse=True):
if neg and neg in file:
continue
files.append(file)

figures = []
for file in files:
title = os.path.split(file)[1].split('.')[0]
df1 = pd.read_csv(file)
figures.append(px.box(df1, x="map_name", y="counts",
color="algo_type",
notched=True, title=title))
return figures


def collect_line_plots(pattern: str, neg: Optional[str] = None):
files = []
for file in sorted(glob.glob(pattern), reverse=True):
if neg and neg in file:
continue
files.append(file)

lineplots = []
for file in files:
with open(file) as f:
data = json.loads(f.readline().rstrip('\n'))
title = os.path.split(file)[1].split('.')[0]
fig = go.Figure()
fig.update_layout(dict(title=title))
for map_name, counts in data.items():
uniform_mean = np.array(counts['uniform']['mean'])
uniform_std = np.array(counts['uniform']['std'])

y_u1 = (uniform_mean - uniform_std).tolist()
y_u2 = (uniform_mean + uniform_std).tolist()

roi_mean = np.array(counts['roi']['mean'])
roi_std = np.array(counts['roi']['std'])
y_r1 = (roi_mean - 0.5 * roi_std).tolist()
y_r2 = (roi_mean + roi_std).tolist()

x1 = list(range(len(uniform_mean)))
x2 = list(range(len(roi_mean)))

fig.add_trace(go.Scatter(x=x1 + x1[::-1], y=y_u1 + y_u2[::-1], fill='toself', name='RRT*-uniform_' + map_name))
fig.add_trace(go.Scatter(x=x2 + x2[::-1], y=y_r1 + y_r2[::-1], fill='toself', name='RRT*-roi_' + map_name))

#fig.add_trace(go.Scatter(x=x1, y=uniform_mean, name='RRT*-uniform_' + map_name))
#fig.add_trace(go.Scatter(x=x2, y=roi_mean, name='RRT*-roi_' + map_name))
fig.update_traces(mode='lines')
lineplots.append(fig)
return lineplots


figures = collect_first_best('logs/collected_stats_gan*.csv')
figures.extend(collect_first_best('logs/collected_stats_pix2pix*.csv'))
figures.extend(collect_first_best('logs/gan*.csv', neg='moving_ai'))
figures.extend(collect_first_best('logs/pix2pix*.csv', neg='moving_ai'))
figures.extend(collect_first_best('logs/gan_moving_ai*.csv'))
figures.extend(collect_first_best('logs/pix2pix_moving_ai*.csv'))

lineplots = collect_line_plots('logs/collected_stats_gan*.plot')
lineplots.extend(collect_line_plots('logs/collected_stats_pix2pix*.plot'))
lineplots.extend(collect_line_plots('logs/gan*.plot', neg='moving_ai'))
lineplots.extend(collect_line_plots('logs/pix2pix*.plot', neg='moving_ai'))
lineplots.extend(collect_line_plots('logs/gan_moving_ai*.plot'))
lineplots.extend(collect_line_plots('logs/pix2pix_moving_ai*.plot'))

with open('box_plots.html', 'w') as f:
for fig in figures:
f.write(fig.to_html(full_html=False, include_plotlyjs='cdn'))
with open('line_plots.html', 'w') as f:
for line in lineplots:
f.write(line.to_html(full_html=False, include_plotlyjs='cdn'))


app = dash.Dash(__name__)
app.layout = html.Div([
html.Div([dcc.Graph(figure=fig) for fig in figures]),
html.Div([dcc.Graph(figure=lineplot) for lineplot in lineplots])
])
app.run_server(debug=True)"
4 changes: 3 additions & 1 deletion download.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import argparse
from pathgan.data.utils import download_and_extract


if __name__ == "__main__":
parser = argparse.ArgumentParser(prog = "top", description="Training GAN (from original paper")
parser.add_argument("--url", type=str, help="Url of file.")
parser.add_argument("--root", type=str, default=".", help="Root path.")
parser.add_argument("--root", type=str, default="data", help="Root path.")
parser.add_argument("--filename", type=str, default=None, help="Filname.")
args = parser.parse_args()
os.makedirs(args.root, exist_ok=True)
download_and_extract(args.root, args.url, args.filename)
27 changes: 19 additions & 8 deletions notebooks/analyzes_of_connectivity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"sys.path.append(os.path.dirname(os.getcwd()))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import pandas as pd\n",
"from PathGAN.data.utils import rgb2binary\n",
"from pathgan.utils import rgb2binary\n",
"import matplotlib.pyplot as plt\n",
"from tqdm.notebook import tqdm\n",
"from PIL import Image, ImageDraw\n",
Expand All @@ -19,7 +30,7 @@
"import sys\n",
"from pathlib import Path\n",
"from collections import deque\n",
"from PathGAN.RRT_last import RRT"
"from pathgan.models.rrt import RRT"
]
},
{
Expand All @@ -28,8 +39,8 @@
"metadata": {},
"outputs": [],
"source": [
"dataset_path = './PathGAN/data/dataset/'\n",
"data_path = './PathGAN/data/' "
"dataset_path = 'data/generated_dataset/dataset/'\n",
"data_path = 'data/generated_dataset/'"
]
},
{
Expand Down Expand Up @@ -1140,7 +1151,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -1154,9 +1165,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
34 changes: 21 additions & 13 deletions pathgan/data/mpr_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MPRDataset(Dataset):
Dataframe with map/task/roi pairs.
transform: Callable
Transforms for map/task/roi pairs.
to_binary: bool (default=False)
If to load data (image) as binary tensor.
"""
def __init__(
self,
Expand All @@ -37,14 +39,16 @@ def __init__(
roi_dir: str,
csv_file: pd.DataFrame,
transform: Optional[Callable] = None,
test: bool = False,
return_meta: bool = False,
to_binary: bool = False,
):
self.map_dir = map_dir
self.point_dir = point_dir
self.roi_dir = roi_dir
self.csv_file = csv_file
self.transform = transform
self.test = test
self.return_meta = return_meta
self.to_binary = to_binary

def __len__(self) -> int:
return len(self.csv_file)
Expand All @@ -54,19 +58,23 @@ def __getitem__(self, index: int) -> Dict[str, Any]:
map_name = row["map"].split(".")[0]
map_path = f"{self.map_dir}/{row['map']}"
point_path = f"{self.point_dir}/{map_name}/{row['task']}"
if not self.test:
roi_path = f"{self.roi_dir}/{map_name}/{row['roi']}"
roi_path = f"{self.roi_dir}/{map_name}/{row['roi']}"
meta = {"map_path": map_path, "point_path": point_path, "roi_path": roi_path}

map_img = np.array(Image.open(map_path).convert('RGB'))
point_img = np.array(Image.open(point_path).convert('RGB'))
if not self.test:
roi_img = np.array(Image.open(roi_path).convert('RGB'))
if self.to_binary:
map_img = np.array(Image.open(map_path).convert("L"))
point_img = np.array(Image.open(point_path).convert("L"))
roi_img = np.array(Image.open(roi_path).convert("L"))
else:
map_img = np.array(Image.open(map_path).convert("RGB"))
point_img = np.array(Image.open(point_path).convert("RGB"))
roi_img = np.array(Image.open(roi_path).convert("RGB"))

if self.transform is not None:
map_img = self.transform(map_img)
point_img = self.transform(point_img)
if not self.test:
roi_img = self.transform(roi_img)
if not self.test:
return map_img, point_img, roi_img
return map_img, point_img
roi_img = self.transform(roi_img)

if self.return_meta:
return map_img, point_img, roi_img, meta
return map_img, point_img, roi_img
1 change: 1 addition & 0 deletions pathgan/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .metrics import *
23 changes: 23 additions & 0 deletions pathgan/metrics/metrics.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import numpy as np
import torch
import torch.nn as nn
from torch.nn import functional as F
from torchvision.models import inception_v3
from .functional import kl_divergence, covariance, frechet_distance


def intersection_over_union(roi_pred, roi_true):
roi_pred = np.sum(roi_pred, axis=-1)
roi_pred = (roi_pred < 0.5 * np.max(roi_pred)).astype(int)
roi_true = np.sum(roi_true, axis=-1)
roi_true = (roi_true < 0.5 * np.max(roi_true)).astype(int)
inter = (roi_pred * roi_true).sum()
union = roi_pred.sum() + roi_true.sum()
iou_value = inter / (union - inter + 1e-6)
return iou_value


def jaccard_coefficient(roi_pred, roi_true):
roi_pred = np.sum(roi_pred, axis=-1)
roi_pred = (roi_pred < 0.5 * np.max(roi_pred)).astype(int)
roi_true = np.sum(roi_true, axis=-1)
roi_true = (roi_true < 0.5 * np.max(roi_true)).astype(int)
inter = (roi_pred * roi_true).sum()
union = roi_pred.sum() + roi_true.sum()
dice_value = 2 * inter / (union + 1e-6)
return dice_value


class KLDivergence(nn.Module):
"""Kullback–Leibler divergence."""
def __init__(self,):
Expand Down
4 changes: 2 additions & 2 deletions pathgan/models/rrt/rrt_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class PathDescription(object):
"""
def __init__(
self,
path: List,
path: Optional[List] = None,
cost: float = float('inf'),
time_sec: float = 0.,
time_it: int = 0,
samples_taken: int = 0,
nodes_taken: int = 0,
):
"""Initialize."""
self.path = path
self.path = path if path is not None else []
self.cost = cost
self.time_sec = time_sec
self.time_it = time_it
Expand Down
13 changes: 13 additions & 0 deletions scripts/data/download_datasets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python

echo "Downloading generated dataset..."

python download.py \
--url https://github.com/akanametov/pathgan/releases/download/2.0/dataset.zip \
--root data/generated_dataset \

echo "Downloading movingai dataset..."

python download.py \
--url https://github.com/akanametov/pathgan/releases/download/2.0/movingai_dataset.zip \
--root data/movingai_dataset \
25 changes: 25 additions & 0 deletions scripts/data/download_results.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python

echo "Downloading SAGAN results on generated dataset..."

python download.py \
--url https://github.com/akanametov/pathgan/releases/download/2.0/results.zip \
--root data/generated_dataset \

echo "Downloading SAGAN results on movingai dataset..."

python download.py \
--url https://github.com/akanametov/pathgan/releases/download/2.0/movingai_results.zip \
--root data/movingai_dataset/movingai \

echo "Downloading Pix2pix results on generated dataset..."

python download.py \
--url https://github.com/akanametov/pathgan/releases/download/2.0/pixresults.zip \
--root data/generated_dataset \

echo "Downloading Pix2pix results on movingai dataset..."

python download.py \
--url https://github.com/akanametov/pathgan/releases/download/2.0/movingai_pixresults.zip \
--root data/movingai_dataset/movingai \
12 changes: 12 additions & 0 deletions scripts/logs/get_logs_generated_pix2pix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

echo "Get Pix2pix logs on generated dataset..."

python get_logs.py A \
--map_params "{'data_folder': './data/generated_dataset', 'maps_folder': 'maps', 'results_folder': 'pixresults', 'results_file': 'result.csv'}" \
--rrt_params "{'path_resolution': 1, 'step_len': 2, 'max_iter': 10000}" \
--mu 0.1 \
--gamma 10 \
--n 50 \
--output_dir logs/pix2pix \
--output_fname pix2pix_generated_logs.txt \
12 changes: 12 additions & 0 deletions scripts/logs/get_logs_generated_sagan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

echo "Get SAGAN logs on generated dataset..."

python get_logs.py A \
--map_params "{'data_folder': './data/generated_dataset', 'maps_folder': 'maps', 'results_folder': 'results', 'results_file': 'results.csv'}" \
--rrt_params "{'path_resolution': 1, 'step_len': 2, 'max_iter': 10000}" \
--mu 0.1 \
--gamma 10 \
--n 50 \
--output_dir logs/ \
--output_fname sagan_generated_logs.txt \
12 changes: 12 additions & 0 deletions scripts/logs/get_logs_movingai_pix2pix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

echo "Get Pix2pix logs on movingai dataset..."

python get_logs.py A \
--map_params "{'data_folder': './data/movingai_dataset', 'maps_folder': 'maps', 'results_folder': 'movingai/pixresults', 'results_file': 'pixresult.csv'}" \
--rrt_params "{'path_resolution': 1, 'step_len': 2, 'max_iter': 10000}" \
--mu 0.1 \
--gamma 10 \
--n 50 \
--output_dir logs/pix2pix \
--output_fname pix2pix_movingai_logs.txt \
12 changes: 12 additions & 0 deletions scripts/logs/get_logs_movingai_sagan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

echo "Get SAGAN logs on movingai dataset..."

python get_logs.py A \
--map_params "{'data_folder': './data/movingai_dataset', 'maps_folder': 'maps', 'results_folder': 'movingai/results', 'results_file': 'result.csv'}" \
--rrt_params "{'path_resolution': 1, 'step_len': 2, 'max_iter': 10000}" \
--mu 0.1 \
--gamma 10 \
--n 50 \
--output_dir logs/sagan \
--output_fname sagan_movingai_logs.txt \
Loading