POZOR: Ak používate počítač, ktorého GPU nepodporuje prostredie CUDA, môžete na akceleráciu použiť Google Colab notebook s GPU podporou.

In [36]:
# Doinštalujeme balíčky, ktoré nie sú defaultne k dispozícii
import sys
!{sys.executable} -m pip install face_recognition google-images-download lapjv
Requirement already satisfied: face_recognition in /usr/local/lib/python3.6/dist-packages (1.2.3)
Requirement already satisfied: google-images-download in /usr/local/lib/python3.6/dist-packages (2.8.0)
Requirement already satisfied: lapjv in /usr/local/lib/python3.6/dist-packages (1.3.1)
Requirement already satisfied: dlib>=19.7 in /usr/local/lib/python3.6/dist-packages (from face_recognition) (19.16.0)
Requirement already satisfied: Pillow in /usr/local/lib/python3.6/dist-packages (from face_recognition) (4.3.0)
Requirement already satisfied: Click>=6.0 in /usr/local/lib/python3.6/dist-packages (from face_recognition) (7.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from face_recognition) (1.16.4)
Requirement already satisfied: face-recognition-models>=0.3.0 in /usr/local/lib/python3.6/dist-packages (from face_recognition) (0.3.0)
Requirement already satisfied: selenium in /usr/local/lib/python3.6/dist-packages (from google-images-download) (3.141.0)
Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from Pillow->face_recognition) (0.46)
Requirement already satisfied: urllib3 in /usr/local/lib/python3.6/dist-packages (from selenium->google-images-download) (1.24.3)
In [0]:
# Import potrebných balíčkov
import os
import shutil
from sklearn.cluster import DBSCAN
from umap import UMAP
import matplotlib.pyplot as plt
import cv2
from lapjv import lapjv
from scipy.spatial.distance import cdist

Zhlukovanie ľudských tvárí

V tomto notebook-u ukážeme, ako sa dá v Python-e jednoducho realizovať zhlukovanie ľudských tvárí. Kód je založený na blog-u Adriana Rosebrocka.

Na začiatok si zadefinujeme niekoľko pomocných funkcií, ktoré budeme neskôr potrebovať.

In [0]:
#@title >> SKRYTÁ IMPLEMENTÁCIA RUTÍN << { display-mode: "form" }
import face_recognition
import cv2
import os
import glob
from imutils import build_montages
import matplotlib.pyplot as plt
import numpy as np

def get_image_filenames(
    directory,
    image_exts = ['.jpg', '.jpeg', '.png', '.gif'],
    recursive = True
):
    images = []
    
    for fname in glob.glob(os.path.join(directory, "**/*"), recursive=recursive):
        if os.path.isfile(fname) and os.path.splitext(fname)[-1] in image_exts:
            images.append(fname)
    
    return images

def enforce_maxres(image, max_resolution):
    height, width = image.shape[:2]
    
    if width > height:
        if width > max_resolution:
            width, height = max_resolution, max_resolution / width * height
            image = cv2.resize(image, (int(width), int(height)))
    else:
        if height > max_resolution:
            width, height = max_resolution / height * width, max_resolution
            image = cv2.resize(image, (int(width), int(height)))
            
    return image

def encode_image(image, imagePath, 
                 detection_method="cnn",
                 max_resolution=1800):
    """
    detection_method: "cnn" or "hog"
    """
    image = enforce_maxres(image, max_resolution)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # detect the (x, y)-coordinates of the bounding boxes
    # corresponding to each face in the input image
    boxes = face_recognition.face_locations(rgb,
        model=detection_method)

    # compute the facial embedding for the face
    encodings = face_recognition.face_encodings(rgb, boxes)

    # build a dictionary of the image path, bounding box location,
    # and facial encodings for the current image
    d = [{"imagePath": imagePath, "loc": box,
          "encoding": enc, 'shape': image.shape}
                for (box, enc) in zip(boxes, encodings)]
    
    return d
    
def encode_dataset(dataset_path, detection_method,
                   verbose=1, max_resolution=1800):
    imagePaths = get_image_filenames(dataset_path)
    data = []
    
    for (i, imagePath) in enumerate(imagePaths):
        # load the input image and convert it from RGB (OpenCV ordering)
        # to dlib ordering (RGB)
        if verbose:
            print("Processing image {}/{}".format(i + 1,
                len(imagePaths)))
            print(imagePath)

        image = cv2.imread(imagePath)
        
        if image is None:
            if verbose:
                print("Skipping '{}'; the file cannot be opened.".format(
                        imagePath))
            continue
        
        d = encode_image(image, imagePath, detection_method=detection_method,
                         max_resolution=max_resolution)
        data.extend(d)
        
    if verbose:
        print("Found the total of {} face boxes.".format(len(data)))

    return data

def plot_clusters(data, clusts, labelIDs, verbose=1, figsize=(10, 8)):
    # loop over the unique face integers
    for labelID in labelIDs:
        # find all indexes into the `data` array that belong to the
        # current label ID, then randomly sample a maximum of 25 indexes
        # from the set
        if verbose:
            print("Faces for face ID: {}".format(labelID))
        
        idxs = np.where(clusts == labelID)[0]
        idxs = np.random.choice(idxs, size=min(25, len(idxs)),
            replace=False)
    
        # initialize the list of faces to include in the montage
        faces = []
    
        # loop over the sampled indexes
        for i in idxs:
            # load the input image and extract the face ROI
            image = cv2.imread(data[i]["imagePath"])
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            
            shape = data[i]["shape"]
            image = cv2.resize(image, (shape[1], shape[0]))
            
            (top, right, bottom, left) = data[i]["loc"]
            face = image[top:bottom, left:right]
    
            # force resize the face ROI to 96x96 and then add it to the
            # faces montage list
            face = cv2.resize(face, (96, 96))
            faces.append(face)
    
        # create a montage using 96x96 "tiles" with 5 rows and 5 columns
        montage = build_montages(faces, (96, 96), (5, 5))[0]
        
        # show the output montage
        title = "Face ID #{}".format(labelID)
        title = "Unknown Faces" if labelID == -1 else title
        
        plt.figure(figsize=figsize)
        plt.imshow(montage)
        plt.title(title)

