{"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":"2022-07-22T05:33:36.112898Z","iopub.execute_input":"2022-07-22T05:33:36.113791Z","iopub.status.idle":"2022-07-22T05:33:36.122872Z","shell.execute_reply.started":"2022-07-22T05:33:36.113745Z","shell.execute_reply":"2022-07-22T05:33:36.121602Z"},"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()\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/train.zip') as existing_zip:\n    existing_zip.extractall()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:36.210613Z","iopub.execute_input":"2022-07-22T05:33:36.211532Z","iopub.status.idle":"2022-07-22T05:33:53.256856Z","shell.execute_reply.started":"2022-07-22T05:33:36.211494Z","shell.execute_reply":"2022-07-22T05:33:53.255883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:53.258872Z","iopub.execute_input":"2022-07-22T05:33:53.259230Z","iopub.status.idle":"2022-07-22T05:33:53.267150Z","shell.execute_reply.started":"2022-07-22T05:33:53.259201Z","shell.execute_reply":"2022-07-22T05:33:53.266024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:53.268993Z","iopub.execute_input":"2022-07-22T05:33:53.269624Z","iopub.status.idle":"2022-07-22T05:33:53.948613Z","shell.execute_reply.started":"2022-07-22T05:33:53.269589Z","shell.execute_reply":"2022-07-22T05:33:53.947378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:53.951853Z","iopub.execute_input":"2022-07-22T05:33:53.952487Z","iopub.status.idle":"2022-07-22T05:33:54.634930Z","shell.execute_reply.started":"2022-07-22T05:33:53.952444Z","shell.execute_reply":"2022-07-22T05:33:54.633681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport glob\nimport os\n\n#移動関数\ndef move_glob(dst_path, pathname, recursive=True):\n    for p in glob.glob(pathname, recursive=recursive):\n        shutil.move(p, dst_path)\n#画像の移動\nmove_glob('./dogs', 'dog*.jpg')\nmove_glob('./cats', 'cat*.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:54.638102Z","iopub.execute_input":"2022-07-22T05:33:54.638421Z","iopub.status.idle":"2022-07-22T05:33:55.847017Z","shell.execute_reply.started":"2022-07-22T05:33:54.638379Z","shell.execute_reply":"2022-07-22T05:33:55.845859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:55.849196Z","iopub.execute_input":"2022-07-22T05:33:55.849807Z","iopub.status.idle":"2022-07-22T05:33:55.857463Z","shell.execute_reply.started":"2022-07-22T05:33:55.849740Z","shell.execute_reply":"2022-07-22T05:33:55.856460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir valid","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:55.859509Z","iopub.execute_input":"2022-07-22T05:33:55.860269Z","iopub.status.idle":"2022-07-22T05:33:56.549875Z","shell.execute_reply.started":"2022-07-22T05:33:55.860230Z","shell.execute_reply":"2022-07-22T05:33:56.548666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd valid","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:56.554533Z","iopub.execute_input":"2022-07-22T05:33:56.555340Z","iopub.status.idle":"2022-07-22T05:33:56.564947Z","shell.execute_reply.started":"2022-07-22T05:33:56.555304Z","shell.execute_reply":"2022-07-22T05:33:56.563318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:56.566350Z","iopub.execute_input":"2022-07-22T05:33:56.567157Z","iopub.status.idle":"2022-07-22T05:33:57.359598Z","shell.execute_reply.started":"2022-07-22T05:33:56.567121Z","shell.execute_reply":"2022-07-22T05:33:57.358422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:57.365417Z","iopub.execute_input":"2022-07-22T05:33:57.365728Z","iopub.status.idle":"2022-07-22T05:33:58.148619Z","shell.execute_reply.started":"2022-07-22T05:33:57.365700Z","shell.execute_reply":"2022-07-22T05:33:58.131214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:58.155724Z","iopub.execute_input":"2022-07-22T05:33:58.157093Z","iopub.status.idle":"2022-07-22T05:33:58.172053Z","shell.execute_reply.started":"2022-07-22T05:33:58.157033Z","shell.execute_reply":"2022-07-22T05:33:58.171039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\n\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\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:58.175026Z","iopub.execute_input":"2022-07-22T05:33:58.176041Z","iopub.status.idle":"2022-07-22T05:33:58.503119Z","shell.execute_reply.started":"2022-07-22T05:33:58.175998Z","shell.execute_reply":"2022-07-22T05:33:58.502157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(0, 12500):\n    resize('train/dogs/dog.' + str(num) + '.jpg', 'train/dogs/dog.' + str(num) + '.jpg', 224, 224)\n    resize('train/cats/cat.' + str(num) + '.jpg', 'train/cats/cat.' + str(num) + '.jpg', 224, 224)\n\nfor num in range(2500):\n    move_glob('valid/cats', 'train/cats/cat.'+ str(num) +'.jpg')\n    move_glob('valid/dogs', 'train/dogs/dog.'+ str(num) +'.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:33:58.504616Z","iopub.execute_input":"2022-07-22T05:33:58.504950Z","iopub.status.idle":"2022-07-22T05:35:39.257097Z","shell.execute_reply.started":"2022-07-22T05:33:58.504916Z","shell.execute_reply":"2022-07-22T05:35:39.256037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras import optimizers, regularizers\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nimport os\nimport datetime\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:35:39.258602Z","iopub.execute_input":"2022-07-22T05:35:39.258929Z","iopub.status.idle":"2022-07-22T05:35:44.397812Z","shell.execute_reply.started":"2022-07-22T05:35:39.258894Z","shell.execute_reply":"2022-07-22T05:35:44.396853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_dir = 'train'\nvalid_data_dir = 'valid'","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:35:44.399093Z","iopub.execute_input":"2022-07-22T05:35:44.399711Z","iopub.status.idle":"2022-07-22T05:35:44.407592Z","shell.execute_reply.started":"2022-07-22T05:35:44.399671Z","shell.execute_reply":"2022-07-22T05:35:44.405318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1.0/255, rotation_range=90, horizontal_flip=True, channel_shift_range=50.)\nvalid_datagen = ImageDataGenerator(rescale=1.0/255)\n\ntrain_generator = train_datagen.flow_from_directory(\n    directory=train_data_dir, target_size=(224, 224), batch_size=64, class_mode=\"binary\")\nvalid_generator = valid_datagen.flow_from_directory(\n    directory=valid_data_dir, target_size=(224, 224), batch_size=64, class_mode=\"binary\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:35:44.409296Z","iopub.execute_input":"2022-07-22T05:35:44.410371Z","iopub.status.idle":"2022-07-22T05:35:45.598880Z","shell.execute_reply.started":"2022-07-22T05:35:44.410335Z","shell.execute_reply":"2022-07-22T05:35:45.597877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64, kernel_size=(5, 5),\n          activation=\"relu\", kernel_regularizer=regularizers.l2(0.0001), input_shape=(224, 224, 3)))\n\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(128, kernel_size=(5, 5),\n          kernel_regularizer=regularizers.l2(0.0001), activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(128, kernel_size=(5, 5),\n          kernel_regularizer=regularizers.l2(0.0001), activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(128, kernel_size=(5, 5),\n          kernel_regularizer=regularizers.l2(0.0001), activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation=\"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation=\"sigmoid\"))\n\nopt = optimizers.Adam(learning_rate=0.0001)\nmodel.compile(loss=\"binary_crossentropy\",\n              optimizer=opt, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:35:45.600446Z","iopub.execute_input":"2022-07-22T05:35:45.600813Z","iopub.status.idle":"2022-07-22T05:35:48.659425Z","shell.execute_reply.started":"2022-07-22T05:35:45.600778Z","shell.execute_reply":"2022-07-22T05:35:48.657626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:35:48.660652Z","iopub.execute_input":"2022-07-22T05:35:48.661045Z","iopub.status.idle":"2022-07-22T05:35:48.675155Z","shell.execute_reply.started":"2022-07-22T05:35:48.661009Z","shell.execute_reply":"2022-07-22T05:35:48.673263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, \n                    epochs=80, \n                    verbose=1, \n                    validation_data=(valid_generator), \n                    steps_per_epoch=20000 / 64, \n                    validation_steps=5000/64)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T05:35:48.676345Z","iopub.execute_input":"2022-07-22T05:35:48.676663Z","iopub.status.idle":"2022-07-22T11:14:35.542400Z","shell.execute_reply.started":"2022-07-22T05:35:48.676631Z","shell.execute_reply":"2022-07-22T11:14:35.541319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:14:35.544303Z","iopub.execute_input":"2022-07-22T11:14:35.544677Z","iopub.status.idle":"2022-07-22T11:14:35.651871Z","shell.execute_reply.started":"2022-07-22T11:14:35.544641Z","shell.execute_reply":"2022-07-22T11:14:35.650928Z"},"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()\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":"2022-07-22T11:14:35.653231Z","iopub.execute_input":"2022-07-22T11:14:35.653783Z","iopub.status.idle":"2022-07-22T11:14:36.047775Z","shell.execute_reply.started":"2022-07-22T11:14:35.653745Z","shell.execute_reply":"2022-07-22T11:14:36.046873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:14:36.049071Z","iopub.execute_input":"2022-07-22T11:14:36.050058Z","iopub.status.idle":"2022-07-22T11:14:36.055341Z","shell.execute_reply.started":"2022-07-22T11:14:36.050019Z","shell.execute_reply":"2022-07-22T11:14:36.054254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = load_model('model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:14:36.057104Z","iopub.execute_input":"2022-07-22T11:14:36.057650Z","iopub.status.idle":"2022-07-22T11:14:36.230487Z","shell.execute_reply.started":"2022-07-22T11:14:36.057610Z","shell.execute_reply":"2022-07-22T11:14:36.229528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []\n\nfor num in range(1, 12501):\n    # 予測したい画像のパス\n    TESTPATH = \"test/\" + 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 = new_model.predict(x)\n    # print(result_predict)\n    result.append(result_predict)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:14:36.232024Z","iopub.execute_input":"2022-07-22T11:14:36.232359Z","iopub.status.idle":"2022-07-22T11:22:43.952056Z","shell.execute_reply.started":"2022-07-22T11:14:36.232325Z","shell.execute_reply":"2022-07-22T11:22:43.951089Z"},"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":"2022-07-22T11:22:43.953492Z","iopub.execute_input":"2022-07-22T11:22:43.953841Z","iopub.status.idle":"2022-07-22T11:22:43.961330Z","shell.execute_reply.started":"2022-07-22T11:22:43.953808Z","shell.execute_reply":"2022-07-22T11:22:43.959477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_res = pd.DataFrame([result])\ndf_res = df_res.T\nfor col in df_res:\n    df_res[col] = df_res[col].astype(\n        str).str.replace(\"[\", \"\").str.replace(\"]\", \"\")\ndf = pd.read_csv('../input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\ndf['label'] = df_res","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:22:43.962821Z","iopub.execute_input":"2022-07-22T11:22:43.963539Z","iopub.status.idle":"2022-07-22T11:22:45.344916Z","shell.execute_reply.started":"2022-07-22T11:22:43.963501Z","shell.execute_reply":"2022-07-22T11:22:45.343811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:22:45.346380Z","iopub.execute_input":"2022-07-22T11:22:45.346937Z","iopub.status.idle":"2022-07-22T11:22:45.364877Z","shell.execute_reply.started":"2022-07-22T11:22:45.346897Z","shell.execute_reply":"2022-07-22T11:22:45.363984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:22:45.368477Z","iopub.execute_input":"2022-07-22T11:22:45.368731Z","iopub.status.idle":"2022-07-22T11:22:45.397343Z","shell.execute_reply.started":"2022-07-22T11:22:45.368708Z","shell.execute_reply":"2022-07-22T11:22:45.396513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}