Diff of /ConvNet.ipynb [000000] .. [a8e9b4]

Switch to unified view

a b/ConvNet.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 1,
6
   "metadata": {
7
    "collapsed": true
8
   },
9
   "outputs": [],
10
   "source": [
11
    "%matplotlib inline\n",
12
    "import pandas as pd\n",
13
    "import numpy as np\n",
14
    "from cdtw import pydtw\n",
15
    "import seaborn as sns\n",
16
    "from tqdm import tqdm\n",
17
    "import os\n",
18
    "import json\n",
19
    "import h5py"
20
   ]
21
  },
22
  {
23
   "cell_type": "code",
24
   "execution_count": 2,
25
   "metadata": {
26
    "collapsed": false
27
   },
28
   "outputs": [
29
    {
30
     "name": "stderr",
31
     "output_type": "stream",
32
     "text": [
33
      "Using TensorFlow backend.\n"
34
     ]
35
    }
36
   ],
37
   "source": [
38
    "from keras import backend as K\n",
39
    "from keras.regularizers import l2, activity_l2\n",
40
    "\n",
41
    "from keras import backend as K\n",
42
    "from keras.engine.topology import Layer\n",
43
    "from keras.optimizers import RMSprop, SGD, Adam\n",
44
    "from keras.layers.core import Dense, Dropout, Activation, Flatten, Lambda, Merge\n",
45
    "from keras.layers.recurrent import LSTM, GRU\n",
46
    "from keras.models import Sequential, Model, load_model\n",
47
    "from keras.layers import Input, Bidirectional, merge\n",
48
    "    from keras.layers.convolutional import Convolution1D\n",
49
    "from keras.layers.pooling import MaxPooling1D, AveragePooling1D, GlobalMaxPooling1D"
50
   ]
51
  },
52
  {
53
   "cell_type": "code",
54
   "execution_count": null,
55
   "metadata": {
56
    "collapsed": true
57
   },
58
   "outputs": [],
59
   "source": [
60
    "def read_train(fname):\n",
61
    "    subjects = {}\n",
62
    "    with h5py.File(fname, \"r\") as data_file:\n",
63
    "        for subject, subject_data in data_file.items():\n",
64
    "            print(subject)\n",
65
    "            X = subject_data[\"data\"][:]\n",
66
    "            y = subject_data[\"labels\"][:]\n",
67
    "            subjects[subject] = (X, y)\n",
68
    "    return subjects\n",
69
    "\n",
70
    "def read_test(fname):\n",
71
    "    subjects = {}\n",
72
    "    with h5py.File(fname, \"r\") as data_file:\n",
73
    "        X = {}\n",
74
    "        for subject, subject_data in data_file.items():\n",
75
    "            X[subject] = {}\n",
76
    "            for chunk_id, chunk in data_file[subject].items():\n",
77
    "                X[subject][chunk_id] = chunk[:]\n",
78
    "\n",
79
    "    return X"
80
   ]
81
  },
82
  {
83
   "cell_type": "code",
84
   "execution_count": null,
85
   "metadata": {
86
    "collapsed": false
87
   },
88
   "outputs": [],
89
   "source": [
90
    "train = read_train(\"train.h5\")\n",
91
    "test = read_test(\"test.h5\")"
92
   ]
93
  },
94
  {
95
   "cell_type": "code",
96
   "execution_count": null,
97
   "metadata": {
98
    "collapsed": true
99
   },
100
   "outputs": [],
101
   "source": [
102
    "def batch(ts, y, n=1):\n",
103
    "    l = len(ts)\n",
104
    "    for ndx in range(0, l-n, 1):\n",
105
    "        yield (ts[ndx:min(ndx + n, l)], y[ndx:min(ndx + n, l)])"
106
   ]
107
  },
108
  {
109
   "cell_type": "code",
110
   "execution_count": null,
111
   "metadata": {
112
    "collapsed": false
113
   },
114
   "outputs": [],
115
   "source": [
116
    "def label_batch(batch):\n",
117
    "    if all([i == 1 for i in batch[1]]):\n",
118
    "        return 1\n",
119
    "    elif all([i == 0 for i in batch[1]]):\n",
120
    "        return 0\n",
121
    "    elif all([i == 2 for i in batch[1]]):\n",
122
    "        return 2\n",
123
    "    return -1\n",
124
    "\n",
125
    "subject_datas = {}\n",
126
    "for subject, data in tqdm(train.items()):\n",
127
    "    subject_ts = data[0].T\n",
128
    "    subject_y = data[1][0]\n",
129
    "    batches = [i for i in batch(subject_ts, subject_y, n=1125)]\n",
130
    "    batches = [(i[0], label_batch(i)) for i in batches]\n",
131
    "    batches = [i for i in batches if i[1] != -1]\n",
132
    "    batches = [i for i in batches if len(i[0]) == 1125]\n",
133
    "    subject_datas[subject] = batches"
134
   ]
135
  },
