{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nfrom os import listdir\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\n\nprint(os.listdir('../input/dogs-vs-cats/'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plot dog photos from the dogs vs cats dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"# define location of dataset\nfolder = '../input/dogs-vs-cats/train/train/'\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tplt.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'dog.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tplt.imshow(image)\n# show the figure\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Cats images"},{"metadata":{"trusted":true},"cell_type":"code","source":"# define location of dataset\nfolder = '../input/dogs-vs-cats/train/train/'\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tplt.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'cat.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tplt.imshow(image)\n# show the figure\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load dogs vs cats dataset, reshape and save to a new file"},{"metadata":{"trusted":true},"cell_type":"code","source":"# define location of dataset\nfolder = '../input/dogs-vs-cats/train/train/'\nphotos, labels = list(), list()\n# enumerate files in the directory\nfor file in listdir(folder):\n\t# determine class\n\toutput = 0.0\n\tif file.startswith('cat'):\n\t\toutput = 1.0\n\t# load image\n\tphoto = load_img(folder + file, target_size=(100, 100))\n\t# convert to numpy array\n\tphoto = img_to_array(photo)\n\t# store\n\tphotos.append(photo)\n\tlabels.append(output)\n# convert to a numpy arrays\nphotos = np.asarray(photos)\nlabels = np.asarray(labels)\nprint(photos.shape, labels.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"\n# Save the reshaped photos"},{"metadata":{"trusted":true},"cell_type":"code","source":"np.save('dogs_vs_cats_photos.npy', photos)\nnp.save('dogs_vs_cats_labels.npy', labels)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load and confirm the shape"},{"metadata":{"trusted":true},"cell_type":"code","source":"from numpy import load\nphotos = load('dogs_vs_cats_photos.npy')\nlabels = load('dogs_vs_cats_labels.npy')\nprint(photos.shape, labels.shape)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}