コード例 #1
0
ファイル: image.py プロジェクト: Miraj50/CS335-AIML-Lab
def label_image(img, cluster_centroids):
	'''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

	cluster_labels = []
	# todo: task6
	# Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid
	for i in img:
		temp = []
		for j in i:
			dis = distance(j, cluster_centroids[0])
			index = 0
			for in_, k in enumerate(cluster_centroids[1:]):
				t = distance(j, k)
				if t<dis:
					dis = t
					index = in_+1
			temp.append(index)
		cluster_labels.append(temp)

	return cluster_labels
コード例 #2
0
def label_image(img, cluster_centroids):
    '''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''
    height = len(img)
    if len(img) != 0:
        width = len(img[0])
    cluster_labels = [[0 for _ in range(width)] for _ in range(height)]
    for i in range(height):
        for l in range(width):
            m = 0
            d = distance(img[i][l], cluster_centroids[0])
            for j in range(len(cluster_centroids)):
                d1 = distance(img[i][l], cluster_centroids[j])
                if d1 < d:
                    m = j
                    d = d1
            cluster_labels[i][l] = m
    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid

    return cluster_labels
コード例 #3
0
ファイル: image.py プロジェクト: SnigdhaNelli/AIML
def label_image(img, cluster_centroids):
	'''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

	cluster_labels = None
	# todo: task6
	# Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid

	cluster_labels = []

	for i in range(len(img)):
		lab = []
		for k in range(len(img[0])):
			min_label = 0
			min_dist = distance(img[i][k], cluster_centroids[0])  # tuple(img[i][k])
			for j in range(1, len(cluster_centroids)):
				if(distance(img[i][k], cluster_centroids[j]) < min_dist):
					min_label = j
					min_dist = distance(img[i][k], cluster_centroids[j])
			lab.append(min_label)
		cluster_labels.append(lab)

	return cluster_labels
コード例 #4
0
def label_image(img, cluster_centroids):
    '''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

    labels = [[0 for _ in range(len(img[0]))] for _ in range(len(img))]

    for i in range(len(img)):
        for j in range(len(img[0])):
            minm = distance(tuple(img[i][j]), cluster_centroids[0])
            center = 0

            for p in range(1, len(cluster_centroids)):
                dist = distance(tuple(img[i][j]), cluster_centroids[p])
                if dist < minm:
                    center = p
                    minm = dist
            labels[i][j] = center
    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid

    return labels
コード例 #5
0
def label_image(img, cluster_centroids):
    '''
    img: RGB image as 3D list of size [h,w,3]

    Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
    [Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
    '''

    cluster_labels = None
    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid
    width = len(img[0])
    height = len(img)
    data = preprocess_image(img)
    cluster_labels = [[0 for _ in range(width)] for _ in range(height)]
    i = 0  # height counter
    j = 0  # width counter
    for pixel in data:
        dist = [distance(pixel, centroid) for centroid in cluster_centroids]
        min_dist = min(dist)
        if (j == width):
            i += 1
            j = 0
        cluster_labels[i][j] = dist.index(min_dist)
        j += 1
    return cluster_labels
コード例 #6
0
def label_image(img, cluster_centroids):
    '''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

    h = len(img)
    w = len(img[0])
    cluster_labels = [[0 for _ in range(w)] for _ in range(h)]
    for i in range(h):
        for j in range(w):
            p = tuple(img[i][j])
            mind = float("inf")
            mini = 0
            for k in range(len(cluster_centroids)):
                dist = distance(p, tuple(cluster_centroids[k]))
                if (dist < mind):
                    mind = dist
                    mini = k
            cluster_labels[i][j] = mini

    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid

    return cluster_labels
コード例 #7
0
ファイル: image.py プロジェクト: joshinh/AIML-Lab
def label_image(img, cluster_centroids):
    '''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

    height = len(img)
    width = len(img[0])

    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid
    cluster_labels = [[0 for _ in range(width)] for _ in range(height)]
    for i in range(height):
        for j in range(width):
            d_min = -1
            d_index = -1
            for k in range(len(cluster_centroids)):
                d_cluster = distance(cluster_centroids[k], tuple(img[i][j]))
                if d_min == -1:
                    d_min = d_cluster
                    d_index = k
                elif d_cluster < d_min:
                    d_min = d_cluster
                    d_index = k
            cluster_labels[i][j] = d_index

    return cluster_labels