136
  {
137
   "cell_type": "code",
138
   "execution_count": null,
139
   "metadata": {
140
    "collapsed": false
141
   },
142
   "outputs": [],
143
   "source": [
144
    "subject_datas[\"subject_1\"][0][0].shape"
145
   ]
146
  },
147
  {
148
   "cell_type": "code",
149
   "execution_count": null,
150
   "metadata": {
151
    "collapsed": false
152
   },
153
   "outputs": [],
154
   "source": [
155
    "X = []\n",
156
    "y = []\n",
157
    "for subj, subj_data in tqdm(subject_datas.items()):\n",
158
    "    X.extend([i[0] for i in subj_data])\n",
159
    "    y.extend([i[1] for i in subj_data])"
160
   ]
161
  },
162
  {
163
   "cell_type": "code",
164
   "execution_count": null,
165
   "metadata": {
166
    "collapsed": false
167
   },
168
   "outputs": [],
169
   "source": [
170
    "X = np.array(X)\n",
171
    "y = np.array(y)"
172
   ]
173
  },
174
  {
175
   "cell_type": "code",
176
   "execution_count": null,
177
   "metadata": {
178
    "collapsed": true
179
   },
180
   "outputs": [],
181
   "source": [
182
    "def shuffle_in_unison_scary(a, b):\n",
183
    "    rng_state = np.random.get_state()\n",
184
    "    np.random.shuffle(a)\n",
185
    "    np.random.set_state(rng_state)\n",
186
    "    np.random.shuffle(b)"
187
   ]
188
  },
189
  {
190
   "cell_type": "code",
191
   "execution_count": null,
192
   "metadata": {
193
    "collapsed": false
194
   },
195
   "outputs": [],
196
   "source": [
197
    "shuffle_in_unison_scary(X, y)"
198
   ]
199
  },
200
  {
201
   "cell_type": "code",
202
   "execution_count": null,
203
   "metadata": {
204
    "collapsed": false
205
   },
206
   "outputs": [],
207
   "source": [
208
    "def toarr(label):\n",
209
    "    arr = np.zeros(3)\n",
210
    "    arr[label] = 1\n",
211
    "    return arr\n",
212
    "y_arr = np.vstack([toarr(i) for i in y])"
213
   ]
214
  },
215
  {
216
   "cell_type": "code",
217
   "execution_count": null,
218
   "metadata": {
219
    "collapsed": false
220
   },
221
   "outputs": [],
222
   "source": [
223
    "validation_start = len(X) - 30000\n",
224
    "X_train = X[:validation_start]\n",
225
    "y_train = y_arr[:validation_start]\n",
226
    "X_val = X[validation_start:]\n",
227
    "y_val = y_arr[validation_start:]"
228
   ]
229
  },
230
  {
231
   "cell_type": "code",
232
   "execution_count": 3,
233
   "metadata": {
234
    "collapsed": true
235
   },
236
   "outputs": [],
237
   "source": [
238
    "def get_base_model():\n",
239
    "    '''Base network to be shared (eq. to feature extraction).\n",
240
    "    '''\n",
241
    "    with K.tf.device('/gpu:2'):\n",
242
    "        input_seq = Input(shape=(1125, 24))\n",
243
    "        \n",
244
    "        filter_sizes = [5, 7, 14]\n",
245
    "        nb_filters = 100\n",
246
    "        filter_size = 7\n",
247
    "        different_scales = []\n",
248
    "        for fsize in filter_sizes:\n",
249
    "            convolved = Convolution1D(nb_filters, fsize, border_mode=\"same\", activation=\"tanh\")(input_seq)\n",
250
    "            processed = GlobalMaxPooling1D()(convolved)\n",
251
    "            different_scales.append(processed)\n",
252
    "            \n",
253
    "        different_scales = merge(different_scales, mode='concat')\n",
254
    "        compressed = Dense(150, activation=\"tanh\")(different_scales)\n",
255
    "        compressed = Dropout(0.2)(compressed)\n",
256
    "        compressed = Dense(150, activation=\"tanh\")(compressed)\n",
257
    "        model = Model(input=input_seq, output=compressed)\n",
258
    "        #return model\n",
259
    "        #filter_size = 5\n",
260
    "        \n",
261
    "        #convolved = Convolution1D(nb_filters, filter_size, border_mode=\"same\", activation=\"tanh\")(input_seq)\n",
262
    "        #processed = GlobalMaxPooling1D()(convolved)\n",
263
    "        #compressed = Dense(300, activation=\"tanh\")(processed)\n",
264
    "        #compressed = Dropout(0.3)(compressed)\n",
265
    "        #compressed = Dense(300, activation=\"linear\")(compressed)\n",
266
    "        #model = Model(input=input_seq, output=compressed)            \n",
267
    "        return model"
268
   ]
269
  },
