Skip to content

Instantly share code, notes, and snippets.

View konverner's full-sized avatar

Konstantin Verner konverner

  • Bordeaux, France
View GitHub Profile
@konverner
konverner / pil_image_from_url.py
Created April 27, 2023 22:31
Read image with PIL from url
from PIL import Image
import requests
from io import BytesIO
url = 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png'
response = requests.get(url)
img = Image.open(BytesIO(response.content))
@konverner
konverner / iqr_anomaly_processing.py
Last active April 22, 2023 10:04
Apply IQR method for anomaly remove for the given columns in a dataframe
N_before = len(df)
COLUMNS_TO_PROCESS = ['sqft_living', 'sqft_basement', 'bedrooms']
TRESHOLD_Q3 = 0.95
TRESHOLD_Q1 = 0.05
for column_name in COLUMNS_TO_PROCESS:
Q3 = df[column_name].quantile(TRESHOLD_Q3)
Q1 = df[column_name].quantile(TRESHOLD_Q1)
print(f"{column_name} [Q1, Q3] : [{Q1}, {Q3}]")
IQR = Q3 - Q1
@konverner
konverner / numb_nan_percent.py
Created April 22, 2023 09:15
show number of NaNs in percents
pd.options.display.float_format = '{:,.1f}%'.format
print((df.isna().sum()/df.count()).sort_values())
@konverner
konverner / clf_borders.py
Last active April 9, 2023 13:47
visualise classifier borders
X_train = ... # (n,2) numpy array dataset
# Create a meshgrid
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
np.arange(y_min, y_max, 0.1))
# Make predictions on the meshgrid
@konverner
konverner / fillna.py
Created April 8, 2023 21:35
fillna based on some conditions
column = ...
cond1, cond2 = ..., ...
agg_func = 'mean'
if agg_func == 'mean':
new_value = df[(cond1) & (cond2)][column].mean()
else:
new_value = df[(cond1) & (cond2)][column].mode()[0]
df.loc[(cond1) & (cond2), column] = df.loc[(cond1) & (cond2)][column].fillna(new_value)
@konverner
konverner / mixup.py
Created February 12, 2023 21:58
pytorch implementation of mixup
def mixup(X : torch.tensor,
Y: torch.tensor,
alpha: int=8,
device: str='cpu'):
"""
it performs mixup between the first half of the batch with the second one
params
---
X : images tensor [bs, c, h, w]
@konverner
konverner / grad_norm.py
Created February 5, 2023 11:58
compute gradient norm of pytorch model
def grad_norm(model: nn.Module):
total_norm = 0
for p in model.parameters():
param_norm = p.grad.detach().data.norm(2)
total_norm += param_norm.item() ** 2
return total_norm ** 0.5
@konverner
konverner / resnet_encoder.py
Created January 26, 2023 23:56
get embeddings from resnet with pytorch
import torch
from torchvision import models
modules=list(models.resnet18(weights='IMAGENET1K_V1').children())[:-1]
model = nn.Sequential(*modules)
x = torch.rand(size=[1, 3, 640, 480])
emb = model(x)
@konverner
konverner / img2tensor.py
Created January 26, 2023 23:53
convert image file into torch tensor
import numpy as np
import torch
from PIL import Image
def img2tensor(img_path: str):
"""
img_path : path to the image to convert
---
t : normalized tensor [1, c, h, w]
@konverner
konverner / imshow_tensor_batch.py
Created January 24, 2023 13:46
display images from batch of torch tensors
def imshow_tensor_batch(batch):
"""
batch: torch.tesnor [batch_size, c, h, w]
"""
batch_size = batch.shape[0]
fig, axis = plt.subplots(1, batch_size)
if batch_size == 1:
axis.imshow((batch[0] * 255).permute(2, 1, 0).permute(1, 0, 2).detach().cpu().numpy().astype(np.uint8))
else: