Skip to content

Instantly share code, notes, and snippets.

View Cospel's full-sized avatar

Michal Lukac Cospel

View GitHub Profile
@Cospel
Cospel / normalized_model.py
Created January 17, 2020 13:34
normalized_model.py
# create the base model
base_model = tf.keras.applications.MobileNetV2(include_top=False, weights="imagenet", input_shape=shape)
# apply normalization on input
inputs = tf.keras.Input(shape=shape, name="input")
x = PreprocessTFLayer()(inputs)
x = base_model(x)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
outputs = tf.keras.layers.Dense(2, activation="softmax", name="probs")(x)
@Cospel
Cospel / preprocess_layer.py
Created January 17, 2020 13:16
preprocess_layer.py
class PreprocessTFLayer(tf.keras.layers.Layer):
def __init__(self, name="preprocess_tf", **kwargs):
super(PreprocessTFLayer, self).__init__(name=name, **kwargs)
self.preprocess = preprocess_tf
def call(self, input):
return self.preprocess(input)
def get_config(self):
config = super(PreprocessTFLayer, self).get_config()
@Cospel
Cospel / preprocess_tensorflow.py
Created January 17, 2020 13:11
preprocess_tensorflow.py
@tf.function
def preprocess_tf(x):
"""
Preprocessing for Keras (MobileNetV2, ResNetV2).
:param x: np.asarray([image, image, ...], dtype="float32") in RGB
:return: normalized image tf style (RGB)
"""
batch, height, width, channels = x.shape
x = tf.cast(x, tf.float32)
import cv2
import numpy as np
# load image in cv2
img = cv2.imread('test_image.jpg')
# the image should be converted to grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply threshod
@Cospel
Cospel / antiaaliasing-tf2-keras.py
Last active June 18, 2020 18:36
attempt to create antiaaliasing cnn blurpool wrapper for existing keras application models - (tf2-keras)
import tensorflow as tf
import numpy as np
class BlurPool(tf.keras.layers.Layer):
"""
https://arxiv.org/abs/1904.11486
https://github.com/adobe/antialiased-cnns
https://github.com/adobe/antialiased-cnns/issues/10
"""
@Cospel
Cospel / confusion.html
Last active July 23, 2019 01:14
Confusion matrix in d3.js
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Confusion/Correlation Matrix</title>
<meta name="description" content="D3 Heatmap Demo">
<meta name="author" content="Marcos Iglesias">
<script src="https://d3js.org/d3.v5.min.js"></script>
<link rel="stylesheet" href="src/css/styles.css">
</head>
@Cospel
Cospel / redis-hash-ring-worker.py
Created April 24, 2018 09:40
redis-hash-ring-worker.py
from redis import Redis
from redis_hashring import RingNode
import gevent
import threading
import random
import signal
N_KEYS = 10
# we cannot use thread object
class Listener():#threading.Thread):
@Cospel
Cospel / tensorflow_rename_variables.py
Created April 13, 2018 12:06 — forked from batzner/tensorflow_rename_variables.py
Small python script to rename variables in a TensorFlow checkpoint
import sys, getopt
import tensorflow as tf
usage_str = 'python tensorflow_rename_variables.py --checkpoint_dir=path/to/dir/ ' \
'--replace_from=substr --replace_to=substr --add_prefix=abc --dry_run'
def rename(checkpoint_dir, replace_from, replace_to, add_prefix, dry_run):
checkpoint = tf.train.get_checkpoint_state(checkpoint_dir)

Keybase proof

I hereby claim:

  • I am cospel on github.
  • I am michallukac (https://keybase.io/michallukac) on keybase.
  • I have a public key ASDoorxH9WsFryZFoIKVQYAm99NyZs1rGub_EI5tpHfEWQo

To claim this, I am signing this object:

@Cospel
Cospel / rbm_after_refactor.py
Created April 3, 2016 13:46 — forked from gabrieleangeletti/rbm_after_refactor.py
Restricted Boltzmann Machine implementation in TensorFlow, before and after code refactoring. Blog post: http://www.gabrieleangeletti.com/blog/programming/2016/02/21/refactoring-rbm-tensor-flow-implementation.html
import tensorflow as tf
import numpy as np
import os
import zconfig
import utils
class RBM(object):