{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":791828,"sourceType":"datasetVersion","datasetId":119698},{"sourceId":7223251,"sourceType":"datasetVersion","datasetId":4181101}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Rozpoznávanie obrazu - Psie plemená\n### Dáta sa skladajú z :\n* train.zip - tréningová množina, pri týchto psoch sú uvedené plemená.\n* test.zip - testovacia množina, musíte predpovedať pravdepodobnosť každého plemena pre každý obrázok\n* sample_submission.csv - vzorový súbor predloženia v správnom formáte\n* labels.csv - plemená pre obrázky v trénovanom súbore\n\n### Vlastnosti údajov :\n* Pracujeme s neštruktúrovaným súborom údajov.\n* V trénovaných údajoch je 10000+ obrázkov psov.\n* V testovacích údajoch je 10000+ obrázkov psov.\n\nKeďže máme neštruktúrované údaje, musíme pracovať s knižnicou, ako je tensorflow","metadata":{}},{"cell_type":"code","source":"#Importovanie knižníc\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nfrom tqdm.autonotebook import tqdm\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Model\nimport numpy as np\nimport pandas as pd \nimport os\nimport gc\nimport time ","metadata":{"execution":{"iopub.status.busy":"2024-03-25T17:01:57.581248Z","iopub.execute_input":"2024-03-25T17:01:57.581951Z","iopub.status.idle":"2024-03-25T17:01:57.587128Z","shell.execute_reply.started":"2024-03-25T17:01:57.581920Z","shell.execute_reply":"2024-03-25T17:01:57.586087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Keras importy\nfrom keras import Sequential\nfrom keras.callbacks import EarlyStopping\nfrom keras.optimizers import Adam, SGD\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout\nfrom keras.layers import Lambda, Input, GlobalAveragePooling2D,BatchNormalization\nfrom keras.utils import to_categorical\nfrom keras.preprocessing.image import load_img","metadata":{"execution":{"iopub.status.busy":"2024-03-25T17:01:54.145836Z","iopub.execute_input":"2024-03-25T17:01:54.146722Z","iopub.status.idle":"2024-03-25T17:01:54.152059Z","shell.execute_reply.started":"2024-03-25T17:01:54.146689Z","shell.execute_reply":"2024-03-25T17:01:54.151120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Načítanie súboru s označeniami plemien pomocou pandas knižnice\ndata_labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-25T16:51:13.863431Z","iopub.execute_input":"2024-03-25T16:51:13.864278Z","iopub.status.idle":"2024-03-25T16:51:13.904256Z","shell.execute_reply.started":"2024-03-25T16:51:13.864245Z","shell.execute_reply":"2024-03-25T16:51:13.903337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_labels.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-25T16:51:15.934528Z","iopub.execute_input":"2024-03-25T16:51:15.934933Z","iopub.status.idle":"2024-03-25T16:51:15.952219Z","shell.execute_reply.started":"2024-03-25T16:51:15.934887Z","shell.execute_reply":"2024-03-25T16:51:15.951133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Vypísanie celkového počut záznamov\ndata_labels.describe()","metadata":{"execution":{"iopub.status.busy":"2024-03-25T16:51:18.593835Z","iopub.execute_input":"2024-03-25T16:51:18.594212Z","iopub.status.idle":"2024-03-25T16:51:18.624416Z","shell.execute_reply.started":"2024-03-25T16:51:18.594184Z","shell.execute_reply":"2024-03-25T16:51:18.623515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Načítanie jedného obrázka\nfrom IPython.display import display, Image\nImage(\"../input/dog-breed-identification/train/006cc3ddb9dc1bd827479569fcdc52dc.jpg\")","metadata":{"execution":{"iopub.status.busy":"2024-03-25T16:54:27.911008Z","iopub.execute_input":"2024-03-25T16:54:27.911815Z","iopub.status.idle":"2024-03-25T16:54:27.938170Z","shell.execute_reply.started":"2024-03-25T16:54:27.911781Z","shell.execute_reply":"2024-03-25T16:54:27.937164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Funcia na vypísanie počtu záznamov pre jednotlivé psie plemená\nplemenaNazovStlpca = 'breed'\n\ndef grafPlemien(ax): \n    for p in ax.patches:\n        val = p.get_width() # Nastavenie šírky podľa počtu záznamov\n        x = p.get_x()+ p.get_width()\n        y = p.get_y() + p.get_height()/2\n        ax.annotate(round(val,2),(x,y))\n        \n# Zoradenie podľa počtu záznamov\nplt.figure(figsize = (5,25))\nax0 = sns.countplot(y = data_labels[plemenaNazovStlpca],order = data_labels[plemenaNazovStlpca].value_counts().index)\ngrafPlemien(ax0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-25T16:52:38.634492Z","iopub.execute_input":"2024-03-25T16:52:38.634854Z","iopub.status.idle":"2024-03-25T16:52:40.171065Z","shell.execute_reply.started":"2024-03-25T16:52:38.634824Z","shell.execute_reply":"2024-03-25T16:52:40.170134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Zistili sme:**\n* Máme celkom 120 jedinečných plemien\n* Na každú kategóriu plemena máme aspoň 60 obrázkov","metadata":{}},{"cell_type":"code","source":"# Vytvorí list plemien v abecednom poradí\ntriedy = sorted(list(set(data_labels['breed'])))\nn_triedy = len(triedy)\n# Priradenie čísla pre každé plemeno \ntriedy_cislo = dict(zip(triedy, range(n_triedy)))","metadata":{"execution":{"iopub.status.busy":"2024-03-25T16:57:15.713210Z","iopub.execute_input":"2024-03-25T16:57:15.713577Z","iopub.status.idle":"2024-03-25T16:57:15.720066Z","shell.execute_reply.started":"2024-03-25T16:57:15.713548Z","shell.execute_reply":"2024-03-25T16:57:15.719054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"velkost_obrazku = (331,331,3)","metadata":{"execution":{"iopub.status.busy":"2024-03-25T16:58:10.895549Z","iopub.execute_input":"2024-03-25T16:58:10.896368Z","iopub.status.idle":"2024-03-25T16:58:10.900520Z","shell.execute_reply.started":"2024-03-25T16:58:10.896336Z","shell.execute_reply":"2024-03-25T16:58:10.899402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def obrazky_do_pola(directory, label_dataframe, target_size = velkost_obrazku):\n    \n    image_labels = label_dataframe['breed']\n    images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]],dtype=np.uint8) \n    y = np.zeros([len(label_dataframe),1],dtype = np.uint8)\n    \n    for ix, image_name in enumerate(tqdm(label_dataframe['id'].values)):\n        img_dir = os.path.join(directory, image_name + '.jpg')\n        img = load_img(img_dir, target_size = target_size)\n        images[ix]=img\n        del img\n        \n        dog_breed = image_labels[ix]\n        y[ix] = class_to_num[dog_breed]\n    \n    y = to_categorical(y)\n    \n    return images,y","metadata":{"execution":{"iopub.status.busy":"2024-03-25T17:00:38.723746Z","iopub.execute_input":"2024-03-25T17:00:38.724123Z","iopub.status.idle":"2024-03-25T17:00:38.731500Z","shell.execute_reply.started":"2024-03-25T17:00:38.724092Z","shell.execute_reply":"2024-03-25T17:00:38.730558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = time.time()\nX,y = obrazky_do_pola('/kaggle/input/dog-breed-identification/train', data_labels[:])\n\nprint('Nahrávanie: {}'.format(time.time() - t))","metadata":{"execution":{"iopub.status.busy":"2024-03-25T17:02:01.978655Z","iopub.execute_input":"2024-03-25T17:02:01.979325Z","iopub.status.idle":"2024-03-25T17:04:05.565622Z","shell.execute_reply.started":"2024-03-25T17:02:01.979293Z","shell.execute_reply":"2024-03-25T17:04:05.564655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# psy a ich plemená\nn = 9 # Počet obrázkov\n\nplt.figure(figsize=(15,15)) # veľkosť obrázkov\n\nfor i in range(n):\n    ax = plt.subplot(3, 3, i+1) # rozdelenie mriežky napr. 3x3\n    plt.title(triedy[np.where(y[i] == 1)[0][0]])\n    plt.imshow(X[i].astype('int32'))\n    ","metadata":{"execution":{"iopub.status.busy":"2024-03-25T17:04:05.567563Z","iopub.execute_input":"2024-03-25T17:04:05.568209Z","iopub.status.idle":"2024-03-25T17:04:08.233218Z","shell.execute_reply.started":"2024-03-25T17:04:05.568172Z","shell.execute_reply":"2024-03-25T17:04:08.232166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Vytváranie spätných volaní:\nlrr = ReduceLROnPlateau(monitor='val_acc', factor = 0.01, patience = 3, min_lr = 1e-5,verbose = 1)\nEarlyStop = EarlyStopping(monitor='val_loss', patience = 10, restore_best_weights = True)","metadata":{"execution":{"iopub.status.busy":"2024-03-21T14:02:36.943980Z","iopub.execute_input":"2024-03-21T14:02:36.944385Z","iopub.status.idle":"2024-03-21T14:02:36.949780Z","shell.execute_reply.started":"2024-03-21T14:02:36.944355Z","shell.execute_reply":"2024-03-21T14:02:36.948696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyperparametre\nbatch_size = 128 # Veľkosť testovacej vzorky\nepochs = 50 # Počet opakovaní\nrychlost_ucenia = 0.001 # Rýchlosť učenia \n# Sigmoid funkcia\nsgd=SGD(learning_rate = rychlost_ucenia,momentum = 0.9,nesterov = False) \n# Optimalizer Adam \nadam=Adam(learning_rate = rychlost_ucenia, beta_1 = 0.9, beta_2 = 0.999, epsilon = None,  amsgrad = False) ","metadata":{"execution":{"iopub.status.busy":"2024-03-21T14:02:37.932548Z","iopub.execute_input":"2024-03-21T14:02:37.933407Z","iopub.status.idle":"2024-03-21T14:02:37.941339Z","shell.execute_reply.started":"2024-03-21T14:02:37.933371Z","shell.execute_reply":"2024-03-21T14:02:37.940568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Vytvorenie modelu:","metadata":{}},{"cell_type":"code","source":"#funkcia na extrakciu vlastností zo súboru údajov pomocou daného predtrénovaného modelu\ndef ziskaj_vlastnosti(nazov_algoritmu, preprocessor, velkost_obrazku, data):\n    vstupna_vrstva = Input(velkost_obrazku)\n    preprocessor = Lambda(preprocessor)(vstupna_vrstva)\n    zakladny_model = nazov_algoritmu(weights = 'imagenet', include_top = False,\n                            velkost_obrazku = velkost_obrazku)(preprocessor)\n    priemer = GlobalAveragePooling2D()(zakladny_model)\n    extraktor_vlastností = Model(inputs = vstupna_vrstva, outputs = priemer)\n    \n    #Extrakcia vlastností\n    mapa_vlastnosti = extraktor_vlastnosti.predict(data, verbose = 1)\n    print('Mapa vlastností: ', mapa_vlastnosti.shape)\n    return mapa_vlastnosti","metadata":{"execution":{"iopub.status.busy":"2024-03-21T14:02:54.843130Z","iopub.execute_input":"2024-03-21T14:02:54.843913Z","iopub.status.idle":"2024-03-21T14:02:54.854351Z","shell.execute_reply.started":"2024-03-21T14:02:54.843876Z","shell.execute_reply":"2024-03-21T14:02:54.853437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# InceptionV3 \nfrom keras.applications.inception_v3 import InceptionV3, preprocess_input\ninception_preprocessor = preprocess_input\ninception_vlastnosti = ziskaj_vlastnosti(InceptionV3,\n                                  inception_preprocessor,\n                                  img_size, X)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Xception \nfrom keras.applications.xception import Xception, preprocess_input\nxception_preprocessor = preprocess_input\nxception_vlastnosti = ziskaj_vlastnosti(Xception,\n                                 xception_preprocessor,\n                                 img_size, X)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# NASNetLarge \nfrom keras.applications.nasnet import NASNetLarge, preprocess_input\nnasnet_preprocessor = preprocess_input\nnasnet_vlastnosti = ziskaj_vlastnosti(NASNetLarge,\n                               nasnet_preprocessor,\n                               img_size, X)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# InceptionResNetV2 \nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\ninc_resnet_preprocessor = preprocess_input\ninc_resnet_vlastnosti = ziskaj_vlastnosti(InceptionResNetV2,\n                                   inc_resnet_preprocessor,\n                                   img_size, X)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Kombinácia viacerých algoritmov a ich výsledkov trénovania do jednej mapy\nzlucene_vlastnosti = np.concatenate([\n                                inception_vlastnosti,\n                                xception_vlastnosti,\n                                nasnet_vlastnosti,\n                                inc_resnet_vlastnosti\n                                ], axis = -1) \n\nprint('Finálna mapa vlastností :', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-21T14:06:33.134012Z","iopub.execute_input":"2024-03-21T14:06:33.134672Z","iopub.status.idle":"2024-03-21T14:06:33.457096Z","shell.execute_reply.started":"2024-03-21T14:06:33.134640Z","shell.execute_reply":"2024-03-21T14:06:33.455938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Príprava hlbokej siete\nmodel = Sequential()\nmodel.add(Dropout(0.7,velkost_obrazku = (zlucene_vlastnosti.shape[1],))) # Pridanie vrstvy\nmodel.add(Dense(n_triedy,activation = 'softmax')) # Pridanie vrstvy\n\nmodel.compile(optimizer=adam,\n              loss = 'categorical_crossentropy',\n              metrics = ['accuracy'])\n\n# Trénovanie modelu \nhistory = model.fit(zlucene_vlastnosti, y,\n            batch_size = batch_size,\n            epochs = epochs,\n            validation_split = 0.2,\n            callbacks = [lrr,EarlyStop])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\npredikcie = model.predict(zlucene_vlastnosti)\n\n# Konvertovanie predikcii a skutočných hodnot na indexy tried\npredikovane_hodnoty = np.argmax(predikcie, axis = 1)\nskutocne_hodnoty = np.argmax(y, axis = 1)\n\n# Výpočet metrík\npresnost = accuracy_score(skutocne_hodnoty, predikovane_hodnoty)\nspravnost = precision_score(skutocne_hodnoty, predikovane_hodnoty, average = 'weighted')\nrecall = recall_score(skutocne_hodnoty, predikovane_hodnoty, average = 'weighted')\nf1 = f1_score(skutocne_hodnoty, predikovane_hodnoty, average = 'weighted')\n\nprint(f'Prenosť: {presnost}')\nprint(f'Správnosť: {spravnost}')\nprint(f'Recall: {recall}')\nprint(f'F1 Score: {f1}')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Funkcia na čítanie obrázkov z testovacieho adresára\ndef test_obrazky_do_pola(test_path, img_size = velkost_obrazku):\n    test_subory = [test_path + fname for fname in os.listdir(test_path)]\n\n    velkost_dat = len(test_subory)\n    images = np.zeros([velkost_dat, img_size[0], img_size[1], 3], dtype=np.uint8)\n    \n    for ix,img_dir in enumerate(tqdm(test_filenames)):\n        obrazok = load_img(img_dir, target_size = velkost_obrazku)\n        obrazky[ix] = img\n        del obrazok\n    return obrazky\n\ntestovacie_data = test_obrazky_do_pola('/kaggle/input/dog-breed-identification/test/', velkost_obrazku)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extrakcia testovacích vlastností\n\ndef extrakcia_vlasnosti(data):\n    inception_vlastnosti = ziskaj_vlastnosti(InceptionV3, inception_preprocessor, velkost_obrazku, data)\n    xception_features = ziskaj_vlastnosti(Xception, xception_preprocessor, velkost_obrazku, data)\n    nasnet_features = ziskaj_vlastnosti(NASNetLarge, nasnet_preprocessor, velkost_obrazku, data)\n    inc_resnet_features = ziskaj_vlastnosti(InceptionResNetV2, inc_resnet_preprocessor, velkost_obrazku, data)\n\n    zlucene_vlastnosti = np.concatenate([inception_vlastnosti, xception_vlastnosti, nasnet_vlastnosti, inc_resnet_vlastnosti],axis = -1)\n    \n    # Odstránenie premenných na uvolnenie pamäte\n    del inception_vlastnosti\n    del xception_vlastnosti\n    del nasnet_vlastnosti\n    del inc_resnetvlastnosti\n    gc.collect()\n    \n    print('Finálna mapa vlastností', zlucene_vlastnosti.shape)\n     \n    return final_features\n\ntestovacie_vlasnosti = extrakcia_vlasnosti(testovacie_data)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prvá predikcia\npredikcia = model.predict(testovacie_vlasnosti)\nprint(predikcia[0])\nprint(f\"Maximálna hodnota (pravdepodobnosť predikcie): {np.max(pred[0])}\") # maximálna hodnota pravdepodobnosti predpovedaná modelom\nprint(f\"Index maximálnej hodnoty: {np.argmax(predikcia[0])}\") # index miesta, kde sa vyskytuje maximálna hodnota v predpovediach[0]\nprint(f\"Predikovaná hodnota: {triedy[np.argmax(predikcia[0])]}\")\nprint(f\"Súčet hodnôt: {np.sum(predikcia[0])}\") # pretože sme v našom modeli použili aktiváciu softmax, bude sa blížiť k hodnote 1","metadata":{"execution":{"iopub.status.busy":"2024-03-25T17:27:29.242415Z","iopub.execute_input":"2024-03-25T17:27:29.243180Z","iopub.status.idle":"2024-03-25T17:27:29.285397Z","shell.execute_reply.started":"2024-03-25T17:27:29.243149Z","shell.execute_reply":"2024-03-25T17:27:29.284226Z"},"trusted":true},"execution_count":null,"outputs":[]}]}