This file contains hidden or 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
# 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 |
This file contains hidden or 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 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) |
This file contains hidden or 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 os | |
import csv | |
from ximilar.client import DetectionClient | |
client = DetectionClient("__YOUR_API_TOKEN__") | |
# mapping of your ids | |
labels = { |
This file contains hidden or 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
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) |
This file contains hidden or 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
# 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", |
This file contains hidden or 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 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 |
This file contains hidden or 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
@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}) |
This file contains hidden or 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
@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) |
This file contains hidden or 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
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}) |
This file contains hidden or 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 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) |
NewerOlder