{"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"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":61938,"databundleVersionId":6698770,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\nimport torch\nfrom torchvision import datasets, transforms, models\nfrom torch import nn, optim\nfrom torch.autograd import Variable\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\nfrom skimage import io, transform\nimport torch.utils.data as data_utils\n\nclass DogBreedsDataset(Dataset):\n   \n\n    def __init__(self, csv_file, root_dir, transform=None):\n    \n        self.labels_frame = pd.read_csv(csv_file)\n        self.map = dict(zip(self.labels_frame['breed'].unique(),range(0,len(self.labels_frame['breed'].unique()))))\n        self.labels_frame['breed'] = self.labels_frame['breed'].map(self.map)\n        self.root_dir = root_dir\n        self.transform = transform\n        \n    def getmap(self):\n        return self.map\n        \n    def __getclasses__(self):\n        return self.labels_frame['breed'].unique().tolist()\n\n    def __len__(self):\n        return len(self.labels_frame)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.root_dir, self.labels_frame.iloc[idx, 0])\n        img_name = img_name + '.jpg'\n        \n        image = io.imread(img_name)\n        PIL_image = Image.fromarray(image)\n        label = self.labels_frame.iloc[idx, 1:]\n        label = int(label.iloc[0].item())\n        label = np.asarray(label)\n        label = torch.from_numpy(label).long()\n        if self.transform:\n            image = self.transform(PIL_image)\n        #sample = {'image': image, 'label': label}\n        return image,label\n    \ncsv='/kaggle/input/dog-breed-identification-data/labels.csv'\nroot='/kaggle/input/dog-breed-identification-data/train'\ntransformer=transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])\nalldata=DogBreedsDataset(csv_file=csv, root_dir=root, transform=transformer)\n#print(\"Nombre d'éléments dans le dataset :\", len(alldata))\n#print(\"Classes uniques :\",len(alldata.__getclasses__()))\n#print(\"Correspondance des classes :\", alldata.getmap())\n\n# Utilisez un DataLoader pour itérer sur le dataset\nbatch_size = 32\ndata_loader = DataLoader(alldata, batch_size=batch_size, shuffle=True)\n\n# Exemple d'itération sur le DataLoader\nfor batch in data_loader:\n    images, labels = batch\n    #Votre code pour traiter le batch ici\n    print(\"limage\",images)\n    print(\"label\",len(labels),labels)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-20T10:28:00.11629Z","iopub.execute_input":"2024-01-20T10:28:00.116654Z","iopub.status.idle":"2024-01-20T10:29:48.290172Z","shell.execute_reply.started":"2024-01-20T10:28:00.11662Z","shell.execute_reply":"2024-01-20T10:29:48.289312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torchvision.models as models\n\nclass CustomVGG16MultiClass(nn.Module):\n    def __init__(self, num_classes):\n        super(CustomVGG16MultiClass, self).__init__()\n        \n        # Chargement du modèle VGG16 pré-entraîné\n        vgg16 = models.vgg16(pretrained=True)\n        \n        # Utilisation des couches convolutives du modèle pré-entraîné\n        self.features = vgg16.features\n        \n        # Ajout de vos propres couches de classification pour la classification multiple\n        self.classifier = nn.Sequential(\n            nn.Linear(512 * 7 * 7, 4096),\n            nn.ReLU(True),\n            nn.Dropout(),\n            nn.Linear(4096, 4096),\n            nn.ReLU(True),\n            nn.Dropout(),\n            nn.Linear(4096, num_classes),  # Utilisez le nombre de classes spécifié\n           # nn.Softmax(dim=1)  # Utilisation de Softmax pour la classification multiple\n        )\n\n    def forward(self, x):\n        # Passage à travers les couches convolutives\n        x = self.features(x)\n        \n        # Aplatir les caractéristiques pour la classification\n        x = x.view(x.size(0), -1)\n        \n        # Passage à travers les couches de classification\n        x = self.classifier(x)\n        \n        return x\n\nnum_classes = 120  # Remplacez par le nombre réel de classes\nmodel = CustomVGG16MultiClass(num_classes)\nalldata = DogBreedsDataset(csv_file=csv, root_dir=root, transform=transformer)\n\nbatch_size = 2\nall_data_loader = DataLoader(alldata, batch_size=batch_size, shuffle=True)\n\nfor x, y in all_data_loader:\n    model(x)\n    break\nprint(\"model is ready\")","metadata":{"execution":{"iopub.status.busy":"2024-01-20T10:47:02.878158Z","iopub.execute_input":"2024-01-20T10:47:02.87893Z","iopub.status.idle":"2024-01-20T10:47:25.814731Z","shell.execute_reply.started":"2024-01-20T10:47:02.878896Z","shell.execute_reply":"2024-01-20T10:47:25.813063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n# Vérifier si CUDA est disponible\nif torch.cuda.is_available():\n    # Afficher le nombre de GPU disponibles\n   \n    print(f\"Nombre de GPU disponibles : \")\nelse:\n    print(\"CUDA n'est pas disponible sur votre système.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-01-04T10:02:11.492881Z","iopub.execute_input":"2024-01-04T10:02:11.493339Z","iopub.status.idle":"2024-01-04T10:02:11.582541Z","shell.execute_reply.started":"2024-01-04T10:02:11.493309Z","shell.execute_reply":"2024-01-04T10:02:11.581507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\ndef train_model(model, train_loader, criterion, optimizer, train_on_gpu):\n    model.train()  # set the model to training mode\n    train_loss = 0.0\n    \n    for batch_i, (data, target) in enumerate(train_loader):\n        # move tensors to GPU if CUDA is available\n        if train_on_gpu:\n            data, target = data.cuda(), target.cuda()\n        \n        # clear the gradients of all optimized variables\n        optimizer.zero_grad()\n        \n        # forward pass: compute predicted outputs by passing inputs to the model\n        output = model(data)\n        \n        # calculate the batch loss\n        loss = criterion(output, target)\n        \n        # backward pass: compute gradient of the loss with respect to model parameters\n        loss.backward()\n        \n        # perform a single optimization step (parameter update)\n        optimizer.step()\n        \n        # update training loss \n        train_loss += loss.item()\n        \n        if batch_i % 20 == 19:    # print training loss every specified number of mini-batches\n            print('Batch %d loss: %.16f' % (batch_i + 1, train_loss / 20))\n            train_loss = 0.0\n\ndef evaluate_model(model, data_loader, criterion):\n    model.eval()  # set the model to evaluation mode\n    loss = 0.0\n    \n    for batch_i, (data, target) in enumerate(data_loader):\n        # move tensors to GPU if CUDA is available\n      \n        if train_on_gpu:\n            data, target = data.cuda(), target.cuda()  # Add this line to move data to GPU\n        \n            \n        # forward pass: compute predicted outputs by passing inputs to the model\n        output = model(data)\n        \n        # calculate the batch loss\n        loss += criterion(output, torch.max(target, 1)[1]).item()\n\n        if batch_i % 20 == 19:    # print validation loss every specified number of mini-batches\n            print('Batch %d loss: %.16f' % (batch_i + 1, loss / 20))\n            loss = 0.0\n    \n    return loss / len(data_loader)\n\n# Usage example:\nprint(\"ok\")","metadata":{"execution":{"iopub.status.busy":"2024-01-04T11:23:34.517454Z","iopub.execute_input":"2024-01-04T11:23:34.518337Z","iopub.status.idle":"2024-01-04T11:23:34.529757Z","shell.execute_reply.started":"2024-01-04T11:23:34.518303Z","shell.execute_reply":"2024-01-04T11:23:34.528811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nfrom torch.utils.data import Subset\n\n# Define your model, loss function, optimizer, and other hyperparameters\nnum_classes = 120\nmodel = CustomVGG16MultiClass(num_classes)\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)\n\n# Split your dataset into folds using StratifiedKFold\nnum_folds = 5  # Adjust as needed\ncv = StratifiedKFold(n_splits=num_folds, shuffle=True, random_state=0)\ntrain_on_gpu =torch.cuda.is_available()\nif train_on_gpu:\n    model.cuda()\nfor fold, (train_idx, val_idx) in enumerate(cv.split(X=alldata.labels_frame, y=alldata.labels_frame['breed'])):\n    print('****************  FOLD ', fold, '  *********************')\n\n    # Create training and validation datasets for the current fold\n    train_dataset = Subset(alldata, train_idx)\n    val_dataset = Subset(alldata, val_idx)\n\n    # Use the subsets in DataLoader for training and validation\n    train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\n    val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)\n\n    # Train the model\n    for epoch in range(2):\n        train_model(model, train_loader, criterion, optimizer, train_on_gpu)\n\n    # Evaluate the model on the validation dataset\n    validation_loss = evaluate_model(model, val_loader, criterion, train_on_gpu)\n    print(f'Fold {fold + 1}, Validation Loss: {validation_loss}')\n\n# Optionally, compute and print average performance metrics over all folds\n","metadata":{"execution":{"iopub.status.busy":"2024-01-04T11:23:43.540438Z","iopub.execute_input":"2024-01-04T11:23:43.541157Z","iopub.status.idle":"2024-01-04T11:29:27.35763Z","shell.execute_reply.started":"2024-01-04T11:23:43.541122Z","shell.execute_reply":"2024-01-04T11:29:27.355862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-01-04T11:30:32.870807Z","iopub.execute_input":"2024-01-04T11:30:32.871175Z","iopub.status.idle":"2024-01-04T11:30:40.606829Z","shell.execute_reply.started":"2024-01-04T11:30:32.871143Z","shell.execute_reply":"2024-01-04T11:30:40.605594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-01-04T11:30:58.57863Z","iopub.execute_input":"2024-01-04T11:30:58.579049Z","iopub.status.idle":"2024-01-04T11:31:01.126051Z","shell.execute_reply.started":"2024-01-04T11:30:58.579017Z","shell.execute_reply":"2024-01-04T11:31:01.124687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-12-31T17:21:00.977885Z","iopub.execute_input":"2023-12-31T17:21:00.978656Z","iopub.status.idle":"2023-12-31T17:21:06.108586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}