{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import necessary libraries\nfrom fastai.vision.all import *\nimport pandas as pd\nimport os\n\n# Set up path to dataset\npath = Path(\"/kaggle/input/aptos2019-blindness-detection\")\n\n# Load CSV and create labels\ndf = pd.read_csv(path/'train.csv')\n\n# Define the DataBlock\n# FastAI automatically handles augmentation, resizing, and normalization\ndblock = DataBlock(\n    blocks=(ImageBlock, CategoryBlock),\n    get_x=lambda x: path/'train_images'/f'{x[\"id_code\"]}.png',\n    get_y=ColReader('diagnosis'),\n    splitter=RandomSplitter(seed=42),\n    item_tfms=Resize(224),  # Resize images to 224x224\n    batch_tfms=aug_transforms(mult=2.0)  # Apply augmentations\n)\n\n# Create a DataLoader from the DataBlock\ndls = dblock.dataloaders(df, bs=32)  # Increase batch size if you have more GPU memory\n\n# Visualize some sample images\ndls.show_batch(max_n=9, figsize=(7, 7))\n\n# Create a learner using a more powerful model, like EfficientNet\nlearn = cnn_learner(dls, resnet50, metrics=accuracy, pretrained=True).to_fp16()  # fp16 for mixed precision\n\n# Train using FastAI's one-cycle policy\nlearn.fine_tune(10)  # You can adjust the number of epochs\n\n# Evaluate the model\nlearn.show_results()\n\n# Save the model\nlearn.save('resnet50-diabetic-retinopathy-model')\n\n# You can also use FastAI's learning rate finder to find the best learning rate\nlearn.lr_find()\n\n# Now, train again with a better learning rate\nlearn.fine_tune(10, base_lr=1e-3)  # Adjust the learning rate according to the LR finder\n\n# Plot training metrics\nlearn.recorder.plot_loss()\n\n# You can also export the model for future use\nlearn.export('resnet50-model.pkl')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}