This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]]]])> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from skimage import measure | |
from stl import mesh | |
def to_stl(fname, vol): | |
assert np.alltrue(np.isin(vol, [1, 0])), "volume must be binary" | |
verts, faces, _, _ = measure.marching_cubes_lewiner(vol) | |
m = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype)) | |
for i, f in enumerate(faces): | |
for j in range(3): | |
m.vectors[i][j] = verts[f[j], :] |
OlderNewer