{"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-11-24T07:33:09.077065Z","iopub.execute_input":"2021-11-24T07:33:09.077779Z","iopub.status.idle":"2021-11-24T07:33:09.117401Z","shell.execute_reply.started":"2021-11-24T07:33:09.077627Z","shell.execute_reply":"2021-11-24T07:33:09.116746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path \nimport glob\n\nimport seaborn as sns \nimport matplotlib.pyplot as plt \n\nimport tensorflow as tf  \nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model  \nfrom tensorflow.keras.optimizers import RMSprop\nfrom keras_preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:33:09.119114Z","iopub.execute_input":"2021-11-24T07:33:09.11996Z","iopub.status.idle":"2021-11-24T07:33:17.164905Z","shell.execute_reply.started":"2021-11-24T07:33:09.119911Z","shell.execute_reply":"2021-11-24T07:33:17.163786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#how to read in the training set\nimport zipfile\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('./')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:33:17.16645Z","iopub.execute_input":"2021-11-24T07:33:17.166731Z","iopub.status.idle":"2021-11-24T07:33:32.050046Z","shell.execute_reply.started":"2021-11-24T07:33:17.166687Z","shell.execute_reply":"2021-11-24T07:33:32.049099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('./')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:33:32.052903Z","iopub.execute_input":"2021-11-24T07:33:32.053644Z","iopub.status.idle":"2021-11-24T07:33:38.159603Z","shell.execute_reply.started":"2021-11-24T07:33:32.053584Z","shell.execute_reply":"2021-11-24T07:33:38.158585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#i am going to do this with a CNN\ndata_dir = Path('./')\ntrain_dir = data_dir / \"train\" \ntest_dir = data_dir / \"test1\"\nvalidate_dir = data_dir / \"validate\"","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:33:38.16107Z","iopub.execute_input":"2021-11-24T07:33:38.161396Z","iopub.status.idle":"2021-11-24T07:33:38.168982Z","shell.execute_reply.started":"2021-11-24T07:33:38.161341Z","shell.execute_reply":"2021-11-24T07:33:38.168081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train/cats\n!mkdir train/dogs","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:33:38.170348Z","iopub.execute_input":"2021-11-24T07:33:38.171336Z","iopub.status.idle":"2021-11-24T07:33:39.720382Z","shell.execute_reply.started":"2021-11-24T07:33:38.17128Z","shell.execute_reply":"2021-11-24T07:33:39.718908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv train/cat*jpg train/cats\n!mv train/dog*jpg train/dogs","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:33:39.722843Z","iopub.execute_input":"2021-11-24T07:33:39.723286Z","iopub.status.idle":"2021-11-24T07:33:42.297091Z","shell.execute_reply.started":"2021-11-24T07:33:39.723223Z","shell.execute_reply":"2021-11-24T07:33:42.295796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir validate\n!mkdir validate/cats\n!mkdir validate/dogs\n\ni=0\nfor file in glob.glob('train/cats/*'):\n    if not np.mod(i,125):\n        os.rename(file, 'validate/cats'+file.split('cats')[1])\n    i += 1\n\ni=0\nfor file in glob.glob('train/dogs/*'):\n    if not np.mod(i,125):\n        os.rename(file, 'validate/dogs'+file.split('dogs')[1])\n    i += 1","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:10.333126Z","iopub.execute_input":"2021-11-24T07:48:10.334007Z","iopub.status.idle":"2021-11-24T07:48:12.87631Z","shell.execute_reply.started":"2021-11-24T07:48:10.333924Z","shell.execute_reply":"2021-11-24T07:48:12.874806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_csv_with_image_labels(TRAIN_PATH):  \n    '''\n    Read in the training data into a database with images and corresponding labels\n    '''\n\n    cat_images = TRAIN_PATH.glob('cats/*.jpg')\n\n    dog_images = TRAIN_PATH.glob('dogs/*.jpg')\n\n    df = [] \n\n    for i in cat_images: \n\n        df.append((i, 0)) # appending cat images as 0\n\n    for j in dog_images: \n\n        df.append((j, 1)) # appending dog images as 1\n\n    df = pd.DataFrame(df, columns=[\"image_path\", \"label\"], index = None) # converting into dataframe \n\n    df = df.sample(frac = 1).reset_index(drop=True) \n\n    return df  ","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:14.176347Z","iopub.execute_input":"2021-11-24T07:48:14.176779Z","iopub.status.idle":"2021-11-24T07:48:14.187998Z","shell.execute_reply.started":"2021-11-24T07:48:14.176739Z","shell.execute_reply":"2021-11-24T07:48:14.186978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = make_csv_with_image_labels(train_dir) \n\ntrain_csv.head(-100)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:14.552446Z","iopub.execute_input":"2021-11-24T07:48:14.553553Z","iopub.status.idle":"2021-11-24T07:48:15.226616Z","shell.execute_reply.started":"2021-11-24T07:48:14.553486Z","shell.execute_reply":"2021-11-24T07:48:15.225438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#visualise training sample\n\nlen_cat = len(train_csv[\"label\"][train_csv.label == 0])\nlen_dog = len(train_csv[\"label\"][train_csv.label == 1])\narr = np.array([len_cat , len_dog]) \nlabels = ['CAT', 'DOG'] \nprint(\"# of cat samples : \", len_cat)\nprint(\"# of dog samples : \", len_dog)\nplt.pie(arr, labels=labels, explode = [0.2,0.0] , shadow=True) \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:15.22899Z","iopub.execute_input":"2021-11-24T07:48:15.229399Z","iopub.status.idle":"2021-11-24T07:48:15.394918Z","shell.execute_reply.started":"2021-11-24T07:48:15.229352Z","shell.execute_reply":"2021-11-24T07:48:15.393845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_generator(train_dir, batch_size=64, target_size=(224, 224)):  \n    ''' \n    Function for preparing training data\n    '''\n    train_datagen = ImageDataGenerator(rescale = 1./255., # normalizing the image \n                                   rotation_range = 40,\n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.2,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True) \n    train_generator = train_datagen.flow_from_directory('train/',\n                                                    batch_size = batch_size, \n                                                    color_mode='rgb',\n                                                    class_mode = 'binary', \n                                                    target_size = target_size)   \n    return train_generator  \ntrain_generator = get_train_generator(train_dir)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:15.417116Z","iopub.execute_input":"2021-11-24T07:48:15.4174Z","iopub.status.idle":"2021-11-24T07:48:16.590509Z","shell.execute_reply.started":"2021-11-24T07:48:15.417371Z","shell.execute_reply":"2021-11-24T07:48:16.589453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_testgenerator(test_dir,batch_size=64, target_size=(224,224)): \n    ''' \n    Function for preparing testing data\n    '''\n    test_datagen = ImageDataGenerator( rescale = 1.0/255. )\n    test_generator  =  test_datagen.flow_from_directory(test_dir, \n                                                          batch_size  = batch_size, \n                                                          color_mode='rgb',\n                                                          class_mode  = 'binary', \n                                                          target_size = target_size) \n    return test_generator\ntest_generator = get_testgenerator(validate_dir)\n\n#this does not make sense because the test images are obviously not labelled by class\n#i think this would have worked for a validation dataset","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:16.592713Z","iopub.execute_input":"2021-11-24T07:48:16.593259Z","iopub.status.idle":"2021-11-24T07:48:16.704455Z","shell.execute_reply.started":"2021-11-24T07:48:16.59321Z","shell.execute_reply":"2021-11-24T07:48:16.703705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    layers.Conv2D(64, (3,3), strides=(2,2),padding='same',input_shape= (224,224,3),activation = 'relu'), \n    layers.MaxPool2D(2,2), \n    layers.Conv2D(128, (3,3), strides=(2,2),padding='same',activation = 'relu'),\n    layers.MaxPool2D(2,2), \n    layers.Conv2D(256, (3,3), strides=(2,2),padding='same',activation = 'relu'), \n    layers.MaxPool2D(2,2),  \n    layers.Flatten(), \n    layers.Dense(158, activation ='relu'), \n    layers.Dense(256, activation = 'relu'), \n    layers.Dense(128, activation = 'relu'), \n    layers.Dense(1, activation = 'sigmoid'), \n]) \nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:16.705823Z","iopub.execute_input":"2021-11-24T07:48:16.706181Z","iopub.status.idle":"2021-11-24T07:48:16.990373Z","shell.execute_reply.started":"2021-11-24T07:48:16.706133Z","shell.execute_reply":"2021-11-24T07:48:16.989183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=RMSprop(learning_rate=0.001), loss='binary_crossentropy', metrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:16.992964Z","iopub.execute_input":"2021-11-24T07:48:16.993364Z","iopub.status.idle":"2021-11-24T07:48:17.012829Z","shell.execute_reply.started":"2021-11-24T07:48:16.993314Z","shell.execute_reply":"2021-11-24T07:48:17.01175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(train_generator,\n                              epochs=15,\n                              verbose=1,\n                              validation_data=test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T07:48:17.103027Z","iopub.execute_input":"2021-11-24T07:48:17.103361Z","iopub.status.idle":"2021-11-24T10:13:46.183104Z","shell.execute_reply.started":"2021-11-24T07:48:17.103326Z","shell.execute_reply":"2021-11-24T10:13:46.180836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('my_model.h5') # saving the trained model \n\nnew_model = tf.keras.models.load_model('./my_model.h5') # loading the trained model\n","metadata":{"execution":{"iopub.status.busy":"2021-11-24T10:13:46.487423Z","iopub.execute_input":"2021-11-24T10:13:46.488351Z","iopub.status.idle":"2021-11-24T10:13:46.792846Z","shell.execute_reply.started":"2021-11-24T10:13:46.488218Z","shell.execute_reply":"2021-11-24T10:13:46.791531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ah fuck, the model didn't save\n\nimport matplotlib.image  as mpimg\nimport matplotlib.pyplot as plt\nacc=history.history['acc']\nval_acc=history.history['val_acc']\nloss=history.history['loss']\nval_loss=history.history['val_loss']\nepochs=range(len(acc))\nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.ylim(0,1)\nplt.yticks([.1,.2,.3,.4,.5,.6,.7,.8,.9],[.1,.2,.3,.4,.5,.6,.7,.8,.9])\nplt.figure()\nplt.plot(epochs, loss, 'r', \"Training Loss\")\nplt.plot(epochs, val_loss, 'b', \"Validation Loss\")\nplt.title('Training and validation loss')\nplt.ylim(0,1)\nplt.yticks([.1,.2,.3,.4,.5,.6,.7,.8,.9],[.1,.2,.3,.4,.5,.6,.7,.8,.9])","metadata":{"execution":{"iopub.status.busy":"2021-11-24T10:18:45.521508Z","iopub.execute_input":"2021-11-24T10:18:45.522816Z","iopub.status.idle":"2021-11-24T10:18:46.016337Z","shell.execute_reply.started":"2021-11-24T10:18:45.522762Z","shell.execute_reply":"2021-11-24T10:18:46.015132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}