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
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)) |
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
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 |
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
pd.options.display.float_format = '{:,.1f}%'.format | |
print((df.isna().sum()/df.count()).sort_values()) |
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
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 |
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
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) |
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 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] |
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_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 |
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 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) |
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 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] |
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 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: |