This file contains 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 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. | |
) |
This file contains 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
def binary_search_iterative(array, element): | |
mid = 0 | |
start = 0 | |
end = len(array) | |
step = 0 | |
while (start <= end): | |
# print('Subarray in step {}: {}'.format(step, str(array[start:end+1]))) | |
step = step + 1 | |
mid = (start + end) // 2 |
This file contains 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 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([]) |
This file contains 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 | |
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) | |
This file contains 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
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 |
This file contains 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
# 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) |
This file contains 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 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') |
This file contains 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 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') |
This file contains 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
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) |
This file contains 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
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] |
NewerOlder