Parametre

Definujeme zopár základných parametrov, ako sú cesta ku dátovej množine a pod.

In [0]:
# Cesta ku priečinku, v ktorom budú obrázky.
dataset_path = "dataset"

# Metóda na detekciu tvárí: "cnn" alebo "hog".
# CNN je presnejšia, ale HoG je trochu rýchlejšia, najmä
# ak sa nepoužíva GPU na akceleráciu.
detection_method = "cnn"

# Počet paralelných vlákien, ktoré sa použijú na zhlukovanie
# (-1 znamená použiť všetky CPU).
num_jobs = -1

Úloha 1: Stiahnutie obrázkov

Ďalej budeme potrebovať obrázky ľudských tvárí, na ktoré budeme aplikovať zhlukovanie. Použijeme preto balíček googleimagesdownload pomocou ktorého si stiahneme niekoľko obrázkov zo služby Google Images – napríklad fotografie známych osobností.

Do zoznamu keywords v nasledujúcej bunke zapíšte 5 alebo 6 mien osobností, ktoré sa použijú ako kľúčové slová pri vyhľadávaní fotografií.


In [0]:
keywords = [

    
    
    # sem doplňte zoznam 5 alebo 6 mien
    
    
    
]

Pre každé z kľúčových slov teraz stiahneme niekoľko obrázkov a uložíme ich v priečinku downloads.

In [41]:
for k in keywords:
  !googleimagesdownload -k "$k" --limit=25
Item no.: 1 --> Item name = Stephen Chbosky
Evaluating...
Starting Download...
Completed Image ====> 1.stephen-chbosky.jpg
Completed Image ====> 2.stephen-chbosky-1843916.jpg
Completed Image ====> 3.stephen-chbosky.jpg
Completed Image ====> 4.Stephen_Chbosky%2C_Jericho_Panel_at_Comic_Con_SD_2006_cropped.jpg
Completed Image ====> 5.temp_file20141028-16184-lgagyy.jpeg
Completed Image ====> 6.5064ab7306f9a.image.jpg
Completed Image ====> 7.Stephen-Chbosky-4-1563220432.jpg
Completed Image ====> 8.MV5BMTYzODIyMTM0OF5BMl5BanBnXkFtZTcwMjI1MDgwOA@@._V1_.jpg
Completed Image ====> 9.62039-1.JPG
Completed Image ====> 10.stephenchbosky-640x360.jpg
Completed Image ====> 11.chb__131010201837.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 12.Chboskyphoto.jpg
Completed Image ====> 13.Stephen-Chbosky-Interview-Perks-Being-Wallflower.jpg
Completed Image ====> 14.19_chbosky.jpg
Completed Image ====> 15.A686483_list_20180105192059.jpg
Completed Image ====> 16.stephen-chbosky.jpg
Completed Image ====> 17.Stephen+Chbosky+Premiere+Lionsgate+Wonder+Ghjh1E_5NDBl.jpg
Completed Image ====> 18.stephen-chbosky-los-angeles-premiere-of-the-perks-of-being-a-wallflower-DM1HET.jpg
Completed Image ====> 19.imaginary-friend-2.jpg
Completed Image ====> 20.StephenChboskyvitaminwaterHostsOfficialfEMB55vzgYpl.jpg
Completed Image ====> 21.wonder-film-premiere-tokyo-japan-shutterstock-editorial-9697679o.jpg
Completed Image ====> 22.wonder-movie-bts-stephen-chbosky.jpg
Completed Image ====> 23.9781451696196.jpg
Completed Image ====> 24.8e0605ff0eaf76cf76435c3da7ef9ddb_400x400.jpeg
Completed Image ====> 25.stephen_a.jpg

Errors: 1


Everything downloaded!
Total errors: 1
Total time taken: 7.2344138622283936 Seconds

Item no.: 1 --> Item name = J. K. Rowling
Evaluating...
Starting Download...
Completed Image ====> 1.JK-Rowling-1024x721.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 2.JKRowling_Shot_D_029_V3_lower-300x170.jpg
Completed Image ====> 3.104979551-GettyImages-623391110.jpg
Completed Image ====> 4.2019_06_01_73727_1559403221._large.jpg
Completed Image ====> 5.s2xAjcwj_400x400.png
Completed Image ====> 6.jk-rowling-40998-1-402.jpg
Completed Image ====> 7.20190506204025-GettyImages-1061228894-crop.jpeg
Completed Image ====> 8.563592-gettyimages-1067071942_0.jpg
Completed Image ====> 9.jk-rowling.jpg
Completed Image ====> 10.GettyImages-950047366.jpg
Completed Image ====> 11.uploads%252Fcard%252Fimage%252F804559%252Ffc8c28cf-a490-4908-a397-ce881c1f91f8.jpg
Completed Image ====> 12.j-k-rowling-gty-ml-181108_hpMain_4x3_992.jpg
Completed Image ====> 13._104892510_tv050586449.jpg
Completed Image ====> 14.INLINE-JK-Rowling.jpg
Completed Image ====> 15.jk-rowling-portrait.jpg
Completed Image ====> 16.960x0.jpg
Completed Image ====> 17.567125-gettyimages-1059193966.jpg
Completed Image ====> 18.1014BTB-superJumbo.jpg
Completed Image ====> 19.gettyimages-623410768.jpg
Completed Image ====> 20.694940094001_6042691578001_6042694219001-vs.jpg
Completed Image ====> 21.gettyimages-187481772.jpg
Completed Image ====> 22.jk_2617100b.jpg
Completed Image ====> 23.gettyimages-622237608_1.jpg
Completed Image ====> 24.getty_584707664_200013332000928089_240861.jpg
Completed Image ====> 25.jk-rowling.jpg

