{"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 # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-29T03:01:29.072352Z","iopub.execute_input":"2022-11-29T03:01:29.073017Z","iopub.status.idle":"2022-11-29T03:01:29.112317Z","shell.execute_reply.started":"2022-11-29T03:01:29.072883Z","shell.execute_reply":"2022-11-29T03:01:29.111379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip -d.","metadata":{"execution":{"iopub.status.busy":"2022-12-01T06:35:54.972883Z","iopub.execute_input":"2022-12-01T06:35:54.973329Z","iopub.status.idle":"2022-12-01T06:36:07.320755Z","shell.execute_reply.started":"2022-12-01T06:35:54.973241Z","shell.execute_reply":"2022-12-01T06:36:07.319624Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip -d.","metadata":{"execution":{"iopub.status.busy":"2022-12-01T06:36:11.727319Z","iopub.execute_input":"2022-12-01T06:36:11.728289Z","iopub.status.idle":"2022-12-01T06:36:18.925039Z","shell.execute_reply.started":"2022-12-01T06:36:11.728247Z","shell.execute_reply":"2022-12-01T06:36:18.923886Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='./train/'\ntest_dir='./test/'","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.001422Z","iopub.execute_input":"2022-11-29T03:01:50.001852Z","iopub.status.idle":"2022-11-29T03:01:50.008010Z","shell.execute_reply.started":"2022-11-29T03:01:50.001808Z","shell.execute_reply":"2022-11-29T03:01:50.006950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=os.listdir(train_dir)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.009215Z","iopub.execute_input":"2022-11-29T03:01:50.010073Z","iopub.status.idle":"2022-11-29T03:01:50.031021Z","shell.execute_reply.started":"2022-11-29T03:01:50.010036Z","shell.execute_reply":"2022-11-29T03:01:50.030081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.DataFrame()\ntrain['file']=a\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.042370Z","iopub.execute_input":"2022-11-29T03:01:50.042994Z","iopub.status.idle":"2022-11-29T03:01:50.069461Z","shell.execute_reply.started":"2022-11-29T03:01:50.042956Z","shell.execute_reply":"2022-11-29T03:01:50.068443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=[]\nfor i in range(len(train)):\n    label=train['file'][i].split('.')[0]\n    if label=='dog':\n        target.append('1')\n    else:\n        target.append('0')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.124054Z","iopub.execute_input":"2022-11-29T03:01:50.124406Z","iopub.status.idle":"2022-11-29T03:01:50.289700Z","shell.execute_reply.started":"2022-11-29T03:01:50.124373Z","shell.execute_reply":"2022-11-29T03:01:50.288840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label']=target\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.312317Z","iopub.execute_input":"2022-11-29T03:01:50.312980Z","iopub.status.idle":"2022-11-29T03:01:50.333646Z","shell.execute_reply.started":"2022-11-29T03:01:50.312942Z","shell.execute_reply":"2022-11-29T03:01:50.332282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check=[]\nfor i in range(len(train)):\n    label=train['file'][i].split('.')[-1]\n    if label!='jpg':\n        check.append(train['file'][i])\ncheck","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.335395Z","iopub.execute_input":"2022-11-29T03:01:50.335985Z","iopub.status.idle":"2022-11-29T03:01:50.493621Z","shell.execute_reply.started":"2022-11-29T03:01:50.335947Z","shell.execute_reply":"2022-11-29T03:01:50.492578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image check","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\n\nimg1=Image.open(train_dir+'cat.1022.jpg')\nimg2=Image.open(train_dir+'dog.11329.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.494930Z","iopub.execute_input":"2022-11-29T03:01:50.495287Z","iopub.status.idle":"2022-11-29T03:01:50.528209Z","shell.execute_reply.started":"2022-11-29T03:01:50.495247Z","shell.execute_reply":"2022-11-29T03:01:50.527175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img1)\nplt.show()\nplt.imshow(img2)\nplt.show()\nprint(img1.size, img2.size)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:50.529659Z","iopub.execute_input":"2022-11-29T03:01:50.530011Z","iopub.status.idle":"2022-11-29T03:01:51.218518Z","shell.execute_reply.started":"2022-11-29T03:01:50.529977Z","shell.execute_reply":"2022-11-29T03:01:51.216380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Loader","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:51.227312Z","iopub.execute_input":"2022-11-29T03:01:51.230919Z","iopub.status.idle":"2022-11-29T03:01:51.255717Z","shell.execute_reply.started":"2022-11-29T03:01:51.230867Z","shell.execute_reply":"2022-11-29T03:01:51.253330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"isize=(224,224)\nishape=(224,224,3)\nbs=16\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nigen=ImageDataGenerator(rescale=1/255, validation_split=0.2)\ntrain0=igen.flow_from_dataframe(train,directory=train_dir, x_col='file', \n                                y_col='label', target_size=isize, \n                                class_mode='binary', batch_size=bs,\n                               subset='training')\nval0=igen.flow_from_dataframe(train,directory=train_dir, x_col='file', \n                                y_col='label', target_size=isize, \n                                class_mode='binary', batch_size=bs,\n                               subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:51.264172Z","iopub.execute_input":"2022-11-29T03:01:51.267694Z","iopub.status.idle":"2022-11-29T03:01:57.659037Z","shell.execute_reply.started":"2022-11-29T03:01:51.267629Z","shell.execute_reply":"2022-11-29T03:01:57.657968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y=train0.next()\nfor i in range(bs):\n    plt.imshow((x[i]*255).astype('int'))\n    plt.show()\n    print(y[i])","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:01:57.660456Z","iopub.execute_input":"2022-11-29T03:01:57.661393Z","iopub.status.idle":"2022-11-29T03:02:01.279904Z","shell.execute_reply.started":"2022-11-29T03:01:57.661352Z","shell.execute_reply":"2022-11-29T03:02:01.278993Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer learning","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras import Model, Input\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout\nfrom tensorflow.keras.applications import Xception","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:02:01.281125Z","iopub.execute_input":"2022-11-29T03:02:01.281867Z","iopub.status.idle":"2022-11-29T03:02:01.290705Z","shell.execute_reply.started":"2022-11-29T03:02:01.281829Z","shell.execute_reply":"2022-11-29T03:02:01.289407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base=Xception(include_top=False, weights='imagenet', input_shape=ishape)\nx= base.output\nx= GlobalAveragePooling2D()(x)\nx= Dropout(0.4)(x)\nx= Dense(1024, activation='relu')(x)\nx= Dropout(0.3)(x)\nx= Dense(256, activation='relu')(x)\nx= Dense(16, activation='relu')(x)\npred= Dense(1, activation='sigmoid')(x)\nmodel=Model(inputs=base.input, outputs=pred)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:02:01.292604Z","iopub.execute_input":"2022-11-29T03:02:01.293316Z","iopub.status.idle":"2022-11-29T03:02:06.311910Z","shell.execute_reply.started":"2022-11-29T03:02:01.293277Z","shell.execute_reply":"2022-11-29T03:02:06.310900Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics='acc')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:02:06.313494Z","iopub.execute_input":"2022-11-29T03:02:06.314136Z","iopub.status.idle":"2022-11-29T03:02:06.328748Z","shell.execute_reply.started":"2022-11-29T03:02:06.314097Z","shell.execute_reply":"2022-11-29T03:02:06.327837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist=model.fit(train0, epochs=5, validation_data=val0)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:02:06.331810Z","iopub.execute_input":"2022-11-29T03:02:06.332394Z","iopub.status.idle":"2022-11-29T03:25:32.866124Z","shell.execute_reply.started":"2022-11-29T03:02:06.332357Z","shell.execute_reply":"2022-11-29T03:25:32.865184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T03:26:28.733153Z","iopub.execute_input":"2022-11-29T03:26:28.733521Z","iopub.status.idle":"2022-11-29T03:26:29.512678Z","shell.execute_reply.started":"2022-11-29T03:26:28.733488Z","shell.execute_reply":"2022-11-29T03:26:29.511677Z"},"trusted":true},"execution_count":null,"outputs":[]}]}