{"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-28T03:34:00.300333Z","iopub.execute_input":"2022-07-28T03:34:00.300779Z","iopub.status.idle":"2022-07-28T03:34:00.335188Z","shell.execute_reply.started":"2022-07-28T03:34:00.300662Z","shell.execute_reply":"2022-07-28T03:34:00.333843Z"},"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()\n\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/train.zip') as existing_zip:\n    existing_zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:03.722342Z","iopub.execute_input":"2022-07-28T03:34:03.722935Z","iopub.status.idle":"2022-07-28T03:34:26.100641Z","shell.execute_reply.started":"2022-07-28T03:34:03.722858Z","shell.execute_reply":"2022-07-28T03:34:26.099450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:28.539880Z","iopub.execute_input":"2022-07-28T03:34:28.540297Z","iopub.status.idle":"2022-07-28T03:34:28.547864Z","shell.execute_reply.started":"2022-07-28T03:34:28.540268Z","shell.execute_reply":"2022-07-28T03:34:28.546469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:29.665800Z","iopub.execute_input":"2022-07-28T03:34:29.666233Z","iopub.status.idle":"2022-07-28T03:34:29.679469Z","shell.execute_reply.started":"2022-07-28T03:34:29.666189Z","shell.execute_reply":"2022-07-28T03:34:29.678143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:30.776465Z","iopub.execute_input":"2022-07-28T03:34:30.776866Z","iopub.status.idle":"2022-07-28T03:34:31.556568Z","shell.execute_reply.started":"2022-07-28T03:34:30.776835Z","shell.execute_reply":"2022-07-28T03:34:31.554987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:32.657777Z","iopub.execute_input":"2022-07-28T03:34:32.658626Z","iopub.status.idle":"2022-07-28T03:34:33.420668Z","shell.execute_reply.started":"2022-07-28T03:34:32.658579Z","shell.execute_reply":"2022-07-28T03:34:33.419130Z"},"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\n#画像の移動\nmove_glob('./dogs', 'dog*.jpg')\nmove_glob('./cats', 'cat*.jpg')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:34.610886Z","iopub.execute_input":"2022-07-28T03:34:34.611936Z","iopub.status.idle":"2022-07-28T03:34:35.847907Z","shell.execute_reply.started":"2022-07-28T03:34:34.611902Z","shell.execute_reply":"2022-07-28T03:34:35.846550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:37.658941Z","iopub.execute_input":"2022-07-28T03:34:37.660001Z","iopub.status.idle":"2022-07-28T03:34:38.567582Z","shell.execute_reply.started":"2022-07-28T03:34:37.659943Z","shell.execute_reply":"2022-07-28T03:34:38.566126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:39.618393Z","iopub.execute_input":"2022-07-28T03:34:39.618838Z","iopub.status.idle":"2022-07-28T03:34:39.629845Z","shell.execute_reply.started":"2022-07-28T03:34:39.618806Z","shell.execute_reply":"2022-07-28T03:34:39.628134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir valid","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:41.130847Z","iopub.execute_input":"2022-07-28T03:34:41.131752Z","iopub.status.idle":"2022-07-28T03:34:41.884570Z","shell.execute_reply.started":"2022-07-28T03:34:41.131707Z","shell.execute_reply":"2022-07-28T03:34:41.882793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd valid","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:43.171167Z","iopub.execute_input":"2022-07-28T03:34:43.171615Z","iopub.status.idle":"2022-07-28T03:34:43.181059Z","shell.execute_reply.started":"2022-07-28T03:34:43.171582Z","shell.execute_reply":"2022-07-28T03:34:43.179548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:44.354563Z","iopub.execute_input":"2022-07-28T03:34:44.355063Z","iopub.status.idle":"2022-07-28T03:34:45.118892Z","shell.execute_reply.started":"2022-07-28T03:34:44.355018Z","shell.execute_reply":"2022-07-28T03:34:45.117354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:46.243907Z","iopub.execute_input":"2022-07-28T03:34:46.244327Z","iopub.status.idle":"2022-07-28T03:34:47.007338Z","shell.execute_reply.started":"2022-07-28T03:34:46.244295Z","shell.execute_reply":"2022-07-28T03:34:47.005700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:34:48.228205Z","iopub.execute_input":"2022-07-28T03:34:48.229387Z","iopub.status.idle":"2022-07-28T03:34:48.239205Z","shell.execute_reply.started":"2022-07-28T03:34:48.229338Z","shell.execute_reply":"2022-07-28T03:34:48.237766Z"},"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    src_img = cv2.imread(src_file)\n    h, w, c = src_img.shape\n\n    # アス比固定, padding\n    scale_w = width / w\n    scale_h = height / h\n\n    ret_scale = 1.0\n\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\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":"2022-07-28T03:34:49.827933Z","iopub.execute_input":"2022-07-28T03:34:49.829297Z","iopub.status.idle":"2022-07-28T03:34:50.046761Z","shell.execute_reply.started":"2022-07-28T03:34:49.829253Z","shell.execute_reply":"2022-07-28T03:34:50.045435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#画像の作成\nfor 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\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-28T03:34:54.828294Z","iopub.execute_input":"2022-07-28T03:34:54.828753Z","iopub.status.idle":"2022-07-28T03:36:59.050840Z","shell.execute_reply.started":"2022-07-28T03:34:54.828721Z","shell.execute_reply":"2022-07-28T03:36:59.049547Z"},"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\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:38:34.673270Z","iopub.execute_input":"2022-07-28T03:38:34.673722Z","iopub.status.idle":"2022-07-28T03:38:41.136544Z","shell.execute_reply.started":"2022-07-28T03:38:34.673683Z","shell.execute_reply":"2022-07-28T03:38:41.135038Z"},"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-28T03:38:51.899182Z","iopub.execute_input":"2022-07-28T03:38:51.899859Z","iopub.status.idle":"2022-07-28T03:38:51.906343Z","shell.execute_reply.started":"2022-07-28T03:38:51.899826Z","shell.execute_reply":"2022-07-28T03:38:51.904506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(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(directory=train_data_dir, target_size=(224, 224), batch_size=64, class_mode=\"binary\")\nvalid_generator = valid_datagen.flow_from_directory(directory=valid_data_dir, target_size=(224, 224), batch_size=64, class_mode=\"binary\")","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:38:54.637232Z","iopub.execute_input":"2022-07-28T03:38:54.637597Z","iopub.status.idle":"2022-07-28T03:38:56.422285Z","shell.execute_reply.started":"2022-07-28T03:38:54.637568Z","shell.execute_reply":"2022-07-28T03:38:56.420814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64, kernel_size=(3,3), activation=\"relu\", kernel_regularizer=regularizers.l2(0.0001), input_shape=(224,224,3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128,kernel_size=(3,3), kernel_regularizer=regularizers.l2(0.0001),activation = \"relu\"))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(256,kernel_size=(3,3), kernel_regularizer=regularizers.l2(0.0001),activation = \"relu\"))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(512,kernel_size = (3,3),kernel_regularizer=regularizers.l2(0.0001),activation = \"relu\"))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(1024,kernel_size = (3,3),kernel_regularizer=regularizers.l2(0.0001),activation = \"relu\"))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\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)\n\nmodel.compile(loss=\"binary_crossentropy\", optimizer=opt, metrics=[\"accuracy\"])\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:48:45.629334Z","iopub.execute_input":"2022-07-28T03:48:45.629715Z","iopub.status.idle":"2022-07-28T03:48:45.746485Z","shell.execute_reply.started":"2022-07-28T03:48:45.629687Z","shell.execute_reply":"2022-07-28T03:48:45.745288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(train_generator,epochs = 100,verbose = 1, validation_data = (valid_generator), steps_per_epoch = 20000/64, validation_steps = 5000/64)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-07-24T15:27:33.780496Z","iopub.execute_input":"2022-07-24T15:27:33.781116Z","iopub.status.idle":"2022-07-24T15:27:33.901077Z","shell.execute_reply.started":"2022-07-24T15:27:33.781077Z","shell.execute_reply":"2022-07-24T15:27:33.900084Z"},"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-24T15:27:38.379118Z","iopub.execute_input":"2022-07-24T15:27:38.379466Z","iopub.status.idle":"2022-07-24T15:27:38.770529Z","shell.execute_reply.started":"2022-07-24T15:27:38.379437Z","shell.execute_reply":"2022-07-24T15:27:38.769491Z"},"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-24T15:28:07.243729Z","iopub.execute_input":"2022-07-24T15:28:07.244103Z","iopub.status.idle":"2022-07-24T15:28:07.249601Z","shell.execute_reply.started":"2022-07-24T15:28:07.244070Z","shell.execute_reply":"2022-07-24T15:28:07.248246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = load_model('model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T15:28:34.312188Z","iopub.execute_input":"2022-07-24T15:28:34.312553Z","iopub.status.idle":"2022-07-24T15:28:34.479001Z","shell.execute_reply.started":"2022-07-24T15:28:34.312501Z","shell.execute_reply":"2022-07-24T15:28:34.478022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#配列に結果を代入\nresult = []\nfor num in range(1, 12501):\n    TEST_PATH = \"test/\" + str(num) + '.jpg'\n    img = image.load_img(TEST_PATH, 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    result.append(result_predict)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-24T15:33:50.367252Z","iopub.execute_input":"2022-07-24T15:33:50.367625Z","iopub.status.idle":"2022-07-24T15:42:09.360835Z","shell.execute_reply.started":"2022-07-24T15:33:50.367593Z","shell.execute_reply":"2022-07-24T15:42:09.359827Z"},"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-24T15:44:11.607043Z","iopub.execute_input":"2022-07-24T15:44:11.607404Z","iopub.status.idle":"2022-07-24T15:44:11.614322Z","shell.execute_reply.started":"2022-07-24T15:44:11.607374Z","shell.execute_reply":"2022-07-24T15:44:11.613065Z"},"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-24T15:44:16.770874Z","iopub.execute_input":"2022-07-24T15:44:16.771548Z","iopub.status.idle":"2022-07-24T15:44:19.499311Z","shell.execute_reply.started":"2022-07-24T15:44:16.771483Z","shell.execute_reply":"2022-07-24T15:44:19.497971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-07-24T15:44:22.300963Z","iopub.execute_input":"2022-07-24T15:44:22.301423Z","iopub.status.idle":"2022-07-24T15:44:22.325883Z","shell.execute_reply.started":"2022-07-24T15:44:22.301381Z","shell.execute_reply":"2022-07-24T15:44:22.324236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission2.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T15:44:44.881648Z","iopub.execute_input":"2022-07-24T15:44:44.882121Z","iopub.status.idle":"2022-07-24T15:44:44.911383Z","shell.execute_reply.started":"2022-07-24T15:44:44.882081Z","shell.execute_reply":"2022-07-24T15:44:44.910535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}