Errors: 1


Everything downloaded!
Total errors: 1
Total time taken: 6.4857752323150635 Seconds

Item no.: 1 --> Item name = Philip Pullman
Evaluating...
Starting Download...
Completed Image ====> 1.philip-pullman-1.jpg
Completed Image ====> 2.p05jw9zx.jpg
Completed Image ====> 3.3618.jpg
Completed Image ====> 4.29wt056pullman-hir_2029574a.jpg
Completed Image ====> 5.Philip_Pullman_2005-04-16.png
Completed Image ====> 6.skynews-philip-pullman-dark-materials_4132101.jpg
Completed Image ====> 7.51874576.0.jpg
Completed Image ====> 8.philip-pullman-trans-argument-1200x800.jpg
Completed Image ====> 9.pullman_wide-e9dfa96da2ca8a5bdc6daa488c50556caf9aa558-s800-c85.jpg
Completed Image ====> 10.4099.jpg
Completed Image ====> 11.6886-177.jpg
Completed Image ====> 12.pullman_450.jpg
Completed Image ====> 13.headshots_1508507151992.1508468845.jpg
Completed Image ====> 14.2992.jpg
Completed Image ====> 15.methode%2Fsundaytimes%2Fprod%2Fweb%2Fbin%2F36e483d0-a9d5-11e7-b601-427587316988.jpg
Completed Image ====> 16.5ad767b0a3105cdce0a36d07.jpeg
Completed Image ====> 17.Header_1288827_1.1.jpg
Completed Image ====> 18.558612515.jpg
Completed Image ====> 19.original-20162-1487154809-3.jpg
Completed Image ====> 20.brown_main_2025899f.jpg
Completed Image ====> 21.p06b6m09.jpg
Completed Image ====> 22.5344829.jpg
Completed Image ====> 23.PhillipPullman_SecretCommonwealth_Announcement_ArticleCard.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 24.PP_2826742b.jpg
Completed Image ====> 25.philip-pullman-the-book-of-dust.jpg

Errors: 1


Everything downloaded!
Total errors: 1
Total time taken: 8.7462317943573 Seconds

Item no.: 1 --> Item name = Geoffrey Hinton
Evaluating...
Starting Download...
Completed Image ====> 1.2016-12-05-geoffrey-hinton.jpg
Completed Image ====> 2.Geoffrey_Hinton_at_UBC.jpg
Completed Image ====> 3.fQGCUSt0.jpg
Completed Image ====> 4.geoffrey-hinton-3.jpg
Completed Image ====> 5.geoffhinton_14977197.jpg
Completed Image ====> 6.1*a3Hl_0CJ3ZP3xvknC-LAVg.jpeg
Completed Image ====> 7.geoffrey-hinton-reuters_650x400_41512437461.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 8.2017-07-04-Geoff%20Hinton_1%20%28web%20lead%29_1.jpg
Completed Image ====> 9.geoffrey-hinton.jpg
Completed Image ====> 10.HODLGeoffHinton.jpg
Completed Image ====> 11.VQ6lOOw5_400x400.jpeg
Completed Image ====> 12.220px-Deep_Thinkers_on_Deep_Learning_%28cropped_to_Geoffrey_Hinton%29.jpg
Completed Image ====> 13.maxresdefault.jpg
Completed Image ====> 14.HINTON_opener-1200x628-1515526516.jpg
Completed Image ====> 15.lmb_geoffhinton.png
Completed Image ====> 16.geoffrey_hinton.jpeg
Completed Image ====> 17.GeoffHinton-TA-h_14977198.jpg
Completed Image ====> 18.oe9zwmztm67jqtho3ywb.jpg
Completed Image ====> 19.MAR19_HINTON_POST01.jpg
Completed Image ====> 20.tp-hinton-6830-1.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 21.maxresdefault.jpg
Completed Image ====> 22.python.jpg
Completed Image ====> 23.business_geoffrey-hinton_15050473.jpg
Completed Image ====> 24.1513305524601.png
Completed Image ====> 25.verge_180326_3322_0001.0.jpg

Errors: 2


Everything downloaded!
Total errors: 2
Total time taken: 8.103859186172485 Seconds

