Skip to content

Instantly share code, notes, and snippets.

View syaffers's full-sized avatar

Syafiq Kamarul Azman syaffers

View GitHub Profile
@syaffers
syaffers / mapred_site.xml
Created April 7, 2021 09:02
Example Mapreduce site configuration for COSC class
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>yarn.app.mapreduce.am.env</name>
<value>HADOOP_MAPRED_HOME=/opt/hadoop-3.2.2</value>
</property>
<property>
@syaffers
syaffers / yarn-site.xml
Last active April 7, 2021 08:56
Example YARN site configuration for COSC class
<configuration>
<property>
<name>yarn.resourcemanager.hostname</name>
<value>your_hostname</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
@syaffers
syaffers / whole_palette.py
Created December 4, 2020 10:10
Modified recolor function with a better color distance
class EntirePalette:
# ... omitted
def recolor(self, image):
image_shape = image.shape
image = self._preprocess(image)
image_colors = np.unique(image, axis=0)
self_da = da.from_array(
self.source_pixels.astype(np.long), chunks=(self.chunk_size, 3)
@syaffers
syaffers / color_distance.c++
Created December 4, 2020 10:08
Color distance c++ code from compuphase
typedef struct {
unsigned char r, g, b;
} RGB;
double ColourDistance(RGB e1, RGB e2)
{
long rmean = ( (long)e1.r + (long)e2.r ) / 2;
long r = (long)e1.r - (long)e2.r;
long g = (long)e1.g - (long)e2.g;
long b = (long)e1.b - (long)e2.b;
@syaffers
syaffers / notebook_cell.py
Created December 4, 2020 10:04
Displaying computaitonal burdens using dask in jupyter notebooks
src = np.array(imread('carles-rabada-DD1fSz2HF1s-unsplash-small.png'))[:, :, :3]
trn = np.array(imread('olia-gozha-9A_peGrSbZc-unsplash-small.png'))[:, :, :3]
src = np.unique(src.reshape(-1, 3), axis=0)
trn = np.unique(trn.reshape(-1, 3), axis=0)
self_da = da.from_array(src.astype(np.long), chunks=(1024, 3))
other_da = da.from_array(trn.reshape(-1, 1, 3).astype(np.long),
chunks=(1024, 1, 3))
rgb_da = self_da - other_da
display(self_da)
display(other_da)
@syaffers
syaffers / whole_palette.py
Created December 4, 2020 10:02
Entire palette transfer class
class EntirePalette:
def __init__(self, chunk_size=1024):
self.chunk_size = chunk_size
self.source_pixels = None
def _preprocess(self, image):
# ... omitted, same as KMeansReducedPalette
def fit(self, image):
self.source_pixels = np.unique(self._preprocess(image), axis=0)
@syaffers
syaffers / reduced_palette.py
Created December 4, 2020 09:59
Random walk that traverses neighbors in a cluster
class KMeansReducedPalette:
# ... omitted
def random_neighborhood_walk_recolor(self, image, max_steps):
original_shape = image.shape
image = self._preprocess(image)
recolor = image.copy()
centroid_idxs = self.kmeans.predict(image)
for ci in range(self.num_colors):
@syaffers
syaffers / reduced_palette.py
Created December 4, 2020 09:58
Modified fit function to precalculate the distances of pixels to the nearest centroid
class KMeansReducedPalette:
# ... omitted
def fit(self, image):
image_cpy = image.copy()
self.source_pixels = self._preprocess(image_cpy)
self.kmeans.fit(self.source_pixels)
self.centroid_nearest_pixels = []
@syaffers
syaffers / reduced_palette.py
Created December 4, 2020 09:52
Random walk recolor method of the reduced palette class
class KMeansReducedPalette:
# ... omitted
def random_walk_recolor(self, image, max_steps):
original_shape = image.shape
image = self._preprocess(image)
centroids = self.kmeans.predict(image)
start = np.round(self.kmeans.cluster_centers_[centroids])
diff = np.zeros(image.shape)
@syaffers
syaffers / reduced_palette.py
Created December 4, 2020 09:41
K-means clustering on unique colors
class UniqueKMeansReducedPalette(KMeansReducedPalette):
def __init__(self, num_colors):
super().__init__(num_colors)
def fit(self, image):
image_cpy = image.copy()
pixels = self._preprocess(image_cpy)
super().fit(np.unique(pixels, axis=0))