{"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":"markdown","source":"# **Here Importing the Eseantial libraries **","metadata":{"papermill":{"duration":0.021038,"end_time":"2021-06-24T15:02:10.10844","exception":false,"start_time":"2021-06-24T15:02:10.087402","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport os\nimport keras\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport cv2\nfrom keras import applications\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Input\nfrom keras.models import Model\nfrom keras.optimizers import Adam","metadata":{"papermill":{"duration":6.030843,"end_time":"2021-06-24T15:02:16.160175","exception":false,"start_time":"2021-06-24T15:02:10.129332","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T14:25:23.928241Z","iopub.execute_input":"2021-06-26T14:25:23.928578Z","iopub.status.idle":"2021-06-26T14:25:29.538039Z","shell.execute_reply.started":"2021-06-26T14:25:23.928509Z","shell.execute_reply":"2021-06-26T14:25:29.537093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the Lables of the images from train.csv file ","metadata":{"papermill":{"duration":0.020155,"end_time":"2021-06-24T15:02:16.200655","exception":false,"start_time":"2021-06-24T15:02:16.1805","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dataframe = pd.read_csv('/kaggle/input/human-protein-atlas-image-classification/train.csv')\ndataframe.head(5)","metadata":{"papermill":{"duration":0.095631,"end_time":"2021-06-24T15:02:16.316174","exception":false,"start_time":"2021-06-24T15:02:16.220543","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:12:25.009082Z","iopub.execute_input":"2021-06-26T10:12:25.009398Z","iopub.status.idle":"2021-06-26T10:12:25.052967Z","shell.execute_reply.started":"2021-06-26T10:12:25.009367Z","shell.execute_reply":"2021-06-26T10:12:25.051988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" * Defining the input shape of 1st input layer \n * decalare the batch size\n * save the path of train images in the variable 'path_to_train'","metadata":{"papermill":{"duration":0.020138,"end_time":"2021-06-24T15:02:16.357501","exception":false,"start_time":"2021-06-24T15:02:16.337363","status":"completed"},"tags":[]}},{"cell_type":"code","source":"INPUT_SHAPE = (512, 512, 3)\nBATCH_SIZE = 16\npath_to_train = '/kaggle/input/human-protein-atlas-image-classification/train/'","metadata":{"papermill":{"duration":0.026904,"end_time":"2021-06-24T15:02:16.404902","exception":false,"start_time":"2021-06-24T15:02:16.377998","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:12:27.297433Z","iopub.execute_input":"2021-06-26T10:12:27.297789Z","iopub.status.idle":"2021-06-26T10:12:27.303091Z","shell.execute_reply.started":"2021-06-26T10:12:27.297756Z","shell.execute_reply":"2021-06-26T10:12:27.302123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" Adding the column with the name of complete_path and load the full path of each image on csv ","metadata":{"papermill":{"duration":0.020168,"end_time":"2021-06-24T15:02:16.44542","exception":false,"start_time":"2021-06-24T15:02:16.425252","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dataframe[\"complete_path\"] = path_to_train + dataframe[\"Id\"]\ndataframe.head(5)","metadata":{"papermill":{"duration":0.053473,"end_time":"2021-06-24T15:02:16.519584","exception":false,"start_time":"2021-06-24T15:02:16.466111","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:12:32.280967Z","iopub.execute_input":"2021-06-26T10:12:32.28129Z","iopub.status.idle":"2021-06-26T10:12:32.326111Z","shell.execute_reply.started":"2021-06-26T10:12:32.281259Z","shell.execute_reply":"2021-06-26T10:12:32.324475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Visualizing the pictures\n\n","metadata":{"papermill":{"duration":0.020428,"end_time":"2021-06-24T15:02:16.560853","exception":false,"start_time":"2021-06-24T15:02:16.540425","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import random\nfig, axes = plt.subplots(3, 4, figsize=(10, 10))\nfor i in range(3):\n    for j in range(4):\n        idx = random.randint(0, dataframe.shape[0])\n        row = dataframe.iloc[idx,:]\n        path = row.complete_path\n        red = np.array(Image.open(path + '_red.png'))\n        green = np.array(Image.open(path + '_green.png'))\n        blue = np.array(Image.open(path + '_blue.png'))\n        im = np.stack((\n                red,\n                green,\n                blue),-1)\n        axes[i][j].imshow(im)\n        axes[i][j].set_title(row.Target)\n        axes[i][j].set_xticks([])\n        axes[i][j].set_yticks([])\nfig.tight_layout()\nfig.show(5);","metadata":{"papermill":{"duration":1.622107,"end_time":"2021-06-24T15:02:18.203884","exception":false,"start_time":"2021-06-24T15:02:16.581777","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:12:34.771678Z","iopub.execute_input":"2021-06-26T10:12:34.771988Z","iopub.status.idle":"2021-06-26T10:12:36.268521Z","shell.execute_reply.started":"2021-06-26T10:12:34.77196Z","shell.execute_reply":"2021-06-26T10:12:36.267657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Spliting the data into parts train and val(valiation)","metadata":{"papermill":{"duration":0.032007,"end_time":"2021-06-24T15:02:18.26897","exception":false,"start_time":"2021-06-24T15:02:18.236963","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train, val = train_test_split(dataframe, test_size=0.2, random_state=42)\n","metadata":{"papermill":{"duration":0.045106,"end_time":"2021-06-24T15:02:18.34601","exception":false,"start_time":"2021-06-24T15:02:18.300904","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:12:42.40555Z","iopub.execute_input":"2021-06-26T10:12:42.405937Z","iopub.status.idle":"2021-06-26T10:12:42.420566Z","shell.execute_reply.started":"2021-06-26T10:12:42.405905Z","shell.execute_reply":"2021-06-26T10:12:42.419666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Shape of train: {train.shape}')\nprint(f'Shape of val: {val.shape}')","metadata":{"papermill":{"duration":0.040129,"end_time":"2021-06-24T15:02:18.417674","exception":false,"start_time":"2021-06-24T15:02:18.377545","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:12:47.42004Z","iopub.execute_input":"2021-06-26T10:12:47.420365Z","iopub.status.idle":"2021-06-26T10:12:47.425248Z","shell.execute_reply.started":"2021-06-26T10:12:47.420334Z","shell.execute_reply":"2021-06-26T10:12:47.423901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cleaning the data for better results ","metadata":{"papermill":{"duration":0.037527,"end_time":"2021-06-24T15:02:18.490848","exception":false,"start_time":"2021-06-24T15:02:18.453321","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def get_clean_data(df):\n    targets = []\n    paths = []\n    for _, row in df.iterrows():\n        target_np = np.zeros((28))\n        t = [int(t) for t in row.Target.split()]\n        target_np[t] = 1\n        targets.append(target_np)\n        paths.append(row.complete_path)\n    return np.array(paths), np.array(targets)","metadata":{"papermill":{"duration":0.040055,"end_time":"2021-06-24T15:02:18.565414","exception":false,"start_time":"2021-06-24T15:02:18.525359","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:16:12.309659Z","iopub.execute_input":"2021-06-26T10:16:12.310064Z","iopub.status.idle":"2021-06-26T10:16:12.320031Z","shell.execute_reply.started":"2021-06-26T10:16:12.310026Z","shell.execute_reply":"2021-06-26T10:16:12.318221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path, train_target = get_clean_data(train)\nval_path, val_target = get_clean_data(val)","metadata":{"papermill":{"duration":2.851145,"end_time":"2021-06-24T15:02:21.448331","exception":false,"start_time":"2021-06-24T15:02:18.597186","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:16:14.448609Z","iopub.execute_input":"2021-06-26T10:16:14.44896Z","iopub.status.idle":"2021-06-26T10:16:17.225102Z","shell.execute_reply.started":"2021-06-26T10:16:14.448929Z","shell.execute_reply":"2021-06-26T10:16:17.224243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"printing train and val path and target\n\n","metadata":{"papermill":{"duration":0.032592,"end_time":"2021-06-24T15:02:21.514193","exception":false,"start_time":"2021-06-24T15:02:21.481601","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(f'Train path shape: {train_path.shape}')\nprint(f'Train target shape: {train_target.shape}')\nprint(f'Val path shape: {val_path.shape}')\nprint(f'Val target shape: {val_target.shape}')\n","metadata":{"papermill":{"duration":0.040879,"end_time":"2021-06-24T15:02:21.58725","exception":false,"start_time":"2021-06-24T15:02:21.546371","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:16:21.436862Z","iopub.execute_input":"2021-06-26T10:16:21.437175Z","iopub.status.idle":"2021-06-26T10:16:21.449394Z","shell.execute_reply.started":"2021-06-26T10:16:21.437147Z","shell.execute_reply":"2021-06-26T10:16:21.448359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"creating datasets from cleaned data\n","metadata":{"papermill":{"duration":0.032682,"end_time":"2021-06-24T15:02:21.653914","exception":false,"start_time":"2021-06-24T15:02:21.621232","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data = tf.data.Dataset.from_tensor_slices((train_path, train_target))\nval_data = tf.data.Dataset.from_tensor_slices((val_path, val_target))","metadata":{"papermill":{"duration":1.897658,"end_time":"2021-06-24T15:02:23.584377","exception":false,"start_time":"2021-06-24T15:02:21.686719","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:16:23.963464Z","iopub.execute_input":"2021-06-26T10:16:23.963896Z","iopub.status.idle":"2021-06-26T10:16:23.98532Z","shell.execute_reply.started":"2021-06-26T10:16:23.963864Z","shell.execute_reply":"2021-06-26T10:16:23.983941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Experimental API for building input pipelines.\n\n","metadata":{"papermill":{"duration":0.053214,"end_time":"2021-06-24T15:02:23.691415","exception":false,"start_time":"2021-06-24T15:02:23.638201","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def load_data(path, target):\n    red = tf.squeeze(tf.image.decode_png(tf.io.read_file(path+'_red.png'), channels=1), [2])\n    blue = tf.squeeze(tf.image.decode_png(tf.io.read_file(path+'_blue.png'), channels=1), [2])\n    green = tf.squeeze(tf.image.decode_png(tf.io.read_file(path+'_green.png'), channels=1), [2])\n    #yellow=tf.squeeze(tf.image.decode_png(tf.io.read_file(path+'_yellow.png'), channels=1), [2])\n    img = tf.stack((\n                red,\n                green,\n                blue), axis=2)\n    return img, target\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE\n\ntrain_data = train_data.map(load_data, num_parallel_calls=AUTOTUNE)\nval_data = val_data.map(load_data, num_parallel_calls=AUTOTUNE)","metadata":{"papermill":{"duration":0.193437,"end_time":"2021-06-24T15:02:23.93746","exception":false,"start_time":"2021-06-24T15:02:23.744023","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:16:26.243865Z","iopub.execute_input":"2021-06-26T10:16:26.244191Z","iopub.status.idle":"2021-06-26T10:16:26.278259Z","shell.execute_reply.started":"2021-06-26T10:16:26.24415Z","shell.execute_reply":"2021-06-26T10:16:26.277548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Adjust the contrast of an image or images by a random factor.\n\n","metadata":{"papermill":{"duration":0.032134,"end_time":"2021-06-24T15:02:24.00251","exception":false,"start_time":"2021-06-24T15:02:23.970376","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def image_augment(img, target):\n    img = tf.image.random_contrast(img, lower=0.3, upper=2.0)\n    img = tf.image.random_flip_up_down(img)\n    img = tf.image.random_brightness(img, max_delta=0.1)\n    return img, target\n    \ntrain_data = train_data.map(image_augment, num_parallel_calls=AUTOTUNE)","metadata":{"papermill":{"duration":0.138231,"end_time":"2021-06-24T15:02:24.173171","exception":false,"start_time":"2021-06-24T15:02:24.03494","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:16:29.216198Z","iopub.execute_input":"2021-06-26T10:16:29.216705Z","iopub.status.idle":"2021-06-26T10:16:29.321914Z","shell.execute_reply.started":"2021-06-26T10:16:29.216662Z","shell.execute_reply":"2021-06-26T10:16:29.321152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Better performance with the tf.data API\n","metadata":{"papermill":{"duration":0.032562,"end_time":"2021-06-24T15:02:24.238997","exception":false,"start_time":"2021-06-24T15:02:24.206435","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data_batches = train_data.batch(BATCH_SIZE).prefetch(buffer_size=AUTOTUNE)\nval_data_batches = val_data.batch(BATCH_SIZE).prefetch(buffer_size=AUTOTUNE)","metadata":{"papermill":{"duration":0.041366,"end_time":"2021-06-24T15:02:24.313185","exception":false,"start_time":"2021-06-24T15:02:24.271819","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:17:05.607464Z","iopub.execute_input":"2021-06-26T10:17:05.607805Z","iopub.status.idle":"2021-06-26T10:17:05.617218Z","shell.execute_reply.started":"2021-06-26T10:17:05.607772Z","shell.execute_reply":"2021-06-26T10:17:05.613282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the Model Architecture ","metadata":{"papermill":{"duration":0.032278,"end_time":"2021-06-24T15:02:24.378425","exception":false,"start_time":"2021-06-24T15:02:24.346147","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"Using transfer learning and fine tuning on InceptionResNetV2","metadata":{"papermill":{"duration":0.032176,"end_time":"2021-06-24T15:02:24.443547","exception":false,"start_time":"2021-06-24T15:02:24.411371","status":"completed"},"tags":[]}},{"cell_type":"code","source":"inception_model = applications.InceptionResNetV2(include_top=False, weights='imagenet')\n\ninception_model.trainable = False\n\ninput_layer = Input(shape=INPUT_SHAPE)\nx = inception_model(input_layer)\nx = Flatten()(x)\nx = Dropout(0.5)(x)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x)\noutput = Dense(28, activation='sigmoid')(x)\nmodel = Model(input_layer, output)\n\nmodel.summary()","metadata":{"papermill":{"duration":2.808658,"end_time":"2021-06-24T15:02:27.285416","exception":false,"start_time":"2021-06-24T15:02:24.476758","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:19:21.739466Z","iopub.execute_input":"2021-06-26T10:19:21.739832Z","iopub.status.idle":"2021-06-26T10:19:27.931758Z","shell.execute_reply.started":"2021-06-26T10:19:21.739797Z","shell.execute_reply":"2021-06-26T10:19:27.930946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model compilation ","metadata":{"papermill":{"duration":0.055733,"end_time":"2021-06-24T15:02:27.378049","exception":false,"start_time":"2021-06-24T15:02:27.322316","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model.compile(optimizer=Adam(1e-3), loss='binary_crossentropy', metrics=['binary_accuracy'])","metadata":{"papermill":{"duration":0.056375,"end_time":"2021-06-24T15:02:27.470878","exception":false,"start_time":"2021-06-24T15:02:27.414503","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:19:31.636237Z","iopub.execute_input":"2021-06-26T10:19:31.636553Z","iopub.status.idle":"2021-06-26T10:19:31.660648Z","shell.execute_reply.started":"2021-06-26T10:19:31.636522Z","shell.execute_reply":"2021-06-26T10:19:31.659642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now finally fit the model for trianing ...","metadata":{"papermill":{"duration":0.037128,"end_time":"2021-06-24T15:02:27.543614","exception":false,"start_time":"2021-06-24T15:02:27.506486","status":"completed"},"tags":[]}},{"cell_type":"code","source":"history = model.fit(train_data_batches, validation_data = val_data_batches,steps_per_epoch = 50, epochs=5)","metadata":{"papermill":{"duration":1422.221833,"end_time":"2021-06-24T15:26:09.801095","exception":false,"start_time":"2021-06-24T15:02:27.579262","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:19:35.169847Z","iopub.execute_input":"2021-06-26T10:19:35.170195Z","iopub.status.idle":"2021-06-26T10:31:05.414397Z","shell.execute_reply.started":"2021-06-26T10:19:35.170159Z","shell.execute_reply":"2021-06-26T10:31:05.413594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualizing the Results ","metadata":{"papermill":{"duration":0.424489,"end_time":"2021-06-24T15:26:10.655506","exception":false,"start_time":"2021-06-24T15:26:10.231017","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model.save('InceptionV2.h5')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:04:40.134949Z","iopub.execute_input":"2021-06-26T11:04:40.135333Z","iopub.status.idle":"2021-06-26T11:04:47.648882Z","shell.execute_reply.started":"2021-06-26T11:04:40.135298Z","shell.execute_reply":"2021-06-26T11:04:47.647571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binary_accuracy=history.history['binary_accuracy']\nval_binary_accuracy=history.history['val_binary_accuracy']\nepochs=range(1,len(binary_accuracy)+1)\nplt.plot(epochs,binary_accuracy,'b',label='Training accuracy')  \nplt.plot(epochs,val_binary_accuracy,'r',label='Validation accuracy')\nplt.title('Training and Validation accuracy')\nplt.legend()\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.573804,"end_time":"2021-06-24T15:26:11.669549","exception":false,"start_time":"2021-06-24T15:26:11.095745","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T12:28:04.200535Z","iopub.execute_input":"2021-06-26T12:28:04.200969Z","iopub.status.idle":"2021-06-26T12:28:04.217755Z","shell.execute_reply.started":"2021-06-26T12:28:04.200935Z","shell.execute_reply":"2021-06-26T12:28:04.216023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss=history.history['loss']\nval_loss=history.history['val_loss']\n\nepochs=range(1,len(binary_accuracy)+1)\nplt.plot(epochs,loss,'b',label='Training loss')\nplt.plot(epochs,val_loss,'r',label='Validation loss')\nplt.title('Training and Validation loss')\nplt.legend()\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.579296,"end_time":"2021-06-24T15:26:12.665922","exception":false,"start_time":"2021-06-24T15:26:12.086626","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-26T10:50:16.158312Z","iopub.execute_input":"2021-06-26T10:50:16.15865Z","iopub.status.idle":"2021-06-26T10:50:16.395399Z","shell.execute_reply.started":"2021-06-26T10:50:16.1586Z","shell.execute_reply":"2021-06-26T10:50:16.394653Z"},"trusted":true},"execution_count":null,"outputs":[]}]}