{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This Kernel for someone want to deep dive into image classification. I use CNN for classification model. If you found this Kernel helpful please up vote it. If you have some feedback and question don't forget to comment below. \n\nI have simplier model with \n* https://www.kaggle.com/uysimty/get-start-image-classification","metadata":{}},{"cell_type":"markdown","source":"# Import Library","metadata":{"_uuid":"fe76d1d1ded592430e7548feacfa38dc42f085d9"}},{"cell_type":"code","source":"!python -v","metadata":{"execution":{"iopub.status.busy":"2022-02-10T15:43:26.82699Z","iopub.execute_input":"2022-02-10T15:43:26.827288Z","iopub.status.idle":"2022-02-10T15:43:43.845365Z","shell.execute_reply.started":"2022-02-10T15:43:26.827231Z","shell.execute_reply":"2022-02-10T15:43:43.844588Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip3 install numpy\n!pip3 install pandas\n!pip3 install keras\n!pip3 install sklearn\n!pip3 install matplotlib","metadata":{"execution":{"iopub.status.busy":"2022-02-08T14:00:47.778818Z","iopub.execute_input":"2022-02-08T14:00:47.779134Z","iopub.status.idle":"2022-02-08T14:02:54.274158Z","shell.execute_reply.started":"2022-02-08T14:00:47.779075Z","shell.execute_reply":"2022-02-08T14:02:54.27336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nprint(os.listdir(\"../input\"))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-15T19:55:20.431963Z","iopub.execute_input":"2022-04-15T19:55:20.432257Z","iopub.status.idle":"2022-04-15T19:55:22.076333Z","shell.execute_reply.started":"2022-04-15T19:55:20.432203Z","shell.execute_reply":"2022-04-15T19:55:22.075502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Constants","metadata":{}},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)\nIMAGE_CHANNELS=3","metadata":{"execution":{"iopub.status.busy":"2022-04-15T19:55:42.022529Z","iopub.execute_input":"2022-04-15T19:55:42.022822Z","iopub.status.idle":"2022-04-15T19:55:42.027082Z","shell.execute_reply.started":"2022-04-15T19:55:42.022773Z","shell.execute_reply":"2022-04-15T19:55:42.02609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Traning Data","metadata":{"_uuid":"7335a579cc0268fba5d34d6f7558f33c187eedb3"}},{"cell_type":"code","source":"trans={'ì¿¬«½∞ß¬á∩ π½¿µá':'Никольская улица','î«ß¬«óß¬¿⌐ îÑΓα«»«½¿ΓÑ¡':'Московский Метрополитен','èαáß¡á∩ »½«Θáñ∞':'Красная площадь','ü«½∞Φ«⌐ ΓÑáΓα':'Большой театр','æ¼«Γα«óá∩ »½«Θáñ¬á Panorama360':'Смотровая площадка Panorama360','æ«í«α éáß¿½¿∩ ü½áªÑ¡¡«ú«':'Собор Василия Блаженного',\n       'ö«¡Γá¡ äαπªíá ¡áα«ñ«ó':'Фонтан Дружба народов','ä«¡ß¬«⌐ ¼«¡áßΓδα∞':'Донской монастырь','èαπΓ¿µ¬«Ñ »«ñó«α∞Ñ':'Крутицкое подворье','éδßΓáó¬á ñ«ßΓ¿ªÑ¡¿⌐ ¡áα«ñ¡«ú« σ«º∩⌐ßΓóá (éäìò)':'Выставка достижений народного хозяйства (ВДНХ)','òαá¼ òα¿ßΓá æ»áß¿ΓÑ½∩':'Храм Христа Спасителя','î«ß¬«óß¬¿⌐ ú«ßπñáαßΓóÑ¡¡δ⌐ π¡¿óÑαß¿ΓÑΓ ¿¼Ñ¡¿\\xa0î.é.ï«¼«¡«ß«óá':'Московский государственный университет имени М.В.Ломоносова',\n       'æΓáñ¿«¡ ïπª¡¿¬¿':'Стадион Лужники','æ«í«α¡á∩ »½«Θáñ∞':'Соборная площадь'}","metadata":{"execution":{"iopub.status.busy":"2022-02-20T07:07:19.202488Z","iopub.execute_input":"2022-02-20T07:07:19.202766Z","iopub.status.idle":"2022-02-20T07:07:19.207749Z","shell.execute_reply.started":"2022-02-20T07:07:19.202718Z","shell.execute_reply":"2022-02-20T07:07:19.206907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"../input/dost-mid/dost\")\nprint(filenames)\nnames=[]\ncategories = []\nr_name=[]\ni=0\nfor doc in filenames:\n    d = os.listdir(\"../input/dost-mid/dost/\"+doc)\n    for filename in d:\n        name = filename.split('.')[0]\n        names.append(filename)\n        categories.append(i)\n        r_name.append(trans[doc])\n    i+=1\ndf = pd.DataFrame({\n    'filename': names,\n    'category': categories,\n    'full name':r_name\n    \n})","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-02-20T07:07:19.44522Z","iopub.execute_input":"2022-02-20T07:07:19.445471Z","iopub.status.idle":"2022-02-20T07:07:19.533086Z","shell.execute_reply.started":"2022-02-20T07:07:19.445428Z","shell.execute_reply":"2022-02-20T07:07:19.53228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans[\"î«ß¬«óß¬¿⌐ îÑΓα«»«½¿ΓÑ¡\"]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T07:07:21.181013Z","iopub.execute_input":"2022-02-20T07:07:21.181302Z","iopub.status.idle":"2022-02-20T07:07:21.188147Z","shell.execute_reply.started":"2022-02-20T07:07:21.181238Z","shell.execute_reply":"2022-02-20T07:07:21.18733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"_uuid":"915bb9ba7063ab4d5c07c542419ae119003a5f98","execution":{"iopub.status.busy":"2022-02-20T07:07:22.876573Z","iopub.execute_input":"2022-02-20T07:07:22.876852Z","iopub.status.idle":"2022-02-20T07:07:22.902911Z","shell.execute_reply.started":"2022-02-20T07:07:22.876805Z","shell.execute_reply":"2022-02-20T07:07:22.901818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"_uuid":"72bf69e817f67f5a2eaff8561217e22077248553","execution":{"iopub.status.busy":"2022-02-19T12:36:50.826479Z","iopub.execute_input":"2022-02-19T12:36:50.826783Z","iopub.status.idle":"2022-02-19T12:36:50.839849Z","shell.execute_reply.started":"2022-02-19T12:36:50.826733Z","shell.execute_reply":"2022-02-19T12:36:50.839052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### See Total In count","metadata":{"_uuid":"a999484fc35b73373fafe2253ae9db7ff46fdb90"}},{"cell_type":"code","source":"df['full name'].value_counts().plot.bar()","metadata":{"_uuid":"fa26f0bc7a6d835a24989790b20f3c6f32946f45","execution":{"iopub.status.busy":"2022-02-19T12:36:52.388358Z","iopub.execute_input":"2022-02-19T12:36:52.388649Z","iopub.status.idle":"2022-02-19T12:36:52.784176Z","shell.execute_reply.started":"2022-02-19T12:36:52.388598Z","shell.execute_reply":"2022-02-19T12:36:52.783398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From our data we have 12000 cats and 12000 dogs","metadata":{"_uuid":"3a08da58107777a1dd05c4a4bf5c484484923cac"}},{"cell_type":"markdown","source":"# See sample image","metadata":{"_uuid":"400a293df3c8499059d9175f3915187074efd971"}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for doc in filenames:\n    d = os.listdir(\"../input/dost-mid/dost/\"+doc+'/')\n    sample = random.choice(d)\n    image = load_img(\"../input/dost-mid/dost/\"+doc+'/'+sample)\n    plt.figure()\n    plt.imshow(image)\n    print(trans[doc])","metadata":{"_uuid":"602b40f7353871cb161c60b5237f0da0096b2f47","execution":{"iopub.status.busy":"2022-02-19T12:36:56.719308Z","iopub.execute_input":"2022-02-19T12:36:56.719636Z","iopub.status.idle":"2022-02-19T12:37:04.25507Z","shell.execute_reply.started":"2022-02-19T12:36:56.719584Z","shell.execute_reply":"2022-02-19T12:37:04.254368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Model\n\n<img src=\"https://i.imgur.com/ebkMGGu.jpg\" width=\"100%\"/>","metadata":{"_uuid":"b244e6b7715a04fc6df92dd6dfa3d35c473ca600"}},{"cell_type":"markdown","source":"* **Input Layer**: It represent input image data. It will reshape image into single diminsion array. Example your image is 64x64 = 4096, it will convert to (4096,1) array.\n* **Conv Layer**: This layer will extract features from image.\n* **Pooling Layer**: This layerreduce the spatial volume of input image after convolution.\n* **Fully Connected Layer**: It connect the network from a layer to another layer\n* **Output Layer**: It is the predicted values layer. ","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom keras.utils.vis_utils import plot_model\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='softmax')) # 3 because we have 3 classes\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nplot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"_uuid":"8c9f833c1441b657c779844912d0b8028218d454","execution":{"iopub.status.busy":"2022-04-15T19:57:47.282614Z","iopub.execute_input":"2022-04-15T19:57:47.282881Z","iopub.status.idle":"2022-04-15T19:57:48.865301Z","shell.execute_reply.started":"2022-04-15T19:57:47.282835Z","shell.execute_reply":"2022-04-15T19:57:48.86439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)\ntrain_df['full name'\n        ].value_counts().plot.bar()","metadata":{"_uuid":"b84836337441705eda9d2e655665ffa14d9feead","execution":{"iopub.status.busy":"2022-02-19T13:09:28.504025Z","iopub.execute_input":"2022-02-19T13:09:28.504327Z","iopub.status.idle":"2022-02-19T13:09:28.920762Z","shell.execute_reply.started":"2022-02-19T13:09:28.504273Z","shell.execute_reply":"2022-02-19T13:09:28.91983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['full name'].value_counts().plot.bar()","metadata":{"_uuid":"19cf03f9a3c39532d6e2d06bd30be49a5afd9d57","execution":{"iopub.status.busy":"2022-02-19T13:09:29.941337Z","iopub.execute_input":"2022-02-19T13:09:29.941644Z","iopub.status.idle":"2022-02-19T13:09:30.361489Z","shell.execute_reply.started":"2022-02-19T13:09:29.94159Z","shell.execute_reply":"2022-02-19T13:09:30.360456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\nbatch_size=15","metadata":{"_uuid":"ae3dec0361f0443132d0309d3b883ee80070cf9f","execution":{"iopub.status.busy":"2022-02-19T12:38:17.305979Z","iopub.execute_input":"2022-02-19T12:38:17.306265Z","iopub.status.idle":"2022-02-19T12:38:17.310219Z","shell.execute_reply.started":"2022-02-19T12:38:17.306213Z","shell.execute_reply":"2022-02-19T12:38:17.309456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Traning Generator","metadata":{"_uuid":"ff760be9104f7d9492467b8d9d3405011aa77d11"}},{"cell_type":"code","source":"validation_split=0.3\ndatagen = ImageDataGenerator(\n    rescale=1. / 255, #Значения цвета меняем на дробные показания\n    rotation_range=10, #Поворачиваем изображения при генерации выборки\n    width_shift_range=0.1, #Двигаем изображения по ширине при генерации выборки\n    height_shift_range=0.1, #Двигаем изображения по высоте при генерации выборки\n    zoom_range=0.1, #Зумируем изображения при генерации выборки\n    horizontal_flip=True, #Отключаем отзеркаливание изображений\n    fill_mode='nearest', #Заполнение пикселей вне границ ввода\n    validation_split=validation_split #Указываем разделение изображений на обучающую и тестовую выборку\n)\n\ntrain_generator = datagen.flow_from_directory(\n    \"../input/dost-mid/dost\", \n    target_size=IMAGE_SIZE, #Размер изображений\n    batch_size=batch_size, #Размер батча\n    class_mode='categorical', #Разбиение выборки по материалу\n    shuffle=True, #Перемешивание выборки\n    subset='training' # устанавливаем как набор для обучения\n)","metadata":{"_uuid":"4d1c7818703a8a4bac5c036fdea45972aa9e5e9e","execution":{"iopub.status.busy":"2022-02-19T13:03:45.76614Z","iopub.execute_input":"2022-02-19T13:03:45.766429Z","iopub.status.idle":"2022-02-19T13:03:45.895336Z","shell.execute_reply.started":"2022-02-19T13:03:45.766368Z","shell.execute_reply":"2022-02-19T13:03:45.894557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Validation Generator","metadata":{"_uuid":"859c7b2857939c19fd2e3bb32839c9f7deb5aa3f"}},{"cell_type":"code","source":"validation_generator = datagen.flow_from_directory(\n    \"../input/dost-mid/dost\", \n    target_size=IMAGE_SIZE, #Размер изображений\n    batch_size=batch_size, #Размер батча\n    class_mode='categorical', #Разбиение выборки по материалу\n    shuffle=True, #Перемешивание выборки\n    subset='validation' # устанавливаем как набор для проверки при обучении\n)","metadata":{"_uuid":"7925e16bcacc89f4484fb6fe47e54d6420af732e","execution":{"iopub.status.busy":"2022-02-19T13:04:26.134367Z","iopub.execute_input":"2022-02-19T13:04:26.134724Z","iopub.status.idle":"2022-02-19T13:04:26.243913Z","shell.execute_reply.started":"2022-02-19T13:04:26.134669Z","shell.execute_reply":"2022-02-19T13:04:26.243032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# See how our generator work","metadata":{"_uuid":"6e17fc1f002fedd60febb78fee5e81770640b909"}},{"cell_type":"code","source":"e_datagen = ImageDataGenerator(\n    rescale=1. / 255, #Значения цвета меняем на дробные показания\n    rotation_range=10, #Поворачиваем изображения при генерации выборки\n    width_shift_range=0.1, #Двигаем изображения по ширине при генерации выборки\n    height_shift_range=0.1, #Двигаем изображения по высоте при генерации выборки\n    zoom_range=0.1, #Зумируем изображения при генерации выборки\n    horizontal_flip=True, #Отключаем отзеркаливание изображений\n    fill_mode='nearest', #Заполнение пикселей вне границ ввода\n    validation_split=0.05 #Указываем разделение изображений на обучающую и тестовую выборку\n)\nexample_generator = e_datagen.flow_from_directory(\n    \"../input/dost-mid/dost\", \n    target_size=IMAGE_SIZE, #Размер изображений\n    batch_size=batch_size, #Размер батча\n    class_mode='categorical', #Разбиение выборки по материалу\n    shuffle=true, #Перемешивание выборки\n    subset='validation' # устанавливаем как набор для проверки при обучении\n)","metadata":{"_uuid":"4252cce168ab65f88e44a8ebc2672607bc852af4","execution":{"iopub.status.busy":"2022-02-19T13:05:48.731976Z","iopub.execute_input":"2022-02-19T13:05:48.732274Z","iopub.status.idle":"2022-02-19T13:05:48.844139Z","shell.execute_reply.started":"2022-02-19T13:05:48.732221Z","shell.execute_reply":"2022-02-19T13:05:48.843183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"23d923dba747f8b47dc75569244cecc6f70df321","execution":{"iopub.status.busy":"2022-02-19T13:05:51.881716Z","iopub.execute_input":"2022-02-19T13:05:51.882188Z","iopub.status.idle":"2022-02-19T13:06:05.419602Z","shell.execute_reply.started":"2022-02-19T13:05:51.882129Z","shell.execute_reply":"2022-02-19T13:06:05.418873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Seem to be nice ","metadata":{"_uuid":"810ddf1373d9db470ed48da4f30ca5a6c1274435"}},{"cell_type":"markdown","source":"# Fit Model","metadata":{"_uuid":"5cd8df64e794ed17de326b613a9819e7da977a0e"}},{"cell_type":"code","source":"total_validate","metadata":{"execution":{"iopub.status.busy":"2022-02-10T15:45:33.72239Z","iopub.execute_input":"2022-02-10T15:45:33.722678Z","iopub.status.idle":"2022-02-10T15:45:33.727688Z","shell.execute_reply.started":"2022-02-10T15:45:33.722629Z","shell.execute_reply":"2022-02-10T15:45:33.726932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FAST_RUN=0","metadata":{"execution":{"iopub.status.busy":"2022-02-10T15:45:34.012803Z","iopub.execute_input":"2022-02-10T15:45:34.01317Z","iopub.status.idle":"2022-02-10T15:45:34.017549Z","shell.execute_reply.started":"2022-02-10T15:45:34.013085Z","shell.execute_reply":"2022-02-10T15:45:34.016805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=50\nhistory_ = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=5,\n    steps_per_epoch=total_train//batch_size\n)","metadata":{"_uuid":"0836a4cc8aa0abf603e0f96573c0c4ff383ad56b","execution":{"iopub.status.busy":"2022-02-19T13:29:01.88256Z","iopub.execute_input":"2022-02-19T13:29:01.882851Z","iopub.status.idle":"2022-02-19T13:43:31.338342Z","shell.execute_reply.started":"2022-02-19T13:29:01.882804Z","shell.execute_reply":"2022-02-19T13:43:31.337543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model","metadata":{"_uuid":"aa1fbc4081ae0de2993188b2bf658a0be5bc0687"}},{"cell_type":"code","source":"from keras.models import load_model\nmodel.save('./model.h5')\nmodel = load_model('./model.h5')","metadata":{"_uuid":"67575a4decdaf79a915d23151626b784ffa82642","execution":{"iopub.status.busy":"2022-02-19T13:44:23.156775Z","iopub.execute_input":"2022-02-19T13:44:23.157055Z","iopub.status.idle":"2022-02-19T13:44:28.085941Z","shell.execute_reply.started":"2022-02-19T13:44:23.157008Z","shell.execute_reply":"2022-02-19T13:44:28.085195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Virtualize Training","metadata":{"_uuid":"1b76c0a9040bc0babf0a453e567e41e22f8a1e0e"}},{"cell_type":"markdown","source":"Первая волна обучения","metadata":{}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(history.history['acc'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"79055f2dc3e2abb47bea758e0464c86ca42ab431","execution":{"iopub.status.busy":"2022-02-19T13:45:09.839973Z","iopub.execute_input":"2022-02-19T13:45:09.840271Z","iopub.status.idle":"2022-02-19T13:45:11.414078Z","shell.execute_reply.started":"2022-02-19T13:45:09.840219Z","shell.execute_reply":"2022-02-19T13:45:11.41321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Вторая волна обучения","metadata":{}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(history_.history['loss'], color='b', label=\"Training loss\")\nax1.plot(history_.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(history_.history['acc'], color='b', label=\"Training accuracy\")\nax2.plot(history_.history['val_acc'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T13:45:53.624578Z","iopub.execute_input":"2022-02-19T13:45:53.62486Z","iopub.status.idle":"2022-02-19T13:45:55.120109Z","shell.execute_reply.started":"2022-02-19T13:45:53.624813Z","shell.execute_reply":"2022-02-19T13:45:55.119378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Testing Data","metadata":{"_uuid":"764dc66e4b2bc558f3a0f90b80bb802f5b3d45a8"}},{"cell_type":"code","source":"nb_samples = df['filename'].shape[0]","metadata":{"_uuid":"c35e70d3e1e4834dbbf840fa0ea08c049bfcd915","execution":{"iopub.status.busy":"2022-02-19T13:53:52.457362Z","iopub.execute_input":"2022-02-19T13:53:52.457689Z","iopub.status.idle":"2022-02-19T13:53:52.46133Z","shell.execute_reply.started":"2022-02-19T13:53:52.457639Z","shell.execute_reply":"2022-02-19T13:53:52.460623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{"_uuid":"2fa580afca2931ec5ce374e732d8c1789d03f2ed"}},{"cell_type":"code","source":"trans['ä«¡ß¬«⌐ ¼«¡áßΓδα∞']","metadata":{"execution":{"iopub.status.busy":"2022-02-20T07:09:47.384126Z","iopub.execute_input":"2022-02-20T07:09:47.384501Z","iopub.status.idle":"2022-02-20T07:09:47.391532Z","shell.execute_reply.started":"2022-02-20T07:09:47.384439Z","shell.execute_reply":"2022-02-20T07:09:47.390707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nos.mkdir(\"./folder\")\nos.mkdir(\"./folder/folder\")\nfolder_from = r'../input/dost-mid/dost/ä«¡ß¬«⌐ ¼«¡áßΓδα∞'\nfolder_to = r'./folder/folder'\n\nfor f in os.listdir(folder_from):\n    if os.path.isfile(os.path.join(folder_from, f)):\n        shutil.copy(os.path.join(folder_from, f), os.path.join(folder_to, f))\n    if os.path.isdir(os.path.join(folder_from, f)):\n        os.system(f'rd /S /Q {folder_to}\\\\{f}')\n        shutil.copytree(os.path.join(folder_from, f), os.path.join(folder_to, f))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T07:09:48.203165Z","iopub.execute_input":"2022-02-20T07:09:48.203452Z","iopub.status.idle":"2022-02-20T07:09:49.507015Z","shell.execute_reply.started":"2022-02-20T07:09:48.203402Z","shell.execute_reply":"2022-02-20T07:09:49.506284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_split=0\ndatagen_1 = ImageDataGenerator(\n    rescale=1. / 255, #Значения цвета меняем на дробные показания\n    rotation_range=10, #Поворачиваем изображения при генерации выборки\n    width_shift_range=0.1, #Двигаем изображения по ширине при генерации выборки\n    height_shift_range=0.1, #Двигаем изображения по высоте при генерации выборки\n    zoom_range=0.1, #Зумируем изображения при генерации выборки\n    horizontal_flip=True, #Отключаем отзеркаливание изображений\n    fill_mode='nearest', #Заполнение пикселей вне границ ввода\n    validation_split=validation_split #Указываем разделение изображений на обучающую и тестовую выборку\n)\n\n\ndemonstraition_generator = datagen_1.flow_from_directory(\n    './folder', \n    target_size=IMAGE_SIZE, #Размер изображений\n    batch_size=1, #Размер батча\n    class_mode='categorical', #Разбиение выборки по материалу\n    shuffle=False, #Перемешивание выборки\n    subset='training' # устанавливаем как набор для проверки \n)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T07:09:50.297578Z","iopub.execute_input":"2022-02-20T07:09:50.297842Z","iopub.status.idle":"2022-02-20T07:09:50.412198Z","shell.execute_reply.started":"2022-02-20T07:09:50.297796Z","shell.execute_reply":"2022-02-20T07:09:50.411036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes=['ä«¡ß¬«⌐ ¼«¡áßΓδα∞','æ«í«α éáß¿½¿∩ ü½áªÑ¡¡«ú«','æ«í«α¡á∩ »½«Θáñ∞','æ¼«Γα«óá∩ »½«Θáñ¬á Panorama360','æΓáñ¿«¡ ïπª¡¿¬¿','èαáß¡á∩ »½«Θáñ∞','èαπΓ¿µ¬«Ñ »«ñó«α∞Ñ','éδßΓáó¬á ñ«ßΓ¿ªÑ¡¿⌐ ¡áα«ñ¡«ú« σ«º∩⌐ßΓóá (éäìò)','ì¿¬«½∞ß¬á∩ π½¿µá','î«ß¬«óß¬¿⌐ îÑΓα«»«½¿ΓÑ¡','î«ß¬«óß¬¿⌐ ú«ßπñáαßΓóÑ¡¡δ⌐ π¡¿óÑαß¿ΓÑΓ ¿¼Ñ¡¿ î.é.ï«¼«¡«ß«óá','òαá¼ òα¿ßΓá æ»áß¿ΓÑ½∩','ö«¡Γá¡ äαπªíá ¡áα«ñ«ó','ü«½∞Φ«⌐ ΓÑáΓα']","metadata":{"execution":{"iopub.status.busy":"2022-02-20T07:09:52.260803Z","iopub.execute_input":"2022-02-20T07:09:52.261081Z","iopub.status.idle":"2022-02-20T07:09:52.265223Z","shell.execute_reply.started":"2022-02-20T07:09:52.26103Z","shell.execute_reply":"2022-02-20T07:09:52.26439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nmodel = load_model('./model.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(demonstraition_generator, steps=45)","metadata":{"_uuid":"4782eb23fa7d003f0e2415d995894017edb2d896","execution":{"iopub.status.busy":"2022-02-20T07:09:52.937578Z","iopub.execute_input":"2022-02-20T07:09:52.937839Z","iopub.status.idle":"2022-02-20T07:09:53.087425Z","shell.execute_reply.started":"2022-02-20T07:09:52.937792Z","shell.execute_reply":"2022-02-20T07:09:53.086469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res=[]\nfor i in range(len(predict)):\n    res.append(trans[classes[np.argmax(predict[i])]])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T14:33:22.631695Z","iopub.execute_input":"2022-02-19T14:33:22.631974Z","iopub.status.idle":"2022-02-19T14:33:22.636527Z","shell.execute_reply.started":"2022-02-19T14:33:22.631926Z","shell.execute_reply":"2022-02-19T14:33:22.6358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"./folder/folder\")[0]","metadata":{"execution":{"iopub.status.busy":"2022-02-19T14:56:56.880202Z","iopub.execute_input":"2022-02-19T14:56:56.880599Z","iopub.status.idle":"2022-02-19T14:56:56.886934Z","shell.execute_reply.started":"2022-02-19T14:56:56.88052Z","shell.execute_reply":"2022-02-19T14:56:56.886153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30, 30))\nfor i in range(0, 45):\n    ax=plt.subplot(15, 3, i+1)\n    for X_batch, Y_batch in demonstraition_generator:\n        image = X_batch[0]\n        ax.title.set_text(res[i])\n        plt.imshow(image)\n        break\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T14:56:31.172317Z","iopub.execute_input":"2022-02-19T14:56:31.172672Z","iopub.status.idle":"2022-02-19T14:56:38.435058Z","shell.execute_reply.started":"2022-02-19T14:56:31.172617Z","shell.execute_reply":"2022-02-19T14:56:38.434422Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predict)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T14:23:34.668129Z","iopub.execute_input":"2022-02-19T14:23:34.668407Z","iopub.status.idle":"2022-02-19T14:23:34.673458Z","shell.execute_reply.started":"2022-02-19T14:23:34.668355Z","shell.execute_reply":"2022-02-19T14:23:34.67254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-02-19T14:24:55.431246Z","iopub.execute_input":"2022-02-19T14:24:55.431544Z","iopub.status.idle":"2022-02-19T14:24:55.436827Z","shell.execute_reply.started":"2022-02-19T14:24:55.431492Z","shell.execute_reply":"2022-02-19T14:24:55.436086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res","metadata":{"execution":{"iopub.status.busy":"2022-02-19T14:24:55.941134Z","iopub.execute_input":"2022-02-19T14:24:55.941426Z","iopub.status.idle":"2022-02-19T14:24:55.94734Z","shell.execute_reply.started":"2022-02-19T14:24:55.94136Z","shell.execute_reply":"2022-02-19T14:24:55.946515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res","metadata":{"execution":{"iopub.status.busy":"2022-02-19T14:19:47.312593Z","iopub.execute_input":"2022-02-19T14:19:47.313133Z","iopub.status.idle":"2022-02-19T14:19:47.320361Z","shell.execute_reply.started":"2022-02-19T14:19:47.313078Z","shell.execute_reply":"2022-02-19T14:19:47.319643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For categoral classication the prediction will come with probability of each category. So we will pick the category that have the highest probability with numpy average max","metadata":{}},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T13:24:38.975744Z","iopub.status.idle":"2022-02-19T13:24:38.976265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will convert the predict category back into our generator classes by using `train_generator.class_indices`. It is the classes that image generator map while converting data into computer vision","metadata":{}},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T15:53:29.173479Z","iopub.execute_input":"2022-02-10T15:53:29.173756Z","iopub.status.idle":"2022-02-10T15:53:29.181312Z","shell.execute_reply.started":"2022-02-10T15:53:29.173707Z","shell.execute_reply":"2022-02-10T15:53:29.180433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From our prepare data part. We map data with `{1: 'dog', 0: 'cat'}`. Now we will map the result back to dog is 1 and cat is 0","metadata":{}},{"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2022-02-10T15:53:30.611701Z","iopub.execute_input":"2022-02-10T15:53:30.611984Z","iopub.status.idle":"2022-02-10T15:53:30.617832Z","shell.execute_reply.started":"2022-02-10T15:53:30.611923Z","shell.execute_reply":"2022-02-10T15:53:30.617104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Virtaulize Result","metadata":{"_uuid":"b00add65fe765529e637c3a9904d710bb7eff1d8"}},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"_uuid":"d0bf6dd5ff344092fa0121f70bdd60fa5a40e29c","execution":{"iopub.status.busy":"2022-02-10T15:53:31.752436Z","iopub.execute_input":"2022-02-10T15:53:31.752733Z","iopub.status.idle":"2022-02-10T15:53:31.969653Z","shell.execute_reply.started":"2022-02-10T15:53:31.752683Z","shell.execute_reply":"2022-02-10T15:53:31.968827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### See predicted result with images","metadata":{"_uuid":"ce72a83f80d6e012b12b82c8ee3365d671a3b307"}},{"cell_type":"code","source":"sample_test = test_df.head(18)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = load_img(\"../input/dost/\"+filename, target_size=IMAGE_SIZE)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(trans(category)) + ')' )\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"98b41dc83075e6297137fb45bf703c313dd4ae28","execution":{"iopub.status.busy":"2022-02-19T13:52:42.764801Z","iopub.execute_input":"2022-02-19T13:52:42.765215Z","iopub.status.idle":"2022-02-19T13:52:42.786468Z","shell.execute_reply.started":"2022-02-19T13:52:42.765115Z","shell.execute_reply":"2022-02-19T13:52:42.78509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"_uuid":"d1ca25943e73aa20a37f9fb8670ee430caeaaf1f"}},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"_uuid":"cce9f3e2ffff0693d79d84590ed71fbbca7c3c7c","execution":{"iopub.status.busy":"2022-02-10T15:53:37.800331Z","iopub.execute_input":"2022-02-10T15:53:37.800696Z","iopub.status.idle":"2022-02-10T15:53:37.971921Z","shell.execute_reply.started":"2022-02-10T15:53:37.800655Z","shell.execute_reply":"2022-02-10T15:53:37.97128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}