{"nbformat_minor": 1, "nbformat": 4, "cells": [{"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", "\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "from skimage import io\n", "from skimage import filters\n", "from skimage import transform\n", "import matplotlib.pyplot as plt\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", "\n", "from subprocess import check_output\n", "#print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "ic=io.imread_collection(\"../input/train/*jpg\")\n", "resized=[]\n", "resized3=[]\n", "count=0\n", "for x in ic:\n", "    blur2=filters.gaussian(x)\n", "    resize_2=transform.resize(blur2,(100,100))\n", "    resize_3=transform.resize(x,(100,100))\n", "    resized.append(resize_2)\n", "    resized3.append(resize_3)\n", "    count=count+1;\n", "    if count>3:\n", "        break\n", "print(\"resized after a gaussian\")\n", "io.imshow(resized[0])\n", "plt.show()\n", "plt.plot(resized[0][50])\n", "plt.show()\n", "\n", "io.imshow(resized[1])\n", "plt.show()\n", "plt.plot(resized[1][50])\n", "plt.show()\n", "\n", "print(\"resized without a gaussian\")\n", "io.imshow(resized3[0])\n", "plt.show()\n", "plt.plot(resized3[0][50])\n", "plt.show()\n", "\n", "io.imshow(resized3[1])\n", "plt.show()\n", "plt.plot(resized3[1][50])\n", "plt.show()\n", "\n", "# Any results you write to the current directory are saved as output."], "metadata": {"_cell_guid": "277e6df1-1c82-46e0-b263-e10470e238e9", "collapsed": true, "_uuid": "2d9bcef8a6d7d17debd0ca1462e8343f7e65e620"}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["kaggle is using the older version of scipy, so anti-aliasing cant be done while resizing ( any other options?) the resize function uses a box filter; tried using a gausian filter before resizing ,the infromation in both the images looks the same, but the ones resized after a gaussian have less noise. so i used the ones after the gaussian filter\n", "\n", "splitting the images according to its RGB components and preparing them for a classifier, as in the digits dataset  "], "cell_type": "markdown", "metadata": {"_cell_guid": "eb7c61e7-1527-4e88-8961-e66458600e25", "_uuid": "62c9fac0197727501366d3375e0da91018d0f436"}}, {"source": ["import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "from skimage import io\n", "from skimage import filters\n", "from skimage import transform\n", "import matplotlib.pyplot as plt\n", "from sklearn.model_selection import train_test_split\n", "from sklearn import svm\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", "\n", "from subprocess import check_output\n", "\n", "def read_and_return():\n", " \n", " ic=io.imread_collection(\"../input/train/*jpg\")\n", " #apply gaussian blur, resize and split into rgb componants\n", " resized_Red=[]\n", " resized_Blue=[]\n", " resized_Green=[]\n", " count=0\n", " for x in ic:\n", "    blur2=filters.gaussian(x)\n", "    resize_2=transform.resize(blur2,(100,100))\n", "    resize_Red=resize_2[:,:,0]\n", "    resize_Green=resize_2[:,:,1]\n", "    resize_Blue=resize_2[:,:,2]\n", "    resized_Red.append(resize_Red)\n", "    resized_Green.append(resize_Green)\n", "    resized_Blue.append(resize_Blue)\n", " #display   \n", " print(\"resized after a gaussian\")\n", " io.imshow(resized_Red[0])\n", " plt.show()\n", " plt.plot(resized_Red[0][50])\n", " plt.show()\n", "\n", " io.imshow(resized_Red[1])\n", " plt.show()\n", " plt.plot(resized_Red[1][50])\n", " plt.show()\n", "    \n", " ##get file names\n", " file_list=ic.files\n", " lables=pd.read_csv(\"../input/labels.csv\")\n", " fl_count=0;\n", " fl_names=[]\n", " for x in file_list:\n", "    #print(x.replace(\"/\",\" \"))\n", "    temp=x.replace(\"/\",\" \")\n", "    temp1=temp.replace(\".\",\" \")\n", "    temp2=temp1.split()\n", "    fl_names.append(temp2[2])\n", "    \n", " for id_,name in enumerate(lables.id):\n", "    for index,item in enumerate(fl_names):\n", "        if item==name:\n", "            fl_names[index]=lables.breed[id_]\n", "            \n", " ##prepare the data for a classifier\n", " resized_Red_=np.array(resized_Red)\n", " resized_Blue_=np.array(resized_Blue)\n", " resized_Green_=np.array(resized_Green)\n", " new_resized_Red=resized_Red_.reshape((len(resized_Red_),-1))\n", " new_resized_Blue=resized_Blue_.reshape((len(resized_Blue_),-1))\n", " new_resized_Green=resized_Green_.reshape((len(resized_Green_),-1))\n", " return new_resized_Red,new_resized_Blue,new_resized_Green,fl_names\n", "(new_resized_Red,new_resized_Blue,new_resized_Green,fl_names)=read_and_return()\n", "\n", "print(\"train_done\")\n", "\n", "Red_train, Red_test,names_train, names_test = train_test_split(new_resized_Red,fl_names,test_size=0.30, random_state=42)\n", "\n", "clif_red=svm.SVC(decision_function_shape='ovo')\n", "clif_red.fit(Red_train,names_train)\n", "#pred=clif_red.predict(Red_test)\n", "score=clif_red.score(Red_test,names_test)\n", "print(\"Score\",score)\n", "\n", "\n", "\n", "\n", "\n", "#get test data\n", "def Test_data():\n", "  ic=io.imread_collection(\"../input/test/*jpg\")\n", "  resized_Red=[]\n", "  resized_Blue=[]\n", "  resized_Green=[]\n", "  count=0\n", "  for x in ic:\n", "    blur2=filters.gaussian(x)\n", "    resize_2=transform.resize(blur2,(100,100))\n", "    resize_Red=resize_2[:,:,0]\n", "    resize_Green=resize_2[:,:,1]\n", "    resize_Blue=resize_2[:,:,2]\n", "    resized_Red.append(resize_Red)\n", "    resized_Green.append(resize_Green)\n", "    resized_Blue.append(resize_Blue)\n", "  print(\"test data resized after a gaussian\")\n", "  io.imshow(resized_Red[0])\n", "  plt.show()\n", "  plt.plot(resized_Red[0][50])\n", "  plt.show()\n", "\n", "  io.imshow(resized_Red[1])\n", "  plt.show()\n", "  plt.plot(resized_Red[1][50])\n", "  plt.show()\n", "  ##prepare the data for a classifier\n", "  resized_Red_=np.array(resized_Red)\n", "  resized_Blue_=np.array(resized_Blue)\n", "  resized_Green_=np.array(resized_Green)\n", "  new_resized_Red=resized_Red_.reshape((len(resized_Red_),-1))\n", "  new_resized_Blue=resized_Blue_.reshape((len(resized_Blue_),-1))\n", "  new_resized_Green=resized_Green_.reshape((len(resized_Green_),-1))\n", "  return new_resized_Red,new_resized_Blue,new_resized_Green\n", "#(test_resized_Red,test_resized_Blue,test_resized_Green)=Test_data()\n", "#print(\"test_done\")\n", "    \n", "##preparing the classifier"], "metadata": {"_cell_guid": "9500d283-13f2-4444-a79f-4e380feffb21", "collapsed": true, "scrolled": true, "_uuid": "3e080a7316733b664e9d30547974f693c62500fc"}, "cell_type": "code", "outputs": [], "execution_count": null}, {"source": ["the predicted score on the Red part is bad, the plan was to use each spectrum to predict and then using voting to make the final prediction\n", "\n", "Running the script on all the colours together below and resizing the image to a smaller size(50,50) to help with processing, but the score didnt change much at all"], "cell_type": "markdown", "metadata": {"_cell_guid": "c48c5cea-62e1-4dcb-b945-1f5a2b9ba929", "_uuid": "cc7748684506b2fe15e6623655a536a520aad0a2"}}, {"source": ["looking at the differences in spectrum in the graphs. it appears that the spectrums seems to merge on the dog so could we use this difference to isolate dog from the background? the code below tries to do just that. but the images are too different so it doesnt work very well"], "cell_type": "markdown", "metadata": {"_cell_guid": "10df9237-315d-4885-b963-b376e7ce2e3a", "collapsed": true, "_uuid": "78d43b45c47d57a5e7abecd8ec097db6d303ec0c"}}, {"source": ["import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "from skimage import io\n", "from skimage import filters\n", "from skimage import transform\n", "import matplotlib.pyplot as plt\n", "from sklearn.model_selection import train_test_split\n", "from sklearn import svm\n", "from PIL import Image\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", "\n", "from subprocess import check_output\n", "\n", "def read_and_return():\n", " \n", " ic=io.imread_collection(\"../input/train/*jpg\")\n", " #apply gaussian blur, resize and split into rgb componants\n", " resized_Red=[]\n", " resized_Blue=[]\n", " resized_Green=[]\n", " count=0\n", " for x in ic:\n", "    blur2=filters.gaussian(x,preserve_range=True)\n", "    resize_2=transform.resize(blur2,(100,100))\n", "    resize_Red=resize_2[:,:,0]\n", "    resize_Green=resize_2[:,:,1]\n", "    resize_Blue=resize_2[:,:,2]\n", "    resized_Red.append(resize_Red)\n", "    resized_Green.append(resize_Green)\n", "    resized_Blue.append(resize_Blue)\n", "    count=count+1\n", "    if count==7:\n", "        break\n", " \n", " ##isolate dog from the background\n", " for index_, item_ in enumerate(resized_Red):\n", "  resized_Red_=np.array(resized_Red[index_])\n", "  resized_Blue_=np.array(resized_Blue[index_])\n", "  resized_Green_=np.array(resized_Green[index_])\n", "  new_resized_Red=resized_Red_.ravel()\n", "  new_resized_Blue=resized_Blue_.ravel()\n", "  new_resized_Green=resized_Green_.ravel()\n", "  for index,item in enumerate(new_resized_Red):\n", "   x_y=new_resized_Red[index]-new_resized_Blue[index]\n", "   y_z=new_resized_Blue[index]-new_resized_Green[index]\n", "  #print(x_y,\"x_y\")\n", "   if x_y<35 and y_z<35:\n", "    continue\n", "   else:\n", "    new_resized_Red[index]=0\n", "    new_resized_Blue[index]=0\n", "    new_resized_Green[index]=0\n", "  rs_Red=new_resized_Red.reshape((100,100))\n", "  rs_Blue=new_resized_Blue.reshape((100,100))\n", "  rs_Green=new_resized_Green.reshape((100,100))\n", " #print(rs_Red)\n", "  img_red=Image.fromarray(rs_Red)\n", " #print(img_red)\n", "  plt.imshow(img_red)\n", "  plt.show()\n", "  img_blue=Image.fromarray(rs_Blue)\n", "  img_green=Image.fromarray(rs_Green)\n", "  \n", "a=read_and_return()"], "metadata": {"_cell_guid": "8b75b0a9-692e-406b-934a-7d9e8f9ed97e", "_uuid": "ba0be9b94f6e0ae685f062e4dc54d3b655d1f47a"}, "cell_type": "code", "outputs": [], "execution_count": 21}, {"source": [], "cell_type": "markdown", "metadata": {}}, {"source": [], "cell_type": "markdown", "metadata": {}}], "metadata": {"kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "language_info": {"pygments_lexer": "ipython3", "mimetype": "text/x-python", "version": "3.6.3", "file_extension": ".py", "nbconvert_exporter": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "name": "python"}}}