{"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 tensorflow as tf\nfrom tensorflow.keras.layers import Dense,Conv2D,Flatten, MaxPool2D, Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport os\nimport cv2\nfrom tensorflow.keras.models import Sequential,load_model\nfrom tensorflow.keras.applications.vgg16 import VGG16,preprocess_input","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-04T04:14:34.494407Z","iopub.execute_input":"2022-05-04T04:14:34.494762Z","iopub.status.idle":"2022-05-04T04:14:39.778726Z","shell.execute_reply.started":"2022-05-04T04:14:34.494679Z","shell.execute_reply":"2022-05-04T04:14:39.778003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/dogs-vs-cats/train.zip","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:48:55.498838Z","iopub.execute_input":"2022-04-28T12:48:55.499085Z","iopub.status.idle":"2022-04-28T12:49:07.409837Z","shell.execute_reply.started":"2022-04-28T12:48:55.499042Z","shell.execute_reply":"2022-04-28T12:49:07.408923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:49:07.41174Z","iopub.execute_input":"2022-04-28T12:49:07.412009Z","iopub.status.idle":"2022-04-28T12:49:08.219164Z","shell.execute_reply.started":"2022-04-28T12:49:07.411973Z","shell.execute_reply":"2022-04-28T12:49:08.218315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:49:08.221647Z","iopub.execute_input":"2022-04-28T12:49:08.224906Z","iopub.status.idle":"2022-04-28T12:49:09.013789Z","shell.execute_reply.started":"2022-04-28T12:49:08.224863Z","shell.execute_reply":"2022-04-28T12:49:09.012862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir images","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:49:09.01573Z","iopub.execute_input":"2022-04-28T12:49:09.016006Z","iopub.status.idle":"2022-04-28T12:49:09.676554Z","shell.execute_reply.started":"2022-04-28T12:49:09.015969Z","shell.execute_reply":"2022-04-28T12:49:09.675487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c=0\nfor i in os.listdir('./train'):\n    c=c+1\n    label=i.split(\".\")[0]\n    im=cv2.imread(os.path.join('./train',i))\n    img_rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)\n    if label not in os.listdir('./images'):\n        os.makedirs(os.path.join('./images',label))\n    cv2.imwrite(os.path.join('./images',label,i),img_rgb)\nprint(c)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:49:09.680441Z","iopub.execute_input":"2022-04-28T12:49:09.680684Z","iopub.status.idle":"2022-04-28T12:51:12.730939Z","shell.execute_reply.started":"2022-04-28T12:49:09.680655Z","shell.execute_reply":"2022-04-28T12:51:12.73006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:12.732337Z","iopub.execute_input":"2022-04-28T12:51:12.732608Z","iopub.status.idle":"2022-04-28T12:51:12.73674Z","shell.execute_reply.started":"2022-04-28T12:51:12.732573Z","shell.execute_reply":"2022-04-28T12:51:12.735862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in os.listdir(\"/kaggle/working/images/cat\"):\n    im=cv2.imread(os.path.join(\"/kaggle/working/images/cat\",i))\n    img_rgb=cv2.cvtColor(cv2.COLOR_BRG2RGB)\n    plt.imshow(im)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:12.73817Z","iopub.execute_input":"2022-04-28T12:51:12.738692Z","iopub.status.idle":"2022-04-28T12:51:13.011605Z","shell.execute_reply.started":"2022-04-28T12:51:12.738654Z","shell.execute_reply":"2022-04-28T12:51:13.010896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:13.012581Z","iopub.execute_input":"2022-04-28T12:51:13.012803Z","iopub.status.idle":"2022-04-28T12:51:13.668926Z","shell.execute_reply.started":"2022-04-28T12:51:13.012771Z","shell.execute_reply":"2022-04-28T12:51:13.668052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen= ImageDataGenerator(rotation_range=10,\n                            fill_mode='nearest',\n                            brightness_range=[0.5,1.5]\n                           zoom_range=0.2,\n                           height_shift_range=0.1,\n                           width_shift_range=0.1,\n                           horizontal_flip=True,\n                           validation_split=0.2,\n                           rescale=1/255)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:13.673465Z","iopub.execute_input":"2022-04-28T12:51:13.67413Z","iopub.status.idle":"2022-04-28T12:51:13.68041Z","shell.execute_reply.started":"2022-04-28T12:51:13.674096Z","shell.execute_reply":"2022-04-28T12:51:13.679516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=datagen.flow_from_directory('/kaggle/working/images',target_size=(112,112),class_mode='binary',batch_size=32,subset=\"training\",color_mode='rgb')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:13.681718Z","iopub.execute_input":"2022-04-28T12:51:13.682198Z","iopub.status.idle":"2022-04-28T12:51:14.248186Z","shell.execute_reply.started":"2022-04-28T12:51:13.68216Z","shell.execute_reply":"2022-04-28T12:51:14.247428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data=datagen.flow_from_directory('/kaggle/working/images',target_size=(112,112),class_mode='binary',batch_size=32,subset=\"validation\",color_mode='rgb')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:14.249519Z","iopub.execute_input":"2022-04-28T12:51:14.249929Z","iopub.status.idle":"2022-04-28T12:51:14.470899Z","shell.execute_reply.started":"2022-04-28T12:51:14.24989Z","shell.execute_reply":"2022-04-28T12:51:14.469435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.next()[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:14.472046Z","iopub.execute_input":"2022-04-28T12:51:14.472292Z","iopub.status.idle":"2022-04-28T12:51:14.641433Z","shell.execute_reply.started":"2022-04-28T12:51:14.472257Z","shell.execute_reply":"2022-04-28T12:51:14.639821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nx= train_data.next()[0]\nfor i in range(0,4):\n    image = x[i]\n    plt.imshow(image)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:14.642618Z","iopub.execute_input":"2022-04-28T12:51:14.642937Z","iopub.status.idle":"2022-04-28T12:51:15.572833Z","shell.execute_reply.started":"2022-04-28T12:51:14.642899Z","shell.execute_reply":"2022-04-28T12:51:15.572169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg=VGG16(include_top=False,weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:15.574162Z","iopub.execute_input":"2022-04-28T12:51:15.574607Z","iopub.status.idle":"2022-04-28T12:51:18.847712Z","shell.execute_reply.started":"2022-04-28T12:51:15.57457Z","shell.execute_reply":"2022-04-28T12:51:18.846682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in vgg.layers[:-5]:\n    i.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:18.849206Z","iopub.execute_input":"2022-04-28T12:51:18.849481Z","iopub.status.idle":"2022-04-28T12:51:18.854793Z","shell.execute_reply.started":"2022-04-28T12:51:18.849434Z","shell.execute_reply":"2022-04-28T12:51:18.854041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=(112,112,3),activation='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Conv2D(64,kernel_size=(3,3),activation='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1,activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:18.85637Z","iopub.execute_input":"2022-04-28T12:51:18.857283Z","iopub.status.idle":"2022-04-28T12:51:18.937425Z","shell.execute_reply.started":"2022-04-28T12:51:18.857246Z","shell.execute_reply":"2022-04-28T12:51:18.936627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(loss='binary_crossentropy',optimizer=tf.keras.optimizers.Adam(1e-5),metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:18.938719Z","iopub.execute_input":"2022-04-28T12:51:18.938996Z","iopub.status.idle":"2022-04-28T12:51:18.943684Z","shell.execute_reply.started":"2022-04-28T12:51:18.938949Z","shell.execute_reply":"2022-04-28T12:51:18.942504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_data,validation_data=val_data,epochs=50)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T12:51:18.945739Z","iopub.execute_input":"2022-04-28T12:51:18.946119Z","iopub.status.idle":"2022-04-28T14:45:13.031429Z","shell.execute_reply.started":"2022-04-28T12:51:18.946076Z","shell.execute_reply":"2022-04-28T14:45:13.030527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('dogsvscats.h5')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T14:45:13.03454Z","iopub.execute_input":"2022-04-28T14:45:13.034933Z","iopub.status.idle":"2022-04-28T14:45:13.091524Z","shell.execute_reply.started":"2022-04-28T14:45:13.034889Z","shell.execute_reply":"2022-04-28T14:45:13.09062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T14:45:29.77676Z","iopub.execute_input":"2022-04-28T14:45:29.77735Z","iopub.status.idle":"2022-04-28T14:45:29.973963Z","shell.execute_reply.started":"2022-04-28T14:45:29.77731Z","shell.execute_reply":"2022-04-28T14:45:29.973247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T14:45:19.361138Z","iopub.execute_input":"2022-04-28T14:45:19.361697Z","iopub.status.idle":"2022-04-28T14:45:19.541022Z","shell.execute_reply.started":"2022-04-28T14:45:19.361659Z","shell.execute_reply":"2022-04-28T14:45:19.54022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2022-04-28T14:45:40.64758Z","iopub.execute_input":"2022-04-28T14:45:40.648313Z","iopub.status.idle":"2022-04-28T14:45:41.387922Z","shell.execute_reply.started":"2022-04-28T14:45:40.648268Z","shell.execute_reply":"2022-04-28T14:45:41.387009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=load_model('../input/dogsvscat/dogsvscats.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:15:07.332758Z","iopub.execute_input":"2022-05-04T04:15:07.333469Z","iopub.status.idle":"2022-05-04T04:15:09.958930Z","shell.execute_reply.started":"2022-05-04T04:15:07.333432Z","shell.execute_reply":"2022-05-04T04:15:09.958210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/dogs-vs-cats/test1.zip","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:15:09.961304Z","iopub.execute_input":"2022-05-04T04:15:09.961968Z","iopub.status.idle":"2022-05-04T04:15:15.786573Z","shell.execute_reply.started":"2022-05-04T04:15:09.961922Z","shell.execute_reply":"2022-05-04T04:15:15.785829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:15:15.788294Z","iopub.execute_input":"2022-05-04T04:15:15.788794Z","iopub.status.idle":"2022-05-04T04:15:15.793386Z","shell.execute_reply.started":"2022-05-04T04:15:15.788751Z","shell.execute_reply":"2022-05-04T04:15:15.792361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res=[]\nc_dog=0\nc_cat=0\nfor i in os.listdir('./test1'):\n    img=tf.keras.preprocessing.image.load_img(os.path.join('./test1',i),target_size=(112,112,3))\n    np_img=tf.keras.preprocessing.image.img_to_array(img)\n    np_img=np_img/255\n    plt.imshow(np_img)\n    np_img=np.expand_dims(np_img,axis=0)\n    print(np_img.shape)\n    pred=model.predict(np_img)\n#     print(pred[0][0]>0.5)\n    if pred[0][0]>0.5:\n        c_dog=c_dog+1\n        res.append('dog')\n    else:\n        c_cat=c_cat+1\n        res.append('cat')\n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:44:34.896278Z","iopub.execute_input":"2022-05-04T04:44:34.896546Z","iopub.status.idle":"2022-05-04T04:56:39.729484Z","shell.execute_reply.started":"2022-05-04T04:44:34.896517Z","shell.execute_reply":"2022-05-04T04:56:39.728719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c_dog,c_cat","metadata":{"execution":{"iopub.status.busy":"2022-05-04T05:30:58.486946Z","iopub.execute_input":"2022-05-04T05:30:58.487463Z","iopub.status.idle":"2022-05-04T05:30:58.492662Z","shell.execute_reply.started":"2022-05-04T05:30:58.487426Z","shell.execute_reply":"2022-05-04T05:30:58.491803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res=[0 if x=='cat' else 1 for x in res]","metadata":{"execution":{"iopub.status.busy":"2022-05-04T05:33:34.797195Z","iopub.execute_input":"2022-05-04T05:33:34.797474Z","iopub.status.idle":"2022-05-04T05:33:34.803455Z","shell.execute_reply.started":"2022-05-04T05:33:34.797445Z","shell.execute_reply":"2022-05-04T05:33:34.802322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nsubmission=pd.read_csv('../input/dogs-vs-cats/sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T05:31:05.396619Z","iopub.execute_input":"2022-05-04T05:31:05.397205Z","iopub.status.idle":"2022-05-04T05:31:05.422665Z","shell.execute_reply.started":"2022-05-04T05:31:05.397166Z","shell.execute_reply":"2022-05-04T05:31:05.421937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['label']=res","metadata":{"execution":{"iopub.status.busy":"2022-05-04T05:34:03.711717Z","iopub.execute_input":"2022-05-04T05:34:03.712053Z","iopub.status.idle":"2022-05-04T05:34:03.728026Z","shell.execute_reply.started":"2022-05-04T05:34:03.712015Z","shell.execute_reply":"2022-05-04T05:34:03.727376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T05:34:17.044208Z","iopub.execute_input":"2022-05-04T05:34:17.044753Z","iopub.status.idle":"2022-05-04T05:34:17.053120Z","shell.execute_reply.started":"2022-05-04T05:34:17.044713Z","shell.execute_reply":"2022-05-04T05:34:17.052069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('dogs_cats.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T05:34:53.277399Z","iopub.execute_input":"2022-05-04T05:34:53.277700Z","iopub.status.idle":"2022-05-04T05:34:53.310342Z","shell.execute_reply.started":"2022-05-04T05:34:53.277664Z","shell.execute_reply":"2022-05-04T05:34:53.309650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}