<jupyter_start><jupyter_text>Dermatorlogist AI We will design an algorithm that can visually diagnose **melanoma**, the deadliest from of skin cancer.The algorithm will distinguish this malignant skin tummor from two types of benign lesions (**Nevus**, **Seborrheic keratosis**)![skin tumor](images/skin_disease_classes.png)  Import Libraries<jupyter_code>import torch 
import torchvision
import numpy as np
import matplotlib.pyplot as plt
import os
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F

from torchvision import datasets, transforms
# Data loaders for training, validation, and test sets
# Using meand and std dev from the ImageNet dataset
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]

# Define Batch size
BATCH_SIZE = 20

# Composition of the transforms
data_transform = transforms.Compose([
    transforms.Resize((255, 255)),
    transforms.ToTensor(),
    transforms.Normalize(mean, std)])

# Load images
train_set = datasets.ImageFolder(root='data/test', transform=data_transform)
train_loader = torch.utils.data.DataLoader(train_set, BATCH_SIZE, shuffle=True, num_workers=2)
# Loading classes
classes = train_set.classes
print('Classes: ', classes)
n_classes = len(train_set.classes)<jupyter_output>Classes:  ['melanoma', 'nevus', 'seborrheic_keratosis']<jupyter_text>Visualize the data<jupyter_code>data_iter = iter(train_loader)
images, classes_ = data_iter.next()
# Function to return an unnormalized image
def img_show(img, mean, std):
    img = img * std[:, None, None] + mean[:, None, None] # unnormalize
    np_img = img.numpy()
    return np.transpose(np_img, (1, 2, 0))
# Plot the images in the batch with their corresponding class names
fig = plt.figure(figsize=(20, 10))
for idx in np.arange(BATCH_SIZE):
    ax = fig.add_subplot(4, 5, idx+1, xticks=[], yticks=[])
    plt.imshow(img_show(images[idx], torch.Tensor(mean), torch.Tensor(std)))
    ax.set_title(classes[classes_[idx]])
plt.show()<jupyter_output><empty_output><jupyter_text>Initialize CUDA variable<jupyter_code>use_cuda = torch.cuda.is_available()
print('Use CUDA: ', use_cuda)<jupyter_output>Use CUDA:  False<jupyter_text>Define a Convolutional Neural Network<jupyter_code>class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1),
        self.conv2 = nn.Conv2d(16, 32, 3, padding=1),
        
        self.pool = nn.MaxPool2d(2, 2),
        
        self.fc1 = nn.Linear(32 * 16 * 16, 128),
        self.fc2 = nn.Linear(128, 64),
        self.fc3 = nn.Linear(64, n_classes)
    
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        
        x = torch.flatten(x, 1) # Flatten all dimensions except batch
        
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
    
model = Net()
if use_cuda:
    model.cuda()
print(model)<jupyter_output>Net(
  (fc3): Linear(in_features=64, out_features=3, bias=True)
)<jupyter_text>Define a Loss Function and Optimizer<jupyter_code>criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12580152671755726, 'fraction_numerical': 0.02900763358778626, 'mean_word_length': 4.397034596375618, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '544182', 'license': 'MIT', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1144, 'n_words': 275, 'path': 'dermatologist_ai.ipynb', 'repo_name': 'rcepedaraza/dermatologist-ai', 'size': '3275'}