Skip to content

Instantly share code, notes, and snippets.

View MartinWeiss12's full-sized avatar

Martin Weiss MartinWeiss12

View GitHub Profile
@MartinWeiss12
MartinWeiss12 / spotify-grid.py
Created November 18, 2024 15:22
Download Top Images
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)
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:
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:
@MartinWeiss12
MartinWeiss12 / skin-cancer-recognition.py
Last active October 23, 2024 17:40
Generate Grad-CAM Plots
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)'},
]
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)
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)
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
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])
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
@MartinWeiss12
MartinWeiss12 / spotify-wrapped.py
Last active December 12, 2023 15:02
Get Spotify API Access Token
# 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}'}