{"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":"2021-07-02T00:12:36.431916Z","iopub.execute_input":"2021-07-02T00:12:36.432261Z","iopub.status.idle":"2021-07-02T00:12:51.078381Z","shell.execute_reply.started":"2021-07-02T00:12:36.432229Z","shell.execute_reply":"2021-07-02T00:12:51.075143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2\n# Technically not necessary in newest versions of jupyter meh\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.083102Z","iopub.execute_input":"2021-07-02T00:12:51.083345Z","iopub.status.idle":"2021-07-02T00:12:51.407316Z","shell.execute_reply.started":"2021-07-02T00:12:51.083319Z","shell.execute_reply":"2021-07-02T00:12:51.406483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat4 = cv2.imread('../input/cats-dogs/CATS_DOGS/train/CAT/921.jpg')\ncat4 = cv2.cvtColor(cat4,cv2.COLOR_BGR2RGB)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.409059Z","iopub.execute_input":"2021-07-02T00:12:51.409408Z","iopub.status.idle":"2021-07-02T00:12:51.450732Z","shell.execute_reply.started":"2021-07-02T00:12:51.409373Z","shell.execute_reply":"2021-07-02T00:12:51.450006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(cat4)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.453551Z","iopub.execute_input":"2021-07-02T00:12:51.453793Z","iopub.status.idle":"2021-07-02T00:12:51.459762Z","shell.execute_reply.started":"2021-07-02T00:12:51.453769Z","shell.execute_reply":"2021-07-02T00:12:51.458929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat4.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.461064Z","iopub.execute_input":"2021-07-02T00:12:51.461449Z","iopub.status.idle":"2021-07-02T00:12:51.472270Z","shell.execute_reply.started":"2021-07-02T00:12:51.461393Z","shell.execute_reply":"2021-07-02T00:12:51.471456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cat4)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.473468Z","iopub.execute_input":"2021-07-02T00:12:51.473793Z","iopub.status.idle":"2021-07-02T00:12:51.661115Z","shell.execute_reply.started":"2021-07-02T00:12:51.473760Z","shell.execute_reply":"2021-07-02T00:12:51.660176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog2 = cv2.imread('../input/cats-dogs/CATS_DOGS/train/DOG/921.jpg')\ndog2 = cv2.cvtColor(dog2,cv2.COLOR_BGR2RGB)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.662439Z","iopub.execute_input":"2021-07-02T00:12:51.662782Z","iopub.status.idle":"2021-07-02T00:12:51.673992Z","shell.execute_reply.started":"2021-07-02T00:12:51.662746Z","shell.execute_reply":"2021-07-02T00:12:51.673140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog2.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.676728Z","iopub.execute_input":"2021-07-02T00:12:51.677065Z","iopub.status.idle":"2021-07-02T00:12:51.681926Z","shell.execute_reply.started":"2021-07-02T00:12:51.677033Z","shell.execute_reply":"2021-07-02T00:12:51.680992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(dog2)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.683924Z","iopub.execute_input":"2021-07-02T00:12:51.684457Z","iopub.status.idle":"2021-07-02T00:12:51.820715Z","shell.execute_reply.started":"2021-07-02T00:12:51.684422Z","shell.execute_reply":"2021-07-02T00:12:51.819803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.821975Z","iopub.execute_input":"2021-07-02T00:12:51.822318Z","iopub.status.idle":"2021-07-02T00:12:51.827721Z","shell.execute_reply.started":"2021-07-02T00:12:51.822284Z","shell.execute_reply":"2021-07-02T00:12:51.826702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_gen = ImageDataGenerator(rotation_range=30, # rotate the image 30 degrees\n                               width_shift_range=0.1, # Shift the pic width by a max of 10%\n                               height_shift_range=0.1, # Shift the pic height by a max of 10%\n                               rescale=1/255, # Rescale the image by normalzing it.\n                               shear_range=0.2, # Shear means cutting away part of the image (max 20%)\n                               zoom_range=0.2, # Zoom in by 20% max\n                               horizontal_flip=True, # Allo horizontal flipping\n                               fill_mode='nearest' # Fill in missing pixels with the nearest filled value\n                              )","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.829242Z","iopub.execute_input":"2021-07-02T00:12:51.829626Z","iopub.status.idle":"2021-07-02T00:12:51.835829Z","shell.execute_reply.started":"2021-07-02T00:12:51.829590Z","shell.execute_reply":"2021-07-02T00:12:51.834711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image_gen.random_transform(dog2))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.837324Z","iopub.execute_input":"2021-07-02T00:12:51.837704Z","iopub.status.idle":"2021-07-02T00:12:51.983863Z","shell.execute_reply.started":"2021-07-02T00:12:51.837630Z","shell.execute_reply":"2021-07-02T00:12:51.982965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image_gen.random_transform(dog2))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:51.985181Z","iopub.execute_input":"2021-07-02T00:12:51.985513Z","iopub.status.idle":"2021-07-02T00:12:52.127745Z","shell.execute_reply.started":"2021-07-02T00:12:51.985479Z","shell.execute_reply":"2021-07-02T00:12:52.126786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image_gen.random_transform(dog2))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:52.128975Z","iopub.execute_input":"2021-07-02T00:12:52.129325Z","iopub.status.idle":"2021-07-02T00:12:52.279706Z","shell.execute_reply.started":"2021-07-02T00:12:52.129290Z","shell.execute_reply":"2021-07-02T00:12:52.278755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_gen.flow_from_directory('../input/cats-dogs/CATS_DOGS/train')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:52.281015Z","iopub.execute_input":"2021-07-02T00:12:52.281397Z","iopub.status.idle":"2021-07-02T00:12:53.275284Z","shell.execute_reply.started":"2021-07-02T00:12:52.281361Z","shell.execute_reply":"2021-07-02T00:12:53.274512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_gen.flow_from_directory('../input/cats-dogs/CATS_DOGS/test')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:53.277246Z","iopub.execute_input":"2021-07-02T00:12:53.277595Z","iopub.status.idle":"2021-07-02T00:12:53.611624Z","shell.execute_reply.started":"2021-07-02T00:12:53.277560Z","shell.execute_reply":"2021-07-02T00:12:53.610885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# width,height,channels\nimage_shape = (150,150,3)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:53.612826Z","iopub.execute_input":"2021-07-02T00:12:53.613180Z","iopub.status.idle":"2021-07-02T00:12:53.619173Z","shell.execute_reply.started":"2021-07-02T00:12:53.613144Z","shell.execute_reply":"2021-07-02T00:12:53.618394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Activation, Dropout, Flatten, Dense, Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:53.620394Z","iopub.execute_input":"2021-07-02T00:12:53.620741Z","iopub.status.idle":"2021-07-02T00:12:53.626299Z","shell.execute_reply.started":"2021-07-02T00:12:53.620708Z","shell.execute_reply":"2021-07-02T00:12:53.625057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters=32, kernel_size=(3,3),input_shape=(150,150,3), activation='relu',))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=(150,150,3), activation='relu',))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=(150,150,3), activation='relu',))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\n\nmodel.add(Flatten())\n\n\nmodel.add(Dense(128))\nmodel.add(Activation('relu'))\n\n# Dropouts help reduce overfitting by randomly turning neurons off during training.\n# Here we say randomly turn off 50% of neurons.\nmodel.add(Dropout(0.5))\n\n# Last layer, remember its binary, 0=cat , 1=dog\nmodel.add(Dense(1))\nmodel.add(Activation('sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:53.627767Z","iopub.execute_input":"2021-07-02T00:12:53.628138Z","iopub.status.idle":"2021-07-02T00:12:53.701724Z","shell.execute_reply.started":"2021-07-02T00:12:53.628104Z","shell.execute_reply":"2021-07-02T00:12:53.700908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:53.702884Z","iopub.execute_input":"2021-07-02T00:12:53.703209Z","iopub.status.idle":"2021-07-02T00:12:53.713697Z","shell.execute_reply.started":"2021-07-02T00:12:53.703177Z","shell.execute_reply":"2021-07-02T00:12:53.712922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\n\ntrain_image_gen = image_gen.flow_from_directory('../input/cats-dogs/CATS_DOGS/train',\n                                               target_size=image_shape[:2],\n                                               batch_size=batch_size,\n                                               class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:53.716219Z","iopub.execute_input":"2021-07-02T00:12:53.716466Z","iopub.status.idle":"2021-07-02T00:12:54.721161Z","shell.execute_reply.started":"2021-07-02T00:12:53.716443Z","shell.execute_reply":"2021-07-02T00:12:54.720363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_gen = image_gen.flow_from_directory('../input/cats-dogs/CATS_DOGS/test',\n                                               target_size=image_shape[:2],\n                                               batch_size=batch_size,\n                                               class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:54.722371Z","iopub.execute_input":"2021-07-02T00:12:54.722715Z","iopub.status.idle":"2021-07-02T00:12:55.041571Z","shell.execute_reply.started":"2021-07-02T00:12:54.722679Z","shell.execute_reply":"2021-07-02T00:12:55.040703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_gen.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:55.044951Z","iopub.execute_input":"2021-07-02T00:12:55.045218Z","iopub.status.idle":"2021-07-02T00:12:55.050044Z","shell.execute_reply.started":"2021-07-02T00:12:55.045193Z","shell.execute_reply":"2021-07-02T00:12:55.049161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore') # ONLY GOD CAN JUDGE ME","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:55.051417Z","iopub.execute_input":"2021-07-02T00:12:55.051906Z","iopub.status.idle":"2021-07-02T00:12:55.059100Z","shell.execute_reply.started":"2021-07-02T00:12:55.051869Z","shell.execute_reply":"2021-07-02T00:12:55.057980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.fit_generator(train_image_gen,epochs=100,\n                              steps_per_epoch=150,\n                              validation_data=test_image_gen,\n                             validation_steps=12)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:12:55.060337Z","iopub.execute_input":"2021-07-02T00:12:55.060724Z","iopub.status.idle":"2021-07-02T00:48:55.779330Z","shell.execute_reply.started":"2021-07-02T00:12:55.060691Z","shell.execute_reply":"2021-07-02T00:48:55.778460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.history['accuracy']","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:52:00.360711Z","iopub.execute_input":"2021-07-02T00:52:00.361142Z","iopub.status.idle":"2021-07-02T00:52:00.372165Z","shell.execute_reply.started":"2021-07-02T00:52:00.361101Z","shell.execute_reply":"2021-07-02T00:52:00.371260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(results.history['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:52:03.980523Z","iopub.execute_input":"2021-07-02T00:52:03.980840Z","iopub.status.idle":"2021-07-02T00:52:04.108844Z","shell.execute_reply.started":"2021-07-02T00:52:03.980812Z","shell.execute_reply":"2021-07-02T00:52:04.107928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_gen.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-07-02T00:52:07.052713Z","iopub.execute_input":"2021-07-02T00:52:07.053040Z","iopub.status.idle":"2021-07-02T00:52:07.061507Z","shell.execute_reply.started":"2021-07-02T00:52:07.053009Z","shell.execute_reply":"2021-07-02T00:52:07.060722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom keras.preprocessing import image\n\ndog_file = '../input/cats-dogs/CATS_DOGS/train/DOG/10.jpg'\nraccon_file = '../input/xxxxxx/trashpanda.jpg'\n\ndog_img = image.load_img(dog_file, target_size=(150, 150))\n\ndog_img = image.img_to_array(dog_img)\n\ndog_img = np.expand_dims(dog_img, axis=0)\ndog_img = dog_img/255","metadata":{"execution":{"iopub.status.busy":"2021-07-02T01:08:00.770036Z","iopub.execute_input":"2021-07-02T01:08:00.770396Z","iopub.status.idle":"2021-07-02T01:08:00.781598Z","shell.execute_reply.started":"2021-07-02T01:08:00.770366Z","shell.execute_reply":"2021-07-02T01:08:00.780752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_prob = model.predict(dog_img)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T01:08:00.927574Z","iopub.execute_input":"2021-07-02T01:08:00.927865Z","iopub.status.idle":"2021-07-02T01:08:00.969006Z","shell.execute_reply.started":"2021-07-02T01:08:00.927839Z","shell.execute_reply":"2021-07-02T01:08:00.968175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Output prediction\nprint(f'Probability that image is a dog is: {prediction_prob} ')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T01:08:01.089553Z","iopub.execute_input":"2021-07-02T01:08:01.089816Z","iopub.status.idle":"2021-07-02T01:08:01.095171Z","shell.execute_reply.started":"2021-07-02T01:08:01.089790Z","shell.execute_reply":"2021-07-02T01:08:01.093350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trashpanda = cv2.imread('../input/xxxxxx/trashpanda.jpg')\nplt.imshow(trashpanda)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T01:07:51.704532Z","iopub.execute_input":"2021-07-02T01:07:51.704838Z","iopub.status.idle":"2021-07-02T01:07:51.940711Z","shell.execute_reply.started":"2021-07-02T01:07:51.704811Z","shell.execute_reply":"2021-07-02T01:07:51.939879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}