{"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 matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport PIL\nimport tensorflow as tf\nfrom keras.layers import Input, Dense, Flatten\n\nfrom tensorflow.keras.models import load_model, Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:10.113316Z","iopub.execute_input":"2022-03-02T05:33:10.113563Z","iopub.status.idle":"2022-03-02T05:33:10.120542Z","shell.execute_reply.started":"2022-03-02T05:33:10.113528Z","shell.execute_reply":"2022-03-02T05:33:10.119360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pre_process_images(filename):\n    img_ = cv.imread(filename)\n    gass_blur = cv.GaussianBlur(img_, (5, 5), 10)\n    thr_imag = cv.threshold(gass_blur, 127, 255, cv.THRESH_BINARY)\n    return thr_imag[1]","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:22.424860Z","iopub.execute_input":"2022-03-02T05:33:22.425133Z","iopub.status.idle":"2022-03-02T05:33:22.429471Z","shell.execute_reply.started":"2022-03-02T05:33:22.425085Z","shell.execute_reply":"2022-03-02T05:33:22.428754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180\ndata_dir_train = '../input/dog-vs-cat-classification/train/train'\ndata_dir_test = '../input/dog-vs-cat-classification/test/test'\n","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:23.890090Z","iopub.execute_input":"2022-03-02T05:33:23.890799Z","iopub.status.idle":"2022-03-02T05:33:23.895154Z","shell.execute_reply.started":"2022-03-02T05:33:23.890763Z","shell.execute_reply":"2022-03-02T05:33:23.894245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1./255,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True)\n\ntest_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:25.594971Z","iopub.execute_input":"2022-03-02T05:33:25.595608Z","iopub.status.idle":"2022-03-02T05:33:25.600700Z","shell.execute_reply.started":"2022-03-02T05:33:25.595569Z","shell.execute_reply":"2022-03-02T05:33:25.599787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set = train_datagen.flow_from_directory(data_dir_train,\n                                                 target_size = (img_height, img_width),\n                                                 batch_size = batch_size,\n                                                 class_mode = 'binary')","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:27.307222Z","iopub.execute_input":"2022-03-02T05:33:27.307519Z","iopub.status.idle":"2022-03-02T05:33:34.004224Z","shell.execute_reply.started":"2022-03-02T05:33:27.307491Z","shell.execute_reply":"2022-03-02T05:33:34.002754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing_set = test_datagen.flow_from_directory('../input/dog-vs-cat-classification/test',\n                                                 target_size = (img_height, img_width)\n                                                 )","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:34.005948Z","iopub.execute_input":"2022-03-02T05:33:34.006203Z","iopub.status.idle":"2022-03-02T05:33:37.169194Z","shell.execute_reply.started":"2022-03-02T05:33:34.006167Z","shell.execute_reply":"2022-03-02T05:33:37.167545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception_model = tf.keras.applications.InceptionV3(input_shape = (img_height, img_width, 3), include_top=False,weights=\"imagenet\")","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:38.668009Z","iopub.execute_input":"2022-03-02T05:33:38.668558Z","iopub.status.idle":"2022-03-02T05:33:43.508701Z","shell.execute_reply.started":"2022-03-02T05:33:38.668522Z","shell.execute_reply":"2022-03-02T05:33:43.507733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in inception_model.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:43.510223Z","iopub.execute_input":"2022-03-02T05:33:43.510484Z","iopub.status.idle":"2022-03-02T05:33:43.529956Z","shell.execute_reply.started":"2022-03-02T05:33:43.510450Z","shell.execute_reply":"2022-03-02T05:33:43.529315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = Flatten()(inception_model.output)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:33:52.833761Z","iopub.execute_input":"2022-03-02T05:33:52.834512Z","iopub.status.idle":"2022-03-02T05:33:52.842709Z","shell.execute_reply.started":"2022-03-02T05:33:52.834456Z","shell.execute_reply":"2022-03-02T05:33:52.841969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"op_layer = Dense(1, activation='sigmoid')(x)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:36:06.106875Z","iopub.execute_input":"2022-03-02T05:36:06.107528Z","iopub.status.idle":"2022-03-02T05:36:06.122763Z","shell.execute_reply.started":"2022-03-02T05:36:06.107482Z","shell.execute_reply":"2022-03-02T05:36:06.122056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incp_model = Model(inputs=inception_model.input, outputs=op_layer)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:36:08.254846Z","iopub.execute_input":"2022-03-02T05:36:08.255133Z","iopub.status.idle":"2022-03-02T05:36:08.279855Z","shell.execute_reply.started":"2022-03-02T05:36:08.255072Z","shell.execute_reply":"2022-03-02T05:36:08.279041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incp_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:36:10.482019Z","iopub.execute_input":"2022-03-02T05:36:10.482694Z","iopub.status.idle":"2022-03-02T05:36:10.613496Z","shell.execute_reply.started":"2022-03-02T05:36:10.482652Z","shell.execute_reply":"2022-03-02T05:36:10.612838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incp_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:36:20.593514Z","iopub.execute_input":"2022-03-02T05:36:20.594179Z","iopub.status.idle":"2022-03-02T05:36:20.609040Z","shell.execute_reply.started":"2022-03-02T05:36:20.594120Z","shell.execute_reply":"2022-03-02T05:36:20.608204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incp_model.fit_generator(training_set, epochs = 10, steps_per_epoch = len(training_set))","metadata":{"execution":{"iopub.status.busy":"2022-03-02T05:36:21.975409Z","iopub.execute_input":"2022-03-02T05:36:21.975940Z","iopub.status.idle":"2022-03-02T06:15:48.668499Z","shell.execute_reply.started":"2022-03-02T05:36:21.975904Z","shell.execute_reply":"2022-03-02T06:15:48.667820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_samples = len(testing_set.filenames)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:16:17.674778Z","iopub.execute_input":"2022-03-02T06:16:17.675452Z","iopub.status.idle":"2022-03-02T06:16:17.678988Z","shell.execute_reply.started":"2022-03-02T06:16:17.675414Z","shell.execute_reply":"2022-03-02T06:16:17.678288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_samples","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:16:23.359274Z","iopub.execute_input":"2022-03-02T06:16:23.359526Z","iopub.status.idle":"2022-03-02T06:16:23.364724Z","shell.execute_reply.started":"2022-03-02T06:16:23.359498Z","shell.execute_reply":"2022-03-02T06:16:23.364018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = incp_model.predict_generator(testing_set,steps = np.ceil(nb_samples/batch_size))","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:17:06.042067Z","iopub.execute_input":"2022-03-02T06:17:06.042456Z","iopub.status.idle":"2022-03-02T06:18:11.447217Z","shell.execute_reply.started":"2022-03-02T06:17:06.042416Z","shell.execute_reply":"2022-03-02T06:18:11.446409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(predict)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:33:11.044838Z","iopub.execute_input":"2022-03-02T06:33:11.046376Z","iopub.status.idle":"2022-03-02T06:33:11.055143Z","shell.execute_reply.started":"2022-03-02T06:33:11.046337Z","shell.execute_reply":"2022-03-02T06:33:11.054283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = incp_model.predict(testing_set,steps = np.ceil(nb_samples/batch_size))","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:32:44.809061Z","iopub.execute_input":"2022-03-02T06:32:44.809362Z","iopub.status.idle":"2022-03-02T06:33:11.036548Z","shell.execute_reply.started":"2022-03-02T06:32:44.809333Z","shell.execute_reply":"2022-03-02T06:33:11.035738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_int = pred.astype('int')","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:34:52.512083Z","iopub.execute_input":"2022-03-02T06:34:52.512746Z","iopub.status.idle":"2022-03-02T06:34:52.516348Z","shell.execute_reply.started":"2022-03-02T06:34:52.512710Z","shell.execute_reply":"2022-03-02T06:34:52.515532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nsubmission = pd.read_csv('../input/dog-vs-cat-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:35:46.876484Z","iopub.execute_input":"2022-03-02T06:35:46.876730Z","iopub.status.idle":"2022-03-02T06:35:46.895774Z","shell.execute_reply.started":"2022-03-02T06:35:46.876702Z","shell.execute_reply":"2022-03-02T06:35:46.895140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:35:54.766547Z","iopub.execute_input":"2022-03-02T06:35:54.766792Z","iopub.status.idle":"2022-03-02T06:35:54.784627Z","shell.execute_reply.started":"2022-03-02T06:35:54.766765Z","shell.execute_reply":"2022-03-02T06:35:54.783974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = pred_int","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:36:23.845071Z","iopub.execute_input":"2022-03-02T06:36:23.845730Z","iopub.status.idle":"2022-03-02T06:36:23.852824Z","shell.execute_reply.started":"2022-03-02T06:36:23.845696Z","shell.execute_reply":"2022-03-02T06:36:23.851939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:36:35.456826Z","iopub.execute_input":"2022-03-02T06:36:35.457485Z","iopub.status.idle":"2022-03-02T06:36:35.469756Z","shell.execute_reply.started":"2022-03-02T06:36:35.457448Z","shell.execute_reply":"2022-03-02T06:36:35.469032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T06:37:28.471817Z","iopub.execute_input":"2022-03-02T06:37:28.472074Z","iopub.status.idle":"2022-03-02T06:37:28.493111Z","shell.execute_reply.started":"2022-03-02T06:37:28.472046Z","shell.execute_reply":"2022-03-02T06:37:28.492413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}