2021-06-23 10:12:11 +02:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "5046da60",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2021-08-06 20:20:52 +02:00
|
|
|
"import os\n",
|
|
|
|
"\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
"glob_path = '/opt/iui-datarelease2-sose2021/*/split_letters_csv/*'\n",
|
|
|
|
"\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"pickle_file = 'data.pickle'\n",
|
|
|
|
"\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
"create_new = True\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"checkpoint_path = \"training_1/cp.ckpt\"\n",
|
|
|
|
"checkpoint_dir = os.path.dirname(checkpoint_path)\n",
|
|
|
|
"\n",
|
|
|
|
"# divisor for neuron count step downs (hard to describe), e.g. dense_step = 3: layer1=900, layer2 = 300, layer3 = 100, layer4 = 33...\n",
|
|
|
|
"dense_steps = 2\n",
|
|
|
|
"# amount of dense/dropout layers\n",
|
|
|
|
"layer_count = 3\n",
|
|
|
|
"# how much to drop\n",
|
|
|
|
"drop_count = 0.1"
|
2021-06-23 10:12:11 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "52925985",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from glob import glob\n",
|
|
|
|
"import pandas as pd\n",
|
|
|
|
"from tqdm import tqdm\n",
|
|
|
|
"\n",
|
|
|
|
"def dl_from_blob(filename) -> list:\n",
|
|
|
|
" all_data = []\n",
|
|
|
|
" \n",
|
|
|
|
" for path in tqdm(glob(filename)):\n",
|
|
|
|
" path = path\n",
|
|
|
|
" df = pd.read_csv(path, ';')\n",
|
|
|
|
" u = path.split('/')[3]\n",
|
|
|
|
" l = ''.join(filter(lambda x: x.isalpha(), path.split('/')[5]))[0] \n",
|
|
|
|
" d = {\n",
|
|
|
|
" 'file': path,\n",
|
|
|
|
" 'data': df,\n",
|
|
|
|
" 'user': u,\n",
|
|
|
|
" 'label': l\n",
|
|
|
|
" }\n",
|
|
|
|
" all_data.append(d)\n",
|
|
|
|
" return all_data"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "3dc191e7",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def save_pickle(f, structure):\n",
|
|
|
|
" _p = open(f, 'wb')\n",
|
|
|
|
" pickle.dump(structure, _p)\n",
|
|
|
|
" _p.close()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "858d807e",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import pickle\n",
|
|
|
|
"\n",
|
|
|
|
"def load_pickles(f) -> list:\n",
|
|
|
|
" _p = open(pickle_file, 'rb')\n",
|
|
|
|
" _d = pickle.load(_p)\n",
|
|
|
|
" _p.close()\n",
|
|
|
|
" \n",
|
|
|
|
" return _d"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "0885776a",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
2021-08-06 22:16:00 +02:00
|
|
|
"outputs": [],
|
2021-06-23 10:12:11 +02:00
|
|
|
"source": [
|
|
|
|
"import os\n",
|
|
|
|
"def load_data() -> list:\n",
|
|
|
|
" if os.path.isfile(pickle_file):\n",
|
|
|
|
" print(f'{pickle_file} found...')\n",
|
|
|
|
" return load_pickles(pickle_file)\n",
|
|
|
|
" print(f'Didn\\'t find {pickle_file}...')\n",
|
|
|
|
" all_data = dl_from_blob(glob_path)\n",
|
|
|
|
" print(f'Creating {pickle_file}...')\n",
|
|
|
|
" save_pickle(pickle_file, all_data)\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" return all_data\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Loading data...\")\n",
|
|
|
|
"data = load_data()\n",
|
|
|
|
"# plot_pd(data[0]['data'], False)"
|
2021-06-23 10:12:11 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "4ec7f36a",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import matplotlib.pyplot as plt\n",
|
|
|
|
"\n",
|
|
|
|
"def plot_pd(data, force=True):\n",
|
|
|
|
" fig, axs = plt.subplots(5, 3, figsize=(3*3, 3*5))\n",
|
|
|
|
" axs[0][0].plot(data['Acc1 X'])\n",
|
|
|
|
" axs[0][1].plot(data['Acc1 Y'])\n",
|
|
|
|
" axs[0][2].plot(data['Acc1 Z'])\n",
|
|
|
|
" axs[1][0].plot(data['Acc2 X'])\n",
|
|
|
|
" axs[1][1].plot(data['Acc2 Y'])\n",
|
|
|
|
" axs[1][2].plot(data['Acc2 Z'])\n",
|
|
|
|
" axs[2][0].plot(data['Gyro X'])\n",
|
|
|
|
" axs[2][1].plot(data['Gyro Y'])\n",
|
|
|
|
" axs[2][2].plot(data['Gyro Z'])\n",
|
|
|
|
" axs[3][0].plot(data['Mag X'])\n",
|
|
|
|
" axs[3][1].plot(data['Mag Y'])\n",
|
|
|
|
" axs[3][2].plot(data['Mag Z'])\n",
|
|
|
|
" axs[4][0].plot(data['Time'])\n",
|
|
|
|
"\n",
|
|
|
|
" if force:\n",
|
|
|
|
" for a in axs:\n",
|
|
|
|
" for b in a:\n",
|
|
|
|
" b.plot(data['Force'])\n",
|
|
|
|
" else:\n",
|
|
|
|
" axs[4][1].plot(data['Force'])\n",
|
|
|
|
"\n",
|
|
|
|
"def plot_np(data, force=True):\n",
|
|
|
|
" fig, axs = plt.subplots(5, 3, figsize=(3*3, 3*5))\n",
|
|
|
|
" axs[0][0].plot(data[0])\n",
|
|
|
|
" axs[0][1].plot(data[1])\n",
|
|
|
|
" axs[0][2].plot(data[2])\n",
|
|
|
|
" axs[1][0].plot(data[3])\n",
|
|
|
|
" axs[1][1].plot(data[4])\n",
|
|
|
|
" axs[1][2].plot(data[5])\n",
|
|
|
|
" axs[2][0].plot(data[6])\n",
|
|
|
|
" axs[2][1].plot(data[7])\n",
|
|
|
|
" axs[2][2].plot(data[8])\n",
|
|
|
|
" axs[3][0].plot(data[9])\n",
|
|
|
|
" axs[3][1].plot(data[10])\n",
|
|
|
|
" axs[3][2].plot(data[11])\n",
|
|
|
|
" axs[4][0].plot(data[13])\n",
|
|
|
|
"\n",
|
|
|
|
" if force:\n",
|
|
|
|
" for a in axs:\n",
|
|
|
|
" for b in a:\n",
|
|
|
|
" b.plot(data[12])\n",
|
|
|
|
" else:\n",
|
|
|
|
" axs[4][1].plot(data[12])\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "631fedbc",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def mill_drop(entry):\n",
|
|
|
|
" #drop millis on single\n",
|
|
|
|
" data_wo_mill = entry['data'].drop(labels='Millis', axis=1, inplace=False)\n",
|
|
|
|
" drop_entry = entry\n",
|
|
|
|
" drop_entry['data'] = data_wo_mill.reset_index(drop=True)\n",
|
|
|
|
" \n",
|
|
|
|
" return drop_entry"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "c86572fb",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import numpy as np\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
"\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
"def cut_force(drop_entry):\n",
|
|
|
|
" # force trans\n",
|
|
|
|
" shorten_entry = drop_entry\n",
|
|
|
|
" shorten_data = shorten_entry['data']\n",
|
|
|
|
" sf_entry = shorten_data['Force']\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" leeway = 10\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" \n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" try:\n",
|
|
|
|
" thresh = 70\n",
|
|
|
|
" temps_over_T = np.where(sf_entry > thresh)[0]\n",
|
|
|
|
" shorten_data = shorten_data[max(temps_over_T.min()-leeway,0):min(len(sf_entry)-1,temps_over_T.max()+leeway)]\n",
|
|
|
|
" except:\n",
|
|
|
|
" thresold = 0.05\n",
|
|
|
|
" thresh = sf_entry.max()*thresold\n",
|
|
|
|
" temps_over_T = np.where(sf_entry > thresh)[0]\n",
|
|
|
|
" shorten_data = shorten_data[max(temps_over_T.min()-leeway,0):min(len(sf_entry)-1,temps_over_T.max()+leeway)]\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" \n",
|
|
|
|
" shorten_entry['data'] = shorten_data.reset_index(drop=True)\n",
|
|
|
|
" return shorten_entry"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "875a8179",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def norm_force(shorten_entry, flist):\n",
|
|
|
|
" fnorm_entry = shorten_entry\n",
|
|
|
|
" u = fnorm_entry['user']\n",
|
|
|
|
" d = fnorm_entry['data']\n",
|
|
|
|
" \n",
|
|
|
|
" \n",
|
|
|
|
" d['Force'] = ((d['Force'] - flist[u].mean())/flist[u].std())\n",
|
|
|
|
" \n",
|
|
|
|
" fnorm_entry['data'] = fnorm_entry['data'].reset_index(drop=True)\n",
|
|
|
|
" return fnorm_entry"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "084feef0",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def time_trans(fnorm_entry):\n",
|
|
|
|
" #timetrans\n",
|
|
|
|
" time_entry = fnorm_entry\n",
|
|
|
|
" \n",
|
|
|
|
" time_entry['data']['Time'] = fnorm_entry['data']['Time']-fnorm_entry['data']['Time'][0]\n",
|
|
|
|
" \n",
|
|
|
|
" time_entry['data'] = time_entry['data'].reset_index(drop=True)\n",
|
|
|
|
"\n",
|
|
|
|
" return time_entry"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "ab674796",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def norm(time_entry):\n",
|
|
|
|
" # normalize\n",
|
|
|
|
" norm_entry = time_entry\n",
|
|
|
|
" \n",
|
|
|
|
" norm_entry['data']['Acc1 X'] = norm_entry['data']['Acc1 X'] / 32768\n",
|
|
|
|
" norm_entry['data']['Acc1 Y'] = norm_entry['data']['Acc1 Y'] / 32768\n",
|
|
|
|
" norm_entry['data']['Acc1 Z'] = norm_entry['data']['Acc1 Z'] / 32768\n",
|
|
|
|
" norm_entry['data']['Acc2 X'] = norm_entry['data']['Acc2 X'] / 8192\n",
|
|
|
|
" norm_entry['data']['Acc2 Y'] = norm_entry['data']['Acc2 Y'] / 8192\n",
|
|
|
|
" norm_entry['data']['Acc2 Z'] = norm_entry['data']['Acc2 Z'] / 8192\n",
|
|
|
|
" norm_entry['data']['Gyro X'] = norm_entry['data']['Gyro X'] / 32768\n",
|
|
|
|
" norm_entry['data']['Gyro Y'] = norm_entry['data']['Gyro Y'] / 32768\n",
|
|
|
|
" norm_entry['data']['Gyro Z'] = norm_entry['data']['Gyro Z'] / 32768\n",
|
|
|
|
" norm_entry['data']['Mag X'] = norm_entry['data']['Mag X'] / 8192\n",
|
|
|
|
" norm_entry['data']['Mag Y'] = norm_entry['data']['Mag Y'] / 8192\n",
|
|
|
|
" norm_entry['data']['Mag Z'] = norm_entry['data']['Mag Z'] / 8192\n",
|
|
|
|
" \n",
|
|
|
|
" norm_entry['data'] = norm_entry['data'].reset_index(drop=True)\n",
|
|
|
|
" \n",
|
|
|
|
" return norm_entry"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "cb1866fc",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
2021-08-06 22:16:00 +02:00
|
|
|
"outputs": [],
|
2021-06-23 10:12:11 +02:00
|
|
|
"source": [
|
|
|
|
"def preproc(d):\n",
|
|
|
|
" flist = {} \n",
|
|
|
|
" d_res = []\n",
|
|
|
|
" for e in data:\n",
|
|
|
|
" if e['user'] not in flist:\n",
|
|
|
|
" flist[e['user']] = e['data']['Force']\n",
|
|
|
|
" else:\n",
|
|
|
|
" flist[e['user']] = flist[e['user']].append(e['data']['Force'])\n",
|
|
|
|
" \n",
|
|
|
|
" for e in tqdm(data):\n",
|
|
|
|
" d_res.append(preproc_entry(e, flist))\n",
|
|
|
|
" return d_res\n",
|
|
|
|
" \n",
|
|
|
|
"def preproc_entry(entry, flist):\n",
|
|
|
|
" drop_entry = mill_drop(entry)\n",
|
|
|
|
"# plot_pd(drop_entry['data'])\n",
|
|
|
|
"# \n",
|
|
|
|
" shorten_entry = cut_force(drop_entry)\n",
|
|
|
|
"# plot_pd(shorten_entry['data'])\n",
|
|
|
|
"# \n",
|
|
|
|
" fnorm_entry = norm_force(shorten_entry, flist)\n",
|
|
|
|
"# plot_pd(fnorm_entry['data'])\n",
|
|
|
|
"# \n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" time_entry = time_trans(shorten_entry)\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
"# plot_pd(time_entry['data'])\n",
|
|
|
|
"# \n",
|
|
|
|
" norm_entry = norm(time_entry)\n",
|
|
|
|
"# plot_pd(norm_entry['data'], False)\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" return norm_entry\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Preprocessing...\")\n",
|
|
|
|
"pdata = preproc(data)\n",
|
|
|
|
"# plot_pd(pdata[0]['data'], False)"
|
2021-06-23 10:12:11 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "12ec5a1a",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
2021-08-06 22:16:00 +02:00
|
|
|
"outputs": [],
|
2021-06-23 10:12:11 +02:00
|
|
|
"source": [
|
|
|
|
"def throw(pdata):\n",
|
|
|
|
" llist = pd.Series([len(x['data']) for x in pdata])\n",
|
|
|
|
" threshold = int(llist.quantile(threshold_p))\n",
|
|
|
|
" longdex = np.where(llist <= threshold)[0]\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" return np.array(pdata)[longdex]\n",
|
|
|
|
"\n",
|
|
|
|
"llist = pd.Series([len(x['data']) for x in pdata])\n",
|
|
|
|
"threshold_p = 0.75\n",
|
|
|
|
"threshold = int(llist.quantile(threshold_p))\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Truncating...\")\n",
|
|
|
|
"tpdata = throw(pdata)\n",
|
|
|
|
"# plot_pd(tpdata[0]['data'], False)"
|
2021-06-23 10:12:11 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "44aaa509",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
2021-08-06 22:16:00 +02:00
|
|
|
"outputs": [],
|
2021-06-23 10:12:11 +02:00
|
|
|
"source": [
|
|
|
|
"from tensorflow.keras.preprocessing.sequence import pad_sequences\n",
|
|
|
|
"# ltpdata = []\n",
|
|
|
|
"def elong(tpdata):\n",
|
|
|
|
" for x in tqdm(tpdata):\n",
|
|
|
|
" y = x['data'].to_numpy().T\n",
|
|
|
|
" x['data'] = pad_sequences(y, dtype=float, padding='post', maxlen=threshold)\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" return tpdata\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Padding...\")\n",
|
|
|
|
"ltpdata = elong(tpdata)\n",
|
|
|
|
"# plot_np(ltpdata[0]['data'], False)"
|
2021-06-23 10:12:11 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "594a3d76",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import tensorflow as tf\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"from tensorflow.keras.regularizers import l2\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
"from tensorflow.keras.models import Sequential\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"from tensorflow.keras.layers import Dense, Flatten, BatchNormalization, Dropout\n",
|
|
|
|
"from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
|
|
|
|
"from tensorflow.keras.optimizers import Adam\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
"\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"def build_model(shape, classes):\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" model = Sequential()\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" \n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" ncount = shape[0]*shape[1]\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" \n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" model.add(Flatten(input_shape=shape, name='flatten'))\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" \n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" model.add(Dropout(drop_count, name=f'dropout_{drop_count*100}'))\n",
|
|
|
|
" model.add(BatchNormalization(name='batchNorm'))\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" \n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" for i in range(1,layer_count+1):\n",
|
|
|
|
" neurons = int(ncount/pow(dense_steps,i))\n",
|
|
|
|
" if neurons <= classes:\n",
|
|
|
|
" break\n",
|
|
|
|
" model.add(Dropout(drop_count*i, name=f'HiddenDropout_{drop_count*i*100:.0f}'))\n",
|
|
|
|
" model.add(Dense(neurons, activation='relu', \n",
|
|
|
|
" kernel_regularizer=l2(0.001), name=f'Hidden_{i}')\n",
|
|
|
|
" )\n",
|
|
|
|
" \n",
|
|
|
|
" model.add(Dense(classes, activation='softmax', name='Output'))\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" \n",
|
|
|
|
" model.compile(\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" optimizer=Adam(),\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" loss=\"categorical_crossentropy\", \n",
|
|
|
|
" metrics=[\"acc\"],\n",
|
|
|
|
" )\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" \n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" return model"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "e63d8c6a",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2021-07-05 15:01:40 +02:00
|
|
|
"checkpoint_file = './goat.weights'\n",
|
|
|
|
"\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
"def train(X_train, y_train, X_test, y_test):\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" model = build_model(X_train[0].shape, 52)\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" \n",
|
|
|
|
" model.summary()\n",
|
|
|
|
" \n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" # Create a callback that saves the model's weights\n",
|
|
|
|
" model_checkpoint = ModelCheckpoint(filepath=checkpoint_path, monitor='loss', \n",
|
|
|
|
"\t\t\tsave_best_only=True)\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" \n",
|
2021-07-19 01:21:53 +02:00
|
|
|
" history = model.fit(X_train, y_train, \n",
|
2021-08-06 22:16:00 +02:00
|
|
|
" epochs=100,\n",
|
|
|
|
" batch_size=32,\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" shuffle=True,\n",
|
|
|
|
" validation_data=(X_test, y_test),\n",
|
|
|
|
" verbose=2,\n",
|
|
|
|
" callbacks=[model_checkpoint]\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" )\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
" \n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" \n",
|
|
|
|
" model.load_weights(checkpoint_path)\n",
|
2021-06-23 10:12:11 +02:00
|
|
|
" print(\"Evaluate on test data\")\n",
|
2021-07-19 01:21:53 +02:00
|
|
|
" return model, history"
|
2021-06-23 10:12:11 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "8ba620e1",
|
2021-07-05 15:01:40 +02:00
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' # this is required\n",
|
|
|
|
"os.environ['CUDA_VISIBLE_DEVICES'] = '0' # set to '0' for GPU0, '1' for GPU1 or '2' for GPU2. Check \"gpustat\" in a terminal."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "4449465c",
|
2021-06-23 10:12:11 +02:00
|
|
|
"metadata": {},
|
2021-07-05 15:01:40 +02:00
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
|
|
"from sklearn.preprocessing import LabelEncoder, LabelBinarizer\n",
|
|
|
|
"\n",
|
|
|
|
"X = np.array([x['data'] for x in ltpdata])\n",
|
|
|
|
"y = np.array([x['label'] for x in ltpdata])\n",
|
|
|
|
"\n",
|
|
|
|
"lb = LabelBinarizer()\n",
|
|
|
|
"y_tran = lb.fit_transform(y)\n",
|
|
|
|
"\n",
|
|
|
|
"X_train, X_test, y_train, y_test = train_test_split(X, y_tran, test_size=0.2, random_state=177013)\n",
|
|
|
|
"\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"X_train=X_train.reshape(X_train.shape[0],X_train.shape[1],X_train.shape[2])\n",
|
|
|
|
"X_test=X_test.reshape(X_test.shape[0],X_test.shape[1],X_test.shape[2])\n",
|
2021-07-05 15:01:40 +02:00
|
|
|
"\n",
|
|
|
|
"train_shape = X_train[0].shape\n",
|
|
|
|
"classes = y_train[0].shape[0]"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "f36d7904",
|
2021-07-05 15:01:40 +02:00
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
2021-08-06 22:16:00 +02:00
|
|
|
"outputs": [],
|
2021-07-05 15:01:40 +02:00
|
|
|
"source": [
|
2021-08-06 20:20:52 +02:00
|
|
|
"%%time\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
"if not os.path.isdir(checkpoint_dir) or create_new:\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" tf.keras.backend.clear_session()\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
" model, history = train(np.array(X), np.array(y_tran), np.array(X_test), np.array(y_test))\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"else:\n",
|
|
|
|
" print(\"Loaded weights...\")\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
" model = build_model(X_train[0].shape, 52)\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
" model.load_weights(checkpoint_path)"
|
2021-07-05 15:01:40 +02:00
|
|
|
]
|
|
|
|
},
|
2021-08-06 22:16:00 +02:00
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "45d3110f",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Evaluation"
|
|
|
|
]
|
|
|
|
},
|
2021-07-05 15:01:40 +02:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2021-08-06 22:16:00 +02:00
|
|
|
"execution_count": null,
|
|
|
|
"id": "e05ce2ac",
|
2021-07-05 15:01:40 +02:00
|
|
|
"metadata": {},
|
2021-08-06 22:16:00 +02:00
|
|
|
"outputs": [],
|
2021-07-05 15:01:40 +02:00
|
|
|
"source": [
|
2021-08-06 22:16:00 +02:00
|
|
|
"ptest = [lb.classes_[e] for e in np.argmax(model.predict(X_test), axis=-1)]\n",
|
|
|
|
"ltest = lb.inverse_transform(y_test)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"id": "8b2f0353",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"%%time\n",
|
|
|
|
"\n",
|
|
|
|
"from sklearn.metrics import confusion_matrix\n",
|
|
|
|
"import seaborn as sn\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
"from sklearn.metrics import classification_report\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
"set_digits = sorted(list(set(ltest)))\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
"test_cm = confusion_matrix(ltest, ptest, labels=list(set_digits), normalize='true')\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
"df_cm = pd.DataFrame(test_cm, index=set_digits, columns=set_digits)\n",
|
|
|
|
"plt.figure(figsize = (20,14))\n",
|
|
|
|
"sn_plot = sn.heatmap(df_cm, cmap=\"Greys\")\n",
|
|
|
|
"plt.ylabel(\"True Label\")\n",
|
|
|
|
"plt.xlabel(\"Predicted Label\")\n",
|
|
|
|
"plt.show()\n",
|
2021-08-06 20:20:52 +02:00
|
|
|
"\n",
|
2021-08-06 22:16:00 +02:00
|
|
|
"print(classification_report(ltest, ptest, zero_division=0))"
|
2021-07-05 15:01:40 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
2021-08-06 22:16:00 +02:00
|
|
|
"id": "5121b883",
|
2021-07-05 15:01:40 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": []
|
2021-06-23 10:12:11 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "Python 3",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2021-07-14 10:15:52 +02:00
|
|
|
"version": "3.8.10"
|
2021-06-23 10:12:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 5
|
|
|
|
}
|