Skip to content

Instantly share code, notes, and snippets.

View santhalakshminarayana's full-sized avatar

Santha Lakshmi Narayana santhalakshminarayana

View GitHub Profile
@santhalakshminarayana
santhalakshminarayana / pspnet_architecture.py
Created November 18, 2019 10:15
PSPNet architecture for semantic segmentation Medium
def conv_block(X,filters,block):
# resiudal block with dilated convolutions
# add skip connection at last after doing convoluion operation to input X
b = 'block_'+str(block)+'_'
f1,f2,f3 = filters
X_skip = X
# block_a
X = Convolution2D(filters=f1,kernel_size=(1,1),dilation_rate=(1,1),
padding='same',kernel_initializer='he_normal',name=b+'a')(X)
@santhalakshminarayana
santhalakshminarayana / white_board_enhance.py
Created October 18, 2021 22:06
White Board image color enhancement in python
import argparse
import cv2
import numpy as np
def normalize_kernel(kernel, k_width, k_height, scaling_factor = 1.0):
'''Zero-summing normalize kernel'''
K_EPS = 1.0e-12
# positive and negative sum of kernel values
pos_range, neg_range = 0, 0
@santhalakshminarayana
santhalakshminarayana / Quotes_LSTM_model.py
Last active November 4, 2020 20:05
Quotes LSTM model - Medium
def get_batches_x(tot_seq, batch_size):
ind = np.random.permutation(tot_seq).tolist()
i = 0
for i in range(0, tot_seq, batch_size):
batch_ids = ind[i:i+batch_size]
yield X[batch_ids], Y[batch_ids]
class Quote_Generator(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_len):
super(Quote_Generator, self).__init__()
@santhalakshminarayana
santhalakshminarayana / face_recognition_train_test_data.py
Last active July 7, 2020 12:11
Face recognition train and test data preparation
# Prepare Train Data
x_train=[]
y_train=[]
person_rep=dict()
person_folders=os.listdir(path+'/Images_crop/')
for i,person in enumerate(person_folders):
person_rep[i]=person
image_names=os.listdir('Images_crop/'+person+'/')
for image_name in image_names:
img=load_img(path+'/Images_crop/'+person+'/'+image_name,target_size=(224,224))
@santhalakshminarayana
santhalakshminarayana / Quotes_generation.py
Last active January 6, 2020 09:21
Quotes Generation - Medium
def predict(x, temp):
probs = F.softmax(x / temp, dim = 0)
probs = np.squeeze(probs.detach().cpu().numpy())
ind = np.random.choice(vocab_len, 1, p = probs)
return ind[0]
generated_text = ['there','is','no','one','love']
curr_len = 0
embeds = []
is_end = word_to_int[';']
window = 5 # max_seq_length
sequences, next_words = [], []
for quote in quotes:
words = quote.split(' ')
for i in range(0,len(words) - window + 1):
sequences.append(words[i:i+window])
if (i + window) < len(words):
next_words.append(words[i+window])
else:
next_words.append(';')
@santhalakshminarayana
santhalakshminarayana / Quotes_TSNE.py
Created January 6, 2020 07:20
Quotes TSNE - Medium
tsne = TSNE(n_components=2, perplexity=40, n_iter=300,metric='cosine')
n_points = 100
emb_tsne = tsne.fit_transform(emb[:n_points, :])
labels = list(word_to_int.keys())[:n_points]
x = emb_tsne[:,0]
y = emb_tsne[:,1]
plt.figure(figsize=(16, 16))
for i in range(n_points):
plt.scatter(x[i],y[i])
@santhalakshminarayana
santhalakshminarayana / Quotes_Glove_model.py
Last active January 6, 2020 07:18
Quotes Glove Model - Medium
def get_batch(batch_size):
ind = np.random.permutation(occs.size).tolist()
i = 0
for i in range(0, tot_pairs, batch_size):
batch_ids = ind[i:i+batch_size]
yield p1[batch_ids], p2[batch_ids], occs[batch_ids]
device = None
if torch.cuda.is_available():
device = torch.device("cuda:0")
@santhalakshminarayana
santhalakshminarayana / Quotes_Glove_train.py
Created January 6, 2020 07:00
Quotes Glove Train - Medium
def f_x(x, x_max, alpha):
x = (x/x_max)**alpha
return torch.min(x, torch.ones_like(x)).to(device)
def weight_mse(w_x, x, log_x):
loss = w_x * F.mse_loss(x, log_x, reduction='none')
return torch.mean(loss).to(device)
def glove_train(glove):
epochs = 100
tot_pairs = len(co_occ_matrix)
p1, p2,occs = list(), list(), list()
for i in co_occ_matrix.keys():
p1.append(i[0])
p2.append(i[1])
occs.append(co_occ_matrix[i])
p1 = np.array(p1)
p2 = np.array(p2)
occs = np.array(occs)