{"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":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport os\nimport cv2\nimport PIL\nfrom PIL import Image ","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:02:09.435134Z","iopub.execute_input":"2022-03-01T09:02:09.435685Z","iopub.status.idle":"2022-03-01T09:02:10.878529Z","shell.execute_reply.started":"2022-03-01T09:02:09.435577Z","shell.execute_reply":"2022-03-01T09:02:10.877582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/dog-vs-cat-classification/sample_submission.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:02:10.880183Z","iopub.execute_input":"2022-03-01T09:02:10.880424Z","iopub.status.idle":"2022-03-01T09:02:10.920466Z","shell.execute_reply.started":"2022-03-01T09:02:10.880395Z","shell.execute_reply":"2022-03-01T09:02:10.919573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_dir='../input/dog-vs-cat-classification/'\ntrain_cat=os.listdir(train_img_dir + 'train/' + 'train/' + 'cats/')\ntrain_dogs=os.listdir(train_img_dir + 'train/' + 'train/' + 'dogs/')","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:02:10.921952Z","iopub.execute_input":"2022-03-01T09:02:10.922770Z","iopub.status.idle":"2022-03-01T09:02:11.431425Z","shell.execute_reply.started":"2022-03-01T09:02:10.922725Z","shell.execute_reply":"2022-03-01T09:02:11.430765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset=[]\nlabels=[]","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:02:11.433402Z","iopub.execute_input":"2022-03-01T09:02:11.434041Z","iopub.status.idle":"2022-03-01T09:02:11.437618Z","shell.execute_reply.started":"2022-03-01T09:02:11.434001Z","shell.execute_reply":"2022-03-01T09:02:11.436894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_name in train_cat:\n    image=cv2.imread(train_img_dir + 'train/' + 'train/' + 'cats/' + img_name)\n    image=Image.fromarray(image,'RGB')\n    image=image.resize((128,128))\n    dataset.append(np.array(image))\n    labels.append(0)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:02:11.440991Z","iopub.execute_input":"2022-03-01T09:02:11.441359Z","iopub.status.idle":"2022-03-01T09:04:14.286721Z","shell.execute_reply.started":"2022-03-01T09:02:11.441318Z","shell.execute_reply":"2022-03-01T09:04:14.285849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataset","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:04:14.287942Z","iopub.execute_input":"2022-03-01T09:04:14.288182Z","iopub.status.idle":"2022-03-01T09:04:14.293967Z","shell.execute_reply.started":"2022-03-01T09:04:14.288155Z","shell.execute_reply":"2022-03-01T09:04:14.293035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_name in train_dogs:\n    image=cv2.imread(train_img_dir + 'train/' + 'train/' + 'dogs/' + img_name)\n    image=Image.fromarray(image,'RGB')\n    image=image.resize((128,128))\n    dataset.append(np.array(image))\n    labels.append(1)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:04:14.295484Z","iopub.execute_input":"2022-03-01T09:04:14.296108Z","iopub.status.idle":"2022-03-01T09:06:15.589348Z","shell.execute_reply.started":"2022-03-01T09:04:14.296069Z","shell.execute_reply":"2022-03-01T09:06:15.588198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[1]","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:06:15.590675Z","iopub.execute_input":"2022-03-01T09:06:15.590907Z","iopub.status.idle":"2022-03-01T09:06:15.600910Z","shell.execute_reply.started":"2022-03-01T09:06:15.590880Z","shell.execute_reply":"2022-03-01T09:06:15.599938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=np.asarray(dataset)\nlab=np.asarray(labels)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:06:15.602436Z","iopub.execute_input":"2022-03-01T09:06:15.602751Z","iopub.status.idle":"2022-03-01T09:06:16.039972Z","shell.execute_reply.started":"2022-03-01T09:06:15.602718Z","shell.execute_reply":"2022-03-01T09:06:16.038932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.shape,lab.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:06:16.043681Z","iopub.execute_input":"2022-03-01T09:06:16.044532Z","iopub.status.idle":"2022-03-01T09:06:16.050421Z","shell.execute_reply.started":"2022-03-01T09:06:16.044473Z","shell.execute_reply":"2022-03-01T09:06:16.049345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data[2])","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:06:16.052227Z","iopub.execute_input":"2022-03-01T09:06:16.052580Z","iopub.status.idle":"2022-03-01T09:06:16.359528Z","shell.execute_reply.started":"2022-03-01T09:06:16.052536Z","shell.execute_reply":"2022-03-01T09:06:16.358606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model training","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras \nfrom keras import Sequential,models,layers ","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:06:16.361157Z","iopub.execute_input":"2022-03-01T09:06:16.361839Z","iopub.status.idle":"2022-03-01T09:06:22.122490Z","shell.execute_reply.started":"2022-03-01T09:06:16.361801Z","shell.execute_reply":"2022-03-01T09:06:22.121560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential([\n                    layers.Conv2D(50,(3,3),activation=\"relu\",input_shape=(128,128,3)),\n                    layers.MaxPooling2D((2,2)),\n     \n                    layers.Conv2D(64,(3,3),activation=\"relu\"),\n                    layers.MaxPooling2D((2,2)),\n    \n                    layers.Conv2D(54,(3,3),activation=\"relu\"),\n                    layers.MaxPooling2D((2,2)),\n    \n                     #dense_layer\n                     layers.Flatten(),\n                     layers.Dense(64,activation=\"relu\"),\n                     layers.Dense(2,activation=\"softmax\")   \n])","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:06:22.123899Z","iopub.execute_input":"2022-03-01T09:06:22.124164Z","iopub.status.idle":"2022-03-01T09:06:22.303595Z","shell.execute_reply.started":"2022-03-01T09:06:22.124134Z","shell.execute_reply":"2022-03-01T09:06:22.301633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss=\"sparse_categorical_crossentropy\", metrics=[\"accuracy\"])\nmodel.fit(data,lab,epochs=8)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:06:22.304727Z","iopub.execute_input":"2022-03-01T09:06:22.305206Z","iopub.status.idle":"2022-03-01T09:50:17.305923Z","shell.execute_reply.started":"2022-03-01T09:06:22.305173Z","shell.execute_reply":"2022-03-01T09:50:17.305066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_set=[]\nimg_dir_test='../input/dog-vs-cat-classification/'","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:50:17.307196Z","iopub.execute_input":"2022-03-01T09:50:17.307418Z","iopub.status.idle":"2022-03-01T09:50:17.311797Z","shell.execute_reply.started":"2022-03-01T09:50:17.307392Z","shell.execute_reply":"2022-03-01T09:50:17.310862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_dir=os.listdir(img_dir_test + 'test/' + 'test/')\nfor img_name in pred_dir:\n    image=cv2.imread(img_dir_test + 'test/' + 'test/' + img_name)\n    image=Image.fromarray(image,'RGB')\n    image=image.resize((128,128))\n    pred_set.append(np.array(image))\npred=np.asarray(pred_set)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:50:17.313319Z","iopub.execute_input":"2022-03-01T09:50:17.313815Z","iopub.status.idle":"2022-03-01T09:51:57.278545Z","shell.execute_reply.started":"2022-03-01T09:50:17.313775Z","shell.execute_reply":"2022-03-01T09:51:57.277683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=model.predict(pred)\nresult=np.argmax(X,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:51:57.280105Z","iopub.execute_input":"2022-03-01T09:51:57.280619Z","iopub.status.idle":"2022-03-01T09:52:20.524174Z","shell.execute_reply.started":"2022-03-01T09:51:57.280578Z","shell.execute_reply":"2022-03-01T09:52:20.523087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result=np.transpose(result)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:52:20.525457Z","iopub.execute_input":"2022-03-01T09:52:20.525757Z","iopub.status.idle":"2022-03-01T09:52:20.530277Z","shell.execute_reply.started":"2022-03-01T09:52:20.525722Z","shell.execute_reply":"2022-03-01T09:52:20.529611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dF = pd.DataFrame(data = result,columns=[\"labels\"])\ndF","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:52:20.531583Z","iopub.execute_input":"2022-03-01T09:52:20.531844Z","iopub.status.idle":"2022-03-01T09:52:20.549320Z","shell.execute_reply.started":"2022-03-01T09:52:20.531812Z","shell.execute_reply":"2022-03-01T09:52:20.548622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df.drop(\"labels\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:52:20.550486Z","iopub.execute_input":"2022-03-01T09:52:20.551165Z","iopub.status.idle":"2022-03-01T09:52:20.567006Z","shell.execute_reply.started":"2022-03-01T09:52:20.551124Z","shell.execute_reply":"2022-03-01T09:52:20.566044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.concat([df,dF],axis=1)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:52:20.568551Z","iopub.execute_input":"2022-03-01T09:52:20.569227Z","iopub.status.idle":"2022-03-01T09:52:20.587604Z","shell.execute_reply.started":"2022-03-01T09:52:20.569190Z","shell.execute_reply":"2022-03-01T09:52:20.586382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:52:20.589341Z","iopub.execute_input":"2022-03-01T09:52:20.589677Z","iopub.status.idle":"2022-03-01T09:52:20.617559Z","shell.execute_reply.started":"2022-03-01T09:52:20.589625Z","shell.execute_reply":"2022-03-01T09:52:20.616649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!kaggle competitions submit -c dog-vs-cat-classification -f submission.csv -m \"Message\"","metadata":{"execution":{"iopub.status.busy":"2022-03-01T09:52:20.618831Z","iopub.execute_input":"2022-03-01T09:52:20.619112Z","iopub.status.idle":"2022-03-01T09:52:21.916921Z","shell.execute_reply.started":"2022-03-01T09:52:20.619073Z","shell.execute_reply":"2022-03-01T09:52:21.915864Z"},"trusted":true},"execution_count":null,"outputs":[]}]}