maj
BIN
A4/A4C4-C2.pdf
Normal file
BIN
A4/TP_GPU-master/TP1_image/Kmeans_cuda.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
D3/Exo/corr_p1.jpg
Normal file
After Width: | Height: | Size: 145 KiB |
BIN
D3/Exo/corr_p2.jpg
Normal file
After Width: | Height: | Size: 145 KiB |
BIN
D3/Exo/corr_p3.jpg
Normal file
After Width: | Height: | Size: 141 KiB |
BIN
D3/Exo/corr_p4.jpg
Normal file
After Width: | Height: | Size: 145 KiB |
BIN
D3/Exo/p1.jpg
Normal file
After Width: | Height: | Size: 164 KiB |
BIN
D3/Exo/p2.jpg
Normal file
After Width: | Height: | Size: 148 KiB |
BIN
D3/Exo/p3.jpg
Normal file
After Width: | Height: | Size: 140 KiB |
|
@ -100,7 +100,7 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.10 64-bit",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
@ -114,12 +114,12 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.10"
|
||||
"version": "3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]"
|
||||
},
|
||||
"orig_nbformat": 4,
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
|
||||
"hash": "2ef431f6525756fa8a44688585fa332ef3b2e5fcfe8fe75df35bbf7028a8b511"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
BIN
D3/TP/D3_TP_Sasa_Radosavljevic.rar
Normal file
|
@ -21,7 +21,7 @@ def kmeans(points = [0,0], K = 1):
|
|||
Pc_index.append(np.random.randint(0,N))
|
||||
Pc = points[Pc_index,:]
|
||||
|
||||
while (np.mean(distance(Pc,Pc_save)) > eps and iter < 3):
|
||||
while (np.mean(distance(Pc,Pc_save)) > eps and iter < 10):
|
||||
iter += 1
|
||||
Pc_save = Pc
|
||||
# print(Pc)
|
||||
|
@ -74,9 +74,10 @@ def kmeans_image(path_image, K):
|
|||
# imgplot = plt.imshow(img_seg)
|
||||
return Pc, index, img_seg
|
||||
|
||||
path_image = "fruits.jpg"
|
||||
path_image = "images/fruits.jpg"
|
||||
|
||||
for K in range(1,21):
|
||||
start_time = time.time()
|
||||
Pc, index, img_seg = kmeans_image(path_image=path_image, K=2)
|
||||
Pc, index, img_seg = kmeans_image(path_image=path_image, K=K)
|
||||
end_time = time.time()
|
||||
print(f"It took {end_time-start_time:.2f} seconds to compute")
|
||||
print(f"It took {end_time-start_time:.2f} seconds to compute for K =", K)
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
import numpy as np
|
||||
import pycuda.autoinit
|
||||
import pycuda.driver as cuda
|
||||
from pycuda.compiler import SourceModule
|
||||
import time
|
||||
|
||||
# Load the image and convert it to a NumPy array
|
||||
from PIL import Image
|
||||
im = Image.open('fruits.jpg')
|
||||
im_data = np.array(im)
|
||||
|
||||
# Convert the image data to float32 and normalize it
|
||||
im_data = im_data.astype(np.float32) / 255
|
||||
|
||||
# Create a CUDA kernel to perform K-means clustering
|
||||
kernel = """
|
||||
__global__ void kmeans(float *data, int *labels, float *centroids, int n, int k, int dim)
|
||||
{
|
||||
int tid = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (tid >= n)
|
||||
return;
|
||||
|
||||
float min_dist = 10000;
|
||||
int min_centroid = -1;
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
float dist = 0.0;
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
float diff = data[tid * dim + j] - centroids[i * dim + j];
|
||||
dist += diff * diff;
|
||||
}
|
||||
if (dist < min_dist)
|
||||
{
|
||||
min_dist = dist;
|
||||
min_centroid = i;
|
||||
}
|
||||
}
|
||||
labels[tid] = min_centroid;
|
||||
}
|
||||
"""
|
||||
|
||||
mod = SourceModule(kernel)
|
||||
kmeans = mod.get_function("kmeans")
|
||||
|
||||
# Set the number of clusters and the number of iterations
|
||||
k = 2
|
||||
n_iter = 5
|
||||
|
||||
# Initialize the centroids and labels
|
||||
centroids = np.random.rand(k, im_data.shape[-1]).astype(np.float32)
|
||||
labels = np.zeros(im_data.shape[:2], dtype=np.int32)
|
||||
|
||||
def replace_with_nearest_centroid(centroids, colors):
|
||||
# Compute the distance between each color and each centroid
|
||||
distances = np.sqrt(np.sum((colors[:, :] - centroids) ** 2, axis=2))
|
||||
|
||||
# Find the index of the centroid that is nearest to each color
|
||||
nearest_centroids = np.argmin(distances, axis=1)
|
||||
|
||||
# Replace each color with the nearest centroid
|
||||
colors[:] = centroids[nearest_centroids]
|
||||
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
# Run the K-means algorithm
|
||||
for _ in range(n_iter):
|
||||
kmeans(cuda.In(im_data), cuda.Out(labels), cuda.In(centroids), np.int32(im_data.shape[0] * im_data.shape[1]), np.int32(k), np.int32(im_data.shape[-1]), block=(1024,1,1), grid=(im_data.shape[0] * im_data.shape[1] // 1024 + 1, 1))
|
||||
|
||||
# Update the centroids
|
||||
for i in range(k):
|
||||
centroids[i] = np.mean(im_data[labels == i], axis=0)
|
||||
|
||||
replace_with_nearest_centroid(centroids=centroids, colors=im_data)
|
||||
# Convert the labels back to the original image format
|
||||
labels = labels
|
||||
|
||||
end_time = time.time()
|
||||
print(f"It took {end_time-start_time:.2f} seconds to compute")
|
||||
|
||||
|
|
@ -1,23 +1,41 @@
|
|||
import numpy as np
|
||||
import cv2
|
||||
import cupy as cp
|
||||
import numpy as np
|
||||
from sklearn.cluster import KMeans
|
||||
from skimage import io
|
||||
import time
|
||||
|
||||
# Load the image and convert it to a NumPy array
|
||||
image = cv2.imread("fruits.jpg")
|
||||
image = image.astype(np.float32)
|
||||
# Load the image using skimage
|
||||
image = io.imread('fruits.jpg')
|
||||
|
||||
# Use cupy to transfer the image to the GPU
|
||||
image_gpu = cp.asarray(image)
|
||||
# Convert the image to a CuPy array
|
||||
image_cp = cp.asarray(image)
|
||||
|
||||
# Perform k-means clustering on the GPU
|
||||
cluster_centers_gpu, labels_gpu, _ = cp.cluster.kmeans(image_gpu.reshape(-1, 3), k=8)
|
||||
# Flatten the image into a 2D array of pixels
|
||||
image_flat = image_cp.get().reshape(image_cp.shape[0] * image_cp.shape[1], image_cp.shape[2])
|
||||
|
||||
# Transfer the cluster centers and labels back to the CPU
|
||||
cluster_centers = cp.asnumpy(cluster_centers_gpu)
|
||||
labels = cp.asnumpy(labels_gpu)
|
||||
def Kmeans_cuda(K=1):
|
||||
# Use KMeans to cluster the pixels into a specified number of clusters
|
||||
kmeans = KMeans(n_clusters=K, random_state=0).fit(image_flat)
|
||||
|
||||
# Convert the image pixels to the closest cluster
|
||||
clustered_image = cluster_centers[labels].reshape(image.shape)
|
||||
# Predict the cluster for each pixel
|
||||
clusters = kmeans.predict(image_flat)
|
||||
|
||||
# Save the clustered image as a PNG file
|
||||
cv2.imwrite("clustered_image.png", clustered_image)
|
||||
# Create a new CuPy array to hold the modified image
|
||||
new_image_cp = cp.empty_like(image_cp)
|
||||
|
||||
# Iterate over each pixel and assign its value to the corresponding cluster center
|
||||
for i, cluster in enumerate(clusters):
|
||||
new_image_cp[i // image_cp.shape[1], i % image_cp.shape[1]] = cp.asarray(kmeans.cluster_centers_[cluster])
|
||||
|
||||
# Convert the CuPy array back to a NumPy array
|
||||
new_image = cp.asnumpy(new_image_cp)
|
||||
|
||||
# Save the modified image using skimage
|
||||
io.imsave("fruits" + "_%d" % K + "_cuda.jpg", new_image)
|
||||
|
||||
|
||||
for K in range(1,256):
|
||||
start_time = time.time()
|
||||
Kmeans_cuda(K=K)
|
||||
end_time = time.time()
|
||||
print(f"It took {end_time-start_time:.2f} seconds to compute for K =",K)
|
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.3 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_10.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_100_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_101_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_102_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_103_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_104_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_105_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_106_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_107_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_108_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_109_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_10_cuda.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_11.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_110_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_111_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_112_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_113_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_114_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_115_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_116_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_117_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_118_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_119_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_11_cuda.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_12.jpg
Normal file
After Width: | Height: | Size: 21 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_120_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_121_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_122_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_123_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_124_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_125_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_126_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_127_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_128_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_129_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_12_cuda.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_13.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_130_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_131_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_132_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_133_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_134_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_135_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_136_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_137_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_138_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_139_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_13_cuda.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_14.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_140_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_141_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_142_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_143_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_144_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_145_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_146_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_147_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_148_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_149_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_14_cuda.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_15.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_150_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_151_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
D3/TP/TP_SETI_Kmeans/images/fruits_152_cuda.jpg
Normal file
After Width: | Height: | Size: 19 KiB |