{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-12T21:56:06.23363Z","iopub.execute_input":"2021-07-12T21:56:06.233985Z","iopub.status.idle":"2021-07-12T21:56:06.246758Z","shell.execute_reply.started":"2021-07-12T21:56:06.233909Z","shell.execute_reply":"2021-07-12T21:56:06.245761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs-vs-cats","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:06.299295Z","iopub.execute_input":"2021-07-29T13:56:06.299615Z","iopub.status.idle":"2021-07-29T13:56:06.961144Z","shell.execute_reply.started":"2021-07-29T13:56:06.299585Z","shell.execute_reply":"2021-07-29T13:56:06.960231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/test.zip') as existing_zip:\n    existing_zip.extractall('./dogs-vs-cats')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:07.859923Z","iopub.execute_input":"2021-07-29T13:56:07.860295Z","iopub.status.idle":"2021-07-29T13:56:11.915014Z","shell.execute_reply.started":"2021-07-29T13:56:07.860264Z","shell.execute_reply":"2021-07-29T13:56:11.91365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/train.zip') as existing_zip:\n    existing_zip.extractall('./dogs-vs-cats')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:17.119438Z","iopub.execute_input":"2021-07-29T13:56:17.119744Z","iopub.status.idle":"2021-07-29T13:56:26.318671Z","shell.execute_reply.started":"2021-07-29T13:56:17.119716Z","shell.execute_reply":"2021-07-29T13:56:26.316239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd dogs-vs-cats/train","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:27.556373Z","iopub.execute_input":"2021-07-29T13:56:27.55671Z","iopub.status.idle":"2021-07-29T13:56:27.562506Z","shell.execute_reply.started":"2021-07-29T13:56:27.55668Z","shell.execute_reply":"2021-07-29T13:56:27.561608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:28.492701Z","iopub.execute_input":"2021-07-29T13:56:28.493039Z","iopub.status.idle":"2021-07-29T13:56:29.129916Z","shell.execute_reply.started":"2021-07-29T13:56:28.493007Z","shell.execute_reply":"2021-07-29T13:56:29.128788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:29.882194Z","iopub.execute_input":"2021-07-29T13:56:29.882525Z","iopub.status.idle":"2021-07-29T13:56:30.668985Z","shell.execute_reply.started":"2021-07-29T13:56:29.88249Z","shell.execute_reply":"2021-07-29T13:56:30.667781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport glob\nimport os\n\ndef move_glob(dst_path, pathname, recursive=True):\n    for p in glob.glob(pathname, recursive=recursive):\n        shutil.move(p, dst_path)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:31.462744Z","iopub.execute_input":"2021-07-29T13:56:31.463144Z","iopub.status.idle":"2021-07-29T13:56:31.468065Z","shell.execute_reply.started":"2021-07-29T13:56:31.463105Z","shell.execute_reply":"2021-07-29T13:56:31.467119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"move_glob('./dogs', 'dog*.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:32.905517Z","iopub.execute_input":"2021-07-29T13:56:32.90587Z","iopub.status.idle":"2021-07-29T13:56:33.428354Z","shell.execute_reply.started":"2021-07-29T13:56:32.905836Z","shell.execute_reply":"2021-07-29T13:56:33.427057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"move_glob('./cats', 'cat*.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:34.206273Z","iopub.execute_input":"2021-07-29T13:56:34.20662Z","iopub.status.idle":"2021-07-29T13:56:34.675613Z","shell.execute_reply.started":"2021-07-29T13:56:34.206592Z","shell.execute_reply":"2021-07-29T13:56:34.67468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:35.42342Z","iopub.execute_input":"2021-07-29T13:56:35.423769Z","iopub.status.idle":"2021-07-29T13:56:35.432403Z","shell.execute_reply.started":"2021-07-29T13:56:35.423735Z","shell.execute_reply":"2021-07-29T13:56:35.431335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir valid","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:36.398755Z","iopub.execute_input":"2021-07-29T13:56:36.399123Z","iopub.status.idle":"2021-07-29T13:56:37.079542Z","shell.execute_reply.started":"2021-07-29T13:56:36.39909Z","shell.execute_reply":"2021-07-29T13:56:37.078346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd valid","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:37.824276Z","iopub.execute_input":"2021-07-29T13:56:37.824614Z","iopub.status.idle":"2021-07-29T13:56:37.830871Z","shell.execute_reply.started":"2021-07-29T13:56:37.824578Z","shell.execute_reply":"2021-07-29T13:56:37.82962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:39.254365Z","iopub.execute_input":"2021-07-29T13:56:39.254689Z","iopub.status.idle":"2021-07-29T13:56:39.929477Z","shell.execute_reply.started":"2021-07-29T13:56:39.254659Z","shell.execute_reply":"2021-07-29T13:56:39.928296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:40.714945Z","iopub.execute_input":"2021-07-29T13:56:40.715334Z","iopub.status.idle":"2021-07-29T13:56:41.392189Z","shell.execute_reply.started":"2021-07-29T13:56:40.715288Z","shell.execute_reply":"2021-07-29T13:56:41.391139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:42.113352Z","iopub.execute_input":"2021-07-29T13:56:42.113709Z","iopub.status.idle":"2021-07-29T13:56:42.121688Z","shell.execute_reply.started":"2021-07-29T13:56:42.113673Z","shell.execute_reply":"2021-07-29T13:56:42.118805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:42.921801Z","iopub.execute_input":"2021-07-29T13:56:42.922172Z","iopub.status.idle":"2021-07-29T13:56:42.930094Z","shell.execute_reply.started":"2021-07-29T13:56:42.922142Z","shell.execute_reply":"2021-07-29T13:56:42.929339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\n\ndef resize(src_file, dst_file, width, height):\n    \"\"\"\n    画像ファイルを与えられたサイズにサイズ変換する関数\n    アス比は固定、左上に寄せる、余った部分はゼロで埋める\n\n    Returns\n    -------\n    ret_scale : float\n    拡大/縮小した倍率\n    \"\"\"\n\n    src_img = cv2.imread(src_file)\n    h, w, c = src_img.shape\n    # dst_img = cv2.resize(src_img, dsize=(width, height))\n\n    # アス比固定, padding\n    scale_w = width / w\n    scale_h = height / h\n\n    ret_scale = 1.0\n    # Down Convert\n    if(scale_w < 1.0 or scale_h < 1.0):\n        if(scale_w < scale_h): \n            resize_img = cv2.resize(src_img, dsize=None, fx=scale_w, fy=scale_w, interpolation = cv2.INTER_AREA)\n            ret_scale = scale_w\n        else:\n            resize_img = cv2.resize(src_img, dsize=None, fx=scale_h, fy=scale_h, interpolation = cv2.INTER_AREA)\n            ret_scale = scale_h\n    else:\n        resize_img = src_img\n\n    # dst_img 生成\n    dst_img = np.zeros((height, width, 3), dtype = np.uint8)\n\n    # dst_imgにresize_imgを合成\n    top = 0\n    left = 0\n    #dst_img[top:height + top, left:width + left] = resize_img\n    h, w, c = resize_img.shape\n    dst_img[0:h, 0:w] = resize_img\n\n    cv2.imwrite(dst_file, dst_img)\n\n    return ret_scale","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:43.836681Z","iopub.execute_input":"2021-07-29T13:56:43.837049Z","iopub.status.idle":"2021-07-29T13:56:44.001104Z","shell.execute_reply.started":"2021-07-29T13:56:43.837013Z","shell.execute_reply":"2021-07-29T13:56:44.000232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport random\ndef reverserandom90(src_file, dst_file):\n    import numpy as np\n    from PIL import Image\n \n    img = np.array(Image.open(src_file))\n    random_num = random.randint(0,1)\n    if(random_num == 0):\n        dst_img = Image.fromarray(cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)).save(dst_file)\n    if(random_num == 1):\n        dst_img = Image.fromarray(cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)).save(dst_file)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:47.541999Z","iopub.execute_input":"2021-07-29T13:56:47.542365Z","iopub.status.idle":"2021-07-29T13:56:47.548197Z","shell.execute_reply.started":"2021-07-29T13:56:47.542334Z","shell.execute_reply":"2021-07-29T13:56:47.547306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\ndef blur(src_file, dst_file):\n    import numpy as np\n    from PIL import Image\n \n    img = np.array(Image.open(src_file))\n    \n    dst_img = Image.fromarray(cv2.blur(img, ksize=(3, 3))).save(dst_file)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:48.403272Z","iopub.execute_input":"2021-07-29T13:56:48.403607Z","iopub.status.idle":"2021-07-29T13:56:48.408277Z","shell.execute_reply.started":"2021-07-29T13:56:48.403577Z","shell.execute_reply":"2021-07-29T13:56:48.407466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(0, 6250):\n    reverserandom90('./dogs-vs-cats/train/dogs/dog.' + str(num) + '.jpg', './dogs-vs-cats/train/dogs/dog.' + str(num + 12500) + '.jpg')\n    reverserandom90('./dogs-vs-cats/train/cats/cat.' + str(num) + '.jpg', './dogs-vs-cats/train/cats/cat.' + str(num + 12500) + '.jpg')\n    blur('./dogs-vs-cats/train/dogs/dog.' + str(num + 6250) + '.jpg', './dogs-vs-cats/train/dogs/dog.' + str(num + 18750) + '.jpg')\n    blur('./dogs-vs-cats/train/cats/cat.' + str(num + 6250) + '.jpg', './dogs-vs-cats/train/cats/cat.' + str(num + 18750) + '.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:56:50.697308Z","iopub.execute_input":"2021-07-29T13:56:50.697636Z","iopub.status.idle":"2021-07-29T14:00:01.192211Z","shell.execute_reply.started":"2021-07-29T13:56:50.697606Z","shell.execute_reply":"2021-07-29T14:00:01.191243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(0, 25000):\n    resize('./dogs-vs-cats/train/dogs/dog.' + str(num) + '.jpg', './dogs-vs-cats/train/dogs/dog.' + str(num) + '.jpg', 216, 216)\n    resize('./dogs-vs-cats/train/cats/cat.' + str(num) + '.jpg', './dogs-vs-cats/train/cats/cat.' + str(num) + '.jpg', 216, 216)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:00:12.366741Z","iopub.execute_input":"2021-07-29T14:00:12.367128Z","iopub.status.idle":"2021-07-29T14:03:40.861053Z","shell.execute_reply.started":"2021-07-29T14:00:12.367093Z","shell.execute_reply":"2021-07-29T14:03:40.860184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(2500):\n    move_glob('./dogs-vs-cats/valid/cats', './dogs-vs-cats/train/cats/cat.'+ str(num) +'.jpg')\n    move_glob('./dogs-vs-cats/valid/dogs', './dogs-vs-cats/train/dogs/dog.'+ str(num) +'.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:03:49.506536Z","iopub.execute_input":"2021-07-29T14:03:49.506845Z","iopub.status.idle":"2021-07-29T14:03:49.732494Z","shell.execute_reply.started":"2021-07-29T14:03:49.506816Z","shell.execute_reply":"2021-07-29T14:03:49.731554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(1250):\n    move_glob('./dogs-vs-cats/valid/cats', './dogs-vs-cats/train/cats/cat.'+ str(num + 12500) +'.jpg')\n    move_glob('./dogs-vs-cats/valid/dogs', './dogs-vs-cats/train/dogs/dog.'+ str(num + 12500) +'.jpg')\n    move_glob('./dogs-vs-cats/valid/cats', './dogs-vs-cats/train/cats/cat.'+ str(num + 18750) +'.jpg')\n    move_glob('./dogs-vs-cats/valid/dogs', './dogs-vs-cats/train/dogs/dog.'+ str(num + 18750) +'.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:03:50.890271Z","iopub.execute_input":"2021-07-29T14:03:50.890637Z","iopub.status.idle":"2021-07-29T14:03:51.119329Z","shell.execute_reply.started":"2021-07-29T14:03:50.890606Z","shell.execute_reply":"2021-07-29T14:03:51.118487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:03:52.336551Z","iopub.execute_input":"2021-07-29T14:03:52.336857Z","iopub.status.idle":"2021-07-29T14:03:57.971629Z","shell.execute_reply.started":"2021-07-29T14:03:52.336828Z","shell.execute_reply":"2021-07-29T14:03:57.970596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_dir = './dogs-vs-cats/train'\nvalid_data_dir = './dogs-vs-cats/valid'\n\ntrain_datagen = ImageDataGenerator(rescale = 1.0/255)\nvalid_datagen = ImageDataGenerator(rescale = 1.0/255)\n\ntrain_generator = train_datagen.flow_from_directory(directory = train_data_dir,target_size = (216, 216),batch_size = 32, class_mode = \"binary\")\nvalid_generator = valid_datagen.flow_from_directory(directory = valid_data_dir,target_size = (216, 216),batch_size = 32, class_mode = \"binary\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:19:14.332458Z","iopub.execute_input":"2021-07-29T17:19:14.332785Z","iopub.status.idle":"2021-07-29T17:19:16.466242Z","shell.execute_reply.started":"2021-07-29T17:19:14.332751Z","shell.execute_reply":"2021-07-29T17:19:16.465332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Flatten\nfrom keras.layers import Conv2D,MaxPooling2D,Dropout\nfrom keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:19:18.889712Z","iopub.execute_input":"2021-07-29T17:19:18.89005Z","iopub.status.idle":"2021-07-29T17:19:18.896413Z","shell.execute_reply.started":"2021-07-29T17:19:18.890019Z","shell.execute_reply":"2021-07-29T17:19:18.895645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64,kernel_size = (5,5),activation = \"relu\",input_shape = (216,216,3)))\n\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(128,kernel_size = (5,5),activation = \"relu\"))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Conv2D(128,kernel_size = (5,5),activation = \"relu\"))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Conv2D(128,kernel_size = (5,5),activation = \"relu\"))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(256,activation = \"relu\"))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(1,activation = \"sigmoid\"))\n\nsgd = optimizers.SGD(lr = 0.1)\n\nmodel.compile(loss = \"mean_squared_error\",optimizer = sgd,metrics = [\"accuracy\"])\nhistory = model.fit_generator(train_generator,epochs = 30,verbose = 1, validation_data = (valid_generator), steps_per_epoch = 40000 / 32,validation_steps = 10000/32)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:08.344231Z","iopub.execute_input":"2021-07-29T17:20:08.344552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"./dogs-vs-cats/dogs-vs-cats_model.hdf5\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:32:53.507025Z","iopub.execute_input":"2021-07-29T12:32:53.507458Z","iopub.status.idle":"2021-07-29T12:32:53.566632Z","shell.execute_reply.started":"2021-07-29T12:32:53.507394Z","shell.execute_reply":"2021-07-29T12:32:53.565507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import plot_model\nmodel.summary()\nplot_model(model,to_file='./dogs-vs-cats/dogs-vs-cats_model.png')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:33:00.252948Z","iopub.execute_input":"2021-07-29T12:33:00.253328Z","iopub.status.idle":"2021-07-29T12:33:00.868547Z","shell.execute_reply.started":"2021-07-29T12:33:00.253295Z","shell.execute_reply":"2021-07-29T12:33:00.867371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nloss = history.history[\"loss\"]\nval_loss = history.history[\"val_loss\"]\n\nlearning_count = len(loss)+1\n\nplt.plot(range(1,learning_count),loss,marker = \"+\",label = \"loss\")\nplt.plot(range(1,learning_count),val_loss,marker = \"+\",label = \"val_loss\")\nplt.legend(loc = \"best\",fontsize = 10)\nplt.xlabel(\"learning_count\")\nplt.ylabel(\"loss\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:33:20.671817Z","iopub.execute_input":"2021-07-29T12:33:20.672199Z","iopub.status.idle":"2021-07-29T12:33:20.900919Z","shell.execute_reply.started":"2021-07-29T12:33:20.672163Z","shell.execute_reply":"2021-07-29T12:33:20.899354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = history.history[\"accuracy\"]\nval_accuracy = history.history[\"val_accuracy\"]\n\nlearning_count = len(loss)+1\n\nplt.plot(range(1,learning_count),accuracy,marker = \"+\",label = \"accuracy\")\nplt.plot(range(1,learning_count),val_accuracy,marker = \"+\",label = \"val_accuracy\")\nplt.legend(loc = \"best\",fontsize = 10)\nplt.xlabel(\"learning_count\")\nplt.ylabel(\"accuracy\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:33:33.73651Z","iopub.execute_input":"2021-07-29T12:33:33.736924Z","iopub.status.idle":"2021-07-29T12:33:33.935201Z","shell.execute_reply.started":"2021-07-29T12:33:33.736892Z","shell.execute_reply":"2021-07-29T12:33:33.933821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nnew_model = load_model('./dogs-vs-cats/dogs-vs-cats_model.hdf5')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:33:44.843611Z","iopub.execute_input":"2021-07-29T12:33:44.843996Z","iopub.status.idle":"2021-07-29T12:33:44.996001Z","shell.execute_reply.started":"2021-07-29T12:33:44.843966Z","shell.execute_reply":"2021-07-29T12:33:44.994888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(1, 12501):\n    resize('./dogs-vs-cats/test/' + str(num) + '.jpg', './dogs-vs-cats/test/' + str(num) + '.jpg', 216, 216)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:33:48.426296Z","iopub.execute_input":"2021-07-29T12:33:48.426746Z","iopub.status.idle":"2021-07-29T12:34:47.919393Z","shell.execute_reply.started":"2021-07-29T12:33:48.426716Z","shell.execute_reply":"2021-07-29T12:34:47.918266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_num = []\n\nfor num in range(1, 12501):\n    result_num.append(num)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:53:07.35212Z","iopub.execute_input":"2021-07-29T13:53:07.352599Z","iopub.status.idle":"2021-07-29T13:53:07.365513Z","shell.execute_reply.started":"2021-07-29T13:53:07.352502Z","shell.execute_reply":"2021-07-29T13:53:07.364075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#12500枚までの結果出力\nfrom skimage import io\nimport numpy as np\nfrom skimage.transform import resize\nfrom keras.preprocessing.image import load_img, img_to_array\nresult = []\n\n%matplotlib inline\n\nfor num in range(1, 12501):\n    img = io.imread('./dogs-vs-cats/test/'+ str(num) +'.jpg')\n    io.imshow(img)\n\n    # 判定する画像の読み込み\n    image_infer = load_img('./dogs-vs-cats/test/'+ str(num) +'.jpg', target_size=(216, 216))\n    data_infer = img_to_array(image_infer)\n    data_infer = np.expand_dims(data_infer, axis=0)\n    data_infer = data_infer / 255.0\n\n    # 判定処理の実行\n    model.load_weights(os.path.join('./dogs-vs-cats/', 'dogs-vs-cats_model.hdf5'))\n    result_predict = model.predict(data_infer)[0] * 100\n    result.append(result_predict)\n    if num % 100 == 0:\n        print(num,'：Dog (%.1f%%)' % result_predict)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:35:05.113406Z","iopub.execute_input":"2021-07-29T12:35:05.113886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nimport pandas as pd\ndf = pd.DataFrame([result])\ndf = df.T\nfor col in df:\n    df[col]=df[col].astype(str).str.replace(\"[\",\"\").str.replace(\"]\",\"\")\n\ndf.to_csv(\"./dogs-vs-cats.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T16:48:31.108088Z","iopub.execute_input":"2021-07-27T16:48:31.108443Z","iopub.status.idle":"2021-07-27T16:48:33.319436Z","shell.execute_reply.started":"2021-07-27T16:48:31.108411Z","shell.execute_reply":"2021-07-27T16:48:33.318578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}