Item no.: 1 --> Item name = George R. R. Martin
Evaluating...
Starting Download...
Completed Image ====> 1.rr-martin-got-index.jpg
Completed Image ====> 2.george-rr-martin-1566137529.jpg
Completed Image ====> 3.shutterstock_8999492b.jpg
Completed Image ====> 4.xznryeph9gfsxmpsczqx.jpg
Completed Image ====> 5.GettyImages-1035092140-54c7742.jpg
Completed Image ====> 6.intro-1566230443.jpg
Invalid or missing image format. Skipping...
Invalid or missing image format. Skipping...
Completed Image ====> 7.A-Song-of-Ice-and-Fire-author-George-RR-Martin.jpg
Completed Image ====> 8.george11280wjpg-f5535f.jpg
Completed Image ====> 9.1427723664-467403908.jpg
Completed Image ====> 10.IL75FL45ZJAMRHKANFW3JA7G64.jpg
Completed Image ====> 11.georgerrmartin1-1828238-640x360.jpg
Completed Image ====> 12.1200px-Portrait_photoshoot_at_Worldcon_75%2C_Helsinki%2C_before_the_Hugo_Awards_%E2%80%93_George_R._R._Martin.jpg
Completed Image ====> 13.george-r-r-martin-cinema.jpg
Completed Image ====> 14.george-rr-martin-says-game-thrones-finale-doesnt-change-anything.png
Completed Image ====> 15._8cb69ec0-c255-11e9-b964-dfb37bdfef35.jpg
Completed Image ====> 16.000c9f7f-500.jpg
Completed Image ====> 17.4480.jpg
Completed Image ====> 18.george.jpg
Completed Image ====> 19.george-rr-martin-confessed-that-the-got-tv-series-wasnt-very-good-for-him-.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 20.4480.jpg
Completed Image ====> 21.Webp.net-resizeimage-10.jpg
Completed Image ====> 22.Game-of-Thrones-George-RR-Martin-1166992.jpg
Completed Image ====> 23.960x0.jpg
Completed Image ====> 24.05dca795-george-rr-martin-1200x858.jpg
Completed Image ====> 25.shutterstock_editorial_10232201g_huge.jpg

Errors: 3


Everything downloaded!
Total errors: 3
Total time taken: 5.212132215499878 Seconds

Item no.: 1 --> Item name = Patrick Rothfuss
Evaluating...
Starting Download...
Completed Image ====> 1.Patrick-rothfuss-2014-kyle-cassidy.jpg
Completed Image ====> 2.636446107036801758-1508424147276.jpg
Completed Image ====> 3.108424.jpg
Completed Image ====> 4.1508424147506.jpeg
Completed Image ====> 5.rothfuss.jpg
Completed Image ====> 6.maxresdefault.jpg
Completed Image ====> 7.PatRothfussGnome.JPG
URLError on an image...trying next one... Error: <urlopen error _ssl.c:835: The handshake operation timed out>
Completed Image ====> 8.rothfuss-inline.jpg
Completed Image ====> 9.480px-patrick-rothfuss.jpg
Completed Image ====> 10.Patrick_Rothfuss.png
Completed Image ====> 11.Patrick_Rothfuss_39252724390.jpg
Completed Image ====> 12.Patrick-Rothfuss-Interview-02.1.jpg
Completed Image ====> 13.the-slow-regard-of-silent-things-patrick-rothfuss-book-review.png
Completed Image ====> 14.Issue08_Rothfuss_200x289.jpg
Completed Image ====> 15.rothfuss-ft.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 16.author-spotlight-patrick-rothfuss-paperback-paris.jpg
Completed Image ====> 17.patrick.jpg
Invalid or missing image format. Skipping...
Completed Image ====> 18.Joss-whedon-t-shirt-photo-Smaller.jpg
Completed Image ====> 19.patrick-rothfuss.jpg
Completed Image ====> 20.miranda-rothfuss.jpg
Completed Image ====> 21.author_patrick_rothfuss_-_h_2015.jpg
Completed Image ====> 22.DuLTFQqXgAIH8aE.jpg
Completed Image ====> 23.1-8.png
Completed Image ====> 24.10458882_10202696203858658_5783387950055269_n-600x350.jpg
Completed Image ====> 25.home_photo.jpg

Errors: 3


Everything downloaded!
Total errors: 3
Total time taken: 15.510260343551636 Seconds

Ďalej prejdeme všetky novostiahnuté podpriečinky priečinka downloads a súbory kopírujeme do priečinka dataset, odkiaľ ich budeme následne čítať.

In [0]:
dataset_path = "dataset"

shutil.rmtree(dataset_path, ignore_errors=True)
os.makedirs(dataset_path)

for root, dirs, files in os.walk('downloads'):
   for file in files:
      src_file = os.path.join(root, file)
      dst_file = os.path.join(dataset_path, file)
      shutil.copy2(src_file, dst_file)

Extrakcia a transformácia tvárí

Keďže obrázky, ktoré v príklade používame, sú stiahnuté priamo z Google Images, budú obsahovať nielen ľudské tváre, ale aj celé osoby a ďalšie objekty. Musíme z nich preto nejakou metódou tváre extrahovať. Ak sme vyššie zvolili možnosť detection_method = "cnn", použije sa na to metóda založená na konvolučných neurónových sieťach.

V ďalšom kroku použijeme inú hlbokú sieť na transformáciu obrázkov tvárí do novej reprezentácie, ktorá lepšie vyjadruje podobnosti a rozdiely medzi ľudskými tvárami, než by to dokázala surová pixelová reprezentácia. Sieť je predtrénovaná na dátovej množine v s veľkým množstvom tvárí, a to podľa schémy na obrázku.

image.png

Face recognition with OpenCV, Python, and deep learning

Každá tvár sa transformuje na 128-rozmerný vektor. Počas učenia sa sieti prezentujú trojice obrázkov. Dva z nich predstavujú dve odlišné fotografie tej istej osoby, zatiaľ čo tretí obrázok inej osoby. Úlohou siete je minimalizovať rozdiel medzi reprezentáciou fotografií toho isté človeka a maximalizovať rozdiel oproti fotografii inej osoby.

