{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30628,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport tensorflow as tf\nimport cv2\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import AveragePooling2D,Conv2D,MaxPooling2D,Flatten,Dense,Resizing\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import SGD\nfrom tensorflow.keras.optimizers.schedules import PiecewiseConstantDecay\nfrom tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:24:12.832448Z","iopub.execute_input":"2023-12-28T17:24:12.83273Z","iopub.status.idle":"2023-12-28T17:24:26.247057Z","shell.execute_reply.started":"2023-12-28T17:24:12.832704Z","shell.execute_reply":"2023-12-28T17:24:26.246186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('/kaggle/working/', exist_ok=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:24:26.249014Z","iopub.execute_input":"2023-12-28T17:24:26.249712Z","iopub.status.idle":"2023-12-28T17:24:26.254759Z","shell.execute_reply.started":"2023-12-28T17:24:26.249674Z","shell.execute_reply":"2023-12-28T17:24:26.253895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MKDIR = \"/kaggle/input/UBC-OCEAN\"\ndata_train_path = os.path.join(MKDIR,'train_thumbnails')\ndata_test_path=os.path.join(MKDIR,'test_images')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:24:26.255954Z","iopub.execute_input":"2023-12-28T17:24:26.256325Z","iopub.status.idle":"2023-12-28T17:24:26.290991Z","shell.execute_reply.started":"2023-12-28T17:24:26.256274Z","shell.execute_reply":"2023-12-28T17:24:26.290063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_full = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:24:26.293641Z","iopub.execute_input":"2023-12-28T17:24:26.294477Z","iopub.status.idle":"2023-12-28T17:24:26.315018Z","shell.execute_reply.started":"2023-12-28T17:24:26.294442Z","shell.execute_reply":"2023-12-28T17:24:26.314289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_full","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:24:26.316037Z","iopub.execute_input":"2023-12-28T17:24:26.316293Z","iopub.status.idle":"2023-12-28T17:24:26.338828Z","shell.execute_reply.started":"2023-12-28T17:24:26.316271Z","shell.execute_reply":"2023-12-28T17:24:26.338092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define the data preprocessing pipline\n","metadata":{}},{"cell_type":"code","source":"def read_image(image_id):\n    target_size=(256,256)\n    image_path=os.path.join(data_train_path,'{}_thumbnail.png'.format(image_id))\n    try:\n        file = tf.io.read_file(image_path)\n        image = tf.io.decode_png(file, 3)\n        image_tf = tf.image.resize(image, (224, 224))/255\n        image = image.numpy()\n        image = Image.fromarray(np.uint8(image * 255))\n\n        image.save(os.path.join('/kaggle/working','{}_thumbnail.png'.format(image_id)))\n        #image = tf.image.per_image_standardization(image)\n        #img=img.resize((225,225))\n        #img=cv2.resize(img,(224,224))\n        #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        return image_tf\n    except Exception as e:\n        print(f\"Error reading image with id {image_id}: {e}\")\n        return None","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:24:26.339848Z","iopub.execute_input":"2023-12-28T17:24:26.340195Z","iopub.status.idle":"2023-12-28T17:24:26.347157Z","shell.execute_reply.started":"2023-12-28T17:24:26.340163Z","shell.execute_reply":"2023-12-28T17:24:26.34628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_full['image']=data_full['image_id'].apply(read_image)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:24:26.348504Z","iopub.execute_input":"2023-12-28T17:24:26.348851Z","iopub.status.idle":"2023-12-28T17:35:32.678989Z","shell.execute_reply.started":"2023-12-28T17:24:26.34882Z","shell.execute_reply":"2023-12-28T17:35:32.677825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_full","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:35:32.680645Z","iopub.execute_input":"2023-12-28T17:35:32.681043Z","iopub.status.idle":"2023-12-28T17:41:42.882511Z","shell.execute_reply.started":"2023-12-28T17:35:32.681004Z","shell.execute_reply":"2023-12-28T17:41:42.881409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_full.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:41:42.883786Z","iopub.execute_input":"2023-12-28T17:41:42.884121Z","iopub.status.idle":"2023-12-28T17:41:42.901439Z","shell.execute_reply.started":"2023-12-28T17:41:42.884094Z","shell.execute_reply":"2023-12-28T17:41:42.900525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_full.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:41:42.905107Z","iopub.execute_input":"2023-12-28T17:41:42.905713Z","iopub.status.idle":"2023-12-28T17:41:42.932415Z","shell.execute_reply.started":"2023-12-28T17:41:42.905687Z","shell.execute_reply":"2023-12-28T17:41:42.931357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_full['image'][0])\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:41:42.93361Z","iopub.execute_input":"2023-12-28T17:41:42.933898Z","iopub.status.idle":"2023-12-28T17:41:43.148117Z","shell.execute_reply.started":"2023-12-28T17:41:42.933873Z","shell.execute_reply":"2023-12-28T17:41:43.147165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape=data_full['image'][0].shape\ninput_shape=input_shape.as_list()\ninput_shape","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:41:43.149168Z","iopub.execute_input":"2023-12-28T17:41:43.149415Z","iopub.status.idle":"2023-12-28T17:41:43.155805Z","shell.execute_reply.started":"2023-12-28T17:41:43.149393Z","shell.execute_reply":"2023-12-28T17:41:43.154936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(data_full['label'], bins='auto', alpha=0.7, rwidth=0.85)\nplt.xlabel('Classes')\nplt.ylabel('Frequency')\nplt.title('Histogram of Classe')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:41:43.156752Z","iopub.execute_input":"2023-12-28T17:41:43.157099Z","iopub.status.idle":"2023-12-28T17:41:43.424947Z","shell.execute_reply.started":"2023-12-28T17:41:43.157071Z","shell.execute_reply":"2023-12-28T17:41:43.424119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder=LabelEncoder()\ndata_full['label']=label_encoder.fit_transform(data_full['label'])","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:41:43.426076Z","iopub.execute_input":"2023-12-28T17:41:43.426343Z","iopub.status.idle":"2023-12-28T17:41:43.430884Z","shell.execute_reply.started":"2023-12-28T17:41:43.426319Z","shell.execute_reply":"2023-12-28T17:41:43.430032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_full['label']","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:41:43.43182Z","iopub.execute_input":"2023-12-28T17:41:43.432115Z","iopub.status.idle":"2023-12-28T17:41:43.445557Z","shell.execute_reply.started":"2023-12-28T17:41:43.432091Z","shell.execute_reply":"2023-12-28T17:41:43.44469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build the DL model","metadata":{}},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(data_full['image'],data_full['label'],test_size=.1,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:42:05.092111Z","iopub.execute_input":"2023-12-28T17:42:05.09285Z","iopub.status.idle":"2023-12-28T17:42:05.100381Z","shell.execute_reply.started":"2023-12-28T17:42:05.092817Z","shell.execute_reply":"2023-12-28T17:42:05.099196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\n# Initial Convolution and Pooling\nmodel.add(Conv2D(32, kernel_size=(7, 7), strides=(2, 2), padding='same', input_shape=input_shape))\nmodel.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))\n\n# Dense Block 1\nfor i in range(5):\n    model.add(Conv2D(32, kernel_size=(1, 1)))\n    model.add(Conv2D(32, kernel_size=(3, 3)))\n\n# Transition Layer 1\nmodel.add(Conv2D(32, kernel_size=(1, 1)))\nmodel.add(AveragePooling2D((2, 2), padding='same'))  # Adjusted pooling layer\n\n# Dense Block 2\nfor i in range(5):\n    model.add(Conv2D(32, kernel_size=(1, 1)))\n    model.add(Conv2D(32, kernel_size=(3, 3)))\n\n# Transition Layer 2\nmodel.add(Conv2D(32, kernel_size=(1, 1)))\nmodel.add(AveragePooling2D((2, 2), padding='same'))  # Adjusted pooling layer\n\n# Dense Block 3\nfor i in range(5):\n    model.add(Conv2D(32, kernel_size=(1, 1)))\n    model.add(Conv2D(32, kernel_size=(3, 3), padding='same'))\n\n# Transition Layer 3\nmodel.add(Conv2D(32, kernel_size=(1, 1)))\nmodel.add(AveragePooling2D((2, 2), padding='same'))\n\n# Dense Block 4\nfor i in range(5):\n    model.add(Conv2D(32, kernel_size=(1, 1)))\n    model.add(Conv2D(32, kernel_size=(3, 3), padding='same'))\n\n# Classification Layer\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(5, activation='softmax'))  # Assuming you have 5 output classes\n\nmodel.build(input_shape=(None,)+tuple(input_shape))\n\nmodel.summary()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-12-28T17:43:12.298945Z","iopub.execute_input":"2023-12-28T17:43:12.300115Z","iopub.status.idle":"2023-12-28T17:43:12.921532Z","shell.execute_reply.started":"2023-12-28T17:43:12.30008Z","shell.execute_reply":"2023-12-28T17:43:12.919836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nunique_values, counts = np.unique(data_full['label'], return_counts=True)\ncounts = counts/sum(counts)\ncounts","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:43:17.094868Z","iopub.execute_input":"2023-12-28T17:43:17.095707Z","iopub.status.idle":"2023-12-28T17:43:17.103226Z","shell.execute_reply.started":"2023-12-28T17:43:17.095675Z","shell.execute_reply":"2023-12-28T17:43:17.102367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20\nbatch_size=32\n\nboundaries=[.5*epochs,.75*epochs]\nvalues=[1.,.01,.001]\nlearning_rate_schedule = keras.optimizers.schedules.PiecewiseConstantDecay(boundaries,values)\n\n# Create SGD optimizer with the defined learning rate schedule\noptimizer = SGD()\n\nhistory=model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:43:53.731425Z","iopub.execute_input":"2023-12-28T17:43:53.731809Z","iopub.status.idle":"2023-12-28T17:43:53.746968Z","shell.execute_reply.started":"2023-12-28T17:43:53.731776Z","shell.execute_reply":"2023-12-28T17:43:53.746044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_encoder = to_categorical(y_train, num_classes=5)\nX_array = np.array([tensor.numpy() for tensor in X_train])\n\nmodel.fit(X_array, y_train_encoder, epochs=epochs, batch_size=batch_size, validation_split=0.2)\n","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-12-28T17:43:54.330672Z","iopub.execute_input":"2023-12-28T17:43:54.331053Z","iopub.status.idle":"2023-12-28T17:44:10.708299Z","shell.execute_reply.started":"2023-12-28T17:43:54.331021Z","shell.execute_reply":"2023-12-28T17:44:10.707244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input,concatenate, Conv2D, MaxPooling2D, GlobalAveragePooling2D, BatchNormalization,Dense, Dropout, Flatten, Activation, Concatenate, Lambda,Dense, Dropout, Flatten, Activation, Concatenate, Lambda\nfrom tensorflow.keras import backend\nfrom tensorflow.keras.models import Model\nfrom keras import backend as K\nfrom keras.layers import Layer,InputSpec\nimport keras.layers as kl\nimport tensorflow as tf\n\n\n#Convolutional Block\ndef conv2d(x,numfilt,filtsz,strides=1,pad='same',act=True,name=None):\n    x = Conv2D(numfilt,filtsz,strides,padding=pad,data_format='channels_last',use_bias=False,name=name+'conv2d')(x)\n    x = BatchNormalization(axis=3,scale=False,name=name+'conv2d'+'bn')(x)\n    if act:\n        x = Activation('relu',name=name+'conv2d'+'act')(x)\n    return x\n\n#Inception Resnet A \n\ndef incresA(x,scale,name=None):\n    pad = 'same'\n    branch0 = conv2d(x,32,1,1,pad,True,name=name+'b0')\n    branch1 = conv2d(x,32,1,1,pad,True,name=name+'b1_1')\n    branch1 = conv2d(branch1,32,3,1,pad,True,name=name+'b1_2')\n    branch2 = conv2d(x,32,1,1,pad,True,name=name+'b2_1')\n    branch2 = conv2d(branch2,48,3,1,pad,True,name=name+'b2_2')\n    branch2 = conv2d(branch2,64,3,1,pad,True,name=name+'b2_3')\n    branches = [branch0,branch1,branch2]\n    mixed = Concatenate(axis=3, name=name + '_concat')(branches)\n    filt_exp_1x1 = conv2d(mixed,384,1,1,pad,False,name=name+'filt_exp_1x1')\n    final_lay = Lambda(lambda inputs, scale: inputs[0] + inputs[1] * scale,\n                      output_shape=backend.int_shape(x)[1:],\n                      arguments={'scale': scale},\n                      name=name+'act_scaling')([x, filt_exp_1x1])\n    return final_lay\n\n#Inception ResNet B Block\n\ndef incresB(x,scale,name=None):\n    pad = 'same'\n    branch0 = conv2d(x,192,1,1,pad,True,name=name+'b0')\n    branch1 = conv2d(x,128,1,1,pad,True,name=name+'b1_1')\n    branch1 = conv2d(branch1,160,[1,7],1,pad,True,name=name+'b1_2')\n    branch1 = conv2d(branch1,192,[7,1],1,pad,True,name=name+'b1_3')\n    branches = [branch0,branch1]\n    mixed = Concatenate(axis=3, name=name + '_mixed')(branches)\n    filt_exp_1x1 = conv2d(mixed,1152,1,1,pad,False,name=name+'filt_exp_1x1')\n    final_lay = Lambda(lambda inputs, scale: inputs[0] + inputs[1] * scale,\n                      output_shape=backend.int_shape(x)[1:],\n                      arguments={'scale': scale},\n                      name=name+'act_scaling')([x, filt_exp_1x1])\n    return final_lay\n\n#Inception ResNet C Block\n\ndef incresC(x,scale,name=None):\n    pad = 'same'\n    branch0 = conv2d(x,192,1,1,pad,True,name=name+'b0')\n    branch1 = conv2d(x,192,1,1,pad,True,name=name+'b1_1')\n    branch1 = conv2d(branch1,224,[1,3],1,pad,True,name=name+'b1_2')\n    branch1 = conv2d(branch1,256,[3,1],1,pad,True,name=name+'b1_3')\n    branches = [branch0,branch1]\n    mixed = Concatenate(axis=3, name=name + '_mixed')(branches)\n    filt_exp_1x1 = conv2d(mixed,2048,1,1,pad,False,name=name+'fin1x1')\n    final_lay = Lambda(lambda inputs, scale: inputs[0] + inputs[1] * scale,\n                      output_shape=backend.int_shape(x)[1:],\n                      arguments={'scale': scale},\n                      name=name+'act_saling')([x, filt_exp_1x1])\n    return final_lay\n\n#Soft-attention block\n\n\n\nclass SoftAttention(Layer):\n    def __init__(self,ch,m,concat_with_x=False,aggregate=False,**kwargs):\n        self.channels=int(ch)\n        self.multiheads = m\n        self.aggregate_channels = aggregate\n        self.concat_input_with_scaled = concat_with_x\n\n        \n        super(SoftAttention,self).__init__(**kwargs)\n\n    def build(self,input_shape):\n\n        self.i_shape = input_shape\n\n        kernel_shape_conv3d = (self.channels, 3, 3) + (1, self.multiheads) # DHWC\n    \n        self.out_attention_maps_shape = input_shape[0:1]+(self.multiheads,)+input_shape[1:-1]\n        \n        if self.aggregate_channels==False:\n\n            self.out_features_shape = input_shape[:-1]+(input_shape[-1]+(input_shape[-1]*self.multiheads),)\n        else:\n            if self.concat_input_with_scaled:\n                self.out_features_shape = input_shape[:-1]+(input_shape[-1]*2,)\n            else:\n                self.out_features_shape = input_shape\n        \n\n        self.kernel_conv3d = self.add_weight(shape=kernel_shape_conv3d,\n                                        initializer='he_uniform',\n                                        name='kernel_conv3d')\n        self.bias_conv3d = self.add_weight(shape=(self.multiheads,),\n                                      initializer='zeros',\n                                      name='bias_conv3d')\n\n        super(SoftAttention, self).build(input_shape)\n\n    def call(self, x):\n\n        exp_x = K.expand_dims(x,axis=-1)\n\n        c3d = K.conv3d(exp_x,\n                     kernel=self.kernel_conv3d,\n                     strides=(1,1,self.i_shape[-1]), padding='same', data_format='channels_last')\n        conv3d = K.bias_add(c3d,\n                        self.bias_conv3d)\n        conv3d = kl.Activation('relu')(conv3d)\n\n        conv3d = K.permute_dimensions(conv3d,pattern=(0,4,1,2,3))\n\n        \n        conv3d = K.squeeze(conv3d, axis=-1)\n        conv3d = K.reshape(conv3d,shape=(-1, self.multiheads ,self.i_shape[1]*self.i_shape[2]))\n\n        softmax_alpha = K.softmax(conv3d, axis=-1) \n        softmax_alpha = kl.Reshape(target_shape=(self.multiheads, self.i_shape[1],self.i_shape[2]))(softmax_alpha)\n\n        \n        if self.aggregate_channels==False:\n            exp_softmax_alpha = K.expand_dims(softmax_alpha, axis=-1)       \n            exp_softmax_alpha = K.permute_dimensions(exp_softmax_alpha,pattern=(0,2,3,1,4))\n   \n            x_exp = K.expand_dims(x,axis=-2)\n   \n            u = kl.Multiply()([exp_softmax_alpha, x_exp])   \n  \n            u = kl.Reshape(target_shape=(self.i_shape[1],self.i_shape[2],u.shape[-1]*u.shape[-2]))(u)\n\n        else:\n            exp_softmax_alpha = K.permute_dimensions(softmax_alpha,pattern=(0,2,3,1))\n\n            exp_softmax_alpha = K.sum(exp_softmax_alpha,axis=-1)\n\n            exp_softmax_alpha = K.expand_dims(exp_softmax_alpha, axis=-1)\n\n            u = kl.Multiply()([exp_softmax_alpha, x])   \n\n        if self.concat_input_with_scaled:\n            o = kl.Concatenate(axis=-1)([u,x])\n        else:\n            o = u\n        \n        return [o, softmax_alpha]\n\n    def compute_output_shape(self, input_shape): \n        return [self.out_features_shape, self.out_attention_maps_shape]\n\n    \n    def get_config(self):\n        return super(SoftAttention,self).get_config()\n #Stem block\n\nimg_input = Input(shape=input_shape)\n\nx = conv2d(img_input,32,3,2,'valid',True,name='conv1')\nx = conv2d(x,32,3,1,'valid',True,name='conv2')\nx = conv2d(x,64,3,1,'valid',True,name='conv3')\n\nx_11 = MaxPooling2D(3,strides=1,padding='valid',name='stem_br_11'+'_maxpool_1')(x)\nx_12 = conv2d(x,64,3,1,'valid',True,name='stem_br_12')\n\nx = Concatenate(axis=3, name = 'stem_concat_1')([x_11,x_12])\n\nx_21 = conv2d(x,64,1,1,'same',True,name='stem_br_211')\nx_21 = conv2d(x_21,64,[1,7],1,'same',True,name='stem_br_212')\nx_21 = conv2d(x_21,64,[7,1],1,'same',True,name='stem_br_213')\nx_21 = conv2d(x_21,96,3,1,'valid',True,name='stem_br_214')\n\nx_22 = conv2d(x,64,1,1,'same',True,name='stem_br_221')\nx_22 = conv2d(x_22,96,3,1,'valid',True,name='stem_br_222')\n\nx = Concatenate(axis=3, name = 'stem_concat_2')([x_21,x_22])\n\nx_31 = conv2d(x,192,3,1,'valid',True,name='stem_br_31')\nx_32 = MaxPooling2D(3,strides=1,padding='valid',name='stem_br_32'+'_maxpool_2')(x)\nx = Concatenate(axis=3, name = 'stem_concat_3')([x_31,x_32])\n\n#Inception-ResNet Network\n    \n\nx = incresA(x,0.15,name='incresA_1')\nx = incresA(x,0.15,name='incresA_2')\nx = incresA(x,0.15,name='incresA_3')\nx = incresA(x,0.15,name='incresA_4')\n\n#35*35 to 17*17 reduction modules\n\nx_red_11 = MaxPooling2D(3,strides=2,padding='valid',name='red_maxpool_1')(x)\n\nx_red_12 = conv2d(x,384,3,2,'valid',True,name='x_red1_c1')\n\nx_red_13 = conv2d(x,256,1,1,'same',True,name='x_red1_c2_1')\nx_red_13 = conv2d(x_red_13,256,3,1,'same',True,name='x_red1_c2_2')\nx_red_13 = conv2d(x_red_13,384,3,2,'valid',True,name='x_red1_c2_3')\n\nx = Concatenate(axis=3, name='red_concat_1')([x_red_11,x_red_12,x_red_13])\n\n# Inception-ResNet-B modules\n\nx = incresB(x,0.1,name='incresB_1')\nx = incresB(x,0.1,name='incresB_2')\nx = incresB(x,0.1,name='incresB_3')\nx = incresB(x,0.1,name='incresB_4')\nx = incresB(x,0.1,name='incresB_5')\nx = incresB(x,0.1,name='incresB_6')\nx = incresB(x,0.1,name='incresB_7')\n\n# 17*17 to 8*8 reduction module\nx_red_21 = MaxPooling2D(3,strides=2,padding='valid',name='red_maxpool_2')(x)\n\nx_red_22 = conv2d(x,256,1,1,'same',True,name='x_red2_c11')\nx_red_22 = conv2d(x_red_22,384,3,2,'valid',True,name='x_red2_c12')\n\nx_red_23 = conv2d(x,256,1,1,'same',True,name='x_red2_c21')\nx_red_23 = conv2d(x_red_23,256,3,2,'valid',True,name='x_red2_c22')\n\nx_red_24 = conv2d(x,256,1,1,'same',True,name='x_red2_c31')\nx_red_24 = conv2d(x_red_24,256,3,1,'same',True,name='x_red2_c32')\nx_red_24 = conv2d(x_red_24,256,3,2,'valid',True,name='x_red2_c33')\n\nx = Concatenate(axis=3, name='red_concat_2')([x_red_21,x_red_22,x_red_23,x_red_24])\n\n#Inception-ResNet-C modules\nx = incresC(x,0.2,name='incresC_1')\nx = incresC(x,0.2,name='incresC_2')\nx = incresC(x,0.2,name='incresC_3')\n\n#The attention layer\nattention_layer,map2 = SoftAttention(aggregate=True,m=16,concat_with_x=False,ch=int(x.shape[-1]),name='soft_attention')(x)\nattention_layer=(MaxPooling2D(pool_size=(2, 2),padding=\"same\")(attention_layer))\nconv=(MaxPooling2D(pool_size=(2, 2),padding=\"same\")(x))\n\nx = concatenate([conv,attention_layer])\nx  = Activation('relu')(x)\nx = Dropout(0.5)(x)\n\nx = Flatten()(x)\nx = Dense(5, activation='softmax')(x)\n\n\nmodel_SOFT = Model(img_input,x,name='inception_resnet_v2')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:44:19.297791Z","iopub.execute_input":"2023-12-28T17:44:19.298167Z","iopub.status.idle":"2023-12-28T17:44:22.009561Z","shell.execute_reply.started":"2023-12-28T17:44:19.298136Z","shell.execute_reply":"2023-12-28T17:44:22.008667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_SOFT.summary()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-12-28T17:44:24.155186Z","iopub.execute_input":"2023-12-28T17:44:24.155555Z","iopub.status.idle":"2023-12-28T17:44:25.1686Z","shell.execute_reply.started":"2023-12-28T17:44:24.155525Z","shell.execute_reply":"2023-12-28T17:44:25.167559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nfrom IPython.display import SVG\n\nplot_model(model_SOFT, to_file=\"model_Soft_plot.png\", \n                  show_shapes=True, show_layer_names=True)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-12-28T17:44:26.156588Z","iopub.execute_input":"2023-12-28T17:44:26.156966Z","iopub.status.idle":"2023-12-28T17:44:35.852681Z","shell.execute_reply.started":"2023-12-28T17:44:26.156929Z","shell.execute_reply":"2023-12-28T17:44:35.850936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import Adam\nmodel_SOFT.compile(Adam(lr=0.01), \n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model check point","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping\n\nfilepath = \"model_Soft.h5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_acc', \n                        verbose=1, save_best_only=True, mode='max')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:44:54.754072Z","iopub.execute_input":"2023-12-28T17:44:54.754419Z","iopub.status.idle":"2023-12-28T17:44:54.760619Z","shell.execute_reply.started":"2023-12-28T17:44:54.754394Z","shell.execute_reply":"2023-12-28T17:44:54.759541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Early stoping","metadata":{}},{"cell_type":"code","source":"early = EarlyStopping(monitor=\"val_loss\", \n                      mode=\"min\", \n                      patience=4, restore_best_weights=True)\ncallbacks_list = [checkpoint, early]","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:44:56.450908Z","iopub.execute_input":"2023-12-28T17:44:56.451746Z","iopub.status.idle":"2023-12-28T17:44:56.458209Z","shell.execute_reply.started":"2023-12-28T17:44:56.451706Z","shell.execute_reply":"2023-12-28T17:44:56.457145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Triainig the model\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\npath_csv='/kaggle/input/UBC-OCEAN/train.csv'\nimage_dir = '/kaggle/working/'\n\ndata_frame=pd.read_csv(path_csv)\n\n# Create an ImageDataGenerator\ndatagen = ImageDataGenerator(\n    rescale=1./255,  # Normalize pixel values to be between 0 and 1\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2  # Set the validation split\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:45:07.844928Z","iopub.execute_input":"2023-12-28T17:45:07.845309Z","iopub.status.idle":"2023-12-28T17:45:07.859328Z","shell.execute_reply.started":"2023-12-28T17:45:07.845283Z","shell.execute_reply":"2023-12-28T17:45:07.858383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame=data_frame[data_frame['is_tma']==False]","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:45:10.331024Z","iopub.execute_input":"2023-12-28T17:45:10.331429Z","iopub.status.idle":"2023-12-28T17:45:10.337788Z","shell.execute_reply.started":"2023-12-28T17:45:10.331401Z","shell.execute_reply":"2023-12-28T17:45:10.336696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:45:12.221835Z","iopub.execute_input":"2023-12-28T17:45:12.222587Z","iopub.status.idle":"2023-12-28T17:45:12.23257Z","shell.execute_reply.started":"2023-12-28T17:45:12.222553Z","shell.execute_reply":"2023-12-28T17:45:12.231352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame['image_id'] = data_frame['image_id'].astype(str)+'_thumbnail.png'","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:45:14.43321Z","iopub.execute_input":"2023-12-28T17:45:14.433586Z","iopub.status.idle":"2023-12-28T17:45:14.443766Z","shell.execute_reply.started":"2023-12-28T17:45:14.433556Z","shell.execute_reply":"2023-12-28T17:45:14.442641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame['image_id']","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:45:17.761441Z","iopub.execute_input":"2023-12-28T17:45:17.761808Z","iopub.status.idle":"2023-12-28T17:45:17.770851Z","shell.execute_reply.started":"2023-12-28T17:45:17.761778Z","shell.execute_reply":"2023-12-28T17:45:17.769688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training, validation, and evaluation sets\ntrain_df, temp_df = train_test_split(data_frame, test_size=0.4, random_state=42)\nval_df, eval_df = train_test_split(temp_df, test_size=0.5, random_state=42)\n\n# Create an ImageDataGenerator\ndatagen = ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True\n)\n\n# Create a training data generator\ntrain_generator = datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=image_dir,\n    x_col='image_id',\n    y_col='label',\n    target_size=(224,224),\n    batch_size=32,\n    class_mode='categorical'\n)\n\n# Create a validation data generator\nvalidation_generator = datagen.flow_from_dataframe(\n    dataframe=val_df,\n    directory=image_dir,\n    x_col='image_id',\n    y_col='label',\n    target_size=(224,224),\n    batch_size=32,\n    class_mode='categorical'\n)\n\n# Create an evaluation data generator\nevaluation_generator = datagen.flow_from_dataframe(\n    dataframe=eval_df,\n    directory=image_dir,\n    x_col='image_id',\n    y_col='label',\n    target_size=(224,224),\n    batch_size=32,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:53:27.089635Z","iopub.execute_input":"2023-12-28T17:53:27.090642Z","iopub.status.idle":"2023-12-28T17:53:27.121786Z","shell.execute_reply.started":"2023-12-28T17:53:27.090598Z","shell.execute_reply":"2023-12-28T17:53:27.121022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\n# Define a function to resize images\ndef resize_image(image_path, target_size):\n    img = Image.open(image_path)\n    img_resized = img.resize(target_size, Image.ANTIALIAS)\n    img_resized.save(image_path)\n\n# Apply the function to all images in your directory\nfor filename in os.listdir(image_dir):\n    if filename.endswith('.png'):\n        image_path = os.path.join(image_dir, filename)\n        resize_image(image_path, (224, 224))","metadata":{"scrolled":true,"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-28T17:53:28.716718Z","iopub.execute_input":"2023-12-28T17:53:28.717214Z","iopub.status.idle":"2023-12-28T17:53:43.261854Z","shell.execute_reply.started":"2023-12-28T17:53:28.717181Z","shell.execute_reply":"2023-12-28T17:53:43.260429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_SOFT = model_SOFT.fit(train_generator,\n                              steps_per_epoch=int(307/32),          \n                              validation_data=validation_generator,\n                              validation_steps=2, \n                              epochs=20, verbose=1,\n                              callbacks=callbacks_list)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_SOFT","metadata":{"execution":{"iopub.status.busy":"2023-12-28T17:53:56.967453Z","iopub.execute_input":"2023-12-28T17:53:56.968243Z","iopub.status.idle":"2023-12-28T17:53:56.972875Z","shell.execute_reply.started":"2023-12-28T17:53:56.968205Z","shell.execute_reply":"2023-12-28T17:53:56.971876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training plots\nepochs = [i for i in range(1, len(history.history['loss'])+1)]\n\nplt.plot(epochs, history_SOFT.history['loss'], color='blue', label=\"training_loss\")\nplt.plot(epochs, history_SOFT.history['val_loss'], color='red', label=\"validation_loss\")\nplt.legend(loc='best')\nplt.title('training')\nplt.xlabel('epoch')\nplt.savefig(TRAINING_PLOT_FILE, bbox_inches='tight')\nplt.show()\n\nplt.plot(epochs, history_SOFT.history['acc'], color='blue', label=\"training_accuracy\")\nplt.plot(epochs, history_SOFT.history['val_acc'], color='red',label=\"validation_accuracy\")\nplt.legend(loc='best')\nplt.title('validation')\nplt.xlabel('epoch')\nplt.savefig(VALIDATION_PLOT_FILE, bbox_inches='tight')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#make sure to load the best model\n#model.load_weights('model.h5')\n\npredictions = model.predict_generator(evaluation_generator, \n                                      steps=num_test_images, \n                                      verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}