Skip to content

Instantly share code, notes, and snippets.

# winpty fix
alias python='winpty python.exe'
alias pip='winpty pip'
# custom aliases
alias rt='reset'
alias ll='ls -la'
alias l='ls -1'
alias la='ls -a'
@KornelDylski
KornelDylski / fetch-image.ts
Created October 1, 2021 17:01
Fetch with async vs fetch with then
async function getImages(numbers) {
// fetch all at once, return fetch promises
const allResult = await Promise.all(
numbers.map((num) => fetch("url/to/img" + num))
);
// iterate and return array of json promises
return Promise.all(
allResult.map((result) => {
if (result.ok) {
def accuracy(input, target, thresh=0.4, na_idx=0):
valm, argm = input.max(-1)
# results below threshold are considered as "na" category
argm[valm < thresh] = na_idx
return (argm==target).float().mean()
# apply sigmoid and one-hot
input = input.sigmoid()
target = F.one_hot(target, input.shape[1]).float()
# change all target category “na” (placed at 0 index) to zero
target[:, 0] = 0
# finally count bce loss
loss = F.binary_cross_entropy(input, target)
# borrowed from https://github.com/clcarwin/focal_loss_pytorch/blob/master/focalloss.py
# added "reduction" param for fastai
import torch
import torch.nn as nn
import torch.nn.functional as F
class FocalLoss(nn.Module):
def __init__(self, gamma=0.0, alpha=None, reduction='mean'):
super(FocalLoss, self).__init__()
@KornelDylski
KornelDylski / download_google_images.py
Last active April 29, 2019 16:35
Download google images using keywords and labels from csv
import numpy as np
import pandas as pd
import cv2
import os
import uuid
import argparse
from pathlib import Path
from google_images_download.google_images_download import googleimagesdownload