{"cells":[{"metadata":{"_uuid":"62d2d513d8fbf983a7020be95a5bf319f2566dcc"},"cell_type":"markdown","source":"# Import libraries and show an image"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom time import time,sleep\nfrom termcolor import cprint, colored\nfrom random import randint, choice\nimport cv2\nfrom tqdm import tqdm\nimport os\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.models import load_model\n\nos.listdir(\"../input/dogs-vs-cats-redux-kernels-edition\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aae55e17913d84e3e06d9a3f4f43330580d552d6"},"cell_type":"code","source":"# class I use later on to time things that take a long time\nclass stopwatch():\n    def __init__(self, msg=None):\n        self.t0 = time()\n        if msg == None:\n            self.msg = \"\"\n        else:\n            self.msg = msg\n    \n    def stop(self):\n        self.elapsed = round(time() - self.t0,1)\n    \n        # the math is from stack overflow\n        hours = self.elapsed // 3600 % 24\n        minutes = self.elapsed // 60 % 60\n        seconds = self.elapsed % 60\n    \n        hours = round(hours)\n        minutes = round(minutes)\n        seconds = round(seconds,1)\n\n        if self.msg:\n            m = \"Elapsed Time For \"+self.msg+\": \"\n        else:\n            m = \"Elapsed Time: \"\n        if hours and minutes and seconds:\n            msg = f\"{m}{hours} hours, {minutes} minutes, and {seconds} seconds\"\n        elif minutes and seconds:\n            if minutes == 1.0:\n                msg = f\"{m}1 minute and {seconds} seconds\"\n            else: msg = f\"{m}{minutes} minutes and {seconds} seconds\"\n        elif seconds:\n            msg = f\"{m}{seconds} seconds\"\n        elif seconds == 0.0:\n            msg = f\"{m}{round(time()-self.t0,6)} seconds\" # give time rounded to 6 places (not 1) if it is 0 when rounded. Ex: 0.000143 seconds\n        else: \n            msg = \"something went wrong with stopwatch class\\n\"+str(self.elapsed)+\" seconds\"\n        cprint(msg,\"red\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"IMSIZE = 50\npath = \"../input/dogs-vs-cats-redux-kernels-edition/train/\"\ndef load_image(path):\n    try:\n        img = cv2.resize(cv2.imread(path, cv2.IMREAD_COLOR), # read as grayscale\n                            (IMSIZE, IMSIZE)) # resize it to IMG_SIZE\n    except:\n        print(\"error loading an image\")\n        return \n    return img\n    \nx = load_image(path+\"dog.\"+str(randint(0,2000-1))+\".jpg\")\nplt.imshow(x)\nplt.title(\"a doggo\")\nplt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"95bd8911b6021f3d66e8fe94f6e2aa7affdf94e9"},"cell_type":"markdown","source":"# Gather 1000s of images to train model on"},{"metadata":{"trusted":true,"_uuid":"ed600336d0ab28b82d91213e8c8534ada0d1080e"},"cell_type":"code","source":"def create_train_data(stop=100):\n    error = False\n    print(f\"Loading {stop} images\")\n    if stop == \"all\": stop = -1\n    images = []\n    for i in tqdm(os.listdir(path)[:stop]):\n        error = False\n        im = load_image(path+i)\n        try:\n            if im == None:\n                error = True\n                print(\"error loading an image\")\n        except:\n                images.append(im)\n        \n    labels = []\n    for i in os.listdir(path)[:stop]:\n        if i[:3] == \"cat\": \n            if not error: \n                labels.append(np.array(\n                    [1,0] # very cat, no doggo\n                ))\n        elif i[:3] == \"dog\": \n            if not error: \n                labels.append(np.array(\n                    [0,1] # no cat, very doggo\n                ))\n        else: print(\"a problem occured in labelling\")\n    return images, labels\n\ndef show_distribution(labels):\n    c = 0\n    d = 0\n    for i in labels:\n        if list(i) == [1,0]:\n            c += 1\n        elif list(i) == [0,1]:\n            d += 1\n    print(f\"cats: {c}, dogs: {d}\")\n\nimages, labels = create_train_data(10000)\nimages = np.array(images) # because why not\nlabels = np.array(labels) # and also keras expects np arrays\nprint([len(i) for i in [images, labels]])\nshow_distribution(labels)\nplt.imshow(images[2],cmap='gray')\nplt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f07d8b3ecc4b4abd9e2a4c2ed87469fc90a85d25"},"cell_type":"code","source":"index = int(len(labels) * 0.1)\n\nx_train = np.array(images[:-index]).reshape(-1,IMSIZE,IMSIZE,3) / 255.0 # normalize\ny_train = np.array(labels[:-index])\n\nx_val = np.array(images[-index:]).reshape(-1,IMSIZE,IMSIZE,3) / 255.0 # normalize\ny_val = np.array(labels[-index:])\n\nprint([len(i) for i in [x_train, y_train, x_val, y_val]])\nprint(\"Shape of input data: \",x_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13b844579ca675f92ad4c3d5ce145a42e1d3f2ab","scrolled":false},"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D, Dense, Flatten, Dropout, MaxPooling2D\n\nmodel = Sequential([\n    Conv2D(32, (5,5), strides=(1,1) ,input_shape=x_train[0].shape, name='conv1'),\n    MaxPooling2D(pool_size=(2,2), name='pool1' ),\n    Conv2D(64, (5,5), name='conv2', activation='relu'),\n    MaxPooling2D((2,2), name='pool2'),\n    Conv2D(4, (2,2), name='conv3'),\n    Flatten(name='flatten'),\n    Dense(256, activation='relu', name='dense1'),\n    Dropout(0.2),\n    Dense(100, activation='relu', name='dense2'),\n    Dense(2, activation='softmax', name='output')\n])\n\nmodel.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7a4e596573e3fdec62caf1850beed36df86a7ac","scrolled":false},"cell_type":"code","source":"m_path = \"../input/a-cnn-model-70/\"\nstate = \"train it\"\nif os.path.exists(m_path+'my_model.h5') and state==\"normal\":\n    print(os.listdir(m_path))\n    print(\"loading model from file...\")\n    print()\n    model = load_model(m_path+\"my_model.h5\")\n    model.summary()\nelse: \n    print(\"training model\")\n    t = stopwatch(\"Training\")\n    model.fit(x_train, y_train, epochs=5, validation_data=(x_val,y_val))\n    print()\n    t.stop()\n    model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0d1860f1b2ce75a4b9b1e09fddd932a53d28dfa","scrolled":false},"cell_type":"code","source":"from termcolor import colored\ntry:\n    print(colored(\"Accuracy: \",\"cyan\"),round(model.history.history['acc'][-1]*100,2),\"%\")\n    print(colored(\"Validation Accuracy: \",\"cyan\"),round(model.history.history['val_acc'][-1]*100,2),\"%\")\n\n    plt.plot(model.history.history[\"val_acc\"],label=\"val\")\n    plt.plot(model.history.history[\"acc\"],label=\"train\")\n    plt.title(\"accuracy over time\")\n    plt.legend()\n    plt.show()\n\n    plt.plot(model.history.history[\"val_loss\"],label=\"val\")\n    plt.plot(model.history.history[\"loss\"],label=\"train\")\n    plt.title(\"loss over time\")\n    plt.legend()\n    plt.show()\nexcept AttributeError:\n#     print(\"Loaded pretrained model, so can't show graph of progress :(\")\n    print(\"evaluating accuracy...\")\n    val_acc = round(model.evaluate(x_val, y_val)[0]*100,2)\n#     print(val_acc)\n    print(colored(\"Validation Accuracy: \",\"cyan\"),f\"{val_acc}%\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"906d52c3fbfe51e4a67de64d01bcb3ea45c09e44"},"cell_type":"code","source":"def makeplot():\n    fig, a = plt.subplots(2,2)\n    listofimages = []\n    for r in range(2):\n        for c in range(2):\n            i = randint(0,len(x_val)-1)\n            listofimages.append(i)\n            if i in listofimages: i = randint(0,len(x_val)-1)\n            a[r, c].imshow(x_val[i])\n            a[r, c].axis('off')\n            catness, dogness = predictions[i]\n            if dogness > catness:\n                percent = str(round(dogness*100,2))+\"% dog\"\n            elif catness > dogness:\n                percent = str(round(catness*100,2))+\"% cat\"\n            a[r,c].set_title(percent)\n            # show if it got it right or not\n            if list( y_val[i] ) == [round(catness), round(dogness)]: \n                a[r,c].text(3, 8, 'correct', style='italic',\n                bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 2})\n            else: \n                a[r,c].text(3, 8, 'incorrect', style='italic',\n                bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 2})\n                a[r,c].set_xlabel('incorrect')\n\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f06bdaace2b29fdc06fdf25d6c57996e17b2c300","scrolled":false},"cell_type":"code","source":"predictions = model.predict(x_val)\nfirst = list(predictions[0])\nsame = True\nprint(\"first few predictions\\n\",predictions[:4])\nfor i in predictions:\n    if list(i) != first:\n        same = False\nif not same:\n    for i in range(5):\n        makeplot()\n\nelse:\n    print(\"all predictions are the same :(\")\n    print(\"They are all \",predictions[0])","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}