{"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":"!pip install albumentations==0.4.6\nimport albumentations ","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:36:05.127544Z","iopub.execute_input":"2023-06-01T13:36:05.128181Z","iopub.status.idle":"2023-06-01T13:36:22.031082Z","shell.execute_reply.started":"2023-06-01T13:36:05.128129Z","shell.execute_reply":"2023-06-01T13:36:22.029884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os, os.path\nfrom PIL import Image              # from RBG to YCbCr\nimport sys, timeit, math, copy, random\nsys.path.append(\"../input/timmmaster/\")\n# Basics\nimport pandas as pd\nimport numpy as np\nfrom numpy import pi                # for DCT\nfrom numpy import r_                # for DCT\nimport scipy                        # for cosine similarity\nfrom scipy import fftpack           # for DCT\nimport random\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg    # to check images\n%matplotlib inline\nfrom tqdm.notebook import tqdm      # beautiful progression bar\n# SKlearn\nfrom sklearn.model_selection import KFold\nfrom sklearn import metrics\n\n# PyTorch\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch import FloatTensor, LongTensor\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nimport torch.nn.functional as F\n\n# Data Augmentation for Image Preprocessing\nfrom albumentations import (ToFloat, Normalize, VerticalFlip, HorizontalFlip, Compose, Resize,\n                            RandomBrightness, RandomContrast, HueSaturationValue, Blur, GaussNoise, RandomGridShuffle)\n#from albumentations.pytorch import ToTensorV2, ToTensor\nfrom torchvision.models import resnet34\n#from torchvision import transforms as transforms\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport transformers","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:36:33.231289Z","iopub.execute_input":"2023-06-01T13:36:33.232937Z","iopub.status.idle":"2023-06-01T13:36:33.250989Z","shell.execute_reply.started":"2023-06-01T13:36:33.232890Z","shell.execute_reply":"2023-06-01T13:36:33.249823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from albumentations.pytorch import ToTensorV2\nimport timm\nfrom sklearn.metrics import average_precision_score","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:39:46.150967Z","iopub.execute_input":"2023-06-01T13:39:46.151813Z","iopub.status.idle":"2023-06-01T13:39:47.158522Z","shell.execute_reply.started":"2023-06-01T13:39:46.151768Z","shell.execute_reply":"2023-06-01T13:39:47.157314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"!git clone https://github.com/dwgoon/jpegio\n!pip install jpegio/.\nimport jpegio as jio\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed = 1234):\n    '''Sets the seed of the entire notebook so results are the same every time we run.\n    This is for REPRODUCIBILITY.'''\n    np.random.seed(seed)\n    random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    # When running on the CuDNN backend, two further options must be set\n    torch.backends.cudnn.deterministic = True\n    # Set a fixed value for the hash seed\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    \nset_seed()\n\ndevice = \"cuda\"\nprint('Device available now:', device)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:40:00.140921Z","iopub.execute_input":"2023-06-01T13:40:00.141335Z","iopub.status.idle":"2023-06-01T13:40:00.152659Z","shell.execute_reply.started":"2023-06-01T13:40:00.141296Z","shell.execute_reply":"2023-06-01T13:40:00.151435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path1='../input/alaska2-image-steganalysis'\ndef append_path(pre):\n    return np.vectorize(lambda file: os.path.join(path1, pre, file))\ntrain_filenames_cover = np.array(os.listdir(\"../input/alaska2-image-steganalysis/Cover\"))\ntrain_filenames_cover=sorted(train_filenames_cover)\npositives = train_filenames_cover.copy()\nneg_train_path1 = append_path('Cover')(positives[:8000])\nneg_train_path2 = append_path('Cover')(positives[10000:18000])\nneg_train_path3 = append_path('Cover')(positives[20000:28000])\nned_valid_path1 = append_path('Cover')(positives[8000:9000])\nned_valid_path2 = append_path('Cover')(positives[18000:19000])\nned_valid_path3 = append_path('Cover')(positives[28000:29000])\nneg_test_path1 = append_path('Cover')(positives[9000:10000])\nneg_test_path2 = append_path('Cover')(positives[19000:20000])\nneg_test_path3 = append_path('Cover')(positives[29000:30000])\n\njmi_train_path=append_path('JMiPOD')(positives[:8000])\njmi_valid_path=append_path('JMiPOD')(positives[8000:9000])\njmi_test_path=append_path('JMiPOD')(positives[9000:10000])\njun_train_path=append_path('JUNIWARD')(positives[10000:18000])\njun_valid_path=append_path('JUNIWARD')(positives[18000:19000])\njun_test_path=append_path('JUNIWARD')(positives[19000:20000])\nue_train_path=append_path('UERD')(positives[20000:28000])\nue_valid_path=append_path('UERD')(positives[28000:29000])\nue_test_path=append_path('UERD')(positives[29000:30000])\n\ntrain_paths = np.concatenate([neg_train_path1, neg_train_path2, neg_train_path3, jmi_train_path, jun_train_path, ue_train_path])\nvalid_paths= np.concatenate([ned_valid_path1, ned_valid_path2, ned_valid_path3, jmi_valid_path, jun_valid_path, ue_valid_path])\ntest_paths=np.concatenate([neg_test_path1, neg_test_path2, neg_test_path3, jmi_test_path, jun_test_path, ue_test_path])\ntrain_labels = np.array([0] * len(neg_train_path1) + [0] * len(neg_train_path2)+[0] * len(neg_train_path3)+[1] * len(jmi_train_path)+ [1] * len(jun_train_path)+[1] * len(ue_train_path))\nvalid_labels = np.array([0] * len(ned_valid_path1) + [0] * len(ned_valid_path2)+[0] * len(ned_valid_path3)+[1] * len(jmi_valid_path)+ [1] * len(jun_valid_path)+[1] * len(ue_valid_path))\ntest_labels = np.array([0] * len(neg_test_path1) + [0] * len(neg_test_path2)+[0] * len(neg_test_path3)+[1] * len(jmi_test_path)+ [1] * len(jun_test_path)+[1] * len(ue_test_path))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:57:50.512677Z","iopub.execute_input":"2023-05-24T11:57:50.513668Z","iopub.status.idle":"2023-05-24T11:57:51.440900Z","shell.execute_reply.started":"2023-05-24T11:57:50.513610Z","shell.execute_reply":"2023-05-24T11:57:51.439706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"path1='../input/alaska2-image-steganalysis'\ndef append_path(pre):\n    return np.vectorize(lambda file: os.path.join(path1, pre, file))\ntrain_filenames_cover = np.array(os.listdir(\"../input/alaska2-image-steganalysis/Cover\"))\ntrain_filenames_cover=sorted(train_filenames_cover)\npositives = train_filenames_cover.copy()\nneg_train_path1 = append_path('Cover')(positives[:2000])\nneg_train_path2 = append_path('Cover')(positives[3000:5000])\nneg_train_path3 = append_path('Cover')(positives[6000:8000])\nned_valid_path1 = append_path('Cover')(positives[2000:2250])\nned_valid_path2 = append_path('Cover')(positives[5000:5250])\nned_valid_path3 = append_path('Cover')(positives[8000:8250])\nneg_test_path1 = append_path('Cover')(positives[2250:2500])\nneg_test_path2 = append_path('Cover')(positives[5250:5500])\nneg_test_path3 = append_path('Cover')(positives[8250:8500])\n\njmi_train_path=append_path('JMiPOD')(positives[:2000])\njmi_valid_path=append_path('JMiPOD')(positives[2000:2250])\njmi_test_path=append_path('JMiPOD')(positives[2250:2500])\njun_train_path=append_path('JUNIWARD')(positives[3000:5000])\njun_valid_path=append_path('JUNIWARD')(positives[5000:5250])\njun_test_path=append_path('JUNIWARD')(positives[5250:5500])\nue_train_path=append_path('UERD')(positives[6000:8000])\nue_valid_path=append_path('UERD')(positives[8000:8250])\nue_test_path=append_path('UERD')(positives[8250:8500])\n\ntrain_paths = np.concatenate([neg_train_path1, neg_train_path2, neg_train_path3, jmi_train_path, jun_train_path, ue_train_path])\nvalid_paths= np.concatenate([ned_valid_path1, ned_valid_path2, ned_valid_path3, jmi_valid_path, jun_valid_path, ue_valid_path])\ntest_paths=np.concatenate([neg_test_path1, neg_test_path2, neg_test_path3, jmi_test_path, jun_test_path, ue_test_path])\ntrain_labels = np.array([0] * len(neg_train_path1) + [0] * len(neg_train_path2)+[0] * len(neg_train_path3)+[1] * len(jmi_train_path)+ [1] * len(jun_train_path)+[1] * len(ue_train_path))\nvalid_labels = np.array([0] * len(ned_valid_path1) + [0] * len(ned_valid_path2)+[0] * len(ned_valid_path3)+[1] * len(jmi_valid_path)+ [1] * len(jun_valid_path)+[1] * len(ue_valid_path))\ntest_labels = np.array([0] * len(neg_test_path1) + [0] * len(neg_test_path2)+[0] * len(neg_test_path3)+[1] * len(jmi_test_path)+ [1] * len(jun_test_path)+[1] * len(ue_test_path))\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-05-23T10:55:34.143253Z","iopub.execute_input":"2023-05-23T10:55:34.143636Z","iopub.status.idle":"2023-05-23T10:55:35.192119Z","shell.execute_reply.started":"2023-05-23T10:55:34.143602Z","shell.execute_reply":"2023-05-23T10:55:35.191028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"path='../input/alaska2-image-steganalysis'\ndef append_path(pre):\n    return np.vectorize(lambda file: os.path.join(path, pre, file))\ntrain_filenames_cover = np.array(os.listdir(\"../input/alaska2-image-steganalysis/Cover\"))\ntrain_filenames_cover=sorted(train_filenames_cover)\npositives = train_filenames_cover.copy()\nneg_train_path = append_path('Cover')(positives[:12000])\nneg_valid_path = append_path('Cover')(positives[12000:13500])\nneg_test_path = append_path('Cover')(positives[13500:15000])\njun_train_path=append_path('JUNIWARD')(positives[:12000])\njun_valid_path=append_path('JUNIWARD')(positives[12000:13500])\njun_test_path=append_path('JUNIWARD')(positives[13500:15000])\n\ntrain_paths = np.concatenate([neg_train_path, jun_train_path])\nvalid_paths= np.concatenate([neg_valid_path, jun_valid_path])\ntest_paths=np.concatenate([neg_test_path, jun_test_path])\ntrain_labels = np.array([0] * len(neg_train_path) + [1] * len(jun_train_path))\nvalid_labels = np.array([0] * len(neg_valid_path) + [1] * len(jun_valid_path))\ntest_labels = np.array([0] * len(neg_test_path) + [1] * len(jun_test_path))\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train_df=pd.DataFrame({'Id': train_paths, 'Label': train_labels})\nvalid_df=pd.DataFrame({'Id': valid_paths, 'Label': valid_labels})\ntest_df=pd.DataFrame({'Id': test_paths, 'Label': test_labels})\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train_df.head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"path1='../input/data-ycrbr1'\ndef append_path1(pre):\n    return np.vectorize(lambda file: os.path.join(path1, pre, file))\npath2='../input/data-ycrbr2'\ndef append_path2(pre):\n    return np.vectorize(lambda file: os.path.join(path2, pre, file))\npath3='../input/data-ycrbr3'\ndef append_path3(pre):\n    return np.vectorize(lambda file: os.path.join(path3, pre, file))\npath4='../input/data-ycrbr4'\ndef append_path4(pre):\n    return np.vectorize(lambda file: os.path.join(path4, pre, file))\npath5='../input/data-ycrbr5'\ndef append_path5(pre):\n    return np.vectorize(lambda file: os.path.join(path5, pre, file))\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train_filenames_cover1 = np.array(os.listdir(\"../input/data-ycrbr1/cover1\"))\ntrain_filenames_cover1=sorted(train_filenames_cover1)\ntrain_filenames_cover2 = np.array(os.listdir(\"../input/data-ycrbr2/cover2\"))\ntrain_filenames_cover2=sorted(train_filenames_cover2)\ntrain_filenames_cover3 = np.array(os.listdir(\"../input/data-ycrbr3/cover3\"))\ntrain_filenames_cover3=sorted(train_filenames_cover3)\ntrain_filenames_cover4 = np.array(os.listdir(\"../input/data-ycrbr4/cover4\"))\ntrain_filenames_cover4=sorted(train_filenames_cover4)\ntrain_filenames_cover5 = np.array(os.listdir(\"../input/data-ycrbr5/cover5\"))\ntrain_filenames_cover5=sorted(train_filenames_cover5)\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"positives1 = train_filenames_cover1.copy()\npositives2 = train_filenames_cover2.copy()\npositives3 = train_filenames_cover3.copy()\npositives4 = train_filenames_cover4.copy()\npositives5 = train_filenames_cover5.copy()\nneg_train_path1 = append_path1('cover1')(positives1[:3000])\nneg_train_path2 = append_path2('cover2')(positives2[:3000])\nneg_train_path3 = append_path3('cover3')(positives3[:3000])\nneg_train_path4 = append_path4('cover4')(positives4[:3000])\nned_valid_path = append_path5('cover5')(positives5[:1500])\nneg_test_path = append_path5('cover5')(positives5[1500:3000])\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"jun_train_path1=append_path1('JUN1')(positives1[:3000])\njun_train_path2=append_path2('JUN2')(positives2[:3000])\njun_train_path3=append_path3('JUN3')(positives3[:3000])\njun_train_path4=append_path4('JUN4')(positives4[:3000])\njun_valid_path=append_path5('JUN5')(positives5[:1500])\njun_test_path=append_path5('JUN5')(positives5[1500:3000])\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train_paths1 = np.concatenate([neg_train_path1, neg_train_path2, neg_train_path3,neg_train_path4, jun_train_path1, jun_train_path2, jun_train_path3, jun_train_path4])\nvalid_paths1= np.concatenate([ned_valid_path, jun_valid_path])\ntest_paths1=np.concatenate([neg_test_path,jun_test_path])\ntrain_labels1 = np.array([0] * len(neg_train_path1) + [0] * len(neg_train_path2)+[0] * len(neg_train_path3)+[0] * len(neg_train_path4)+[1] * len(jun_train_path1)+ [1] * len(jun_train_path2)+[1] * len(jun_train_path3)+[1] * len(jun_train_path4))\nvalid_labels1 = np.array([0] * len(ned_valid_path) +[1] * len(jun_valid_path))\ntest_labels1 = np.array([0] * len(neg_test_path) + [1] * len(jun_test_path))\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path1='/kaggle/input/cover-and-jun2'\ndef append_path1(pre):\n    return np.vectorize(lambda file: os.path.join(path1, pre, file))\ntrain_filenames_cover1 = np.array(os.listdir(\"/kaggle/input/cover-and-jun2/cover2\"))\ntrain_filenames_cover1=sorted(train_filenames_cover1)\npositives1 = train_filenames_cover1.copy()\nneg_train_path1 = append_path1('cover2')(positives1[:2400])\nned_valid_path= append_path1('cover2')(positives1[2400:2700])\nneg_test_path = append_path1('cover2')(positives1[2700:3000])\njun_train_path1=append_path1('JUN2')(positives1[:2400])\njun_valid_path=append_path1('JUN2')(positives1[2400:2700])\njun_test_path=append_path1('JUN2')(positives1[2700:3000])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:50:11.921382Z","iopub.execute_input":"2023-06-01T13:50:11.921968Z","iopub.status.idle":"2023-06-01T13:50:11.963315Z","shell.execute_reply.started":"2023-06-01T13:50:11.921922Z","shell.execute_reply":"2023-06-01T13:50:11.962254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_paths = np.concatenate([neg_train_path1, jun_train_path1])\nvalid_paths= np.concatenate([ned_valid_path, jun_valid_path])\ntest_paths=np.concatenate([neg_test_path,jun_test_path])\ntrain_labels = np.array([0] * len(neg_train_path1)+ [1] * len(jun_train_path1))\nvalid_labels = np.array([0] * len(ned_valid_path) +[1] * len(jun_valid_path))\ntest_labels = np.array([0] * len(neg_test_path) + [1] * len(jun_test_path))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:50:15.470798Z","iopub.execute_input":"2023-06-01T13:50:15.471319Z","iopub.status.idle":"2023-06-01T13:50:15.482328Z","shell.execute_reply.started":"2023-06-01T13:50:15.471260Z","shell.execute_reply":"2023-06-01T13:50:15.481061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1_df=pd.DataFrame({'Id': train_paths, 'Label': train_labels})\nvalid1_df=pd.DataFrame({'Id': valid_paths, 'Label': valid_labels})\ntest1_df=pd.DataFrame({'Id': test_paths, 'Label': test_labels})","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:50:19.775171Z","iopub.execute_input":"2023-06-01T13:50:19.776230Z","iopub.status.idle":"2023-06-01T13:50:19.787255Z","shell.execute_reply.started":"2023-06-01T13:50:19.776177Z","shell.execute_reply":"2023-06-01T13:50:19.786165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path2='/kaggle/input/alaska2-image-steganalysis'\ndef append_path2(pre):\n    return np.vectorize(lambda file: os.path.join(path2, pre, file))\ntrain_filenames_cover = np.array(os.listdir(\"/kaggle/input/alaska2-image-steganalysis/Cover\"))\ntrain_filenames_cover=sorted(train_filenames_cover)\npositives = train_filenames_cover.copy()\nneg_train_path2 = append_path2('Cover')(positives[3000:5400])\nned_valid_path2= append_path2('Cover')(positives[5400:5700])\nneg_test_path2 = append_path2('Cover')(positives[5700:6000])\njun_train_path2=append_path2('JUNIWARD')(positives[3000:5400])\njun_valid_path2=append_path2('JUNIWARD')(positives[5400:5700])\njun_test_path2=append_path2('JUNIWARD')(positives[5700:6000])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:52:01.510846Z","iopub.execute_input":"2023-06-01T13:52:01.511603Z","iopub.status.idle":"2023-06-01T13:52:01.698585Z","shell.execute_reply.started":"2023-06-01T13:52:01.511560Z","shell.execute_reply":"2023-06-01T13:52:01.697449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pathss = np.concatenate([neg_train_path2, jun_train_path2])\nvalid_pathss= np.concatenate([ned_valid_path2, jun_valid_path2])\ntest_pathss=np.concatenate([neg_test_path2,jun_test_path2])\ntrain_labelss = np.array([0] * len(neg_train_path2)+ [1] * len(jun_train_path2))\ntest_labelss = np.array([0] * len(neg_test_path2) + [1] * len(jun_test_path2))\nvalid_labelss = np.array([0] * len(ned_valid_path2) +[1] * len(jun_valid_path2))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:53:15.980534Z","iopub.execute_input":"2023-06-01T13:53:15.980924Z","iopub.status.idle":"2023-06-01T13:53:15.991592Z","shell.execute_reply.started":"2023-06-01T13:53:15.980890Z","shell.execute_reply":"2023-06-01T13:53:15.990356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train2_df=pd.DataFrame({'Id': train_pathss, 'Label': train_labelss})\nvalid2_df=pd.DataFrame({'Id': valid_pathss, 'Label': valid_labelss})\ntest2_df=pd.DataFrame({'Id': test_pathss, 'Label': test_labelss})","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:53:19.607110Z","iopub.execute_input":"2023-06-01T13:53:19.607842Z","iopub.status.idle":"2023-06-01T13:53:19.618788Z","shell.execute_reply.started":"2023-06-01T13:53:19.607798Z","shell.execute_reply":"2023-06-01T13:53:19.617611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1_df.head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train1_df.to_csv('train1.csv', index=False)\nvalid1_df.to_csv('valid1.csv', index=False)\ntest1_df.to_csv('test1.csv', index=False)\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train1_df = pd.read_csv('/kaggle/working/train1.csv', \n                       header=0, names=['Path', 'Label'], dtype = {'Label':np.int32})\nvalid1_df = pd.read_csv('/kaggle/working/valid1.csv', \n                       header=0, names=['Path', 'Label'], dtype = {'Label':np.int32})\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train1_df['Label'] = train1_df['Label'].astype('float32')\ntrain1_df.dtypes\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"class AlaskaDatasetTrain(Dataset):\n    def __init__(self, dataframe):\n        self.dataframe = dataframe\n    def __len__(self):\n        return len(self.dataframe)\n    def __getitem__(self, idx):\n        img_path = self.dataframe.iloc[idx, 0]\n        image = read_image(img_path)\n        label = self.dataframe.iloc[idx, 1]\n        return image, label\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"prom = np.zeros(shape = (512, 512, 3), dtype=\"float32\")\nkoef = np.zeros(shape = (512, 512, 3), dtype=\"float32\")\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"20it [00:12,  1.58it/s]\ntorch.Size([672, 3, 512, 512])\ntensor(-0.3711) tensor(21.0777)\ntensor(-0.2116) tensor(6.0352)\ntensor(0.1129) tensor(5.8410)\"\"\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"torch.Size([672, 3, 512, 512])\ntensor(-0.0687) tensor(0.4390)\ntensor(-0.0397) tensor(0.1255)\ntensor(0.0236) tensor(0.1230)\"\"\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDatasetTrain(Dataset):\n    def __init__(self, dataframe1, dataframe2):\n        self.dataframe1 = dataframe1\n        self.dataframe2 = dataframe2\n        self.transform_image_dct = Compose([\n            Normalize(\n            mean=[-0.3711, -0.2116, 0.1129],\n            std=[21.0777, 6.0352, 5.8410],\n            max_pixel_value=1.0), \n            ToTensorV2()])\n        self.transform_image = Compose([Normalize(\n            mean=[0.485, 0.456, 0.406],\n            std=[0.229, 0.224, 0.225],\n            max_pixel_value=255.0), ToTensorV2()])\n    def __len__(self):\n        return len(self.dataframe2)\n    def __getitem__(self, idx):\n            #koef = np.zeros(shape = (512, 512, 3), dtype=\"float32\")\n        img_path1 = self.dataframe1.iloc[idx, 0]\n            #jpeg = jio.read(img_path)\n        image = cv2.imread(img_path1)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        img_path2 = self.dataframe2.iloc[idx, 0]\n        koef=np.load(img_path2)\n            #koef[:,:,0] = jpeg.coef_arrays[0] ; koef[:,:,1] = jpeg.coef_arrays[1] ; koef[:,:,2] = jpeg.coef_arrays[2]\n            #koef = np.array(koef, dtype=np.float32)\n        koef = self.transform_image_dct(image = koef)[\"image\"]\n        label = self.dataframe2.iloc[idx, 1]\n        label = torch.tensor(label, dtype=torch.float32)\n        image = self.transform_image(image = image)[\"image\"]\n            #image_dct=torch.concat([image,koef],dim=0)\n        image=np.array(image)\n            #koef=np.array(koef)\n        image_dct=np.concatenate([image,koef],axis=0)\n        image_dct=torch.tensor(image_dct)\n            #koef=torch.tensor(koef)\n        return image_dct, label","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:53:26.911633Z","iopub.execute_input":"2023-06-01T13:53:26.912480Z","iopub.status.idle":"2023-06-01T13:53:26.930467Z","shell.execute_reply.started":"2023-06-01T13:53:26.912437Z","shell.execute_reply":"2023-06-01T13:53:26.929344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDatasetTrain(Dataset):\n    def __init__(self, dataframe1, is_val, vertical_flip=0.5, horizontal_flip=0.5):\n        self.dataframe1 = dataframe1\n        self.is_val = is_val\n        flag = is_val\n        if flag:\n            self.transform_image = Compose([Resize(512, 512),\n            Normalize(mean=[0.485, 0.456, 0.406],\n            std=[0.229, 0.224, 0.225],\n            max_pixel_value=255.0),\n                                        ToTensorV2()])\n        else:\n            # Compose transforms and handle all transformations regarding bounding boxes\n            self.transform_image = Compose([Resize(512, 512), \n                                      VerticalFlip(p = vertical_flip),\n                                      HorizontalFlip(p = horizontal_flip),RandomGridShuffle(grid=(40, 40), always_apply=False, p=0.5), \n                                      Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225],max_pixel_value=255.0),\n                                ToTensorV2()])\n    def __len__(self):\n        return len(self.dataframe1)\n    def __getitem__(self, idx): \n        img_path1 = self.dataframe1.iloc[idx, 0]\n        #image = cv2.imread(img_path1)\n        #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        ycb=np.array(Image.open(img_path1).convert('YCbCr'))\n        ycb = self.transform_image(image = ycb)[\"image\"]\n        label = self.dataframe1.iloc[idx, 1]\n        label = torch.tensor(label, dtype=torch.float32)\n        #image = self.transform_image(image = image)[\"image\"]\n            #image_dct=torch.concat([image,koef],dim=0)\n        #image=np.array(image)\n            #koef=np.array(koef)\n        #image_dct=np.concatenate([image,koef],axis=0)\n        #image_dct=torch.tensor(image_dct)\n            #koef=torch.tensor(koef)\n        return ycb, label","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:59:34.057163Z","iopub.execute_input":"2023-05-24T11:59:34.057898Z","iopub.status.idle":"2023-05-24T11:59:34.071613Z","shell.execute_reply.started":"2023-05-24T11:59:34.057854Z","shell.execute_reply":"2023-05-24T11:59:34.069922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ex_data=CustomDatasetTrain(train1_df,is_val=False)\nex_loader=torch.utils.data.DataLoader(ex_data, batch_size = 1, shuffle=True, num_workers = 4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#example_data = AlaskaDatasetTrain(train_df)\n#example_loader = torch.utils.data.DataLoader(example_data, batch_size = 1, shuffle=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"for image, label in example_loader:\n    images_example = image\n    label_example = torch.tensor(label, dtype=torch.long)\n    break\nprint('Images shape:', images_example.shape)\nprint('Labels:', label, '\\n')\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_workers = 2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"images=[]\nfor i, (image, label) in tqdm(enumerate(ex_loader)):\n    images.append(image)\n    if i==20:\n        break\nimages_res=torch.concat(images,dim=0)\nprint(images_res.shape)\nfor i in range(3):\n    print(images_res[:,i,:,:].mean(), images_res[:,i,:,:].std())\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDatasetTest(Dataset):\n    def __init__(self, dataframe):\n        self.dataframe = dataframe\n        self.transform_image = Compose([\n            Normalize(\n            mean=[0.485, 0.456, 0.406],\n            std=[0.229, 0.224, 0.225],\n            max_pixel_value=255.0),\n                                        ToTensorV2()])\n    def __len__(self):\n        return len(self.dataframe)\n    def __getitem__(self, idx):\n        img_path1 = self.dataframe.iloc[idx, 0]\n        ycb=np.array(Image.open(img_path1).convert('YCbCr'))\n        ycb = self.transform_image(image = ycb)[\"image\"]\n        #image = cv2.imread(img_path1)\n        #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        #image = self.transform_image(image = image)[\"image\"]\n        return ycb","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:59:40.057532Z","iopub.execute_input":"2023-05-24T11:59:40.058511Z","iopub.status.idle":"2023-05-24T11:59:40.066964Z","shell.execute_reply.started":"2023-05-24T11:59:40.058429Z","shell.execute_reply":"2023-05-24T11:59:40.065816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"class AlaskaDatasetTest(Dataset):\n    def __init__(self, dataframe):\n        self.dataframe = dataframe\n    def __len__(self):\n        return len(self.dataframe)\n    def __getitem__(self, idx):\n        img_path = self.dataframe.iloc[idx, 0]\n        image = read_image(img_path)\n        return image\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train Data Size:', len(train1_df), '\\n' +\n      'Valid Data Size:', len(valid1_df), '\\n' +\n      '----------------------', '\\n' +\n      'Total:', len(train1_df) + len(valid1_df))\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize = (14, 5))\n\nsns.countplot(x = train1_df['Label'], ax = ax1, palette = sns.color_palette(\"GnBu_d\", 10))\nsns.countplot(x = valid1_df['Label'], ax = ax2, palette = sns.color_palette(\"YlOrRd\", 10))\n\nax1.set_title('Train Data', fontsize=16)\nax2.set_title('Valid Data', fontsize=16);","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:46:12.324596Z","iopub.execute_input":"2023-06-01T13:46:12.325209Z","iopub.status.idle":"2023-06-01T13:46:12.716106Z","shell.execute_reply.started":"2023-06-01T13:46:12.325169Z","shell.execute_reply":"2023-06-01T13:46:12.713689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BaseSwin(nn.Module):\n    def __init__(self, name=\"tf_efficientnet_b0_ns\", pretrained=True, n_classes = 1):\n        super(BaseSwin, self).__init__()\n        self.model = timm.create_model(name, pretrained=pretrained, in_chans=6)\n        self.n_classes = n_classes\n        self.n_features = self.model.classifier.in_features\n        self.model.classifier = nn.Identity()\n        self.fc = nn.Linear(self.n_features, self.n_classes)\n\n    def forward(self, x):\n        x = self.model(x)\n        x = self.fc(x)\n        if self.n_classes == 1:\n            return x.ravel()\n        else:\n            return x","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:46:20.008364Z","iopub.execute_input":"2023-06-01T13:46:20.009562Z","iopub.status.idle":"2023-06-01T13:46:20.019587Z","shell.execute_reply.started":"2023-06-01T13:46:20.009518Z","shell.execute_reply":"2023-06-01T13:46:20.018048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"class EfficientNetwork(nn.Module):\n    def __init__(self):\n        super().__init__()\n        # Define Feature part\n        self.features = EfficientNet.from_pretrained('efficientnet-b3')\n        # Define Classification part\n        self.classification = nn.Linear(1408, 1)\n        #self.avg_pool=nn.AvgPool2d(kernel_size=3, stride=2, padding=0, ceil_mode=False,\n            #count_include_pad=False)\n    def forward(self, image, prints=False):\n        if prints: print('Input Image shape:', image.shape)\n        \n        image = self.features.extract_features(image)\n        print('Features Image shape:', image.shape)\n        \n        image = F.avg_pool2d(image, image.size()[2:]).reshape(-1, 1408)\n        if prints: print('Image Reshaped shape:', image.shape)\n\n        \n        out = self.classification(image)\n        if prints: print('Out shape:', out.shape)\n        \n        return torch.sigmoid(out)\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create an example model (B7)\nmodel_example = BaseSwin()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:42:23.576400Z","iopub.execute_input":"2023-06-01T13:42:23.577647Z","iopub.status.idle":"2023-06-01T13:42:24.876861Z","shell.execute_reply.started":"2023-06-01T13:42:23.577597Z","shell.execute_reply":"2023-06-01T13:42:24.875346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train1_data = CustomDatasetTrain(train1_df, is_val=False)\n#valid1_data = CustomDatasetTrain(valid1_df, is_val=True)\ntrain1_data = CustomDatasetTrain(train2_df,train1_df)\nvalid1_data = CustomDatasetTrain(valid2_df,valid1_df)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:53:36.315340Z","iopub.execute_input":"2023-06-01T13:53:36.316175Z","iopub.status.idle":"2023-06-01T13:53:36.322671Z","shell.execute_reply.started":"2023-06-01T13:53:36.316132Z","shell.execute_reply":"2023-06-01T13:53:36.321274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1_data[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train_jmi_df=pd.DataFrame({'Id': train_paths, 'Label': train_labels})\nvalid_jmi_df=pd.DataFrame({'Id': valid_paths, 'Label': valid_labels})\ntest_jmi_df=pd.DataFrame({'Id': test_paths, 'Label': test_labels})\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_jmi_data = CustomDatasetTrain(train_jmi_df)\n#valid_jmi_data = CustomDatasetTrain(valid_jmi_df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(model, epochs, batch_size, num_workers, learning_rate, weight_decay, \n          version = 'vx'):\n    # Create file to save logs\n    f = open(f\"logs_{version}.txt\", \"w+\")  \n    \n    # Data Loaders\n    train_loader = torch.utils.data.DataLoader(train1_data, batch_size=batch_size, num_workers=num_workers,\n                                              drop_last=True, shuffle=True)\n    valid_loader = torch.utils.data.DataLoader(valid1_data, batch_size=batch_size, num_workers=num_workers,\n                                              drop_last=True, shuffle=True)\n\n    # Criterion\n    criterion = torch.nn.BCEWithLogitsLoss()\n    # Optimizer\n    optimizer = optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)\n\n\n    train_losses = []\n    evaluation_losses = []\n    AP_l=[]\n\n    for epoch in range(epochs):\n\n        # Sets the model in training mode\n        model.train()\n\n        train_loss = 0\n\n        for images, labels in tqdm(train_loader):\n            # Need to access the images\n            images = images.to(device)\n            labels = labels.to(device)\n\n            # Clear gradients\n            optimizer.zero_grad()\n\n            # Make prediction\n            out = model(images)\n\n            # Compute loss and Backpropagate\n            loss = criterion(out, labels)\n            loss.backward()\n            optimizer.step()\n\n            train_loss += loss.item()\n            #gc.collect()\n\n        #Compute average epoch loss\n        epoch_loss_train = train_loss / batch_size\n        train_losses.append(epoch_loss_train)\n        #gc.collect()\n\n\n        # ===== Evaluate =====\n        model.eval()\n\n        evaluation_loss = 0\n        actuals, predictions = [], []\n        num_correct = 0\n        num_samples = 0    \n\n        # To disable gradients\n        with torch.no_grad():\n            for images, labels in tqdm(valid_loader):\n                images = images.to(device)\n                labels = labels.to(device)\n\n                # Prediction\n                out = model(images)\n                loss = criterion(out, labels)\n                evaluation_loss += loss.item()\n                actuals.append(labels.cpu())\n                predictions.append(out.cpu())\n                #gc.collect() \n        # Compute epoch loss\n        epoch_loss_eval = evaluation_loss/batch_size\n        evaluation_losses.append(epoch_loss_eval)\n        AP=average_precision_score(torch.concat(actuals),torch.concat(predictions))\n        AP_l.append(AP)\n        #print(average_precision_score(torch.concat(actuals),torch.concat(predictions)))\n        with open(f\"logs_{version}.txt\", 'a+') as f:\n            print('Epoch: {}/{} | Train Loss: {:.3f} | Eval Loss: {:.3f} | AP: {:.3f}'.\\\n                     format(epoch+1, epochs, epoch_loss_train, epoch_loss_eval, AP),file=f)\n        \n        print('Epoch: {}/{} | Train Loss: {:.3f} | Eval Loss: {:.3f} | AP: {:.3f}'.\\\n              format(epoch+1, epochs, epoch_loss_train, epoch_loss_eval, AP))\n        \n    plt.figure(figsize=(9, 15))\n    plt.subplot(3, 1, 1)\n    plt.plot(train_losses, c='#00008b', lw = 2)\n    plt.title(\"Model train loss\") # заголовок\n    plt.ylabel(\"loss\", fontsize=12) # ось ординат\n    plt.xlabel(\"epoch\", fontsize=12)\n    plt.grid(True)                # включение отображение сетки\n        \n    plt.subplot(3, 1, 2)\n    plt.plot(evaluation_losses, c='#29896bff', lw = 2)\n    plt.title(\"Model eval loss\") # заголовок\n    plt.ylabel(\"loss\", fontsize=12) # ось ординат\n    plt.xlabel(\"epoch\", fontsize=12)\n    plt.grid(True)\n        \n    plt.subplot(3, 1, 3)\n    plt.plot(AP_l, c='#ff0000', lw = 2)\n    plt.title(\"Model AP\") # заголовок\n    plt.ylabel(\"AP\", fontsize=12) # ось ординат\n    plt.xlabel(\"epoch\", fontsize=12)\n    plt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:42:55.372217Z","iopub.execute_input":"2023-06-01T13:42:55.372631Z","iopub.status.idle":"2023-06-01T13:42:55.396839Z","shell.execute_reply.started":"2023-06-01T13:42:55.372595Z","shell.execute_reply":"2023-06-01T13:42:55.395822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"version = 'v8'\nepochs = 9\nbatch_size = 16\nnum_workers = 2\nlearning_rate = 0.0001\nweight_decay = 0.00001","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:42:59.631997Z","iopub.execute_input":"2023-06-01T13:42:59.632505Z","iopub.status.idle":"2023-06-01T13:42:59.638193Z","shell.execute_reply.started":"2023-06-01T13:42:59.632466Z","shell.execute_reply":"2023-06-01T13:42:59.637099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eff_net = BaseSwin().to(device)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:55:49.460599Z","iopub.execute_input":"2023-06-01T13:55:49.461786Z","iopub.status.idle":"2023-06-01T13:55:49.885171Z","shell.execute_reply.started":"2023-06-01T13:55:49.461741Z","shell.execute_reply":"2023-06-01T13:55:49.884085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:43:11.144115Z","iopub.execute_input":"2023-06-01T13:43:11.144518Z","iopub.status.idle":"2023-06-01T13:43:11.149756Z","shell.execute_reply.started":"2023-06-01T13:43:11.144477Z","shell.execute_reply":"2023-06-01T13:43:11.148539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train(model=eff_net, epochs=epochs, batch_size=batch_size, num_workers=num_workers, learning_rate=learning_rate, weight_decay=weight_decay)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T13:55:51.908538Z","iopub.execute_input":"2023-06-01T13:55:51.909601Z","iopub.status.idle":"2023-06-01T14:36:10.404477Z","shell.execute_reply.started":"2023-06-01T13:55:51.909541Z","shell.execute_reply":"2023-06-01T14:36:10.403303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"ошиблась со значениями","metadata":{}},{"cell_type":"code","source":"AP=[0.644,0.663, 0.677, 0.68, 0.688, 0.69, 0.704, 0.703, 0.702]","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-05-23T12:08:17.621843Z","iopub.execute_input":"2023-05-23T12:08:17.622912Z","iopub.status.idle":"2023-05-23T12:08:17.629104Z","shell.execute_reply.started":"2023-05-23T12:08:17.622844Z","shell.execute_reply":"2023-05-23T12:08:17.627606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(9, 5))\nplt.plot(AP, c='#ff0000', lw = 2)\nplt.title(\"Model AP\") # заголовок\nplt.ylabel(\"AP\", fontsize=12) # ось ординат\nplt.xlabel(\"epoch\", fontsize=12)\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T12:08:21.135288Z","iopub.execute_input":"2023-05-23T12:08:21.136267Z","iopub.status.idle":"2023-05-23T12:08:21.383857Z","shell.execute_reply.started":"2023-05-23T12:08:21.136211Z","shell.execute_reply":"2023-05-23T12:08:21.382824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_jmi_data = CustomDatasetTest(test_jmi_df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = CustomDatasetTest(test1_df)\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size = batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:15:07.620643Z","iopub.execute_input":"2023-05-24T16:15:07.621143Z","iopub.status.idle":"2023-05-24T16:15:07.628743Z","shell.execute_reply.started":"2023-05-24T16:15:07.621096Z","shell.execute_reply":"2023-05-24T16:15:07.627571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eff_net.eval()\n\nout = []\nout_res= []\nwith torch.no_grad():\n    for images in tqdm(test_loader):\n        images = images.to(device)\n        out = eff_net(images)\n        out_res.append(out.detach().cpu())","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:15:12.131817Z","iopub.execute_input":"2023-05-24T16:15:12.132793Z","iopub.status.idle":"2023-05-24T16:18:27.016868Z","shell.execute_reply.started":"2023-05-24T16:15:12.132739Z","shell.execute_reply":"2023-05-24T16:18:27.015548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=torch.concat(out_res)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:23:38.486074Z","iopub.execute_input":"2023-05-24T16:23:38.487051Z","iopub.status.idle":"2023-05-24T16:23:38.493412Z","shell.execute_reply.started":"2023-05-24T16:23:38.487011Z","shell.execute_reply":"2023-05-24T16:23:38.491825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sigmoid(x):\n    return 1 / (1 + torch.exp(-x))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:23:40.435487Z","iopub.execute_input":"2023-05-24T16:23:40.436497Z","iopub.status.idle":"2023-05-24T16:23:40.442525Z","shell.execute_reply.started":"2023-05-24T16:23:40.436411Z","shell.execute_reply":"2023-05-24T16:23:40.440997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_s=[]","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:23:43.236669Z","iopub.execute_input":"2023-05-24T16:23:43.237064Z","iopub.status.idle":"2023-05-24T16:23:43.242624Z","shell.execute_reply.started":"2023-05-24T16:23:43.237028Z","shell.execute_reply":"2023-05-24T16:23:43.241339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_s=sigmoid(pred)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:23:45.894742Z","iopub.execute_input":"2023-05-24T16:23:45.895145Z","iopub.status.idle":"2023-05-24T16:23:45.942835Z","shell.execute_reply.started":"2023-05-24T16:23:45.895107Z","shell.execute_reply":"2023-05-24T16:23:45.941747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1_df['Pred_value']=\"\"\ntest1_df.Pred_value = pred_s\ntest1_df","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:23:47.794614Z","iopub.execute_input":"2023-05-24T16:23:47.795260Z","iopub.status.idle":"2023-05-24T16:23:47.822458Z","shell.execute_reply.started":"2023-05-24T16:23:47.795220Z","shell.execute_reply":"2023-05-24T16:23:47.821176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=test1_df.Label","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:23:50.670779Z","iopub.execute_input":"2023-05-24T16:23:50.671785Z","iopub.status.idle":"2023-05-24T16:23:50.677596Z","shell.execute_reply.started":"2023-05-24T16:23:50.671717Z","shell.execute_reply":"2023-05-24T16:23:50.676269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import (classification_report, roc_curve, \nprecision_recall_curve, roc_auc_score, accuracy_score)\n#print(\"Accuracy: %f\" % accuracy_score(y_test, y_pred))\nprint(average_precision_score(y_test, pred_s))","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-05-24T16:23:52.430812Z","iopub.execute_input":"2023-05-24T16:23:52.431222Z","iopub.status.idle":"2023-05-24T16:23:52.441824Z","shell.execute_reply.started":"2023-05-24T16:23:52.431186Z","shell.execute_reply":"2023-05-24T16:23:52.440354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1_df['Label'].hist(bins=100);","metadata":{"execution":{"iopub.status.busy":"2023-05-23T15:45:08.565609Z","iopub.execute_input":"2023-05-23T15:45:08.566126Z","iopub.status.idle":"2023-05-23T15:45:08.954045Z","shell.execute_reply.started":"2023-05-23T15:45:08.566087Z","shell.execute_reply":"2023-05-23T15:45:08.952905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1_df['Pred_value'].hist(bins=100);","metadata":{"execution":{"iopub.status.busy":"2023-05-23T15:45:12.245012Z","iopub.execute_input":"2023-05-23T15:45:12.247801Z","iopub.status.idle":"2023-05-23T15:45:12.643305Z","shell.execute_reply.started":"2023-05-23T15:45:12.247734Z","shell.execute_reply":"2023-05-23T15:45:12.642292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}