In [43]:
data = encode_dataset(dataset_path, detection_method)
Processing image 1/197
dataset/22.DuLTFQqXgAIH8aE.jpg
Processing image 2/197
dataset/2.stephen-chbosky-1843916.jpg
Processing image 3/197
dataset/9.Patweb.jpg
Processing image 4/197
dataset/20.StephenChboskyvitaminwaterHostsOfficialfEMB55vzgYpl.jpg
Processing image 5/197
dataset/8.rothfuss-inline.jpg
Processing image 6/197
dataset/18.stephen-chbosky-2232-24-03-2017-15-43-53.jpg
Processing image 7/197
dataset/5.Philip_Pullman_2005-04-16.png
Processing image 8/197
dataset/24.PP_2826742b.jpg
Processing image 9/197
dataset/12.Patrick_Rothfuss_39252724390.jpg
Processing image 10/197
dataset/25.home_photo.jpg
Processing image 11/197
dataset/25.verge_180326_3322_0001.0.jpg
Processing image 12/197
dataset/1.Patrick-rothfuss-2014-kyle-cassidy.jpg
Processing image 13/197
dataset/12.george-r-r-martin-cinema.jpg
Processing image 14/197
dataset/9.geoffrey-hinton.jpg
Processing image 15/197
dataset/4.29wt056pullman-hir_2029574a.jpg
Processing image 16/197
dataset/15.methode%2Fsundaytimes%2Fprod%2Fweb%2Fbin%2F36e483d0-a9d5-11e7-b601-427587316988.jpg
Processing image 17/197
dataset/11.6886-177.jpg
Processing image 18/197
dataset/10.4099.jpg
Processing image 19/197
dataset/12.Patrick-Rothfuss-Interview-02.1.jpg
Processing image 20/197
dataset/3.3618.jpg
Processing image 21/197
dataset/10.IL75FL45ZJAMRHKANFW3JA7G64.jpg
Processing image 22/197
dataset/13._104892510_tv050586449.jpg
Processing image 23/197
dataset/14.HINTON_opener-1200x628-1515526516.jpg
Processing image 24/197
dataset/7.51874576.0.jpg
Processing image 25/197
dataset/3.fQGCUSt0.jpg
Processing image 26/197
dataset/13.INLINE-JK-Rowling.jpg
Processing image 27/197
dataset/15.lmb_geoffhinton.png
Processing image 28/197
dataset/24.8e0605ff0eaf76cf76435c3da7ef9ddb_400x400.jpeg
Processing image 29/197
dataset/20.brown_main_2025899f.jpg
Processing image 30/197
dataset/23.getty_584707664_200013332000928089_240861.jpg
Processing image 31/197
dataset/11.georgerrmartin1-1828238-640x360.jpg
Processing image 32/197
dataset/15.the-slow-regard-of-silent-things-patrick-rothfuss-book-review.png
Processing image 33/197
dataset/10.uploads%252Fcard%252Fimage%252F804559%252Ffc8c28cf-a490-4908-a397-ce881c1f91f8.jpg
Processing image 34/197
dataset/12.220px-Deep_Thinkers_on_Deep_Learning_%28cropped_to_Geoffrey_Hinton%29.jpg
Processing image 35/197
dataset/6.maxresdefault.jpg
Processing image 36/197
dataset/11.j-k-rowling-gty-ml-181108_hpMain_4x3_992.jpg
Processing image 37/197
dataset/24.1513305524601.png
Processing image 38/197
dataset/18.george.jpg
Processing image 39/197
dataset/12.Chboskyphoto.jpg
Processing image 40/197
dataset/23.9781451696196.jpg
Processing image 41/197
dataset/6.jk-rowling-40998-1-402.jpg
Processing image 42/197
dataset/21.author_patrick_rothfuss_-_h_2015.jpg
Processing image 43/197
dataset/2.p05jw9zx.jpg
Processing image 44/197
dataset/13.Stephen-Chbosky-Interview-Perks-Being-Wallflower.jpg
Processing image 45/197
dataset/20.patrick-rothfuss.jpg
Processing image 46/197
dataset/13.george-rr-martin-says-game-thrones-finale-doesnt-change-anything.png
Processing image 47/197
dataset/16.rothfuss-ft.jpg
Processing image 48/197
dataset/16.960x0.jpg
Processing image 49/197
dataset/23.960x0.jpg
Processing image 50/197
dataset/17.Stephen+Chbosky+Premiere+Lionsgate+Wonder+Ghjh1E_5NDBl.jpg
Processing image 51/197
dataset/5.intro-1566230443.jpg
Processing image 52/197
dataset/18.558612515.jpg
Processing image 53/197
dataset/9.GettyImages-950047366.jpg
Processing image 54/197
dataset/8.george11280wjpg-f5535f.jpg
Processing image 55/197
dataset/1.2016-12-05-geoffrey-hinton.jpg
Processing image 56/197
dataset/24.9781451696196.jpg
Processing image 57/197
dataset/1.JK-Rowling-1024x721.jpg
Processing image 58/197
dataset/14.s2xAjcwj_400x400.png
Processing image 59/197
dataset/2.Geoffrey_Hinton_at_UBC.jpg
Processing image 60/197
dataset/15.A686483_list_20180105192059.jpg
Processing image 61/197
dataset/11.uploads%252Fcard%252Fimage%252F804559%252Ffc8c28cf-a490-4908-a397-ce881c1f91f8.jpg
Processing image 62/197
dataset/21.p06b6m09.jpg
Processing image 63/197
dataset/3.104979551-GettyImages-623391110.jpg
Processing image 64/197
dataset/4.1508424147506.jpeg
Processing image 65/197
dataset/24.getty_584707664_200013332000928089_240861.jpg
Processing image 66/197
dataset/2.george-rr-martin-1566137529.jpg
Processing image 67/197
dataset/8.philip-pullman-trans-argument-1200x800.jpg
Processing image 68/197
dataset/15.000c9f7f-500.jpg
Processing image 69/197
dataset/11.VQ6lOOw5_400x400.jpeg
Processing image 70/197
dataset/19.george-rr-martin-confessed-that-the-got-tv-series-wasnt-very-good-for-him-.jpg
Processing image 71/197
dataset/14.george-rr-martin-says-game-thrones-finale-doesnt-change-anything.png
Processing image 72/197
dataset/9.pullman_wide-e9dfa96da2ca8a5bdc6daa488c50556caf9aa558-s800-c85.jpg
Processing image 73/197
dataset/10.george11280wjpg-f5535f.jpg
Processing image 74/197
dataset/23.PhillipPullman_SecretCommonwealth_Announcement_ArticleCard.jpg
Processing image 75/197
dataset/20.4480.jpg
Processing image 76/197
dataset/19.gettyimages-623410768.jpg
Processing image 77/197
dataset/17.GeoffHinton-TA-h_14977198.jpg
Processing image 78/197
dataset/15.stephen-chbosky.jpg
Processing image 79/197
dataset/16.geoffrey_hinton.jpeg
Processing image 80/197
dataset/21.gettyimages-187481772.jpg
Processing image 81/197
dataset/5.rothfuss.jpg
Processing image 82/197
dataset/18.oe9zwmztm67jqtho3ywb.jpg
Processing image 83/197
dataset/20.imaginary-friend-2.jpg
Processing image 84/197
dataset/14.2992.jpg
Processing image 85/197
dataset/8.MV5BMTYzODIyMTM0OF5BMl5BanBnXkFtZTcwMjI1MDgwOA@@._V1_.jpg
Processing image 86/197
dataset/25.jk-rowling.jpg
Processing image 87/197
dataset/6.skynews-philip-pullman-dark-materials_4132101.jpg
Processing image 88/197
dataset/21.Webp.net-resizeimage-10.jpg
Processing image 89/197
dataset/18.1014BTB-superJumbo.jpg
Processing image 90/197
dataset/9.1427723664-467403908.jpg
Processing image 91/197
dataset/13.maxresdefault.jpg
Processing image 92/197
dataset/7.Stephen-Chbosky-4-1563220432.jpg
Processing image 93/197
dataset/5.GettyImages-1035092140-54c7742.jpg
Processing image 94/197
dataset/15._8cb69ec0-c255-11e9-b964-dfb37bdfef35.jpg
Processing image 95/197
dataset/21.StephenChboskyvitaminwaterHostsOfficialfEMB55vzgYpl.jpg
Processing image 96/197
dataset/11.chb__131010201837.jpg
Processing image 97/197
dataset/2.636446107036801758-1508424147276.jpg
Processing image 98/197
dataset/9.IL75FL45ZJAMRHKANFW3JA7G64.jpg
Processing image 99/197
dataset/25.philip-pullman-the-book-of-dust.jpg
Processing image 100/197
dataset/3.1508424147506.jpeg
Processing image 101/197
dataset/9.jk-rowling.jpg
Processing image 102/197
dataset/4.108424.jpg
Processing image 103/197
dataset/24.10458882_10202696203858658_5783387950055269_n-600x350.jpg
Processing image 104/197
dataset/4.2019_06_01_73727_1559403221._large.jpg
Processing image 105/197
dataset/5.jk-rowling-40998-1-402.jpg
Processing image 106/197
dataset/25.8e0605ff0eaf76cf76435c3da7ef9ddb_400x400.jpeg
Processing image 107/197
dataset/20.miranda-rothfuss.jpg
Processing image 108/197
dataset/16.stephen-chbosky.jpg
Processing image 109/197
dataset/23.author_patrick_rothfuss_-_h_2015.jpg
Processing image 110/197
dataset/8.1427723664-467403908.jpg
Processing image 111/197
dataset/6.GettyImages-1035092140-54c7742.jpg
Processing image 112/197
dataset/19.stephen-chbosky-los-angeles-premiere-of-the-perks-of-being-a-wallflower-DM1HET.jpg
Processing image 113/197
dataset/23.gettyimages-622237608_1.jpg
Processing image 114/197
dataset/7.jk-rowling.jpg
Processing image 115/197
dataset/16.000c9f7f-500.jpg
Processing image 116/197
dataset/18.patrick.jpg
Processing image 117/197
dataset/24.05dca795-george-rr-martin-1200x858.jpg
Processing image 118/197
dataset/13.george-r-r-martin-cinema.jpg
Processing image 119/197
dataset/25.1-8.png
Processing image 120/197
dataset/21.wonder-film-premiere-tokyo-japan-shutterstock-editorial-9697679o.jpg
Processing image 121/197
dataset/22.wonder-film-premiere-tokyo-japan-shutterstock-editorial-9697679o.jpg
Processing image 122/197
dataset/16.author-spotlight-patrick-rothfuss-paperback-paris.jpg
Processing image 123/197
dataset/16._8cb69ec0-c255-11e9-b964-dfb37bdfef35.jpg
Processing image 124/197
dataset/12.pullman_450.jpg
Processing image 125/197
dataset/13.the-slow-regard-of-silent-things-patrick-rothfuss-book-review.png
Processing image 126/197
dataset/11.480px-patrick-rothfuss.jpg
Processing image 127/197
dataset/17.4480.jpg
Processing image 128/197
dataset/1.rr-martin-got-index.jpg
Processing image 129/197
dataset/17.geoffrey_hinton.jpeg
Processing image 130/197
dataset/22.Game-of-Thrones-George-RR-Martin-1166992.jpg
Processing image 131/197
dataset/10.HODLGeoffHinton.jpg
Processing image 132/197
dataset/13.Patrick-Rothfuss-Interview-02.1.jpg
Processing image 133/197
dataset/13.headshots_1508507151992.1508468845.jpg
Processing image 134/197
dataset/12.j-k-rowling-gty-ml-181108_hpMain_4x3_992.jpg
Processing image 135/197
dataset/15.rothfuss-ft.jpg
Processing image 136/197
dataset/15.jk-rowling-portrait.jpg
Processing image 137/197
dataset/16.GeoffHinton-TA-h_14977198.jpg
Processing image 138/197
dataset/2.JKRowling_Shot_D_029_V3_lower-300x170.jpg
Processing image 139/197
dataset/7.A-Song-of-Ice-and-Fire-author-George-RR-Martin.jpg
Processing image 140/197
dataset/22.jk_2617100b.jpg
Processing image 141/197
dataset/19.MAR19_HINTON_POST01.jpg
Processing image 142/197
dataset/4.geoffrey-hinton-3.jpg
Processing image 143/197
dataset/22.5344829.jpg
Processing image 144/197
dataset/5.geoffhinton_14977197.jpg
Processing image 145/197
dataset/9.480px-patrick-rothfuss.jpg
Processing image 146/197
dataset/7.rothfuss-inline.jpg
Processing image 147/197
dataset/3.108424.jpg
Processing image 148/197
dataset/16.A686483_list_20180105192059.jpg
Processing image 149/197
dataset/17.567125-gettyimages-1059193966.jpg
Processing image 150/197
dataset/20.tp-hinton-6830-1.jpg
Processing image 151/197
dataset/17.patrick.jpg
Processing image 152/197
dataset/6.20190506204025-GettyImages-1061228894-crop.jpeg
Processing image 153/197
dataset/14.1200px-Portrait_photoshoot_at_Worldcon_75%2C_Helsinki%2C_before_the_Hugo_Awards_%E2%80%93_George_R._R._Martin.jpg
Processing image 154/197
dataset/22.wonder-movie-bts-stephen-chbosky.jpg
Processing image 155/197
dataset/19.Joss-whedon-t-shirt-photo-Smaller.jpg
Processing image 156/197
dataset/3.shutterstock_8999492b.jpg
Processing image 157/197
dataset/3.stephen-chbosky.jpg
Processing image 158/197
dataset/21.miranda-rothfuss.jpg
Processing image 159/197
dataset/7.geoffrey-hinton-reuters_650x400_41512437461.jpg
Processing image 160/197
dataset/8.2017-07-04-Geoff%20Hinton_1%20%28web%20lead%29_1.jpg
Processing image 161/197
dataset/23.1-8.png
Processing image 162/197
dataset/6.intro-1566230443.jpg
Processing image 163/197
dataset/1.philip-pullman-1.jpg
Processing image 164/197
dataset/4.xznryeph9gfsxmpsczqx.jpg
Processing image 165/197
dataset/1.stephen-chbosky.jpg
Processing image 166/197
dataset/12._104892510_tv050586449.jpg
Processing image 167/197
dataset/7.20190506204025-GettyImages-1061228894-crop.jpeg
Processing image 168/197
dataset/18.Joss-whedon-t-shirt-photo-Smaller.jpg
Processing image 169/197
dataset/10.GettyImages-950047366.jpg
Processing image 170/197
dataset/21.maxresdefault.jpg
Processing image 171/197
dataset/8.563592-gettyimages-1067071942_0.jpg
Processing image 172/197
dataset/14.INLINE-JK-Rowling.jpg
Processing image 173/197
dataset/4.Stephen_Chbosky%2C_Jericho_Panel_at_Comic_Con_SD_2006_cropped.jpg
Processing image 174/197
dataset/10.Patrick_Rothfuss.png
Processing image 175/197
dataset/25.shutterstock_editorial_10232201g_huge.jpg
Processing image 176/197
dataset/11.Patrick_Rothfuss_39252724390.jpg
Processing image 177/197
dataset/19.original-20162-1487154809-3.jpg
Processing image 178/197
dataset/5.temp_file20141028-16184-lgagyy.jpeg
Processing image 179/197
dataset/18.stephen-chbosky-los-angeles-premiere-of-the-perks-of-being-a-wallflower-DM1HET.jpg
Processing image 180/197
dataset/23.business_geoffrey-hinton_15050473.jpg
Processing image 181/197
dataset/14.Issue08_Rothfuss_200x289.jpg
Processing image 182/197
dataset/20.694940094001_6042691578001_6042694219001-vs.jpg
Processing image 183/197
dataset/5.s2xAjcwj_400x400.png
Processing image 184/197
dataset/17.Header_1288827_1.1.jpg
Processing image 185/197
dataset/23.wonder-movie-bts-stephen-chbosky.jpg
Processing image 186/197
dataset/25.stephen_a.jpg
Processing image 187/197
dataset/22.python.jpg
Processing image 188/197
dataset/10.stephenchbosky-640x360.jpg
Processing image 189/197
dataset/19.patrick-rothfuss.jpg
Processing image 190/197
dataset/16.5ad767b0a3105cdce0a36d07.jpeg
Processing image 191/197
dataset/14.19_chbosky.jpg
Processing image 192/197
dataset/6.5064ab7306f9a.image.jpg
Processing image 193/197
dataset/12.1200px-Portrait_photoshoot_at_Worldcon_75%2C_Helsinki%2C_before_the_Hugo_Awards_%E2%80%93_George_R._R._Martin.jpg
Processing image 194/197
dataset/17.author-spotlight-patrick-rothfuss-paperback-paris.jpg
Processing image 195/197
dataset/24.gettyimages-622237608_1.jpg
Processing image 196/197
dataset/6.1*a3Hl_0CJ3ZP3xvknC-LAVg.jpeg
Processing image 197/197
dataset/19.imaginary-friend-2.jpg
Found the total of 211 face boxes.

