Skip to content

Instantly share code, notes, and snippets.

@danyashorokh
danyashorokh / [Python] Plot Mosaic
Created September 25, 2024 13:47
[Python] Plot Mosaic
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
def plot_mosaic(images, titles, n_rows=2):
fig = plt.figure(figsize=(16, 16.))
grid = ImageGrid(fig, 111,
nrows_ncols=(n_rows, len(images) // n_rows), # creates 2x2 grid of axes
axes_pad=0.3, # pad between axes in inch.
)
@danyashorokh
danyashorokh / [Python] Binary search
Created November 6, 2022 16:38
[Python] Binary search
@danyashorokh
danyashorokh / [Python] Plot multiple images with titles
Last active May 4, 2022 20:02
[Python] Plot multiple images with titles
import matplotlib.pyplot as plt
def visualize(**images):
'Plot images'
n = len(images)
plt.figure(figsize=(16, 5))
for i, (name, image) in enumerate(images.items()):
plt.subplot(1, n, i + 1)
plt.xticks([])
@danyashorokh
danyashorokh / [KERAS] U-net
Created October 4, 2021 13:40
[KERAS] U-net
import tensorflow as tf
def conv2d_block(input_tensor, n_filters, kernel_size=3):
x = input_tensor
for i in range(2):
x = tf.keras.layers.Conv2D(filters=n_filters,
kernel_size=(kernel_size, kernel_size))(x)
@danyashorokh
danyashorokh / [KERAS] Feature extractors
Last active July 16, 2021 14:39
[KERAS] Feature extractors
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16, preprocess_input as preprocess_input_vgg
from keras.applications.inception_v3 import InceptionV3, preprocess_input as preprocess_input_inception
from keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input as preprocess_input_mobilenet
from keras.applications.resnet import ResNet50, preprocess_input as preprocess_input_resnet
from keras.models import Model, load_model
from keras.layers import Input, GlobalAveragePooling2D, GlobalMaxPooling2D
import numpy as np
import cv2
@danyashorokh
danyashorokh / [TF-KERAS] Simple object localization
Last active July 1, 2021 22:26
[TF-KERAS] Simple object localization
# Detect hardware
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
except ValueError:
tpu = None
gpus = tf.config.experimental.list_logical_devices("GPU")
# Select appropriate distribution strategy
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
@danyashorokh
danyashorokh / [KERAS] CustomImageDataGenerator images and masks
Last active October 9, 2020 13:54
[KERAS] CustomImageDataGenerator images and masks
import numpy as np
from keras.utils import Sequence
import imgaug as ia
import imgaug.augmenters as iaa
from imgaug.augmentables.segmaps import SegmentationMapsOnImage
ia.seed(1)
train_imgs = np.load('train_imgs.npy')
train_masks = np.load('train_masks.npy')
@danyashorokh
danyashorokh / [KERAS] ImageDataGenerator images and masks
Last active October 9, 2020 13:55
[KERAS] ImageDataGenerator images and masks
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
import imgaug as ia
import imgaug.augmenters as iaa
ia.seed(1)
train_imgs = np.load('train_imgs.npy')
train_masks = np.load('train_masks.npy')
val_imgs = np.load('val_imgs.npy')
@danyashorokh
danyashorokh / [KERAS] Multi gpu
Created August 20, 2020 21:40
[KERAS] Multi gpu
from keras.utils import multi_gpu_model
from keras import backend as K
print(K.tensorflow_backend._get_available_gpus())
n_gpus = 2
model = get_some_model()
gpu_model = multi_gpu_model(model, gpus=n_gpus)
@danyashorokh
danyashorokh / [CV] Apply alpha mask
Created November 15, 2019 06:59
[CV] Apply alpha mask
def mask_overlay(image, mask, color=(0, 255, 0)):
"""
Helper function to visualize mask on the top of the object
"""
mask = np.dstack((mask, mask, mask)) * np.array(color)
mask = mask.astype(np.uint8)
weighted_sum = cv2.addWeighted(mask, 0.5, image, 0.5, 0.)
img = image.copy()
ind = mask[:, :, 1] > 0
img[ind] = weighted_sum[ind]