{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os, cv2, random\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import ticker\nimport seaborn as sns\n%matplotlib inline \nimport warnings  \nwarnings.filterwarnings('ignore')\nfrom keras import backend as K\nfrom keras.models import Sequential, Model\nfrom keras.layers import Input, Dropout, Flatten, Conv2D, MaxPooling2D, Dense, Activation\nfrom keras.optimizers import RMSprop, Adam, SGD\nfrom keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\nfrom keras.utils import np_utils","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Realių duomenų iš \"Dogs vs. Cats: Kernels Edition\" užkrovimas į temp direktoriją\nimport zipfile\nimport glob\n\nif not os.path.exists('../working/temp'):\n    print('Kuriama temp direktorija..')\n    zip_file = glob.glob('../input/*/*.zip')\n    print(zip_file)\n\n    def extract_zip(file):\n        with zipfile.ZipFile(file,\"r\") as zip_ref:\n            zip_ref.extractall(\"temp\")\n\n    for files in zip_file:\n        extract_zip(files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"synth_train_dir = '../input/synth3000/synth3000/'\nsmall_real_train_dir = '../input/my-real/real_images/'\nreal_train_dir = '../working/temp/train/'\ntest_dir = '../working/temp/test/'\n\nchannels = 3\nclass_size = 3000 # Su kiek realių ir sintetinių duomenų norime apmokyti tinklą\nepochs = 200 # Klasifikavimo tinklo treniravimo epochų skaičius\n\n# Duomenys įsikeliami iš direktorijų pagal klases\ntrain_dogs = [real_train_dir+i for i in os.listdir(real_train_dir) if 'dog' in i]\ntrain_cats = [real_train_dir+i for i in os.listdir(real_train_dir) if 'cat' in i]\n\nmy_train_dogs = [small_real_train_dir+i for i in os.listdir(small_real_train_dir) if 'dog' in i]\nmy_train_cats = [small_real_train_dir+i for i in os.listdir(small_real_train_dir) if 'cat' in i]\n\nsynth_dogs = [synth_train_dir+i for i in os.listdir(synth_train_dir) if 'dog' in i]\nsynth_cats = [synth_train_dir+i for i in os.listdir(synth_train_dir) if 'cat' in i]\n\ntest_images = [test_dir+i for i in os.listdir(test_dir)]\n\n# Pasiimamas tam tikras kiekis nuotraukų pagal klasę(class_size) iš realių mokymo bei sintetinių duomenų rinkinių\ntrain_images = train_dogs[:class_size] + train_cats[:class_size]\nrandom.shuffle(train_images)\n\nsmall_train_images = my_train_dogs + my_train_cats\nrandom.shuffle(small_train_images)\n\nsynth_images = synth_dogs[:class_size] + synth_cats[:class_size]\nrandom.shuffle(synth_images)\n\ntest_images =  test_images[:1000]\nrandom.shuffle(test_images)\n\nval_images = train_dogs[4000:5000] + train_cats[4000:5000]\nrandom.shuffle(val_images)\n\ndef read_image(file_path, rows, cols):\n    image = cv2.imread(file_path, cv2.IMREAD_COLOR)\n    b,g,r = cv2.split(image)\n    new_image = cv2.merge([r,g,b])\n    return cv2.resize(new_image, (rows, cols), interpolation=cv2.INTER_CUBIC)\n\ndef prep_data(images, rows=64, cols=64):\n    count = len(images)\n    data = np.ndarray((count, channels, rows, cols), dtype=np.uint8)\n\n    for i, image_file in enumerate(images):\n        image = read_image(image_file, rows, cols)\n        data[i] = image.T\n    return data\n\nreal = prep_data(train_images)\nsmall_real = prep_data(small_train_images)\nsynth = prep_data(synth_images)\ntest = prep_data(test_images)\nshow_test = prep_data(test_images, 256, 256)\nvalidation = prep_data(val_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Susižymimi duomenys(pagal nuotraukų pavadinimą): katė - 0, šuo - 1\ndef get_labels(labels, images):\n    for i in images:\n        if 'cat' in i:\n            labels.append(0)\n        else:\n            labels.append(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = []\nsmall_labels = []\nsynth_labels = []\nval_labels = []\n\nget_labels(labels, train_images)\nget_labels(small_labels, small_train_images)\nget_labels(synth_labels, synth_images)\nget_labels(val_labels, val_images)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**KLASIFIKAVIMO TINKLAS**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras.backend.tensorflow_backend as tfback\nimport tensorflow as tf\n\ndef _get_available_gpus():  \n    if tfback._LOCAL_DEVICES is None:  \n        devices = tf.config.list_logical_devices()  \n        tfback._LOCAL_DEVICES = [x.name for x in devices]  \n    return [x for x in tfback._LOCAL_DEVICES if 'device:gpu' in x.lower()]\n\ntfback._get_available_gpus = _get_available_gpus\n\noptimizer = RMSprop(lr=1e-4)\noptimizer2 = Adam(lr=1e-4, decay=1e-6)\noptimizer3 = SGD(lr=1e-4, momentum=0.9)\n\ndef catdognet(): \n    catdog_input = Input(shape=(3, 64, 64))\n    \n    x = Conv2D(32, 3, padding='same', activation='relu')(catdog_input)\n    x = MaxPooling2D(pool_size=(2, 2), data_format=\"channels_first\")(x)\n    x = Dropout(0.4)(x)\n    \n    x = Conv2D(64, 3, padding='same', activation='relu')(x)\n    x = MaxPooling2D(pool_size=(2, 2), data_format=\"channels_first\")(x)\n    x = Dropout(0.4)(x)\n    \n    x = Conv2D(128, 3, padding='same', activation='relu')(x)\n    x = MaxPooling2D(pool_size=(2, 2), data_format=\"channels_first\")(x)\n    x = Dropout(0.4)(x)\n\n    x = Flatten()(x)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.7)(x)\n    catdog_output = Dense(1, activation='sigmoid')(x)\n    \n    catdog = Model(inputs=catdog_input, outputs=catdog_output, name='Klasifikatorius')\n    catdog.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=optimizer)\n    return catdog","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = catdognet()\nsmall_model = catdognet()\nsynth_model = catdognet()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class LossAccHistory(Callback):\n    def on_train_begin(self, logs={}):\n        self.losses = []\n        self.val_losses = []\n        self.acc = []\n        self.val_acc = []\n        \n    def on_epoch_end(self, batch, logs={}):\n        self.losses.append(logs.get('loss'))\n        self.val_losses.append(logs.get('val_loss'))\n        self.acc.append(logs.get('accuracy'))\n        self.val_acc.append(logs.get('val_accuracy'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def run_catdognet(model, real, labels, batch_size=32):\n    history = LossAccHistory()\n    model.fit(real, labels, batch_size=batch_size, epochs=epochs, validation_data=(validation, val_labels), verbose=2, shuffle=True, callbacks=[history])\n    predictions = model.predict(test, verbose=0)\n    return predictions, history","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**KLASIFIKAVIMO TINKLO MOKYMAS SU REALIAIS DUOMENIMIS (6000) IŠ DOGS VS. CATS**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions, history = run_catdognet(model, real, labels, 128)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**REZULTATAI:**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"loss = history.losses\nval_loss = history.val_losses\nacc = history.acc\nval_acc = history.val_acc\n\nprint('Aukščiausias pasiektas klasifikavimo tikslumas:', max(val_acc))\n\nplt.xlabel('Epocha')\nplt.ylabel('Tikslumas')\nplt.title('Tikslumo raida su realiais duomenimis')\nplt.plot(acc, 'mediumvioletred', label='Mokymosi')\nplt.plot(val_acc, 'lightseagreen', label='Validacijos')\nplt.xticks(range(0,epochs)[1::20])\nplt.legend()\nplt.show()\n\nplt.xlabel('Epocha')\nplt.ylabel('Nuostolis')\nplt.title('Nuostolių raida su realiais duomenimis')\nplt.plot(loss, 'mediumvioletred', label='Mokymosi')\nplt.plot(val_loss, 'lightseagreen', label='Validacijos')\nplt.xticks(range(0,epochs)[1::20])\nplt.legend()\nplt.ylim(0.6, 1.05)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**SPĖJIMAI:**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,10):\n    if predictions[i, 0] >= 0.5: \n        print('{:.2%} jog tai yra šuo'.format(predictions[i][0]))\n    else: \n        print('{:.2%} jog tai yra katė'.format(1-predictions[i][0]))\n        \n    plt.imshow(show_test[i].T)\n    plt.axis('off')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**KLASIFIKAVIMO TINKLO MOKYMAS SU SINTETINIAIS DUOMENIMIS (6000)**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"s_predictions, s_history = run_catdognet(synth_model, synth, synth_labels, 128)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**REZULTATAI:**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"loss = s_history.losses\nval_loss = s_history.val_losses\nacc = s_history.acc\nval_acc = s_history.val_acc\n\nprint('Aukščiausias pasiektas klasifikavimo tikslumas:', max(val_acc))\n\nplt.xlabel('Epocha')\nplt.ylabel('Tikslumas')\nplt.title('Tikslumo raida su sintetiniais duomenimis')\nplt.plot(acc, 'mediumvioletred', label='Mokymosi')\nplt.plot(val_acc, 'lightseagreen', label='Validacijos')\nplt.xticks(range(0,epochs)[1::20])\nplt.legend()\nplt.show()\n\nplt.xlabel('Epocha')\nplt.ylabel('Nuostolis')\nplt.title('Nuostolių raida su sintetiniais duomenimis')\nplt.plot(loss, 'mediumvioletred', label='Mokymosi')\nplt.plot(val_loss, 'lightseagreen', label='Validacijos')\nplt.xticks(range(0,epochs)[1::20])\nplt.legend()\nplt.ylim(0.1, 1.05)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**SPĖJIMAI:**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,10):\n    if s_predictions[i, 0] >= 0.5: \n        print('{:.2%} jog tai yra šuo'.format(s_predictions[i][0]))\n    else: \n        print('{:.2%} jog tai yra katė'.format(1-s_predictions[i][0]))\n        \n    plt.imshow(show_test[i].T)\n    plt.axis('off')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**KLASIFIKAVIMO TINKLO MOKYMAS SU PRADINIAIS REALIAIS DUOMENIMIS (20)**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"r_predictions, r_history = run_catdognet(small_model, small_real, small_labels, 16)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**REZULTATAI:**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"loss = r_history.losses\nval_loss = r_history.val_losses\nacc = r_history.acc\nval_acc = r_history.val_acc\n\nprint('Aukščiausias pasiektas klasifikavimo tikslumas:', max(val_acc))\n\nplt.xlabel('Epocha')\nplt.ylabel('Tikslumas')\nplt.title('Tikslumo raida su pradiniu realių duomenų rinkiniu')\nplt.plot(acc, 'mediumvioletred', label='Mokymosi')\nplt.plot(val_acc, 'lightseagreen', label='Validacijos')\nplt.xticks(range(0,epochs)[1::20])\nplt.legend()\nplt.ylim(0, 1.05)\nplt.show()\n\nplt.xlabel('Epocha')\nplt.ylabel('Nuostolis')\nplt.title('Nuostolių raida su pradiniu realių duomenų rinkiniu')\nplt.plot(loss, 'mediumvioletred', label='Mokymosi')\nplt.plot(val_loss, 'lightseagreen', label='Validacijos')\nplt.xticks(range(0,epochs)[1::20])\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**SPĖJIMAI:**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,10):\n    if r_predictions[i, 0] >= 0.5: \n        print('{:.2%} jog tai yra šuo'.format(r_predictions[i][0]))\n    else: \n        print('{:.2%} jog tai yra katė'.format(1-r_predictions[i][0]))\n        \n    plt.imshow(show_test[i].T)\n    plt.axis('off')\n    plt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}