def predict_LSTM(self, id):
        test_data = [self.data[id]]
        X_signal, Y = get_signal_features(test_data)
        X = X_signal

        skip = self.md.LSTM_skip
        timestamps = self.data[id].signal_bundle.timestamps
        window = self.md.LSTM_window
        batch_stride = self.md.LSTM_batch_stride
        batch_size = self.md.LSTM_batch_size
        sg = SeqGen(X[0:-1:skip],
                    timestamps[0:-1:skip],
                    batch_size,
                    window,
                    batch_stride=batch_stride)

        predictions = np.array([]).reshape(0, len(self.md.label_names_df))
        predictions_timestamps = np.array([])

        for ii in range(len(sg)):
            predictions = np.append(predictions,
                                    self.model_LSTM.predict(
                                        sg[ii][0], batch_size=batch_size),
                                    axis=0)
            predictions_timestamps = np.append(predictions_timestamps,
                                               sg[ii][1],
                                               axis=0)

        predictions_LSTM_only = sortify(predictions,
                                        predictions_timestamps.tolist())

        predict_df = pd.DataFrame(predictions, columns=self.md.label_names_df)
        predict_list = list(predict_df.idxmax(axis=1))

        predict_list_sorted = sortify(predict_list,
                                      predictions_timestamps.tolist())

        predict_list_sorted_LSTM = upsample_labels(predict_list_sorted,
                                                   predictions_timestamps,
                                                   timestamps)
        predictions_LSTM_upsampled = upsample_labels(predictions_LSTM_only,
                                                     predictions_timestamps,
                                                     timestamps)

        return predict_list_sorted_LSTM, predictions_LSTM_upsampled
from keras.models import load_model
import cPickle as pickle

if __name__ == "__main__":

    epochs = 100
    continue_training = False

    data = load_db('grab_objects', path='./')
    data = relabel_data(data, label_map)
    training_data = data[0:10]
    test_data = data[10:-2]

    features = (0, 8)

    trainX_signal, trainY = get_signal_features(training_data)
    trainX_wavelet, _ = get_wavelet_features(training_data, features=features)
    trainX = np.append(trainX_signal, trainX_wavelet, axis=1)

    train_sample_weights = compute_sample_weight('balanced', trainY)
    Y_train_one_hot = pd.get_dummies(trainY)

    testX_signal, testY = get_signal_features(test_data)
    testX_wavelet, _ = get_wavelet_features(test_data, features=features)
    testX = np.append(testX_signal, testX_wavelet, axis=1)

    test_sample_weights = compute_sample_weight('balanced', testY)
    Y_test_one_hot = pd.get_dummies(testY)

    label_names_df = list(Y_train_one_hot.columns.values)
