{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "53f57e68", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "import pickle\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "b240abfb", "metadata": {}, "outputs": [], "source": [ "delim = ';'\n", "\n", "user_count = 100\n", "\n", "base_path = '/opt/iui-datarelease1-sose2021/'\n", "\n", "Xpickle_file = './X.pickle'\n", "\n", "ypickle_file = './y.pickle'" ] }, { "cell_type": "code", "execution_count": 4, "id": "f5464e9e", "metadata": {}, "outputs": [], "source": [ "def load_pickles():\n", " _p = open(Xpickle_file, 'rb')\n", " X = pickle.load(_p)\n", " _p.close()\n", " \n", " _p = open(ypickle_file, 'rb')\n", " y = pickle.load(_p)\n", " _p.close()\n", " \n", " return (np.asarray(X, dtype=pd.DataFrame), np.asarray(y, dtype=str))" ] }, { "cell_type": "code", "execution_count": 5, "id": "3c51ef28", "metadata": {}, "outputs": [], "source": [ "def load_data():\n", " if os.path.isfile(Xpickle_file) and os.path.isfile(ypickle_file):\n", " return load_pickles()\n", " data = []\n", " label = []\n", " for user in range(0, user_count):\n", " user_path = base_path + str(user) + '/split_letters_csv/'\n", " for file in os.listdir(user_path):\n", " file_name = user_path + file\n", " letter = ''.join(filter(lambda x: x.isalpha(), file))[0]\n", " data.append(pd.read_csv(file_name, delim))\n", " label.append(letter)\n", " return (np.asarray(data, dtype=pd.DataFrame), np.asarray(label, dtype=str))" ] }, { "cell_type": "code", "execution_count": 6, "id": "03ce941d", "metadata": {}, "outputs": [], "source": [ "X, y = load_data()" ] }, { "cell_type": "code", "execution_count": 7, "id": "7ea0c433", "metadata": {}, "outputs": [], "source": [ "def save_pickle():\n", "# _p = open(np.asarray(data, dtype=pd.DataFrame), 'wb')\n", " _p = open(Xpickle_file, 'wb')\n", " pickle.dump(X, _p)\n", " _p.close()\n", "\n", "# _p = open(np.asarray(label, dtype=str), 'wb')\n", " _p = open(ypickle_file, 'wb')\n", " pickle.dump(y, _p)\n", " _p.close()" ] }, { "cell_type": "code", "execution_count": 8, "id": "210a3d37", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(13102, 13102)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(X), len(y)" ] }, { "cell_type": "code", "execution_count": 51, "id": "eadec7ee", "metadata": {}, "outputs": [], "source": [ "def plot_data(data):\n", " fig, axs = plt.subplots(4, 3, figsize=(3*3, 3*4))\n", " t = data['Millis']\n", " axs[0][0].plot(t, data['Acc1 X'])\n", " axs[0][1].plot(t, data['Acc1 Y'])\n", " axs[0][2].plot(t, data['Acc1 Z'])\n", " axs[1][0].plot(t, data['Acc2 X'])\n", " axs[1][1].plot(t, data['Acc2 Y'])\n", " axs[1][2].plot(t, data['Acc2 Z'])\n", " axs[2][0].plot(t, data['Gyro X'])\n", " axs[2][1].plot(t, data['Gyro Y'])\n", " axs[2][2].plot(t, data['Gyro Z'])\n", " axs[3][0].plot(t, data['Mag X'])\n", " axs[3][1].plot(t, data['Mag Y'])\n", " axs[3][2].plot(t, data['Mag Z'])\n", "\n", " for a in axs:\n", " for b in a:\n", " b.plot(t, data['Force'])\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "ade23f06", "metadata": {}, "outputs": [], "source": [ "# FIRST CELL: set these variables to limit GPU usage.\n", "import os\n", "os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' # this is required\n", "os.environ['CUDA_VISIBLE_DEVICES'] = '2' # set to '0' for GPU0, '1' for GPU1 or '2' for GPU2. Check \"gpustat\" in a terminal." ] }, { "cell_type": "code", "execution_count": 57, "id": "d5db75fc", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import LabelEncoder\n", "\n", "le = LabelEncoder()\n", "yyt_filtered = le.fit_transform(yy_filtered) # Lables in Zahlenwerte transformiert\n", "XX_filtered = np.asarray(XX_filtered).astype('float64')\n", "XXX_filtered = np.delete(np.delete(XX_filtered, 0, 2), 13,2) # Drops time col and Millis col" ] }, { "cell_type": "code", "execution_count": 59, "id": "290be797", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(10271, 456, 13)\n", "(2568, 456, 13)\n", "(10271,)\n", "(2568,)\n" ] } ], "source": [ "X_train, X_test, y_train, y_test = train_test_split(XXX_filtered, yyt_filtered, test_size=0.2, random_state=177013)\n", "\n", "print(X_train.shape)\n", "print(X_test.shape)\n", "print(y_train.shape)\n", "print(y_test.shape)" ] }, { "cell_type": "code", "execution_count": 77, "id": "cf763407", "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv1D, MaxPooling1D\n", "\n", "model = Sequential()\n", "\n", "# model.add(Conv1D(32, 3, input_shape = X_train.shape[1:]))\n", "# model.add(Activation('relu'))\n", "# model.add(MaxPooling1D(pool_size=3))\n", "\n", "# model.add(Conv1D(32, 3))\n", "# model.add(Activation('relu'))\n", "# model.add(MaxPooling1D(pool_size=3))\n", "\n", "model.add(Flatten(input_shape = (456,13)))\n", "model.add(Dense(456, activation = 'relu'))\n", "\n", "model.add(Dense(104))\n", "\n", "model.add(Dense(26))\n", "model.add(Activation('sigmoid'))\n", "\n", "model.compile(\n", " optimizer = tf.keras.optimizers.Adam(0.001),\n", " loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True),\n", " metrics = [tf.keras.metrics.SparseCategoricalAccuracy()],\n", ")" ] }, { "cell_type": "code", "execution_count": 78, "id": "ed97582c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['K', 'T', 'U', ..., 'F', 'H', 'G'], dtype='" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X_train, y_train, \n", " epochs=10,\n", " batch_size=32,\n", " verbose=1\n", " )" ] }, { "cell_type": "code", "execution_count": 81, "id": "8f8fedfd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Evaluate on test data\n", "81/81 [==============================] - 0s 1ms/step - loss: 11.4346 - sparse_categorical_accuracy: 0.0312\n", "test loss, test acc: [11.434555053710938, 0.031152648851275444]\n", "Generate predictions for 3 samples\n", "predictions shape: (3, 26)\n" ] }, { "data": { "text/plain": [ "(array(['K', 'T', 'U'], dtype='