{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":4477,"databundleVersionId":44220,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\nlabels = ['HandStart', 'FirstDigitTouch', 'BothStartLoadPhase', 'LiftOff',\n       'Replace', 'BothReleased']\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-27T09:36:58.125515Z","iopub.execute_input":"2024-03-27T09:36:58.126459Z","iopub.status.idle":"2024-03-27T09:36:58.134989Z","shell.execute_reply.started":"2024-03-27T09:36:58.126428Z","shell.execute_reply":"2024-03-27T09:36:58.133929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nimport gc\n\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\nimport pywt\nimport pandas as pd\nfrom sklearn.preprocessing import StandardScaler,Normalizer,MinMaxScaler\nimport scipy\nfrom scipy.signal import butter, lfilter, convolve, boxcar\nfrom scipy.signal import freqz\nfrom scipy.fftpack import fft, ifft\nimport argparse\nimport matplotlib.pyplot as plt\n\nfrom sklearn import metrics\nimport torch\nfrom torch.utils.data import DataLoader, Dataset\nimport torch.nn as nn\nimport torch.optim as optim","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:37:00.473290Z","iopub.execute_input":"2024-03-27T09:37:00.474356Z","iopub.status.idle":"2024-03-27T09:37:03.670246Z","shell.execute_reply.started":"2024-03-27T09:37:00.474317Z","shell.execute_reply":"2024-03-27T09:37:03.669102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.manual_seed(2021)\nnp.random.seed(2021)\ndevice = 'cuda:0' if torch.cuda.is_available() else 'cpu'\n\nparser = argparse.ArgumentParser()\nparser.add_argument(\"--n_epochs\", type=int, default=1, help=\"number of epochs of training\")\nparser.add_argument(\"--batch_size\", type=int, default=1024, help=\"size of the batches\")\nparser.add_argument(\"--lr\", type=float, default=0.005, help=\"adam's learning rate\")\nparser.add_argument(\"--b1\", type=float, default=0.5, help=\"adam: decay of first order momentum of gradient\")\nparser.add_argument(\"--b2\", type=float, default=0.99, help=\"adam: decay of first order momentum of gradient\")\nparser.add_argument(\"--n_cpu\", type=int, default=4, help=\"number of cpu threads to use during batch generation\")\nparser.add_argument(\"--in_len\", type=int, default=2**10, help=\"length of the input fed to neural net\")\nparser.add_argument(\"--in_channels\", type=int, default=32, help=\"number of signal channels\")\nparser.add_argument(\"--out_channels\", type=int, default=6, help=\"number of classes\")\nparser.add_argument(\"--chunk\", type=int, default=1000, help=\"length of splited chunks\")\nparser.add_argument(\"--lstm_hidden\", type=int, default=1024, help=\"length of splited chunks\")\nopt, unknown = parser.parse_known_args()\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:37:06.152998Z","iopub.execute_input":"2024-03-27T09:37:06.153849Z","iopub.status.idle":"2024-03-27T09:37:06.226205Z","shell.execute_reply.started":"2024-03-27T09:37:06.153813Z","shell.execute_reply":"2024-03-27T09:37:06.225216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# zip the files as ZIPFILE :)\nimport zipfile\nwith zipfile.ZipFile(\"../input/grasp-and-lift-eeg-detection/test.zip\",\"r\") as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile(\"../input/grasp-and-lift-eeg-detection/train.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:22:13.623966Z","iopub.execute_input":"2024-03-27T08:22:13.624333Z","iopub.status.idle":"2024-03-27T08:22:46.015044Z","shell.execute_reply.started":"2024-03-27T08:22:13.624306Z","shell.execute_reply":"2024-03-27T08:22:46.013985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def wavelet_denoising(x, wavelet='db2', level=3):\n    coeff = pywt.wavedec(x, wavelet, mode=\"per\")\n    sigma = (1/0.6745) * madev(coeff[-level])\n    uthresh = sigma * np.sqrt(2 * np.log(len(x)))\n    coeff[1:] = (pywt.threshold(i, value=uthresh, mode='hard') for i in coeff[1:])\n    return pywt.waverec(coeff, wavelet, mode='per')\ndef madev(d, axis=None):\n    \"\"\" Mean absolute deviation of a signal \"\"\"\n    return np.mean(np.absolute(d - np.mean(d, axis)), axis)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:37:09.297520Z","iopub.execute_input":"2024-03-27T09:37:09.298574Z","iopub.status.idle":"2024-03-27T09:37:09.307252Z","shell.execute_reply.started":"2024-03-27T09:37:09.298526Z","shell.execute_reply":"2024-03-27T09:37:09.306071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DataSet For ResCNN","metadata":{}},{"cell_type":"code","source":"%%time\ndef read_csv(data, events):\n    x = pd.read_csv(data)\n    y = pd.read_csv(events)\n    id = '_'.join(x.iloc[0, 0].split('_')[:-1])\n    x = x.iloc[:,1:].values\n    y = y.iloc[:,1:].values\n    return x, y\n    \n\ntrainset = []\ngt = []\nfor filename in tqdm(os.listdir('./train')):\n    if 'data' in filename:\n        data_file_name = os.path.join('./train', filename)\n        id = filename.split('.')[0]\n        events_file_name = os.path.join('./train', '_'.join(id.split('_')[:-1]) + '_events.csv')\n        x, y = read_csv(data_file_name, events_file_name)\n        x = wavelet_denoising(x)\n        trainset.append(x.T.astype(np.float32))\n        gt.append(y.T.astype(np.float32))","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:37:11.499055Z","iopub.execute_input":"2024-03-27T09:37:11.500105Z","iopub.status.idle":"2024-03-27T09:39:11.571337Z","shell.execute_reply.started":"2024-03-27T09:37:11.500065Z","shell.execute_reply":"2024-03-27T09:39:11.570174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_dataset = trainset[-2:]\nvalid_gt = gt[-2:]\ntrainset = trainset[:-2]\ngt = gt[:-2]","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:39:18.496059Z","iopub.execute_input":"2024-03-27T09:39:18.496481Z","iopub.status.idle":"2024-03-27T09:39:18.503822Z","shell.execute_reply.started":"2024-03-27T09:39:18.496450Z","shell.execute_reply":"2024-03-27T09:39:18.501617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resample_data(gt, chunk_size=opt.chunk):\n    \"\"\"\n    split long signals to smaller chunks, discard no-events chunks  \n    \"\"\"\n    total_discard_chunks = 0\n    mean_val = []\n    threshold = 0.01\n    index = []\n    \n    for i in range(len(gt)):\n        for j in range(0, gt[i].shape[1], chunk_size):\n            mean_val.append(np.mean(gt[i][:, j:min(gt[i].shape[1],j+chunk_size)]))\n            if mean_val[-1] < threshold and j > 0:  # discard chunks with low events time\n                total_discard_chunks += 1\n                index.extend([(i, k) for k in range(j, min(gt[i].shape[1],j+chunk_size), chunk_size//100)])\n            else:\n                index.extend([(i, k) for k in range(j, min(gt[i].shape[1],j+chunk_size))])\n\n    plt.plot([0, len(mean_val)], [threshold, threshold], color='r')\n    plt.scatter(range(len(mean_val)), mean_val, s=1)\n    plt.show()\n    print('Total number of chunks discarded: {} chunks'.format(total_discard_chunks))\n    print('{}% data'.format(total_discard_chunks/len(mean_val)))\n    del mean_val\n    gc.collect()\n    return index","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:39:21.513470Z","iopub.execute_input":"2024-03-27T09:39:21.515383Z","iopub.status.idle":"2024-03-27T09:39:21.526554Z","shell.execute_reply.started":"2024-03-27T09:39:21.515334Z","shell.execute_reply":"2024-03-27T09:39:21.525311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nclass EEGSignalDataset(Dataset):\n    def __init__(self, data, gt, soft_label=True, train=True):\n        self.data = data\n        self.gt = gt\n        self.train = train\n        self.soft_label = soft_label\n        if train:\n            self.index = resample_data(gt)\n        else:\n            self.index = [(i, j) for i in range(len(data)) for j in range(data[i].shape[1])]\n    \n    def __getitem__(self, i):\n        i, j = self.index[i]\n        raw_data, label = self.data[i][:,max(0, j-opt.in_len+1):j+1], \\\n                self.gt[i][:,j]\n        pad = opt.in_len - raw_data.shape[1]\n        if pad:\n            raw_data = np.pad(raw_data, ((0,0),(pad,0)), 'constant',constant_values=0)\n\n        raw_data, label = torch.from_numpy(raw_data.astype(np.float32)),\\\n                            torch.from_numpy(label.astype(np.float32))\n        if self.soft_label:\n            label[label < .05] = .05\n        return raw_data, label\n            \n    \n    def __len__(self):\n        return len(self.index)\n    \ndataset = EEGSignalDataset(trainset, gt) \ndataloader = DataLoader(dataset, batch_size = opt.batch_size,\\\n                                       num_workers = opt.n_cpu, shuffle=True)\nprint(len(dataset))\ntestset = EEGSignalDataset(valid_dataset, valid_gt, train=False, soft_label=False) \ntestloader = DataLoader(testset, batch_size = opt.batch_size,\\\n                                       num_workers = opt.n_cpu, shuffle=False)\nvalid_gt = np.concatenate(valid_gt, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:39:26.493451Z","iopub.execute_input":"2024-03-27T09:39:26.494232Z","iopub.status.idle":"2024-03-27T09:39:29.641776Z","shell.execute_reply.started":"2024-03-27T09:39:26.494191Z","shell.execute_reply":"2024-03-27T09:39:29.640642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResCNN","metadata":{}},{"cell_type":"code","source":"class NNet(nn.Module):\n    def __init__(self, in_channels=opt.in_channels, out_channels=opt.out_channels):\n        super(NNet, self).__init__()\n        self.hidden = 32\n        self.net = nn.Sequential(\n            nn.BatchNorm1d(opt.in_channels),\n            nn.Conv1d(opt.in_channels, opt.in_channels, 5, padding=2),\n            nn.Conv1d(opt.in_channels, self.hidden, 16, stride=16),\n            nn.LeakyReLU(0.1),\n            nn.Conv1d(self.hidden, self.hidden, 7, padding=3),\n        )\n        for i in range(2):\n            self.net.add_module('conv{}'.format(i), \\\n                                self.__block(self.hidden, self.hidden)) # 16\n        \n        self.mid = nn.Sequential(\n            self.__block(self.hidden, self.hidden),\n            self.__block(self.hidden, self.hidden)\n        ) # 4\n        self.final = nn.Sequential(\n            nn.Linear(256, 64),\n            nn.LeakyReLU(0.1),\n            nn.Linear(64, 6),\n            nn.Sigmoid()\n        )\n        \n    def __block(self, inchannels, outchannels):\n        return nn.Sequential(\n            nn.MaxPool1d(2, 2),\n            nn.Conv1d(inchannels, outchannels, 5, padding=2),\n            nn.LeakyReLU(0.1),\n            nn.BatchNorm1d(outchannels),\n            nn.Conv1d(outchannels, outchannels, 5, padding=2),\n            nn.LeakyReLU(0.1)\n        )\n    \n    def forward(self, x):\n        x = self.net(x)\n        y = self.mid(x)\n        y = torch.cat((x[..., -4:], y), dim=-1).view(x.shape[0], -1)\n        return self.final(y)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:39:58.529370Z","iopub.execute_input":"2024-03-27T09:39:58.529820Z","iopub.status.idle":"2024-03-27T09:39:58.543514Z","shell.execute_reply.started":"2024-03-27T09:39:58.529787Z","shell.execute_reply":"2024-03-27T09:39:58.542230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nnnet = NNet()\nnnet.to(device)\nloss_fnc = nn.BCELoss()\nadam = optim.Adam(nnet.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2))\nloss_his, train_loss, valid_auc, train_auc = [], [], [], []\nnnet.train()\nfor epoch in range(opt.n_epochs):\n    p_bar = tqdm(dataloader)\n    for i, (x, y) in enumerate(p_bar):\n        x, y = x.to(device), y.to(device)\n        pred = nnet(x)\n        loss = loss_fnc(pred.squeeze(dim=-1), y)\n        adam.zero_grad()\n        loss.backward()\n        nn.utils.clip_grad_value_(nnet.parameters(), 2.)\n        adam.step()\n        train_loss.append(loss.item())\n        p_bar.set_description('[Loss: {}]'.format(train_loss[-1]))\n        if i % 50 == 0:\n            loss_his.append(np.mean(train_loss))\n            train_loss.clear()\n#         if i % 1500 == 0:\n#             y[y<0.1]=0\n#             train_auc.append(metrics.roc_auc_score(y.detach().cpu().numpy(), \\\n#                                 pred.detach().cpu().numpy()))\n#             valid_auc.append(calc_valid_auc(nnet))\n    print('[Epoch {}/{}] [Loss: {}]'.format(epoch+1, opt.n_epochs, loss_his[-1]))\n    \ntorch.save(nnet.state_dict(), 'model.pt')","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:40:01.051131Z","iopub.execute_input":"2024-03-27T09:40:01.052026Z","iopub.status.idle":"2024-03-27T10:02:38.542065Z","shell.execute_reply.started":"2024-03-27T09:40:01.051986Z","shell.execute_reply":"2024-03-27T10:02:38.540854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(range(len(loss_his)), loss_his, label='loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T10:02:38.544656Z","iopub.execute_input":"2024-03-27T10:02:38.545519Z","iopub.status.idle":"2024-03-27T10:02:38.819999Z","shell.execute_reply.started":"2024-03-27T10:02:38.545483Z","shell.execute_reply":"2024-03-27T10:02:38.819038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nnet.eval()\ny_pred = []\nwith torch.no_grad():\n    for x, _ in tqdm(testloader):\n        x = x.to(device)\n        pred = nnet(x).detach().cpu().numpy()\n        y_pred.append(pred)\ny_pred = np.concatenate(y_pred, axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T10:02:38.821330Z","iopub.execute_input":"2024-03-27T10:02:38.821700Z","iopub.status.idle":"2024-03-27T10:03:41.418625Z","shell.execute_reply.started":"2024-03-27T10:02:38.821648Z","shell.execute_reply":"2024-03-27T10:03:41.417629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_roc(y_true, y_pred):\n    fig, axs = plt.subplots(3, 2, figsize=(15,15))\n    for i, label in enumerate(labels):\n        fpr, tpr, _ = metrics.roc_curve(y_true[i], y_pred[i])\n        ax = axs[i//2, i%2]\n        ax.plot(fpr, tpr,linewidth=3)\n        ax.set_title(label+\" ROC\")\n        ax.plot([0, 1], [0, 1], 'k--')\n        plt.xlim([0.0, 1.0])\n        plt.ylim([0.0, 1.0])\n        plt.xlabel('False Positive Rate')\n        plt.ylabel('True Positive Rate')\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T10:03:41.420997Z","iopub.execute_input":"2024-03-27T10:03:41.421381Z","iopub.status.idle":"2024-03-27T10:03:41.429232Z","shell.execute_reply.started":"2024-03-27T10:03:41.421348Z","shell.execute_reply":"2024-03-27T10:03:41.428162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc(valid_gt, y_pred.T)\nprint('auc roc: ', metrics.roc_auc_score(valid_gt.T, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-03-27T10:03:41.430646Z","iopub.execute_input":"2024-03-27T10:03:41.431035Z","iopub.status.idle":"2024-03-27T10:03:44.615025Z","shell.execute_reply.started":"2024-03-27T10:03:41.430983Z","shell.execute_reply":"2024-03-27T10:03:44.614030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorboardX import SummaryWriter\nnnet = NNet()\nnnet.to(device)\nx.to(device)\n# 创建一个SummaryWriter实例\nwriter = SummaryWriter('./runs/model_visualization')\n\n# 添加模型的计算图\nwriter.add_graph(nnet, x)\n\n# 关闭SummaryWriter\nwriter.close()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T10:03:44.616424Z","iopub.execute_input":"2024-03-27T10:03:44.617281Z","iopub.status.idle":"2024-03-27T10:03:54.738402Z","shell.execute_reply.started":"2024-03-27T10:03:44.617243Z","shell.execute_reply":"2024-03-27T10:03:54.737321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DataSet For LSTM","metadata":{"_kg_hide-input":false}},{"cell_type":"code","source":"%%time\ndef read_csv(data, events):\n    x = pd.read_csv(data)\n    y = pd.read_csv(events)\n    id = '_'.join(x.iloc[0, 0].split('_')[:-1])\n    x = x.iloc[:,1:].values\n    y = y.iloc[:,1:].values\n    return x, y\n    \n\ntrainset = []\ngt = []\nfor filename in tqdm(os.listdir('./train')):\n    if 'data' in filename:\n        data_file_name = os.path.join('./train', filename)\n        id = filename.split('.')[0]\n        events_file_name = os.path.join('./train', '_'.join(id.split('_')[:-1]) + '_events.csv')\n        x, y = read_csv(data_file_name, events_file_name)\n        x = wavelet_denoising(x)\n        trainset.append(x.astype(np.float32))\n        gt.append(y.astype(np.float32))","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:23:10.281244Z","iopub.execute_input":"2024-03-27T08:23:10.281607Z","iopub.status.idle":"2024-03-27T08:24:59.832939Z","shell.execute_reply.started":"2024-03-27T08:23:10.281575Z","shell.execute_reply":"2024-03-27T08:24:59.832050Z"},"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_dataset = trainset[-2:]\nvalid_gt = gt[-2:]\ntrainset = trainset[:-2]\ngt = gt[:-2]","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:25:42.011054Z","iopub.execute_input":"2024-03-27T08:25:42.012166Z","iopub.status.idle":"2024-03-27T08:25:42.016843Z","shell.execute_reply.started":"2024-03-27T08:25:42.012124Z","shell.execute_reply":"2024-03-27T08:25:42.016022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gt[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:25:44.582075Z","iopub.execute_input":"2024-03-27T08:25:44.582411Z","iopub.status.idle":"2024-03-27T08:25:44.589152Z","shell.execute_reply.started":"2024-03-27T08:25:44.582387Z","shell.execute_reply":"2024-03-27T08:25:44.588065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DataLoader For LSTM","metadata":{}},{"cell_type":"code","source":"def resample_data_time_var(gt, chunk_size=opt.chunk):\n    \"\"\"\n    split long signals to smaller chunks, discard no-events chunks  \n    \"\"\"\n    total_discard_chunks = 0\n    mean_val = []\n    threshold = 0.01\n    index = []\n    \n    for i in range(len(gt)):\n        for j in range(0, gt[i].shape[0], chunk_size):\n            mean_val.append(np.mean(gt[i][j:min(gt[i].shape[0],j+chunk_size), :]))\n            if mean_val[-1] < threshold and j > 0: \n                total_discard_chunks += 1\n                index.extend([(i, k) for k in range(j, min(gt[i].shape[0],j+chunk_size), chunk_size//100)])\n            else:\n                index.extend([(i, k) for k in range(j, min(gt[i].shape[0],j+chunk_size))])\n\n    del mean_val\n    gc.collect()\n    return index","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:25:47.837625Z","iopub.execute_input":"2024-03-27T08:25:47.838023Z","iopub.status.idle":"2024-03-27T08:25:47.846690Z","shell.execute_reply.started":"2024-03-27T08:25:47.837993Z","shell.execute_reply":"2024-03-27T08:25:47.845731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nclass EEGSignalDataset(Dataset):\n    def __init__(self, data, gt, soft_label=True, train=True):\n        self.data = data\n        self.gt = gt\n        self.train = train\n        self.soft_label = soft_label\n        if train:\n            self.index = resample_data_time_var(gt)\n        else:\n            self.index = [(i, j) for i in range(len(data)) for j in range(data[i].shape[0])]\n    \n    def __getitem__(self, i):\n        i, j = self.index[i]\n        raw_data, label = self.data[i][max(0, j-opt.in_len+1):j+1,:], \\\n                self.gt[i][j,:]\n        pad = opt.in_len - raw_data.shape[0]\n        if pad:\n            raw_data = np.pad(raw_data, ((pad,0),(0,0)), 'constant',constant_values=0)\n\n        raw_data, label = torch.from_numpy(raw_data.astype(np.float32)),\\\n                            torch.from_numpy(label.astype(np.float32))\n        if self.soft_label:\n            label[label < .05] = .05\n        return raw_data, label\n            \n    \n    def __len__(self):\n        return len(self.index)\n    \ndataset = EEGSignalDataset(trainset, gt) \ndataloader = DataLoader(dataset, batch_size = opt.batch_size,\\\n                                       num_workers = opt.n_cpu, shuffle=True)\nprint(len(dataset))\ntestset = EEGSignalDataset(valid_dataset, valid_gt, train=False, soft_label=False) \ntestloader = DataLoader(testset, batch_size = opt.batch_size,\\\n                                       num_workers = opt.n_cpu, shuffle=False)\nvalid_gt = np.concatenate(valid_gt, axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:25:56.735875Z","iopub.execute_input":"2024-03-27T08:25:56.736249Z","iopub.status.idle":"2024-03-27T08:25:59.263079Z","shell.execute_reply.started":"2024-03-27T08:25:56.736216Z","shell.execute_reply":"2024-03-27T08:25:59.262213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Neural NetWork","metadata":{}},{"cell_type":"code","source":"class LSTMNet(nn.Module):\n    def __init__(self, in_channels=opt.in_channels, out_channels=opt.out_channels):\n        super(LSTMNet, self).__init__()\n        self.hidden = 32\n        self.seq = opt.in_len\n        self.LSTM = nn.Sequential(\n        nn.LSTM(input_size = opt.in_channels,\n                hidden_size =opt.in_channels,\n                batch_first = True,dropout = 0.8,bidirectional = False),\n        )  \n        self.MLP = nn.Sequential(\n        nn.Linear(160,80),\n        nn.LeakyReLU(0.1),\n        nn.Linear(80,6),\n        nn.Sigmoid()\n        )\n        self.ConvNet = nn.Sequential(\n        nn.BatchNorm1d(opt.in_channels),\n        nn.Conv1d(opt.in_channels, opt.in_channels, 5, padding=2),\n        nn.Conv1d(opt.in_channels, self.hidden, 16, stride=16),\n        nn.LeakyReLU(0.1),\n        nn.Conv1d(self.hidden, self.hidden, 7, padding=3),\n        self.__block(self.hidden,self.hidden),\n        self.__block(self.hidden,self.hidden),\n        nn.Conv1d(opt.in_channels, self.hidden, 4, stride=4),\n        )\n        self.Hidden = nn.Sequential(\n        self.__block(self.hidden,self.hidden),\n        self.__block(self.hidden,self.hidden)\n        )\n    def __block(self, inchannels, outchannels):\n        return nn.Sequential(\n            nn.MaxPool1d(2, 2),\n            nn.Conv1d(inchannels, outchannels, 5, padding=2),\n            nn.LeakyReLU(0.1),\n            nn.BatchNorm1d(outchannels),\n            nn.Conv1d(outchannels, outchannels, 5, padding=2),\n            nn.LeakyReLU(0.1)\n        )\n    def forward(self, x):\n        x,_ = self.LSTM(x)\n        x = x.permute(0,2,1)\n        x = self.ConvNet(x)\n        y = self.Hidden(x)\n        y = torch.cat((x[...,-4:],y),dim = 2).view(x.shape[0],-1)\n        y = self.MLP(y)\n        return y","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:51:46.046661Z","iopub.execute_input":"2024-03-27T08:51:46.048171Z","iopub.status.idle":"2024-03-27T08:51:46.064326Z","shell.execute_reply.started":"2024-03-27T08:51:46.048134Z","shell.execute_reply":"2024-03-27T08:51:46.063161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nlstmnet = LSTMNet()\nlstmnet.to(device)\nloss_fnc = nn.BCELoss()\nadam = optim.Adam(lstmnet.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2))\nloss_his, train_loss, valid_auc, train_auc = [], [], [], []\nlstmnet.train()\nfor epoch in range(opt.n_epochs):\n    p_bar = tqdm(dataloader)\n    for i, (x, y) in enumerate(p_bar):\n        x, y = x.to(device), y.to(device)\n        pred = lstmnet(x)\n        loss = loss_fnc(pred.squeeze(dim=-1), y)\n        adam.zero_grad()\n        loss.backward()\n        nn.utils.clip_grad_value_(lstmnet.parameters(), 2.)\n        adam.step()\n        train_loss.append(loss.item())\n        p_bar.set_description('[Loss: {}]'.format(train_loss[-1]))\n        if i % 50 == 0:\n            loss_his.append(np.mean(train_loss))\n            train_loss.clear()\n#         if i % 1500 == 0:\n#             y[y<0.1]=0\n#             train_auc.append(metrics.roc_auc_score(y.detach().cpu().numpy(), \\\n#                                 pred.detach().cpu().numpy()))\n#             valid_auc.append(calc_valid_auc(lstmnet))\n    print('[Epoch {}/{}] [Loss: {}]'.format(epoch+1, opt.n_epochs, loss_his[-1]))\n    \ntorch.save(lstmnet.state_dict(), 'model.pt')","metadata":{"execution":{"iopub.status.busy":"2024-03-27T08:51:48.467474Z","iopub.execute_input":"2024-03-27T08:51:48.467903Z","iopub.status.idle":"2024-03-27T09:19:34.860940Z","shell.execute_reply.started":"2024-03-27T08:51:48.467867Z","shell.execute_reply":"2024-03-27T09:19:34.859607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(range(len(loss_his)), loss_his, label='loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:20:16.123595Z","iopub.execute_input":"2024-03-27T09:20:16.124433Z","iopub.status.idle":"2024-03-27T09:20:16.513699Z","shell.execute_reply.started":"2024-03-27T09:20:16.124394Z","shell.execute_reply":"2024-03-27T09:20:16.512711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lstmnet.eval()\ny_pred = []\nwith torch.no_grad():\n    for x, _ in tqdm(testloader):\n        x = x.to(device)\n        pred = lstmnet(x).detach().cpu().numpy()\n        y_pred.append(pred)\ny_pred = np.concatenate(y_pred, axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:27:01.582719Z","iopub.execute_input":"2024-03-27T09:27:01.583110Z","iopub.status.idle":"2024-03-27T09:28:10.315001Z","shell.execute_reply.started":"2024-03-27T09:27:01.583080Z","shell.execute_reply":"2024-03-27T09:28:10.313594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_roc(y_true, y_pred):\n    fig, axs = plt.subplots(3, 2, figsize=(15,15))\n    for i, label in enumerate(labels):\n        fpr, tpr, _ = metrics.roc_curve(y_true[:,i], y_pred[:,i])\n        ax = axs[i//2, i%2]\n        ax.plot(fpr, tpr,linewidth=3)\n        ax.set_title(label+\" ROC\")\n        ax.plot([0, 1], [0, 1], 'k--')\n        plt.xlim([0.0, 1.0])\n        plt.ylim([0.0, 1.0])\n        plt.xlabel('False Positive Rate')\n        plt.ylabel('True Positive Rate')\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:29:16.077280Z","iopub.execute_input":"2024-03-27T09:29:16.077696Z","iopub.status.idle":"2024-03-27T09:29:16.086721Z","shell.execute_reply.started":"2024-03-27T09:29:16.077644Z","shell.execute_reply":"2024-03-27T09:29:16.085735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_gt.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:28:44.780589Z","iopub.execute_input":"2024-03-27T09:28:44.781003Z","iopub.status.idle":"2024-03-27T09:28:44.787352Z","shell.execute_reply.started":"2024-03-27T09:28:44.780970Z","shell.execute_reply":"2024-03-27T09:28:44.786231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc(valid_gt, y_pred)\nprint('auc roc: ', metrics.roc_auc_score(valid_gt, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:29:22.204484Z","iopub.execute_input":"2024-03-27T09:29:22.205291Z","iopub.status.idle":"2024-03-27T09:29:25.434657Z","shell.execute_reply.started":"2024-03-27T09:29:22.205252Z","shell.execute_reply":"2024-03-27T09:29:25.433626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Visualization","metadata":{}},{"cell_type":"code","source":"from tensorboardX import SummaryWriter\nlstmnet = LSTMNet()\nlstmnet.to(device)\nx.to(device)\n# 创建一个SummaryWriter实例\nwriter = SummaryWriter('./runs/model_visualization')\n\n# 添加模型的计算图\nwriter.add_graph(lstmnet, x)\n\n# 关闭SummaryWriter\nwriter.close()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T09:30:09.734316Z","iopub.execute_input":"2024-03-27T09:30:09.734747Z","iopub.status.idle":"2024-03-27T09:30:27.971100Z","shell.execute_reply.started":"2024-03-27T09:30:09.734707Z","shell.execute_reply":"2024-03-27T09:30:27.970133Z"},"trusted":true},"execution_count":null,"outputs":[]}]}