Platí, že na extrahovaných obrázkoch sa môžu vyskytovať aj tváre iných osôb, než sme predpokladali (na pôvodných fotografiách mohli byť aj ďalší ľudia). Niektoré tváre môžu byť extrahované chybne, alebo sieť môže omylom namiesto tváre extrahovať inú časť fotografie. Uvidíme, ako sa s tým sieť extrahujúca 128-rozmerné reprezentácie vysporiada.

Následne z predspracovaných dát extrahujeme len vypočítané reprezentácie tvárí.

In [0]:
data = np.array(data)
encodings = [d["encoding"] for d in data]

Úloha 2: Zhlukovanie

Vykonajte zhlukovanie na poli encodings, napr. pomocou metódy DBSCAN. Výsledné čísla zhlukov priraďte do premennej clusts.


In [0]:
# vykonajte zhlukovanie


clusts =      # sem priraďte čísla zhlukov

Zobrazenie výsledkov

Nakoniec vizualizujeme tváre patriace do jednotlivých zhlukov. Prvý obrázok predstavuje tváre, ktoré nepatria do žiadneho zhluku.

In [72]:
labelIDs = np.unique(clusts)
numUniqueFaces = len(np.where(labelIDs > -1)[0])
print("Počet rozličných tvárí: {}".format(numUniqueFaces))
print("Fotografie sme hľadali podľa {} rozličných kľúčových slov.".format(len(keywords)))
Počet rozličných tvárí: 6
Fotografie sme hľadali podľa 6 rozličných kľúčových slov.
In [73]:
plot_clusters(data, clusts, labelIDs, verbose=0)

