{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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# 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 pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import KFold\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n\n\n# Set paths (update these with your actual file paths)\ntrain_csv_path = '/kaggle/input/siim-isic-melanoma-classification/train.csv'\ntrain_image_path = '/kaggle/input/siim-isic-melanoma-classification/jpeg/train'\n\n# Load data\ntrain_df = pd.read_csv(train_csv_path)\ntrain_df['image_path'] = train_df['image_name'].apply(lambda x: os.path.join(train_image_path, f'{x}.jpg'))\ntrain_df['target'] = train_df['target'].astype(str)  # Convert target to string for ImageDataGenerator\n\n# Image preprocessing parameters\nIMG_SIZE = (224, 224)\nBATCH_SIZE = 32\nK_FOLDS = 5\n\n# Data augmentation\ndatagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    validation_split=0.2\n)\n\n# Define model\ndef create_model():\n    base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(128, activation='relu')(x)\n    predictions = Dense(1, activation='sigmoid')(x)\n    model = Model(inputs=base_model.input, outputs=predictions)\n    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n    return model\n\n# K-Fold Cross-Validation\nkf = KFold(n_splits=K_FOLDS, shuffle=True, random_state=42)\nfold_no = 1\nval_scores = []\n\nfor train_index, val_index in kf.split(train_df):\n    print(f'Training fold {fold_no}...')\n    \n    # Split data\n    train_data = train_df.iloc[train_index]\n    val_data = train_df.iloc[val_index]\n    \n    # Create data generators\n    train_generator = datagen.flow_from_dataframe(\n        train_data,\n        x_col='image_path',\n        y_col='target',\n        target_size=IMG_SIZE,\n        batch_size=BATCH_SIZE,\n        class_mode='binary',\n        subset='training'\n    )\n    \n    val_generator = datagen.flow_from_dataframe(\n        val_data,\n        x_col='image_path',\n        y_col='target',\n        target_size=IMG_SIZE,\n        batch_size=BATCH_SIZE,\n        class_mode='binary',\n        subset='validation'\n    )\n    \n    # Create and train model\n    model = create_model()\n    model.fit(\n        train_generator,\n        epochs=10,\n        validation_data=val_generator,\n        verbose=1\n    )\n    \n    # Evaluate model\n    val_score = model.evaluate(val_generator)[1]  # Get accuracy","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T14:29:43.859209Z","iopub.execute_input":"2025-10-11T14:29:43.859455Z","iopub.status.idle":"2025-10-11T14:35:18.406107Z","shell.execute_reply.started":"2025-10-11T14:29:43.859436Z","shell.execute_reply":"2025-10-11T14:35:18.404866Z"}},"outputs":[],"execution_count":null}]}