{"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 zipfile\n\ndata = zipfile.ZipFile('../input/dogs-vs-cats/test1.zip','r')\ndata.extractall()\ndata.close()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-03T13:07:35.176886Z","iopub.execute_input":"2022-09-03T13:07:35.178136Z","iopub.status.idle":"2022-09-03T13:07:41.348896Z","shell.execute_reply.started":"2022-09-03T13:07:35.178085Z","shell.execute_reply":"2022-09-03T13:07:41.34792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\ndata = zipfile.ZipFile('../input/dogs-vs-cats/train.zip','r')\ndata.extractall()\ndata.close()","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:07:41.350854Z","iopub.execute_input":"2022-09-03T13:07:41.351217Z","iopub.status.idle":"2022-09-03T13:07:52.468306Z","shell.execute_reply.started":"2022-09-03T13:07:41.351181Z","shell.execute_reply":"2022-09-03T13:07:52.467211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir('./'))","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:07:29.923845Z","iopub.execute_input":"2022-09-03T13:07:29.92446Z","iopub.status.idle":"2022-09-03T13:07:29.952592Z","shell.execute_reply.started":"2022-09-03T13:07:29.92435Z","shell.execute_reply":"2022-09-03T13:07:29.951592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"address_train = os.listdir('./train')","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:07:59.077002Z","iopub.execute_input":"2022-09-03T13:07:59.07739Z","iopub.status.idle":"2022-09-03T13:07:59.097244Z","shell.execute_reply.started":"2022-09-03T13:07:59.077356Z","shell.execute_reply":"2022-09-03T13:07:59.096266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"address_train[0:10]","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:08:08.834051Z","iopub.execute_input":"2022-09-03T13:08:08.834421Z","iopub.status.idle":"2022-09-03T13:08:08.843693Z","shell.execute_reply.started":"2022-09-03T13:08:08.83439Z","shell.execute_reply":"2022-09-03T13:08:08.842722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cat: 0, dog:1\nlabel = []\nfor i in address_train:\n    if \"cat\" in i:\n        label.append(0)\n    elif \"dog\" in i:\n        label.append(1)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:08:52.330601Z","iopub.execute_input":"2022-09-03T13:08:52.330971Z","iopub.status.idle":"2022-09-03T13:08:52.343148Z","shell.execute_reply.started":"2022-09-03T13:08:52.330939Z","shell.execute_reply":"2022-09-03T13:08:52.34208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(label)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:08:52.888936Z","iopub.execute_input":"2022-09-03T13:08:52.88958Z","iopub.status.idle":"2022-09-03T13:08:52.900449Z","shell.execute_reply.started":"2022-09-03T13:08:52.889534Z","shell.execute_reply":"2022-09-03T13:08:52.898333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = './train/'\n\nif not os.path.exists(path+'1'):      \n    os.makedirs(path+'1')\n    \nif not os.path.exists(path+'0'):      \n    os.makedirs(path+'0')","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:13:52.877452Z","iopub.execute_input":"2022-09-03T13:13:52.878089Z","iopub.status.idle":"2022-09-03T13:13:52.883729Z","shell.execute_reply.started":"2022-09-03T13:13:52.878048Z","shell.execute_reply":"2022-09-03T13:13:52.882657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\nfor i in range(len(address_train)):\n\n    shutil.move(path+str(address_train[i]),'./train/'+str(label[i])+'/'+address_train[i])","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:15:22.608952Z","iopub.execute_input":"2022-09-03T13:15:22.609306Z","iopub.status.idle":"2022-09-03T13:15:23.307257Z","shell.execute_reply.started":"2022-09-03T13:15:22.609276Z","shell.execute_reply":"2022-09-03T13:15:23.306101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = os.listdir('./train/'+str(0))","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:15:55.739225Z","iopub.execute_input":"2022-09-03T13:15:55.739604Z","iopub.status.idle":"2022-09-03T13:15:55.75116Z","shell.execute_reply.started":"2022-09-03T13:15:55.739573Z","shell.execute_reply":"2022-09-03T13:15:55.750221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nim = plt.imread('./train/0/cat.2430.jpg')\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:16:20.831428Z","iopub.execute_input":"2022-09-03T13:16:20.832043Z","iopub.status.idle":"2022-09-03T13:16:21.180499Z","shell.execute_reply.started":"2022-09-03T13:16:20.831998Z","shell.execute_reply":"2022-09-03T13:16:21.179477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t[0:10]","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:16:03.049789Z","iopub.execute_input":"2022-09-03T13:16:03.050436Z","iopub.status.idle":"2022-09-03T13:16:03.056966Z","shell.execute_reply.started":"2022-09-03T13:16:03.050402Z","shell.execute_reply":"2022-09-03T13:16:03.055935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  './train/',\n  validation_split=0.2,\n  labels = label,\n  label_mode='int',\n  subset=\"training\",\n  seed=123,\n  image_size=(200, 200),\n  batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:16:36.145345Z","iopub.execute_input":"2022-09-03T13:16:36.146384Z","iopub.status.idle":"2022-09-03T13:16:45.579112Z","shell.execute_reply.started":"2022-09-03T13:16:36.146347Z","shell.execute_reply":"2022-09-03T13:16:45.578051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''for i in train_ds:\n    print(i)'''","metadata":{"execution":{"iopub.status.busy":"2022-09-03T06:46:43.317692Z","iopub.execute_input":"2022-09-03T06:46:43.318059Z","iopub.status.idle":"2022-09-03T06:46:43.324911Z","shell.execute_reply.started":"2022-09-03T06:46:43.318028Z","shell.execute_reply":"2022-09-03T06:46:43.323709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPooling2D, Dropout\nfrom tensorflow.keras.models import Sequential\nmodel = Sequential()\nmodel.add(Conv2D(20,(3,3),activation='relu',input_shape = (200,200,3)))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(40,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(80,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Flatten())\nmodel.add(Dense(250,activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(150,activation = 'relu'))\nmodel.add(Dense(50,activation = 'relu'))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(1,activation = 'sigmoid'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:17:01.994558Z","iopub.execute_input":"2022-09-03T13:17:01.995126Z","iopub.status.idle":"2022-09-03T13:17:02.116284Z","shell.execute_reply.started":"2022-09-03T13:17:01.995092Z","shell.execute_reply":"2022-09-03T13:17:02.115219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nmodel.fit(train_ds,epochs=20)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:17:11.658177Z","iopub.execute_input":"2022-09-03T13:17:11.658556Z","iopub.status.idle":"2022-09-03T13:28:15.80899Z","shell.execute_reply.started":"2022-09-03T13:17:11.658504Z","shell.execute_reply":"2022-09-03T13:28:15.807834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\naddress_test = os.listdir('./test1')","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:44:17.491873Z","iopub.execute_input":"2022-09-03T13:44:17.492607Z","iopub.status.idle":"2022-09-03T13:44:17.505482Z","shell.execute_reply.started":"2022-09-03T13:44:17.492569Z","shell.execute_reply":"2022-09-03T13:44:17.504555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"address_test[0:10]","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:44:17.911766Z","iopub.execute_input":"2022-09-03T13:44:17.912072Z","iopub.status.idle":"2022-09-03T13:44:17.919508Z","shell.execute_reply.started":"2022-09-03T13:44:17.912046Z","shell.execute_reply":"2022-09-03T13:44:17.918513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimage = cv2.imread('./test1/'+address_test[2])\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:44:35.563902Z","iopub.execute_input":"2022-09-03T13:44:35.564537Z","iopub.status.idle":"2022-09-03T13:44:36.015884Z","shell.execute_reply.started":"2022-09-03T13:44:35.564484Z","shell.execute_reply":"2022-09-03T13:44:36.014605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.input_shape","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:45:48.840691Z","iopub.execute_input":"2022-09-03T13:45:48.841317Z","iopub.status.idle":"2022-09-03T13:45:48.847968Z","shell.execute_reply.started":"2022-09-03T13:45:48.841279Z","shell.execute_reply":"2022-09-03T13:45:48.846902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = cv2.resize(image,(200,200))\nimport numpy as np\nim = np.expand_dims(im, axis=0)\nim.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:46:07.721821Z","iopub.execute_input":"2022-09-03T13:46:07.722192Z","iopub.status.idle":"2022-09-03T13:46:07.736798Z","shell.execute_reply.started":"2022-09-03T13:46:07.72216Z","shell.execute_reply":"2022-09-03T13:46:07.735287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(im)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T13:46:24.202087Z","iopub.execute_input":"2022-09-03T13:46:24.20244Z","iopub.status.idle":"2022-09-03T13:46:24.38171Z","shell.execute_reply.started":"2022-09-03T13:46:24.202409Z","shell.execute_reply":"2022-09-03T13:46:24.380739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model, to_file='convnet.png', show_shapes=True,show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T06:27:32.171593Z","iopub.execute_input":"2022-09-03T06:27:32.171994Z","iopub.status.idle":"2022-09-03T06:27:33.287979Z","shell.execute_reply.started":"2022-09-03T06:27:32.171962Z","shell.execute_reply":"2022-09-03T06:27:33.286812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install visualkeras\nimport visualkeras\nvisualkeras.layered_view(model, legend=True) ","metadata":{"execution":{"iopub.status.busy":"2022-09-03T06:27:54.796497Z","iopub.execute_input":"2022-09-03T06:27:54.796901Z","iopub.status.idle":"2022-09-03T06:28:07.003496Z","shell.execute_reply.started":"2022-09-03T06:27:54.796863Z","shell.execute_reply":"2022-09-03T06:28:07.002374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_generator = ImageDataGenerator(rescale=1/255, validation_split=0.2)    \n\ntrain_dataset = image_generator.flow_from_directory(batch_size=32,\n                                                 directory='full_dataset',\n                                                 shuffle=True,\n                                                 target_size=(280, 280), \n                                                 subset=\"training\",\n                                                 class_mode='categorical')\n\nvalidation_dataset = image_generator.flow_from_directory(batch_size=32,\n                                                 directory='full_dataset',\n                                                 shuffle=True,\n                                                 target_size=(280, 280), \n                                                 subset=\"validation\",\n                                                 class_mode='categorical')","metadata":{},"execution_count":null,"outputs":[]}]}