{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":4104,"databundleVersionId":46661,"sourceType":"competition"},{"sourceId":7866129,"sourceType":"datasetVersion","datasetId":4614938},{"sourceId":7869237,"sourceType":"datasetVersion","datasetId":4617269},{"sourceId":71482,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":59704}],"dockerImageVersionId":30733,"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 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","scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom glob import glob\nimport matplotlib.pyplot as plt\nimport cv2\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torchvision import models, transforms\nfrom torch.utils.data import Dataset, DataLoader, Subset","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:12:45.704740Z","iopub.execute_input":"2024-07-02T07:12:45.705254Z","iopub.status.idle":"2024-07-02T07:12:50.719112Z","shell.execute_reply.started":"2024-07-02T07:12:45.705227Z","shell.execute_reply":"2024-07-02T07:12:50.718088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_lbl = \"/kaggle/input/diabetic-retinopathy-detection/trainLabels.csv.zip\"\ndf_train = pd.read_csv(file_lbl, sep=',')\nprint(df_train)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths_train = glob('/kaggle/input/diabetic-retinopathy-train-unzipped/train/*.jpeg')\npaths_train","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.ToPILImage(),\n    transforms.Resize((224, 224)),\n    transforms.ToTensor()\n])\n","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:14:22.970868Z","iopub.execute_input":"2024-07-01T12:14:22.971538Z","iopub.status.idle":"2024-07-01T12:14:22.977920Z","shell.execute_reply.started":"2024-07-01T12:14:22.971505Z","shell.execute_reply":"2024-07-01T12:14:22.976084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RetinopathyDataset(Dataset):\n    def __init__(self, img_paths, labels, transform=None):\n        self.img_paths = img_paths\n        self.labels = labels\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.img_paths)\n\n    def __getitem__(self, idx):\n        img = cv2.imread(self.img_paths[idx])\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        label = self.labels[idx]\n        if self.transform:\n            img = self.transform(img)\n        return img, label","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:21:26.114137Z","iopub.execute_input":"2024-07-02T07:21:26.114779Z","iopub.status.idle":"2024-07-02T07:21:26.121401Z","shell.execute_reply.started":"2024-07-02T07:21:26.114748Z","shell.execute_reply":"2024-07-02T07:21:26.120426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = RetinopathyDataset(paths_train, df_train['level'].values, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:14:37.584752Z","iopub.execute_input":"2024-07-01T12:14:37.585078Z","iopub.status.idle":"2024-07-01T12:14:37.589644Z","shell.execute_reply.started":"2024-07-01T12:14:37.585053Z","shell.execute_reply":"2024-07-01T12:14:37.588759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subset_indices = list(range(0, len(train_dataset), 10))  ","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:14:43.233035Z","iopub.execute_input":"2024-07-01T12:14:43.233384Z","iopub.status.idle":"2024-07-01T12:14:43.238210Z","shell.execute_reply.started":"2024-07-01T12:14:43.233355Z","shell.execute_reply":"2024-07-01T12:14:43.237183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset = Subset(train_dataset, subset_indices)\ntrain_subset","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:14:49.506440Z","iopub.execute_input":"2024-07-01T12:14:49.507252Z","iopub.status.idle":"2024-07-01T12:14:49.513069Z","shell.execute_reply.started":"2024-07-01T12:14:49.507222Z","shell.execute_reply":"2024-07-01T12:14:49.512170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_subset, batch_size=32, shuffle=True)\ntrain_loader","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:14:56.718456Z","iopub.execute_input":"2024-07-01T12:14:56.719145Z","iopub.status.idle":"2024-07-01T12:14:56.725177Z","shell.execute_reply.started":"2024-07-01T12:14:56.719114Z","shell.execute_reply":"2024-07-01T12:14:56.724166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.mobilenet_v2(pretrained=True)\nmodel.classifier[1] = nn.Linear(model.classifier[1].in_features, 5)\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=1e-4)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:15:03.838661Z","iopub.execute_input":"2024-07-01T12:15:03.839019Z","iopub.status.idle":"2024-07-01T12:15:04.215628Z","shell.execute_reply.started":"2024-07-01T12:15:03.838991Z","shell.execute_reply":"2024-07-01T12:15:04.214726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(3):  # réduire le nombre d'époques\n    model.train()\n    running_loss = 0.0\n    for batch_idx, (inputs, labels) in enumerate(train_loader):\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n        \n        # Afficher l'avancement de chaque batch\n        if batch_idx % 10 == 0:  # affiche tous les 10 batches\n            print(f'Epoch [{epoch+1}/{3}], Batch [{batch_idx}/{len(train_loader)}], Loss: {loss.item():.4f}')\n","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:15:11.291961Z","iopub.execute_input":"2024-07-01T12:15:11.292310Z","iopub.status.idle":"2024-07-01T12:53:30.119808Z","shell.execute_reply.started":"2024-07-01T12:15:11.292282Z","shell.execute_reply":"2024-07-01T12:53:30.118876Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(), 'retinopathy_model.pth')","metadata":{"execution":{"iopub.status.busy":"2024-07-01T12:53:30.121764Z","iopub.execute_input":"2024-07-01T12:53:30.122291Z","iopub.status.idle":"2024-07-01T12:53:30.158724Z","shell.execute_reply.started":"2024-07-01T12:53:30.122255Z","shell.execute_reply":"2024-07-01T12:53:30.157930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Testing model","metadata":{}},{"cell_type":"code","source":"model_path = \"/kaggle/input/mobilenet-retinopathy/pytorch/mymodel/1/retinopathy_model.pth\"\ntest_path = glob(\"/kaggle/input/diabetic-retinopathy-test-unzipped/test/*.jpeg\")","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:37:10.930685Z","iopub.execute_input":"2024-07-02T07:37:10.931367Z","iopub.status.idle":"2024-07-02T07:37:11.121332Z","shell.execute_reply.started":"2024-07-02T07:37:10.931336Z","shell.execute_reply":"2024-07-02T07:37:11.120379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_path)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:37:22.925090Z","iopub.execute_input":"2024-07-02T07:37:22.925826Z","iopub.status.idle":"2024-07-02T07:37:22.931303Z","shell.execute_reply.started":"2024-07-02T07:37:22.925783Z","shell.execute_reply":"2024-07-02T07:37:22.930418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.mobilenet_v2(pretrained=True)\nmodel.classifier[1] = nn.Linear(model.classifier[1].in_features, 5)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=1e-4)\n\nmodel.load_state_dict(torch.load(model_path))\nmodel.eval() ","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:25:44.629631Z","iopub.execute_input":"2024-07-02T07:25:44.630294Z","iopub.status.idle":"2024-07-02T07:25:44.794921Z","shell.execute_reply.started":"2024-07-02T07:25:44.630263Z","shell.execute_reply":"2024-07-02T07:25:44.793757Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformTest = transforms.Compose([\n    transforms.ToPILImage(),\n    transforms.Resize((224, 224)),\n    transforms.ToTensor()\n])","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:27:46.000516Z","iopub.execute_input":"2024-07-02T07:27:46.001400Z","iopub.status.idle":"2024-07-02T07:27:46.005883Z","shell.execute_reply.started":"2024-07-02T07:27:46.001365Z","shell.execute_reply":"2024-07-02T07:27:46.005005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RetinopathyDataset(Dataset):\n    def __init__(self, img_paths, labels=None, transform=None):\n        self.img_paths = img_paths\n        self.labels = labels\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.img_paths)\n\n    def __getitem__(self, idx):\n        img = cv2.imread(self.img_paths[idx])\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        if self.transform:\n            img = self.transform(img)\n        if self.labels is not None:\n            label = self.labels[idx]\n            return img, label\n        else:\n            return img","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:35:25.934792Z","iopub.execute_input":"2024-07-02T07:35:25.935669Z","iopub.status.idle":"2024-07-02T07:35:25.942364Z","shell.execute_reply.started":"2024-07-02T07:35:25.935638Z","shell.execute_reply":"2024-07-02T07:35:25.941318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = RetinopathyDataset(test_path, transform=transformTest)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:37:31.217338Z","iopub.execute_input":"2024-07-02T07:37:31.217707Z","iopub.status.idle":"2024-07-02T07:37:31.222779Z","shell.execute_reply.started":"2024-07-02T07:37:31.217676Z","shell.execute_reply":"2024-07-02T07:37:31.221853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subset_indices_test = list(range(0, len(test_dataset), 10))  ","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:39:17.230427Z","iopub.execute_input":"2024-07-02T07:39:17.230793Z","iopub.status.idle":"2024-07-02T07:39:17.235620Z","shell.execute_reply.started":"2024-07-02T07:39:17.230767Z","shell.execute_reply":"2024-07-02T07:39:17.234676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_subset = Subset(test_dataset, subset_indices_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:39:20.996717Z","iopub.execute_input":"2024-07-02T07:39:20.997094Z","iopub.status.idle":"2024-07-02T07:39:21.001497Z","shell.execute_reply.started":"2024-07-02T07:39:20.997067Z","shell.execute_reply":"2024-07-02T07:39:21.000401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DataLoader(test_subset, batch_size=32, shuffle=True)\ntest_loader","metadata":{"execution":{"iopub.status.busy":"2024-07-02T07:39:50.575900Z","iopub.execute_input":"2024-07-02T07:39:50.576772Z","iopub.status.idle":"2024-07-02T07:39:50.582713Z","shell.execute_reply.started":"2024-07-02T07:39:50.576739Z","shell.execute_reply":"2024-07-02T07:39:50.581853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, test_loader):\n    model.eval()\n    predictions = []\n    with torch.no_grad():  # Disable gradient calculation for testing\n        for inputs in test_loader:\n            outputs = model(inputs)\n            _, predicted = torch.max(outputs, 1)\n            predictions.extend(predicted.cpu().numpy())\n    \n    return predictions\n\npredictions = predict(model, test_loader)\nprint(predictions)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predictions)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:23:52.729063Z","iopub.execute_input":"2024-07-02T08:23:52.729451Z","iopub.status.idle":"2024-07-02T08:23:52.735829Z","shell.execute_reply.started":"2024-07-02T08:23:52.729424Z","shell.execute_reply":"2024-07-02T08:23:52.734703Z"},"trusted":true},"execution_count":null,"outputs":[]}]}