{"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-20T05:14:04.61274Z","iopub.execute_input":"2022-07-20T05:14:04.613553Z","iopub.status.idle":"2022-07-20T05:14:04.64827Z","shell.execute_reply.started":"2022-07-20T05:14:04.613123Z","shell.execute_reply":"2022-07-20T05:14:04.646768Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:04.65045Z","iopub.execute_input":"2022-07-20T05:14:04.651355Z","iopub.status.idle":"2022-07-20T05:14:28.23145Z","shell.execute_reply.started":"2022-07-20T05:14:04.651314Z","shell.execute_reply":"2022-07-20T05:14:28.230229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:28.233475Z","iopub.execute_input":"2022-07-20T05:14:28.23436Z","iopub.status.idle":"2022-07-20T05:14:28.244443Z","shell.execute_reply.started":"2022-07-20T05:14:28.234314Z","shell.execute_reply":"2022-07-20T05:14:28.242209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:28.248878Z","iopub.execute_input":"2022-07-20T05:14:28.249411Z","iopub.status.idle":"2022-07-20T05:14:28.999494Z","shell.execute_reply.started":"2022-07-20T05:14:28.249364Z","shell.execute_reply":"2022-07-20T05:14:28.997925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:29.003387Z","iopub.execute_input":"2022-07-20T05:14:29.004426Z","iopub.status.idle":"2022-07-20T05:14:29.75183Z","shell.execute_reply.started":"2022-07-20T05:14:29.004347Z","shell.execute_reply":"2022-07-20T05:14:29.750214Z"},"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-20T05:14:29.753957Z","iopub.execute_input":"2022-07-20T05:14:29.755291Z","iopub.status.idle":"2022-07-20T05:14:31.155601Z","shell.execute_reply.started":"2022-07-20T05:14:29.755232Z","shell.execute_reply":"2022-07-20T05:14:31.154314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:31.157877Z","iopub.execute_input":"2022-07-20T05:14:31.15869Z","iopub.status.idle":"2022-07-20T05:14:31.169048Z","shell.execute_reply.started":"2022-07-20T05:14:31.158647Z","shell.execute_reply":"2022-07-20T05:14:31.167828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir valid","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:31.171151Z","iopub.execute_input":"2022-07-20T05:14:31.172827Z","iopub.status.idle":"2022-07-20T05:14:32.03786Z","shell.execute_reply.started":"2022-07-20T05:14:31.172786Z","shell.execute_reply":"2022-07-20T05:14:32.036326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd valid","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:32.040431Z","iopub.execute_input":"2022-07-20T05:14:32.040939Z","iopub.status.idle":"2022-07-20T05:14:32.049546Z","shell.execute_reply.started":"2022-07-20T05:14:32.040892Z","shell.execute_reply":"2022-07-20T05:14:32.048248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:32.056209Z","iopub.execute_input":"2022-07-20T05:14:32.057188Z","iopub.status.idle":"2022-07-20T05:14:32.804997Z","shell.execute_reply.started":"2022-07-20T05:14:32.057158Z","shell.execute_reply":"2022-07-20T05:14:32.803495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:32.807245Z","iopub.execute_input":"2022-07-20T05:14:32.807676Z","iopub.status.idle":"2022-07-20T05:14:33.62331Z","shell.execute_reply.started":"2022-07-20T05:14:32.807632Z","shell.execute_reply":"2022-07-20T05:14:33.621612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:33.626636Z","iopub.execute_input":"2022-07-20T05:14:33.62715Z","iopub.status.idle":"2022-07-20T05:14:33.651754Z","shell.execute_reply.started":"2022-07-20T05:14:33.627103Z","shell.execute_reply":"2022-07-20T05:14:33.647622Z"},"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","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:14:33.655741Z","iopub.execute_input":"2022-07-20T05:14:33.660245Z","iopub.status.idle":"2022-07-20T05:14:34.12682Z","shell.execute_reply.started":"2022-07-20T05:14:33.659162Z","shell.execute_reply":"2022-07-20T05:14:34.123473Z"},"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-20T05:14:34.131056Z","iopub.execute_input":"2022-07-20T05:14:34.131472Z","iopub.status.idle":"2022-07-20T05:16:37.869181Z","shell.execute_reply.started":"2022-07-20T05:14:34.131431Z","shell.execute_reply":"2022-07-20T05:16:37.867787Z"},"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-20T05:16:37.871234Z","iopub.execute_input":"2022-07-20T05:16:37.871833Z","iopub.status.idle":"2022-07-20T05:16:44.388171Z","shell.execute_reply.started":"2022-07-20T05:16:37.871792Z","shell.execute_reply":"2022-07-20T05:16:44.386883Z"},"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-20T05:16:44.389671Z","iopub.execute_input":"2022-07-20T05:16:44.390401Z","iopub.status.idle":"2022-07-20T05:16:44.396246Z","shell.execute_reply.started":"2022-07-20T05:16:44.390359Z","shell.execute_reply":"2022-07-20T05:16:44.395218Z"},"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-20T05:16:44.397835Z","iopub.execute_input":"2022-07-20T05:16:44.399434Z","iopub.status.idle":"2022-07-20T05:16:45.892051Z","shell.execute_reply.started":"2022-07-20T05:16:44.399389Z","shell.execute_reply":"2022-07-20T05:16:45.890717Z"},"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-20T05:16:45.89491Z","iopub.execute_input":"2022-07-20T05:16:45.895775Z","iopub.status.idle":"2022-07-20T05:16:49.264328Z","shell.execute_reply.started":"2022-07-20T05:16:45.895732Z","shell.execute_reply":"2022-07-20T05:16:49.262919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T05:16:49.266234Z","iopub.execute_input":"2022-07-20T05:16:49.266997Z","iopub.status.idle":"2022-07-20T05:16:49.278144Z","shell.execute_reply.started":"2022-07-20T05:16:49.266953Z","shell.execute_reply":"2022-07-20T05:16:49.276697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, \n                    epochs=30, \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-20T05:16:49.280436Z","iopub.execute_input":"2022-07-20T05:16:49.281114Z"},"trusted":true},"execution_count":null,"outputs":[]}]}