Úloha 3: Znižovanie rozmeru pomocou UMAP

Použite metódu UMAP, aby ste znížili rozmer dát v poli encodings zo 128 na 2 – aby sa dáta dali vizualizovať. Vhodným nastavením argumentov min_dist a spread zabezpečte, aby mal obrázok dobrú výpovednú hodnotu (t.j. všetky tváre sa nebudú navzájom úplne prekrývať). Výsledné dáta uložte do poľa s názvom embeds.


In [0]:
# vykonajte znižovanie rozmeru dát




embeds =          # sem priraďte dáta so zníženým rozmerom

Dáta zníženého rozmeru normalizujeme do rozsahu [0, 1].

In [0]:
embeds -= embeds.min(axis=0)
embeds /= embeds.max(axis=0)

Definujeme metódu plot_faces, ktorá vykreslí príslušné tváre na zadaných pozíciách.

In [0]:
def plot_faces(data, poses, w=0.08, h=0.08, ax=None):
  ax = plt.gca()
  
  for i in range(len(data)):
    try:
        image = cv2.imread(data[i]['imagePath'])
        shape = data[i]["shape"]
        image = cv2.resize(image, (shape[1], shape[0]))
        
        (top, right, bottom, left) = data[i]["loc"]
        face = image[top:bottom, left:right]
        face = cv2.resize(face, (96, 96))
        face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
        
        pos = poses[i]
        ax.imshow(face, extent=[pos[0] - w/2, pos[0] + w/2,
                                 pos[1] - h/2, pos[1] + h/2])
    except:
        pass
  
  plt.xlim([np.min(poses[:, 0]) - w, np.max(poses[:, 0]) + w])
  plt.ylim([np.min(poses[:, 1]) - h, np.max(poses[:, 1]) + h])