コード例 #8
0
ファイル: image.py プロジェクト: aman0456/AI-ML-Labs
def label_image(img, cluster_centroids):
	'''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''
	height = len(img)
	width = len(img[0])
	cluster_labels = [[0 for _ in range(width)] for _ in range(height)]
	for i in range(height):
		for j in range(width):
			darr = [distance(img[i][j], x) for x in cluster_centroids]
			ind = darr.index(min(darr))
			cluster_labels[i][j] = ind
	# todo: task6
	# Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid

	return cluster_labels
コード例 #9
0
def label_image(img, cluster_centroids):
    '''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

    cluster_labels = None
    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid

    cluster_labels = img
    for ind1 in range(len(img)):
        for ind2 in range(len(img[ind1])):
            dist = [distance(img[ind1][ind2], z) for z in cluster_centroids]

            cluster_labels[ind1][ind2] = dist.index(min(dist))

    return cluster_labels
コード例 #10
0
def label_image(img, cluster_centroids):
	'''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

	cluster_labels = None
	# todo: task6
	# Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid
	cluster_labels = [ [0 for _ in range(len(img[0]))] for _ in range(len(img))]
	for i in range(len(img)):
		for j in range(len(img[0])):
			datapoint = tuple(img[i][j])
			#  make a distance list wrt all the cluster points
			distance_cluster = [ distance(datapoint , k) for k in cluster_centroids]
			# get the minimal cluster index
			cluster_labels[i][j] = distance_cluster.index(min(distance_cluster))

	return cluster_labels
コード例 #11
0
def label_image(img, cluster_centroids):
    '''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

    cluster_labels = [[0 for i in range(len(img[j]))] for j in range(len(img))]
    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid
    for i in range(len(img)):
        for j in range(len(img[i])):
            min_dist = float("inf")
            for k in range(len(cluster_centroids)):
                dist = distance(img[i][j], cluster_centroids[k])
                if dist < min_dist:
                    min_dist = dist
                    cluster_labels[i][j] = k

    return cluster_labels
