This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download_images(folder, type): | |
top_49 = folder[f'{type} Image URL'][:49] | |
folder_path = os.path.abspath(f'{type}-Images') | |
for i, url in enumerate(top_49): | |
with open(os.path.join(folder_path, f'{type}-{i+1}.png'), 'wb') as file: | |
file.write(requests.get(url).content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mini_grid(path, grid_type): | |
grid = Image.new('RGB', (8000, 8000)) | |
list = [f'{path}/{grid_type}-{i}.png' for i in range(1, 15)] | |
for i, img in enumerate(list, start=1): | |
if i == 1: | |
image = Image.open(img).resize((4000, 4000)) | |
grid.paste(image, (2000, 2000)) | |
if i >= 2 and i <= 4: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def full_grid(path, grid_type): | |
grid = Image.new('RGB', (8000, 8000)) | |
list = [f'{path}/{grid_type}-{i}.png' for i in range(1, 50)] | |
for i, img in enumerate(list, start=1): | |
if i == 1: | |
image = Image.open(img).resize((3200, 3200)) | |
grid.paste(image, (2400, 2400)) | |
if i >= 2 and i <= 4: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = torch.load('skin-cancer-recognition.pth', weights_only=False, map_location=device) | |
threshold = 0.6 | |
examples = [ | |
{'image_class': 'mel', 'image_id': 28087, 'lesion_type': 'Malignant Melanoma (mel)'}, | |
{'image_class': 'mel', 'image_id': 29571, 'lesion_type': 'Malignant Melanoma (mel)'}, | |
{'image_class': 'bcc', 'image_id': 32290, 'lesion_type': 'Basal Cell Carcinoma (bcc)'}, | |
{'image_class': 'vasc', 'image_id': 24867, 'lesion_type': 'Vascular Lesion (vasc)'}, | |
{'image_class': 'nv', 'image_id': 28507, 'lesion_type': 'Benign Melanocytic Nevi (nv)'}, | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def grad_cam(model, threshold, image_id, img_path, lesion_type, image_width, image_height, device): | |
img = cv2.imread(img_path) | |
img = cv2.resize(img, (image_width, image_height)) | |
img_original = cv2.resize(img.copy(), (450, 450)) | |
img = img / 255.0 | |
img = img - np.array([0.5, 0.5, 0.5]) | |
img = img / np.array([0.5, 0.5, 0.5]) | |
img = np.transpose(img, (2, 0, 1)) | |
img = torch.from_numpy(img).unsqueeze(0).float().to(device) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = CNN( | |
num_classes=len(class_names) | |
) | |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
model.to(device) | |
learning_rate = 0.001 | |
criterion = nn.CrossEntropyLoss() | |
optimizer = optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=1e-4) | |
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=3, factor=0.3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CNN(nn.Module): | |
def __init__(self, num_classes): | |
super(CNN, self).__init__() | |
self.CONV1 = 64 | |
self.CONV2 = 128 | |
self.CONV3 = 256 | |
self.CONV4 = 512 | |
self.FC1 = 64 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image_width, image_height = 224, 224 | |
data_transforms = { | |
'train': transforms.Compose([ | |
transforms.Resize((image_height, image_width)), | |
transforms.RandomHorizontalFlip(), | |
transforms.RandomRotation(degrees=15), | |
transforms.RandomResizedCrop((image_height, image_width), scale=(0.8, 1.0)), | |
transforms.ToTensor(), | |
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torch.utils.data import DataLoader | |
from torchvision import datasets, transforms | |
import torch.nn as nn | |
import torch.nn.functional as F | |
import numpy as np | |
import cv2 | |
import matplotlib.pyplot as plt | |
import matplotlib.cm as cm | |
import torch.optim as optim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# your client id | |
client_id = '' | |
# your client secret | |
client_secret = '' | |
token_url = 'https://accounts.spotify.com/api/token' | |
token_data = {'grant_type': 'client_credentials'} | |
token_response = requests.post(token_url, auth=(client_id, client_secret), data=token_data) | |
access_token = token_response.json()['access_token'] | |
headers = {'Authorization': f'Bearer {access_token}'} |
NewerOlder