Skip to content

Instantly share code, notes, and snippets.

@antoinebrl
antoinebrl / mnist.py
Created November 30, 2023 22:26
MNIST MLP - 97% accuracy
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda, Compose
transform = Compose([ToTensor(), Lambda(lambda x: torch.flatten(x))])
train_dataset = datasets.MNIST(root='data', train=True, transform=transform, download=True)
test_dataset = datasets.MNIST(root='data', train=False, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=250, shuffle=True, drop_last=True)
@antoinebrl
antoinebrl / onnx-surgery.py
Last active November 21, 2022 14:40
ONNX surgery - Add input normalization
import timm
import torchvision
import torch
import urllib
from PIL import Image
from torch import nn
import onnx
import onnx.helper as oh
from onnx.onnx_pb import ValueInfoProto
import numpy as np
@antoinebrl
antoinebrl / image_normalization.py
Last active June 23, 2023 16:36
Rounding error when normalizing color intensity with float16
# Images have color pixels encoded as uint8. In computer
# vision and deep learning the images is represented with
# floatingpoints with value in the range [0,1]. This is
# obtained by dividing by 255, the highest color intensity.
# However, reduced precision training an inference become
# the new standard.
#
# What is the magnitude of the rouding error when normalizing
# an image by 255 rather than 256 (256=2^8)?
@antoinebrl
antoinebrl / knn-vectorized.py
Last active November 22, 2022 19:43
Vectorized K-nearest neighbors (no for-loop)
import numpy as np
from scipy.stats import mode
def euclidean_distance(x1, x2):
# x1 array of shape (N, D)
# x2 array of shape (N', D)
# output pairwise distances, array of shape (N, N')
return np.linalg.norm(x1[:, np.newaxis] - x2, axis=1)
@antoinebrl
antoinebrl / file-permissions.sh
Created May 19, 2022 09:07
Reset file permissions
find . ! -user root -exec chown root:root {} \;
find . -type f ! -perm 644 -exec chmod 644 {} \;
find . -type d ! -perm 755 -exec chmod 755 {} \;
@antoinebrl
antoinebrl / onnx-surgery.py
Last active May 18, 2022 13:00
ONNX Surgery example - Replacing `GlobalAveragePool` with `AveragePool`
import onnx
model = onnx.load("model.onnx")
graph = model.graph
# Identify relevant node
idx = -1
for i, n in enumerate(model.graph.node):
if n.name == "GlobalAveragePool":
idx = i
@antoinebrl
antoinebrl / README.md
Last active April 21, 2024 08:59
Prepare ImageNet

Preparation of ImageNet (ILSVRC2012)

The dataset can be found on the official website if you are affiliated with a research organization. It is also available on Academic torrents.

This script extracts all the images and group them so that folders contain images that belong to the same class.

  1. Download the ILSVRC2012_img_train.tar and ILSVRC2012_img_val.tar
  2. Download the script wget https://gist.githubusercontent.com/antoinebrl/7d00d5cb6c95ef194c737392ef7e476a/raw/dc53ad5fcb69dcde2b3e0b9d6f8f99d000ead696/prepare.sh
  3. Run it ./prepare.sh
@antoinebrl
antoinebrl / GoogleXSSChallenge-Solutions.md
Last active September 19, 2022 04:22
Google XSS Challenge - Solutions