Skip to content

Instantly share code, notes, and snippets.

View deepak112's full-sized avatar

Deepak Birla deepak112

  • Mindtree
  • Banglore Karnataka
View GitHub Profile
@deepak112
deepak112 / images.py
Last active November 8, 2018 06:48
Below functions returns High Resolution and Low Resolution images in form of numpy array from given list of images(List comprising of images in form of numpy array).
from numpy import array
from scipy.misc import imresize
# Takes list of images and provide HR images in form of numpy array
def hr_images(images):
images_hr = array(images)
return images_hr
# Takes list of images and provide LR images in form of numpy array
def lr_images(images_real , downscale):
images = []
@deepak112
deepak112 / generator.py
Last active November 8, 2018 06:49
Generator Network for SRGAN
from keras.layers.core import Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import UpSampling2D
from keras.layers import Input
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.models import Model
from keras.layers.advanced_activations import LeakyReLU, PReLU
from keras.layers import add
# Residual block
@deepak112
deepak112 / discriminator.py
Last active November 7, 2018 11:06
Discriminator Network for SRGAN
from keras.layers import Dense
from keras.layers.core import Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.core import Flatten
from keras.layers import Input
from keras.layers.convolutional import Conv2D
from keras.models import Model
from keras.layers.advanced_activations import LeakyReLU
def discriminator_block(model, filters, kernel_size, strides):
@deepak112
deepak112 / vgg.py
Last active November 8, 2018 06:40
VGG or Content loss function
from keras.applications.vgg19 import VGG19
import keras.backend as K
from keras.models import Model
def vgg_loss(self, y_true, y_pred):
vgg19 = VGG19(include_top=False, weights='imagenet', input_shape=self.image_shape)
vgg19.trainable = False
for l in vgg19.layers:
l.trainable = False
model = Model(inputs=vgg19.input, outputs=vgg19.get_layer('block5_conv4').output)
@deepak112
deepak112 / gan.py
Created November 8, 2018 06:46
Generator and discriminator combined (GAN)
from keras.models import Model
from keras.layers import Input
def get_gan_network(discriminator, shape, generator, optimizer, vgg_loss):
discriminator.trainable = False
gan_input = Input(shape=shape)
x = generator(gan_input)
gan_output = discriminator(x)
gan = Model(inputs=gan_input, outputs=[x,gan_output])
gan.compile(loss=[vgg_loss, "binary_crossentropy"], loss_weights=[1., 1e-3], optimizer=optimizer)
@deepak112
deepak112 / optimizer.py
Created November 8, 2018 06:51
Adam optimizer
def get_optimizer():
adam = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
return adam
@deepak112
deepak112 / train.py
Last active November 8, 2018 07:21
Training SRGAN
from tqdm import tqdm
import numpy as np
# Better to use downscale factor as 4
downscale_factor = 4
# Remember to change image shape if you are having different size of images
image_shape = (384,384,3)
def train(epochs, batch_size, input_dir, output_dir, model_save_dir, number_of_images, train_test_ratio):
# Loads training and test data
@deepak112
deepak112 / getMousePointsOpenCV.py
Last active May 30, 2020 14:24
Get points from mouse click event CV2 python.
import cv2
cv2.namedWindow("image")
cv2.setMouseCallback("image", get_mouse_points)
def get_mouse_points(event, x, y, flags, param):
global mouse_pts
if event == cv2.EVENT_LBUTTONDOWN:
if len(mouse_pts) < 4:
cv2.circle(image, (x, y), 5, (0, 0, 255), 10)
@deepak112
deepak112 / yolov3PersonDetection.py
Last active May 30, 2020 14:28
Detects person in video and get bounding box around person
import cv2
import numpy as np
weightsPath = "path to yolov3.weights"
configPath = "path to yolov3.cfg"
vid_path = "path to video"
net_yl = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net_yl.getLayerNames()
ln1 = [ln[i[0] - 1] for i in net_yl.getUnconnectedOutLayers()]
import cv2
import numpy as np
(H, W) = frame.shape[:2]
points = mouse_pts
# Using first 4 points or coordinates for perspective transformation. The region marked by these 4 points are
# considered ROI. This polygon shaped ROI is then warped into a rectangle which becomes the bird eye view.
# This bird eye view then has the property property that points are distributed uniformally horizontally and
# vertically(scale for horizontal and vertical direction will be different). So for bird eye view points are