{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport zipfile\nimport cv2\nimport tensorflow as tf\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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip','r') as zippitydooda:\n    zippitydooda.extractall('/kaggle/temp/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def bignorm(nf):\n    return nf/max(pixelval for row in nf for pixel in row for pixelval in pixel)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def normed_data(path):\n    numform=plt.imread(path)\n    resized=cv2.resize(numform,(100,100),interpolation=cv2.INTER_AREA)\n    normed=bignorm(resized)\n    return normed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainpath='/kaggle/temp/train/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nfiles=[f for f in os.listdir('/kaggle/temp/train')]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def makearr(l):\n    if type(l) in [np.ndarray,list] and not type(l) in [int,float]:\n        return np.array([makearr(x) for x in list(l)])\n    else:\n        return l","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def flip(img):\n    return img[::-1]\ndef mirr(img):\n    return np.array([x[::-1] for x in img])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def pad(x,n):\n    Lr=len(x[0])\n    Lc=len(x)\n    emppix=[0,0,0]\n    toprow = [[emppix for i in range(Lr+2*n)]]\n    out = []\n    for i in range(Lc+2*n):\n        if (i < n) or (i > Lc + n - 1):\n            out+=toprow\n        else:\n            out+=[n*[emppix]+list(x[i-n])+n*[emppix]]\n    return makearr(out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def split_data(dat,pad_n=0):\n    y,x = makearr([int('dog' in x[0]) for x in dat]),makearr([np.array(x[1]) for x in dat])\n    return y,[pad(img,pad_n) for img in x]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def prep_and_train(m,n,pad_n):\n    trn=[(f,normed_data(trainpath+f)) for f in files[:m]]\n    tst=[(f,normed_data(trainpath+f)) for f in files[m:m+n]]\n    y_train,x_train=split_data(trn,pad_n)\n    y_test,x_test=split_data(tst,pad_n)\n    loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)\n    \n    pad_shape = (100+2*pad_n,100+2*pad_n,3)\n    \n    for i in range(4):\n        if i<4:\n            y,x=y_train,x_train\n            y_tst,x_tst=y_test,x_test\n        else:\n            y,x=y_test,x_test\n            y_tst,x_tst=y_train,x_train\n        exec('model'+str(i) + '= tf.keras.models.Sequential(['+\n        #'tf.keras.layers.Flatten(input_shape=(100, 100, 3)),'+\n        'tf.keras.layers.Conv2D(32,kernel_size=3,activation=\"relu\",input_shape='+str(pad_shape)+'),'+\n        'tf.keras.layers.MaxPooling2D((2,2)),'+\n        #'tf.keras.layers.Dropout(0.2),'+\n        #'tf.keras.layers.Conv2D(64,kernel_size=3,activation=\"relu\"),'+\n        #'tf.keras.layers.MaxPooling2D((2,2)),'+\n        #'tf.keras.layers.Dropout(0.2),'+\n        #'tf.keras.layers.Conv2D(128,kernel_size=3,activation=\"relu\"),'+\n        #'tf.keras.layers.MaxPooling2D((2,2)),'+\n        #'tf.keras.layers.Dropout(0.2),'+\n        'tf.keras.layers.Flatten(),'+\n        #\"tf.keras.layers.Dense(128, activation='relu'),\"+\n        #\"tf.keras.layers.Dense(64, activation='relu'),\"+\n        \"tf.keras.layers.Dense(32, activation='relu'),\"+\n        #'tf.keras.layers.Dropout(0.5),'+\n        \"tf.keras.layers.Dense(1,activation='sigmoid')\"+\n        \"])\")\n        opt = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)\n        eval('model'+str(i)).compile(opt,#optimizer='adam',\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n    \n        #if i%4==1:\n            #x=list(x) + [flip(img) for img in x]\n            #x_tst=list(x_tst) + [flip(img) for img in x_tst]\n        #elif i%4==2:\n        if i%2==1:\n            x=list(x) + [mirr(img) for img in x]\n            x_tst=list(x_tst) + [mirr(img) for img in x_tst]\n        #elif i%4==3:\n        #    x=list(x) + [mirr(img) for img in x]+[flip(img) for img in x]\n        #    x_tst=list(x_tst) + [mirr(img) for img in x_tst]+[flip(img) for img in x_tst]\n        #if i%4 in [1,2]:\n        if i%2 == 1:\n            y=makearr(list(y)*2)\n            y_tst=makearr(list(y_tst)*2)\n        #elif i%4 == 3:\n        #    y=makearr(list(y)*3)\n        #    y_tst=makearr(list(y_tst)*3)\n        x=makearr(x)\n        x_tst=makearr(x_tst)\n        \n        eval('model'+str(i)).fit(x,y,epochs=4)\n        eval('model'+str(i)).evaluate(x_tst,y_tst)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prep_and_train(1000,1000,3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def fixshape(img_arr):\n    return np.reshape(img_arr,(-1,100,100,3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def prediction(img_arr):\n    animal_dict={0:'cat',1:'dog'}\n    return animal_dict[model.predict(fixshape(img_arr)).argmax()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test,x_test=split_data(tst100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction(x_test[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_test[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.predict(fixshape(x_test[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.predict(fixshape(x_test[1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction(x_test[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_test[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x_train,y_train,epochs=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x_train,y_train,epochs=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate(x_test,y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(flip(x_train[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(mirr(x_train[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#create pipeline for adding flip and mirror data to x_train and y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"[1]*3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def split_data(dat):\n    y,x = makearr([int('dog' in x[0]) for x in dat]),[makearr(x[1]) for x in dat]\n    x+=[flip(img) for img in x]+[mirr(img) for img in x]\n    y=np.array(list(y)*3)\n    return y,np.array(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train,x_train=split_data(sm100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_train[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips = tf.keras.models.Sequential([\n  tf.keras.layers.Flatten(input_shape=(100, 100, 3)),\n  tf.keras.layers.Dense(128, activation='relu'),\n  tf.keras.layers.Dropout(0.2),\n  tf.keras.layers.Dense(2)\n])\nmodelflips.compile(optimizer='adam',\n              loss=loss_fn1,\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips.fit(x_train,y_train,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips.fit(x_train,y_train,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips.fit(x_train,y_train,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips.fit(x_train,y_train,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips.fit(x_train,y_train,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips.fit(x_train,y_train,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test,x_test=split_data(tst100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"modelflips.evaluate(x_test,y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}