{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"tez_path = '../input/tez-modified-tqdm/'\neffnet_path = '../input/pytorch-efficientnet'\nimport sys,glob\nsys.path.append(tez_path)\nsys.path.append(effnet_path)\nimport librosa\nimport cv2,os\nimport numpy as np # linear algebra\nimport pandas as pd\nimport seaborn as sns\nfrom pathlib import Path\nimport os,random\nimport matplotlib.pyplot as plt\nimport gc\nimport matplotlib.image as immg\nfrom tqdm.notebook import tqdm\nimport albumentations\nimport tez\nimport torch\nimport torch.nn as nn\nfrom torch.nn import functional as F\nfrom efficientnet_pytorch import EfficientNet\nfrom fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:56:43.191018Z","iopub.execute_input":"2021-09-06T15:56:43.191358Z","iopub.status.idle":"2021-09-06T15:56:43.198857Z","shell.execute_reply.started":"2021-09-06T15:56:43.191331Z","shell.execute_reply":"2021-09-06T15:56:43.198091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q timm","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:56:44.115897Z","iopub.execute_input":"2021-09-06T15:56:44.116272Z","iopub.status.idle":"2021-09-06T15:56:51.866158Z","shell.execute_reply.started":"2021-09-06T15:56:44.116243Z","shell.execute_reply":"2021-09-06T15:56:51.865052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings,os,gc,sys\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:56:51.868152Z","iopub.execute_input":"2021-09-06T15:56:51.868639Z","iopub.status.idle":"2021-09-06T15:56:51.876446Z","shell.execute_reply.started":"2021-09-06T15:56:51.868593Z","shell.execute_reply":"2021-09-06T15:56:51.875448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/testg2net-audio-wave-data')","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:56:51.880043Z","iopub.execute_input":"2021-09-06T15:56:51.881482Z","iopub.status.idle":"2021-09-06T15:56:51.901750Z","shell.execute_reply.started":"2021-09-06T15:56:51.881452Z","shell.execute_reply":"2021-09-06T15:56:51.900958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/train-g2net-audio-wave-data')","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:56:51.904754Z","iopub.execute_input":"2021-09-06T15:56:51.905038Z","iopub.status.idle":"2021-09-06T15:56:51.925116Z","shell.execute_reply.started":"2021-09-06T15:56:51.905011Z","shell.execute_reply":"2021-09-06T15:56:51.924276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/band-pass-traintest-g2net-audio-wave-data')","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:56:51.928479Z","iopub.execute_input":"2021-09-06T15:56:51.928772Z","iopub.status.idle":"2021-09-06T15:56:51.943654Z","shell.execute_reply.started":"2021-09-06T15:56:51.928745Z","shell.execute_reply":"2021-09-06T15:56:51.942840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/train/')","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:57:03.262696Z","iopub.execute_input":"2021-09-06T15:57:03.263054Z","iopub.status.idle":"2021-09-06T15:57:03.267435Z","shell.execute_reply.started":"2021-09-06T15:57:03.263026Z","shell.execute_reply":"2021-09-06T15:57:03.266401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -qq '../input/band-pass-traintest-g2net-audio-wave-data/TrainG2NET.zip' -d '/kaggle/working/train/'","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:57:04.696596Z","iopub.execute_input":"2021-09-06T15:57:04.696960Z","iopub.status.idle":"2021-09-06T15:59:36.719802Z","shell.execute_reply.started":"2021-09-06T15:57:04.696926Z","shell.execute_reply":"2021-09-06T15:59:36.718711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/test/')","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:59:36.721531Z","iopub.execute_input":"2021-09-06T15:59:36.721882Z","iopub.status.idle":"2021-09-06T15:59:36.728820Z","shell.execute_reply.started":"2021-09-06T15:59:36.721850Z","shell.execute_reply":"2021-09-06T15:59:36.727112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -qq '../input/band-pass-traintest-g2net-audio-wave-data/TestG2NET.zip' -d '/kaggle/working/test/'","metadata":{"execution":{"iopub.status.busy":"2021-09-06T15:59:36.730981Z","iopub.execute_input":"2021-09-06T15:59:36.731314Z","iopub.status.idle":"2021-09-06T16:00:36.394253Z","shell.execute_reply.started":"2021-09-06T15:59:36.731280Z","shell.execute_reply":"2021-09-06T16:00:36.393225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_df = pd.read_csv('../input/train-g2net-audio-wave-data/g2net_train_kfold.csv',usecols=['id','target','kfold'])","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:00:36.397863Z","iopub.execute_input":"2021-09-06T16:00:36.398149Z","iopub.status.idle":"2021-09-06T16:00:37.483472Z","shell.execute_reply.started":"2021-09-06T16:00:36.398119Z","shell.execute_reply":"2021-09-06T16:00:37.482638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:00:37.484681Z","iopub.execute_input":"2021-09-06T16:00:37.485043Z","iopub.status.idle":"2021-09-06T16:00:37.510124Z","shell.execute_reply.started":"2021-09-06T16:00:37.485005Z","shell.execute_reply":"2021-09-06T16:00:37.509400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '/kaggle/working'","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:00:37.511365Z","iopub.execute_input":"2021-09-06T16:00:37.511694Z","iopub.status.idle":"2021-09-06T16:00:37.516551Z","shell.execute_reply.started":"2021-09-06T16:00:37.511660Z","shell.execute_reply":"2021-09-06T16:00:37.514444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FOLD = 1","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:00:37.517773Z","iopub.execute_input":"2021-09-06T16:00:37.518156Z","iopub.status.idle":"2021-09-06T16:00:37.524189Z","shell.execute_reply.started":"2021-09-06T16:00:37.518120Z","shell.execute_reply":"2021-09-06T16:00:37.523415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_df1 =  trn_df[trn_df.kfold<5].reset_index(drop=True)\nval_idxs1 = trn_df1[trn_df1.kfold==FOLD].index","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:00:37.526261Z","iopub.execute_input":"2021-09-06T16:00:37.526788Z","iopub.status.idle":"2021-09-06T16:00:37.601622Z","shell.execute_reply.started":"2021-09-06T16:00:37.526749Z","shell.execute_reply":"2021-09-06T16:00:37.600639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_tfms = [Normalize.from_stats(*imagenet_stats)]","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:00:37.602903Z","iopub.execute_input":"2021-09-06T16:00:37.603451Z","iopub.status.idle":"2021-09-06T16:00:42.430231Z","shell.execute_reply.started":"2021-09-06T16:00:37.603412Z","shell.execute_reply":"2021-09-06T16:00:42.429357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(trn_df1, path=img_path, fn_col = 'id', \n                               folder='train',label_col='target',suff='.png',bs=128,\n                               item_tfms=None,batch_tfms=batch_tfms,val_idxs=val_idxs1)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:00:44.876068Z","iopub.execute_input":"2021-09-06T16:00:44.876405Z","iopub.status.idle":"2021-09-06T16:01:51.259475Z","shell.execute_reply.started":"2021-09-06T16:00:44.876377Z","shell.execute_reply":"2021-09-06T16:01:51.258555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(figsize=(20,6))","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:01:51.260989Z","iopub.execute_input":"2021-09-06T16:01:51.261328Z","iopub.status.idle":"2021-09-06T16:01:52.850588Z","shell.execute_reply.started":"2021-09-06T16:01:51.261294Z","shell.execute_reply":"2021-09-06T16:01:52.849667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dls.train_ds),len(dls.valid_ds)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:12.970132Z","iopub.execute_input":"2021-09-06T16:03:12.970482Z","iopub.status.idle":"2021-09-06T16:03:12.977694Z","shell.execute_reply.started":"2021-09-06T16:03:12.970452Z","shell.execute_reply":"2021-09-06T16:03:12.976698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.metrics as sklm","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:13.890450Z","iopub.execute_input":"2021-09-06T16:03:13.890773Z","iopub.status.idle":"2021-09-06T16:03:13.896845Z","shell.execute_reply.started":"2021-09-06T16:03:13.890745Z","shell.execute_reply":"2021-09-06T16:03:13.895980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rocauc(preds, targs):\n    return sklm.roc_auc_score(targs, preds[:,1])","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:16.050233Z","iopub.execute_input":"2021-09-06T16:03:16.050594Z","iopub.status.idle":"2021-09-06T16:03:16.054943Z","shell.execute_reply.started":"2021-09-06T16:03:16.050566Z","shell.execute_reply":"2021-09-06T16:03:16.054037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = [AccumMetric(rocauc, flatten=False)]","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:16.347568Z","iopub.execute_input":"2021-09-06T16:03:16.347844Z","iopub.status.idle":"2021-09-06T16:03:16.353702Z","shell.execute_reply.started":"2021-09-06T16:03:16.347817Z","shell.execute_reply":"2021-09-06T16:03:16.352942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:21.589098Z","iopub.execute_input":"2021-09-06T16:03:21.589446Z","iopub.status.idle":"2021-09-06T16:03:21.594995Z","shell.execute_reply.started":"2021-09-06T16:03:21.589417Z","shell.execute_reply":"2021-09-06T16:03:21.592244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:21.798861Z","iopub.execute_input":"2021-09-06T16:03:21.799209Z","iopub.status.idle":"2021-09-06T16:03:22.907831Z","shell.execute_reply.started":"2021-09-06T16:03:21.799180Z","shell.execute_reply":"2021-09-06T16:03:22.906789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_pretrained_models_available = timm.list_models(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:23.610382Z","iopub.execute_input":"2021-09-06T16:03:23.610733Z","iopub.status.idle":"2021-09-06T16:03:23.618069Z","shell.execute_reply.started":"2021-09-06T16:03:23.610699Z","shell.execute_reply":"2021-09-06T16:03:23.616277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.array(all_pretrained_models_available)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:23.958736Z","iopub.execute_input":"2021-09-06T16:03:23.959060Z","iopub.status.idle":"2021-09-06T16:03:23.962739Z","shell.execute_reply.started":"2021-09-06T16:03:23.959030Z","shell.execute_reply":"2021-09-06T16:03:23.961531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:25.610204Z","iopub.execute_input":"2021-09-06T16:03:25.610555Z","iopub.status.idle":"2021-09-06T16:03:25.821183Z","shell.execute_reply.started":"2021-09-06T16:03:25.610526Z","shell.execute_reply":"2021-09-06T16:03:25.820111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = timm.create_model('resnest50d',pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:25.873400Z","iopub.execute_input":"2021-09-06T16:03:25.873699Z","iopub.status.idle":"2021-09-06T16:03:31.888950Z","shell.execute_reply.started":"2021-09-06T16:03:25.873672Z","shell.execute_reply":"2021-09-06T16:03:31.888029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = nn.Sequential(net,\n                    nn.Dropout(0.1),\n                    nn.Linear(1000,2))","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:31.890442Z","iopub.execute_input":"2021-09-06T16:03:31.890777Z","iopub.status.idle":"2021-09-06T16:03:31.898390Z","shell.execute_reply.started":"2021-09-06T16:03:31.890742Z","shell.execute_reply":"2021-09-06T16:03:31.897553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# learn = cnn_learner(dls, resnet50,\n#                     loss_func=LabelSmoothingCrossEntropyFlat(),\n#                     metrics = [RocAucBinary()],\n#                     model_dir='/kaggle/working').to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:34.280155Z","iopub.execute_input":"2021-09-06T16:03:34.280485Z","iopub.status.idle":"2021-09-06T16:03:34.284023Z","shell.execute_reply.started":"2021-09-06T16:03:34.280456Z","shell.execute_reply":"2021-09-06T16:03:34.282894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#effnet = EfficientNet.from_pretrained('efficientnet-b0',num_classes=dls.c)\nlearn = Learner(dls, res,\n                loss_func = CrossEntropyLossFlat(), \n                metrics=[RocAucBinary()],\n                model_dir='/kaggle/working/').to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:35.548431Z","iopub.execute_input":"2021-09-06T16:03:35.548768Z","iopub.status.idle":"2021-09-06T16:03:35.554510Z","shell.execute_reply.started":"2021-09-06T16:03:35.548738Z","shell.execute_reply":"2021-09-06T16:03:35.553500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cb1 = SaveModelCallback(monitor='roc_auc_score',fname='best_model_band_pass',comp=np.greater) # Callbacks\ncb2 = ReduceLROnPlateau(monitor='roc_auc_score', min_delta=0.01, patience=2,factor=0.2)\nlearn.fit_one_cycle(10, 1e-3, cbs = [cb1,cb2])","metadata":{"execution":{"iopub.status.busy":"2021-09-06T16:03:37.459102Z","iopub.execute_input":"2021-09-06T16:03:37.459442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.load('best_model_band_pass');","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/g2net-gravitational-wave-detection/sample_submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T19:47:06.975742Z","iopub.status.idle":"2021-09-05T19:47:06.976624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_path = sub['id'].apply(lambda x:'/kaggle/working/test/'+str(x)+'.png').values\ntst_dl = learn.dls.test_dl(test_data_path)\npredictions = learn.get_preds(dl = tst_dl)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T19:47:06.978168Z","iopub.status.idle":"2021-09-05T19:47:06.979063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ts_preds = predictions[0].numpy()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T19:47:06.980501Z","iopub.status.idle":"2021-09-05T19:47:06.981296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['target'] = ts_preds[:,1]\nsub.to_csv('submissiong2net4.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T19:47:06.982687Z","iopub.status.idle":"2021-09-05T19:47:06.983629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r '/kaggle/working/test/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r '/kaggle/working/train/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}