Ejemplo n.º 3
0
def recognize_ld(ld):
    data = [ld]
    timestamps = np.array(ld.signal_bundle.timestamps)

    ####################################################################################################################
    ##  Wavelet Recognition  ###########################################################################################
    ####################################################################################################################

    # estimation
    # training data
    features = (0, 5)
    X_signal, _ = get_signal_features(data)
    X_wavelet, _ = get_wavelet_features(data, features=features)
    X = np.append(X_signal, X_wavelet, axis=1)

    # Loading and configuring model parameters
    window = 100
    batch_stride = 1
    batch_size = 1
    num_classes = len(label_names_df)
    data_dim = np.shape(X)[1]

    model_LSTM_saved = load_model(
        "./trained_models/LSTM_wavelet_100ep_5feat.hdf5")
    model_LSTM = LSTM_model(data_dim,
                            window,
                            batch_size,
                            num_classes,
                            stateful=False)
    model_LSTM.set_weights(model_LSTM_saved.get_weights())

    # Creating data blocks for training the model
    skip = 100
    sg = SeqGen(X[0:-1:skip], timestamps[0:-1:skip], 1, window, batch_stride=1)

    predictions = np.array([]).reshape(0, len(label_names_df))
    predictions_timestamps = np.array([])
    for ii in range(len(sg)):
        predictions = np.append(predictions,
                                model_LSTM.predict(sg[ii][0],
                                                   batch_size=batch_size),
                                axis=0)
        predictions_timestamps = np.append(predictions_timestamps,
                                           sg[ii][1],
                                           axis=0)

    predictions_wavelets = sortify(predictions,
                                   predictions_timestamps.tolist())

    predict_df = pd.DataFrame(predictions, columns=label_names_df)
    predict_list = list(predict_df.idxmax(axis=1))

    predict_list_sorted = sortify(predict_list,
                                  predictions_timestamps.tolist())
    predict_list_sorted_wavelet = upsample_labels(predict_list_sorted,
                                                  predictions_timestamps,
                                                  timestamps)

    ####################################################################################################################
    ##  LSTM_only Recognition  #########################################################################################
    ####################################################################################################################

    X_signal, Y = get_signal_features(data)
    X = X_signal

    window = 100
    batch_stride = 1
    batch_size = 1
    num_classes = len(label_names_df)
    data_dim = np.shape(X)[1]

    model_LSTM_saved = load_model("./trained_models/LSTM_only.hdf5")
    model_LSTM = LSTM_model(data_dim,
                            window,
                            batch_size,
                            num_classes,
                            stateful=False)
    model_LSTM.set_weights(model_LSTM_saved.get_weights())

    skip = 100

    sg = SeqGen(X[0:-1:skip], timestamps[0:-1:skip], 1, window, batch_stride=1)

    predictions = np.array([]).reshape(0, len(label_names_df))
    predictions_timestamps = np.array([])

    for ii in range(len(sg)):
        predictions = np.append(predictions,
                                model_LSTM.predict(sg[ii][0],
                                                   batch_size=batch_size),
                                axis=0)
        predictions_timestamps = np.append(predictions_timestamps,
                                           sg[ii][1],
                                           axis=0)

    predictions_LSTM_only = sortify(predictions,
                                    predictions_timestamps.tolist())

    predict_df = pd.DataFrame(predictions, columns=label_names_df)
    predict_list = list(predict_df.idxmax(axis=1))

    predict_list_sorted = sortify(predict_list,
                                  predictions_timestamps.tolist())

    predict_list_sorted_LSTM_only = upsample_labels(predict_list_sorted,
                                                    predictions_timestamps,
                                                    timestamps)

    ####################################################################################################################
    ##  Wavelet only Recognition  ######################################################################################
    ####################################################################################################################

    # features = (0, 8)
    # X_signal, _ = get_signal_features(data)
    # X_wavelet, _ = get_wavelet_features(data, features=features)
    # X = np.append(X_signal, X_wavelet, axis=1)
    # data_dim = np.shape(X)[1]
    # num_classes = len(label_names_df)
    #
    # model_saved = load_model("./trained_models/wavelet_10ep_8feat.hdf5")
    # model = MLP_model(data_dim, num_classes)
    # model.set_weights(model_saved.get_weights())
    #
    # predictions_wavelet_only = model.predict(X[0:-1:skip])
    #
    # predict_df = pd.DataFrame(predictions_wavelet_only, columns=label_names_df)
    # predict_wavelets = list(predict_df.idxmax(axis=1))
    #
    # predict_wavelets = upsample_labels(predict_wavelets, predictions_timestamps, timestamps)

    ####################################################################################################################
    ##  Combined Recognition  ##########################################################################################
    ####################################################################################################################

    # predictions_combined = np.array(predictions_wavelets) * np.array(predictions_LSTM_only) \
    #                        * np.array(predictions_wavelet_only)
    predictions_combined = np.array(predictions_wavelets) * np.array(
        predictions_LSTM_only)

    predict_df = pd.DataFrame(predictions_combined, columns=label_names_df)
    predict_list_combined = list(predict_df.idxmax(axis=1))

    predict_list_combined = upsample_labels(predict_list_combined,
                                            predictions_timestamps, timestamps)

    # return predict_list_combined, predict_list_sorted_wavelet, predict_list_sorted_LSTM_only, predict_wavelets
    return predict_list_combined, predict_list_sorted_wavelet, predict_list_sorted_LSTM_only
Ejemplo n.º 4
0
import cPickle as pickle

if __name__ == "__main__":
    # parser = argparse.ArgumentParser()
    # parser.add_argument('epochs')

    epochs = 100
    continue_training = False

    data = load_db('grab_objects', path='./')
    data = relabel_data(data, label_map)
    training_data = data[0:10]
    test_data = data[10:-2]
    validation_data = data[-2:]

    trainX, trainY = get_signal_features(training_data)
    train_sample_weights = compute_sample_weight('balanced', trainY)
    Y_train_one_hot = pd.get_dummies(trainY)

    testX, testY = get_signal_features(test_data)
    test_sample_weights = compute_sample_weight('balanced', testY)
    Y_test_one_hot = pd.get_dummies(testY)

    validationX, validationY = get_signal_features(validation_data)
    validation_sample_weights = compute_sample_weight('balanced', validationY)
    Y_validation_one_hot = pd.get_dummies(validationY)

    label_names_df = list(Y_train_one_hot.columns.values)

    pickle.dump(label_names_df, open("label_names_df.pkl", "wb"))
