Skip to content

Instantly share code, notes, and snippets.

View innat's full-sized avatar
:octocat:
Working from home

Mohammed Innat innat

:octocat:
Working from home
View GitHub Profile
import tensorflow as tf
from tensorflow import keras
class ColorJitter(keras.layers.Layer):
def __init__(
self,
brightness_factor=0.5,
contrast_factor=(0.5, 0.9),
saturation_factor=(0.5, 0.9),
hue_factor=0.5,
import tensorflow as tf
from tensorflow import keras
class ChannelShuffle(keras.layers.Layer):
def __init__(self, groups=3, seed=None, **kwargs):
super().__init__(**kwargs)
self.groups = groups
self.seed = seed
def _channel_shuffling(self, images):
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@innat
innat / tf_clahe.py
Created February 21, 2023 13:07
CLAHE implemented in TensorFlow 2
```
# original repo: https://github.com/isears/tf_clahe
# check https://github.com/isears/tf_clahe/issues/1#issuecomment-1356676495
!pip install -q --no-deps /kaggle/input/tfpackages/tf_clahe-0.1.0-py3-none-any.whl
```
import tf_clahe
class CLAHE(keras.layers.Layer):
def __init__(
@innat
innat / WeightedBinaryCrossEntropy.py
Last active April 3, 2023 07:50
Weighted BinaryCrossEntropy Loss in Keras
import tensorflow as tf
def weighted_binary_loss(weight, from_logits=True, reduction="mean"):
def inverse_sigmoid(sigmoidal):
return - tf.math.log(1. / sigmoidal - 1.)
def weighted_loss(labels, predictions):
predictions = tf.convert_to_tensor(predictions)
labels = tf.cast(labels, predictions.dtype)
num_samples = tf.cast(tf.shape(labels)[-1], dtype=labels.dtype)
# Base Image: Ubuntu + Cuda
FROM nvidia/cuda:11.0.3-devel-ubuntu20.04 AS python_base_cuda
# ENV SET
ENV PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1 \
PYTHONHASHSEED=random \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
"""
Reference
# https://github.com/fuenwang/PanoramaUtility
# https://github.com/fuenwang/Equirec2Perspec
# https://github.com/fuenwang/PanoramaUtility
# https://github.com/timy90022/Perspective-and-Equirectangular
"""
import os
import sys
# RESTRICT TENSORFLOW TO 2GB OF GPU RAM
# SO THAT WE HAVE 14GB RAM free
LIMIT = 2.0
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
tf.config.experimental.set_virtual_device_configuration(
gpus[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024*LIMIT)])
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
class WarmupLearningRateSchedule(optimizers.schedules.LearningRateSchedule):
"""WarmupLearningRateSchedule a variety of learning rate
decay schedules with warm up."""
def __init__(
self,
initial_lr,
steps_per_epoch=None,
lr_decay_type="exponential",
decay_factor=0.97,