270
  {
271
   "cell_type": "code",
272
   "execution_count": 4,
273
   "metadata": {
274
    "collapsed": false
275
   },
276
   "outputs": [],
277
   "source": [
278
    "with K.tf.device('/gpu:2'):\n",
279
    "    base_network = get_base_model()\n",
280
    "    input_seq = Input(shape=(1125, 24))\n",
281
    "\n",
282
    "    embedding = base_network(input_seq)\n",
283
    "    out = Dense(3, activation='softmax')(embedding)\n",
284
    "    \n",
285
    "    model = Model(input=input_seq, output=out)\n",
286
    "    \n",
287
    "    #opt = SGD(lr=0.001, momentum=0.9, nesterov=True, clipvalue=0.0001)\n",
288
    "    #opt = RMSprop(lr=0.001, clipvalue=10**6)\n",
289
    "    opt = Adam(lr=0.01)\n",
290
    "    model.compile(loss=\"categorical_crossentropy\", optimizer=opt)"
291
   ]
292
  },
293
  {
294
   "cell_type": "code",
295
   "execution_count": 13,
296
   "metadata": {
297
    "collapsed": false
298
   },
299
   "outputs": [
300
    {
301
     "data": {
302
      "text/plain": [
303
       "<tf.Tensor 'Tanh_3:0' shape=(?, 150) dtype=float32>"
304
      ]
305
     },
306
     "execution_count": 13,
307
     "metadata": {},
308
     "output_type": "execute_result"
309
    }
310
   ],
311
   "source": [
312
    "model.layers[-2].layers[-3].get_output_at(0)"
313
   ]
314
  },
315
  {
316
   "cell_type": "code",
317
   "execution_count": 9,
318
   "metadata": {
319
    "collapsed": false
320
   },
321
   "outputs": [
322
    {
323
     "data": {
324
      "text/plain": [
325
       "[<tf.Tensor 'Tanh_4:0' shape=(?, 150) dtype=float32>]"
326
      ]
327
     },
328
     "execution_count": 9,
329
     "metadata": {},
330
     "output_type": "execute_result"
331
    }
332
   ],
333
   "source": [
334
    "model.layers[-2].outputs = [model.layers[-2].layers[-3].get_output_at(0)]"
335
   ]
336
  },
337
  {
338
   "cell_type": "code",
339
   "execution_count": 10,
340
   "metadata": {
341
    "collapsed": false
342
   },
343
   "outputs": [
344
    {
345
     "ename": "NameError",
346
     "evalue": "name 'X_train' is not defined",
347
     "output_type": "error",
348
     "traceback": [
349
      "\u001b[0;31m\u001b[0m",
350
      "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)",
351
      "\u001b[0;32m<ipython-input-10-e101f3d38d74>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mK\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/gpu:2'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m     model.fit(X_train, y_train, batch_size=60, callbacks=[earlyStopping],\n\u001b[0m\u001b[1;32m      8\u001b[0m               \u001b[0mnb_epoch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalidation_split\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshuffle\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m               class_weight=None, sample_weight=None)\n",
352
      "\u001b[0;31mNameError\u001b[0m: name 'X_train' is not defined"
353
     ]
354
    }
355
   ],
356
   "source": [
357
    "from keras.callbacks import EarlyStopping\n",
358
    "nb_epoch = 100000\n",
359
    "earlyStopping = EarlyStopping(monitor='val_loss', patience=3, verbose=0, mode='auto')\n",
360
    "#samples_per_epoch = 50000\n",
361
    "\n",
362
    "with K.tf.device('/gpu:2'):\n",
363
    "    model.fit(X_train, y_train, batch_size=60, callbacks=[earlyStopping],\n",
364
    "              nb_epoch=100, verbose=1, validation_split=0.2, shuffle=True,\n",
365
    "              class_weight=None, sample_weight=None)"
366
   ]
367
  },
