Skip to content

Instantly share code, notes, and snippets.

@qfgaohao
qfgaohao / rotate_elastic_ip.py
Created June 21, 2017 02:04
Rotate elastic ips for an EC2 Machine
client = boto3.client('ec2')
instance_id = 'my instance id'
def rotate_ip(last_eip=None):
if last_eip:
client.release_address(AllocationId=last_eip['AllocationId'])
eip = client.allocate_address(Domain='vpc')
r = client.associate_address(AllocationId=eip['AllocationId'], InstanceId=instance_id)
return eip
@qfgaohao
qfgaohao / pyramid.py
Created June 22, 2017 19:13
get a pyramid of an image and show multiple images in one notebook cell.
# modified from http://scikit-image.org/docs/stable/auto_examples/transform/plot_pyramid.html#sphx-glr-auto-examples-transform-plot-pyramid-py
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
from skimage.transform import pyramid_gaussian
image = data.astronaut()
@qfgaohao
qfgaohao / overlay.py
Last active June 23, 2017 01:41
Overlay a rectangle on an image
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from skimage import data
# get image
img = data.astronaut()
print('image size', img.shape)
fig, ax = plt.subplots()
ax.imshow(img)
@qfgaohao
qfgaohao / draw_rectangle_circle.py
Created June 23, 2017 01:44
Examples of Drawing rectangles and circles
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from skimage.draw import (line, polygon, circle,
circle_perimeter,
ellipse, ellipse_perimeter,
bezier_curve)
# Draw a rectangle
img = np.ones((10, 10), dtype=np.double) # a gray image
@qfgaohao
qfgaohao / priority_map.py
Created September 8, 2017 09:47
A structure combines priority queue and dict. You can pop out the biggest item from it like in queue. You can also set and get item like on a dict, while the order in the priority queue is maintained.
class Pair:
def __init__(self, key, value):
self.key = key
self.value = value
def __str__(self):
return "({}, {})".format(str(self.key), str(self.value))
def __repr__(self):
return self.__str__()
@qfgaohao
qfgaohao / device_properties.cu
Last active July 5, 2022 15:58
List GPU Specs. The code is modified from the Udacity Parallel Computing Course.
#include <stdio.h>
void deviceQuery ()
{
cudaDeviceProp prop;
int nDevices=0, i;
cudaError_t ierr;
ierr = cudaGetDeviceCount(&nDevices);
@qfgaohao
qfgaohao / bank_conflicts_test.cu
Last active October 17, 2017 04:13
It tests the time overhead of shared memory bank conflicts.
#include <stdio.h>
#define N (32)
__global__ void increment(int* time) {
__shared__ float s[1024];
for (int i = 0; i < 1024; i++) {
s[i] = 1.0f;
}
@qfgaohao
qfgaohao / timer.py
Created October 17, 2017 04:10
A simple Timer.
from datetime import datetime
import json
class Timer:
def __init__(self, outputFile):
self.fout = open(outputFile, 'w')
self.cache = []
self.records = {}
def start(self, event="time"):
@qfgaohao
qfgaohao / average_precision_score.py
Last active October 20, 2017 09:49
It computes average precision based on the definition of Pascal Competition. It computes the under curve area of precision and recall. Recall follows the normal definition. Precision is a variant. pascal_precision[i] = typical_precision[i:].max()
import numpy as np
def compute_average_precision(test_results, num_true_cases):
"""
It computes average precision based on the definition of Pascal Competition. It computes the under curve area
of precision and recall. Recall follows the normal definition. Precision is a variant.
pascal_precision[i] = typical_precision[i:].max()
Usage:
test_results = np.array([True, False, True, False, True, False, False, False, False, True])
@qfgaohao
qfgaohao / image_folder.py
Created October 27, 2017 09:19
Convert an ImageNet like dataset into tfRecord files, provide a method get_dataset to read the created files. It has similar functions as ImageFolder in Pytorch. Modified from https://raw.githubusercontent.com/tensorflow/models/master/research/slim/datasets/download_and_convert_flowers.py https://github.com/tensorflow/models/blob/master/research…
r"""Convert an ImageNet like dataset into tfRecord files, provide a method get_dataset to read the created files.
It has similar functions as ImageFolder in Pytorch.
Modified from
https://raw.githubusercontent.com/tensorflow/models/master/research/slim/datasets/download_and_convert_flowers.py
https://github.com/tensorflow/models/blob/master/research/slim/datasets/flowers.py
"""
from __future__ import absolute_import