{"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":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":7044996,"sourceType":"datasetVersion","datasetId":4053822},{"sourceId":34974522,"sourceType":"kernelVersion"},{"sourceId":145838739,"sourceType":"kernelVersion"},{"sourceId":178,"sourceType":"modelInstanceVersion","modelInstanceId":127},{"sourceId":3732,"sourceType":"modelInstanceVersion","modelInstanceId":2659}],"dockerImageVersionId":30559,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-24T15:17:52.453996Z","iopub.execute_input":"2023-11-24T15:17:52.454629Z","iopub.status.idle":"2023-11-24T15:17:53.272264Z","shell.execute_reply.started":"2023-11-24T15:17:52.454593Z","shell.execute_reply":"2023-11-24T15:17:53.271136Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image\nimport albumentations as ra\nimport cv2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import Sequential,regularizers\nfrom skimage import exposure\nimport albumentations as A\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB3","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:21:52.35123Z","iopub.execute_input":"2023-11-24T15:21:52.351679Z","iopub.status.idle":"2023-11-24T15:21:52.359941Z","shell.execute_reply.started":"2023-11-24T15:21:52.351642Z","shell.execute_reply":"2023-11-24T15:21:52.358799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")\nprint(train.head())\nprint(train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:18:03.214337Z","iopub.execute_input":"2023-11-24T15:18:03.214936Z","iopub.status.idle":"2023-11-24T15:18:03.242638Z","shell.execute_reply.started":"2023-11-24T15:18:03.214904Z","shell.execute_reply":"2023-11-24T15:18:03.241518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.describe())\nprint(train.isnull())\nistma_false=train[train[\"is_tma\"]==False]\nistma_false.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:41.897782Z","iopub.execute_input":"2023-11-24T15:19:41.898171Z","iopub.status.idle":"2023-11-24T15:19:41.936245Z","shell.execute_reply.started":"2023-11-24T15:19:41.898141Z","shell.execute_reply":"2023-11-24T15:19:41.93526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"label\"].value_counts()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nln=LabelEncoder()\ntrain[\"label\"]=ln.fit_transform(train[\"label\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-21T09:42:42.132149Z","iopub.execute_input":"2023-11-21T09:42:42.133231Z","iopub.status.idle":"2023-11-21T09:42:42.141411Z","shell.execute_reply.started":"2023-11-21T09:42:42.133185Z","shell.execute_reply":"2023-11-21T09:42:42.139895Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=\"/kaggle/input/UBC-OCEAN/train_thumbnails\"\ntrain_thumbnails=os.listdir(path)\ntest_path=\"/kaggle/input/UBC-OCEAN/test_thumbnails\"\ntest_thumbnails=os.listdir(test_path)\nprint(len(train_thumbnails))","metadata":{"execution":{"iopub.status.busy":"2023-11-23T04:52:08.462852Z","iopub.execute_input":"2023-11-23T04:52:08.464267Z","iopub.status.idle":"2023-11-23T04:52:08.480687Z","shell.execute_reply.started":"2023-11-23T04:52:08.464193Z","shell.execute_reply":"2023-11-23T04:52:08.479015Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def newdataset(row):\n    if row[\"is_tma\"]:\n        return f\"{test_path}/{row['image_id']}_thumbnail.png\"\n    else:\n        return f\"{path}/{row['image_id']}_thumbnail.png\"\ntrain[\"image_path\"]=train.apply(newdataset,axis=1)\nprint(train.head())","metadata":{"execution":{"iopub.status.busy":"2023-11-23T09:00:30.340883Z","iopub.execute_input":"2023-11-23T09:00:30.341503Z","iopub.status.idle":"2023-11-23T09:00:31.655175Z","shell.execute_reply.started":"2023-11-23T09:00:30.341459Z","shell.execute_reply":"2023-11-23T09:00:31.648287Z"},"_kg_hide-input":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_clahe(img,clip_limit=50.0,title_grid_size=(2,2)):\n    img_lab=cv2.cvtColor(img,cv2.COLOR_BGR2LAB)\n    l,a,b=cv2.split(img_lab)\n    \n    clahe=cv2.createCLAHE(clipLimit=clip_limit,tileGridSize=title_grid_size)\n    l_clahe=clahe.apply(l)\n    \n    img_lab_clahe=cv2.merge((l_clahe,a,b))\n    img_clahe=cv2.cvtColor(img_lab_clahe,cv2.COLOR_LAB2BGR)\n    \n    img_clahe=np.expand_dims(img_clahe,axis=1)\n    return img_clahe","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:19.47231Z","iopub.execute_input":"2023-11-24T15:19:19.472706Z","iopub.status.idle":"2023-11-24T15:19:19.479859Z","shell.execute_reply.started":"2023-11-24T15:19:19.472672Z","shell.execute_reply":"2023-11-24T15:19:19.478547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_hist_equalization(img):\n    img_equalized=exposure.equalize_adapthist(img)\n    return img_equalized\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:21.073529Z","iopub.execute_input":"2023-11-24T15:19:21.073892Z","iopub.status.idle":"2023-11-24T15:19:21.078415Z","shell.execute_reply.started":"2023-11-24T15:19:21.073863Z","shell.execute_reply":"2023-11-24T15:19:21.077651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_augmentation(img):\n    # Augmentation using albumentations library\n    transform = A.Compose([\n        A.RandomRotate90(),\n        A.Flip(),\n        A.Transpose(),\n        A.OneOf([\n            A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2),\n            A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20),\n        ], p=0.5),\n        A.OneOf([\n            A.Blur(blur_limit=3),\n            A.MotionBlur(blur_limit=3),\n            A.GaussNoise(var_limit=(5.0, 30.0)),\n        ], p=0.5),\n    ])\n    augmented = transform(image=img)\n    return augmented['image']\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:23.034375Z","iopub.execute_input":"2023-11-24T15:19:23.034762Z","iopub.status.idle":"2023-11-24T15:19:23.041272Z","shell.execute_reply.started":"2023-11-24T15:19:23.034708Z","shell.execute_reply":"2023-11-24T15:19:23.040159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocessing(path):\n    image=cv2.imread(path,cv2.IMREAD_COLOR)\n\n    image=cv2.resize(image,(224,224),interpolation=cv2.INTER_AREA)\n    \n   # image=apply_clahe(image)\n    #image=apply_hist_equalization(image)\n    #image=apply_augmentation(image)\n    \n    return image\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:24.718113Z","iopub.execute_input":"2023-11-24T15:19:24.718757Z","iopub.status.idle":"2023-11-24T15:19:24.725771Z","shell.execute_reply.started":"2023-11-24T15:19:24.718711Z","shell.execute_reply":"2023-11-24T15:19:24.723553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport imgaug.augmenters as iaa\naugmenter=iaa.Sequential([\n    iaa.Fliplr(0.5),\n    iaa.Affine(rotate=(-10,10))\n])\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:25.9851Z","iopub.execute_input":"2023-11-24T15:19:25.985473Z","iopub.status.idle":"2023-11-24T15:19:25.991956Z","shell.execute_reply.started":"2023-11-24T15:19:25.985445Z","shell.execute_reply":"2023-11-24T15:19:25.991009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\ndef getter(dataframe, path,augmenter_function=None):\n    classes = []\n    images = []\n    for row in dataframe.iterrows():\n        img_path = os.path.join(path,f\"{str(row[1][0])}_thumbnail.png\")\n        \n        classes.append(row[1][1])\n        images.append(preprocessing(img_path))\n       \n    \n    encoder = LabelEncoder()\n    label = encoder.fit_transform(classes)\n    labels_list = list(encoder.classes_)\n        \n    return images, label, labels_list","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:32.513865Z","iopub.execute_input":"2023-11-24T15:19:32.514254Z","iopub.status.idle":"2023-11-24T15:19:32.52118Z","shell.execute_reply.started":"2023-11-24T15:19:32.514225Z","shell.execute_reply":"2023-11-24T15:19:32.519951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=\"/kaggle/input/UBC-OCEAN/train_thumbnails\"\nimages,label,labels_list=getter(istma_false,path,augmenter_function=augmenter)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:19:45.643243Z","iopub.execute_input":"2023-11-24T15:19:45.643609Z","iopub.status.idle":"2023-11-24T15:21:52.165699Z","shell.execute_reply.started":"2023-11-24T15:19:45.643581Z","shell.execute_reply":"2023-11-24T15:21:52.164626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images=np.array(images)\nimages.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-24T16:22:46.257853Z","iopub.execute_input":"2023-11-24T16:22:46.258248Z","iopub.status.idle":"2023-11-24T16:22:46.294638Z","shell.execute_reply.started":"2023-11-24T16:22:46.258217Z","shell.execute_reply":"2023-11-24T16:22:46.293896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(images,label,test_size=0.2,random_state=100,shuffle=True)\n\ndatagen=ImageDataGenerator(rescale=1.0/255,\n                          rotation_range=40,\n                          horizontal_flip=True,\n                          vertical_flip=True,\n                          width_shift_range=0.2,\n                          height_shift_range=0.2,\n                          zoom_range=0.5,\n                          \n                          fill_mode=\"nearest\",\n                          shear_range=0.2)\n\ntrain_gen=datagen.flow(\n    x_train,\n    y=y_train,\n    batch_size=8)\nvalid_gen=datagen.flow(\n    x_test,\n    y=y_test,\n    batch_size=8)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:21:52.207872Z","iopub.execute_input":"2023-11-24T15:21:52.208266Z","iopub.status.idle":"2023-11-24T15:21:52.348686Z","shell.execute_reply.started":"2023-11-24T15:21:52.208232Z","shell.execute_reply":"2023-11-24T15:21:52.347575Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nefficientnet=EfficientNetB3(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=(224,224,3))\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-11-24T16:25:21.576086Z","iopub.execute_input":"2023-11-24T16:25:21.576591Z","iopub.status.idle":"2023-11-24T16:25:21.583359Z","shell.execute_reply.started":"2023-11-24T16:25:21.576546Z","shell.execute_reply":"2023-11-24T16:25:21.582145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path = \"/kaggle/input/efficientnetb3-weights/efficientnetb3_notop.h5\"  # Replace with the path to the downloaded file\nefficientnet = EfficientNetB3(\n    include_top=False,\n    weights=weights_path,\n    input_shape=(224, 224, 3)\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T16:35:09.452266Z","iopub.execute_input":"2023-11-24T16:35:09.45271Z","iopub.status.idle":"2023-11-24T16:35:14.02576Z","shell.execute_reply.started":"2023-11-24T16:35:09.452677Z","shell.execute_reply":"2023-11-24T16:35:14.024348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in efficientnet.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-11-24T16:35:16.537069Z","iopub.execute_input":"2023-11-24T16:35:16.53746Z","iopub.status.idle":"2023-11-24T16:35:16.554312Z","shell.execute_reply.started":"2023-11-24T16:35:16.537426Z","shell.execute_reply":"2023-11-24T16:35:16.552825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=tf.keras.layers.Flatten()(efficientnet.output)\n#x=tf.keras.layers.GlobalAveragePooling2D()(efficientnet.output)\nx=tf.keras.layers.Dropout(0.5)(x)\nx=tf.keras.layers.Dense(1024,activation=\"relu\",kernel_regularizer=regularizers.l2(1e-4))(x)\nx=tf.keras.layers.Dense(5,activation=\"softmax\")(x)\nmodel=tf.keras.Model(inputs=efficientnet.input,outputs=x)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T16:35:18.871649Z","iopub.execute_input":"2023-11-24T16:35:18.872389Z","iopub.status.idle":"2023-11-24T16:35:20.540075Z","shell.execute_reply.started":"2023-11-24T16:35:18.872343Z","shell.execute_reply":"2023-11-24T16:35:20.538547Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(model.layers))\nprint(len(efficientnet.layers))","metadata":{"execution":{"iopub.status.busy":"2023-11-24T16:26:55.417889Z","iopub.execute_input":"2023-11-24T16:26:55.418284Z","iopub.status.idle":"2023-11-24T16:26:55.423878Z","shell.execute_reply.started":"2023-11-24T16:26:55.418252Z","shell.execute_reply":"2023-11-24T16:26:55.422779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),loss=tf.keras.losses.SparseCategoricalCrossentropy(),metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:22:36.266981Z","iopub.execute_input":"2023-11-24T15:22:36.267369Z","iopub.status.idle":"2023-11-24T15:22:36.29506Z","shell.execute_reply.started":"2023-11-24T15:22:36.267339Z","shell.execute_reply":"2023-11-24T15:22:36.293955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr=ReduceLROnPlateau(monitor=\"val_loss\",factor=0.1,patience=5,min_lr=1e-5)\nepoch=50 \nhistory=model.fit(\n    images,label,validation_split=0.2,\n    epochs=epoch,\n    batch_size=8,\n    callbacks=[reduce_lr]\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:22:41.30386Z","iopub.execute_input":"2023-11-24T15:22:41.304274Z","iopub.status.idle":"2023-11-24T15:52:30.5105Z","shell.execute_reply.started":"2023-11-24T15:22:41.304239Z","shell.execute_reply":"2023-11-24T15:52:30.509147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(history.history[\"accuracy\"])\nplt.plot(history.history[\"val_accuracy\"])\nplt.title(\"training accuracy's & validation accuracy\")\nplt.xlabel(\"epoch\")\nplt.ylabel(\"accuracy\")\nplt.legend([\"Train\" ,\"validation\"],loc=\"upper left\")\nplt.show()\n\nplt.plot(history.history[\"loss\"])\nplt.plot(history.history[\"val_loss\"])\nplt.title(\"train & validation losses curve\")\nplt.xlabel(\"epoch\")\nplt.ylabel(\"loss\")\nplt.legend([\"train\", \"validation\"],loc=\"upper right\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:52:46.683034Z","iopub.execute_input":"2023-11-24T15:52:46.683804Z","iopub.status.idle":"2023-11-24T15:52:47.243354Z","shell.execute_reply.started":"2023-11-24T15:52:46.683767Z","shell.execute_reply":"2023-11-24T15:52:47.242235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer_names=[layer.name for layer in efficientnet.layers[:10]]\noutputs=[layer.output for layer in efficientnet.layers[:10]]\n\nactivation_model=tf.keras.Model(inputs=efficientnet.input,outputs=outputs)\n\nsample_image=images[0].reshape((1,224,224,3))\n\nactivation=activation_model.predict(sample_image)\n\nfor layer_name ,layer_activation in zip(layer_names,activation):\n    n_features=layer_activation.shape[-1]\n    size=layer_activation.shape[1]\n    \n    display_grid=np.zeros((size,size * n_features))\n    \n    for i in range(n_features):\n        channel_image = layer_activation[0, :, :, i]\n        channel_image -= channel_image.mean()\n        channel_image /= channel_image.std()\n        channel_image *= 64\n        channel_image += 128\n        channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n        display_grid[:, i * size : (i + 1) * size] = channel_image\n\n    scale = 1.0 / n_features\n    plt.figure(figsize=(scale * display_grid.shape[1], scale * display_grid.shape[0]))\n    plt.title(layer_name)\n    plt.grid(False)\n    plt.imshow(display_grid, aspect='auto', cmap='viridis')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-23T11:07:31.64522Z","iopub.execute_input":"2023-11-23T11:07:31.645716Z","iopub.status.idle":"2023-11-23T11:09:45.081792Z","shell.execute_reply.started":"2023-11-23T11:07:31.64568Z","shell.execute_reply":"2023-11-23T11:09:45.079122Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv(\"/kaggle/input/UBC-OCEAN/sample_submission.csv\")\nsubmission[\"label\"]=labels_list[np.argmax(labels_list)]\nsubmission.to_csv(\"submission.csv\",index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:52:53.771807Z","iopub.execute_input":"2023-11-24T15:52:53.772186Z","iopub.status.idle":"2023-11-24T15:52:53.790089Z","shell.execute_reply.started":"2023-11-24T15:52:53.772156Z","shell.execute_reply":"2023-11-24T15:52:53.789019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:52:55.506705Z","iopub.execute_input":"2023-11-24T15:52:55.507834Z","iopub.status.idle":"2023-11-24T15:52:55.517129Z","shell.execute_reply.started":"2023-11-24T15:52:55.507796Z","shell.execute_reply":"2023-11-24T15:52:55.516183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install wandb -qqq","metadata":{"execution":{"iopub.status.busy":"2023-11-18T06:44:20.334329Z","iopub.execute_input":"2023-11-18T06:44:20.335712Z","iopub.status.idle":"2023-11-18T06:44:36.142634Z","shell.execute_reply.started":"2023-11-18T06:44:20.335664Z","shell.execute_reply":"2023-11-18T06:44:36.140875Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport wandb\nwandb.login()\nWANDB_PROJECT=\"UBC-cancer\"\n\"\"\"","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-25T02:28:01.898832Z","iopub.execute_input":"2023-11-25T02:28:01.899965Z","iopub.status.idle":"2023-11-25T02:28:01.946226Z","shell.execute_reply.started":"2023-11-25T02:28:01.899919Z","shell.execute_reply":"2023-11-25T02:28:01.944979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_IMAGES_PER_BATCH=8\nNUM_BATCHES_TO_LOG=10","metadata":{"execution":{"iopub.status.busy":"2023-11-18T06:45:16.692605Z","iopub.execute_input":"2023-11-18T06:45:16.693508Z","iopub.status.idle":"2023-11-18T06:45:16.699071Z","shell.execute_reply.started":"2023-11-18T06:45:16.693467Z","shell.execute_reply":"2023-11-18T06:45:16.697793Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\"\nwandb.init(project=\"UBC-cancer\")\ncfg=wandb.config\ncfg.update({\"epochs\" : epoch, \"batch_size\": batch_size, \"lr\":0.001,\"l1_size\" : L1_SIZE, \"l2_size\": L2_SIZE,\n            \n            \"img_count\" : min(10000, NUM_IMAGES_PER_BATCH*NUM_BATCHES_TO_LOG)})\n\n\"\"\"","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-25T02:28:06.569236Z","iopub.execute_input":"2023-11-25T02:28:06.569667Z","iopub.status.idle":"2023-11-25T02:28:06.577489Z","shell.execute_reply.started":"2023-11-25T02:28:06.569636Z","shell.execute_reply":"2023-11-25T02:28:06.576336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nfrom tensorflow.keras import optimizers\nmodel_ko.compile(optimizers.Adam(lr=0.1),loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])\nepoch=20\nbatch_size=32\nL1_SIZE = 32\nL2_SIZE = 64\n\"\"\"","metadata":{"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nclass Efficientnetb3(pl.LightningModule):\n    def __init__(self,pretrained_model_path=None):\n        super().__init__()\n        self.model=timm.create_model('tf_efficientnet_b3',pretrained=False)\n        self.model.load_state_dict(torch.load(pretrained_model_path))\n        self.model.eval\n    def forward(self,x):\n        return self.model(x)\n    def configure_optimizers(self):\n        optimizer=torch.optim.Adam(self.parameters(),lr=0.01)\n        return optimizer\n    def training_step(self,batch,batch_idx):\n        x,y=batch\n        logits=self(x)\n        loss=F.cross_entropy(logits,torch.tensor(y).to(logits.device))\n        return {\"loss\":loss}\n    def validation_step(self,batch,batch_idx):\n        x,y=batch\n        logits=self(x)\n        loss=F.cross_entropy(logits,torch.tensor(y).to(logits.device))\n        return {\"val_loss\":loss}\n\"\"\"","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ntrainer=pl.Trainer(max_epochs=10)\ntrainer.fit(model, train_loader,val_dataloaders=test_loader)\n\"\"\"","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}