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

Switch to unified view

a b/MultiscaleNet.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, AtrousConvolution1D\n",
49
    "from keras.layers.pooling import MaxPooling1D, AveragePooling1D, GlobalMaxPooling1D"
50
   ]
51
  },
52
  {
53
   "cell_type": "code",
54
   "execution_count": 3,
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\n",
80
    "\n",
81
    "def batch(ts, y, n=1):\n",
82
    "    l = len(ts)\n",
83
    "    for ndx in range(0, l-n, 1):\n",
84
    "        yield (ts[ndx:min(ndx + n, l)], y[ndx:min(ndx + n, l)])\n",
85
    "\n",
86
    "def label_batch(batch):\n",
87
    "    if all([i == 1 for i in batch[1]]):\n",
88
    "        return 1\n",
89
    "    elif all([i == 0 for i in batch[1]]):\n",
90
    "        return 0\n",
91
    "    elif all([i == 2 for i in batch[1]]):\n",
92
    "        return 2\n",
93
    "    return -1\n",
94
    "        \n",
95
    "def get_data():\n",
96
    "    train = read_train(\"train.h5\")\n",
97
    "    test = read_test(\"test.h5\")\n",
98
    "    \n",
99
    "\n",
100
    "    subject_datas = {}\n",
101
    "    for subject, data in tqdm(train.items()):\n",
102
    "        subject_ts = data[0].T\n",
103
    "        subject_y = data[1][0]\n",
104
    "        batches = [i for i in batch(subject_ts, subject_y, n=1125)]\n",
105
    "        batches = [(i[0], label_batch(i)) for i in batches]\n",
106
    "        batches = [i for i in batches if i[1] != -1]\n",
107
    "        batches = [i for i in batches if len(i[0]) == 1125]\n",
108
    "        subject_datas[subject] = batches\n",
109
    "    \n",
110
    "    X = []\n",
111
    "    y = []\n",
112
    "    for subj, subj_data in tqdm(subject_datas.items()):\n",
113
    "        X.extend([i[0] for i in subj_data])\n",
114
    "        y.extend([i[1] for i in subj_data])\n",
115
    "    return X, y, test"
116
   ]
117
  },
118
  {
119
   "cell_type": "code",
120
   "execution_count": 4,
121
   "metadata": {
122
    "collapsed": false
123
   },
124
   "outputs": [
125
    {
126
     "name": "stdout",
127
     "output_type": "stream",
128
     "text": [
129
      "subject_0\n",
130
      "subject_1\n",
131
      "subject_2\n",
132
      "subject_3\n"
133
     ]
134
    },
135
    {
136
     "name": "stderr",
137
     "output_type": "stream",
138
     "text": [
139
      "100%|██████████| 4/4 [01:14<00:00, 19.86s/it]\n",
140
      "100%|██████████| 4/4 [00:00<00:00, 145.58it/s]\n"
141
     ]
142
    }
143
   ],
144
   "source": [
145
    "X, y, test = get_data()"
146
   ]
147
  },
148
  {
149
   "cell_type": "code",
150
   "execution_count": 5,
151
   "metadata": {
152
    "collapsed": false
153
   },
154
   "outputs": [],
155
   "source": [
156
    "X = np.array(X)\n",
157
    "y = np.array(y)"
158
   ]
159
  },
160
  {
161
   "cell_type": "code",
162
   "execution_count": 6,
163
   "metadata": {
164
    "collapsed": true
165
   },
166
   "outputs": [],
167
   "source": [
168
    "def shuffle_in_unison_scary(a, b):\n",
169
    "    rng_state = np.random.get_state()\n",
170
    "    np.random.shuffle(a)\n",
171
    "    np.random.set_state(rng_state)\n",
172
    "    np.random.shuffle(b)"
173
   ]
174
  },
175
  {
176
   "cell_type": "code",
177
   "execution_count": 7,
178
   "metadata": {
179
    "collapsed": true
180
   },
181
   "outputs": [],
182
   "source": [
183
    "shuffle_in_unison_scary(X, y)"
184
   ]
185
  },
