{"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":"2023-07-26T10:32:41.328528Z","iopub.execute_input":"2023-07-26T10:32:41.328875Z","iopub.status.idle":"2023-07-26T10:32:41.338326Z","shell.execute_reply.started":"2023-07-26T10:32:41.328846Z","shell.execute_reply":"2023-07-26T10:32:41.336827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST1_DIR = 'test1'\nTRAIN_DIR = 'train'\nINPUT_DIR = '../input/dogs-vs-cats'\nOUTPUT_DIR = '/output/'","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:41.346767Z","iopub.execute_input":"2023-07-26T10:32:41.347637Z","iopub.status.idle":"2023-07-26T10:32:41.354838Z","shell.execute_reply.started":"2023-07-26T10:32:41.347585Z","shell.execute_reply":"2023-07-26T10:32:41.353937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test1', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:41.356399Z","iopub.execute_input":"2023-07-26T10:32:41.357118Z","iopub.status.idle":"2023-07-26T10:32:50.631822Z","shell.execute_reply.started":"2023-07-26T10:32:41.357087Z","shell.execute_reply":"2023-07-26T10:32:50.630023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = os.listdir(TRAIN_DIR)\nprint('There are {} number of images in directory.'.format(len(file_names)))","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:50.633622Z","iopub.execute_input":"2023-07-26T10:32:50.634002Z","iopub.status.idle":"2023-07-26T10:32:50.649505Z","shell.execute_reply.started":"2023-07-26T10:32:50.633970Z","shell.execute_reply":"2023-07-26T10:32:50.648102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_dataframe(file_names):\n    files, labels = list(), list()\n    for file in file_names:\n        files.append(file)\n        labels.append(file[:3])\n    df = pd.DataFrame({'filename':files, 'label':labels})\n    return df\n\ndf = to_dataframe(file_names)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:50.651293Z","iopub.execute_input":"2023-07-26T10:32:50.651633Z","iopub.status.idle":"2023-07-26T10:32:50.665209Z","shell.execute_reply.started":"2023-07-26T10:32:50.651605Z","shell.execute_reply":"2023-07-26T10:32:50.664146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:50.668494Z","iopub.execute_input":"2023-07-26T10:32:50.668945Z","iopub.status.idle":"2023-07-26T10:32:50.682815Z","shell.execute_reply.started":"2023-07-26T10:32:50.668912Z","shell.execute_reply":"2023-07-26T10:32:50.681479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat = [file for file in file_names if file[:3]=='cat'] # cat images\nprint(\"Images for cat =\",len(cat))\ndog = [file for file in file_names if file[:3]=='dog'] # dog images\nprint(\"Images for dog =\",len(dog))","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:50.684187Z","iopub.execute_input":"2023-07-26T10:32:50.684573Z","iopub.status.idle":"2023-07-26T10:32:50.704820Z","shell.execute_reply.started":"2023-07-26T10:32:50.684512Z","shell.execute_reply":"2023-07-26T10:32:50.703851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2 \nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nimport random\n\nplt.figure(figsize = (18,9))\nindex = random.randint(1,12000)\nfor i, c in enumerate(range(index, index + 5), start=1):\n    im = img.imread('./train/'+cat[c])\n    plt.subplot(2,5,i)\n    plt.imshow(im)\n    \nfor i, c in enumerate(range(index,index + 5), start=6):\n    im = img.imread('./train/'+dog[c])\n    plt.subplot(2,5,i)\n    plt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:50.705934Z","iopub.execute_input":"2023-07-26T10:32:50.706200Z","iopub.status.idle":"2023-07-26T10:32:52.719729Z","shell.execute_reply.started":"2023-07-26T10:32:50.706175Z","shell.execute_reply":"2023-07-26T10:32:52.718601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 画像のリサイズ","metadata":{}},{"cell_type":"code","source":"from PIL import Image, ImageFilter, ImageEnhance\nresized_images=[]\nfor file in file_names:\n    img=Image.open('./train/'+file)\n    resized_images.append(img.resize((64,64)))\n    \nfig, ax = plt.subplots(1, 2, figsize=(15, 5));\nimg=Image.open('./train/'+file_names[0])\nax[0].imshow(img)\nax[1].imshow(resized_images[0])","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:32:52.721016Z","iopub.execute_input":"2023-07-26T10:32:52.723742Z","iopub.status.idle":"2023-07-26T10:33:42.980013Z","shell.execute_reply.started":"2023-07-26T10:32:52.723686Z","shell.execute_reply":"2023-07-26T10:33:42.978577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 画像をグレースケールに変換","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(15, 5));\n\nimg=resized_images[25]\nax[0].imshow(img);\n\ngrey_scale_img = img.convert('LA')\nax[1].imshow(grey_scale_img);","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:42.981641Z","iopub.execute_input":"2023-07-26T10:33:42.982128Z","iopub.status.idle":"2023-07-26T10:33:43.309065Z","shell.execute_reply.started":"2023-07-26T10:33:42.982099Z","shell.execute_reply":"2023-07-26T10:33:43.308220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## エッジ検出","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(15, 5));\n\nimg=resized_images[100]\nimage = img.convert(\"L\")\n\nimage = image.filter(ImageFilter.FIND_EDGES)\nax[0].imshow(img);\nax[1].imshow(image);","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:43.310031Z","iopub.execute_input":"2023-07-26T10:33:43.310257Z","iopub.status.idle":"2023-07-26T10:33:43.644848Z","shell.execute_reply.started":"2023-07-26T10:33:43.310235Z","shell.execute_reply":"2023-07-26T10:33:43.643927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from numpy import asarray\nplt.figure(figsize=(15,15))\n# Read the original image\nimg=resized_images[105]\nimg=asarray(img)\n\n# より良いエッジ検出のために画像をぼかす\nimg_blur = cv2.GaussianBlur(img, (3,3), 0) \n \n# ソーベルエッジ検出\nsobelx = cv2.Sobel(src=img_blur, ddepth=cv2.CV_16S, dx=1, dy=0, ksize=3) # X軸\nsobely = cv2.Sobel(src=img_blur, ddepth=cv2.CV_16S, dx=0, dy=1, ksize=3) # Y軸\nsobelxy = cv2.Sobel(src=img_blur, ddepth=cv2.CV_16S, dx=1, dy=1, ksize=5) # XとYの複合ソーベルエッジ検出\n\n# ラプラシアンエッジ検出\nlaplacian = cv2.Laplacian(img,ksize=3, ddepth=cv2.CV_16S)\n# キャニーエッジ検出\ncanny = cv2.Canny(image=img_blur, threshold1=100, threshold2=200) # Canny Edge Detection\n\nplt.subplot(3,2,1),plt.imshow(img)\nplt.title('Original'), plt.xticks([]), plt.yticks([])\n\nplt.subplot(3,2,2),plt.imshow(laplacian)\nplt.title('Laplacian'), plt.xticks([]), plt.yticks([])\n\nplt.subplot(3,2,3),plt.imshow(canny)\nplt.title('Canny'), plt.xticks([]), plt.yticks([])\n\nplt.subplot(3,2,4),plt.imshow(sobelxy)\nplt.title('Sobel XY'), plt.xticks([]), plt.yticks([])\n\nplt.subplot(3,2,5),plt.imshow(sobelx)\nplt.title('Sobel X'), plt.xticks([]), plt.yticks([])\n\nplt.subplot(3,2,6),plt.imshow(sobely)\nplt.title('Sobel Y'), plt.xticks([]), plt.yticks([]);","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:43.645956Z","iopub.execute_input":"2023-07-26T10:33:43.646230Z","iopub.status.idle":"2023-07-26T10:33:44.022013Z","shell.execute_reply.started":"2023-07-26T10:33:43.646206Z","shell.execute_reply":"2023-07-26T10:33:44.021182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ガウシアンフィルタリング","metadata":{}},{"cell_type":"code","source":"img=resized_images[50]\nimg=asarray(img)\n\nblur = cv2.GaussianBlur(img,(5,5),0)\n\nfig, ax = plt.subplots(1, 2, figsize=(15, 5));\nplt.suptitle('RESULT', x=0.5, y=0.8)\nplt.tight_layout()\n\nax[0].set_title('ORIGINAL', fontsize=12)\nax[1].set_title('GAUSSIAN BLUR', fontsize=12)\n\nax[0].imshow(img);\nax[1].imshow(blur);","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:44.022991Z","iopub.execute_input":"2023-07-26T10:33:44.023496Z","iopub.status.idle":"2023-07-26T10:33:44.473341Z","shell.execute_reply.started":"2023-07-26T10:33:44.023468Z","shell.execute_reply":"2023-07-26T10:33:44.472459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(TRAIN_DIR)\nfilenames[:5]","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:44.474874Z","iopub.execute_input":"2023-07-26T10:33:44.475257Z","iopub.status.idle":"2023-07-26T10:33:44.495938Z","shell.execute_reply.started":"2023-07-26T10:33:44.475217Z","shell.execute_reply":"2023-07-26T10:33:44.495005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [1 if filename.startswith('dog') else 0 for filename in filenames]\nlabels[:5]","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:44.500310Z","iopub.execute_input":"2023-07-26T10:33:44.500866Z","iopub.status.idle":"2023-07-26T10:33:44.513021Z","shell.execute_reply.started":"2023-07-26T10:33:44.500814Z","shell.execute_reply":"2023-07-26T10:33:44.511820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\n    'filename': filenames,\n    'category': labels\n})\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:44.513959Z","iopub.execute_input":"2023-07-26T10:33:44.514208Z","iopub.status.idle":"2023-07-26T10:33:44.537760Z","shell.execute_reply.started":"2023-07-26T10:33:44.514186Z","shell.execute_reply":"2023-07-26T10:33:44.536094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = df.category.value_counts().plot.bar(color=['dodgerblue', 'slategray'])\nplt.title('Dogs and Cats images count')\nplt.xlabel('Dog = 1 - Cat = 0')\nplt.ylabel('samples count')\nax.set_xticklabels(['Dog', 'Cat'], rotation=0, fontsize=11)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:44.538753Z","iopub.execute_input":"2023-07-26T10:33:44.539059Z","iopub.status.idle":"2023-07-26T10:33:44.722360Z","shell.execute_reply.started":"2023-07-26T10:33:44.539034Z","shell.execute_reply":"2023-07-26T10:33:44.720870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_images_from_dir(dir_location, filenames):\n    return [np.array(Image.open('{}/{}'.format(dir_location, filename)).resize((64, 64))) for filename in filenames]\n    \nimages = np.array(load_images_from_dir(TRAIN_DIR, filenames))\nimages.shape\n\n# Dimesion Reduction\nprint(images[0].shape)\nh, w, d = images[0].shape\nimages_resized = np.array([np.reshape(img, (w*h*d)) for img in images])\nimages_resized.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-26T10:33:44.723866Z","iopub.execute_input":"2023-07-26T10:33:44.724147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dimesion Reduction\nprint(images[0].shape)\nh, w, d = images[0].shape\nimages_resized = np.array([np.reshape(img, (w*h*d)) for img in images])\nprint(images_resized.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}