{"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":"# 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-01T16:03:54.860360Z","iopub.execute_input":"2022-08-01T16:03:54.860880Z","iopub.status.idle":"2022-08-01T16:03:54.901611Z","shell.execute_reply.started":"2022-08-01T16:03:54.860779Z","shell.execute_reply":"2022-08-01T16:03:54.900375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport cv2\nimport matplotlib.pyplot as plt \nimport os","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:56:21.185513Z","iopub.execute_input":"2022-08-04T06:56:21.185828Z","iopub.status.idle":"2022-08-04T06:56:25.572086Z","shell.execute_reply.started":"2022-08-04T06:56:21.185755Z","shell.execute_reply":"2022-08-04T06:56:25.570178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip -q /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:03:59.501314Z","iopub.execute_input":"2022-08-01T16:03:59.501964Z","iopub.status.idle":"2022-08-01T16:04:10.118881Z","shell.execute_reply.started":"2022-08-01T16:03:59.501925Z","shell.execute_reply":"2022-08-01T16:04:10.117350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip -q /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:10.121853Z","iopub.execute_input":"2022-08-01T16:04:10.122857Z","iopub.status.idle":"2022-08-01T16:04:15.618773Z","shell.execute_reply.started":"2022-08-01T16:04:10.122803Z","shell.execute_reply":"2022-08-01T16:04:15.617518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasets_train = os.listdir('train')\ndatasets_test = os.listdir('test')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:20:34.859431Z","iopub.execute_input":"2022-08-01T16:20:34.860377Z","iopub.status.idle":"2022-08-01T16:20:34.942904Z","shell.execute_reply.started":"2022-08-01T16:20:34.860250Z","shell.execute_reply":"2022-08-01T16:20:34.941577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasets_train[0:10]  # data are images with this name  ok\ndatasets_test[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:15.650457Z","iopub.execute_input":"2022-08-01T16:04:15.651260Z","iopub.status.idle":"2022-08-01T16:04:15.687042Z","shell.execute_reply.started":"2022-08-01T16:04:15.651224Z","shell.execute_reply":"2022-08-01T16:04:15.685918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [] \nfor imagename in datasets_train:\n    if 'dog' in  imagename:\n        labels.append('dog')\n    elif 'cat' in imagename:\n        labels.append('cat')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:15.688473Z","iopub.execute_input":"2022-08-01T16:04:15.688997Z","iopub.status.idle":"2022-08-01T16:04:15.701120Z","shell.execute_reply.started":"2022-08-01T16:04:15.688962Z","shell.execute_reply":"2022-08-01T16:04:15.700189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preparing a dataframe of images and their labels\ndfx = pd.DataFrame()\ndfx['imagename']= datasets_train\ndfx['labels']= labels\ndftest = pd.DataFrame()\ndftest['image'] = datasets_test","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:15.703664Z","iopub.execute_input":"2022-08-01T16:04:15.704128Z","iopub.status.idle":"2022-08-01T16:04:15.732260Z","shell.execute_reply.started":"2022-08-01T16:04:15.704100Z","shell.execute_reply":"2022-08-01T16:04:15.731450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndftest.head()\ndfx.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:15.733733Z","iopub.execute_input":"2022-08-01T16:04:15.734058Z","iopub.status.idle":"2022-08-01T16:04:15.749481Z","shell.execute_reply.started":"2022-08-01T16:04:15.734027Z","shell.execute_reply":"2022-08-01T16:04:15.748541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(imageadd):\n    image = cv2.imread(imageadd)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.title('imagename')\n    plt.imshow(image)\nshow_image('/kaggle/working/train/'+ dfx.imagename[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:15.750675Z","iopub.execute_input":"2022-08-01T16:04:15.751575Z","iopub.status.idle":"2022-08-01T16:04:16.068815Z","shell.execute_reply.started":"2022-08-01T16:04:15.751542Z","shell.execute_reply":"2022-08-01T16:04:16.067781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor i in range(10):\n    image = cv2.imread('/kaggle/working/train/'+ dfx.loc[i,'imagename'])\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n    plt.subplot(2,5,i+1)\n    plt.title(dfx.loc[i,'imagename'])\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:44:37.676492Z","iopub.execute_input":"2022-08-01T13:44:37.677111Z","iopub.status.idle":"2022-08-01T13:44:38.252897Z","shell.execute_reply.started":"2022-08-01T13:44:37.677075Z","shell.execute_reply":"2022-08-01T13:44:38.251964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking balance data\ndfx.describe()\ndfx.value_counts('labels')\ndfx.duplicated('imagename').sum()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:44:38.254546Z","iopub.execute_input":"2022-08-01T13:44:38.255236Z","iopub.status.idle":"2022-08-01T13:44:38.302926Z","shell.execute_reply.started":"2022-08-01T13:44:38.255195Z","shell.execute_reply":"2022-08-01T13:44:38.302025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*data is fullly balanced *","metadata":{}},{"cell_type":"code","source":"images = []\nfor imagename in os.listdir('train'):\n    \n    image = cv2.imread('/kaggle/working/train/'+ imagename)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    images.append(image)\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(images)\nlistshape=[]\n\nfor i in range(len(x)):\n    listshape.append(x[i].shape)\nshapearray = np.array(listshape)\nshapearray[0:5]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#mean pixel value od all images\nshapearray.mean(axis=0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking if all images are 3 layer rbg\ncount = 0\nfor i in range(len(shapearray)):\n    if shapearray[i,2] !=3:\n        count = count+1\ncount","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**preparing image data generator**","metadata":{}},{"cell_type":"code","source":"\nidg = tf.keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True,\n                                                      validation_split= 0.1,#\n                                                      preprocessing_function=tf.keras.applications.vgg16.preprocess_input ,\n                                                      width_shift_range=0.1,\n                                                      height_shift_range=0.1,\n                                                      zoom_range=0.2,\n                                                      rotation_range=25,\n                                                     )","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:56:25.574829Z","iopub.execute_input":"2022-08-04T06:56:25.575477Z","iopub.status.idle":"2022-08-04T06:56:26.479779Z","shell.execute_reply.started":"2022-08-04T06:56:25.575437Z","shell.execute_reply":"2022-08-04T06:56:26.478775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('viewaugimages')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx.imagename[8]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = dfx.imagename[8]\nimage = cv2.imread('/kaggle/working/train/'+ image)\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\nimage2 = np.expand_dims(image,axis=0)\nimage2.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking images augmented by idg\ni=0\nfor _ in idg.flow(image2,save_to_dir='viewaugimages'):\n    i=i+1\n    if i >24:\n        break","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auglist=[]\nfor imagename in os.listdir('viewaugimages'):\n    image = cv2.imread('viewaugimages/'+ imagename)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    auglist.append(image)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(auglist)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize= (20,20))\nfor i in range(25):\n    image = auglist[i]\n    #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.subplot(5,5,i+1)\n    plt.imshow(image)\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bs = 32","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:17.338214Z","iopub.execute_input":"2022-08-01T16:04:17.338598Z","iopub.status.idle":"2022-08-01T16:04:17.347631Z","shell.execute_reply.started":"2022-08-01T16:04:17.338563Z","shell.execute_reply":"2022-08-01T16:04:17.346562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"taking image size half of mean and maintaining aspect ratio","metadata":{}},{"cell_type":"code","source":"train_idg = idg.flow_from_dataframe(dfx,directory = '/kaggle/working/train/',\n                                    x_col = 'imagename',y_col = 'labels',\n                                    target_size =(180,200),\n                                    batch_size =bs,\n                                    subset = 'training')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:56:32.443615Z","iopub.execute_input":"2022-08-04T06:56:32.443979Z","iopub.status.idle":"2022-08-04T06:56:32.696900Z","shell.execute_reply.started":"2022-08-04T06:56:32.443949Z","shell.execute_reply":"2022-08-04T06:56:32.695462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_idg = idg.flow_from_dataframe(dfx,directory = '/kaggle/working/train/',\n                                    x_col = 'imagename',y_col = 'labels',\n                                    target_size =(180,200),\n                                    batch_size =bs,\n                                    subset = 'validation')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:56:32.739442Z","iopub.execute_input":"2022-08-04T06:56:32.740042Z","iopub.status.idle":"2022-08-04T06:56:32.762647Z","shell.execute_reply.started":"2022-08-04T06:56:32.740006Z","shell.execute_reply":"2022-08-04T06:56:32.760809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TRANSFERING VGG**","metadata":{}},{"cell_type":"code","source":"VGG16transfer = tf.keras.applications.VGG16(\n    include_top=False,\n    input_shape= (180,200,3),\n    weights=\"imagenet\"\n    \n)\n#look at model structure\ntf.keras.utils.plot_model(VGG16transfer)\n#making weights not trainable of vgg\nfor layer in VGG16transfer.layers:\n    print(layer.name)\n    print(layer.input_shape)\n    print(layer.output_shape)\n\n    print(layer.trainable)\n    layer.trainable = False\n    print(layer.trainable)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:21.699051Z","iopub.execute_input":"2022-08-01T16:04:21.699425Z","iopub.status.idle":"2022-08-01T16:04:26.840399Z","shell.execute_reply.started":"2022-08-01T16:04:21.699372Z","shell.execute_reply":"2022-08-01T16:04:26.839129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**BUILDING MODEL**","metadata":{}},{"cell_type":"code","source":"flat1 = tf.keras.layers.Flatten() (VGG16transfer.output)\nd1 = tf.keras.layers.Dense(32,activation = 'relu') (flat1)\npred = tf.keras.layers.Dense(2,activation = 'softmax') (d1)\n\n\nmodel1 = tf.keras.Model(inputs = [VGG16transfer.input], outputs = [pred])\n\nfor layer in model1.layers:\n    print(layer.trainable)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:26.843210Z","iopub.execute_input":"2022-08-01T16:04:26.843608Z","iopub.status.idle":"2022-08-01T16:04:26.878551Z","shell.execute_reply.started":"2022-08-01T16:04:26.843565Z","shell.execute_reply":"2022-08-01T16:04:26.877639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:26.881314Z","iopub.execute_input":"2022-08-01T16:04:26.881661Z","iopub.status.idle":"2022-08-01T16:04:26.892111Z","shell.execute_reply.started":"2022-08-01T16:04:26.881635Z","shell.execute_reply":"2022-08-01T16:04:26.890951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(optimizer = tf.keras.optimizers.SGD(learning_rate = 0.0031415) ,\n              loss = tf.keras.losses.categorical_crossentropy ,\n              metrics=['acc']\n             )","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:26.895008Z","iopub.execute_input":"2022-08-01T16:04:26.895710Z","iopub.status.idle":"2022-08-01T16:04:26.909504Z","shell.execute_reply.started":"2022-08-01T16:04:26.895675Z","shell.execute_reply":"2022-08-01T16:04:26.908649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.fit(train_idg, epochs = 11,validation_data= valid_idg , batch_size= bs)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:04:31.061470Z","iopub.execute_input":"2022-08-01T16:04:31.062160Z","iopub.status.idle":"2022-08-01T16:08:44.844937Z","shell.execute_reply.started":"2022-08-01T16:04:31.062124Z","shell.execute_reply":"2022-08-01T16:08:44.843985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict = model1.history.history\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:08:44.847294Z","iopub.execute_input":"2022-08-01T16:08:44.847771Z","iopub.status.idle":"2022-08-01T16:08:44.852374Z","shell.execute_reply.started":"2022-08-01T16:08:44.847733Z","shell.execute_reply":"2022-08-01T16:08:44.851321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.figure(figsize=(15,10))\nplt.plot(dict['acc'])\n# plt.plot(dict['val_acc'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:08:44.854177Z","iopub.execute_input":"2022-08-01T16:08:44.855356Z","iopub.status.idle":"2022-08-01T16:08:45.031140Z","shell.execute_reply.started":"2022-08-01T16:08:44.855304Z","shell.execute_reply":"2022-08-01T16:08:45.030234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nplt.plot(dict.get('loss'))\n# plt.plot(dict.get('val_loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:08:45.033419Z","iopub.execute_input":"2022-08-01T16:08:45.034471Z","iopub.status.idle":"2022-08-01T16:08:45.202507Z","shell.execute_reply.started":"2022-08-01T16:08:45.034435Z","shell.execute_reply":"2022-08-01T16:08:45.201376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftest.sample(2)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:08:45.204200Z","iopub.execute_input":"2022-08-01T16:08:45.204881Z","iopub.status.idle":"2022-08-01T16:08:45.215702Z","shell.execute_reply.started":"2022-08-01T16:08:45.204844Z","shell.execute_reply":"2022-08-01T16:08:45.214571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idg2 = tf.keras.preprocessing.image.ImageDataGenerator(\n                                                         preprocessing_function=tf.keras.applications.vgg16.preprocess_input\n                                                           )\ntest_gen = idg2.flow_from_dataframe(\n    dftest, \n    '/kaggle/working/test', \n    x_col='image',\n    class_mode= None,\n    target_size=(180,200),\n    batch_size=bs,\n    shuffle=False\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:08:45.217321Z","iopub.execute_input":"2022-08-01T16:08:45.217832Z","iopub.status.idle":"2022-08-01T16:08:45.327768Z","shell.execute_reply.started":"2022-08-01T16:08:45.217796Z","shell.execute_reply":"2022-08-01T16:08:45.326785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model1.predict(test_gen,batch_size=bs,max_queue_size=1, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:08:45.328980Z","iopub.execute_input":"2022-08-01T16:08:45.329398Z","iopub.status.idle":"2022-08-01T16:09:25.565678Z","shell.execute_reply.started":"2022-08-01T16:08:45.329362Z","shell.execute_reply":"2022-08-01T16:09:25.564638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p=4\nprint((predict[4,1]))#prob of being a dog\nshow_image('/kaggle/working/test/'+ dftest.image[p])\nplt.title(dftest.image[p])\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:11:23.784704Z","iopub.execute_input":"2022-08-01T16:11:23.785109Z","iopub.status.idle":"2022-08-01T16:11:24.030252Z","shell.execute_reply.started":"2022-08-01T16:11:23.785077Z","shell.execute_reply":"2022-08-01T16:11:24.029320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_test = (predict[:,1])\nlabels_test[0:5]\n# dftest.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:12:40.628004Z","iopub.execute_input":"2022-08-01T16:12:40.628614Z","iopub.status.idle":"2022-08-01T16:12:40.635581Z","shell.execute_reply.started":"2022-08-01T16:12:40.628576Z","shell.execute_reply":"2022-08-01T16:12:40.634560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftest['id'] = dftest['image'].apply(lambda f: int(f.split('.')[0]))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:16:43.944449Z","iopub.execute_input":"2022-08-01T16:16:43.945345Z","iopub.status.idle":"2022-08-01T16:16:43.963200Z","shell.execute_reply.started":"2022-08-01T16:16:43.945296Z","shell.execute_reply":"2022-08-01T16:16:43.962276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:17:01.866935Z","iopub.execute_input":"2022-08-01T16:17:01.867274Z","iopub.status.idle":"2022-08-01T16:17:01.878679Z","shell.execute_reply.started":"2022-08-01T16:17:01.867246Z","shell.execute_reply":"2022-08-01T16:17:01.877456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame()\nresult['id']=   dftest.id\nresult['label']=labels_test\nresult.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:19:26.252218Z","iopub.execute_input":"2022-08-01T16:19:26.252839Z","iopub.status.idle":"2022-08-01T16:19:26.268591Z","shell.execute_reply.started":"2022-08-01T16:19:26.252800Z","shell.execute_reply":"2022-08-01T16:19:26.267021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#saving to  sumbission.csv","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('submission.csv',index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}