2024-03-19 19:22:03 +01:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 1,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import numpy as np\n",
|
|
|
|
"import matplotlib.pyplot as plt\n",
|
|
|
|
"from torch.utils.data import DataLoader, random_split\n",
|
|
|
|
"from torchvision import datasets, transforms, utils\n",
|
|
|
|
"\n",
|
|
|
|
"%matplotlib inline"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 2,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Parameters\n",
|
|
|
|
"\n",
|
|
|
|
"IMG_SIZE = 224\n",
|
|
|
|
"BATCH_SIZE = 32\n",
|
|
|
|
"IMG_SHOW_NUM = 6"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 3,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Data Augmentation: resize, normalize (ImageNet mean and std)\n",
|
|
|
|
"\n",
|
|
|
|
"transformer = transforms.Compose([\n",
|
|
|
|
" transforms.Resize(size = (IMG_SIZE, IMG_SIZE), antialias = True),\n",
|
|
|
|
" transforms.ToTensor(),\n",
|
|
|
|
" transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225]),\n",
|
|
|
|
"])\n",
|
|
|
|
"\n",
|
|
|
|
"testTransformer = transforms.Compose([\n",
|
|
|
|
" transforms.Resize(size = (IMG_SIZE, IMG_SIZE), antialias = True),\n",
|
|
|
|
" transforms.ToTensor(),\n",
|
|
|
|
" transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225]),\n",
|
|
|
|
"])"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 4,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Dataset from: https://www.kaggle.com/datasets/fanconic/skin-cancer-malignant-vs-benign\n",
|
|
|
|
"\n",
|
|
|
|
"trainPath = \"./train\"\n",
|
|
|
|
"testPath = \"./test\"\n",
|
|
|
|
"\n",
|
|
|
|
"trainData = datasets.ImageFolder(root = trainPath, transform = transformer)\n",
|
|
|
|
"testData = datasets.ImageFolder(root = testPath, transform = testTransformer)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 5,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"trainLoader = DataLoader(trainData, batch_size = BATCH_SIZE, shuffle = True, num_workers = 4)\n",
|
|
|
|
"testLoader = DataLoader(testData, batch_size = BATCH_SIZE, shuffle = False, num_workers = 4)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 6,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
2024-03-20 10:47:15 +01:00
|
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAGsCAYAAAAfROn9AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/H5lhTAAAACX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M+3EtxHMfxrBlHYHUcx3EcdzUefPBB/Pt//+/xN//m38Tf+3t/D/fffz9e97rX4dWvfjU+67M+6yM9PQDAJ37iJ+Inf/In8dVf/dX4+3//7+PFL34x3vjGN+J//a//dVdZi8/0+EDW7PWvfz1EBP/4H/9jfPVXfzVe/vKX49/+23+Lr/zKr1y192FmvOlNb8K3f/u344d+6Ifwb/7Nv8HJyQle+tKX4m/8jb8RQeyf93mfh3e+8534gR/4ATzyyCN4znOeg1e+8pX4xm/8xlWG43Ecx3E8s+PY0uY4juM4fteP1772tXjb296Gt7/97R/pqXxAo9aKhx56CJ//+Z9/oevvOI7jOH7njWOM1XEcx3H8rhq3bt1a/f72t78d//E//sdzbWF+p42zs7NzLsIf+qEfwmOPPfY7fu7HcRzH0cbRYnUcx3Ecv6vGC17wAnzpl34pXvrSl+I3fuM38L3f+73Y7Xb45V/+Zfze3/t7P9LTu3S85S1vwVd91VfhC77gC/Dggw/il37pl/DP//k/x8te9jK89a1vvbCu2HEcx3H8zhvHGKvjOI7j+F01PvuzPxv/6l/9K7z73e/GNE34w3/4D+Obv/mbf0eDKgD4mI/5GLz4xS/
|
2024-03-19 19:22:03 +01:00
|
|
|
"text/plain": [
|
|
|
|
"<Figure size 700x1000 with 1 Axes>"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "display_data"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"# Show images\n",
|
|
|
|
"\n",
|
|
|
|
"def show_images(imgs, title):\n",
|
|
|
|
" grid = utils.make_grid(imgs, nrow = 3, padding = 2, normalize=True)\n",
|
|
|
|
" plt.figure(figsize = (7, 10))\n",
|
|
|
|
" plt.imshow(np.transpose(grid, (1, 2, 0)))\n",
|
|
|
|
" plt.title(title)\n",
|
|
|
|
"\n",
|
|
|
|
"dataiter = iter(trainLoader)\n",
|
|
|
|
"images, labels = next(dataiter)\n",
|
|
|
|
"show_images(images[:IMG_SHOW_NUM], title = \"Training images\")\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 7,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"Train data statistics\n",
|
|
|
|
"Number of samples: 11879\n",
|
|
|
|
"Number of classes: 2\n",
|
|
|
|
"Classes: ['Benign', 'Malignant']\n",
|
|
|
|
"Shape of the data: torch.Size([3, 224, 224])\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"# Train data statistics: number of samples, number of classes, classes, shape of the data\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Train data statistics\")\n",
|
|
|
|
"print(\"Number of samples: \", len(trainData))\n",
|
|
|
|
"print(\"Number of classes: \", len(trainData.classes))\n",
|
|
|
|
"print(\"Classes: \", trainData.classes)\n",
|
|
|
|
"print(\"Shape of the data: \", trainData[0][0].shape)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 8,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"Test data statistics\n",
|
|
|
|
"Number of samples: 2000\n",
|
|
|
|
"Number of classes: 2\n",
|
|
|
|
"Classes: ['Benign', 'Malignant']\n",
|
|
|
|
"Shape of the data: torch.Size([3, 224, 224])\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"# Test data statistics: number of samples, number of classes, classes, shape of the data\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Test data statistics\")\n",
|
|
|
|
"print(\"Number of samples: \", len(testData))\n",
|
|
|
|
"print(\"Number of classes: \", len(testData.classes))\n",
|
|
|
|
"print(\"Classes: \", testData.classes)\n",
|
|
|
|
"print(\"Shape of the data: \", testData[0][0].shape)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 9,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2024-03-20 10:47:15 +01:00
|
|
|
"{'Benign': 6289, 'Malignant': 5590}\n"
|
2024-03-19 19:22:03 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"# Number of samples per class in train data\n",
|
|
|
|
"\n",
|
2024-03-20 10:47:15 +01:00
|
|
|
"class_dict = dict.fromkeys(testData.class_to_idx, 0)\n",
|
2024-03-19 19:22:03 +01:00
|
|
|
" \n",
|
|
|
|
"for i in range(len(trainData)):\n",
|
|
|
|
" class_dict[trainData.classes[trainData[i][1]]] += 1\n",
|
|
|
|
"\n",
|
|
|
|
"print(class_dict)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 10,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"Mean: tensor([1.0394, 0.4445, 0.5912])\n",
|
|
|
|
"Std: tensor([0.5279, 0.5983, 0.6442])\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"# Mean and std of the train data\n",
|
|
|
|
"\n",
|
|
|
|
"mean = 0\n",
|
|
|
|
"std = 0\n",
|
|
|
|
"for images, _ in trainLoader:\n",
|
|
|
|
" batch_samples = images.size(0)\n",
|
|
|
|
" images = images.view(batch_samples, images.size(1), -1)\n",
|
|
|
|
" mean += images.mean(2).sum(0)\n",
|
|
|
|
" std += images.std(2).sum(0)\n",
|
|
|
|
"\n",
|
|
|
|
"mean /= len(trainLoader.dataset)\n",
|
|
|
|
"std /= len(trainLoader.dataset)\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Mean: \", mean)\n",
|
|
|
|
"print(\"Std: \", std)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-03-20 10:47:15 +01:00
|
|
|
"execution_count": 11,
|
2024-03-19 19:22:03 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2024-03-20 10:47:15 +01:00
|
|
|
"Number of samples in train data: 9503\n",
|
|
|
|
"Number of samples in validation data: 2376\n"
|
2024-03-19 19:22:03 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"# Split train data into train and validation data\n",
|
|
|
|
"\n",
|
|
|
|
"trainData, valData = random_split(trainData, [int(0.8*len(trainData)), len(trainData) - int(0.8*len(trainData))])\n",
|
|
|
|
"valLoader = DataLoader(valData, batch_size = BATCH_SIZE, shuffle = False, num_workers = 4)\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Number of samples in train data: \", len(trainData))\n",
|
|
|
|
"print(\"Number of samples in validation data: \", len(valData))"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "dl",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
|
|
|
"version": "3.11.8"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|