Skip to content

Instantly share code, notes, and snippets.

View maxrohleder's full-sized avatar
🏠
Working from home

Maximilian Rohleder maxrohleder

🏠
Working from home
View GitHub Profile
@maxrohleder
maxrohleder / basic-indexing.py
Last active July 23, 2021 06:58
smart indexing can save you a lot of looping
a = np.array([[0, 1, 2],
[3, 4, 5]])
b = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# subsetting of the array (start is included, end is excluded)
print(a[0:2, 0:2])
#> [[0 1]
#> [3 4]]
# striding / stepping "every second entry"
@maxrohleder
maxrohleder / simplify-loops.py
Created November 28, 2020 13:37
use array enumeration to simplify for loops
import numpy as np
img = np.arange(6).reshape(2, 3)
# looping over all elements like this ...
for x in range(img.shape[0]):
for y in range(img.shape[1]):
print(img[x, y])
# ... is easier and more readable with numpy
@maxrohleder
maxrohleder / advanced-indexing.py
Created November 28, 2020 14:38
advanced indexing lets you manipulate arrays elegantly
import numpy as np
# some sample data in shaped cubic (100, 100, 100)
img = np.random.sample((100, 100, 100))
# set all values between 0.5 and 0.6 to zero
img[(img < 0.6) & (img > 0.5)] = 0
# in a small sub cube (10, 10, 10)..
# .. get index arrays of all elements above 0.6
@maxrohleder
maxrohleder / ditch-for-loops.py
Last active March 11, 2021 08:56
Square signal without for loops
import numpy as np
# first define our input values and all values for k
t = np.linspace(0, 1, 200)
k = np.arange(0, 1000)
f = 10 # frequency in Hertz
# from the formula, isolate all factors in the sinus term which include k
k = 2 * np.pi * (2 * k - 1) * f
@maxrohleder
maxrohleder / keras-sequential.py
Last active February 7, 2021 22:28
Three different approaches to design a model in keras.
from tensorflow.keras import layers, models
# define the model
model = models.Sequential(name="Keras Test CNN")
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
@maxrohleder
maxrohleder / keras-functional.py
Created February 7, 2021 18:18
this toy-resnet demonstrates the functional api of keras.
# taken from https://www.tensorflow.org/guide/keras/functional#a_toy_resnet_model
inputs = keras.Input(shape=(32, 32, 3), name="img")
x = layers.Conv2D(32, 3, activation="relu")(inputs)
x = layers.Conv2D(64, 3, activation="relu")(x)
block_1_output = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(block_1_output)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_2_output = layers.add([x, block_1_output])
# Adapted from https://www.tensorflow.org/overview
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# 1. Define a model
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
@maxrohleder
maxrohleder / tf_slicing.py
Created June 21, 2021 09:43
tensorflow slice assignment
# we have 1 batch of a stack of size 2 of images shape (3, 4)
test = tf.constant(np.arange(24).reshape(1, 2, 3, 4), dtype=tf.int64)
# <tf.Tensor: shape=(1, 2, 3, 4), dtype=int64, numpy=
# array([[[[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]],
# [[12, 13, 14, 15],
# [16, 17, 18, 19],
# [20, 21, 22, 23]]]])>
@maxrohleder
maxrohleder / DiceBCELoss.py
Last active June 14, 2023 13:54
losses for biomedical image segmentation
import tensorflow as tf
class DiceBCELoss(tf.keras.losses.Loss):
def __init__(self, roi, smooth=1e-6, eps=1e-8, name='DiceBCE'):
""" A more stable surrogate for the dice metric
Sources:
[1] https://github.com/Project-MONAI/MONAI/issues/807
[2] https://www.kaggle.com/bigironsphere/loss-function-library-keras-pytorch
Args:
roi: bool mask same size as y_pred and y_true
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.