Skip to content

Instantly share code, notes, and snippets.

View hagerty's full-sized avatar

Patrick Hagerty hagerty

View GitHub Profile
@hagerty
hagerty / CosmiQNet.py
Last active March 20, 2023 10:29
Fully convolutional neural network for classifying building from 3-band and 8-band imagery
#Generator
with tf.device(gpu):
x8 = tf.placeholder(tf.float32, shape=[None, FLAGS.ws, FLAGS.ws, 8]) # 8-band input
x3 = tf.placeholder(tf.float32, shape=[None, scale * FLAGS.ws, scale * FLAGS.ws, 3]) # 3-band ipnput
label_distance = tf.placeholder(tf.float32, shape=[None, FLAGS.ws, FLAGS.ws, 1]) # distance transform as a label
for i in range(layers):
alpha[i] = tf.Variable(0.9, name='alpha_' + str(i))
beta[i] = tf.maximum( 0.0 , tf.minimum ( 1.0 , alpha[i] ), name='beta_'+str(i))
bi[i] = tf.Variable(tf.constant(0.0,shape=[FLAGS.filters]), name='bi_'+str(i))
@hagerty
hagerty / distancexform.py
Created September 14, 2016 18:01
Distance transform from geoJSON to a bumpy array
import json
from shapely.geometry import Polygon, shape, Point
import gdal
import numpy as np
import sys
fn = sys.argv[1]
path = sys.argv[2]
def Pixel2World ( geoMatrix, i , j ):
ulX = geoMatrix[0]
@hagerty
hagerty / TrainCosmiQNet.py
Last active October 14, 2017 10:49
Training CosmiQNet using Tensorflow
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
sess.run(ginit)
global_step = 0
for i in range(layers):
for step in range(FLAGS.max_layer_steps):
global_step += 1
random_input8, random_input3, random_label = GetRandomInput( labels, im_raw8, im_raw3, FLAGS.batch_size, FLAGS.ws, scale)
sess.run(label_optimizer[i],feed_dict={x8: random_input8, x3: random_input3, label_distance: random_label})
for step in range(5*FLAGS.max_layer_steps):
global_step += 1
@hagerty
hagerty / geoJSONfromCluster.py
Last active September 21, 2016 19:15
Create geoJSON from cluster array
def CreateGeoJSON ( fn, cluster, geom, proj ):
memdrv = gdal.GetDriverByName ( 'MEM')
src_ds = memdrv.Create('',cluster.shape[1],cluster.shape[0],1)
src_ds.SetGeoTransform(geom)
src_ds.SetProjection(proj)
band = src_ds.GetRasterBand(1)
band.WriteArray(cluster)
dst_layername = "BuildingID"
drv = ogr.GetDriverByName("geojson")
@hagerty
hagerty / CosmiQNet.Alternative.py
Last active February 27, 2017 14:12
Alternative architecture for CosmiQNet
with tf.device(gpu):
# Generator
x8 = tf.placeholder(tf.float32, shape=[None, FLAGS.ws, FLAGS.ws, 8])
x3 = tf.placeholder(tf.float32, shape=[None, scale * FLAGS.ws, scale * FLAGS.ws, 3])
label_distance = tf.placeholder(tf.float32, shape=[None, FLAGS.ws, FLAGS.ws, 1])
for i in range(layers):
alpha[i] = tf.Variable(0.9, name='alpha_' + str(i))
beta[i] = tf.maximum( 0.0 , tf.minimum ( 1.0 , alpha[i] ), name='beta_'+str(i))
bi[i] = tf.Variable(tf.constant(0.0,shape=[FLAGS.filters]), name='bi_'+str(i))
bo[i] = tf.Variable(tf.constant(0.0,shape=[FLAGS.filters]), name='bo_'+str(i))
@hagerty
hagerty / GreedyClustering.py
Created September 21, 2016 19:13
Greedy clustering algorithm. No checks on simply connected are implemented. Probably could merge/eliminate really small clusters but I don't.
def GrowCluster ( intensity, cluster, mask, k ):
newcluster = cluster
#find boundary of current cluster
indexes = np.nonzero( np.logical_and(mask, (np.logical_not(newcluster))))
while indexes :
growingboundary = 0 * cluster
for kk in range ( len (indexes[0] ) ):
i = indexes[0][kk]
j = indexes[1][kk]
isConnected = False
@hagerty
hagerty / FixGeoJSON.py
Created September 23, 2016 19:34
Remove erroneous polygons from geoJSON
def FixGeoJSON( fn ):
buf_dist = 0.0
dst_layername = "BuildingID"
drv = ogr.GetDriverByName("geojson")
dst_ds = drv.Open ( './geojson.full/' + fn + dst_layername + ".geojson")
dst_layer = dst_ds.GetLayer(0)
if os.path.exists('./geojson.full/buffer' + fn + dst_layername + ".geojson"):
drv.DeleteDataSource('./geojson.full/buffer' + fn + dst_layername + ".geojson")
adst_ds = drv.CreateDataSource ( './geojson.full/buffer' + fn + dst_layername + ".geojson")
@hagerty
hagerty / LICENSE.md
Last active September 27, 2016 12:47
LICENSE.md

Copyright (c) 2016 In-Q-Tel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHE

@hagerty
hagerty / CosmiQNet.training.py
Created March 3, 2017 16:24
Network Architecture for CosmiQNet
# The NN
with tf.device(gpu):
# Input is has numberOfBands for the pre-processed image and numberOfBands for the original image
xy = tf.placeholder(tf.float32, shape=[None, FLAGS.ws, FLAGS.ws, 2*numberOfBands])
with tf.name_scope("split") as scope:
x = tf.slice(xy, [0,0,0,0], [-1,-1,-1,numberOfBands]) # low res image
y = tf.slice(xy, [0,0,0,numberOfBands], [-1,-1,-1,-1]) # high res image
with tf.name_scope("initial_costs") as scope:
# used as a measure of improvement not for optimization