{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ファイルの解凍\nimport zipfile\n\nwith zipfile.ZipFile('../input/dogs-vs-cats/test1.zip') as existing_zip:\n    existing_zip.extractall()\nwith zipfile.ZipFile('../input/dogs-vs-cats/train.zip') as existing_zip:\n    existing_zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:56:13.226687Z","iopub.execute_input":"2021-07-16T08:56:13.227101Z","iopub.status.idle":"2021-07-16T08:56:32.091578Z","shell.execute_reply.started":"2021-07-16T08:56:13.226990Z","shell.execute_reply":"2021-07-16T08:56:32.090638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:56:40.551119Z","iopub.execute_input":"2021-07-16T08:56:40.551443Z","iopub.status.idle":"2021-07-16T08:56:40.556782Z","shell.execute_reply.started":"2021-07-16T08:56:40.551413Z","shell.execute_reply":"2021-07-16T08:56:40.555893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:56:42.823740Z","iopub.execute_input":"2021-07-16T08:56:42.824089Z","iopub.status.idle":"2021-07-16T08:56:43.464089Z","shell.execute_reply.started":"2021-07-16T08:56:42.824057Z","shell.execute_reply":"2021-07-16T08:56:43.463051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:56:47.410368Z","iopub.execute_input":"2021-07-16T08:56:47.410707Z","iopub.status.idle":"2021-07-16T08:56:48.071184Z","shell.execute_reply.started":"2021-07-16T08:56:47.410673Z","shell.execute_reply":"2021-07-16T08:56:48.070158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#フォルダの移動\nimport 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-16T08:57:38.684681Z","iopub.execute_input":"2021-07-16T08:57:38.685034Z","iopub.status.idle":"2021-07-16T08:57:38.690051Z","shell.execute_reply.started":"2021-07-16T08:57:38.684986Z","shell.execute_reply":"2021-07-16T08:57:38.688987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"move_glob('/kaggle/working/train/dogs', 'dog*.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:57:43.877653Z","iopub.execute_input":"2021-07-16T08:57:43.877979Z","iopub.status.idle":"2021-07-16T08:57:44.337918Z","shell.execute_reply.started":"2021-07-16T08:57:43.877949Z","shell.execute_reply":"2021-07-16T08:57:44.337096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"move_glob('/kaggle/working/train/cats', 'cat*.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:57:46.761458Z","iopub.execute_input":"2021-07-16T08:57:46.761795Z","iopub.status.idle":"2021-07-16T08:57:47.195636Z","shell.execute_reply.started":"2021-07-16T08:57:46.761763Z","shell.execute_reply":"2021-07-16T08:57:47.194804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:57:50.738463Z","iopub.execute_input":"2021-07-16T08:57:50.738778Z","iopub.status.idle":"2021-07-16T08:57:50.744380Z","shell.execute_reply.started":"2021-07-16T08:57:50.738747Z","shell.execute_reply":"2021-07-16T08:57:50.743391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir valid","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:57:57.658339Z","iopub.execute_input":"2021-07-16T08:57:57.658661Z","iopub.status.idle":"2021-07-16T08:57:58.331347Z","shell.execute_reply.started":"2021-07-16T08:57:57.658632Z","shell.execute_reply":"2021-07-16T08:57:58.330299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd valid","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:58:00.609740Z","iopub.execute_input":"2021-07-16T08:58:00.610186Z","iopub.status.idle":"2021-07-16T08:58:00.620452Z","shell.execute_reply.started":"2021-07-16T08:58:00.610142Z","shell.execute_reply":"2021-07-16T08:58:00.619249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:58:04.131577Z","iopub.execute_input":"2021-07-16T08:58:04.131894Z","iopub.status.idle":"2021-07-16T08:58:04.765896Z","shell.execute_reply.started":"2021-07-16T08:58:04.131865Z","shell.execute_reply":"2021-07-16T08:58:04.764886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:58:06.589239Z","iopub.execute_input":"2021-07-16T08:58:06.589581Z","iopub.status.idle":"2021-07-16T08:58:07.219100Z","shell.execute_reply.started":"2021-07-16T08:58:06.589546Z","shell.execute_reply":"2021-07-16T08:58:07.218055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../train","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:58:09.062420Z","iopub.execute_input":"2021-07-16T08:58:09.062742Z","iopub.status.idle":"2021-07-16T08:58:09.070712Z","shell.execute_reply.started":"2021-07-16T08:58:09.062709Z","shell.execute_reply":"2021-07-16T08:58:09.069686Z"},"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-16T08:58:11.653889Z","iopub.execute_input":"2021-07-16T08:58:11.654241Z","iopub.status.idle":"2021-07-16T08:58:11.771917Z","shell.execute_reply.started":"2021-07-16T08:58:11.654210Z","shell.execute_reply":"2021-07-16T08:58:11.771117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(0, 12500):\n    resize('/kaggle/working/train/dogs/dog.' + str(num) + '.jpg', '/kaggle/working/train/dogs/dog.' + str(num) + '.jpg', 224, 224)\n    resize('/kaggle/working/train/cats/cat.' + str(num) + '.jpg', '/kaggle/working/train/cats/cat.' + str(num) + '.jpg', 224, 224)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T08:58:17.418146Z","iopub.execute_input":"2021-07-16T08:58:17.418471Z","iopub.status.idle":"2021-07-16T08:59:57.163683Z","shell.execute_reply.started":"2021-07-16T08:58:17.418442Z","shell.execute_reply":"2021-07-16T08:59:57.162828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(2500):\n    move_glob('/kaggle/working/valid/cats', '/kaggle/working/train/cats/cat.'+ str(num) +'.jpg')\n    move_glob('/kaggle/working/valid/dogs', '/kaggle/working/train/dogs/dog.'+ str(num) +'.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:00:30.616453Z","iopub.execute_input":"2021-07-16T09:00:30.616783Z","iopub.status.idle":"2021-07-16T09:00:30.833611Z","shell.execute_reply.started":"2021-07-16T09:00:30.616753Z","shell.execute_reply":"2021-07-16T09:00:30.832738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:00:33.039836Z","iopub.execute_input":"2021-07-16T09:00:33.040168Z","iopub.status.idle":"2021-07-16T09:00:37.823587Z","shell.execute_reply.started":"2021-07-16T09:00:33.040136Z","shell.execute_reply":"2021-07-16T09:00:37.822751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_dir = '/kaggle/working/train'\nvalid_data_dir = '/kaggle/working/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 = (224, 224),batch_size = 32, class_mode = \"binary\")\nvalid_generator = valid_datagen.flow_from_directory(directory = valid_data_dir,target_size = (224, 224),batch_size = 32, class_mode = \"binary\")","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:00:43.544229Z","iopub.execute_input":"2021-07-16T09:00:43.544685Z","iopub.status.idle":"2021-07-16T09:00:44.758650Z","shell.execute_reply.started":"2021-07-16T09:00:43.544641Z","shell.execute_reply":"2021-07-16T09:00:44.757812Z"},"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\nfrom keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:05:59.911890Z","iopub.execute_input":"2021-07-16T09:05:59.912248Z","iopub.status.idle":"2021-07-16T09:05:59.916868Z","shell.execute_reply.started":"2021-07-16T09:05:59.912215Z","shell.execute_reply":"2021-07-16T09:05:59.915918Z"},"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 = (224,224,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(Flatten())\nmodel.add(Dense(256,activation = \"relu\"))\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 = 80,verbose = 1, validation_data = (valid_generator), steps_per_epoch = 20000 / 32,validation_steps = 5000/32)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:23:10.507331Z","iopub.execute_input":"2021-07-16T09:23:10.507658Z","iopub.status.idle":"2021-07-16T09:23:59.892400Z","shell.execute_reply.started":"2021-07-16T09:23:10.507626Z","shell.execute_reply":"2021-07-16T09:23:59.889944Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"dogs-vs-cats_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:16:31.471648Z","iopub.execute_input":"2021-07-16T09:16:31.471977Z","iopub.status.idle":"2021-07-16T09:16:31.643201Z","shell.execute_reply.started":"2021-07-16T09:16:31.471948Z","shell.execute_reply":"2021-07-16T09:16:31.642327Z"},"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-16T09:16:33.455670Z","iopub.execute_input":"2021-07-16T09:16:33.456038Z","iopub.status.idle":"2021-07-16T09:16:33.603674Z","shell.execute_reply.started":"2021-07-16T09:16:33.455990Z","shell.execute_reply":"2021-07-16T09:16:33.602734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\naccuracy = 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-16T09:16:36.491561Z","iopub.execute_input":"2021-07-16T09:16:36.491909Z","iopub.status.idle":"2021-07-16T09:16:36.633555Z","shell.execute_reply.started":"2021-07-16T09:16:36.491874Z","shell.execute_reply":"2021-07-16T09:16:36.632591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(1, 12501):\n    resize('test1/' + str(num) + '.jpg', 'test1/' + str(num) + '.jpg', 224, 224)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:16:57.940798Z","iopub.execute_input":"2021-07-16T09:16:57.941134Z","iopub.status.idle":"2021-07-16T09:17:45.704806Z","shell.execute_reply.started":"2021-07-16T09:16:57.941101Z","shell.execute_reply":"2021-07-16T09:17:45.703973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nnew_model = load_model('dogs-vs-cats_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:21:20.876987Z","iopub.execute_input":"2021-07-16T09:21:20.877347Z","iopub.status.idle":"2021-07-16T09:21:21.068861Z","shell.execute_reply.started":"2021-07-16T09:21:20.877316Z","shell.execute_reply":"2021-07-16T09:21:21.067998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom keras.preprocessing import image\nresult = []\n\nfor num in range(1, 12501):\n    #予測したい画像のパス\n    TESTPATH = \"test1/\" + str(num) + \".jpg\"\n\n    #予測モデルに入力できるように画像を配列に落とし込む\n    img = image.load_img(TESTPATH, target_size=(224, 224))\n    x = image.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    x = x / 255.0\n    #予測\n    result_predict = model.predict(x)\n    result.append(result_predict)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:23:59.893263Z","iopub.status.idle":"2021-07-16T09:23:59.893635Z"},"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(\"sample_submission.csv\", index=False)","metadata":{},"execution_count":null,"outputs":[]}]}