{"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)\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport os\nimport random\nfrom keras.utils import to_categorical\nimport keras","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Importing all the images\nx=[] ## variable to save all the images\nz=[] ## varaible to save all breeds\nfrom tqdm import tqdm\nimg_size=120","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img,breed in tqdm(labels.values):\n  images=cv2.imread(\"/kaggle/input/dog-breed-identification/train/{}.jpg\".format(img),cv2.IMREAD_COLOR)\n  images=cv2.resize(images,(img_size,img_size))\n  x.append(images)\n  z.append(breed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"z","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(z)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencdr=LabelEncoder()\ny=encdr.fit_transform(z)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Chnaging the type of x\nx=np.array(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Normalizing the x\nx=x/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#### spiltting the data into test and train\nfrom sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0,stratify=y,shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### viewing the image\nindex=7\ny_train[index]\nplt.imshow(x_train[index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index=8\ny_train[index]\nplt.imshow(x_train[index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index=9\ny_train[index]\nplt.imshow(x_train[index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index=15\ny_train[index]\nplt.imshow(x_train[index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index=20\ny_train[index]\nplt.imshow(x_train[index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index=25\ny_train[index]\nplt.imshow(x_train[index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nfrom keras.models import Sequential\nfrom keras.layers import Dense,MaxPooling2D,Dropout,Flatten,Conv2D\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#### Model building\nmodel=Sequential()\n### Conv layer-1\nmodel.add(Conv2D(32,(3,3),activation=\"relu\",input_shape=(120,120,3)))\n### Polling layer-1\nmodel.add(MaxPooling2D((2,2)))\n### Conv layer-2\nmodel.add(Conv2D(32,(3,3),activation=\"relu\"))\n### Pooling layer-2\nmodel.add(MaxPooling2D((2,2)))\n### Flatten layer\nmodel.add(Flatten())\n### Hidden layer\nmodel.add(Dense(250,activation=\"relu\"))\n### Output layer\nmodel.add(Dense(120,activation=\"softmax\"))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n## Compiling and fitting the model\nmodel.compile(optimizer=\"Adam\",\n             loss=\"sparse_categorical_crossentropy\",\n             metrics=[\"accuracy\"])\nmodel.fit(x_train,y_train,epochs=5,batch_size=256,validation_split=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Checking the accuracy by increasing number of layers\n\n#### Model building\nmodel_2=Sequential()\n### Conv layer-1\nmodel_2.add(Conv2D(32,(3,3),activation=\"relu\",input_shape=(120,120,3)))\n### Polling layer-1\nmodel_2.add(MaxPooling2D((2,2)))\n### Conv layer-2\nmodel_2.add(Conv2D(32,(3,3),activation=\"relu\"))\n### Pooling layer-2\nmodel_2.add(MaxPooling2D((2,2)))\n### Flatten layer\nmodel_2.add(Flatten())\n### Hidden layer\nmodel_2.add(Dense(500,activation=\"relu\"))\n### Dropout -1\nmodel_2.add(Dropout(0.5))\n### Hidden layer -2\nmodel_2.add(Dense(300,activation=\"relu\"))\n## Dropout -2\nmodel_2.add(Dropout(0.5))\n### hidden layer-3\nmodel_2.add(Dense(250,activation=\"relu\"))\n### Output layer\nmodel_2.add(Dense(120,activation=\"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from kerastuner import RandomSearch\nfrom kerastuner.engine.hyperparameters import HyperParameters","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_tun","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n## Compiling and fitting the model\nmodel_2.compile(optimizer=\"Adam\",\n             loss=\"sparse_categorical_crossentropy\",\n             metrics=[\"accuracy\"])\nmodel_2.fit(x_train,y_train,epochs=5,batch_size=256,validation_split=0.2)","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":"model_2.evaluate(x_test,y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### if we increase the number of layers i am getting less accuarcy","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Tuning the dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def build_model(hp):\n  model=keras.Sequential([\n                          keras.layers.Conv2D(\n                              filters=hp.Int(\"conv_1_filter\",min_value=32,max_value=128,step=16),\n                              kernel_size=hp.Choice(\"conv_1_kernel\",values=[5,3]),\n                              activation=\"relu\",\n                              input_shape=(28,28,1)\n                          ),\n                          keras.layers.Conv2D(\n                              filters=hp.Int(\"conv_2_filter\",min_value=32,max_value=64,step=16),\n                              kernel_size=hp.Choice(\"conv_2_kernel\",values=[5,3]),\n                              activation=\"relu\"\n                          ),\n                          keras.layers.Flatten(),\n                          keras.layers.Dense(\n                              units=hp.Int(\"dense_1_filter\",min_value=32,max_value=128,step=16),\n                              activation=\"relu\"\n                          ),\n                          keras.layers.Dense(10,activation=\"softmax\")\n  ])\n  model.compile(optimizer=keras.optimizers.Adam(hp.Choice(\"learning_rate\",values=[1e-2,1e-3])),\n                loss=\"sparse_categorical_crossentropy\",\n                metrics=[\"accuracy\"])\n  return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tuner_search.search(x_train,y_train,epochs=3,validation_split=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tuner_search=RandomSearch(build_model,\n                          objective=\"val_accuracy\",\n                          max_trials=5,\n                          directory=\"output\",\n                          project_name=\"dog nad breed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_tun=tuner_search.get_best_models(num_models=1)[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_tun.fit(x_train,y_train,epochs=10,initial_epoch=3,validation_split=0.1)","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}