368
  {
369
   "cell_type": "code",
370
   "execution_count": null,
371
   "metadata": {
372
    "collapsed": false
373
   },
374
   "outputs": [],
375
   "source": [
376
    "model.layers[-2].layers[-8]"
377
   ]
378
  },
379
  {
380
   "cell_type": "code",
381
   "execution_count": null,
382
   "metadata": {
383
    "collapsed": false
384
   },
385
   "outputs": [],
386
   "source": [
387
    "model.save(\"convnet-multiscale\")"
388
   ]
389
  },
390
  {
391
   "cell_type": "code",
392
   "execution_count": null,
393
   "metadata": {
394
    "collapsed": false
395
   },
396
   "outputs": [],
397
   "source": [
398
    "preds = [np.argmax(i) for i in model.predict(X_val)]"
399
   ]
400
  },
401
  {
402
   "cell_type": "code",
403
   "execution_count": null,
404
   "metadata": {
405
    "collapsed": false
406
   },
407
   "outputs": [],
408
   "source": [
409
    "from sklearn.metrics import accuracy_score\n",
410
    "accuracy_score([np.argmax(i) for i in y_val], preds)"
411
   ]
412
  },
413
  {
414
   "cell_type": "code",
415
   "execution_count": null,
416
   "metadata": {
417
    "collapsed": false
418
   },
419
   "outputs": [],
420
   "source": [
421
    "# GENERATES SUBMISSION DF\n",
422
    "df = []\n",
423
    "for subj in test:\n",
424
    "    for chunk in tqdm(test[subj]):\n",
425
    "        data = {}\n",
426
    "        data[\"subject_id\"] = int(subj.split(\"_\")[-1])\n",
427
    "        data[\"chunk_id\"] = int(chunk.split(\"_\")[-1])\n",
428
    "        arr = test[subj][chunk].T\n",
429
    "        preds = model.predict(np.array([arr]))[0]\n",
430
    "        data[\"class_0_score\"] = preds[0]\n",
431
    "        data[\"class_1_score\"] = preds[1]\n",
432
    "        data[\"class_2_score\"] = preds[2]\n",
433
    "        for i in range(0, 1125):\n",
434
    "            data[\"tick\"] = i\n",
435
    "            df.append(data.copy())\n",
436
    "df = pd.DataFrame(df)\n",
437
    "df = df[[\"subject_id\", \"chunk_id\", \"tick\", \"class_0_score\",\n",
438
    "         \"class_1_score\",\"class_2_score\"]]"
439
   ]
440
  },
441
  {
442
   "cell_type": "code",
443
   "execution_count": null,
444
   "metadata": {
445
    "collapsed": false
446
   },
447
   "outputs": [],
448
   "source": [
449
    "df.head()"
450
   ]
451
  },
452
  {
453
   "cell_type": "code",
454
   "execution_count": null,
455
   "metadata": {
456
    "collapsed": true
457
   },
458
   "outputs": [],
459
   "source": [
460
    "df.to_csv('submit_multiscale_untrained.csv', index=False)"
461
   ]
462
  },
463
  {
464
   "cell_type": "code",
465
   "execution_count": null,
466
   "metadata": {
467
    "collapsed": true
468
   },
469
   "outputs": [],
470
   "source": []
471
  }
472
 ],
473
 "metadata": {
474
  "kernelspec": {
475
   "display_name": "Python 2",
476
   "language": "python",
477
   "name": "python2"
478
  },
479
  "language_info": {
480
   "codemirror_mode": {
481
    "name": "ipython",
482
    "version": 2
483
   },
484
   "file_extension": ".py",
485
   "mimetype": "text/x-python",
486
   "name": "python",
487
   "nbconvert_exporter": "python",
488
   "pygments_lexer": "ipython2",
489
   "version": "2.7.6"
490
  }
491
 },
492
 "nbformat": 4,
493
 "nbformat_minor": 1
494
}