コード例 #12
0
def label_image(img, cluster_centroids):
	'''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

	cluster_labels = None
	height = len(img)
	width = len(img[0])
	cluster_labels = [[0 for _ in range(width)] for _ in range(height)]
	for i in range(height):
		for j in range(width):
			# print(img[i][j])
			distancelist = map(lambda x: distance(img[i][j],x),cluster_centroids)
			# print(distancelist.index(min(distancelist)))
			cluster_labels[i][j] = distancelist.index(min(distancelist))
	# todo: task6
	# Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid

	return cluster_labels
コード例 #13
0
ファイル: image.py プロジェクト: sarthug-life/Course-Projects
def label_image(img, cluster_centroids):
    '''
	img: RGB image as 3D list of size [h,w,3]

	Return a 2D matrix where each point stores the nearest cluster centroid of corresponding pixel in the image
	[Empty 2D list] -- labels = [[0 for _ in range(width)] for _ in range(height)]
	'''

    cluster_labels = []
    height = len(img)
    width = len(img[0])
    for i in range(height):
        row = []
        for j in range(width):
            temp = [
                distance(centroid, img[i][j]) for centroid in cluster_centroids
            ]
            index = temp.index(min(temp))
            row.append(index)
        cluster_labels.append(row)
    # todo: task6
    # Iterate over the image pixels and replace each pixel value with cluster number corresponding to its nearest cluster centroid
    return cluster_labels
コード例 #14
0
def test_for_cluster():
    for_node = [(i, 'I') for i in range(1, 4)]
    for_node += [(i, 'O') for i in range(4, 6)]
    for_node += [(i, 'H2') for i in range(6, 8)]
    node_ctr = 8
    indim = 3
    outdim = 2
    innov_num = 11
    dob = 0
    node_lis = [gene.Node(x, y) for x, y in for_node]
    for_conn = [(1, (1, 4), 0.4, False), (2, (1, 5), 0.25, True),
                (3, (2, 4), 0.25, True), (4, (2, 5), 0.5, True),
                (5, (3, 4), 0.7, True), (6, (3, 5), 0.6, False),
                (7, (1, 6), 0.5, True), (8, (6, 4), 0.4, True),
                (9, (3, 7), 0.3, True), (10, (7, 5), 0.6, True)]
    conn_lis = [
        gene.Conn(x, (node_lis[tup[0] - 1], node_lis[tup[1] - 1]), w, status)
        for x, tup, w, status in for_conn
    ]
    for_bias = [(4, 0.2), (5, 0.1)]
    bias_conn_lis = [gene.BiasConn(node_lis[x - 1], y) for x, y in for_bias]

    newchromo1 = Chromosome(indim, outdim)
    newchromo1.__setattr__('conn_arr', conn_lis)
    newchromo1.__setattr__('bias_conn_arr', bias_conn_lis)
    newchromo1.__setattr__('node_arr', node_lis)
    newchromo1.__setattr__('dob', dob)
    newchromo1.set_node_ctr(node_ctr)

    for_node = [(i, 'I') for i in range(1, 4)]
    for_node += [(i, 'O') for i in range(4, 6)]
    st = '2212211'
    for_node += [(i + 6, 'H' + st[i]) for i in range(len(st))]
    node_ctr = 13
    innov_num = 25
    dob = 0
    indim = 8
    outdim = 2
    node_lis = [gene.Node(x, y) for x, y in for_node]
    for_conn = [
        (1, (1, 4), 0.3, True),
        (2, (1, 5), 0.25, False),
        (3, (2, 4), 0.25, False),
        (4, (2, 5), 0.5, False),
        (5, (3, 4), 0.7, False),
        (6, (3, 5), 0.5, True),
        (7, (1, 6), 0.2, True),
        (8, (6, 4), 0.1, True),
        (9, (2, 7), 0.1, True),
        (10, (7, 4), 0.15, True),
        (11, (1, 8), 0.5, True),
        (12, (8, 6), 0.7, True),
        (13, (1, 9), 0.3, False),
        (14, (9, 5), 1.0, True),
        (15, (3, 10), 0.33, True),
        (16, (10, 5), 0.77, True),
        (17, (1, 11), 0.25, True),
        (18, (11, 9), 0.15, True),
        (19, (2, 12), 0.6, True),
        (20, (12, 7), 0.4, True),
        (21, (3, 12), 0.8, True),
        (22, (2, 9), 0.9, True),
        (23, (12, 4), 0.75, True),
        (24, (11, 5), 0.25, True),
    ]
    conn_lis = [
        gene.Conn(x, (node_lis[tup[0] - 1], node_lis[tup[1] - 1]), w, status)
        for x, tup, w, status in for_conn
    ]
    for_bias = [(4, 0.2), (5, 0.1)]
    bias_conn_lis = [gene.BiasConn(node_lis[x - 1], y) for x, y in for_bias]
    newchromo2 = Chromosome(indim, outdim)
    newchromo2.__setattr__('conn_arr', conn_lis)
    newchromo2.__setattr__('bias_conn_arr', bias_conn_lis)
    newchromo2.__setattr__('node_arr', node_lis)
    newchromo2.__setattr__('dob', dob)
    newchromo2.set_node_ctr(node_ctr)

    # newchromo.pp()
    for_node = [(i, 'I') for i in range(1, 4)]
    for_node += [(i, 'O') for i in range(4, 6)]
    st = '221221'
    for_node += [(i + 6, 'H' + st[i]) for i in range(len(st))]
    node_ctr = 12
    dob = 0
    indim = 3
    outdim = 2
    node_lis = [gene.Node(x, y) for x, y in for_node]
    for_conn = [(1, (1, 4), 0.3, True), (2, (1, 5), 0.25, False),
                (3, (2, 4), 0.25, False), (4, (2, 5), 0.5, False),
                (5, (3, 4), 0.7, False), (6, (3, 5), 0.5, True),
                (7, (1, 6), 0.2, True), (8, (6, 4), 0.1, True),
                (9, (2, 7), 0.1, True), (10, (7, 4), 0.15, True),
                (11, (1, 8), 0.5, True), (12, (8, 6), 0.7, True),
                (13, (1, 9), 0.3, False), (14, (9, 5), 1.0, True),
                (15, (3, 10), 0.33, True), (16, (10, 5), 0.77, True),
                (19, (2, 11), 0.6, True), (20, (11, 7), 0.4, True),
                (22, (3, 11), 0.75, True)]
    conn_lis = [
        gene.Conn(x, (node_lis[tup[0] - 1], node_lis[tup[1] - 1]), w, status)
        for x, tup, w, status in for_conn
    ]
    for_bias = [(4, 0.2), (5, 0.1)]
    bias_conn_lis = [gene.BiasConn(node_lis[x - 1], y) for x, y in for_bias]
    newchromo3 = Chromosome(indim, outdim)
    newchromo3.__setattr__('conn_arr', conn_lis)
    newchromo3.__setattr__('bias_conn_arr', bias_conn_lis)
    newchromo3.__setattr__('node_arr', node_lis)
    newchromo3.__setattr__('dob', dob)
    newchromo3.set_node_ctr(node_ctr)

    for_node = [(i, 'I') for i in range(1, 4)]
    for_node += [(i, 'O') for i in range(4, 6)]
    st = '221221'
    for_node += [(i + 6, 'H' + st[i]) for i in range(len(st))]
    node_ctr = 12
    innov_num = 21
    dob = 0
    indim = 3
    outdim = 2
    node_lis = [gene.Node(x, y) for x, y in for_node]
    for_conn = [(1, (1, 4), 0.3, True), (2, (1, 5), 0.25, False),
                (3, (2, 4), 0.25, False), (4, (2, 5), 0.5, False),
                (5, (3, 4), 0.7, False), (6, (3, 5), 0.5, True),
                (7, (1, 6), 0.2, True), (8, (6, 4), 0.1, True),
                (9, (2, 7), 0.1, True), (10, (7, 4), 0.15, True),
                (11, (1, 8), 0.5, True), (12, (8, 6), 0.7, True),
                (13, (1, 9), 0.3, False), (14, (9, 5), 1.0, True),
                (15, (3, 10), 0.33, True), (16, (10, 5), 0.77, True),
                (17, (1, 11), 0.25, True), (18, (11, 9), 0.15, True),
                (21, (2, 9), 0.65, True)]
    conn_lis = [
        gene.Conn(x, (node_lis[tup[0] - 1], node_lis[tup[1] - 1]), w, status)
        for x, tup, w, status in for_conn
    ]
    for_bias = [(4, 0.2), (5, 0.1)]
    bias_conn_lis = [gene.BiasConn(node_lis[x - 1], y) for x, y in for_bias]
    newchromo4 = Chromosome(indim, outdim)
    newchromo4.__setattr__('conn_arr', conn_lis)
    newchromo4.__setattr__('bias_conn_arr', bias_conn_lis)
    newchromo4.__setattr__('node_arr', node_lis)
    newchromo4.__setattr__('dob', dob)
    newchromo4.set_node_ctr(node_ctr)

    for_node = [(i, 'I') for i in range(1, 4)]
    for_node += [(i, 'O') for i in range(4, 6)]
    st = '2212211'
    for_node += [(i + 6, 'H' + st[i]) for i in range(len(st))]
    node_ctr = 13
    innov_num = 25
    dob = 0
    indim = 3
    outdim = 2
    node_lis = [gene.Node(x, y) for x, y in for_node]
    for_conn = [
        (1, (1, 4), 0.3, False),
        (2, (1, 5), 0.25, False),
        (3, (2, 4), 0.25, False),
        (4, (2, 5), 0.5, False),
        (5, (3, 4), 0.7, False),
        (6, (3, 5), 0.5, True),
        (7, (1, 6), 0.2, True),
        (8, (6, 4), 0.1, True),
        (9, (2, 7), 0.1, True),
        (10, (7, 4), 0.15, True),
        (11, (1, 8), 0.5, True),
        (12, (8, 6), 0.7, True),
        (13, (1, 9), 0.3, False),
        (14, (9, 5), 1.0, True),
        (15, (3, 10), 0.33, True),
        (16, (10, 5), 0.77, True),
        (17, (1, 11), 0.25, True),
        (21, (3, 12), 0.8, True),
        (22, (2, 9), 0.9, True),
        (23, (12, 4), 0.75, True),
        (24, (11, 5), 0.25, True),
    ]
    conn_lis = [
        gene.Conn(x, (node_lis[tup[0] - 1], node_lis[tup[1] - 1]), w, status)
        for x, tup, w, status in for_conn
    ]
    for_bias = [(4, 0.2), (5, 0.1)]
    bias_conn_lis = [gene.BiasConn(node_lis[x - 1], y) for x, y in for_bias]
    newchromo5 = Chromosome(indim, outdim)
    newchromo5.__setattr__('conn_arr', conn_lis)
    newchromo5.__setattr__('bias_conn_arr', bias_conn_lis)
    newchromo5.__setattr__('node_arr', node_lis)
    newchromo5.__setattr__('dob', dob)
    newchromo5.set_node_ctr(node_ctr)

    for_node = [(i, 'I') for i in range(1, 4)]
    for_node += [(i, 'O') for i in range(4, 6)]
    st = '22122'
    for_node += [(i + 6, 'H' + st[i]) for i in range(len(st))]
    node_ctr = 11
    innov_num = 17
    dob = 0
    indim = 3
    outdim = 2
    node_lis = [gene.Node(x, y) for x, y in for_node]
    for_conn = [(1, (1, 4), 0.8, True), (2, (1, 5), 0.25, False),
                (3, (2, 4), 0.25, False), (4, (2, 5), 0.5, False),
                (5, (3, 4), 0.7, False), (6, (3, 5), 0.5, True),
                (7, (1, 6), 0.2, True), (8, (6, 4), 0.1, True),
                (9, (2, 7), 0.1, True), (10, (7, 4), 0.15, True),
                (11, (1, 8), 0.5, True), (12, (8, 6), 0.7, True),
                (13, (1, 9), 0.3, False), (14, (9, 5), 1.0, True),
                (15, (3, 10), 0.33, True), (16, (10, 5), 0.77, True)]
    conn_lis = [
        gene.Conn(x, (node_lis[tup[0] - 1], node_lis[tup[1] - 1]), w, status)
        for x, tup, w, status in for_conn
    ]
    for_bias = [(4, 0.2), (5, 0.1)]
    bias_conn_lis = [gene.BiasConn(node_lis[x - 1], y) for x, y in for_bias]
    newchromo6 = Chromosome(indim, outdim)
    newchromo6.__setattr__('conn_arr', conn_lis)
    newchromo6.__setattr__('bias_conn_arr', bias_conn_lis)
    newchromo6.__setattr__('node_arr', node_lis)
    newchromo6.__setattr__('dob', dob)
    newchromo6.set_node_ctr(node_ctr)

    for_node = [(i, 'I') for i in range(1, 4)]
    for_node += [(i, 'O') for i in range(4, 6)]
    for_node += [(i, 'H2') for i in range(6, 8)]
    node_ctr = 8
    indim = 3
    outdim = 2
    innov_num = 11
    dob = 0
    node_lis = [gene.Node(x, y) for x, y in for_node]
    for_conn = [(1, (1, 4), 0.4, False), (2, (1, 5), 0.25, True),
                (3, (2, 4), 0.25, True), (4, (2, 5), 0.5, True),
                (5, (3, 4), 0.7, True), (6, (3, 5), 0.6, False),
                (7, (1, 6), 0.5, True), (8, (6, 4), 0.4, True),
                (9, (3, 7), 0.3, True)]
    conn_lis = [
        gene.Conn(x, (node_lis[tup[0] - 1], node_lis[tup[1] - 1]), w, status)
        for x, tup, w, status in for_conn
    ]
    for_bias = [(4, 0.2), (5, 0.1)]
    bias_conn_lis = [gene.BiasConn(node_lis[x - 1], y) for x, y in for_bias]
    newchromo7 = Chromosome(indim, outdim)
    newchromo7.__setattr__('conn_arr', conn_lis)
    newchromo7.__setattr__('bias_conn_arr', bias_conn_lis)
    newchromo7.__setattr__('node_arr', node_lis)
    newchromo7.__setattr__('dob', dob)
    newchromo7.set_node_ctr(node_ctr)

    chromo_list = [
        newchromo1, newchromo2, newchromo3, newchromo4, newchromo5, newchromo6,
        newchromo7
    ]
    #print([item.pp() for item in chromo_list])

    print(cluster.distance(newchromo3, newchromo2))

    cluster.give_cluster_head(chromo_list, 2)
    inputarr = np.array([[0, 2, 1], [0.8, 1, 2]])
コード例 #15
0
 def get_closest_centroid_idx(point):
     centroid_distances = map(lambda c: distance(point, c),
                              cluster_centroids)
     return centroid_distances.index(min(centroid_distances))