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 torch | |
import torch.nn as nn | |
from torch.nn import init | |
class MaskedBatchNorm1d(nn.Module): | |
""" A masked version of nn.BatchNorm1d. Only tested for 3D inputs. | |
Args: | |
num_features: :math:`C` from an expected input of size |
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 | |
def MeanGradientError(outputs, targets, weight): | |
filter_x = tf.tile(tf.expand_dims(tf.constant([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype = outputs.dtype), axis = -1), [1, 1, outputs.shape[-1]) | |
filter_x = tf.tile(tf.expand_dims(filter_x, axis = -1), [1, 1, 1, outputs.shape[-1]]) | |
filter_y = tf.tile(tf.expand_dims(tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype = outputs.dtype), axis = -1), [1, 1, targets.shape[-1]]) | |
filter_y = tf.tile(tf.expand_dims(filter_y, axis = -1), [1, 1, 1, targets.shape[-1]]) | |
# output gradient | |
output_gradient_x = tf.math.square(tf.nn.conv2d(outputs, filter_x, strides = 1, padding = 'SAME')) |
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 | |
from tensorflow.keras.layers import Lambda, Dense, Input | |
from tensorflow.keras.models import Model | |
from tensorflow.keras import backend as K | |
def loss_fn(args): | |
return K.constant(1, dtype = 'float32')# creating model | |
inputs = Input(shape = (784,)) | |
dense1 = Dense(512, activation = 'relu')(inputs) | |
dense2 = Dense(128, activation = 'relu')(dense1) |
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
""" | |
linear_interpolate - function to bilinear interpolatation trasformation | |
Parameters: | |
origi_img I/P image input | |
scaled_img I/P scaled image input - 2d array of mapped pixels | |
h_ratio I/P scaling ratio for height | |
w_ratio I/P scaling ratio for width | |
O/P transformed image | |
""" | |
def linear_interpolate(origi_img, scaled_img, h_ratio, w_ratio): |
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
""" | |
nn - function nearest neigbor to map pixels of scaled images to the original images for image interpolation. By default, assume that scale around (0, 0) | |
Parameters: | |
orgi_img I/P original image input | |
scaled_img I/P scaled image input - 2d array of mapped pixels | |
h_ratio I/P scaling ratio for height | |
w_ratio I/P scaling ratio for width | |
O/P color-filled image | |
""" | |
def nn(origi_img, scaled_img, h_ratio, w_ratio): |
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
#test.py | |
#import dependencies | |
import cv2 | |
import os | |
import MoCV | |
""" | |
_segment_image_test - function to test optimal thresholding for image segmentation | |
Parameters: |
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
#segmentation.py | |
""" | |
In Computer Vision, segmentation objects and background could be done by different methods: | |
Intensity-based Segmentation: Thresholding - Based on the intensity of colors in histogram | |
Edge-based Segmentation - Based on edges | |
Region-based Segmentation | |
""" | |
#import dependencies | |
from . import histogram |
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
def upcontrastImage(image): | |
hist = histogram.histogram(image) | |
stretched_hist = histogram.eq_hist(hist, 256, image.size) | |
enhanced_image = np.zeros(shape = image.shape) | |
height = len(image) | |
width = len(image[0]) | |
for row in range(height): | |
for col in range(width): | |
enhanced_image[row, col] = stretched_hist[image[row, col]] |
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
def eq_hist(hist, colors, img_size): | |
hist = hist * colors / (2 * img_size) | |
cum_hist = hist.cumsum() #Cummulative histogram | |
equalized_hist = np.floor(cum_hist) | |
return equalized_hist |
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
def histogram(img_channel): | |
#hist, _ = np.histogram(im_channele, bins = 256, range = (0, 256)) | |
hist = np.array([0] * 256) | |
height = len(img_channel) | |
width = len(img_channel[0]) | |
for row in range(height): | |
for col in range(width): | |
hist[img_channel[row, col]] += 1 |
NewerOlder