Exemple #1
0
 def setUp(self):
     print('Start test setup')
     asl = AslDb()
     self.training = asl.build_training(FEATURES)
     self.sequences = self.training.get_all_sequences()
     self.xlengths = self.training.get_all_Xlengths()
     print('End test setup')
Exemple #2
0
class TestRecognize(TestCase):
    def setUp(self):
        self.asl = AslDb()
        self.training_set = self.asl.build_training(FEATURES)
        self.test_set = self.asl.build_test(FEATURES)
        self.models = train_all_words(self.training_set, SelectorConstant)

    def test_recognize_probabilities_interface(self):
        probs, _ = recognize(self.models, self.test_set)
        self.assertEqual(
            len(probs), self.test_set.num_items,
            "Number of test items in probabilities list incorrect.")
        self.assertIn(
            'FRANK', probs[0],
            "Dictionary of probabilities does not contain correct keys")
        self.assertIn(
            'CHICKEN', probs[-1],
            "Dictionary of probabilities does not contain correct keys")

    def test_recognize_guesses_interface(self):
        _, guesses = recognize(self.models, self.test_set)
        self.assertEqual(len(guesses), self.test_set.num_items,
                         "Number of test items in guesses list incorrect.")
        self.assertIsInstance(guesses[0], str, "The guesses are not strings")
        self.assertIsInstance(guesses[-1], str, "The guesses are not strings")
class MyDICTest(unittest.TestCase):
    def setUp(self):
        self.asl = AslDb()  # initializes the database
        self.asl.df.head(
        )  # displays the first five rows of the asl database, indexed by video and frame
        self.asl.df['grnd-ry'] = self.asl.df['right-y'] - self.asl.df['nose-y']
        self.asl.df['grnd-rx'] = self.asl.df['right-x'] - self.asl.df['nose-x']
        self.asl.df['grnd-ly'] = self.asl.df['left-y'] - self.asl.df['nose-y']
        self.asl.df['grnd-lx'] = self.asl.df['left-x'] - self.asl.df['nose-x']
        self.features_ground = ['grnd-rx', 'grnd-ry', 'grnd-lx', 'grnd-ly']
        self.words_to_train = ['FISH', 'BOOK', 'VEGETABLE', 'FUTURE', 'JOHN']
        self.training = self.asl.build_training(
            self.features_ground
        )  # Experiment here with different feature sets defined in part 1

    def test_something(self):
        sequences = self.training.get_all_sequences()
        Xlengths = self.training.get_all_Xlengths()
        for word in self.words_to_train:
            start = timeit.default_timer()
            model = my_model_selectors.SelectorDIC(sequences,
                                                   Xlengths,
                                                   word,
                                                   min_n_components=2,
                                                   max_n_components=15,
                                                   random_state=14).select()
            end = timeit.default_timer() - start
            if model is not None:
                print(
                    "Training complete for {} with {} states with time {} seconds"
                    .format(word, model.n_components, end))
            else:
                print("Training failed for {}".format(word))
class TestRecognize(TestCase):
    def setUp(self):
        self.asl = AslDb()
        self.training_set = self.asl.build_training(FEATURES)
        self.test_set = self.asl.build_test(FEATURES)
        self.models = train_all_words(self.training_set, SelectorConstant)

    def test_recognize_probabilities_interface(self):
        probs, _ = recognize(self.models, self.test_set)
        self.assertEqual(len(probs), self.test_set.num_items, "Number of test items in probabilities list incorrect.")
        self.assertIn('FRANK', probs[0], "Dictionary of probabilities does not contain correct keys")
        self.assertIn('CHICKEN', probs[-1], "Dictionary of probabilities does not contain correct keys")

    def test_recognize_guesses_interface(self):
        _, guesses = recognize(self.models, self.test_set)
        self.assertEqual(len(guesses), self.test_set.num_items, "Number of test items in guesses list incorrect.")
        self.assertIsInstance(guesses[0], str, "The guesses are not strings")
        self.assertIsInstance(guesses[-1], str, "The guesses are not strings")
    # test the code
    test_features_tryit(asl)

    # In[50]:

    # collect the features into a list
    features_ground = ['grnd-rx', 'grnd-ry', 'grnd-lx', 'grnd-ly']
    #show a single set of features for a given (video, frame) tuple
    [asl.df.ix[98, 1][v] for v in features_ground]

    # ##### Build the training set
    # Now that we have a feature list defined, we can pass that list to the `build_training` method to collect the features for all the words in the training set.  Each word in the training set has multiple examples from various videos.  Below we can see the unique words that have been loaded into the training set:

    # In[51]:

    training = asl.build_training(features_ground)
    print("Training words: {}".format(training.words))

    # The training data in `training` is an object of class `WordsData` defined in the `asl_data` module.  in addition to the `words` list, data can be accessed with the `get_all_sequences`, `get_all_Xlengths`, `get_word_sequences`, and `get_word_Xlengths` methods. We need the `get_word_Xlengths` method to train multiple sequences with the `hmmlearn` library.  In the following example, notice that there are two lists; the first is a concatenation of all the sequences(the X portion) and the second is a list of the sequence lengths(the Lengths portion).

    # In[52]:

    training.get_word_Xlengths('CHOCOLATE')

    # ###### More feature sets
    # So far we have a simple feature set that is enough to get started modeling.  However, we might get better results if we manipulate the raw values a bit more, so we will go ahead and set up some other options now for experimentation later.  For example, we could normalize each speaker's range of motion with grouped statistics using [Pandas stats](http://pandas.pydata.org/pandas-docs/stable/api.html#api-dataframe-stats) functions and [pandas groupby](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html).  Below is an example for finding the means of all speaker subgroups.

    # In[53]:

    df_means = asl.df.groupby('speaker').mean()
    df_means
