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

Draft: Port HMM to pomegranate 1.0 #20

Open
wants to merge 4 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
ax2.plot(hidden_state_sequence_feature_space, color="tab:green")
ax2.tick_params(axis="y", labelcolor="tab:green")

plt.xlim([0, 500])
# plt.xlim([0, 500])
fig.tight_layout()
plt.show()

Expand Down
13 changes: 3 additions & 10 deletions examples/stride_segmentation/segmentation_hmm_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,19 @@
stride_model = SimpleHmm(
n_states=20,
n_gmm_components=6,
algo_train="baum-welch",
stop_threshold=1e-9,
max_iterations=5,
architecture="left-right-strict",
verbose=True,
name="stride_model",
)

transition_model = SimpleHmm(
n_states=5,
n_gmm_components=3,
algo_train="baum-welch",
stop_threshold=1e-9,
max_iterations=5,
architecture="left-right-loose",
verbose=True,
name="transition_model",
)

# %%
Expand All @@ -121,13 +117,10 @@
stride_model=stride_model,
transition_model=transition_model,
feature_transform=feature_transform,
algo_predict="viterbi",
algo_train="baum-welch",
stop_threshold=1e-9,
max_iterations=1,
initialization="labels",
verbose=True,
name="segmentation_model",
)

# %%
Expand Down Expand Up @@ -170,9 +163,9 @@

np.set_printoptions(precision=3, linewidth=180, suppress=True)

print(segmentation_model.model.dense_transition_matrix()[0:-2, 0:-2])
print(np.e**segmentation_model.model.edges)

print(segmentation_model.model.states[10])
print(segmentation_model.model.distributions[segmentation_model.n_states - 1])

# %%
# Applying the Model to a Sequence
Expand Down Expand Up @@ -208,6 +201,6 @@
axs[1].set_ylabel("Hidden State [N]")

axs[1].set_xlabel("Samples @ %d Hz" % sampling_rate_hz)
plt.xlim([6000, 7200])
# plt.xlim([6000, 7200])
fig.tight_layout()
plt.show()
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""HMM based stride segmentation by Roth et al. 2021."""
from contextlib import suppress
from importlib.resources import open_text
from pathlib import Path
from typing import Dict, Generic, Optional, TypeVar, Union

import numpy as np
Expand Down Expand Up @@ -39,13 +37,13 @@ class PreTrainedRothSegmentationModel(RothSegmentationHmm):

"""

def __new__(cls):
# try to load models
with open_text(
"gaitmap_mad.stride_segmentation.hmm._pre_trained_models", "fallriskpd_at_lab_model.json"
) as test_data, Path(test_data.name).open(encoding="utf8") as f:
model_json = f.read()
return RothSegmentationHmm.from_json(model_json)
# def __new__(cls):
# # try to load models
# with open_text(
# "gaitmap_mad.stride_segmentation.hmm._pre_trained_models", "fallriskpd_at_lab_model.json"
# ) as test_data, Path(test_data.name).open(encoding="utf8") as f:
# model_json = f.read()
# return RothSegmentationHmm.from_json(model_json)


BaseSegmentationHmmT = TypeVar("BaseSegmentationHmmT", bound=BaseSegmentationHmm)
Expand Down
Loading