Pomocou metódy plot_faces vykreslíme tváre.

In [77]:
plt.figure(figsize=(10, 8))
plot_faces(data, embeds)
plt.xlabel('$d_1$')
plt.ylabel('$d_2$')
Out[77]:
Text(0, 0.5, '$d_2$')

Zobrazenie v mriežke pomocou algoritmu Jonker-Volgenant

V rámci vizualizácie vytvorenej pomocou t-SNE vidno vzdialenosti medzi zhlukmi tvárí a podobne. Obrázky sa však navzájom prekrývajú, čo robí obrázok ťažko čitateľným. Preto pozície skúsime premietnuť do pravidelnej mriežky pomocou algoritmu Jonker-Volgenant.

In [0]:
sqrt_size = int(np.ceil(np.sqrt(len(embeds))))
size = sqrt_size * sqrt_size
grid = np.dstack(np.meshgrid(np.linspace(0, 1, sqrt_size), np.linspace(0, 1, sqrt_size))).reshape(-1, 2)

padded_embeds = np.zeros((size, embeds.shape[1]))
padded_embeds[:embeds.shape[0], :] = embeds

cost_matrix = cdist(grid, padded_embeds, "sqeuclidean").astype(np.float32)
cost_matrix = cost_matrix * (100000 / cost_matrix.max())
row_as, col_as, _ = lapjv(cost_matrix)
grid_jv = grid[col_as]

Nové pozície fotografií uložené v poli grid_jv použijeme na vykreslenie.

In [79]:
plt.figure(figsize=(12, 12))
plot_faces(data, grid_jv)
plt.axis('off')
Out[79]:
(-0.08, 1.08, -0.08, 1.08)
In [0]: