2021-01-26 09:53:21 +01:00
{
"cells": [
{
"cell_type": "code",
2021-02-02 10:31:38 +01:00
"execution_count": 1,
2021-01-26 09:53:21 +01:00
"metadata": {},
"outputs": [],
"source": [
"#Tous les codes sont basés sur l'environnement suivant\n",
"#python 3.7\n",
"#opencv 3.1.0\n",
"#pytorch 1.4.0\n",
"\n",
"import torch\n",
"from torch.autograd import Variable\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"import cv2\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import random\n",
"import math\n",
"import pickle\n",
"import random\n",
"from PIL import Image\n",
"import sys\n",
"from glob import glob\n",
"from IPython.display import clear_output\n",
2021-02-02 10:31:38 +01:00
"from datetime import datetime\n",
"import json\n",
2021-03-12 12:12:26 +01:00
"from time import time\n",
"from random import randint"
2021-01-26 09:53:21 +01:00
]
},
{
"cell_type": "code",
2021-02-02 10:31:38 +01:00
"execution_count": 2,
2021-01-26 09:53:21 +01:00
"metadata": {},
"outputs": [],
"source": [
"# Les fonctions dans ce bloc ne sont pas utilisées par le réseau, mais certaines fonctions d'outils\n",
"\n",
"# Les fonctions de ce bloc se trouvent dans le programme d'apprentissage \n",
"# “Apprentissage_MSELoss_avec_GPU“\n",
"# et les commentaires détaillés se trouvent dans le programme d'apprentissage\n",
"\n",
"def tensor_imshow(im_tensor,cannel):\n",
" b,c,h,w=im_tensor.shape\n",
" if c==1:\n",
" plt.imshow(im_tensor.squeeze().detach().numpy())\n",
" else:\n",
" plt.imshow(im_tensor.squeeze().detach().numpy()[cannel,:])\n",
" \n",
"def get_training_fragment(frag_size,im):\n",
" h,w,c=im.shape\n",
" n=random.randint(0,int(h/frag_size)-1)\n",
" m=random.randint(0,int(w/frag_size)-1)\n",
" \n",
" shape=frag_size/4\n",
" vt_h=math.ceil((h+1)/shape)\n",
" vt_w=math.ceil((w+1)/shape)\n",
" vt=np.zeros([vt_h,vt_w])\n",
" vt_h_po=round((vt_h-1)*(n*frag_size/(h-1)+(n+1)*frag_size/(h-1))/2)\n",
" vt_w_po=round((vt_w-1)*(m*frag_size/(w-1)+(m+1)*frag_size/(w-1))/2)\n",
" vt[vt_h_po,vt_w_po]=1\n",
" vt = np.float32(vt)\n",
" vt=torch.from_numpy(vt.reshape(1,1,vt_h,vt_w))\n",
" \n",
" return im[n*frag_size:(n+1)*frag_size,m*frag_size:(m+1)*frag_size,:],vt\n",
"\n",
"def write_result_in_file(result,file_name):\n",
" n=0\n",
" with open(file_name,'w') as file:\n",
" for i in range(len(result)):\n",
" while n<result[i][0]:\n",
" s=str(n)\n",
" n=n+1\n",
" s=s+\"\\n\"\n",
" file.write(s)\n",
" s=str(result[i][0])+\" \"+str(result[i][1])+\" \"+str(result[i][2])+\" \"+str(result[i][3])\n",
" s=s+\"\\n\"\n",
" n=n+1\n",
" file.write(s)\n",
" file.close()\n",
" \n",
" \n",
"def img2tensor(im):\n",
" im=np.array(im,dtype=\"float32\")\n",
" tensor_cv = torch.from_numpy(np.transpose(im, (2, 0, 1)))\n",
" im_tensor=tensor_cv.unsqueeze(0)\n",
" return im_tensor\n",
"\n",
"def show_coordonnee(position_pred):\n",
" map_corre=position_pred.squeeze().detach().numpy()\n",
" score=sum(sum(map_corre))\n",
" h,w=map_corre.shape\n",
" max_value=map_corre.max()\n",
" coordonnee=np.where(map_corre==max_value)\n",
" return score,coordonnee[0].mean()/h,coordonnee[1].mean()/w\n",
"\n",
2021-03-12 12:12:26 +01:00
"#========================================================================================================================================================================\n",
"def get_initial_pos(carte,indices,max_cartes):\n",
" disp = False\n",
" carte = carte.squeeze().detach().cpu().numpy()\n",
" results = np.where(carte == np.amax(carte))\n",
" posf = [results[0][0],results[1][0]]\n",
" print(\"[BACKTRACK] Position finale: {}...\".format(posf))\n",
" if disp:\n",
" \n",
"\n",
" print(\"ÉTAPE 1:\")\n",
" print(\"Taille indices: {}\".format(indices[2].shape))\n",
" print(\"Taille max_carte: {}\".format(max_cartes[2].shape))\n",
" indices_layer_selected = max_cartes[2][posf[0],posf[1]]\n",
" index = indices[2][0,indices_layer_selected,posf[0],posf[1]]\n",
" posi = [index//251,index%251]\n",
" if disp:\n",
" print(\"Position étage 251: {}\".format(posi))\n",
"\n",
" print(\"ÉTAPE 2:\")\n",
" print(\"Taille indices: {}\".format(indices[1].shape))\n",
" print(\"Taille max_carte: {}\".format(max_cartes[1].shape))\n",
" indices_layer_selected = max_cartes[1][posi[0],posi[1]]\n",
" index = indices[1][0,indices_layer_selected,posi[0],posi[1]]\n",
" posi = [index//501,index%501]\n",
" if disp:\n",
" print(\"Position étage 501: {}\".format(posi))\n",
"\n",
" print(\"ÉTAPE 3:\")\n",
" print(\"Taille indices: {}\".format(indices[0].shape))\n",
" print(\"Taille max_carte: {}\".format(max_cartes[0].shape))\n",
" indices_layer_selected = max_cartes[0][posi[0],posi[1]]\n",
" index = indices[0][0,indices_layer_selected,posi[0],posi[1]]\n",
" posi = [index//1001,index%1001]\n",
" print(\"[BACKTRACK] Position initiale: {}\".format(posi))\n",
" \n",
" return(posi)\n",
" \n",
"\n",
"def max_carte(map_corre):\n",
" map_corre = map_corre.detach().cpu().numpy()\n",
" H,W = map_corre.shape[2],map_corre.shape[3]\n",
" carte = np.zeros((H,W),dtype=int)\n",
" #print(\"Génération de la carte ({})...\".format((H,W)))\n",
" for i in range(H):\n",
" for j in range(W):\n",
" carte[i,j] = np.where(map_corre[0,:,i,j] == np.amax(map_corre[0,:,i,j]))[0][0]\n",
" return(carte)\n",
"\n",
2021-01-26 09:53:21 +01:00
"def test_fragment32_32(frag,seuillage):\n",
" a=frag[:,:,0]+frag[:,:,1]+frag[:,:,2]\n",
" mask = (a == 0)\n",
" arr_new = a[mask]\n",
" if arr_new.size/a.size<=(1-seuillage):\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"def save_net(file_path,net):\n",
" pkl_file = open(file_path, 'wb')\n",
" pickle.dump(net,pkl_file)\n",
" pkl_file.close()\n",
" \n",
"def load_net(file_path): \n",
" pkl_file = open(file_path, 'rb')\n",
" net= pickle.load(pkl_file)\n",
" pkl_file.close()\n",
" return net\n",
"\n",
"def show_mem(txt):\n",
" t = torch.cuda.get_device_properties(0).total_memory\n",
" r = torch.cuda.memory_reserved(0) \n",
" a = torch.cuda.memory_allocated(0)\n",
" print(\"---- {} ----\".format(txt))\n",
" print(\"- Total: {} -\".format(t/1e6))\n",
" print(\"- Reserved: {} -\".format(r/1e6))\n",
" print(\"- Allocated: {} -\".format(a/1e6))\n",
" print(\"---- ----- ----\\n\")"
]
},
{
"cell_type": "code",
2021-02-02 10:31:38 +01:00
"execution_count": 3,
2021-01-26 09:53:21 +01:00
"metadata": {},
"outputs": [],
"source": [
"# Les fonctions de ce bloc sont utilisées pour construire le réseau\n",
"\n",
"# Les fonctions de ce bloc se trouvent dans le programme d'apprentissage \n",
"# “Apprentissage_MSELoss_avec_GPU“\n",
"# et les commentaires détaillés se trouvent dans le programme d'apprentissage\n",
"\n",
"def ini():\n",
" kernel=torch.zeros([8,3,3,3])\n",
" array_0=np.array([[1,2,1],[0,0,0],[-1,-2,-1]],dtype='float32')\n",
" array_1=np.array([[2,1,0],[1,0,-1],[0,-1,-2]],dtype='float32')\n",
" array_2=np.array([[1,0,-1],[2,0,-2],[1,0,-1]],dtype='float32')\n",
" array_3=np.array([[0,-1,-2],[1,0,-1],[2,1,0]],dtype='float32')\n",
" array_4=np.array([[-1,-2,-1],[0,0,0],[1,2,1]],dtype='float32')\n",
" array_5=np.array([[-2,-1,0],[-1,0,1],[0,1,2]],dtype='float32')\n",
" array_6=np.array([[-1,0,1],[-2,0,2],[-1,0,1]],dtype='float32')\n",
" array_7=np.array([[0,1,2],[-1,0,1],[-2,-1,0]],dtype='float32')\n",
" for i in range(3):\n",
" kernel[0,i,:]=torch.from_numpy(array_0)\n",
" kernel[1,i,:]=torch.from_numpy(array_1)\n",
" kernel[2,i,:]=torch.from_numpy(array_2)\n",
" kernel[3,i,:]=torch.from_numpy(array_3)\n",
" kernel[4,i,:]=torch.from_numpy(array_4)\n",
" kernel[5,i,:]=torch.from_numpy(array_5)\n",
" kernel[6,i,:]=torch.from_numpy(array_6)\n",
" kernel[7,i,:]=torch.from_numpy(array_7)\n",
" return torch.nn.Parameter(kernel,requires_grad=True) \n",
"\n",
"def kernel_add_ini(n,m):\n",
" input_canal=int(n*m)\n",
" output_canal=int(n/2)*int(m/2)\n",
" for i in range(int(n/2)):\n",
" for j in range(int(m/2)):\n",
" kernel_add=np.zeros([1,input_canal],dtype='float32')\n",
" kernel_add[0,i*2*m+j*2]=1\n",
" kernel_add[0,i*2*m+j*2+1]=1\n",
" kernel_add[0,(i*2+1)*m+j*2]=1\n",
" kernel_add[0,(i*2+1)*m+j*2+1]=1\n",
" if i==0 and j==0:\n",
" add=torch.from_numpy(kernel_add.reshape(1,input_canal,1,1))\n",
" else:\n",
" add_=torch.from_numpy(kernel_add.reshape(1,input_canal,1,1))\n",
" add=torch.cat((add,add_),0)\n",
" return torch.nn.Parameter(add,requires_grad=False) \n",
"\n",
"def kernel_shift_ini(n,m):\n",
" input_canal=int(n*m)\n",
" output_canal=int(n*m)\n",
" \n",
" kernel_shift=torch.zeros([output_canal,input_canal,3,3])\n",
" \n",
" array_0=np.array([[1,0,0],[0,0,0],[0,0,0]],dtype='float32')\n",
" array_1=np.array([[0,0,1],[0,0,0],[0,0,0]],dtype='float32')\n",
" array_2=np.array([[0,0,0],[0,0,0],[1,0,0]],dtype='float32')\n",
" array_3=np.array([[0,0,0],[0,0,0],[0,0,1]],dtype='float32')\n",
" \n",
" kernel_shift_0=torch.from_numpy(array_0)\n",
" kernel_shift_1=torch.from_numpy(array_1)\n",
" kernel_shift_2=torch.from_numpy(array_2)\n",
" kernel_shift_3=torch.from_numpy(array_3)\n",
" \n",
" \n",
" for i in range(n):\n",
" for j in range(m):\n",
" if i==0 and j==0:\n",
" kernel_shift[0,0,:]=kernel_shift_0\n",
" else:\n",
" if i%2==0 and j%2==0:\n",
" kernel_shift[i*m+j,i*m+j,:]=kernel_shift_0\n",
" if i%2==0 and j%2==1:\n",
" kernel_shift[i*m+j,i*m+j,:]=kernel_shift_1\n",
" if i%2==1 and j%2==0:\n",
" kernel_shift[i*m+j,i*m+j,:]=kernel_shift_2\n",
" if i%2==1 and j%2==1:\n",
" kernel_shift[i*m+j,i*m+j,:]=kernel_shift_3\n",
" \n",
" return torch.nn.Parameter(kernel_shift,requires_grad=False) \n",
"\n",
"def get_patch(fragment,psize,n,m):\n",
" return fragment[:,:,n*psize:(n+1)*psize,m*psize:(m+1)*psize]\n",
"\n",
"class Net(nn.Module):\n",
" def __init__(self,frag_size,psize):\n",
" super(Net, self).__init__()\n",
" \n",
" h_fr=frag_size\n",
" w_fr=frag_size\n",
" \n",
" n=int(h_fr/psize) #n*m patches\n",
" m=int(w_fr/psize)\n",
" \n",
" self.conv1 = nn.Conv2d(3,8,kernel_size=3,stride=1,padding=1)\n",
" #self.conv1.weight=ini()\n",
" self.Relu = nn.ReLU(inplace=True)\n",
2021-03-12 12:12:26 +01:00
" self.maxpooling = nn.MaxPool2d(3,stride=2, padding=1)\n",
" self.maxunpooling = nn.MaxUnpool2d(kernel_size=3,stride=1,padding=1)\n",
2021-01-26 09:53:21 +01:00
" \n",
" self.shift1=nn.Conv2d(n*m,n*m,kernel_size=3,stride=1,padding=1)\n",
" self.shift1.weight=kernel_shift_ini(n,m)\n",
" self.add1 = nn.Conv2d(n*m,int(n/2)*int(m/2),kernel_size=1,stride=1,padding=0)\n",
" self.add1.weight=kernel_add_ini(n,m)\n",
" \n",
" n=int(n/2)\n",
" m=int(m/2)\n",
" if n>=2 and m>=2:\n",
" self.shift2=nn.Conv2d(n*m,n*m,kernel_size=3,stride=1,padding=1)\n",
" self.shift2.weight=kernel_shift_ini(n,m)\n",
" self.add2 = nn.Conv2d(n*m,int(n/2)*int(m/2),kernel_size=1,stride=1,padding=0)\n",
" self.add2.weight=kernel_add_ini(n,m)\n",
" \n",
" n=int(n/2)\n",
" m=int(m/2)\n",
" if n>=2 and m>=2:\n",
" self.shift3=nn.Conv2d(n*m,n*m,kernel_size=3,stride=1,padding=1)\n",
" self.shift3.weight=kernel_shift_ini(n,m)\n",
" self.add3 = nn.Conv2d(n*m,int(n/2)*int(m/2),kernel_size=1,stride=1,padding=0)\n",
" self.add3.weight=kernel_add_ini(n,m)\n",
" \n",
" def get_descripteur(self,img,using_cuda):\n",
" descripteur_img=self.Relu(self.conv1(img))\n",
" b,c,h,w=descripteur_img.shape\n",
" couche_constante=0.5*torch.ones([1,1,h,w])\n",
" if using_cuda:\n",
" couche_constante=couche_constante.cuda()\n",
" descripteur_img=torch.cat((descripteur_img,couche_constante),1)\n",
" descripteur_img_norm=descripteur_img/torch.norm(descripteur_img,dim=1)\n",
" return descripteur_img_norm\n",
" \n",
" def forward(self,img,frag,using_cuda):\n",
" psize=4\n",
2021-03-12 12:12:26 +01:00
" #print(\"[NET][FORWARD] Correlation commencée.\")\n",
2021-01-26 09:53:21 +01:00
" \n",
" descripteur_input1=self.get_descripteur(img,using_cuda)\n",
" descripteur_input2=self.get_descripteur(frag,using_cuda)\n",
" \n",
2021-03-12 12:12:26 +01:00
" max_cartes = []\n",
" indices = []\n",
" \n",
2021-01-26 09:53:21 +01:00
" b,c,h,w=frag.shape\n",
" n=int(h/psize)\n",
" m=int(w/psize)\n",
" \n",
" for i in range(n):\n",
" for j in range(m):\n",
" if i==0 and j==0:\n",
" map_corre=F.conv2d(descripteur_input1,get_patch(descripteur_input2,psize,i,j),padding=2)\n",
" else:\n",
" a=F.conv2d(descripteur_input1,get_patch(descripteur_input2,psize,i,j),padding=2)\n",
" map_corre=torch.cat((map_corre,a),1)\n",
" #shift\n",
2021-03-12 12:12:26 +01:00
" map_corre= self.maxpooling(map_corre)\n",
" #indices.append(ri.detach().cpu().numpy())\n",
" #max_cartes.append(max_carte(map_corre))\n",
2021-01-26 09:53:21 +01:00
" map_corre=self.shift1(map_corre)\n",
" map_corre=self.add1(map_corre)\n",
2021-03-12 12:12:26 +01:00
" #print(\"[NET][FORWARD] Agreg 1 finie.\")\n",
2021-01-26 09:53:21 +01:00
" \n",
" \n",
" n=int(n/2)\n",
" m=int(m/2)\n",
" if n>=2 and m>=2:\n",
" map_corre=self.maxpooling(map_corre)\n",
2021-03-12 12:12:26 +01:00
" #indices.append(ri.detach().cpu().numpy())\n",
" #max_cartes.append(max_carte(map_corre))\n",
2021-01-26 09:53:21 +01:00
" map_corre=self.shift2(map_corre)\n",
" map_corre=self.add2(map_corre)\n",
2021-03-12 12:12:26 +01:00
" #print(\"[NET][FORWARD] Agreg 2 finie.\")\n",
2021-01-26 09:53:21 +01:00
" \n",
" \n",
" n=int(n/2)\n",
" m=int(m/2)\n",
" if n>=2 and m>=2:\n",
" map_corre=self.maxpooling(map_corre)\n",
2021-03-12 12:12:26 +01:00
" #indices.append(ri.detach().cpu().numpy())\n",
" #max_cartes.append(max_carte(map_corre))\n",
2021-01-26 09:53:21 +01:00
" map_corre=self.shift3(map_corre)\n",
" map_corre=self.add3(map_corre)\n",
2021-03-12 12:12:26 +01:00
" #print(\"[NET][FORWARD] Agreg 3 finie.\")\n",
2021-01-26 09:53:21 +01:00
" \n",
" \n",
" b,c,h,w=map_corre.shape\n",
" map_corre=map_corre/(map_corre.max())\n",
" #map_corre=(F.softmax(map_corre.reshape(1,1,h*w,1),dim=2)).reshape(b,c,h,w)\n",
2021-03-12 12:12:26 +01:00
" #print(\"[NET][FORWARD] Correlation finie.\")\n",
2021-01-26 09:53:21 +01:00
" return map_corre"
]
},
{
"cell_type": "code",
2021-02-02 10:31:38 +01:00
"execution_count": 4,
2021-01-26 09:53:21 +01:00
"metadata": {},
"outputs": [],
"source": [
"# Les fonctions de ce bloc sont utilisées pour appliquer le réseau à des fragments (pas à des patchs carrés)\n",
"\n",
"\n",
"# Cette fonction permet de sélectionner un ensemble de patchs carrés à partir d'un fragment\n",
"# Le paramètre “frag_size” fait ici référence à la taille du patch d'entrée carré (16 * 16)\n",
"# Le paramètre “seuillage” limite la proportion de pixels non noirs dans chaque patch\n",
"# Le paramètre “limite” peut limiter le nombre de correctifs trouvés dans chaque fragment\n",
2021-03-12 12:12:26 +01:00
"def get_patch_list_old(frag,frag_size,limite,seuillage):\n",
2021-01-26 09:53:21 +01:00
" n=0\n",
" m=0\n",
" h,w,c=frag.shape\n",
" patch_list=[]\n",
" position_list=[]\n",
" for i in range(4):\n",
" if len(patch_list)>limite and limite!=0:\n",
" break\n",
" for j in range(4):\n",
" if len(patch_list)>limite and limite!=0:\n",
" break\n",
" n_offset=i*4 # n offset\n",
" m_offset=j*4 # m offset\n",
" n=0\n",
" while n+frag_size+n_offset<h:\n",
" m=0\n",
" while m+frag_size+m_offset<w:\n",
" patch=frag[n+n_offset:n+frag_size+n_offset,m+m_offset:m+frag_size+m_offset,:]\n",
2021-03-12 12:12:26 +01:00
" #if test_fragment32_32(patch,seuillage):\n",
" patch_list.append(patch)\n",
" position_list.append([int((n+frag_size/2)+n_offset),int((m+frag_size/2)+m_offset)])\n",
2021-01-26 09:53:21 +01:00
" m=m+frag_size\n",
" n=n+frag_size\n",
2021-03-12 12:12:26 +01:00
" if d:\n",
" for i in range(len(position_list)):\n",
" print(\"{}\".format(position_list[i]))\n",
2021-01-26 09:53:21 +01:00
" return patch_list,position_list\n",
"\n",
2021-03-12 12:12:26 +01:00
"def get_patch_list(frag,frag_size,limite,seuillage):\n",
" frag_taille = frag.shape\n",
" print(frag.shape)\n",
" patchs, positions = [], []\n",
" for i in range(limite):\n",
" h,w = randint(0,frag_taille[0]-frag_size), randint(0,frag_taille[1]-frag_size)\n",
" patchs.append(frag[h:h+frag_size,w:w+frag_size,:])\n",
" positions.append([int(h+frag_size/2),int(w+frag_size/2)])\n",
" return(patchs,positions)\n",
" \n",
"\n",
"# Entré du fragment et de la fresque, exécutez le réseau\n",
2021-01-26 09:53:21 +01:00
"def run_net_v3(net,img,frag,frag_size,limite,seuillage,using_cuda,rotation):\n",
" Img=Image.fromarray(frag)\n",
" frag=np.array(Img.rotate(rotation))\n",
" img_tensor=img2tensor(img)\n",
" \n",
" # la collection de patchs carrée dans le fragement \"sont frag_list[]\"\n",
" # La position de leur centre dans la fragment sont \"position_frag[]\"\n",
2021-03-12 12:12:26 +01:00
" patchs,positions_patchs=get_patch_list(frag,frag_size,limite,seuillage)\n",
" #patchs,positions_patchs = get_patch_list_old(frag,frag_size,limite,seuillage)\n",
" #print(\"Calcule des positions des {} patchs:\".format(len(patchs)))\n",
2021-01-26 09:53:21 +01:00
" if using_cuda:\n",
" img_tensor=img_tensor.cuda()\n",
" \n",
" score_list=[]\n",
" coordonnee_list=[]\n",
" \n",
" # Pour chaque patch carré dans la collection, effectuez un calcul en réseau de leur position\n",
" # Le résultat est placé en \"coordonnee_list[]\"\n",
" # \"score_list[]\" pas utile dans notre programme\n",
2021-03-12 12:12:26 +01:00
" print(\"Estimation de {} patchs\".format(len(patchs)))\n",
" for i in range(len(patchs)):\n",
" #print(\"[RUNNETv3] Patch {}/{}\".format(i,len(patchs)))\n",
" patch_tensor=img2tensor(patchs[i])\n",
2021-01-26 09:53:21 +01:00
" if using_cuda:\n",
2021-03-12 12:12:26 +01:00
" patch_tensor=patch_tensor.cuda()\n",
" res=net.forward(img_tensor,patch_tensor,using_cuda)\n",
" \n",
" #pos_init = get_initial_pos(res,indices,max_cartes)\n",
" \n",
2021-01-26 09:53:21 +01:00
" if using_cuda:\n",
" res=res.cpu()\n",
2021-03-12 12:12:26 +01:00
" \n",
2021-01-26 09:53:21 +01:00
" score,po_h,po_w=show_coordonnee(res)\n",
" coordonnee_list.append([po_h,po_w])\n",
2021-03-12 12:12:26 +01:00
" #score_list.append(score)\n",
2021-01-26 09:53:21 +01:00
" h_img,w_img,c=img.shape\n",
" position=[]\n",
" \n",
" # Mettez les paires correspondante en \"position[]\"\n",
" # [x,y,x',y']\n",
" # La position (x,y) dans le fragment correspond à la position (x',y') dans la fresque\n",
" for i in range(len(coordonnee_list)):\n",
2021-03-12 12:12:26 +01:00
" x0=positions_patchs[i][0]\n",
" y0=positions_patchs[i][1]\n",
2021-01-26 09:53:21 +01:00
" x1=int(round(h_img*coordonnee_list[i][0]))\n",
" y1=int(round(w_img*coordonnee_list[i][1]))\n",
2021-03-12 12:12:26 +01:00
" #x1 = coordonnee_list[i][0]\n",
" #y1 = coordonnee_list[i][1]\n",
2021-01-26 09:53:21 +01:00
" position.append([x0,y0,x1,y1])\n",
" return score_list,position"
]
},
{
"cell_type": "code",
2021-02-02 10:31:38 +01:00
"execution_count": 5,
2021-01-26 09:53:21 +01:00
"metadata": {},
"outputs": [],
"source": [
"# Cette partie du code consiste à implémenter l'algorithme RANSAC amélioré\n",
"\n",
"# Ecrire le point sous forme [x,y,1]T,\n",
"# Utilisé pour construire l'équation de la matrice de transformation\n",
"def creer_point(x,y):\n",
" p=np.zeros((3,1))\n",
" p[0][0]=x\n",
" p[1][0]=y\n",
" p[2][0]=1\n",
" return p\n",
"\n",
"# Sélectionnez aléatoirement n points sans duplication à partir de M points\n",
"def selectionner_points(n,M):\n",
" table=[]\n",
" for i in range(M):\n",
" table.append(i)\n",
" result=[]\n",
" for i in range(n):\n",
" index=random.randint(0,M-i-1)\n",
" result.append(table[index])\n",
" table[index]=table[M-1-i]\n",
" return result\n",
"\n",
"# Selon la matrice de transformation affine, calculer la position centrale transformée et l'angle de rotation\n",
"def position_rotation(h,centre_frag):\n",
" centre=h@centre_frag\n",
" cos_rot=(h[0][0]+h[1][1])/2\n",
" sin_rot=(h[1][0]-h[0][1])/2\n",
" tan_rot=sin_rot/(cos_rot+0.0000001)\n",
" if cos_rot>0:\n",
" rot_frag=math.atan(tan_rot)*(180/math.pi)\n",
" else:\n",
" rot_frag=math.atan(tan_rot)*(180/math.pi)+180\n",
" rot_frag=-rot_frag\n",
" if rot_frag>0:\n",
" rot_frag-=360\n",
" return centre[0][0],centre[1][0],rot_frag\n",
"\n",
"# Vérifiez les résultats de Ransac en avec des changements de distance euclidienne\n",
"def test_frag(inline,frag,fres):\n",
" itera=10\n",
" frag_inline=[]\n",
" fres_inline=[]\n",
" # Metter les coordonnées du point inline dans \"frag_inline[]\",et \"fres_inline[]\"\n",
" for i in range(np.size(inline,0)):\n",
" if inline[i]==1:\n",
" frag_inline.append([frag[i][0],frag[i][1]])\n",
" fres_inline.append([fres[i][0],fres[i][1]])\n",
" p=[]\n",
" \n",
" # Faites une boucle dix fois, \n",
" # sélectionnez à chaque fois deux paires correspondantes inline \n",
" # calculer le changement de leur distance euclidienne\n",
" for i in range(itera):\n",
" point_test=selectionner_points(2,np.size(frag_inline,0))\n",
" diff_x_frag=frag_inline[point_test[1]][0]-frag_inline[point_test[0]][0]\n",
" diff_y_frag=frag_inline[point_test[1]][1]-frag_inline[point_test[0]][1]\n",
" diff_frag=math.sqrt(math.pow(diff_x_frag,2)+math.pow(diff_y_frag,2))\n",
" \n",
" diff_x_fres=fres_inline[point_test[1]][0]-fres_inline[point_test[0]][0]\n",
" diff_y_fres=fres_inline[point_test[1]][1]-fres_inline[point_test[0]][1]\n",
" diff_fres=math.sqrt(math.pow(diff_x_fres,2)+math.pow(diff_y_fres,2))\n",
" if diff_frag !=0:\n",
" fsf=diff_fres/diff_frag\n",
" p.append([fsf])\n",
" result=np.mean(p)\n",
" return result\n",
"\n",
"def frag_match(frag,img,position):\n",
" \n",
" frag_size=frag.shape\n",
" centre_frag=creer_point(frag_size[0]/2,frag_size[1]/2)\n",
" \n",
" retained_matches = []\n",
" frag=[]\n",
" fres=[]\n",
" \n",
" for i in range(len(position)):\n",
" frag.append([float(position[i][0]),float(position[i][1])])\n",
" fres.append([float(position[i][2]),float(position[i][3])])\n",
" \n",
" if np.size(frag)>0:\n",
" # Calculer la matrice de transformation affine à l'aide de la méthode Ransac\n",
" h,inline=cv2.estimateAffinePartial2D(np.array(frag),np.array(fres))\n",
" # Si “h” n'est pas sous la forme de matrice 2 * 3, la matrice de transformation affine n'est pas trouvée\n",
" if np.size(h)!=6:\n",
" return ([-1])\n",
" else:\n",
" x,y,rot=position_rotation(h,centre_frag)\n",
" pourcenttage=sum(inline)/np.size(frag,0)\n",
" # Le nombre de points inline doit être supérieur à un certain nombre\n",
" if sum(inline)>3:\n",
" p=test_frag(inline,frag,fres)\n",
" # La distance euclidienne entre les points correspondants ne doit pas trop changer, \n",
" # sinon cela prouve que le résultat de Ransac est incorrect\n",
" # ici,le changement de la distance euclidienne sont entre 0.7 et 1.3\n",
" if abs(p-1)<0.3:\n",
" # Ce n'est qu'alors que Ransac renvoie le résultat correct\n",
2021-02-02 10:31:38 +01:00
" return([round(x),round(y),round(rot,3)])\n",
2021-01-26 09:53:21 +01:00
" else:\n",
" return ([-2])\n",
" else:\n",
" return ([-3])\n",
" else:\n",
" return ([-4]) "
]
},
{
"cell_type": "code",
2021-02-11 09:00:27 +01:00
"execution_count": 6,
2021-01-26 09:53:21 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2021-03-12 12:12:26 +01:00
"[MAIN]Fresque 5, fragment 99/100 (99.0%)\n",
"[MAIN]Temps par fragment: 5.71. ETA = 5.71s\n",
"Fragment de taille (82, 39, 3)\n",
"(82, 39, 3)\n",
"Estimation de 30 patchs\n",
"Nombre de positions avant match: 30\n",
"[!Retour MATCH] code [-2]\n",
"Sauvegardé dans ./results_bench/results_SLLShift_8_f5_03-11_18-36_E3_0120\n"
2021-01-26 09:53:21 +01:00
]
}
],
"source": [
"if __name__==\"__main__\":\n",
" \n",
2021-03-12 12:12:26 +01:00
" d=False # Affichage des info ou non\n",
" \n",
" try:\n",
" del frag\n",
" del img\n",
" net.zero_grad()\n",
" torch.cuda.empty_cache()\n",
" print(\"Emptied\")\n",
" except:\n",
" pass\n",
" \n",
2021-01-26 09:53:21 +01:00
" # Variable du réseau\n",
" frag_size=16\n",
" using_cuda=True\n",
2021-02-02 10:31:38 +01:00
" for fresque_id in range(6):\n",
" #fresque_id = 2\n",
"\n",
" # Variable des données\n",
2021-03-12 12:12:26 +01:00
" base_dir = './training_data_32/'\n",
2021-02-02 10:31:38 +01:00
" fresque_filename = base_dir+'fresque_small{}.ppm'\n",
" fresque_filename_wild = base_dir+'fresque_small*.ppm'\n",
" fragment_filename = base_dir+'fragments/fresque{}/frag_bench_{:05}.ppm'\n",
" fragments_filename_wild = base_dir+'fragments/fresque{}/frag_bench_*.ppm'\n",
" vt_filename = base_dir+'fragments/fresque{}/vt/frag_bench_{:05}_vt.txt'\n",
2021-03-12 12:12:26 +01:00
" net_filename = \"./trained_net/net_trainned_SLLShift_E3_03-11_17-34_0120\"\n",
2021-01-26 09:53:21 +01:00
" \n",
2021-03-12 12:12:26 +01:00
" expe_id = int(net_filename.split(\"_\")[-1]) # ID de l'expérience, à ajouter à tout les fichiers écrits pour identifier les résultats d'une même expérience.\n",
" #expe_id = 999\n",
2021-02-02 10:31:38 +01:00
" date = datetime.now().strftime(\"%m-%d_%H-%M\")\n",
2021-03-12 12:12:26 +01:00
" results_filename = './results_bench/results_SLLShift_8_f{}_{}_E3_{:04}'.format(fresque_id,date,expe_id)\n",
" \n",
" #placements = {\"frag_filename\":[], \"positions\" : [], \"scores\":[], \"fresque_filename\": []}\n",
" #placements_filename = \"./results_bench/placements_{}_{}\".format(date,expe_id)\n",
2021-02-02 10:31:38 +01:00
"\n",
" # Chargement du réseau\n",
" net=load_net(net_filename)\n",
2021-03-12 12:12:26 +01:00
" if using_cuda:\n",
" net.cuda()\n",
2021-02-02 10:31:38 +01:00
"\n",
" # Charge la fresque en mémoire\n",
" img=cv2.imread(fresque_filename.format(fresque_id))\n",
"\n",
" N_fragments = len(glob(fragments_filename_wild.format(fresque_id)))\n",
2021-03-12 12:12:26 +01:00
" N_fragments = 100\n",
2021-02-02 10:31:38 +01:00
" print(fragments_filename_wild.format(fresque_id))\n",
" print(N_fragments)\n",
"\n",
" # Crée les tableau de résultats\n",
" distances, matched, positions, verite_terrain = [],[],[],[]\n",
" tailles = []\n",
"\n",
" time_old = time()\n",
2021-03-12 12:12:26 +01:00
" # Parcourt tout les fragments de bench de cette fresque\n",
2021-02-02 10:31:38 +01:00
" for fragment_id in range(N_fragments):\n",
" clear_output(wait=True)\n",
2021-03-12 12:12:26 +01:00
" print(\"[MAIN]Fresque {}, fragment {}/{} ({:.3}%)\".format(fresque_id,fragment_id,N_fragments,(fragment_id/N_fragments*100)))\n",
2021-02-02 10:31:38 +01:00
" delta = time()-time_old\n",
2021-03-12 12:12:26 +01:00
" print(\"[MAIN]Temps par fragment: {:.3}. ETA = {:.3}s\".format(delta,(N_fragments-fragment_id)*delta))\n",
2021-02-02 10:31:38 +01:00
" time_old = time()\n",
" frag = cv2.imread(fragment_filename.format(fresque_id,fragment_id))\n",
2021-03-12 12:12:26 +01:00
" \n",
" print(\"Fragment de taille {}\".format(frag.shape))\n",
"\n",
" score_list,positions_patchs=run_net_v3(net,img,frag,frag_size,30,0.7,using_cuda,0)\n",
" \n",
" #placements[\"frag_filename\"].append(fragment_filename.format(fresque_id,fragment_id))\n",
" #placements[\"positions\"].append(positions_patchs)\n",
" #placements[\"scores\"].append(score_list)\n",
" #placements[\"fresque_filename\"].append(fresque_filename.format(fresque_id))\n",
" \n",
" #with open(placements_filename,'w') as f:\n",
" # f.write(json.dumps(placements))\n",
" \n",
" \n",
" \n",
" print(\"Nombre de positions avant match: {}\".format(len(positions_patchs)))\n",
" frag_position=frag_match(frag,img,positions_patchs)\n",
2021-02-02 10:31:38 +01:00
"\n",
" # Si Ransac trouve une solution, la variable renvoyé est une liste de deux positions et une rotation\n",
2021-01-26 09:53:21 +01:00
" if len(frag_position)==3:\n",
2021-03-12 12:12:26 +01:00
" print(\"MATCH !\")\n",
2021-02-02 10:31:38 +01:00
"\n",
" # MATCHED\n",
" matched.append(1)\n",
"\n",
" # POSITION\n",
2021-03-12 12:12:26 +01:00
" frag_position[2]=0\n",
2021-02-02 10:31:38 +01:00
" if frag_position[2]>0:\n",
" frag_position[2]=frag_position[2]-360\n",
" positions.append([frag_position[0],frag_position[1],round(frag_position[2],3)])\n",
"\n",
" # VERITE TERRAIN\n",
" with open(vt_filename.format(fresque_id,fragment_id), 'r') as f:\n",
2021-03-12 12:12:26 +01:00
" vt = f.read().splitlines()\n",
" vt = [int(i) for i in vt]\n",
" verite_terrain.append([vt[2],vt[3],frag.shape[0],frag.shape[1]])\n",
2021-02-02 10:31:38 +01:00
"\n",
" # DISTANCE\n",
2021-03-12 12:12:26 +01:00
" distances.append(np.linalg.norm([vt[2]-frag_position[0],vt[3]-float(frag_position[1])]))\n",
" \n",
" if d:\n",
" print(\"Position: {}\".format(positions[-1]))\n",
" print(\"Verite terrain: {}\".format(vt))\n",
" print(\"Distance: {}\\n\".format(distances[-1]))\n",
2021-02-02 10:31:38 +01:00
" else:\n",
2021-03-12 12:12:26 +01:00
" print(\"[!Retour MATCH] code {}\".format(frag_position))\n",
2021-02-02 10:31:38 +01:00
" matched.append(0)\n",
" distances.append(-1)\n",
" positions.append([])\n",
" verite_terrain.append([])\n",
"\n",
" del frag\n",
"\n",
" meta = {'date':date,'base_dir':base_dir,'fresque_id':fresque_id,'fresque_taille':img.shape,'N_fragments': N_fragments,'expe_id': expe_id}\n",
" res = {'meta':meta, 'matched':matched,'distances':distances,'positions':positions,'vt':verite_terrain}\n",
"\n",
" with open(results_filename,'w') as f:\n",
" f.write(json.dumps(res))\n",
"\n",
" print(\"Sauvegardé dans {}\".format(results_filename))"
2021-01-26 09:53:21 +01:00
]
},
{
"cell_type": "code",
2021-02-02 10:31:38 +01:00
"execution_count": 13,
2021-01-26 09:53:21 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2021-02-02 10:31:38 +01:00
"Sauvegarder dans results_f2_01-31_14-32\n"
2021-01-26 09:53:21 +01:00
]
}
],
"source": [
"date = datetime.now().strftime(\"%m-%d_%H-%M\")\n",
"meta = {'date':date,'base_dir':base_dir,'fresque_id':fresque_id,'fresque_taille':img.shape,'N_fragments': N_fragments}\n",
2021-02-02 10:31:38 +01:00
"res = {'meta':meta, 'matched':matched,'distances':distances,'positions':positions,'vt':verite_terrain}\n",
2021-01-26 09:53:21 +01:00
"\n",
2021-02-02 10:31:38 +01:00
"with open('results_bench/results_bench_from-random_full-dataset-small_MB9_f{}_{}'.format(fresque_id,date),'w') as f:\n",
2021-01-26 09:53:21 +01:00
" f.write(json.dumps(res))\n",
"\n",
"print(\"Sauvegarder dans {}\".format('results_f{}_{}'.format(fresque_id,date)))"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'positions' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-bdbb39569e28>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpositions\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'positions' is not defined"
]
}
],
"source": [
"print(positions)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1305"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Clear GPU memory \n",
"import gc\n",
"torch.cuda.empty_cache()\n",
"gc.collect()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}