Commit 38ceb433 authored by lucas_miranda's avatar lucas_miranda
Browse files

Added notebook with empirically derived radius for latent neighborhood radius

parent e863c9bb
%% Cell type:code id: tags:
``` python
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
""")
```
%% Output
<IPython.core.display.HTML object>
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Output
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
%% Cell type:code id: tags:
``` python
import os
os.chdir(os.path.dirname("../"))
```
%% Cell type:code id: tags:
``` python
import deepof.data
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from deepof.models import *
from scipy.optimize import curve_fit
from scipy.stats import linregress
from sklearn.metrics import pairwise_distances
from tqdm import tqdm
```
%% Cell type:markdown id: tags:
# Tuning of latent space entropy radius
For
%% Cell type:code id: tags:
``` python
data_path = "../../Desktop/deepoftesttemp/"
```
%% Cell type:code id: tags:
``` python
# Load data and tag a few test videos
proj = deepof.data.project(path=data_path, arena_dims=[380]).run()
rules = proj.rule_based_annotation()
X_train, y_train, X_val, y_val = proj.get_coords(propagate_annotations=False).preprocess(test_videos=0)
```
%% Output
Loading trajectories...
Smoothing trajectories...
Interpolating outliers...
Iterative imputation of ocluded bodyparts...
Computing distances...
0%| | 0/2 [00:00<?, ?it/s]
Computing angles...
Done!
100%|██████████| 2/2 [00:06<00:00, 3.00s/it]
%% Cell type:code id: tags:
``` python
#Load the models, and try different radii
# each dataset is rank 3: encoding dimensions, number of clusters, and different radii
x, y = np.zeros([6, 6, 100]), np.zeros([6, 6, 100])
# Iterate over encoding dimensions
for a,d in enumerate(tqdm((2, 4, 6, 8, 10, 12))):
for b, k in enumerate(tqdm((1,5,10,15,20,25))):
gmvaep = SEQ_2_SEQ_GMVAE(encoding=d, number_of_components=k).build(X_train.shape)[3]
# Get encoer and grouper from full model
cluster_means = [
layer for layer in gmvaep.layers if layer.name == "latent_distribution"
][0]
cluster_assignment = [
layer for layer in gmvaep.layers if layer.name == "cluster_assignment"
][0]
encoder = tf.keras.models.Model(
gmvaep.layers[0].input, cluster_means.output
)
grouper = tf.keras.models.Model(
gmvaep.layers[0].input, cluster_assignment.output
)
# Use encoder and grouper to predict on validation data
encoding = encoder.predict(X_train)
groups = grouper.predict(X_train)
pdist = pairwise_distances(encoding)
for i, r in enumerate(np.linspace(0, 5, 100)):
x[a][b][i], y[a][b][i] = np.round(r, 7), np.median(np.sum(pdist < r, axis=0))
```
%% Output
0%| | 0/6 [00:00<?, ?it/s]
0%| | 0/6 [00:00<?, ?it/s]
17%|█▋ | 1/6 [02:44<13:41, 164.39s/it]
33%|███▎ | 2/6 [05:58<11:32, 173.16s/it]
50%|█████ | 3/6 [08:31<08:21, 167.33s/it]
67%|██████▋ | 4/6 [10:58<05:22, 161.26s/it]
83%|████████▎ | 5/6 [13:25<02:36, 156.79s/it]
100%|██████████| 6/6 [15:57<00:00, 159.60s/it]
17%|█▋ | 1/6 [15:57<1:19:48, 957.61s/it]
0%| | 0/6 [00:00<?, ?it/s]
17%|█▋ | 1/6 [02:25<12:05, 145.12s/it]
33%|███▎ | 2/6 [04:40<09:28, 142.20s/it]
50%|█████ | 3/6 [06:57<07:01, 140.64s/it]
67%|██████▋ | 4/6 [09:21<04:43, 141.65s/it]
83%|████████▎ | 5/6 [11:46<02:22, 142.77s/it]