Skip to content

Instantly share code, notes, and snippets.

View quocdat32461997's full-sized avatar
🎯
Focusing

Dat Ngo quocdat32461997

🎯
Focusing
View GitHub Profile
@quocdat32461997
quocdat32461997 / genres.txt
Created November 11, 2019 05:17 — forked from sampsyo/genres.txt
music genre list scraper
2 tone
2-step garage
4-beat
4x4 garage
8-bit
acapella
acid
acid breaks
acid house
acid jazz
@quocdat32461997
quocdat32461997 / histogram.py
Last active January 27, 2020 08:09
Compute image histogram
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
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
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]]
#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
#test.py
#import dependencies
import cv2
import os
import MoCV
"""
_segment_image_test - function to test optimal thresholding for image segmentation
Parameters:
"""
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):
"""
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):
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)
@quocdat32461997
quocdat32461997 / MeanGradientError.py
Last active October 29, 2022 02:54
Trivial example for Mixed-Gradient-Error and Mean-Gradient-Error
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'))