Skip to content

Instantly share code, notes, and snippets.

View Cospel's full-sized avatar

Michal Lukac Cospel

View GitHub Profile
@Cospel
Cospel / gist:a343e741608e48b367c74a4863f7b812
Created July 25, 2023 13:43
simple resnet18 and vgg like architectures in Group Equivariant CNN
# https://github.com/neel-dey/tf2-keras-gcnn
# https://github.com/neel-dey/tf2-GrouPy
import numpy as np
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Input, Activation, MaxPooling2D
from keras_gcnn.layers import GConv2D, GBatchNorm, GroupPool
@Cospel
Cospel / detect.py
Created September 29, 2020 09:51
detect on microcontroller
import pprint
from ximilar.client import DetectionClient
client = DetectionClient("__API_AUTH_TOKEN__")
results = client.detect_on_task([{"_file": "test/IMG_20181228_102636.jpg"}], "__TASK_ID__")
pprint.pprint(results)
@Cospel
Cospel / micro_upload.py
Created September 29, 2020 08:51
medium microcontroller blog post upload to ximilar.com
import os
import csv
from ximilar.client import DetectionClient
client = DetectionClient("__YOUR_API_TOKEN__")
# mapping of your ids
labels = {
@Cospel
Cospel / BatchRenormCallback.py
Created June 4, 2020 13:58
BatchRenormCallback.py
class BatchRenormCallback(tf.keras.callbacks.Callback):
def __init__(self, log_dir, bvalues):
super().__init__()
self.writer = tf.summary.create_file_writer(log_dir)
self.bvalues = bvalues
def on_epoch_begin(self, epoch, logs=None):
if epoch in self.bvalues:
self.change_renorm(epoch)
@Cospel
Cospel / tfaddonsbuild.txt
Last active May 21, 2020 10:36
tfaddonsbuild.txt
# cuda10, cuddn7.5, bazel 2.0, gcc7 through devtoolset-7, our custom tf2 build
0. # scl enable devtoolset-7 -- bash # for gcc on centos
1.
edit files build_deps/toolchains/* change 10.1 to 10.0
edit build_deps/toolchains/gcc7_manylinux2010-nvcc-cuda10.1/BUILD
- "/usr/local/cuda-10.1/targets/x86_64-linux/include",
- "/usr/local/cuda-10.1/include",
@Cospel
Cospel / finetune.py
Last active April 19, 2020 14:03
finetune.py
import tensorflow as tf
base_model = tf.keras.applications.MobileNetV2(
weights="imagenet", input_shape=self.shape, include_top=False
)
# Freeze the base_model
base_model.trainable = False
# Set the base model training=False so batch statistics is not updated
@Cospel
Cospel / cutout.py
Created March 26, 2020 12:46
cutout tensorflow 2
@tf.function
def cutout_channel(image, probability=0.5, iterations=3):
image = tf.cast(image, tf.float32)
r, g, b = tf.split(image, 3, axis=2)
# for cc in tf.range(iterations):
r = random_function(r, cutout_base, probability, None, **{"sl": 0.02, "sh": 0.08, "r1": 0.3})
g = random_function(g, cutout_base, probability, None, **{"sl": 0.02, "sh": 0.08, "r1": 0.3})
b = random_function(b, cutout_base, probability, None, **{"sl": 0.02, "sh": 0.08, "r1": 0.3})
@Cospel
Cospel / preprocess.py
Created March 16, 2020 14:54
preprocess vgg
@tf.function
def preprocess_caffe(x):
"""
Preprocessing for Keras (VGG, ResnetV1).
! This works only for channels_last
:param x: np.asarray([image, image, ...], dtype="float32") in RGB
:return: normalized image vgg style (BGR)
"""
batch, height, width, channels = x.shape
x = tf.cast(x, tf.float32)
@Cospel
Cospel / load_layer.py
Created February 23, 2020 10:27
load_layer.py
tf.keras.models.save_model(model, "model")
model_sm = tf.keras.models.load_model("model")
tf.keras.models.save_model(model, "model.h5")
model_h5 = tf.keras.models.load_model("model.h5", custom_objects={"PreprocessTFLayer": PreprocessTFLayer})
@Cospel
Cospel / batch_norm.py
Created January 26, 2020 07:57
batch_norm.py
import tensorflow as tf
class BatchNormalization(tf.keras.layers.BatchNormalization):
"""
Replace BatchNormalization layers with this new layer.
This layer has fixed momentum 0.9.
"""
def __init__(self, momentum=0.9, name=None, **kwargs):
super(BatchNormalization, self).__init__(momentum=0.9, name=name, **kwargs)