{"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 numpy as np # linear algebra\nimport pandas as pd\nimport os \nimport cv2\nimport random\nimport matplotlib.pyplot as plt\nimport pickle\nimport keras\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:25:21.749687Z","iopub.execute_input":"2022-03-03T02:25:21.750180Z","iopub.status.idle":"2022-03-03T02:25:26.681712Z","shell.execute_reply.started":"2022-03-03T02:25:21.750120Z","shell.execute_reply":"2022-03-03T02:25:26.680892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir = \"../input/dog-vs-cat-classification/train/train\"\ncategorical = ['cats', 'dogs']","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:25:26.683500Z","iopub.execute_input":"2022-03-03T02:25:26.683764Z","iopub.status.idle":"2022-03-03T02:25:26.687728Z","shell.execute_reply.started":"2022-03-03T02:25:26.683728Z","shell.execute_reply":"2022-03-03T02:25:26.687030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scale = 100\ndata = []\nfor category in categorical:\n    folder = os.path.join(dir, category)\n    label = categorical.index(category)\n    for img in os.listdir(folder):\n        img_path = os.path.join(folder, img)\n        image = cv2.imread(img_path)\n        image = cv2.resize(image, (scale,scale))\n        data.append([image, label])\n       ","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:25:26.689783Z","iopub.execute_input":"2022-03-03T02:25:26.690373Z","iopub.status.idle":"2022-03-03T02:28:13.183995Z","shell.execute_reply.started":"2022-03-03T02:25:26.690330Z","shell.execute_reply":"2022-03-03T02:28:13.183159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(data)","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:28:13.186542Z","iopub.execute_input":"2022-03-03T02:28:13.186857Z","iopub.status.idle":"2022-03-03T02:28:13.215843Z","shell.execute_reply.started":"2022-03-03T02:28:13.186820Z","shell.execute_reply":"2022-03-03T02:28:13.214979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X= []\ny= []\n\nfor feature, label in data:\n    X.append(feature)\n    y.append(label)\nX= np.array(X)\ny= np.array(y)\nX = X/255","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:28:13.219160Z","iopub.execute_input":"2022-03-03T02:28:13.219373Z","iopub.status.idle":"2022-03-03T02:28:15.365636Z","shell.execute_reply.started":"2022-03-03T02:28:13.219348Z","shell.execute_reply":"2022-03-03T02:28:15.364827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:28:15.367081Z","iopub.execute_input":"2022-03-03T02:28:15.367354Z","iopub.status.idle":"2022-03-03T02:28:15.373064Z","shell.execute_reply.started":"2022-03-03T02:28:15.367318Z","shell.execute_reply":"2022-03-03T02:28:15.372244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64, (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D((2,2)))\n\nmodel.add(Conv2D(64, (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D((2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, input_shape = (100,100,3), activation = 'relu'))\n\nmodel.add(Dense(2, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:28:15.374611Z","iopub.execute_input":"2022-03-03T02:28:15.374872Z","iopub.status.idle":"2022-03-03T02:28:17.677956Z","shell.execute_reply.started":"2022-03-03T02:28:15.374830Z","shell.execute_reply":"2022-03-03T02:28:17.677201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:28:17.679375Z","iopub.execute_input":"2022-03-03T02:28:17.679791Z","iopub.status.idle":"2022-03-03T02:28:17.698006Z","shell.execute_reply.started":"2022-03-03T02:28:17.679752Z","shell.execute_reply":"2022-03-03T02:28:17.697162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X,y, epochs= 25, validation_split=0.3, batch_size =32)","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:28:17.699659Z","iopub.execute_input":"2022-03-03T02:28:17.699929Z","iopub.status.idle":"2022-03-03T02:30:44.813878Z","shell.execute_reply.started":"2022-03-03T02:28:17.699892Z","shell.execute_reply":"2022-03-03T02:30:44.813046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model_1\", \"./\")","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:30:44.817778Z","iopub.execute_input":"2022-03-03T02:30:44.818240Z","iopub.status.idle":"2022-03-03T02:30:45.962453Z","shell.execute_reply.started":"2022-03-03T02:30:44.818206Z","shell.execute_reply":"2022-03-03T02:30:45.961658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model('./model_1')","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:30:45.964110Z","iopub.execute_input":"2022-03-03T02:30:45.964367Z","iopub.status.idle":"2022-03-03T02:30:46.682071Z","shell.execute_reply.started":"2022-03-03T02:30:45.964329Z","shell.execute_reply":"2022-03-03T02:30:46.681306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path =\"../input/dog-vs-cat-classification/test/test/000000.jpg\"\nimg = cv2.imread(path)\nnew_arr = cv2.resize(img, (scale, scale))\nplt.imshow(new_arr)\nnew_arr = np.array(new_arr)\nnew_arr =new_arr/255\nnew_arr = new_arr.reshape(1,100,100,3)\nprediction = model.predict(new_arr)\nprint(categorical[prediction.argmax()])","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:30:46.683846Z","iopub.execute_input":"2022-03-03T02:30:46.684138Z","iopub.status.idle":"2022-03-03T02:30:47.091728Z","shell.execute_reply.started":"2022-03-03T02:30:46.684083Z","shell.execute_reply":"2022-03-03T02:30:47.090959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category=[0,1]\ncategory[prediction.argmax()]","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:30:47.092895Z","iopub.execute_input":"2022-03-03T02:30:47.093168Z","iopub.status.idle":"2022-03-03T02:30:47.099837Z","shell.execute_reply.started":"2022-03-03T02:30:47.093130Z","shell.execute_reply":"2022-03-03T02:30:47.098971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir =\"../input/dog-vs-cat-classification/test/test\"\nscale = 100\npred = []\nfilename=[]\ncategory=[0,1]\n\nfor img in os.listdir(dir):\n    img_path = os.path.join(dir, img)\n    image = cv2.imread(img_path)\n    image = cv2.resize(image, (scale,scale))\n    new_arr = np.array(image)/255\n    new_arr = new_arr.reshape(1,100,100,3)\n    prediction = model.predict(new_arr)\n    pred.append(category[prediction.argmax()])\n    filename.append(img)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:30:47.101525Z","iopub.execute_input":"2022-03-03T02:30:47.101790Z","iopub.status.idle":"2022-03-03T02:36:41.058422Z","shell.execute_reply.started":"2022-03-03T02:30:47.101753Z","shell.execute_reply":"2022-03-03T02:36:41.057575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit= pd.DataFrame({\"id\": filename, \"labels\":pred})\nsubmit.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:36:41.059868Z","iopub.execute_input":"2022-03-03T02:36:41.060144Z","iopub.status.idle":"2022-03-03T02:36:41.094129Z","shell.execute_reply.started":"2022-03-03T02:36:41.060093Z","shell.execute_reply":"2022-03-03T02:36:41.093013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-03T02:36:41.126033Z","iopub.execute_input":"2022-03-03T02:36:41.126327Z","iopub.status.idle":"2022-03-03T02:36:41.146032Z","shell.execute_reply.started":"2022-03-03T02:36:41.126290Z","shell.execute_reply":"2022-03-03T02:36:41.145403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}