Exemple #6
0
 def setUp(self):
     asl = AslDb()
     self.training = asl.build_training(FEATURES)
     self.sequences = self.training.get_all_sequences()
     self.xlengths = self.training.get_all_Xlengths()
Exemple #7
0
)  # displays the first five rows of the asl database, indexed by video and frame
asl.df.ix[98, 1]  # look at the data available for an individual frame
asl.df['grnd-ry'] = asl.df['right-y'] - asl.df['nose-y']
asl.df.head()  # the new feature 'grnd-ry' is now in the frames dictionary

from asl_utils import test_features_tryit
# TODO add df columns for 'grnd-rx', 'grnd-ly', 'grnd-lx' representing differences between hand and nose locations

# test the code
test_features_tryit(asl)
# collect the features into a list
features_ground = ['grnd-rx', 'grnd-ry', 'grnd-lx', 'grnd-ly']
#show a single set of features for a given (video, frame) tuple
[asl.df.ix[98, 1][v] for v in features_ground]

training = asl.build_training(features_ground)
print("Training words: {}".format(training.words))

training.get_word_Xlengths('CHOCOLATE')

df_means = asl.df.groupby('speaker').mean()
df_means
asl.df['left-x-mean'] = asl.df['speaker'].map(df_means['left-x'])
asl.df.head()

from asl_utils import test_std_tryit
# TODO Create a dataframe named `df_std` with standard deviations grouped by speaker

# test the code
test_std_tryit(df_std)
Exemple #8
0
 def setUp(self):
     asl = AslDb()
     self.training = asl.build_training(FEATURES)
     self.sequences = self.training.get_all_sequences()
     self.xlengths = self.training.get_all_Xlengths()
Exemple #9
0
                                 'BOOK').select()
        self.assertGreaterEqual(model.n_components, 2)

    #def test_select_bic_interface(self):
    #    model = SelectorBIC(self.sequences, self.xlengths, 'FRANK').select()
    #    self.assertGreaterEqual(model.n_components, 2)
    #    model = SelectorBIC(self.sequences, self.xlengths, 'VEGETABLE').select()
    #    self.assertGreaterEqual(model.n_components, 2)

    def test_select_cv_interface(self):
        model = SelectorCV(self.sequences, self.xlengths, 'JOHN').select()
        self.assertGreaterEqual(model.n_components, 2)
        model = SelectorCV(self.sequences, self.xlengths, 'CHICKEN').select()
        self.assertGreaterEqual(model.n_components, 2)

    #def test_select_dic_interface(self):
    #    model = SelectorDIC(self.sequences, self.xlengths, 'MARY').select()
    #    self.assertGreaterEqual(model.n_components, 2)
    #    model = SelectorDIC(self.sequences, self.xlengths, 'TOY').select()
    #    self.assertGreaterEqual(model.n_components, 2)


if __name__ == '__main__':
    asl = AslDb()
    training = asl.build_training(FEATURES)
    sequences = training.get_all_sequences()
    xlengths = training.get_all_Xlengths()

    #SelectorCV(sequences, xlengths, 'JOHN').select()
    SelectorDIC(sequences, xlengths, 'FRANK').select()