Ejemplo n.º 5
0
    wrench2 = np.array([
        np.interp(timesamples, wrencht2, wrench2[:, ii]) for ii in range(6)
    ]).transpose()

    data = np.transpose(np.append(wrench1, wrench2, axis=1)).tolist()

    #addings stuff to the database
    sb = SignalBundle(data, timesamples)
    ld = LabeledData(sb)
    data = [ld]

    timestamps = np.array(ld.signal_bundle.timestamps)

    # estimation
    features = (0, 5)
    X_signal, _ = get_signal_features(data)
    X_wavelet, _ = get_wavelet_features(data, features=features)
    X = np.append(X_signal, X_wavelet, axis=1)

    model_LSTM_saved = load_model(
        "./trained_models/LSTM_wavelet_100ep_5feat.hdf5")

    window = 100
    batch_stride = 1
    batch_size = 1
    num_classes = len(label_names_df)
    data_dim = np.shape(X)[1]

    model_LSTM = LSTM_model(data_dim,
                            window,
                            batch_size,
    def train_LSTM_wavelet(self,
                           ids,
                           valid_ids=None,
                           batch_size=1,
                           epochs=30,
                           skip=100,
                           features=(0, 5),
                           batch_stride=1,
                           window=100,
                           timesteps=100):

        self.md.LSTM_wavelet_batch_size = batch_size
        self.md.LSTM_wavelet_timesteps = timesteps
        self.md.LSTM_wavelet_epochs = epochs
        self.md.LSTM_wavelet_skip = skip
        self.md.LSTM_wavelet_features = features
        self.md.LSTM_wavelet_batch_stride = batch_stride
        self.md.LSTM_wavelet_window = window

        training_data = [self.data[id] for id in ids]
        trainX_signal, trainY = get_signal_features(training_data)

        if not self.wavelet_tf_train_completed:
            self.trainX_wavelet, _ = get_wavelet_features(training_data,
                                                          features=features)

        trainX = np.append(trainX_signal, self.trainX_wavelet, axis=1)

        train_sample_weights = compute_sample_weight('balanced', trainY)
        Y_train_one_hot = pd.get_dummies(trainY)

        if self.md.label_names_df is None:
            self.md.label_names_df = list(Y_train_one_hot.columns.values)

        data_dim = np.shape(trainX)[1]

        self.md.LSTM_wavelet_data_dim = data_dim

        num_classes = len(self.md.label_names_df)

        self.model_LSTM_wavelet = LSTM_model(data_dim,
                                             timesteps,
                                             batch_size,
                                             num_classes,
                                             stateful=False)

        trainX_sampled = trainX[0:-1:skip]
        trainY_one_hot_sampled = Y_train_one_hot[0:-1:skip]
        sw_train_sampled = train_sample_weights[0:-1:skip]

        sg_train = SeqGen(trainX_sampled,
                          trainY_one_hot_sampled,
                          batch_size,
                          window,
                          batch_stride=batch_stride,
                          sample_weights=sw_train_sampled)

        ## Training
        tb = TensorBoard(log_dir='./tensorboard_logs/LSTM_wavelets',
                         histogram_freq=0,
                         write_graph=True,
                         write_images=True)

        if valid_ids is None:
            self.model_LSTM_wavelet.fit_generator(sg_train,
                                                  epochs=epochs,
                                                  callbacks=[tb])
        else:
            validation_data = [self.data[id] for id in valid_ids]
            validationX_signal, validationY = get_signal_features(
                validation_data)
            if not self.wavelet_tf_validation_completed:
                self.validationX_wavelet, _ = get_wavelet_features(
                    validation_data, features=features)

            validation_sample_weights = compute_sample_weight(
                'balanced', validationY)
            Y_validation_one_hot = pd.get_dummies(validationY)

            validationX = np.append(validationX_signal,
                                    self.validationX_wavelet,
                                    axis=1)

            validationX_sampled = validationX[0:-1:skip]
            validationY_one_hot_sampled = Y_validation_one_hot[0:-1:skip]
            sw_validation_sampled = validation_sample_weights[0:-1:skip]

            sg_validation = SeqGen(validationX_sampled,
                                   validationY_one_hot_sampled,
                                   batch_size,
                                   window,
                                   batch_stride=batch_stride,
                                   sample_weights=sw_validation_sampled)

            self.model_LSTM_wavelet.fit_generator(
                sg_train,
                validation_data=sg_validation,
                epochs=epochs,
                callbacks=[tb])

        self.md.LSTM_wavelet_trained = True
        print "Trained LSTM + wavelets model!"