IR
This commit is contained in:
parent
bf1cb09aeb
commit
e10c86baac
15 changed files with 237 additions and 26 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "Projet_SETI_RISC-V"]
|
||||||
|
path = Projet_SETI_RISC-V
|
||||||
|
url = https://gitea.auro.re/higepi/Projet_SETI_RISC-V
|
82
D3/TP/TP_SETI_Kmeans/Kmeans.py
Normal file
82
D3/TP/TP_SETI_Kmeans/Kmeans.py
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
# import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
import scipy.spatial
|
||||||
|
from skimage import io
|
||||||
|
import time
|
||||||
|
|
||||||
|
def distance(points,Pc):
|
||||||
|
return scipy.spatial.distance.cdist(points[:,:], Pc[:,:])
|
||||||
|
|
||||||
|
def kmeans(points = [0,0], K = 1):
|
||||||
|
# Initialisation K prototypes
|
||||||
|
dim = points.shape[1]
|
||||||
|
N = points.shape[0]
|
||||||
|
iter = 0
|
||||||
|
eps = 0.1
|
||||||
|
Pc_index = []
|
||||||
|
Pc_save = np.zeros([K,dim])
|
||||||
|
clusters = []
|
||||||
|
|
||||||
|
for i in range(0,K):
|
||||||
|
Pc_index.append(np.random.randint(0,N))
|
||||||
|
Pc = points[Pc_index,:]
|
||||||
|
|
||||||
|
while (np.mean(distance(Pc,Pc_save)) > eps and iter < 3):
|
||||||
|
iter += 1
|
||||||
|
Pc_save = Pc
|
||||||
|
# print(Pc)
|
||||||
|
# print(points[:,:Pc.shape[0]])
|
||||||
|
dist = distance(points=points[:,:Pc.shape[1]],Pc=Pc)
|
||||||
|
clust = np.argmin(dist, axis=1)
|
||||||
|
clust = np.expand_dims(clust, axis=0)
|
||||||
|
points = np.append(points[:,:Pc.shape[1]], clust.T, axis=1)
|
||||||
|
# print(points)
|
||||||
|
Pc = np.zeros([K,dim])
|
||||||
|
index = np.array([])
|
||||||
|
|
||||||
|
for n in range(0,N):
|
||||||
|
for k in range(0,K):
|
||||||
|
index = np.append(index, (clust==k).sum())
|
||||||
|
if points[n,-1] == k:
|
||||||
|
# print(points)
|
||||||
|
# print(Pc)
|
||||||
|
Pc[k,:] = np.add(Pc[k,:], points[n,:-1])
|
||||||
|
|
||||||
|
for k in range(0,K):
|
||||||
|
Pc[k,:] = np.divide(Pc[k,:],index[k])
|
||||||
|
|
||||||
|
# print(Pc)
|
||||||
|
indice = points[:,-1]
|
||||||
|
points = points[:,:-1]
|
||||||
|
return Pc, indice, points
|
||||||
|
|
||||||
|
def mat_2_img(mat,my_img):
|
||||||
|
img_seg = mat.reshape(my_img.shape[0], my_img.shape[1], my_img.shape[2])
|
||||||
|
return img_seg
|
||||||
|
|
||||||
|
def img_2_mat(my_img):
|
||||||
|
mat = my_img.reshape(my_img.shape[0]*my_img.shape[1],my_img.shape[2])
|
||||||
|
return mat
|
||||||
|
|
||||||
|
def kmeans_image(path_image, K):
|
||||||
|
my_img = io.imread(path_image)
|
||||||
|
# imgplot = plt.imshow(my_img)
|
||||||
|
Mat = img_2_mat(my_img)
|
||||||
|
|
||||||
|
Pc, index, clusters = kmeans(Mat, K)
|
||||||
|
|
||||||
|
for k in range(Mat.shape[0]):
|
||||||
|
Mat[k,:] = np.floor(Pc[index[k],:])
|
||||||
|
|
||||||
|
img_seg = mat_2_img(Mat, my_img)
|
||||||
|
|
||||||
|
io.imsave(path_image.split('.')[0] + "_%d.jpg" % K, img_seg)
|
||||||
|
# imgplot = plt.imshow(img_seg)
|
||||||
|
return Pc, index, img_seg
|
||||||
|
|
||||||
|
path_image = "fruits.jpg"
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
Pc, index, img_seg = kmeans_image(path_image=path_image, K=2)
|
||||||
|
end_time = time.time()
|
||||||
|
print(f"It took {end_time-start_time:.2f} seconds to compute")
|
82
D3/TP/TP_SETI_Kmeans/Kmeans_cuda.py
Normal file
82
D3/TP/TP_SETI_Kmeans/Kmeans_cuda.py
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
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")
|
||||||
|
|
||||||
|
|
23
D3/TP/TP_SETI_Kmeans/Kmeans_skcuda.py
Normal file
23
D3/TP/TP_SETI_Kmeans/Kmeans_skcuda.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import numpy as np
|
||||||
|
import cv2
|
||||||
|
import cupy as cp
|
||||||
|
|
||||||
|
# Load the image and convert it to a NumPy array
|
||||||
|
image = cv2.imread("fruits.jpg")
|
||||||
|
image = image.astype(np.float32)
|
||||||
|
|
||||||
|
# Use cupy to transfer the image to the GPU
|
||||||
|
image_gpu = 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)
|
||||||
|
|
||||||
|
# Transfer the cluster centers and labels back to the CPU
|
||||||
|
cluster_centers = cp.asnumpy(cluster_centers_gpu)
|
||||||
|
labels = cp.asnumpy(labels_gpu)
|
||||||
|
|
||||||
|
# Convert the image pixels to the closest cluster
|
||||||
|
clustered_image = cluster_centers[labels].reshape(image.shape)
|
||||||
|
|
||||||
|
# Save the clustered image as a PNG file
|
||||||
|
cv2.imwrite("clustered_image.png", clustered_image)
|
File diff suppressed because one or more lines are too long
Binary file not shown.
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Binary file not shown.
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
BIN
IA/CEAList&AI-2022-12-08-SansVidéo.pdf
Normal file
BIN
IA/CEAList&AI-2022-12-08-SansVidéo.pdf
Normal file
Binary file not shown.
BIN
IA/Cours_Micro-controleurs et architectures dédiées.pdf
Normal file
BIN
IA/Cours_Micro-controleurs et architectures dédiées.pdf
Normal file
Binary file not shown.
Binary file not shown.
BIN
IA/Edt IA-SETI-2022-23.pdf
Normal file
BIN
IA/Edt IA-SETI-2022-23.pdf
Normal file
Binary file not shown.
BIN
IA/Responsible_AI-2022-12-08-SansVidéo.pdf
Normal file
BIN
IA/Responsible_AI-2022-12-08-SansVidéo.pdf
Normal file
Binary file not shown.
BIN
IR/mimetic[8965].pdf
Normal file
BIN
IR/mimetic[8965].pdf
Normal file
Binary file not shown.
|
@ -1,9 +0,0 @@
|
||||||
# Optimisation robot
|
|
||||||
Trouver meilleurs compromis performance/consommation.
|
|
||||||
Répartition sur 5/6 groupes.
|
|
||||||
On peut commencer dès que les groupes sont faits.
|
|
||||||
|
|
||||||
|
|
||||||
## Jetson
|
|
||||||
Pour optimiser le code, il faut connaître le matériel.
|
|
||||||
Faire beaucoup de doc et de tests :)
|
|
1
Projet_SETI_RISC-V
Submodule
1
Projet_SETI_RISC-V
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit c2541cc3055c9ae0a766d2603419e5d823e7e82e
|
Loading…
Reference in a new issue