def init():

    asl = AslDb()
    #dimensions
    hand = ['right', 'left']
    side = ['r', 'l']
    cartesian = ['x', 'y']
    polar = ['r', 'theta']

    #rename the raw data for consistency
    raw_names = {
        h + '-' + c: 'raw-' + h[0] + c
        for h in hand for c in cartesian
    }
    asl.df = asl.df.rename(columns=raw_names)

    cartesian_features = ['grnd', 'norm', 'delta']
    features = {
        k: [k + '-' + h[0] + c for h in hand for c in cartesian]
        for k in cartesian_features
    }

    features['polar'] = ['polar' + '-' + s + c for s in side for c in polar]
    #derive the features
    for f in features['grnd']:
        asl.df[f] = asl.df['raw' + f[-3:]] - asl.df['nose-' + f[-1:]]

    df_means = asl.df.groupby('speaker').mean()
    df_std = asl.df.groupby('speaker').std()

    for f in features['norm']:
        ref = 'raw' + f[-3:]
        asl.df[f] = (asl.df[ref] - asl.df['speaker'].map(
            df_means[ref])) / asl.df['speaker'].map(df_std[ref])

    for f in features['delta']:
        ref = 'grnd' + f[-3:]
        asl.df[f] = (asl.df[ref].diff()).fillna(0)

    ref = 'grnd'
    asl.df['polar-rtheta'] = (np.arctan2(asl.df[ref + '-rx'],
                                         asl.df[ref + '-ry']))
    asl.df['polar-ltheta'] = (np.arctan2(asl.df[ref + '-lx'],
                                         asl.df[ref + '-ly']))
    asl.df['polar-rr'] = np.sqrt(asl.df[ref + '-rx']**2 +
                                 asl.df[ref + '-ry']**2)
    asl.df['polar-lr'] = np.sqrt(asl.df[ref + '-lx']**2 +
                                 asl.df[ref + '-ly']**2)
    training = {k: asl.build_training(v) for k, v in features.items()}

    xlens = training['grnd'].get_all_Xlengths()
    lens_stats = [(k, len(v[1]), min(v[1]), sum(v[1]) / len(v[1]), max(v[1]),
                   max(v[1]) - min(v[1])) for k, v in xlens.items()]
    words_stats = pd.DataFrame.from_records(
        lens_stats, columns=['word', 'count', 'min', 'avg', 'max',
                             'range']).set_index('word')
    words_stats['spread'] = words_stats['range'] / words_stats['avg']

    #include all words
    min_len = 0
    words = words_stats[words_stats['min'] > min_len].sort_values(
        by='count', ascending=False).index.tolist()

    samples = dict()
    for f in features:
        samples[f] = {k: get_word(training, features, f, k) for k in words}

    threshold = 1e-9
    separated = {
        k: ([s for s in v if min(s.std()) < threshold],
            [s for s in v if min(s.std()) > threshold])
        for k, v in samples['norm'].items()
    }
    separated_stats = pd.DataFrame.from_records(
        {k: (len(v[0]), len(v[1]))
         for k, v in separated.items()}).T.rename(columns={
             0: 'single',
             1: 'double'
         })
    return asl, features, training, samples, words_stats.join(
        separated_stats), separated
for i, feature in ground_index:
    feature_mean = asl.df['speaker'].map(df_means[feature])
    feature_std = asl.df['speaker'].map(df_std[feature])
    asl.df[features_custom[i]] = (asl.df[feature] - feature_mean) / feature_std

print("Top rows of data:\n\n{}".format(asl.df.head()))

all_selectors = [SelectorBIC, SelectorDIC, SelectorCV]
feature_sets = [features_ground, features_polar, features_custom]

warnings.filterwarnings("ignore")

test_SLM = BasicSLM("SLM_data/corpus_sentences.txt", verbose=False)
feature_set = features_custom
selector = SelectorCV
training_set = asl.build_training(feature_set)
testing_set = asl.build_test(feature_set)
train_words = training_set.words
test_words = testing_set.wordlist
#train_words   = ['FISH', 'BOOK', 'VEGETABLE']
#test_words    = ['FISH', 'BOOK', 'VEGETABLE']
sentences = testing_set.sentences_index
sentences = [sentences[i] for i in sentences]

models_dict = train_all_words(training_set,
                              selector,
                              train_words,
                              verbose=False,
                              features=feature_set)

test_probs, test_guesses = recognize_words(models_dict,