{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom itertools import combinations\nfrom tensorflow.keras.utils import Sequence\nfrom sklearn.model_selection import KFold","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:21:36.656169Z","iopub.execute_input":"2023-08-31T17:21:36.656543Z","iopub.status.idle":"2023-08-31T17:21:44.860949Z","shell.execute_reply.started":"2023-08-31T17:21:36.656514Z","shell.execute_reply":"2023-08-31T17:21:44.859929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base = '../input/bioverse-hackathon-a/'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:21:44.862947Z","iopub.execute_input":"2023-08-31T17:21:44.863952Z","iopub.status.idle":"2023-08-31T17:21:44.873314Z","shell.execute_reply.started":"2023-08-31T17:21:44.863883Z","shell.execute_reply":"2023-08-31T17:21:44.872278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal = '../input/bioverse-hackathon-a/train/normal'\nbeneign = '../input/bioverse-hackathon-a/train/beneign'\nmalignant = '../input/bioverse-hackathon-a/train/malignant'","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:44.875086Z","iopub.execute_input":"2023-08-31T17:21:44.876381Z","iopub.status.idle":"2023-08-31T17:21:44.881043Z","shell.execute_reply.started":"2023-08-31T17:21:44.876349Z","shell.execute_reply":"2023-08-31T17:21:44.880040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths_n =[]\nfor folder in os.listdir(normal):\n    folder_path = os.path.join(normal,folder)\n    for file in os.listdir(folder_path):\n        paths_n.append(os.path.join(folder_path,file))\n#         file_path = os.path.join(folder_path, file)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:44.884340Z","iopub.execute_input":"2023-08-31T17:21:44.885101Z","iopub.status.idle":"2023-08-31T17:21:44.985452Z","shell.execute_reply.started":"2023-08-31T17:21:44.885067Z","shell.execute_reply":"2023-08-31T17:21:44.984442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths_b =[]\nfor folder in os.listdir(beneign):\n    folder_path = os.path.join(beneign,folder)\n    for file in os.listdir(folder_path):\n        paths_b.append(os.path.join(folder_path,file))\n#         file_path = os.path.join(folder_path, file)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:44.988123Z","iopub.execute_input":"2023-08-31T17:21:44.989187Z","iopub.status.idle":"2023-08-31T17:21:45.082640Z","shell.execute_reply.started":"2023-08-31T17:21:44.989131Z","shell.execute_reply":"2023-08-31T17:21:45.081580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths_m =[]\nfor folder in os.listdir(malignant):\n    folder_path = os.path.join(malignant,folder)\n    for file in os.listdir(folder_path):\n        paths_m.append(os.path.join(folder_path,file))\n#         file_path = os.path.join(folder_path, file)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:45.084417Z","iopub.execute_input":"2023-08-31T17:21:45.084798Z","iopub.status.idle":"2023-08-31T17:21:45.143944Z","shell.execute_reply.started":"2023-08-31T17:21:45.084763Z","shell.execute_reply":"2023-08-31T17:21:45.142985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Directory where you want to put the photos\ntarget_directory = '/kaggle/working/train/normal'\n\n# Create the target directory if it doesn't exist\nif not os.path.exists(target_directory):\n    os.makedirs(target_directory)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:45.145280Z","iopub.execute_input":"2023-08-31T17:21:45.145717Z","iopub.status.idle":"2023-08-31T17:21:45.151861Z","shell.execute_reply.started":"2023-08-31T17:21:45.145681Z","shell.execute_reply":"2023-08-31T17:21:45.150601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\n# List of photo names\n\n\n\n\n# Copy or move photos to the target directory\nfor photo_name in paths_n:\n    source_path = photo_name\n    target_path = os.path.join(target_directory, source_path)\n    \n    # Use shutil.copy() to copy photos or shutil.move() to move photos\n    shutil.copy(source_path, target_directory)\n    # If you want to move the photos, use shutil.move(source_path, target_path) instead\n\nprint(\"Photos have been copied to the target directory.\")","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:45.153551Z","iopub.execute_input":"2023-08-31T17:21:45.154209Z","iopub.status.idle":"2023-08-31T17:21:48.275893Z","shell.execute_reply.started":"2023-08-31T17:21:45.154167Z","shell.execute_reply":"2023-08-31T17:21:48.274941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Directory where you want to put the photos\ntarget_directory = '/kaggle/working/train/beneign'\n\n# Create the target directory if it doesn't exist\nif not os.path.exists(target_directory):\n    os.makedirs(target_directory)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:48.278226Z","iopub.execute_input":"2023-08-31T17:21:48.279242Z","iopub.status.idle":"2023-08-31T17:21:48.284594Z","shell.execute_reply.started":"2023-08-31T17:21:48.279206Z","shell.execute_reply":"2023-08-31T17:21:48.283545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\n# List of photo names\n\n# Copy or move photos to the target directory\nfor photo_name in paths_b:\n    source_path = photo_name\n    target_path = os.path.join(target_directory, source_path)\n    \n    # Use shutil.copy() to copy photos or shutil.move() to move photos\n    shutil.copy(source_path, target_directory)\n    # If you want to move the photos, use shutil.move(source_path, target_path) instead\n\nprint(\"Photos have been copied to the target directory.\")","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:48.289856Z","iopub.execute_input":"2023-08-31T17:21:48.290528Z","iopub.status.idle":"2023-08-31T17:21:50.496997Z","shell.execute_reply.started":"2023-08-31T17:21:48.290502Z","shell.execute_reply":"2023-08-31T17:21:50.495937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Directory where you want to put the photos\ntarget_directory = '/kaggle/working/train/malignant'\n\n# Create the target directory if it doesn't exist\nif not os.path.exists(target_directory):\n    os.makedirs(target_directory)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:50.498290Z","iopub.execute_input":"2023-08-31T17:21:50.499091Z","iopub.status.idle":"2023-08-31T17:21:50.504496Z","shell.execute_reply.started":"2023-08-31T17:21:50.499055Z","shell.execute_reply":"2023-08-31T17:21:50.503369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\n# List of photo names\n\n# Copy or move photos to the target directory\nfor photo_name in paths_m:\n    source_path = photo_name\n    target_path = os.path.join(target_directory, source_path)\n    \n    # Use shutil.copy() to copy photos or shutil.move() to move photos\n    shutil.copy(source_path, target_directory)\n    # If you want to move the photos, use shutil.move(source_path, target_path) instead\n\nprint(\"Photos have been copied to the target directory.\")","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:50.505849Z","iopub.execute_input":"2023-08-31T17:21:50.507214Z","iopub.status.idle":"2023-08-31T17:21:52.414165Z","shell.execute_reply.started":"2023-08-31T17:21:50.507188Z","shell.execute_reply":"2023-08-31T17:21:52.413141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/working/'","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:52.415579Z","iopub.execute_input":"2023-08-31T17:21:52.415945Z","iopub.status.idle":"2023-08-31T17:21:52.421389Z","shell.execute_reply.started":"2023-08-31T17:21:52.415912Z","shell.execute_reply":"2023-08-31T17:21:52.420392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:21:52.423338Z","iopub.execute_input":"2023-08-31T17:21:52.424033Z","iopub.status.idle":"2023-08-31T17:21:52.435835Z","shell.execute_reply.started":"2023-08-31T17:21:52.423999Z","shell.execute_reply":"2023-08-31T17:21:52.434825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define directories\ntrain_dir = os.path.join(base_dir, 'train/')\ntest_dir = os.path.join(base, 'test/')\n\nimage_size = (224, 224)\nbatch_size = 32\nnum_folds = 5  # Number of folds for cross-validation\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255.0,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode='categorical'\n)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:21:52.438427Z","iopub.execute_input":"2023-08-31T17:21:52.439218Z","iopub.status.idle":"2023-08-31T17:21:52.463286Z","shell.execute_reply.started":"2023-08-31T17:21:52.439186Z","shell.execute_reply":"2023-08-31T17:21:52.462392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SiameseDataGenerator(Sequence):\n    def __init__(self, pairs, labels, batch_size, image_size):\n        self.pairs = pairs\n        self.labels = labels\n        self.batch_size = batch_size\n        self.image_size = image_size\n    \n    def __len__(self):\n        return len(self.pairs) // self.batch_size\n    \n    def __getitem__(self, index):\n        batch_pairs = self.pairs[index * self.batch_size: (index + 1) * self.batch_size]\n        batch_labels = self.labels[index * self.batch_size: (index + 1) * self.batch_size]\n\n        images_a = []\n        images_b = []\n        for pair in batch_pairs:\n            img_a = tf.keras.preprocessing.image.load_img(pair[0], target_size=self.image_size)\n            img_b = tf.keras.preprocessing.image.load_img(pair[1], target_size=self.image_size)\n            img_a = tf.keras.preprocessing.image.img_to_array(img_a) / 255.0\n            img_b = tf.keras.preprocessing.image.img_to_array(img_b) / 255.0\n            images_a.append(img_a)\n            images_b.append(img_b)\n\n        return [np.array(images_a), np.array(images_b)], np.array(batch_labels)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:21:52.464626Z","iopub.execute_input":"2023-08-31T17:21:52.464974Z","iopub.status.idle":"2023-08-31T17:21:52.476183Z","shell.execute_reply.started":"2023-08-31T17:21:52.464942Z","shell.execute_reply":"2023-08-31T17:21:52.475165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_siamese_pairs(directory, class_indices):\n    pairs = []\n    labels = []\n    for class_label, class_index in class_indices.items():\n        class_dir = os.path.join(directory, class_label)\n        images = os.listdir(class_dir)\n        image_pairs = list(combinations(images, 2))\n        for pair in image_pairs:\n            img1 = os.path.join(class_dir, pair[0])  # Complete path\n            img2 = os.path.join(class_dir, pair[1])  # Complete path\n            pairs.append((img1, img2))\n            labels.append(int(pair[0].split('_')[0] == pair[1].split('_')[0]))\n\n    combined = list(zip(pairs, labels))\n    random.shuffle(combined)\n    pairs[:], labels[:] = zip(*combined)\n\n    return pairs, labels","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:21:52.477848Z","iopub.execute_input":"2023-08-31T17:21:52.478120Z","iopub.status.idle":"2023-08-31T17:21:52.487265Z","shell.execute_reply.started":"2023-08-31T17:21:52.478096Z","shell.execute_reply":"2023-08-31T17:21:52.486297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pairs, train_labels = generate_siamese_pairs(train_dir, train_generator.class_indices)\n\nkf = KFold(n_splits=num_folds, shuffle=True, random_state=42)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:21:52.488790Z","iopub.execute_input":"2023-08-31T17:21:52.489744Z","iopub.status.idle":"2023-08-31T17:21:52.641462Z","shell.execute_reply.started":"2023-08-31T17:21:52.489712Z","shell.execute_reply":"2023-08-31T17:21:52.640458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold = 2\nfor train_index, val_index in kf.split(train_pairs):\n    train_pairs_fold = [train_pairs[i] for i in train_index]\n    val_pairs_fold = [train_pairs[i] for i in val_index]\n    train_labels_fold = [train_labels[i] for i in train_index]\n    val_labels_fold = [train_labels[i] for i in val_index]\n\n    train_data_generator = SiameseDataGenerator(train_pairs_fold, train_labels_fold, batch_size, image_size)\n    val_data_generator = SiameseDataGenerator(val_pairs_fold, val_labels_fold, batch_size, image_size)\n\n    class SiameseNetwork:\n        def __init__(self, input_shape):\n            input_a = tf.keras.layers.Input(shape=input_shape)\n            input_b = tf.keras.layers.Input(shape=input_shape)\n\n            conv_base = models.Sequential()\n            conv_base.add(layers.Conv2D(16, (3, 3), activation='relu', input_shape=input_shape))\n\n            encoded_a = conv_base(input_a)\n            encoded_b = conv_base(input_b)\n\n            concatenated = layers.Concatenate(axis=-1)([encoded_a, encoded_b])\n\n            x = layers.Flatten()(concatenated)\n            x = layers.Dense(32, activation='relu')(x)\n            x = layers.Dense(16, activation='relu')(x)\n            similarity_output = layers.Dense(1, activation='sigmoid')(x)\n\n            self.model = models.Model(inputs=[input_a, input_b], outputs=similarity_output)\n            self.model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n    siamese_model = SiameseNetwork(input_shape=image_size + (3,))\n    epochs = 1\n    siamese_model.model.fit(train_data_generator, epochs=epochs, validation_data=val_data_generator)\n\n    test_loss, test_accuracy = siamese_model.model.evaluate(val_data_generator)\n    print(f\"Fold {fold} - Validation Loss: {test_loss}, Validation Accuracy: {test_accuracy}\")\n\n    fold += 1","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:21:52.642994Z","iopub.execute_input":"2023-08-31T17:21:52.643379Z","iopub.status.idle":"2023-08-31T18:02:16.711057Z","shell.execute_reply.started":"2023-08-31T17:21:52.643345Z","shell.execute_reply":"2023-08-31T18:02:16.707043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1.0 / 255.0)\ntest_generator = test_datagen.flow_from_directory(\n    '/kaggle/input/bioverse-hackathon-a/test/',\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode=None  # Set class_mode to None\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:05:16.450182Z","iopub.execute_input":"2023-08-31T18:05:16.450622Z","iopub.status.idle":"2023-08-31T18:05:16.477907Z","shell.execute_reply.started":"2023-08-31T18:05:16.450587Z","shell.execute_reply":"2023-08-31T18:05:16.477010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:07:05.563904Z","iopub.execute_input":"2023-08-31T18:07:05.564291Z","iopub.status.idle":"2023-08-31T18:07:05.743263Z","shell.execute_reply.started":"2023-08-31T18:07:05.564235Z","shell.execute_reply":"2023-08-31T18:07:05.742295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_dir = os.listdir('../input/bioverse-hackathon-a/test')\ntest_imgs = []\nfor i in t_dir:\n    test_imgs.append(cv2.imread(f\"../input/bioverse-hackathon-a/test/{i}\"))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:07:05.792011Z","iopub.execute_input":"2023-08-31T18:07:05.792354Z","iopub.status.idle":"2023-08-31T18:07:19.424443Z","shell.execute_reply.started":"2023-08-31T18:07:05.792326Z","shell.execute_reply":"2023-08-31T18:07:19.423401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the trained model to make predictions\npredictions = siamese_model.model.predict(test_imgs.to)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:07:59.475180Z","iopub.execute_input":"2023-08-31T18:07:59.475562Z","iopub.status.idle":"2023-08-31T18:07:59.564129Z","shell.execute_reply.started":"2023-08-31T18:07:59.475533Z","shell.execute_reply":"2023-08-31T18:07:59.562678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf_submit = pd.DataFrame([test_label , preds_test]).T\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preds_test = [np.argmax(i) for i in predictions]\n# df_test[1] = df_test[1].replace({0:'beneign' , 1:'malignant' , 2:'normal'})","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  df_test.rename(columns = {0:'filename' , 1:'class'}, inplace = True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_test.to_csv('submission.csv',index = False,)","metadata":{},"execution_count":null,"outputs":[]}],"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"}}