{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0437ffe3b12fb995ee360a62877744ebe1a5dc8e"},"cell_type":"code","source":"PATH = \"/kaggle/input\"\nsz=224\nbatch_size=64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2793fca632578db520ecba74614090878b03273"},"cell_type":"code","source":"import numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.applications import ResNet50\nfrom keras.models import Model, Sequential\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras import backend as K\nfrom keras.applications.resnet50 import preprocess_input\nfrom keras.layers import Conv2D, MaxPooling2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4412a43effdd519dc0d4af138194b3094a0ab96c"},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\ntrain_datagen = ImageDataGenerator(\n    rotation_range=30,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest')\ntest_datagen=ImageDataGenerator(rescale=1./255)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29c961dd244c7bd1f2d7ba59f711a73a97353c68"},"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\"/kaggle/input/data/train/\",\n    target_size=(sz, sz),\n    batch_size=batch_size, class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df0557528dde1a170818d989a30864e1f44a5a86"},"cell_type":"code","source":"test_set = test_datagen.flow_from_directory(\"/kaggle/input/data/validation/\",\n                                            target_size=(sz, sz),\n    batch_size=batch_size, class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8caabdb044517e4d75d8626cc9f58d5a69ea710"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), input_shape=(sz,sz,3), activation = 'relu' ))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af4ad4d5780372807ad6713bdf934008cc3d783f"},"cell_type":"code","source":"#pooling\nmodel.add(MaxPooling2D(pool_size=(2,2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38101a191b6d8e3d4d815845fd13d58d623a6d14"},"cell_type":"code","source":"#adding 2nd layer\nmodel.add(Conv2D(32, (3, 3), input_shape=(sz,sz,3), activation = 'relu' ))\nmodel.add(MaxPooling2D(pool_size=(2,2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88724b810c17b35a522e7d1b36a669dd4789d643"},"cell_type":"code","source":"#flatenning \nmodel.add(Flatten())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c77653561bc4709f42fb08df0466c1ae842f55c1"},"cell_type":"code","source":"#adding connection\nmodel.add(Dense(units=128,activation='relu'))\nmodel.add(Dense(units=1,activation='sigmoid'))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"720c7ddacba368061aa4b6a4e8a6e99275a66002"},"cell_type":"code","source":"#compiling CNN\nmodel.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3504e302a0229125d9949fcc458332e1643cbe18"},"cell_type":"code","source":"from keras.utils.vis_utils import plot_model\nimport matplotlib.pyplot as plt\nplot_model(model, to_file='/tmp/model_plot.png', show_shapes=True, show_layer_names=True)\n\n#Image('/tmp/model_plot.png')\n#I = imread ();\nimg = plt.imread('/tmp/model_plot.png')\nplt.imshow(img)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f21244743c63a5e390c394003dd078ef7c0248d"},"cell_type":"code","source":"model.summary()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0d1792e0c5fffa711860d8e20d45867a07fe5b6"},"cell_type":"code","source":"#fitting model to data\nmodel.fit_generator(train_generator, train_generator.n // batch_size, epochs=10, workers=4,\n                   validation_data=test_set, validation_steps=test_set.n // batch_size)\n\n\nmodel.save_weights('/tmp/cat_dog_1.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"976691ee2c641914cc0b2279f82758136311b261"},"cell_type":"code","source":"img = image.load_img('/kaggle/input/data/evaluation/dog/105.jpg', target_size=(224, 224))\nx = image.img_to_array(img)\nx = np.expand_dims(x, axis=0)\n\nimages = np.vstack([x])\nclasses = model.predict_classes(images, batch_size=10)\nprint (classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3760e2971515173db701ee422ef7b40c4f9557a"},"cell_type":"code","source":"eval_set = test_datagen.flow_from_directory(\"/kaggle/input/data/evaluation/\",\n                                            target_size=(sz, sz),\n    batch_size=batch_size, class_mode='binary')\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7e2bb19358c1824a23ea498ce0a8941b1773993"},"cell_type":"code","source":"model.predict_classes(eval_set)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d8fe541f0b8493d69ac334aded1d2f95ec20c69"},"cell_type":"code","source":"model.load_weights('/tmp/cat_dog_1.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dfddba908d1191dc4d29b097d48b775cd0b4de91"},"cell_type":"code","source":"img = image.load_img('/kaggle/input/data/evaluation/cat/114.jpg', target_size=(224, 224))\nx = image.img_to_array(img)\nx = np.expand_dims(x, axis=0)\n\nimages = np.vstack([x])\nclasses = model.predict_classes(images, batch_size=10)\nprint (classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1cd96293cc0226520a14d69ff46d89dcf8f1d27"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}