{"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 os\nimport zipfile\nimport random\nimport shutil\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nimport matplotlib.pyplot as plt\nimport pandas as pd \nimport numpy as np\nimport matplotlib.image as mpimg\nimport zipfile\nimport cv2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-05T21:50:50.776286Z","iopub.execute_input":"2022-12-05T21:50:50.776636Z","iopub.status.idle":"2022-12-05T21:50:57.342653Z","shell.execute_reply.started":"2022-12-05T21:50:50.776540Z","shell.execute_reply":"2022-12-05T21:50:57.341263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:50:57.344723Z","iopub.execute_input":"2022-12-05T21:50:57.345393Z","iopub.status.idle":"2022-12-05T21:51:15.741912Z","shell.execute_reply.started":"2022-12-05T21:50:57.345352Z","shell.execute_reply":"2022-12-05T21:51:15.740925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)\nfiles = os.listdir(path)\nfiles[:5]\n","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:15.743242Z","iopub.execute_input":"2022-12-05T21:51:15.743622Z","iopub.status.idle":"2022-12-05T21:51:15.765809Z","shell.execute_reply.started":"2022-12-05T21:51:15.743564Z","shell.execute_reply":"2022-12-05T21:51:15.764786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plooting some images\n# Set up matplotlib fig, and size it to fit 4x4 pics\nnrows = 4\nncols = 4\nfig = plt.gcf()\nfig.set_size_inches(ncols * 4, nrows * 4)\nfor i, img_path in enumerate(files[:12]):\n  # Set up subplot; subplot indices start at 1\n  sp = plt.subplot(nrows, ncols, i + 1)\n  sp.axis('Off') # Don't show axes (or gridlines)\n\n  img = mpimg.imread(os.path.join(path,img_path))\n  plt.imshow(img)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:15.768497Z","iopub.execute_input":"2022-12-05T21:51:15.768927Z","iopub.status.idle":"2022-12-05T21:51:16.699683Z","shell.execute_reply.started":"2022-12-05T21:51:15.768891Z","shell.execute_reply":"2022-12-05T21:51:16.698447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categories = []\nfor filename in files:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:16.700640Z","iopub.execute_input":"2022-12-05T21:51:16.700941Z","iopub.status.idle":"2022-12-05T21:51:16.723122Z","shell.execute_reply.started":"2022-12-05T21:51:16.700913Z","shell.execute_reply":"2022-12-05T21:51:16.722002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(files))\nlen(categories)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:16.724622Z","iopub.execute_input":"2022-12-05T21:51:16.725276Z","iopub.status.idle":"2022-12-05T21:51:16.733476Z","shell.execute_reply.started":"2022-12-05T21:51:16.725237Z","shell.execute_reply":"2022-12-05T21:51:16.732297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\n    'filename': files,\n    'category': categories\n})\ndf['category'] = df['category'].astype('string')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:16.735286Z","iopub.execute_input":"2022-12-05T21:51:16.736121Z","iopub.status.idle":"2022-12-05T21:51:16.776482Z","shell.execute_reply.started":"2022-12-05T21:51:16.736086Z","shell.execute_reply":"2022-12-05T21:51:16.775811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:16.777871Z","iopub.execute_input":"2022-12-05T21:51:16.778488Z","iopub.status.idle":"2022-12-05T21:51:16.799050Z","shell.execute_reply.started":"2022-12-05T21:51:16.778451Z","shell.execute_reply":"2022-12-05T21:51:16.798223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_df, validate_df = train_test_split(df, test_size=0.1)\ntrain_df = train_df.reset_index()\nvalidate_df = validate_df.reset_index()\n\n# validate_df = validate_df.sample(n=100).reset_index() # use for fast testing code purpose\n# train_df = train_df.sample(n=1800).reset_index() # use for fast testing code purpose\n\ntotal_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:16.800479Z","iopub.execute_input":"2022-12-05T21:51:16.805401Z","iopub.status.idle":"2022-12-05T21:51:17.290174Z","shell.execute_reply.started":"2022-12-05T21:51:16.805358Z","shell.execute_reply":"2022-12-05T21:51:17.288979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### data agumentation\n","metadata":{}},{"cell_type":"code","source":"image_size = 150\ninput_shape = (image_size, image_size, 3)\n\nepochs = 5\nbatch_size = 16","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:17.299870Z","iopub.execute_input":"2022-12-05T21:51:17.302780Z","iopub.status.idle":"2022-12-05T21:51:17.311291Z","shell.execute_reply.started":"2022-12-05T21:51:17.302728Z","shell.execute_reply":"2022-12-05T21:51:17.309771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### train Generator","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    width_shift_range=0.2,\n    height_shift_range=0.2\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/working/train\", \n    x_col='filename',\n    y_col='category',\n    class_mode='binary',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:17.312533Z","iopub.execute_input":"2022-12-05T21:51:17.312867Z","iopub.status.idle":"2022-12-05T21:51:17.854689Z","shell.execute_reply.started":"2022-12-05T21:51:17.312833Z","shell.execute_reply":"2022-12-05T21:51:17.853633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### test Generator","metadata":{}},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/working/train\", \n    x_col='filename',\n    y_col='category',\n    class_mode='binary',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:17.856198Z","iopub.execute_input":"2022-12-05T21:51:17.856547Z","iopub.status.idle":"2022-12-05T21:51:17.894497Z","shell.execute_reply.started":"2022-12-05T21:51:17.856512Z","shell.execute_reply":"2022-12-05T21:51:17.893447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create structure\n\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\n\ndef create_model():\n  '''Creates a CNN with 4 convolutional layers'''\n  model = tf.keras.models.Sequential([\n      tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=input_shape),\n      tf.keras.layers.MaxPooling2D(2, 2),\n      tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n      tf.keras.layers.MaxPooling2D(2,2),\n      tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n      tf.keras.layers.MaxPooling2D(2,2),\n      tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n      tf.keras.layers.MaxPooling2D(2,2),\n      tf.keras.layers.Flatten(),\n      tf.keras.layers.Dense(512, activation='relu'),\n      tf.keras.layers.Dense(1, activation='sigmoid')\n  ])\n\n  model.compile(loss='binary_crossentropy',\n                optimizer=RMSprop(learning_rate=1e-4),\n                metrics=['accuracy'])\n  \n  return model","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:17.896197Z","iopub.execute_input":"2022-12-05T21:51:17.896563Z","iopub.status.idle":"2022-12-05T21:51:19.160513Z","shell.execute_reply.started":"2022-12-05T21:51:17.896528Z","shell.execute_reply":"2022-12-05T21:51:19.159514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:19.161852Z","iopub.execute_input":"2022-12-05T21:51:19.162714Z","iopub.status.idle":"2022-12-05T21:51:19.759506Z","shell.execute_reply.started":"2022-12-05T21:51:19.162667Z","shell.execute_reply":"2022-12-05T21:51:19.755366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nmodel=create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:19.763294Z","iopub.execute_input":"2022-12-05T21:51:19.763713Z","iopub.status.idle":"2022-12-05T21:51:23.077198Z","shell.execute_reply.started":"2022-12-05T21:51:19.763662Z","shell.execute_reply":"2022-12-05T21:51:23.076223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nhistory = model.fit_generator(\n    train_generator,\n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T21:51:23.079361Z","iopub.execute_input":"2022-12-05T21:51:23.080525Z","iopub.status.idle":"2022-12-05T22:07:29.784344Z","shell.execute_reply.started":"2022-12-05T21:51:23.080486Z","shell.execute_reply":"2022-12-05T22:07:29.783413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #train the model\n\n# EPOCHS = 15\n\n# # Create a new model\n# model = create_model()\n\n# # Train the model\n# history_with_aug = model.fit(X, y, epochs=EPOCHS, batch_size=32, validation_split=0.2)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:07:29.785922Z","iopub.execute_input":"2022-12-05T22:07:29.786266Z","iopub.status.idle":"2022-12-05T22:07:29.790698Z","shell.execute_reply.started":"2022-12-05T22:07:29.786231Z","shell.execute_reply":"2022-12-05T22:07:29.789579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate_generator(validation_generator, total_validate//batch_size, workers=12)\nprint(\"Test: accuracy = %f  ;  loss = %f \" % (accuracy, loss))","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:09:07.881438Z","iopub.execute_input":"2022-12-05T22:09:07.882140Z","iopub.status.idle":"2022-12-05T22:09:13.559720Z","shell.execute_reply.started":"2022-12-05T22:09:07.882104Z","shell.execute_reply":"2022-12-05T22:09:13.558581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Testing Generator","metadata":{}},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/working/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:09:20.743375Z","iopub.execute_input":"2022-12-05T22:09:20.744369Z","iopub.status.idle":"2022-12-05T22:09:20.758463Z","shell.execute_reply.started":"2022-12-05T22:09:20.744319Z","shell.execute_reply":"2022-12-05T22:09:20.757432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/working/test1\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    batch_size=batch_size,\n    target_size=(image_size, image_size),\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:09:29.156927Z","iopub.execute_input":"2022-12-05T22:09:29.157298Z","iopub.status.idle":"2022-12-05T22:09:29.259126Z","shell.execute_reply.started":"2022-12-05T22:09:29.157267Z","shell.execute_reply":"2022-12-05T22:09:29.258086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))\nthreshold = 0.5\ntest_df['category'] = np.where(predict > threshold, 1,0)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:09:33.407702Z","iopub.execute_input":"2022-12-05T22:09:33.408068Z","iopub.status.idle":"2022-12-05T22:10:07.587261Z","shell.execute_reply.started":"2022-12-05T22:09:33.408037Z","shell.execute_reply":"2022-12-05T22:10:07.586286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test = test_df.sample(n=9).reset_index()\nsample_test.head()\nplt.figure(figsize=(12, 12))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = mpimg.imread(os.path.join('/kaggle/working/test1',filename))\n    plt.imshow(img)\n    plt.subplot(3, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:10:07.589051Z","iopub.execute_input":"2022-12-05T22:10:07.590513Z","iopub.status.idle":"2022-12-05T22:10:09.137745Z","shell.execute_reply.started":"2022-12-05T22:10:07.590474Z","shell.execute_reply":"2022-12-05T22:10:09.136654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission_.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:07:30.085392Z","iopub.status.idle":"2022-12-05T22:07:30.086191Z","shell.execute_reply.started":"2022-12-05T22:07:30.085924Z","shell.execute_reply":"2022-12-05T22:07:30.085952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### VGG16 Model\n","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation,GlobalMaxPooling2D\nfrom keras import applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers\nfrom keras.applications.vgg16 import VGG16\nvggmodel = VGG16(weights='imagenet', include_top=True)\nfrom keras.models import Model\n\nimage_size = 224\ninput_shape = (image_size, image_size, 3)\n\nepochs = 5\nbatch_size = 16\n\npre_trained_model = VGG16(input_shape=input_shape, include_top=False, weights=\"imagenet\")\n    \nfor layer in pre_trained_model.layers[:15]:\n    layer.trainable = False\n\nfor layer in pre_trained_model.layers[15:]:\n    layer.trainable = True\n    \nlast_layer = pre_trained_model.get_layer('block5_pool')\nlast_output = last_layer.output\n    \n# Flatten the output layer to 1 dimension\nx = GlobalMaxPooling2D()(last_output)\n# Add a fully connected layer with 512 hidden units and ReLU activation\nx = Dense(512, activation='relu')(x)\n# Add a dropout rate of 0.5\nx = Dropout(0.5)(x)\n# Add a final sigmoid layer for classification\nx = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = Model(pre_trained_model.input, x)\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer=tf.keras.optimizers.SGD(lr=1e-4, momentum=0.9),\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:15:46.683750Z","iopub.execute_input":"2022-12-05T22:15:46.684109Z","iopub.status.idle":"2022-12-05T22:15:49.044558Z","shell.execute_reply.started":"2022-12-05T22:15:46.684080Z","shell.execute_reply":"2022-12-05T22:15:49.043579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fine-tune the model\nhistory = model.fit_generator(\n    train_generator,\n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:16:15.145439Z","iopub.execute_input":"2022-12-05T22:16:15.146000Z","iopub.status.idle":"2022-12-05T22:32:43.740184Z","shell.execute_reply.started":"2022-12-05T22:16:15.145962Z","shell.execute_reply":"2022-12-05T22:32:43.739199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plot the model results\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-05T22:32:43.742142Z","iopub.execute_input":"2022-12-05T22:32:43.742604Z","iopub.status.idle":"2022-12-05T22:32:44.134351Z","shell.execute_reply.started":"2022-12-05T22:32:43.742550Z","shell.execute_reply":"2022-12-05T22:32:44.133445Z"},"trusted":true},"execution_count":null,"outputs":[]}]}