{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-11T13:29:58.440063Z","iopub.execute_input":"2023-11-11T13:29:58.44075Z","iopub.status.idle":"2023-11-11T13:29:58.82197Z","shell.execute_reply.started":"2023-11-11T13:29:58.440714Z","shell.execute_reply":"2023-11-11T13:29:58.821077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport torch \nimport torch.nn as nn\nfrom torchvision import models, transforms, datasets\nimport random\nimport zipfile\nfrom torch import optim\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:32:17.310278Z","iopub.execute_input":"2023-11-11T13:32:17.311174Z","iopub.status.idle":"2023-11-11T13:32:20.128895Z","shell.execute_reply.started":"2023-11-11T13:32:17.311134Z","shell.execute_reply":"2023-11-11T13:32:20.127711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/dogs-vs-cats/train.zip'\ntest_path = '/kaggle/input/dogs-vs-cats/test.zip'","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:33:28.935723Z","iopub.execute_input":"2023-11-11T13:33:28.936114Z","iopub.status.idle":"2023-11-11T13:33:28.940871Z","shell.execute_reply.started":"2023-11-11T13:33:28.936083Z","shell.execute_reply":"2023-11-11T13:33:28.939804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(train_path, 'r') as train_zip:\n    train_zip.extractall('.')  \nimage_dir = \"../working/train/\"\nfilenames = os.listdir(image_dir)\nlabels = [x.split(\".\")[0] for x in filenames]\ndf = pd.DataFrame({\"filename\": filenames, \"label\": labels})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:33:34.551843Z","iopub.execute_input":"2023-11-11T13:33:34.552254Z","iopub.status.idle":"2023-11-11T13:33:46.246661Z","shell.execute_reply.started":"2023-11-11T13:33:34.552223Z","shell.execute_reply":"2023-11-11T13:33:46.245841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_data = df.groupby(\"label\")\n\nnum_images_per_category = 5\n\nfig, axes = plt.subplots(len(grouped_data), num_images_per_category, figsize=(20, 20))\n\nfor i, (category, group) in enumerate(grouped_data):\n  \n    random_indices = random.sample(range(len(group)), num_images_per_category)\n\n    for j, index in enumerate(random_indices):\n        filename = group.iloc[index][\"filename\"]\n        label = group.iloc[index][\"label\"]\n        image_path = os.path.join(image_dir, filename)\n        image = Image.open(image_path)\n\n        axes[i, j].imshow(image)\n        axes[i, j].set_title(\"Label: \" + label, fontsize = 30)\n        \n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-11T13:57:24.315354Z","iopub.execute_input":"2023-11-11T13:57:24.315741Z","iopub.status.idle":"2023-11-11T13:57:28.666751Z","shell.execute_reply.started":"2023-11-11T13:57:24.315712Z","shell.execute_reply":"2023-11-11T13:57:28.664068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.densenet121(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-11T14:32:45.377512Z","iopub.execute_input":"2023-11-11T14:32:45.37789Z","iopub.status.idle":"2023-11-11T14:32:47.403928Z","shell.execute_reply.started":"2023-11-11T14:32:45.377861Z","shell.execute_reply":"2023-11-11T14:32:47.402819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 2\n\nfrom collections import OrderedDict\nclassifier = nn.Sequential(OrderedDict([\n                          ('fc1', nn.Linear(1024, 500)),\n                          ('relu', nn.ReLU()),\n                          ('fc2', nn.Linear(500, 2)),\n                          ('output', nn.LogSoftmax(dim=1))\n                          ]))\n#Общий эффект этого кода заключается в создании и\n#установке внутри модели нейронной сети, которая состоит из двух полносвязных слоев, \n#функции активации ReLU и слоя LogSoftmax для решения задачи бинарной классификации с двумя классами.\n\nmodel.classifier = classifier","metadata":{"execution":{"iopub.status.busy":"2023-11-11T14:41:51.485592Z","iopub.execute_input":"2023-11-11T14:41:51.486423Z","iopub.status.idle":"2023-11-11T14:41:51.498446Z","shell.execute_reply.started":"2023-11-11T14:41:51.486384Z","shell.execute_reply":"2023-11-11T14:41:51.49741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for param in model.features.parameters():\n    param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2023-11-11T14:42:11.750393Z","iopub.execute_input":"2023-11-11T14:42:11.750752Z","iopub.status.idle":"2023-11-11T14:42:11.757651Z","shell.execute_reply.started":"2023-11-11T14:42:11.750723Z","shell.execute_reply":"2023-11-11T14:42:11.756645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion= nn.NLLLoss()\n#criterion = nn.CrossEntropyLoss()\n\noptimizer=optim.Adam(model.classifier.parameters(),lr=0.001)\n#optimizer = torch.optim.SGD(model.classifier.parameters(), lr=0.001)\n\nmean = [0.485, 0.456, 0.406]\nstd = [0.229, 0.224, 0.225]\n\ntrain_transforms = transforms.Compose([\n                                        transforms.Resize((224, 224)),  # Resize the image to 224x224\n                                        transforms.ToTensor(),         # Convert the image to a PyTorch tensor\n                                        transforms.Normalize(mean, std)  # Normalize with specified mean and std\n                                        ])\n\ntest_transforms = transforms.Compose([\n                                        transforms.Resize((224, 224)),  # Resize the image to 224x224\n                                        transforms.ToTensor(),         # Convert the image to a PyTorch tensor\n                                        transforms.Normalize(mean, std)  # Normalize with specified mean and std\n                                        ])","metadata":{"execution":{"iopub.status.busy":"2023-11-11T14:43:28.014268Z","iopub.execute_input":"2023-11-11T14:43:28.014686Z","iopub.status.idle":"2023-11-11T14:43:28.022441Z","shell.execute_reply.started":"2023-11-11T14:43:28.014654Z","shell.execute_reply":"2023-11-11T14:43:28.021382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/train/')\n\nwith zipfile.ZipFile(test_path, 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/test/')\ntrain_data = datasets.ImageFolder(root='/kaggle/working/train/', transform=train_transforms)\ntest_data = datasets.ImageFolder(root='/kaggle/working/test/', transform=test_transforms)\n\n# DataLoader\ntrainloader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)\ntestloader = torch.utils.data.DataLoader(test_data, batch_size=32, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-11T14:44:56.202339Z","iopub.execute_input":"2023-11-11T14:44:56.203139Z","iopub.status.idle":"2023-11-11T14:45:08.24392Z","shell.execute_reply.started":"2023-11-11T14:44:56.202909Z","shell.execute_reply":"2023-11-11T14:45:08.243022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 2\nprint_every = 10\nfor epoch in range(num_epochs):\n    model.train()\n    running_loss = 0.0\n    for i, (inputs, labels) in enumerate(trainloader, 1):\n        optimizer.zero_grad()\n        #inputs, labels = inputs.to('cuda'), labels.to('cuda')  # Move data to GPU\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n\n        if i % print_every == 0:\n            print(f\"Epoch: {epoch+1}/{num_epochs}, Batch: {i}/{len(trainloader)}, Loss: {running_loss/print_every:.7f}\")\n            running_loss = 0.0\n    \n    # Validation\n    model.eval()\n    test_loss = 0.0\n    accuracy = 0\n    with torch.no_grad():\n        for inputs, labels in testloader:\n            #inputs, labels = inputs.to('cuda'), labels.to('cuda')  # Move data to GPU\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n            test_loss += loss.item()\n            ps = torch.exp(outputs)\n            top_p, top_class = ps.topk(1, dim=1)\n            equals = top_class == labels.view(*top_class.shape)\n            accuracy += torch.mean(equals.type(torch.FloatTensor))\n\n    print(f\"Epoch: {epoch+1}/{num_epochs}.. \"\n          f\"Training Loss: {running_loss/print_every:.3f}.. \"\n          f\"Test Loss: {test_loss/len(testloader):.3f}.. \"\n          f\"Test Accuracy: {accuracy/len(testloader):.3f}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-11T14:47:27.068261Z","iopub.execute_input":"2023-11-11T14:47:27.068689Z","iopub.status.idle":"2023-11-11T17:39:28.537807Z","shell.execute_reply.started":"2023-11-11T14:47:27.068655Z","shell.execute_reply":"2023-11-11T17:39:28.535627Z"},"trusted":true},"execution_count":null,"outputs":[]}]}