Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@maweigert
maweigert / coin_flip.py
Created March 19, 2024 16:46
coin flipping
"""
Flip a fair coin 100 times—it gives a sequence of heads (H) and tails (T).
For each HH in the sequence of flips, Alice gets a point; for each HT, Bob does,
so e.g. for the sequence THHHT Alice gets 2 points and Bob gets 1 point.
Who is most likely to win?
https://x.com/littmath/status/1769044719034647001?s=20
# Makefile to create arxiv submission conforming single latex files. Created originaly by Uwe Schmidt
all: pdf
pdf: main.tex
pdflatex main.tex
# http://stackoverflow.com/a/21935690
-biber main
pdflatex main.tex
@maweigert
maweigert / environment.yaml
Last active November 19, 2021 11:24
EMBL Course environment
name: dl-image-mw
channels:
- defaults
- nvidia
dependencies:
- python=3.9
- cudatoolkit=11.0.*
- cudnn=8.0.*
- jupyter
- numpy
@maweigert
maweigert / set_batchnorm_momentum.py
Created November 17, 2021 14:55
fix tensorflow batchorm default momentum for mobilenet
"""
The default momentum of MobileNet (for running average calculation) in tf seems to be very high (0.99?), resulting in huge (and suprising) differences in train vs evaluate loss
https://github.com/keras-team/keras/issues/6977
Setting it to something sensible (e.g. 0.9) fixes this
https://stackoverflow.com/questions/65415799/fit-works-as-expected-but-then-during-evaluate-model-performs-at-chance/66692755#66692755
"""
@maweigert
maweigert / downsample_gpu.py
Created July 29, 2021 21:19
Average-Downsampling example in OpenCL (via gputools)
# Average-Downsampling example in OpenCL (via gputools)
# please install it first via
# pip install gputools
import numpy as np
from gputools import OCLProgram, OCLArray, get_device
from timeit import default_timer
from skimage.transform import downscale_local_mean
# opencl kernel
@maweigert
maweigert / stardist_memory.py
Created July 26, 2021 11:06
stardist_memory
import tensorflow as tf
import numpy as np
import psutil
import os
from stardist.models import Config3D, StarDist3D
os.environ['CUDA_VISIBLE_DEVICES']= "0"# '1,0'
from tensorflow.keras.utils import Sequence
import stardist
/* QuPath-Script to export annotations to label tif images (e.g. to be used for stardist)
Use "Run for project" to export annotations for all images within a QuPath project
Afterwards both images and mask tiffs will be stored in the project subfolder
ground_truth
├── images
└── masks
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import label, zoom
from stardist.matching import matching
from stardist.plot import render_label, render_label_pred
np.random.seed(42)
def create_example():
rand_gt = np.random.uniform(0,1,(8,8))
@maweigert
maweigert / label_image_from_rois.py
Last active October 22, 2021 01:55
Generate label image from rois in Fiji
#@ ImagePlus imp
#@ OUTPUT ImagePlus result
from ij import IJ
from ij.plugin.frame import RoiManager
result = IJ.createImage("Labeling", "16-bit black", imp.getWidth(), imp.getHeight(), 1)
ip = result.getProcessor()
rm = RoiManager.getInstance()
#!/usr/bin/env bash
source ~/.bashrc
git clone https://github.com/mpicbg-csbd/stardist
my_venvs_create stardist_env
my_venvs_activate stardist_env
pip install tifffile==2019.7.2 # as newer versions somehow wont install on noto
pip install stardist