{
  "id": 176930,
  "title": "For those who start image classification!",
  "url": "/competitions/siim-isic-melanoma-classification/discussion/176930",
  "author_name": "",
  "post_date": "2020-08-24T06:14:32.785723200Z",
  "votes": null,
  "comment_count": 1,
  "views": 0,
  "content": "<p>Since melanoma is an image classification problem, many of the beginner kagglers wont be able to get a grasp of how things work in neural networks for image computation.</p>\n<p>So this is a simple pytorch code!<br>\nUse this to get a grasp of basics and then start taking on the actual problem.<br>\nAm a beginner too!</p>\n<p>import torch<br>\nimport torchvision<br>\nimport torch.nn as nn<br>\nimport matplotlib.pyplot as plt<br>\nimport torchvision.transforms as transforms</p>\n<p>\"\"\" device config setup \"\"\"<br>\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")<br>\nprint(device)</p>\n<h1>MNIST dataset</h1>\n<p>\"\"\" data loading \"\"\"</p>\n<h1>training data</h1>\n<p>train_dataset = torchvision.datasets.MNIST(root=\"./data\",<br>\n                                           transform=transforms.ToTensor(),<br>\n                                           download=True,<br>\n                                           train=True)</p>\n<h1>test data</h1>\n<p>test_dataset = torchvision.datasets.MNIST(root=\"./data\",<br>\n                                          transform=transforms.ToTensor(),<br>\n                                          train=False)</p>\n<h1>hyper parameters</h1>\n<p>input_size = 784  # this is becoz the image we feed into the network is of size 28 x 28<br>\nhidden_size = 100  # random size<br>\nlearning_rate = 0.01<br>\nnum_of_classes = len(train_dataset.classes)  #this is the nunique targets in the datasets, this is the output size of the neural network<br>\nbatch_size = 100<br>\nnum_epochs = 2</p>\n<h1>loading the dataset</h1>\n<p>train_dataset_loader = torch.utils.data.DataLoader(<br>\n    dataset=train_dataset,<br>\n    batch_size=batch_size,<br>\n    shuffle=True)</p>\n<p>test_dataset_loader = torch.utils.data.DataLoader(<br>\n    dataset=test_dataset,<br>\n    batch_size=batch_size,<br>\n    shuffle=False)</p>\n<p>example = iter(train_dataset_loader)<br>\nfeat, target = example.next()<br>\nprint(feat.shape, target.shape)</p>\n<h1>print(feat, target)</h1>\n<h1>visulaizing the frst some sets of data</h1>\n<p>print(len(train_dataset.classes))<br>\nfor i in range(8):<br>\n    plt.subplot(4,2, i+1)<br>\n    plt.imshow(feat[i][0], cmap=\"gray\")</p>\n<h1>plt.show(block=False)</h1>\n<p>class NeuralNet(nn.Module):</p>\n<pre><code>def __init__(self, input_size, hidden_size, num_of_classes):\n    super(NeuralNet, self).__init__()\n    self.l1 = nn.Linear(input_size, hidden_size)\n    self.relu = nn.ReLU()\n    self.l2 = nn.Linear(hidden_size, num_of_classes)\n\ndef forward(self, x):\n    out = self.l1(x)\n    out = self.relu(out)\n    out = self.l2(out)\n    return out\n</code></pre>\n<p>model = NeuralNet(input_size, hidden_size, num_of_classes)</p>\n<h1>create the loss and optimiser</h1>\n<p>criterion = nn.CrossEntropyLoss()<br>\noptimiser = torch.optim.Adam(model.parameters(), lr=learning_rate)</p>\n<h1>training loop</h1>\n<p>no_of_steps = len(train_dataset_loader)<br>\nfor epoch in range(num_epochs):<br>\n    for i, (images, labels) in enumerate(train_dataset_loader):<br>\n        # print(images.shape)<br>\n        # print(images.reshape(-1, 28<em>28).shape)\n        images = images.reshape(-1, 28</em>28).to(device) #to convert the images to the input size initially mentioned<br>\n        labels = labels.to(device)</p>\n<pre><code>    \"\"\" forward pass \"\"\"\n    outputs = model(images)\n    loss = criterion(outputs, labels)\n\n    \"\"\" backward pass \"\"\"\n    optimiser.zero_grad()\n    loss.backward()\n    optimiser.step()\n\n    if i%100 == 0:\n        print(f\"epoch {epoch+1}/{num_epochs} step {i+1}/{no_of_steps}, loss={loss.item():.4f}\")\n</code></pre>\n<h1>testingb the peformance of the model</h1>\n<p>with torch.no_grad():<br>\n    n_correct = 0<br>\n    n_samples = 0<br>\n    for images, labels in test_dataset_loader:<br>\n        images = images.reshape(-1, 28*28).to(device) #to convert the images to the input size initially mentioned<br>\n        labels = labels.to(device)<br>\n        outputs = model(images)<br>\n        # print(outputs.shape)</p>\n<pre><code>    #value, index\n    _, predictions = torch.max(outputs, 1)\n    n_samples += labels.shape[0]\n    n_correct += (predictions == labels).sum().item()\n\nacc = 100 * n_correct/n_samples\nprint(acc)\n</code></pre>",
  "messages": [
    {
      "id": "983247",
      "postDate": "08/24/2020 06:14:32",
      "content": "<p>Since melanoma is an image classification problem, many of the beginner kagglers wont be able to get a grasp of how things work in neural networks for image computation.</p>\n<p>So this is a simple pytorch code!<br>\nUse this to get a grasp of basics and then start taking on the actual problem.<br>\nAm a beginner too!</p>\n<p>import torch<br>\nimport torchvision<br>\nimport torch.nn as nn<br>\nimport matplotlib.pyplot as plt<br>\nimport torchvision.transforms as transforms</p>\n<p>\"\"\" device config setup \"\"\"<br>\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")<br>\nprint(device)</p>\n<h1>MNIST dataset</h1>\n<p>\"\"\" data loading \"\"\"</p>\n<h1>training data</h1>\n<p>train_dataset = torchvision.datasets.MNIST(root=\"./data\",<br>\n                                           transform=transforms.ToTensor(),<br>\n                                           download=True,<br>\n                                           train=True)</p>\n<h1>test data</h1>\n<p>test_dataset = torchvision.datasets.MNIST(root=\"./data\",<br>\n                                          transform=transforms.ToTensor(),<br>\n                                          train=False)</p>\n<h1>hyper parameters</h1>\n<p>input_size = 784  # this is becoz the image we feed into the network is of size 28 x 28<br>\nhidden_size = 100  # random size<br>\nlearning_rate = 0.01<br>\nnum_of_classes = len(train_dataset.classes)  #this is the nunique targets in the datasets, this is the output size of the neural network<br>\nbatch_size = 100<br>\nnum_epochs = 2</p>\n<h1>loading the dataset</h1>\n<p>train_dataset_loader = torch.utils.data.DataLoader(<br>\n    dataset=train_dataset,<br>\n    batch_size=batch_size,<br>\n    shuffle=True)</p>\n<p>test_dataset_loader = torch.utils.data.DataLoader(<br>\n    dataset=test_dataset,<br>\n    batch_size=batch_size,<br>\n    shuffle=False)</p>\n<p>example = iter(train_dataset_loader)<br>\nfeat, target = example.next()<br>\nprint(feat.shape, target.shape)</p>\n<h1>print(feat, target)</h1>\n<h1>visulaizing the frst some sets of data</h1>\n<p>print(len(train_dataset.classes))<br>\nfor i in range(8):<br>\n    plt.subplot(4,2, i+1)<br>\n    plt.imshow(feat[i][0], cmap=\"gray\")</p>\n<h1>plt.show(block=False)</h1>\n<p>class NeuralNet(nn.Module):</p>\n<pre><code>def __init__(self, input_size, hidden_size, num_of_classes):\n    super(NeuralNet, self).__init__()\n    self.l1 = nn.Linear(input_size, hidden_size)\n    self.relu = nn.ReLU()\n    self.l2 = nn.Linear(hidden_size, num_of_classes)\n\ndef forward(self, x):\n    out = self.l1(x)\n    out = self.relu(out)\n    out = self.l2(out)\n    return out\n</code></pre>\n<p>model = NeuralNet(input_size, hidden_size, num_of_classes)</p>\n<h1>create the loss and optimiser</h1>\n<p>criterion = nn.CrossEntropyLoss()<br>\noptimiser = torch.optim.Adam(model.parameters(), lr=learning_rate)</p>\n<h1>training loop</h1>\n<p>no_of_steps = len(train_dataset_loader)<br>\nfor epoch in range(num_epochs):<br>\n    for i, (images, labels) in enumerate(train_dataset_loader):<br>\n        # print(images.shape)<br>\n        # print(images.reshape(-1, 28<em>28).shape)\n        images = images.reshape(-1, 28</em>28).to(device) #to convert the images to the input size initially mentioned<br>\n        labels = labels.to(device)</p>\n<pre><code>    \"\"\" forward pass \"\"\"\n    outputs = model(images)\n    loss = criterion(outputs, labels)\n\n    \"\"\" backward pass \"\"\"\n    optimiser.zero_grad()\n    loss.backward()\n    optimiser.step()\n\n    if i%100 == 0:\n        print(f\"epoch {epoch+1}/{num_epochs} step {i+1}/{no_of_steps}, loss={loss.item():.4f}\")\n</code></pre>\n<h1>testingb the peformance of the model</h1>\n<p>with torch.no_grad():<br>\n    n_correct = 0<br>\n    n_samples = 0<br>\n    for images, labels in test_dataset_loader:<br>\n        images = images.reshape(-1, 28*28).to(device) #to convert the images to the input size initially mentioned<br>\n        labels = labels.to(device)<br>\n        outputs = model(images)<br>\n        # print(outputs.shape)</p>\n<pre><code>    #value, index\n    _, predictions = torch.max(outputs, 1)\n    n_samples += labels.shape[0]\n    n_correct += (predictions == labels).sum().item()\n\nacc = 100 * n_correct/n_samples\nprint(acc)\n</code></pre>",
      "rawMarkdown": "Since melanoma is an image classification problem, many of the beginner kagglers wont be able to get a grasp of how things work in neural networks for image computation.\n\nSo this is a simple pytorch code!\nUse this to get a grasp of basics and then start taking on the actual problem.\nAm a beginner too!\n\n\nimport torch\nimport torchvision\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nimport torchvision.transforms as transforms\n\n\"\"\" device config setup \"\"\"\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\n# MNIST dataset\n\"\"\" data loading \"\"\"\n\n# training data\ntrain_dataset = torchvision.datasets.MNIST(root=\"./data\",\n                                           transform=transforms.ToTensor(),\n                                           download=True,\n                                           train=True)\n\n# test data\ntest_dataset = torchvision.datasets.MNIST(root=\"./data\",\n                                          transform=transforms.ToTensor(),\n                                          train=False)\n\n\n# hyper parameters\ninput_size = 784  # this is becoz the image we feed into the network is of size 28 x 28\nhidden_size = 100  # random size\nlearning_rate = 0.01\nnum_of_classes = len(train_dataset.classes)  #this is the nunique targets in the datasets, this is the output size of the neural network\nbatch_size = 100\nnum_epochs = 2\n\n\n# loading the dataset\ntrain_dataset_loader = torch.utils.data.DataLoader(\n    dataset=train_dataset,\n    batch_size=batch_size,\n    shuffle=True)\n\ntest_dataset_loader = torch.utils.data.DataLoader(\n    dataset=test_dataset,\n    batch_size=batch_size,\n    shuffle=False)\n\nexample = iter(train_dataset_loader)\nfeat, target = example.next()\nprint(feat.shape, target.shape)\n# print(feat, target)\n\n# visulaizing the frst some sets of data\n\nprint(len(train_dataset.classes))\nfor i in range(8):\n    plt.subplot(4,2, i+1)\n    plt.imshow(feat[i][0], cmap=\"gray\")\n# plt.show(block=False)\n\nclass NeuralNet(nn.Module):\n\n    def __init__(self, input_size, hidden_size, num_of_classes):\n        super(NeuralNet, self).__init__()\n        self.l1 = nn.Linear(input_size, hidden_size)\n        self.relu = nn.ReLU()\n        self.l2 = nn.Linear(hidden_size, num_of_classes)\n\n    def forward(self, x):\n        out = self.l1(x)\n        out = self.relu(out)\n        out = self.l2(out)\n        return out\n\nmodel = NeuralNet(input_size, hidden_size, num_of_classes)\n\n# create the loss and optimiser\ncriterion = nn.CrossEntropyLoss()\noptimiser = torch.optim.Adam(model.parameters(), lr=learning_rate)\n\n# training loop\nno_of_steps = len(train_dataset_loader)\nfor epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(train_dataset_loader):\n        # print(images.shape)\n        # print(images.reshape(-1, 28*28).shape)\n        images = images.reshape(-1, 28*28).to(device) #to convert the images to the input size initially mentioned\n        labels = labels.to(device)\n\n\n        \"\"\" forward pass \"\"\"\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n\n        \"\"\" backward pass \"\"\"\n        optimiser.zero_grad()\n        loss.backward()\n        optimiser.step()\n\n        if i%100 == 0:\n            print(f\"epoch {epoch+1}/{num_epochs} step {i+1}/{no_of_steps}, loss={loss.item():.4f}\")\n\n\n# testingb the peformance of the model\nwith torch.no_grad():\n    n_correct = 0\n    n_samples = 0\n    for images, labels in test_dataset_loader:\n        images = images.reshape(-1, 28*28).to(device) #to convert the images to the input size initially mentioned\n        labels = labels.to(device)\n        outputs = model(images)\n        # print(outputs.shape)\n\n        #value, index\n        _, predictions = torch.max(outputs, 1)\n        n_samples += labels.shape[0]\n        n_correct += (predictions == labels).sum().item()\n\n    acc = 100 * n_correct/n_samples\n    print(acc)",
      "votes": null
    },
    {
      "id": "987115",
      "postDate": "08/27/2020 03:03:14",
      "content": "<p>Melanoma is really not for beginners, should start with MNIST, Fashion MNIST, do some Dogs vs Cats\nThis post is irrelevant to this competition...</p>",
      "rawMarkdown": "Melanoma is really not for beginners, should start with MNIST, Fashion MNIST, do some Dogs vs Cats\nThis post is irrelevant to this competition...",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 987115,
      "author_name": "alincijov",
      "author_url": "",
      "post_date": "08/27/2020 03:03:14",
      "content": "<p>Melanoma is really not for beginners, should start with MNIST, Fashion MNIST, do some Dogs vs Cats\nThis post is irrelevant to this competition...</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "983247": "Since melanoma is an image classification problem, many of the beginner kagglers wont be able to get a grasp of how things work in neural networks for image computation.\n\nSo this is a simple pytorch code!\nUse this to get a grasp of basics and then start taking on the actual problem.\nAm a beginner too!\n\n\nimport torch\nimport torchvision\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nimport torchvision.transforms as transforms\n\n\"\"\" device config setup \"\"\"\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\n# MNIST dataset\n\"\"\" data loading \"\"\"\n\n# training data\ntrain_dataset = torchvision.datasets.MNIST(root=\"./data\",\n                                           transform=transforms.ToTensor(),\n                                           download=True,\n                                           train=True)\n\n# test data\ntest_dataset = torchvision.datasets.MNIST(root=\"./data\",\n                                          transform=transforms.ToTensor(),\n                                          train=False)\n\n\n# hyper parameters\ninput_size = 784  # this is becoz the image we feed into the network is of size 28 x 28\nhidden_size = 100  # random size\nlearning_rate = 0.01\nnum_of_classes = len(train_dataset.classes)  #this is the nunique targets in the datasets, this is the output size of the neural network\nbatch_size = 100\nnum_epochs = 2\n\n\n# loading the dataset\ntrain_dataset_loader = torch.utils.data.DataLoader(\n    dataset=train_dataset,\n    batch_size=batch_size,\n    shuffle=True)\n\ntest_dataset_loader = torch.utils.data.DataLoader(\n    dataset=test_dataset,\n    batch_size=batch_size,\n    shuffle=False)\n\nexample = iter(train_dataset_loader)\nfeat, target = example.next()\nprint(feat.shape, target.shape)\n# print(feat, target)\n\n# visulaizing the frst some sets of data\n\nprint(len(train_dataset.classes))\nfor i in range(8):\n    plt.subplot(4,2, i+1)\n    plt.imshow(feat[i][0], cmap=\"gray\")\n# plt.show(block=False)\n\nclass NeuralNet(nn.Module):\n\n    def __init__(self, input_size, hidden_size, num_of_classes):\n        super(NeuralNet, self).__init__()\n        self.l1 = nn.Linear(input_size, hidden_size)\n        self.relu = nn.ReLU()\n        self.l2 = nn.Linear(hidden_size, num_of_classes)\n\n    def forward(self, x):\n        out = self.l1(x)\n        out = self.relu(out)\n        out = self.l2(out)\n        return out\n\nmodel = NeuralNet(input_size, hidden_size, num_of_classes)\n\n# create the loss and optimiser\ncriterion = nn.CrossEntropyLoss()\noptimiser = torch.optim.Adam(model.parameters(), lr=learning_rate)\n\n# training loop\nno_of_steps = len(train_dataset_loader)\nfor epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(train_dataset_loader):\n        # print(images.shape)\n        # print(images.reshape(-1, 28*28).shape)\n        images = images.reshape(-1, 28*28).to(device) #to convert the images to the input size initially mentioned\n        labels = labels.to(device)\n\n\n        \"\"\" forward pass \"\"\"\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n\n        \"\"\" backward pass \"\"\"\n        optimiser.zero_grad()\n        loss.backward()\n        optimiser.step()\n\n        if i%100 == 0:\n            print(f\"epoch {epoch+1}/{num_epochs} step {i+1}/{no_of_steps}, loss={loss.item():.4f}\")\n\n\n# testingb the peformance of the model\nwith torch.no_grad():\n    n_correct = 0\n    n_samples = 0\n    for images, labels in test_dataset_loader:\n        images = images.reshape(-1, 28*28).to(device) #to convert the images to the input size initially mentioned\n        labels = labels.to(device)\n        outputs = model(images)\n        # print(outputs.shape)\n\n        #value, index\n        _, predictions = torch.max(outputs, 1)\n        n_samples += labels.shape[0]\n        n_correct += (predictions == labels).sum().item()\n\n    acc = 100 * n_correct/n_samples\n    print(acc)",
    "987115": "Melanoma is really not for beginners, should start with MNIST, Fashion MNIST, do some Dogs vs Cats\nThis post is irrelevant to this competition..."
  },
  "source": "meta"
}