186
  {
187
   "cell_type": "code",
188
   "execution_count": 8,
189
   "metadata": {
190
    "collapsed": false
191
   },
192
   "outputs": [],
193
   "source": [
194
    "validation_start = len(X) - 30\n",
195
    "X_train = X[:validation_start]\n",
196
    "y_train = y[:validation_start]\n",
197
    "X_val = X[validation_start:]\n",
198
    "y_val = y[validation_start:]"
199
   ]
200
  },
201
  {
202
   "cell_type": "code",
203
   "execution_count": 9,
204
   "metadata": {
205
    "collapsed": true
206
   },
207
   "outputs": [],
208
   "source": [
209
    "from scipy.signal import resample\n",
210
    "\n",
211
    "\n",
212
    "def toarr(label):\n",
213
    "    arr = np.zeros(3)\n",
214
    "    arr[label] = 1\n",
215
    "    return arr\n",
216
    "\n",
217
    "def data_generator(X, Y, batch_size):\n",
218
    "    while True:\n",
219
    "        inds = np.random.choice(len(X), batch_size)\n",
220
    "        x = X[inds]\n",
221
    "        y = Y[inds]\n",
222
    "        y = np.vstack([toarr(i) for i in y])\n",
223
    "        x_256 = np.array([resample(i, 256) for i in x])\n",
224
    "        x_500 = np.array([resample(i, 500) for i in x])\n",
225
    "        x = np.array([i for i in x])\n",
226
    "        yield ([x_256, x_500, x], y)"
227
   ]
228
  },
229
  {
230
   "cell_type": "code",
231
   "execution_count": 10,
232
   "metadata": {
233
    "collapsed": true
234
   },
235
   "outputs": [],
236
   "source": [
237
    "def multiscale(chunk):\n",
238
    "    resampled_256 = resample(chunk, 256)\n",
239
    "    resampled_500 = resample(chunk, 500)\n",
240
    "    return [resampled_256, resampled_500, chunk]"
241
   ]
242
  },
243
  {
244
   "cell_type": "code",
245
   "execution_count": null,
246
   "metadata": {
247
    "collapsed": true
248
   },
249
   "outputs": [],
250
   "source": [
251
    "def get_base_model(input_len, fsize):\n",
252
    "    '''Base network to be shared (eq. to feature extraction).\n",
253
    "    '''\n",
254
    "    with K.tf.device('/gpu:1'):\n",
255
    "        input_seq = Input(shape=(input_len, 24))\n",
256
    "        nb_filters = 150\n",
257
    "        convolved = Convolution1D(nb_filters, fsize, border_mode=\"same\", activation=\"tanh\")(input_seq)\n",
258
    "        processed = GlobalMaxPooling1D()(convolved)\n",
259
    "        compressed = Dense(150, activation=\"tanh\")(processed)\n",
260
    "        compressed = Dropout(0.3)(compressed)\n",
261
    "        compressed = Dense(150, activation=\"tanh\")(compressed)\n",
262
    "        model = Model(input=input_seq, output=compressed)            \n",
263
    "        return model"
264
   ]
265
  },
266
  {
267
   "cell_type": "code",
268
   "execution_count": null,
269
   "metadata": {
270
    "collapsed": false
271
   },
272
   "outputs": [],
273
   "source": [
274
    "with K.tf.device('/gpu:1'):\n",
275
    "    \n",
276
    "    input256_seq = Input(shape=(256, 24))\n",
277
    "    input500_seq = Input(shape=(500, 24))\n",
278
    "    input1125_seq = Input(shape=(1125, 24))\n",
279
    "    \n",
280
    "    base_network256 = get_base_model(256, 4)\n",
281
    "    base_network500 = get_base_model(500, 7)\n",
282
    "    base_network1125 = get_base_model(1125, 10)\n",
283
    "    \n",
284
    "    embedding_256 = base_network256(input256_seq)\n",
285
    "    embedding_500 = base_network500(input500_seq)\n",
286
    "    embedding_1125 = base_network256(input1125_seq)\n",
287
    "    \n",
288
    "    merged = merge([embedding_256, embedding_500, embedding_1125], mode=\"concat\")\n",
289
    "    out = Dense(3, activation='softmax')(merged)\n",
290
    "    \n",
291
    "    model = Model(input=[input256_seq, input500_seq, input1125_seq], output=out)\n",
292
    "    \n",
293
    "    #opt = SGD(lr=0.001, momentum=0.9, nesterov=True, clipvalue=0.0001)\n",
294
    "    opt = RMSprop(lr=0.005, clipvalue=10**6)\n",
295
    "    #opt = Adam(lr=0.001)\n",
296
    "    model.compile(loss=\"categorical_crossentropy\", optimizer=opt)"
297
   ]
298
  },
