Skip to content

Instantly share code, notes, and snippets.

View DibyaranjanSathua's full-sized avatar

Dibyaranjan DibyaranjanSathua

View GitHub Profile
@DibyaranjanSathua
DibyaranjanSathua / medium.py
Created July 6, 2020 11:51
Scrapy Spider to scrape user information from Medium.com
# -*- coding: utf-8 -*-
import json
import scrapy
from scraper4.items import Scraper4Item
class MediumSpider(scrapy.Spider):
name = 'medium'
allowed_domains = ['medium.com']
@DibyaranjanSathua
DibyaranjanSathua / convert_tensorflow_model_to_opencv.py
Created February 28, 2019 13:28
Convert a tensorflow pre-trained model to work with opencv dnn module.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author: Dibyaranjan Sathua
Created on: 2019-02-28 12:26:54
"""
import tensorflow as tf
@DibyaranjanSathua
DibyaranjanSathua / test_opencv_dnn.py
Created February 28, 2019 12:32
Test opencv dnn module with tensorflow pre-trained model
import cv2
import numpy as np
def test_dnn_module():
""" Function to test OpenCV DNN module. """
model = 'removed_dropout_graph.pb'
net = cv2.dnn.readNet(model)
img = cv2.imread('F1_R0_A.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgGray = imgGray.astype(np.uint8)
@DibyaranjanSathua
DibyaranjanSathua / remove_dropout.py
Created February 28, 2019 11:42
Remove dropout block.
import tensorflow as tf
from tensorflow.core.framework import graph_pb2
import copy
def remove_dropout(input_graph, output_graph):
""" Remove the dropout block from the model. """
graph_def = load_graph(input_graph)
new_graph_def = graph_pb2.GraphDef()
for node in graph_def.node:
@DibyaranjanSathua
DibyaranjanSathua / optimize_batch_normalization.py
Created February 28, 2019 10:42
Optimize the batch normalization block by removing the training nodes.
import tensorflow as tf
from tensorflow.core.framework import graph_pb2
import copy
def optimize_batch_normalization(input_graph, output_graph):
""" Optimize the batch normalization block. """
graph_def = load_graph(input_graph) # Defined above
new_graph_def = graph_pb2.GraphDef()
unused_attrs = ['is_training'] # Attributes of FusedBatchNorm. Not needed during inference.
@DibyaranjanSathua
DibyaranjanSathua / convert_placeholders_to_constant.py
Created February 28, 2019 09:39
Convert the input placeholders to Constant
import tensorflow as tf
from tensorflow.core.framework import graph_pb2
import copy
def load_graph(filename):
""" Use to read the tensorflow protobuf graph file. """
graph_def = tf.GraphDef()
with tf.gfile.FastGFile(filename, 'rb') as f:
graph_def.ParseFromString(f.read())
return graph_def
@DibyaranjanSathua
DibyaranjanSathua / transform_graph.py
Created February 28, 2019 08:09
Run different transformation on tensorflow model
import tensorflow as tf
from tensorflow.tools.graph_transforms import TransformGraph
def transform_graph(input_graph, output_graph):
""" Use to run different transform function on the input graph and generate a output graph. """
with tf.gfile.GFile(input_graph, 'rb') as fid:
od_graph_def = tf.GraphDef()
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
od_graph_def = TransformGraph(od_graph_def, ['input_placeholder/input_image'], ['predicated_output'],