{"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":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.models import Model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-12T05:30:28.290429Z","iopub.execute_input":"2023-08-12T05:30:28.291633Z","iopub.status.idle":"2023-08-12T05:30:28.298305Z","shell.execute_reply.started":"2023-08-12T05:30:28.291585Z","shell.execute_reply":"2023-08-12T05:30:28.297084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data paths and parameters\ntrain_data_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images'\nvalidation_split = 0.2\nbatch_size = 32\nimage_size = (224, 224)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Data paths and parameters\ntrain_data_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/images'\nvalidation_split = 0.2\nbatch_size = 32\nimage_size = (224, 224)  # Adjust according to your chosen architecture\n\n# Load labels from train.csv\nlabels_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\ntrain_labels = labels_df['image_level'].values  # Replace with the actual column name containing labels\n\n# Split the data into train and validation sets\nX_train_filenames, X_val_filenames, y_train, y_val = train_test_split(\n    labels_df['filename'].values, train_labels, test_size=validation_split, random_state=42\n)\n\n# Data augmentation and generators\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0/255,\n    rotation_range=20,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=labels_df,\n    directory=train_data_dir,\n    x_col='filename',   # Assuming 'filename' is the column with image filenames\n    y_col='image_level',  # Use the correct column name for labels\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode='binary',\n    subset='training'\n)\n\nvalidation_generator = train_datagen.flow_from_dataframe(\n    dataframe=labels_df,\n    directory=train_data_dir,\n    x_col='filename',\n    y_col='image_level',\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode='binary',\n    subset='validation'\n)\n\n# Define the ResNet50 base model\nbase_model = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3))\n\n# Rest of the code remains unchanged...\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-12T06:00:41.847573Z","iopub.execute_input":"2023-08-12T06:00:41.848012Z","iopub.status.idle":"2023-08-12T06:00:42.066086Z","shell.execute_reply.started":"2023-08-12T06:00:41.847981Z","shell.execute_reply":"2023-08-12T06:00:42.064456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Load pre-trained ResNet50\nbase_model = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3))\n\n# Build your custom model on top of ResNet50\nx = base_model.output\nx = Flatten()(x)\nx = Dense(128, activation='relu')(x)\noutput = Dense(1, activation='sigmoid')(x)\n\nmodel = Model(inputs=base_model.input, outputs=output)\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(\n    train_generator,\n    steps_per_epoch=train_generator.samples // batch_size,\n    epochs=10,\n    validation_data=validation_generator,\n    validation_steps=validation_generator.samples // batch_size\n)\n\n# Evaluate the model\nloss, accuracy = model.evaluate(validation_generator)\nprint(\"Validation accuracy: {:.2f}%\".format(accuracy * 100))\n\n# Make predictions on test data and create submission file\n# (You'll need to adapt this part according to your dataset structure and submission format)\ntest_data_dir = 'path/to/test_images'\ntest_datagen = ImageDataGenerator(rescale=1.0/255)\ntest_generator = test_datagen.flow_from_directory(\n    test_data_dir,\n    target_size=image_size,\n    batch_size=batch_size,\n    class_mode=None,\n    shuffle=False\n)\n\npredictions = model.predict(test_generator)\n# Create submission CSV file with predictions\n\n# Save the model\nmodel.save('abdominal_tumor_detection_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-12T05:39:50.977649Z","iopub.execute_input":"2023-08-12T05:39:50.978248Z","iopub.status.idle":"2023-08-12T05:39:53.448598Z","shell.execute_reply.started":"2023-08-12T05:39:50.978207Z","shell.execute_reply":"2023-08-12T05:39:53.446768Z"},"trusted":true},"execution_count":null,"outputs":[]}]}