{"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":"gpu","dataSources":[{"sourceId":10418,"databundleVersionId":862236,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os \nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\n\n%matplotlib inline\n\nfrom skimage.io import imread\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n\ntrain_folder='/kaggle/input/human-protein-atlas-image-classification/train' #path for train folder\ncells_imgs=os.listdir(train_folder)\n\nprotein_df=pd.read_csv('/kaggle/input/human-protein-atlas-image-classification/train.csv') \n\nprint(protein_df.head(2))\nprint(protein_df.info()) \nprint(f'\\n cells_img_ids: {len(cells_imgs)/4}')\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:57:41.566393Z","iopub.execute_input":"2024-09-30T14:57:41.566741Z","iopub.status.idle":"2024-09-30T14:57:45.972249Z","shell.execute_reply.started":"2024-09-30T14:57:41.566714Z","shell.execute_reply":"2024-09-30T14:57:45.971242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"current_rand_state=np.random.get_state()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:28.277583Z","iopub.execute_input":"2024-09-30T14:58:28.278181Z","iopub.status.idle":"2024-09-30T14:58:28.282292Z","shell.execute_reply.started":"2024-09-30T14:58:28.278150Z","shell.execute_reply":"2024-09-30T14:58:28.281415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 28 different labels for different unique locations where proteins can be present\n\nprotein_locs = {\n    0:  \"Nucleoplasm\",  \n    1:  \"Nuclear membrane\",   \n    2:  \"Nucleoli\",   \n    3:  \"Nucleoli fibrillar center\",   \n    4:  \"Nuclear speckles\",\n    5:  \"Nuclear bodies\",   \n    6:  \"Endoplasmic reticulum\",   \n    7:  \"Golgi apparatus\",   \n    8:  \"Peroxisomes\",   \n    9:  \"Endosomes\",   \n    10:  \"Lysosomes\",   \n    11:  \"Intermediate filaments\",   \n    12:  \"Actin filaments\",   \n    13:  \"Focal adhesion sites\",   \n    14:  \"Microtubules\",   \n    15:  \"Microtubule ends\",   \n    16:  \"Cytokinetic bridge\",   \n    17:  \"Mitotic spindle\",   \n    18:  \"Microtubule organizing center\",   \n    19:  \"Centrosome\",   \n    20:  \"Lipid droplets\",   \n    21:  \"Plasma membrane\",   \n    22:  \"Cell junctions\",   \n    23:  \"Mitochondria\",   \n    24:  \"Aggresome\",   \n    25:  \"Cytosol\",   \n    26:  \"Cytoplasmic bodies\",   \n    27:  \"Rods & rings\"\n}","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:30.214689Z","iopub.execute_input":"2024-09-30T14:58:30.215048Z","iopub.status.idle":"2024-09-30T14:58:30.221772Z","shell.execute_reply.started":"2024-09-30T14:58:30.214997Z","shell.execute_reply":"2024-09-30T14:58:30.220755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\n\nfrom skmultilearn.model_selection import iterative_train_test_split  \n#to split multicalss-multilabel data while preserving proportions of unique 28 labels\n\n\nprotein_ids=protein_df['Id'].values.reshape(-1,1) \n#get ids array and reshape to 2D to feed into split function \n\nlabels = [list(map(int, target.split())) for target in protein_df['Target']] \n#split target strings,chnage to int and make a list \n\nmlb=MultiLabelBinarizer() #mlb object to access the methods of M..L..B..() class\nbinary_labels=mlb.fit_transform(labels)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:35.342098Z","iopub.execute_input":"2024-09-30T14:58:35.342667Z","iopub.status.idle":"2024-09-30T14:58:35.578826Z","shell.execute_reply.started":"2024-09-30T14:58:35.342630Z","shell.execute_reply":"2024-09-30T14:58:35.578060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_counts=binary_labels.sum(axis=0) \n# sum across each column to get counts of all unique protein locations in the dataset\n\nprint(f'labels_counts: \\n{labels_counts}')\n\nprint(f'percentages of proteins before split: \\n {(labels_counts/labels_counts.sum()*100).round(1)}') \n# percentages of all 28 protein locations in the dataset before split","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:37.821744Z","iopub.execute_input":"2024-09-30T14:58:37.822445Z","iopub.status.idle":"2024-09-30T14:58:37.829300Z","shell.execute_reply.started":"2024-09-30T14:58:37.822415Z","shell.execute_reply":"2024-09-30T14:58:37.828421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_sorted_indxs=np.argsort(labels_counts)[::-1]\nlabels_counts_sorted=labels_counts[labels_sorted_indxs]\n# print(labels_sorted_indxs)\n# print(labels_counts_sorted)\n# print(labels_counts)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:40.062205Z","iopub.execute_input":"2024-09-30T14:58:40.062558Z","iopub.status.idle":"2024-09-30T14:58:40.067700Z","shell.execute_reply.started":"2024-09-30T14:58:40.062532Z","shell.execute_reply":"2024-09-30T14:58:40.066524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def text_bars(bars):\n    for bar in bars:\n        plt.text(\n        bar.get_width(), \n        bar.get_y() + bar.get_height() / 2, \n        f'{int(bar.get_width())}', \n        va='center', \n        ha='left', \n        fontsize=14,\n        color='teal',\n        weight='bold'\n    )","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:41.805342Z","iopub.execute_input":"2024-09-30T14:58:41.805996Z","iopub.status.idle":"2024-09-30T14:58:41.811136Z","shell.execute_reply.started":"2024-09-30T14:58:41.805963Z","shell.execute_reply":"2024-09-30T14:58:41.810179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def outer_ax_invis():\n    ax = plt.gca()\n    ax.spines['top'].set_visible(False)\n    ax.spines['right'].set_visible(False)\n    ax.spines['left'].set_visible(False)\n    ax.spines['bottom'].set_visible(False)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:43.570697Z","iopub.execute_input":"2024-09-30T14:58:43.571054Z","iopub.status.idle":"2024-09-30T14:58:43.576285Z","shell.execute_reply.started":"2024-09-30T14:58:43.571026Z","shell.execute_reply":"2024-09-30T14:58:43.575285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reference notebook for few exploration ideas -https://www.kaggle.com/code/allunia/protein-atlas-exploration-and-baseline\nThat's a cool notebook!","metadata":{}},{"cell_type":"code","source":"# to visualise the count of different unique proteins in the dataset\n\nproteins_names_sorted=[protein_locs[i] for  i in labels_sorted_indxs]\n\n\ndef protein_bars(labels_counts_sorted):\n    plt.figure(figsize=(20,18))\n    bars=plt.barh( proteins_names_sorted,labels_counts_sorted, color='orange')\n    text_bars(bars)\n    plt.title('Counts of Protein Locations',fontsize='14',weight='bold',color='teal')\n    plt.xticks(color='crimson',weight='bold')\n    plt.yticks(color='crimson',fontsize='10',weight='bold')\n    plt.gca().invert_yaxis()\n    outer_ax_invis()\n    plt.tight_layout()\n    plt.show()\n    \nprotein_bars(labels_counts_sorted)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:46.086048Z","iopub.execute_input":"2024-09-30T14:58:46.086437Z","iopub.status.idle":"2024-09-30T14:58:46.928639Z","shell.execute_reply.started":"2024-09-30T14:58:46.086411Z","shell.execute_reply":"2024-09-30T14:58:46.927726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Top 5 occuring protein locations are Nucleoplasm,Cytosol,Plasma Membrane,Nucleoli,Mitochondria.**","metadata":{}},{"cell_type":"code","source":"#to check percentages of different combinations of labels\nlabels_combination=binary_labels.sum(axis=1)\nval,counts=np.unique(labels_combination,return_counts=True)\nlabels_comb_perc=np.round(counts*100/labels_combination.shape[0],1)\n# print(labels_comb_perc)\nplt.bar(val,labels_comb_perc,color='deeppink')\nouter_ax_invis()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:52.759027Z","iopub.execute_input":"2024-09-30T14:58:52.759630Z","iopub.status.idle":"2024-09-30T14:58:53.048246Z","shell.execute_reply.started":"2024-09-30T14:58:52.759600Z","shell.execute_reply":"2024-09-30T14:58:53.047329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport seaborn as sns\n\nnum_classes = 28  \n\nproteins_names=[protein_locs[i] for i in range(28)]\n\n# co_occurrence_matrix = np.dot(binary_labels[labels_combination>1].T, binary_labels[labels_combination>1])\nmorethan1_proteins = binary_labels[labels_combination>1]\nmorethan1_proteins=morethan1_proteins.astype(float)\ncorrelation_matrix = np.corrcoef(binary_labels.T)\n\n\nplt.figure(figsize=(20, 18))\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1, xticklabels=proteins_names, yticklabels=proteins_names)\nplt.title('Protein Co-Occurrence Heatmap')\nplt.xticks(rotation=90)\nplt.yticks(rotation=0)\nplt.tight_layout()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:55.407413Z","iopub.execute_input":"2024-09-30T14:58:55.407772Z","iopub.status.idle":"2024-09-30T14:58:58.047789Z","shell.execute_reply.started":"2024-09-30T14:58:55.407745Z","shell.execute_reply":"2024-09-30T14:58:58.046801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 1)Endosomes and Lysomes seem to occur together.\n* 2)Also Lysosomes and endosomes seem to occur with endoplasmic reticulum too with strength in the order(Mild correlation)\n* 3)Cytokinetic bridge and mitotic spindle with microtubules.\n* 4) Cytokinetic brdge with microtubule ends.\n* 5)Mitotic spindle with cytokinetic bridge.\n* 6)Mitotic Spindle with Microtubule organising center\n* There are few other mild correlations as well.","metadata":{}},{"cell_type":"code","source":"\n# for 1 sample\ndef binary_to_num(binary_labels):\n    bool_labels=(binary_labels==1)\n#     print(bool_labels)\n\n    protein_keys_arr=np.array(list(protein_locs.keys()))\n\n    present_labels_num=protein_keys_arr[bool_labels]\n\n    present_labels= [protein_locs[label] for label in present_labels_num]\n    \n    \n    return present_labels\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:58:58.449594Z","iopub.execute_input":"2024-09-30T14:58:58.450174Z","iopub.status.idle":"2024-09-30T14:58:58.455351Z","shell.execute_reply.started":"2024-09-30T14:58:58.450143Z","shell.execute_reply":"2024-09-30T14:58:58.454427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \n    def plot_1cross4_randids(sample_ids,sample_labels,num_images):\n        rng_indxs=np.random.randint(0,len(sample_ids),num_images,dtype=int)\n        channels = [\"blue\", \"green\", \"red\", \"yellow\"]\n\n            \n        plt.figure(figsize=(20,18))\n        for i,rng_indx in enumerate(rng_indxs):\n            for j,channel in enumerate(channels):\n                image_path = os.path.join(train_folder, f\"{sample_ids[rng_indx,0]}_{channel}.png\")\n                img_arr = imread(image_path)\n                plt.subplot(num_images,4,i*4+j+1)\n                plt.imshow(img_arr, cmap='copper')\n                plt.title(f\"{binary_to_num(sample_labels[rng_indx,:])}, {img_arr.max(),img_arr.min()}\")\n                plt.axis('off')\n        plt.tight_layout()\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:01.422418Z","iopub.execute_input":"2024-09-30T14:59:01.423221Z","iopub.status.idle":"2024-09-30T14:59:01.430393Z","shell.execute_reply.started":"2024-09-30T14:59:01.423188Z","shell.execute_reply":"2024-09-30T14:59:01.429491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef unique_labels_explore(label_name,protein_ids,binary_labels,pred_labels,plot_merged=False):\n    label_index=next(index for index,value in protein_locs.items() if value==label_name)\n    current_protein_ids=protein_ids[binary_labels[:,label_index]==1]\n    current_binary_labels=binary_labels[binary_labels[:,label_index]==1]\n#     print(current_binary_labels[:5])\n    \n    if plot_merged==False:\n        num_images=5\n        plot_1cross4_randids(current_protein_ids,current_binary_labels,num_images=5)\n    else:\n        plot_1cross5_randids(current_protein_ids,current_binary_labels,4,pred_labels=None)\n    \n\nunique_labels_explore('Plasma membrane',protein_ids,binary_labels,None,plot_merged=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:03.965792Z","iopub.execute_input":"2024-09-30T14:59:03.966427Z","iopub.status.idle":"2024-09-30T14:59:06.911041Z","shell.execute_reply.started":"2024-09-30T14:59:03.966395Z","shell.execute_reply":"2024-09-30T14:59:06.909694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.set_state(current_rand_state)\n# print(current_rand_state)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:12.871748Z","iopub.execute_input":"2024-09-30T14:59:12.872117Z","iopub.status.idle":"2024-09-30T14:59:12.879469Z","shell.execute_reply.started":"2024-09-30T14:59:12.872088Z","shell.execute_reply":"2024-09-30T14:59:12.878452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\n\n\nnp.random.seed(32)\n\n\ntrain_ids,train_labels, val_ids, val_labels= iterative_train_test_split(protein_ids, binary_labels, test_size=0.20)\n#split the dataset into train and validation sets\n\nnp.random.set_state(current_rand_state)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:15.300530Z","iopub.execute_input":"2024-09-30T14:59:15.301166Z","iopub.status.idle":"2024-09-30T14:59:18.230656Z","shell.execute_reply.started":"2024-09-30T14:59:15.301126Z","shell.execute_reply":"2024-09-30T14:59:18.229663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'train_ids:{len(train_ids)}\\n')\nprint(f'val_ids:{len(val_ids)}\\n')\n\nprint(f'total_ids:{len(train_ids)+len(val_ids)}\\n')\n\n\n#to check if percentages of all unique labels remain same across all sets\nlabels_counts_train=train_labels.sum(axis=0)\nlabels_counts_val=val_labels.sum(axis=0)\nprint(f'labels_counts_train: \\n {labels_counts_train}')\nprint(f'% of train proteins after split:\\n{(labels_counts_train/labels_counts_train.sum()*100).round(1)}')\nprint(f'labels_counts_val: \\n {labels_counts_val}')\nprint(f'% of val proteins after split:\\n{(labels_counts_val/labels_counts_val.sum()*100).round(1)}')","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:18.232246Z","iopub.execute_input":"2024-09-30T14:59:18.232542Z","iopub.status.idle":"2024-09-30T14:59:18.240862Z","shell.execute_reply.started":"2024-09-30T14:59:18.232518Z","shell.execute_reply":"2024-09-30T14:59:18.239987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Results conclude that the method used for splitting correctly splits the data while preserving the proportions of unique classes across all sets.","metadata":{}},{"cell_type":"code","source":"\n#function to randomly display few samples for each channel\ndef display_random_images(sample_ids,train_folder,num_images):\n        \"\"\"\n        Visualize few image arrays for each channel and compare\n\n        parameters\n        ----------\n        sample_ids: 2-D array , size: samples,1\n        train_folder: string\n          path to train_folder contating all images\n        num_images: int\n          no of sample ids to be visualised basis selection\n  \n        \"\"\"\n        \n        plot_1cross4_randids(sample_ids,binary_labels,num_images)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:20.855292Z","iopub.execute_input":"2024-09-30T14:59:20.855903Z","iopub.status.idle":"2024-09-30T14:59:20.861051Z","shell.execute_reply.started":"2024-09-30T14:59:20.855874Z","shell.execute_reply":"2024-09-30T14:59:20.860153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_random_images(protein_ids,train_folder,5)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:23.443548Z","iopub.execute_input":"2024-09-30T14:59:23.444156Z","iopub.status.idle":"2024-09-30T14:59:26.134253Z","shell.execute_reply.started":"2024-09-30T14:59:23.444126Z","shell.execute_reply":"2024-09-30T14:59:26.133275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Inferences from above visulations and analysis-\n1. Green channel must be kept as it contains the protein of interest to be classified.\n2. Red and Yellow channel representing microtubules and endoplasmic reticulum are almost same in structure\n3. Blue channel representing nucleus structure is unique from red and yellow channels.\n\nAs we are using blue,red and yellow channels to locate the protein for that sample . two channels are almost same so we can keep one channel out of red / yellow to reduce compute time as the same structure won't be giving much extra information for determining the location of protein.","metadata":{}},{"cell_type":"code","source":"np.random.set_state(current_rand_state)\n# print(current_rand_state)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:32.293276Z","iopub.execute_input":"2024-09-30T14:59:32.293634Z","iopub.status.idle":"2024-09-30T14:59:32.298067Z","shell.execute_reply.started":"2024-09-30T14:59:32.293606Z","shell.execute_reply":"2024-09-30T14:59:32.297192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def merge_channels(paths):\n    blue=plt.imread(paths[0])\n    blue=cv2.resize(blue,(256,256))\n    \n    green=plt.imread(paths[1])\n    green=cv2.resize(green,(256,256))\n\n    red=plt.imread(paths[2])\n    red=cv2.resize(red,(256,256))\n    \n    yellow=plt.imread(paths[3])\n    yellow=cv2.resize(yellow,(256,256))\n    \n    merged_img=cv2.merge((red,green,blue))\n    \n    return merged_img","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:35.559239Z","iopub.execute_input":"2024-09-30T14:59:35.560248Z","iopub.status.idle":"2024-09-30T14:59:35.567909Z","shell.execute_reply.started":"2024-09-30T14:59:35.560207Z","shell.execute_reply":"2024-09-30T14:59:35.566925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:39.204529Z","iopub.execute_input":"2024-09-30T14:59:39.205495Z","iopub.status.idle":"2024-09-30T14:59:51.012246Z","shell.execute_reply.started":"2024-09-30T14:59:39.205455Z","shell.execute_reply":"2024-09-30T14:59:51.011420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It looks like some of the proteins(green channels) are very mildly visible which is independent of their class. So model might have hard time to figure out such cases. So we can give some factor of brightness plus contrast to overcome that.","metadata":{}},{"cell_type":"code","source":"# to compare and chosse best contrast , brightnessand saturation effects.\ndef plot_1cross5_randids(sample_ids,sample_labels,num_images,pred_labels=None):\n        rng_indxs=np.random.randint(0,len(sample_ids),num_images,dtype=int)\n        channels = [\"blue\", \"green\", \"red\", \"yellow\"]\n        aug_factors=[1.0,1.3,1.5]\n\n        def set_aug(img,j):\n            if j==0:\n                img=tf.image.adjust_contrast(img,1.5)\n            elif j==1:\n                img=tf.image.adjust_brightness(img,0.05)\n                img=tf.image.adjust_contrast(img,1.5)          \n            elif j==3:\n                img=tf.image.adjust_saturation(img,1.5)\n            return img    \n        \n            \n        plt.figure(figsize=(15,20))\n        for i,samp_id in enumerate(rng_indxs):\n            paths_per_id=[]\n            for channel in ['blue','green','red','yellow']:\n                img_path=os.path.join(train_folder,f'{sample_ids[samp_id,0]}_{channel}.png')\n                paths_per_id.append(img_path)\n            img=merge_channels(paths_per_id)\n            for j in range(3):\n                aug_img=set_aug(img,j)\n                plt.subplot(num_images,3,i*3+j+1)\n                plt.imshow(aug_img)\n                plt.axis('off')\n                plt.title(f\" {binary_to_num(sample_labels[samp_id,:])}\\n predicted:{binary_to_num(pred_labels[samp_id,:] if pred_labels is not None else '_')} \")\n        plt.show()\n        plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:54.687635Z","iopub.execute_input":"2024-09-30T14:59:54.688300Z","iopub.status.idle":"2024-09-30T14:59:54.698932Z","shell.execute_reply.started":"2024-09-30T14:59:54.688269Z","shell.execute_reply":"2024-09-30T14:59:54.698034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_labels_explore('Mitochondria',protein_ids,binary_labels,pred_labels=None,plot_merged=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T14:59:59.327097Z","iopub.execute_input":"2024-09-30T14:59:59.327720Z","iopub.status.idle":"2024-09-30T15:00:02.061994Z","shell.execute_reply.started":"2024-09-30T14:59:59.327688Z","shell.execute_reply":"2024-09-30T15:00:02.061035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.set_state(current_rand_state)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:00:27.888033Z","iopub.execute_input":"2024-09-30T15:00:27.888681Z","iopub.status.idle":"2024-09-30T15:00:27.892751Z","shell.execute_reply.started":"2024-09-30T15:00:27.888651Z","shell.execute_reply":"2024-09-30T15:00:27.891819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ","metadata":{}},{"cell_type":"code","source":"#to check images after resize and combining channels\nimport cv2\n\nexplore_imgs_paths=[]\nfor samp_id in train_ids[:5,0]:\n    for channel in ['blue','green','red','yellow']:\n        img_path=os.path.join(train_folder,f'{samp_id}_{channel}.png')\n        explore_imgs_paths.append(img_path)\n\n\nfor i in range(5):\n    blue=plt.imread(explore_imgs_paths[0+4*i])\n    blue=cv2.resize(blue,(256,256))\n    \n    green=plt.imread(explore_imgs_paths[1+4*i])\n    green=cv2.resize(green,(256,256))\n\n    red=plt.imread(explore_imgs_paths[2+4*i])\n    red=cv2.resize(red,(256,256))\n    \n    yellow=plt.imread(explore_imgs_paths[3+4*i])\n    yellow=cv2.resize(yellow,(256,256))\n    \n    merged_img=cv2.merge((red,green,blue))\n\n    plt.figure()\n    plt.title(merged_img.shape)\n    plt.imshow(merged_img)\n    plt.axis('off')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:00:31.403569Z","iopub.execute_input":"2024-09-30T15:00:31.403864Z","iopub.status.idle":"2024-09-30T15:00:32.668041Z","shell.execute_reply.started":"2024-09-30T15:00:31.403839Z","shell.execute_reply":"2024-09-30T15:00:32.667063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#class to encase function for dataset pipeline \nclass ImageDatasetPipeline():\n    #constructor method to intialise and define instance(self --> refers to current object ) variables\n    def __init__(self, image_ids, labels, image_folder, batch_size=32, image_size=(256,256),augment=False):\n        self.image_ids = image_ids[:,0]  \n        self.labels = labels\n        self.image_folder = image_folder\n        self.batch_size = batch_size\n        self.image_size = image_size\n        self.augment=augment\n   \n\n    def resize(self,img):\n        img=cv2.resize(img,self.image_size)\n        return img\n    \n    def minmax_norm(self,img):\n        img=(img-img.min())/(img.max()-img.min())\n        return img\n    \n        \n\n    def random_geometric_transform(self,image):\n        # Apply random rotation\n#         image = tf.image.rot90(image, k=tf.cast(1,tf.dtypes.int32))\n#         image = tf.image.rot90(image, k=tf.random.uniform(shape=[], minval=0, maxval=4, dtype=tf.int32))\n        # Apply random flip\n        image = tf.image.random_flip_left_right(image)\n        image = tf.image.random_flip_up_down(image)\n        return image\n\n\n    def apply_augmentation(self,image):\n        image=tf.image.adjust_brightness(image,0.05)\n        image=tf.image.random_contrast(image,1.3,1.7) \n        # Apply geometric transformations\n        image = self.random_geometric_transform(image)\n        return image\n\n        \n    def data_generation(self, image_id, label):\n            \"\"\"\n            takes single image id and label as input and outputs the scaled,resized and merged version\n\n            parameters\n            ----------\n            self: current instance\n            image_id: bytes/byte string,as it was a string tensor so got converted to bytes by numpy_func\n              path to one image id\n            label: array, size- 1,28\n              labels array for one target \n              \n            \"\"\"\n            \n            channel_paths=[]\n            for channel in ['blue','green','red','yellow']:\n                channel_paths.append(os.path.join(self.image_folder, f\"{image_id.decode('utf-8')}_{channel}.png\"))   \n            blue_imgarr=plt.imread(channel_paths[0])\n            blue_imgarr=self.minmax_norm(self.resize(blue_imgarr))\n            \n            green_imgarr=plt.imread(channel_paths[1])\n            green_imgarr=self.minmax_norm(self.resize(green_imgarr))\n            \n            red_imgarr=plt.imread(channel_paths[2])\n            red_imgarr=self.minmax_norm(self.resize(red_imgarr))\n            \n            merged_img=cv2.merge((red_imgarr, green_imgarr,blue_imgarr ))\n            \n            merged_img_fin=tf.cast(self.apply_augmentation(merged_img), tf.float64) if self.augment else tf.cast(merged_img,tf.float64)\n            return merged_img_fin,label\n        \n        \n    def create_tf_dataset(self):\n        \"\"\"\n        method to make dataset feasible for parallel processing and \n        for scalable integration with tf models\n        \n        \"\"\"\n\n        dataset = tf.data.Dataset.from_tensor_slices((self.image_ids, self.labels))\n        #slices the dataset along first dimension\n        \n        \n#         dataset = dataset.shuffle(buffer_size=1000) \n        #for a batch select elems randomly from first 1000 elems with replacement\n        \n#         print(f'before shape{dataset.element_spec}')\n        expected_image_shape = (256, 256, 3)  \n        expected_label_shape = (28,)  \n        \n        #to map each element of datset tensors to numpy and convert outputs of data_generation back to tensors\n\n        def map_data(image_id,label):\n            image, label= tf.numpy_function(\n                self.data_generation,\n                [image_id, label],\n                [tf.float64, tf.int64],\n            )\n            image.set_shape(expected_image_shape)\n            label.set_shape(expected_label_shape)\n            return image, label\n        \n        dataset = dataset.map(\n            map_data,\n            num_parallel_calls=tf.data.AUTOTUNE\n        )\n\n        # Batch the dataset and prefetch for performance\n        dataset = dataset.batch(self.batch_size).prefetch(tf.data.AUTOTUNE)\n        \n        return dataset\n    \n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:11.688399Z","iopub.execute_input":"2024-09-30T15:01:11.688763Z","iopub.status.idle":"2024-09-30T15:01:11.707324Z","shell.execute_reply.started":"2024-09-30T15:01:11.688734Z","shell.execute_reply":"2024-09-30T15:01:11.706317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_folder_path = '/kaggle/input/human-protein-atlas-image-classification/train'\n\n# Create data pipeline instances\ntrain_generator = ImageDatasetPipeline(train_ids, train_labels, imgs_folder_path, batch_size=32, image_size=(256,256),augment=True)\nval_generator = ImageDatasetPipeline(val_ids, val_labels, imgs_folder_path, batch_size=32, image_size=(256,256),augment=True)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:17.284860Z","iopub.execute_input":"2024-09-30T15:01:17.285745Z","iopub.status.idle":"2024-09-30T15:01:17.290633Z","shell.execute_reply.started":"2024-09-30T15:01:17.285712Z","shell.execute_reply":"2024-09-30T15:01:17.289674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_generator.create_tf_dataset() #access the method using object\nval_dataset=val_generator.create_tf_dataset()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:19.682370Z","iopub.execute_input":"2024-09-30T15:01:19.682732Z","iopub.status.idle":"2024-09-30T15:01:19.793550Z","shell.execute_reply.started":"2024-09-30T15:01:19.682705Z","shell.execute_reply":"2024-09-30T15:01:19.792570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_dataset.element_spec)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:21.789497Z","iopub.execute_input":"2024-09-30T15:01:21.790574Z","iopub.status.idle":"2024-09-30T15:01:21.795606Z","shell.execute_reply.started":"2024-09-30T15:01:21.790526Z","shell.execute_reply":"2024-09-30T15:01:21.794443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#to display few images from a batch\nfor X_batch, y_batch in val_dataset.take(1):\n    X_batch = X_batch.numpy() \n    y_batch = y_batch.numpy()\n\nnum_labels=mlb.inverse_transform(y_batch)\n\n# to check four samples\nfor i in range(5):\n    plt.figure(figsize=(4,4))\n    plt.title(f'{tuple(protein_locs[elem] for elem in num_labels[i])}', fontsize=9)\n    plt.imshow(X_batch[i])\n    plt.axis('off')\n    \n    plt.tight_layout()\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:24.163298Z","iopub.execute_input":"2024-09-30T15:01:24.164182Z","iopub.status.idle":"2024-09-30T15:01:25.960496Z","shell.execute_reply.started":"2024-09-30T15:01:24.164149Z","shell.execute_reply":"2024-09-30T15:01:25.959499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CNN -- without any state of art network and pretrained weights**\nFew of the model improvement suggestions added by help from chatgpt.","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Input,Conv2D, BatchNormalization, Activation, Dropout, MaxPooling2D, Flatten, Dense, Add\nfrom tensorflow.keras.metrics import Precision, Recall ,F1Score,AUC\nfrom tensorflow.keras.losses import BinaryCrossentropy\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.models import Model\n\n\ndef create_resnet_block(x, filters, kernel_size=3, strides=1):\n    y = Conv2D(filters, kernel_size=kernel_size, strides=strides, padding='same')(x) \n    y = BatchNormalization()(y)\n    y = Activation('relu')(y)\n    #reduce the output size by half if strides=2\n    \n    y = Conv2D(filters, kernel_size=kernel_size, strides=1, padding='same')(y)\n    y = BatchNormalization()(y)\n    #to add more depth \n    \n    #to propagate and preserve input x via layers as network gets deeper!\n    if strides > 1:\n        x = Conv2D(filters, kernel_size=1, strides=strides, padding='same')(x)\n    \n    #add propagated input with reduced and adjusted output size\n    out = Add()([x, y])\n    out = Activation('relu')(out)\n    \n    return out\n\ninput_shape = (256, 256, 3)\ninputs = Input(shape=input_shape)\n\nx = Conv2D(32, (3, 3), padding='same')(inputs)\nx = BatchNormalization()(x)\nx = Activation('relu')(x)\nx = MaxPooling2D(pool_size=(2, 2))(x)\n\n# Add multiple ResNet blocks\nx = create_resnet_block(x, 64, strides=2)\nx = create_resnet_block(x, 64)\n\nx = create_resnet_block(x, 128, strides=2)\nx = create_resnet_block(x, 128)\n\nx = create_resnet_block(x, 256, strides=2)\nx = create_resnet_block(x, 256)\n\nx = Flatten()(x)\nx = Dense(512)(x)\nx = BatchNormalization()(x)\nx = Activation('relu')(x)\nx = Dropout(0.5)(x)\n\noutputs = Dense(28, activation='sigmoid')(x)\n\nprotein_cnnres1 = Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:31.515270Z","iopub.execute_input":"2024-09-30T15:01:31.515619Z","iopub.status.idle":"2024-09-30T15:01:31.848067Z","shell.execute_reply.started":"2024-09-30T15:01:31.515593Z","shell.execute_reply":"2024-09-30T15:01:31.847320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"protein_cnnres1.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:37.091842Z","iopub.execute_input":"2024-09-30T15:01:37.092508Z","iopub.status.idle":"2024-09-30T15:01:37.185099Z","shell.execute_reply.started":"2024-09-30T15:01:37.092478Z","shell.execute_reply":"2024-09-30T15:01:37.184221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"protein_cnnres1.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss=BinaryCrossentropy, metrics=[ tf.keras.metrics.Precision(), tf.keras.metrics.Recall(),AUC])\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:45.914883Z","iopub.execute_input":"2024-09-30T15:01:45.915567Z","iopub.status.idle":"2024-09-30T15:01:45.936365Z","shell.execute_reply.started":"2024-09-30T15:01:45.915534Z","shell.execute_reply":"2024-09-30T15:01:45.935560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path=\"/kaggle/working/protein_cnn.weights.h5\"","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:52.467142Z","iopub.execute_input":"2024-09-30T15:01:52.467937Z","iopub.status.idle":"2024-09-30T15:01:52.471656Z","shell.execute_reply.started":"2024-09-30T15:01:52.467907Z","shell.execute_reply":"2024-09-30T15:01:52.470745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train the model\ndef train_protein_cnnres1(train_dataset, val_dataset, weights_path,epochs=100,use_saved_weights=True):\n        \"\"\"\n        Trains the model. If `use_saved_model` is True, it loads the saved weights and continues training.\n        \"\"\"\n        if use_saved_weights and os.path.exists(weights_path):\n            print(f\"Loading weights from {weights_path}\")\n            protein_cnnres1.load_weights(weights_path)\n        \n        callbacks = [\n            tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=7, restore_best_weights=True),\n            tf.keras.callbacks.ModelCheckpoint(weights_path,save_weights_only=True,save_best_only=True)\n        ]\n        \n        history = protein_cnnres1.fit(\n            train_dataset,\n            validation_data=val_dataset,\n            epochs=epochs,\n            callbacks=callbacks,\n        )\n        return history","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:01:59.640740Z","iopub.execute_input":"2024-09-30T15:01:59.641660Z","iopub.status.idle":"2024-09-30T15:01:59.649716Z","shell.execute_reply.started":"2024-09-30T15:01:59.641620Z","shell.execute_reply":"2024-09-30T15:01:59.648725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_protein_cnnres1(train_dataset, val_dataset,  weights_path,epochs=100,use_saved_weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T15:02:02.374657Z","iopub.execute_input":"2024-09-30T15:02:02.375636Z","iopub.status.idle":"2024-09-30T16:22:04.140872Z","shell.execute_reply.started":"2024-09-30T15:02:02.375599Z","shell.execute_reply":"2024-09-30T16:22:04.139966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_probs_val=protein_cnnres1.predict(val_dataset)\npred_probs_train=protein_cnnres1.predict(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:22:43.345734Z","iopub.execute_input":"2024-09-30T16:22:43.346501Z","iopub.status.idle":"2024-09-30T16:26:37.855162Z","shell.execute_reply.started":"2024-09-30T16:22:43.346468Z","shell.execute_reply":"2024-09-30T16:26:37.854163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_val=(pred_probs_val>0.5).astype(int)\npreds_train=(pred_probs_train>0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:33:00.479361Z","iopub.execute_input":"2024-09-30T16:33:00.480261Z","iopub.status.idle":"2024-09-30T16:33:00.487584Z","shell.execute_reply.started":"2024-09-30T16:33:00.480226Z","shell.execute_reply":"2024-09-30T16:33:00.486688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_pred_labels=np.concatenate((preds_train,preds_val),axis=0)\nprint(all_pred_labels[:5])\nprint(preds_train[:5])","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:33:03.810321Z","iopub.execute_input":"2024-09-30T16:33:03.810710Z","iopub.status.idle":"2024-09-30T16:33:03.820715Z","shell.execute_reply.started":"2024-09-30T16:33:03.810683Z","shell.execute_reply":"2024-09-30T16:33:03.819598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y_trues=[]\nfor _,y_true in train_dataset:\n    train_y_trues.append(y_true.numpy())\n\ntrain_y_trues_np=np.concatenate(train_y_trues,axis=0)\n# print(train_y_trues)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:33:06.760156Z","iopub.execute_input":"2024-09-30T16:33:06.760521Z","iopub.status.idle":"2024-09-30T16:35:59.954344Z","shell.execute_reply.started":"2024-09-30T16:33:06.760494Z","shell.execute_reply":"2024-09-30T16:35:59.953458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_y_trues=[]\nfor _,y_true in val_dataset:\n    val_y_trues.append(y_true.numpy())\n\nval_y_trues_np=np.concatenate(val_y_trues,axis=0)\n# print(train_y_trues)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T05:02:06.554041Z","iopub.execute_input":"2024-09-13T05:02:06.555017Z","iopub.status.idle":"2024-09-13T05:02:49.634359Z","shell.execute_reply.started":"2024-09-13T05:02:06.554956Z","shell.execute_reply":"2024-09-13T05:02:49.633497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_y_trues_np.shape)\nprint(preds_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T05:02:58.002791Z","iopub.execute_input":"2024-09-13T05:02:58.003693Z","iopub.status.idle":"2024-09-13T05:02:58.010521Z","shell.execute_reply.started":"2024-09-13T05:02:58.003656Z","shell.execute_reply":"2024-09-13T05:02:58.009470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_y_trues_np[30:36])","metadata":{"execution":{"iopub.status.busy":"2024-09-13T05:03:00.786970Z","iopub.execute_input":"2024-09-13T05:03:00.787844Z","iopub.status.idle":"2024-09-13T05:03:00.793316Z","shell.execute_reply.started":"2024-09-13T05:03:00.787809Z","shell.execute_reply":"2024-09-13T05:03:00.792234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\nval_conf_matrices=[]\nval_proteins_precision=[]\nval_proteins_recall=[]\n# Plot the confusion matrices\n\nfor i in range(len(proteins_names)):\n    plt.figure(figsize=(2,2))\n    conf_matrix=confusion_matrix(val_y_trues_np[:,i],preds_val[:,i])\n    val_conf_matrices.append(conf_matrix)\n    \n    precision=round(conf_matrix[1][1]/(conf_matrix[1][1]+conf_matrix[0][1]),2)\n    val_proteins_precision.append(precision)\n    \n    recall=round(conf_matrix[1][1]/(conf_matrix[1][1]+conf_matrix[1][0]),2)\n    val_proteins_recall.append(recall)\n    \n    sns.heatmap(conf_matrix, annot=True, fmt=\"d\", cmap='Reds')\n    plt.title(proteins_names[i],fontsize=10)\n    plt.ylabel('True Labels')\n    plt.show()\n\n \n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T05:03:04.027634Z","iopub.execute_input":"2024-09-13T05:03:04.028020Z","iopub.status.idle":"2024-09-13T05:03:10.176553Z","shell.execute_reply.started":"2024-09-13T05:03:04.027969Z","shell.execute_reply":"2024-09-13T05:03:10.175504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\ntrain_conf_matrices=[]\ntrain_proteins_precision=[]\ntrain_proteins_recall=[]\n# Plot the confusion matrices\n\nfor i in range(len(proteins_names)):\n    plt.figure(figsize=(2,2))\n    conf_matrix=confusion_matrix(train_y_trues_np[:,i],preds_train[:,i])\n    train_conf_matrices.append(conf_matrix)\n    \n    precision=round(conf_matrix[1][1]/(conf_matrix[1][1]+conf_matrix[0][1]),2)\n    train_proteins_precision.append(precision)\n    \n    recall=round(conf_matrix[1][1]/(conf_matrix[1][1]+conf_matrix[1][0]),2)\n    train_proteins_recall.append(recall)\n    \n    sns.heatmap(conf_matrix, annot=True, fmt=\"d\", cmap='Reds')\n    plt.title(proteins_names[i],fontsize=10)\n    plt.ylabel('True Labels')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T05:07:23.235441Z","iopub.execute_input":"2024-09-13T05:07:23.236092Z","iopub.status.idle":"2024-09-13T05:07:28.081072Z","shell.execute_reply.started":"2024-09-13T05:07:23.236057Z","shell.execute_reply":"2024-09-13T05:07:28.080053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'train_proteins_precision:{train_proteins_precision}')\nprint(f'train_proteins_recall:{train_proteins_recall}')\nprint(f'val_proteins_precision:{val_proteins_precision}')\nprint(f'val_proteins_recall:{val_proteins_recall}')","metadata":{"execution":{"iopub.status.busy":"2024-09-13T05:11:16.052580Z","iopub.execute_input":"2024-09-13T05:11:16.053312Z","iopub.status.idle":"2024-09-13T05:11:16.058867Z","shell.execute_reply.started":"2024-09-13T05:11:16.053277Z","shell.execute_reply":"2024-09-13T05:11:16.057913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=train_protein_cnnres1(train_dataset, val_dataset,  weights_path,epochs=100,use_saved_weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T08:05:02.714956Z","iopub.execute_input":"2024-09-13T08:05:02.715369Z","iopub.status.idle":"2024-09-13T08:40:51.384804Z","shell.execute_reply.started":"2024-09-13T08:05:02.715339Z","shell.execute_reply":"2024-09-13T08:40:51.383744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auc = history.history['auc']\nval_auc =history.history['val_auc']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']","metadata":{"execution":{"iopub.status.busy":"2024-09-13T08:44:59.001798Z","iopub.execute_input":"2024-09-13T08:44:59.002736Z","iopub.status.idle":"2024-09-13T08:44:59.007304Z","shell.execute_reply.started":"2024-09-13T08:44:59.002699Z","shell.execute_reply":"2024-09-13T08:44:59.006394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.subplot(2, 1, 1)\nplt.plot(auc, label='Training AUC')\nplt.plot(val_auc, label='Validation AUC')\nplt.legend(loc='lower right')\nplt.ylabel('AUC')\nplt.ylim([min(plt.ylim()),1])\nplt.title('Training and Validation AUC')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\nplt.ylim([0,1.0])\nplt.title('Training and Validation Loss')\nplt.xlabel('epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-13T08:45:02.806320Z","iopub.execute_input":"2024-09-13T08:45:02.807161Z","iopub.status.idle":"2024-09-13T08:45:03.235952Z","shell.execute_reply.started":"2024-09-13T08:45:02.807127Z","shell.execute_reply":"2024-09-13T08:45:03.235039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_val_dataset=val_dataset.take(6)\nfor X_samp,y_trues in sub_val_dataset:\n    X_samp=X_samp\n    y_trues=y_trues.numpy()\nprint(X_samp.shape)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:40:41.383340Z","iopub.execute_input":"2024-09-30T16:40:41.384088Z","iopub.status.idle":"2024-09-30T16:40:42.814955Z","shell.execute_reply.started":"2024-09-30T16:40:41.384058Z","shell.execute_reply":"2024-09-30T16:40:42.814064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=protein_cnnres1.predict(X_samp)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:40:52.788420Z","iopub.execute_input":"2024-09-30T16:40:52.789299Z","iopub.status.idle":"2024-09-30T16:40:54.026473Z","shell.execute_reply.started":"2024-09-30T16:40:52.789262Z","shell.execute_reply":"2024-09-30T16:40:54.025710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions[1])","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:40:56.203090Z","iopub.execute_input":"2024-09-30T16:40:56.203455Z","iopub.status.idle":"2024-09-30T16:40:56.209103Z","shell.execute_reply.started":"2024-09-30T16:40:56.203427Z","shell.execute_reply":"2024-09-30T16:40:56.208189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_binary=(predictions > 0.5).astype(int)\n# print(preds_binary)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:40:59.172493Z","iopub.execute_input":"2024-09-30T16:40:59.173107Z","iopub.status.idle":"2024-09-30T16:40:59.177699Z","shell.execute_reply.started":"2024-09-30T16:40:59.173073Z","shell.execute_reply":"2024-09-30T16:40:59.176737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(preds_binary[1])\nprint(y_trues[1])","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:41:01.594949Z","iopub.execute_input":"2024-09-30T16:41:01.595410Z","iopub.status.idle":"2024-09-30T16:41:01.600377Z","shell.execute_reply.started":"2024-09-30T16:41:01.595382Z","shell.execute_reply":"2024-09-30T16:41:01.599509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_labels=mlb.inverse_transform(preds_binary)\nnum_trues=mlb.inverse_transform(y_trues)\n\n# to check few samples\nfor i in range(32):\n    plt.figure(figsize=(4,4))\n    plt.title(f'{tuple(protein_locs[elem] for elem in num_labels[i])}\\n true:{tuple(protein_locs[elem] for elem in num_trues[i])}', fontsize=9)\n    plt.imshow(X_samp[i])\n    plt.axis('off')\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T16:41:05.442881Z","iopub.execute_input":"2024-09-30T16:41:05.443222Z","iopub.status.idle":"2024-09-30T16:41:12.778812Z","shell.execute_reply.started":"2024-09-30T16:41:05.443197Z","shell.execute_reply":"2024-09-30T16:41:12.777710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****To Compare predicted and true labels for each label at a time. ****","metadata":{}},{"cell_type":"code","source":"def unique_labels(label_name,protein_ids,binary_labels):\n    label_index=next(index for index,value in protein_locs.items() if value==label_name)\n    curr_protein_ids=protein_ids[binary_labels[:,label_index]==1]\n    curr_binary_labels=binary_labels[binary_labels[:,label_index]==1]\n    return curr_protein_ids,curr_binary_labels\ncurr_protein_ids,curr_protein_labels=unique_labels(\"Cytosol\",protein_ids,binary_labels)\n# print(curr_protein_ids[:5])\nval_generator = ImageDatasetPipeline(curr_protein_ids, curr_protein_labels, imgs_folder_path, batch_size=32, image_size=(256,256),augment=True)\n\nunique_preds_dataset=val_generator.create_tf_dataset()\npred_probs_curr=protein_cnnres1.predict(unique_preds_dataset)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T07:55:26.710588Z","iopub.execute_input":"2024-09-13T07:55:26.711244Z","iopub.status.idle":"2024-09-13T07:56:30.856811Z","shell.execute_reply.started":"2024-09-13T07:55:26.711211Z","shell.execute_reply":"2024-09-13T07:56:30.855892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"curr_preds_labels=(pred_probs_curr>0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T07:57:30.774414Z","iopub.execute_input":"2024-09-13T07:57:30.774859Z","iopub.status.idle":"2024-09-13T07:57:30.780314Z","shell.execute_reply.started":"2024-09-13T07:57:30.774824Z","shell.execute_reply":"2024-09-13T07:57:30.779142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_1cross5_randids(curr_protein_ids,curr_protein_labels,4,pred_labels=curr_preds_labels)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T07:59:00.773284Z","iopub.execute_input":"2024-09-13T07:59:00.773668Z","iopub.status.idle":"2024-09-13T07:59:02.575480Z","shell.execute_reply.started":"2024-09-13T07:59:00.773637Z","shell.execute_reply":"2024-09-13T07:59:02.574497Z"},"trusted":true},"execution_count":null,"outputs":[]}]}