299
  {
300
   "cell_type": "code",
301
   "execution_count": 13,
302
   "metadata": {
303
    "collapsed": true
304
   },
305
   "outputs": [],
306
   "source": [
307
    "with K.tf.device('/gpu:2'):\n",
308
    "    model = load_model(\"convnet-multiscale-true-022unk\")"
309
   ]
310
  },
311
  {
312
   "cell_type": "code",
313
   "execution_count": 21,
314
   "metadata": {
315
    "collapsed": false
316
   },
317
   "outputs": [
318
    {
319
     "name": "stdout",
320
     "output_type": "stream",
321
     "text": [
322
      "Epoch 1/100000\n",
323
      "100000/100000 [==============================] - 135s - loss: 0.1939   \n",
324
      "Epoch 2/100000\n",
325
      " 99500/100000 [============================>.] - ETA: 0s - loss: 0.1922"
326
     ]
327
    },
328
    {
329
     "ename": "KeyboardInterrupt",
330
     "evalue": "",
331
     "output_type": "error",
332
     "traceback": [
333
      "\u001b[0;31m\u001b[0m",
334
      "\u001b[0;31mKeyboardInterrupt\u001b[0mTraceback (most recent call last)",
335
      "\u001b[0;32m<ipython-input-21-09ad3c297605>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\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[1;32m      7\u001b[0m     model.fit_generator(data_generator(X_train, y_train, batch_size=50), samples_per_epoch, nb_epoch, \n\u001b[0;32m----> 8\u001b[0;31m                         callbacks=[earlyStopping], verbose=1)#, nb_val_samples=20000,\n\u001b[0m\u001b[1;32m      9\u001b[0m                         \u001b[0;31m#validation_data=data_generator(X_val, y_val, batch_size=40))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
336
      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc\u001b[0m in \u001b[0;36mfit_generator\u001b[0;34m(self, generator, samples_per_epoch, nb_epoch, verbose, callbacks, validation_data, nb_val_samples, class_weight, max_q_size, nb_worker, pickle_safe)\u001b[0m\n\u001b[1;32m   1451\u001b[0m                     \u001b[0mbatch_logs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1452\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1453\u001b[0;31m                 \u001b[0mcallbacks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_batch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_logs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1454\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1455\u001b[0m                 \u001b[0;31m# construct epoch logs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
337
      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc\u001b[0m in \u001b[0;36mon_batch_end\u001b[0;34m(self, batch, logs)\u001b[0m\n\u001b[1;32m     59\u001b[0m         \u001b[0mt_before_callbacks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     60\u001b[0m         \u001b[0;32mfor\u001b[0m \u001b[0mcallback\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallbacks\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 61\u001b[0;31m             \u001b[0mcallback\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_batch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     62\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_delta_ts_batch_end\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mt_before_callbacks\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     63\u001b[0m         \u001b[0mdelta_t_median\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmedian\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_delta_ts_batch_end\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
338
      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc\u001b[0m in \u001b[0;36mon_batch_end\u001b[0;34m(self, batch, logs)\u001b[0m\n\u001b[1;32m    187\u001b[0m         \u001b[0;31m# will be handled by on_epoch_end\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    188\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseen\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'nb_sample'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 189\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprogbar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog_values\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    190\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    191\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mon_epoch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepoch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
339
      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/utils/generic_utils.pyc\u001b[0m in \u001b[0;36mupdate\u001b[0;34m(self, current, values, force)\u001b[0m\n\u001b[1;32m    157\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    158\u001b[0m             \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 159\u001b[0;31m             \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    160\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    161\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mcurrent\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtarget\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
340
      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc\u001b[0m in \u001b[0;36mflush\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    267\u001b[0m             \u001b[0mevt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mthreading\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mEvent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    268\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_io_loop\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_callback\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mevt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 269\u001b[0;31m             \u001b[0mevt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    270\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    271\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_flush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
341
      "\u001b[0;32m/usr/lib/python2.7/threading.pyc\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    618\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    619\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__flag\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 620\u001b[0;31m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__cond\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    621\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__flag\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    622\u001b[0m         \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
342
      "\u001b[0;32m/usr/lib/python2.7/threading.pyc\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    337\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m    \u001b[0;31m# restore state no matter what (e.g., KeyboardInterrupt)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    338\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 339\u001b[0;31m                 \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    340\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0m__debug__\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    341\u001b[0m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_note\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"%s.wait(): got it\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
343
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
344
     ]
345
    }
346
   ],
347
   "source": [
348
    "from keras.callbacks import EarlyStopping\n",
349
    "nb_epoch = 100000\n",
350
    "earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='auto')\n",
351
    "samples_per_epoch = 100000\n",
352
    "\n",
353
    "with K.tf.device('/gpu:2'):\n",
354
    "    model.fit_generator(data_generator(X_train, y_train, batch_size=50), samples_per_epoch, nb_epoch, \n",
355
    "                        callbacks=[earlyStopping], verbose=1)#, nb_val_samples=20000,\n",
356
    "                        #validation_data=data_generator(X_val, y_val, batch_size=40))"
357
   ]
358
  },
359
  {
360
   "cell_type": "code",
361
   "execution_count": 26,
362
   "metadata": {
363
    "collapsed": false
364
   },
365
   "outputs": [
366
    {
367
     "name": "stdout",
368
     "output_type": "stream",
369
     "text": [
370
      "Epoch 1/1\n",
371
      "30000/30000 [==============================] - 70s - loss: 0.1640    "
372
     ]
373
    },
374
    {
375
     "name": "stderr",
376
     "output_type": "stream",
377
     "text": [
378
      " 24%|██▍       | 12/49 [00:00<00:00, 119.99it/s]"
379
     ]
380
    },
381
    {
382
     "name": "stdout",
383
     "output_type": "stream",
384
     "text": [
385
      "\n"
386
     ]
387
    },
388
    {
389
     "name": "stderr",
390
     "output_type": "stream",
391
     "text": [
392
      "100%|██████████| 49/49 [00:00<00:00, 135.88it/s]\n",
393
      "100%|██████████| 89/89 [00:00<00:00, 215.36it/s]\n",
394
      "100%|██████████| 49/49 [00:00<00:00, 248.00it/s]\n",
395
      "100%|██████████| 89/89 [00:00<00:00, 219.17it/s]\n"
396
     ]
397
    },
398
    {
399
     "name": "stdout",
400
     "output_type": "stream",
401
     "text": [
402
      "Epoch 1/1\n",
403
      "30000/30000 [==============================] - 67s - loss: 0.1592    "
404
     ]
405
    },
406
    {
407
     "name": "stderr",
408
     "output_type": "stream",
409
     "text": [
410
      "\r",
411
      "  0%|          | 0/49 [00:00<?, ?it/s]"
412
     ]
413
    },
414
    {
415
     "name": "stdout",
416
     "output_type": "stream",
417
     "text": [
418
      "\n"
419
     ]
420
    },
421
    {
422
     "name": "stderr",
423
     "output_type": "stream",
424
     "text": [
425
      "100%|██████████| 49/49 [00:00<00:00, 126.00it/s]\n",
426
      "100%|██████████| 89/89 [00:00<00:00, 190.92it/s]\n",
427
      "100%|██████████| 49/49 [00:00<00:00, 161.84it/s]\n",
428
      "100%|██████████| 89/89 [00:00<00:00, 190.56it/s]\n"
429
     ]
430
    },
431
    {
432
     "name": "stdout",
433
     "output_type": "stream",
434
     "text": [
435
      "Epoch 1/1\n",
436
      "30000/30000 [==============================] - 60s - loss: 0.1557    "
437
     ]
438
    },
439
    {
440
     "name": "stderr",
441
     "output_type": "stream",
442
     "text": [
443
      "\r",
444
      "  0%|          | 0/49 [00:00<?, ?it/s]"
445
     ]
446
    },
447
    {
448
     "name": "stdout",
449
     "output_type": "stream",
450
     "text": [
451
      "\n"
452
     ]
453
    },
454
    {
455
     "name": "stderr",
456
     "output_type": "stream",
457
     "text": [
458
      "100%|██████████| 49/49 [00:00<00:00, 175.59it/s]\n",
459
      "100%|██████████| 89/89 [00:00<00:00, 188.29it/s]\n",
460
      "100%|██████████| 49/49 [00:00<00:00, 170.44it/s]\n",
461
      "100%|██████████| 89/89 [00:00<00:00, 202.76it/s]\n"
462
     ]
463
    },
464
    {
465
     "name": "stdout",
466
     "output_type": "stream",
467
     "text": [
468
      "Epoch 1/1\n",
469
      "30000/30000 [==============================] - 73s - loss: 0.1545    "
470
     ]
471
    },
472
    {
473
     "name": "stderr",
474
     "output_type": "stream",
475
     "text": [
476
      " 29%|██▊       | 14/49 [00:00<00:00, 139.19it/s]"
477
     ]
478
    },
479
    {
480
     "name": "stdout",
481
     "output_type": "stream",
482
     "text": [
483
      "\n"
484
     ]
485
    },
486
    {
487
     "name": "stderr",
488
     "output_type": "stream",
489
     "text": [
490
      "100%|██████████| 49/49 [00:00<00:00, 190.23it/s]\n",
491
      "100%|██████████| 89/89 [00:00<00:00, 235.47it/s]\n",
492
      "100%|██████████| 49/49 [00:00<00:00, 221.90it/s]\n",
493
      "100%|██████████| 89/89 [00:00<00:00, 171.74it/s]\n"
494
     ]
495
    },
496
    {
497
     "name": "stdout",
498
     "output_type": "stream",
499
     "text": [
500
      "Epoch 1/1\n",
501
      "30000/30000 [==============================] - 72s - loss: 0.1556    "
502
     ]
503
    },
504
    {
505
     "name": "stderr",
506
     "output_type": "stream",
507
     "text": [
508
      "\r",
509
      "  0%|          | 0/49 [00:00<?, ?it/s]"
510
     ]
511
    },
512
    {
513
     "name": "stdout",
514
     "output_type": "stream",
515
     "text": [
516
      "\n"
517
     ]
518
    },
519
    {
520
     "name": "stderr",
521
     "output_type": "stream",
522
     "text": [
523
      "100%|██████████| 49/49 [00:00<00:00, 119.83it/s]\n",
524
      "100%|██████████| 89/89 [00:00<00:00, 195.06it/s]\n",
525
      "100%|██████████| 49/49 [00:00<00:00, 187.22it/s]\n",
526
      "100%|██████████| 89/89 [00:00<00:00, 201.47it/s]\n"
527
     ]
528
    },
529
    {
530
     "name": "stdout",
531
     "output_type": "stream",
532
     "text": [
533
      "Epoch 1/1\n",
534
      "30000/30000 [==============================] - 68s - loss: 0.1563    "
535
     ]
536
    },
537
    {
538
     "name": "stderr",
539
     "output_type": "stream",
540
     "text": [
541
      " 27%|██▋       | 13/49 [00:00<00:00, 119.17it/s]"
542
     ]
543
    },
544
    {
545
     "name": "stdout",
546
     "output_type": "stream",
547
     "text": [
548
      "\n"
549
     ]
550
    },
551
    {
552
     "name": "stderr",
553
     "output_type": "stream",
554
     "text": [
555
      "100%|██████████| 49/49 [00:00<00:00, 142.53it/s]\n",
556
      "100%|██████████| 89/89 [00:00<00:00, 203.55it/s]\n",
557
      "100%|██████████| 49/49 [00:00<00:00, 192.02it/s]\n",
558
      "100%|██████████| 89/89 [00:00<00:00, 193.69it/s]\n"
559
     ]
560
    },
561
    {
562
     "name": "stdout",
563
     "output_type": "stream",
564
     "text": [
565
      "Epoch 1/1\n",
566
      "30000/30000 [==============================] - 72s - loss: 0.1588    "
567
     ]
568
    },
569
    {
570
     "name": "stderr",
571
     "output_type": "stream",
572
     "text": [
573
      " 31%|███       | 15/49 [00:00<00:00, 145.08it/s]"
574
     ]
575
    },
576
    {
577
     "name": "stdout",
578
     "output_type": "stream",
579
     "text": [
580
      "\n"
581
     ]
582
    },
583
    {
584
     "name": "stderr",
585
     "output_type": "stream",
586
     "text": [
587
      "100%|██████████| 49/49 [00:00<00:00, 181.02it/s]\n",
588
      "100%|██████████| 89/89 [00:00<00:00, 199.11it/s]\n",
589
      "100%|██████████| 49/49 [00:00<00:00, 229.50it/s]\n",
590
      "100%|██████████| 89/89 [00:00<00:00, 204.20it/s]\n"
591
     ]
592
    },
593
    {
594
     "name": "stdout",
595
     "output_type": "stream",
596
     "text": [
597
      "Epoch 1/1\n",
598
      "30000/30000 [==============================] - 67s - loss: 0.1598    "
599
     ]
600
    },
601
    {
602
     "name": "stderr",
603
     "output_type": "stream",
604
     "text": [
605
      " 37%|███▋      | 18/49 [00:00<00:00, 174.26it/s]"
606
     ]
607
    },
608
    {
609
     "name": "stdout",
610
     "output_type": "stream",
611
     "text": [
612
      "\n"
613
     ]
614
    },
615
    {
616
     "name": "stderr",
617
     "output_type": "stream",
618
     "text": [
619
      "100%|██████████| 49/49 [00:00<00:00, 130.09it/s]\n",
620
      "100%|██████████| 89/89 [00:00<00:00, 208.28it/s]\n",
621
      "100%|██████████| 49/49 [00:00<00:00, 189.43it/s]\n",
622
      "100%|██████████| 89/89 [00:00<00:00, 181.52it/s]\n"
623
     ]
624
    },
625
    {
626
     "name": "stdout",
627
     "output_type": "stream",
628
     "text": [
629
      "Epoch 1/1\n",
630
      "30000/30000 [==============================] - 53s - loss: 0.1634    "
631
     ]
632
    },
633
    {
634
     "name": "stderr",
635
     "output_type": "stream",
636
     "text": [
637
      " 41%|████      | 20/49 [00:00<00:00, 191.91it/s]"
638
     ]
639
    },
640
    {
641
     "name": "stdout",
642
     "output_type": "stream",
643
     "text": [
644
      "\n"
645
     ]
646
    },
647
    {
648
     "name": "stderr",
649
     "output_type": "stream",
650
     "text": [
651
      "100%|██████████| 49/49 [00:00<00:00, 202.34it/s]\n",
652
      "100%|██████████| 89/89 [00:00<00:00, 216.69it/s]\n",
653
      "100%|██████████| 49/49 [00:00<00:00, 212.14it/s]\n",
654
      "100%|██████████| 89/89 [00:00<00:00, 212.55it/s]\n"
655
     ]
656
    },
657
    {
658
     "name": "stdout",
659
     "output_type": "stream",
660
     "text": [
661
      "Epoch 1/1\n",
662
      "30000/30000 [==============================] - 70s - loss: 0.1491    "
663
     ]
664
    },
665
    {
666
     "name": "stderr",
667
     "output_type": "stream",
668
     "text": [
669
      " 29%|██▊       | 14/49 [00:00<00:00, 135.21it/s]"
670
     ]
671
    },
672
    {
673
     "name": "stdout",
674
     "output_type": "stream",
675
     "text": [
676
      "\n"
677
     ]
678
    },
679
    {
680
     "name": "stderr",
681
     "output_type": "stream",
682
     "text": [
683
      "100%|██████████| 49/49 [00:00<00:00, 188.83it/s]\n",
684
      "100%|██████████| 89/89 [00:00<00:00, 169.04it/s]\n",
685
      "100%|██████████| 49/49 [00:00<00:00, 178.81it/s]\n",
686
      "100%|██████████| 89/89 [00:00<00:00, 182.79it/s]\n"
687
     ]
688
    }
689
   ],
690
   "source": [
691
    "# BLEND NNS\n",
692
    "for blend_id in range(25, 35):\n",
693
    "    with K.tf.device('/gpu:2'):\n",
694
    "        model.fit_generator(data_generator(X_train, y_train, batch_size=100), samples_per_epoch=30000, nb_epoch=1, \n",
695
    "                            callbacks=[earlyStopping], verbose=1)\n",
696
    "        \n",
697
    "        df = []\n",
698
    "        for subj in test:\n",
699
    "            for chunk in tqdm(test[subj]):\n",
700
    "                data = {}\n",
701
    "                data[\"subject_id\"] = int(subj.split(\"_\")[-1])\n",
702
    "                data[\"chunk_id\"] = int(chunk.split(\"_\")[-1])\n",
703
    "                arr = test[subj][chunk].T\n",
704
    "                preds = model.predict([np.array([i]) for i in multiscale(arr)])[0]\n",
705
    "                data[\"class_0_score\"] = preds[0]\n",
706
    "                data[\"class_1_score\"] = preds[1]\n",
707
    "                data[\"class_2_score\"] = preds[2]\n",
708
    "                for i in range(0, 1125):\n",
709
    "                    data[\"tick\"] = i\n",
710
    "                    df.append(data.copy())\n",
711
    "        df = pd.DataFrame(df)\n",
712
    "        df = df[[\"subject_id\", \"chunk_id\", \"tick\", \"class_0_score\",\n",
713
    "                 \"class_1_score\",\"class_2_score\"]]\n",
714
    "        \n",
715
    "        df.to_csv('submit_blended_' + str(blend_id) + '.csv', index=False)"
716
   ]
717
  },
718
  {
719
   "cell_type": "code",
720
   "execution_count": 15,
721
   "metadata": {
722
    "collapsed": true
723
   },
724
   "outputs": [],
725
   "source": [
726
    "model.save(\"convnet-multiscale-deep-021unk\")"
727
   ]
728
  },
729
  {
730
   "cell_type": "code",
731
   "execution_count": 19,
732
   "metadata": {
733
    "collapsed": false
734
   },
735
   "outputs": [
736
    {
737
     "name": "stderr",
738
     "output_type": "stream",
739
     "text": [
740
      "100%|██████████| 49/49 [00:00<00:00, 165.60it/s]\n",
741
      "100%|██████████| 89/89 [00:00<00:00, 185.89it/s]\n",
742
      "100%|██████████| 49/49 [00:00<00:00, 189.62it/s]\n",
743
      "100%|██████████| 89/89 [00:00<00:00, 175.60it/s]\n"
744
     ]
745
    }
746
   ],
747
   "source": [
748
    "df = []\n",
749
    "for subj in test:\n",
750
    "    for chunk in tqdm(test[subj]):\n",
751
    "        data = {}\n",
752
    "        data[\"subject_id\"] = int(subj.split(\"_\")[-1])\n",
753
    "        data[\"chunk_id\"] = int(chunk.split(\"_\")[-1])\n",
754
    "        arr = test[subj][chunk].T\n",
755
    "        preds = model.predict([np.array([i]) for i in multiscale(arr)])[0]\n",
756
    "        data[\"class_0_score\"] = preds[0]\n",
757
    "        data[\"class_1_score\"] = preds[1]\n",
758
    "        data[\"class_2_score\"] = preds[2]\n",
759
    "        for i in range(0, 1125):\n",
760
    "            data[\"tick\"] = i\n",
761
    "            df.append(data.copy())\n",
762
    "df = pd.DataFrame(df)\n",
763
    "df = df[[\"subject_id\", \"chunk_id\", \"tick\", \"class_0_score\",\n",
764
    "         \"class_1_score\",\"class_2_score\"]]"
765
   ]
766
  },
767
  {
768
   "cell_type": "code",
769
   "execution_count": 20,
770
   "metadata": {
771
    "collapsed": true
772
   },
773
   "outputs": [],
774
   "source": [
775
    "df.to_csv('submit_true_multiscale_016_large_batch.csv', index=False)"
776
   ]
777
  },
778
  {
779
   "cell_type": "code",
780
   "execution_count": null,
781
   "metadata": {
782
    "collapsed": true
783
   },
784
   "outputs": [],
785
   "source": []
786
  }
787
 ],
788
 "metadata": {
789
  "kernelspec": {
790
   "display_name": "Python 2",
791
   "language": "python",
792
   "name": "python2"
793
  },
794
  "language_info": {
795
   "codemirror_mode": {
796
    "name": "ipython",
797
    "version": 2
798
   },
799
   "file_extension": ".py",
800
   "mimetype": "text/x-python",
801
   "name": "python",
802
   "nbconvert_exporter": "python",
803
   "pygments_lexer": "ipython2",
804
   "version": "2.7.6"
805
  }
806
 },
807
 "nbformat": 4,
808
 "nbformat_minor": 1
809
}