Skip to content

Instantly share code, notes, and snippets.

View CMCDragonkai's full-sized avatar
🚀
Lightspeed

CMCDragonkai

🚀
Lightspeed
View GitHub Profile
@CMCDragonkai
CMCDragonkai / batch_size_image_size.md
Created July 3, 2020 13:18
Batch size and Image size optimisation for CNNs

Batch size and Image size optimisation for CNNs

Given an initial size of an image. If we drop the square image size by side_p percentage points.

How much does the area percentage drop by?

The formula is as below:

area_p = [side_p * x^2 * (2 - side_p)] / 100
@CMCDragonkai
CMCDragonkai / roc_curves.md
Last active July 2, 2020 06:54
ROC Curves #python

ROC Curves

Receiver Operating Characteristic curves are useful in measuring the effectiveness of a classifier.

To calculate a ROC curve, you first need to prepare dataset and pass it through the classifier.

You will have these 2 vectors:

  1. truth vector with values of 0 (negative-class) and 1 (positive-class).
  2. score vector with values with "smaller" or "negative" values pointing to the negative-class and "larger" or "positive" values pointing to the positive-class
@CMCDragonkai
CMCDragonkai / load_image_array.py
Created June 24, 2020 09:16
Load RGB image into numpy array from a file stream #python
import io
import numpy as np
from PIL import Image
from typing import IO
def load_image(image_f: IO[bytes]) -> np.ndarray:
peek = image_f.peek(6)
if image_f.closed:
# if peek caused the underlying stream to be closed
@CMCDragonkai
CMCDragonkai / pycoco.py
Created June 15, 2020 01:36
Reading images from COCO #python
import zipfile
import numpy as np
from PIL import Image
from pycocotools import coco
# coco releases annotations and images in zip files
# you can keep the images in the zip archives because they allow random access
# however the annotations should be extracted and processed as is
c = coco.COCO("./annotations/instances_train2017.json")
@CMCDragonkai
CMCDragonkai / acquiring_nix_hash_large_file.md
Created June 11, 2020 07:10
Acquiring the Nix hash of a large file #nix

Acquiring the Nix hash of a large file

Normally you could use nix-prefetch-url. However it doesn't work when the files don't fit into memory.

For those cases, you should use:

wget https://url-to-large-file.com/file.zip
nix-hash --flat --base32 --type sha256 ./file.zip
@CMCDragonkai
CMCDragonkai / shannon_entropy.py
Last active June 10, 2020 03:54
Shannon Entropy implemented in Python #python
import numpy as np
# these functions reify shannon information and shannon entropy
# the results are in units of "bits" because we are using log with base 2
# prob has to have the [0.0, 1.0] range
# probs should be the array of all probabilities of all possible outcomes of a random variable
# it is assumed that the random variable is discrete
@CMCDragonkai
CMCDragonkai / file-s3-etag
Last active September 14, 2023 07:22
Calculate S3 ETAG including Multipart Uploads #python #aws
#!/usr/bin/env python3
import argparse
import hashlib
# see https://docs.aws.amazon.com/cli/latest/topic/s3-config.html
# for default multipart_threshold and multipart_chunksize
def md5sum(
file_like, multipart_threshold=8 * 1024 * 1024, multipart_chunksize=8 * 1024 * 1024
@CMCDragonkai
CMCDragonkai / README.md
Last active April 11, 2020 10:38
Self-Referential/Recursive Dictionary in Strict Languages #python #javascript

Self-Referential/Recursive Dictionary in Strict Languages

The problem with this in strict languages is that we keep needing lazy annotations.

And the other problem is that this won't be very fast in strict languages.

In strict languages there is probably no caching of the function calls.

What this means is comparing a recursive dictionary to a static dictionary, the recursive dictionary is going to be slower depending on how many function calls there are. In fact the slow down may be linear to the number of self or that function calls.

@CMCDragonkai
CMCDragonkai / git-common-descendant.sh
Last active April 9, 2020 08:38
Acquire the earliest common descendant for 2 Git commit revision hashes #git
#!/usr/bin/env bash
commit_1="$1"
commit_2="$2"
branch="$(git rev-parse --abbrev-ref HEAD)"
if git merge-base --is-ancestor "$commit_1" "$commit_2"; then
echo "$commit_2"
exit 0
fi
@CMCDragonkai
CMCDragonkai / singleton_list_syntax.md
Created March 25, 2020 10:50
Singleton List Syntax #python #haskell

Singleton List Syntax

Sometimes you want to have a singleton list or an empty list.

In Python:

[1] * (True) # [1]
[1] * (False) # []