{"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":"#imports\nimport numpy as np\nimport pandas as pd \nfrom tensorflow.keras.utils import load_img\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-12T10:24:01.821616Z","iopub.execute_input":"2023-05-12T10:24:01.822084Z","iopub.status.idle":"2023-05-12T10:24:09.991316Z","shell.execute_reply.started":"2023-05-12T10:24:01.822049Z","shell.execute_reply":"2023-05-12T10:24:09.990130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unzips training and testing\n#import zipfile\n#with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\", 'r') as zippy:\n#    zippy.extractall(\"../working\")\n#with zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\", 'r') as zippy:\n#    zippy.extractall(\"../working\")","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:09.993999Z","iopub.execute_input":"2023-05-12T10:24:09.995393Z","iopub.status.idle":"2023-05-12T10:24:27.795493Z","shell.execute_reply.started":"2023-05-12T10:24:09.995357Z","shell.execute_reply":"2023-05-12T10:24:27.794316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#constants for images\nwidth=128\nheight=128\nsize=(width, height)\nchannels=3","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:27.796866Z","iopub.execute_input":"2023-05-12T10:24:27.797241Z","iopub.status.idle":"2023-05-12T10:24:27.803226Z","shell.execute_reply.started":"2023-05-12T10:24:27.797212Z","shell.execute_reply":"2023-05-12T10:24:27.802150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sort labeled data\n#filenames = os.listdir(\"../working/train\")\nfilenames = os.listdir(\"../input/train-mini\")\nlabels = []\nfor filename in filenames:\n    label = filename.split('.')[0]\n    if label == 'dog':\n        labels.append(1)\n    else:\n        labels.append(0)\n\n#create dataframe of images (filename and class label)\ndf = pd.DataFrame({\n    'filename': filenames,\n    'label': labels\n})","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:27.804907Z","iopub.execute_input":"2023-05-12T10:24:27.805585Z","iopub.status.idle":"2023-05-12T10:24:27.866140Z","shell.execute_reply.started":"2023-05-12T10:24:27.805542Z","shell.execute_reply":"2023-05-12T10:24:27.865139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create model\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\n\nmodel = Sequential() #model\n\n#convolutional layer, 32 output filters, strides is 3 by 3\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(width, height, channels)))\nmodel.add(BatchNormalization())\n#pooling size is 2 by 2\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n#randomly sets to zero, prevents overfitting\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten()) #flatten layer\nmodel.add(Dense(512, activation='relu')) #regular NN layer\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:27.869325Z","iopub.execute_input":"2023-05-12T10:24:27.870115Z","iopub.status.idle":"2023-05-12T10:24:28.359231Z","shell.execute_reply.started":"2023-05-12T10:24:27.870081Z","shell.execute_reply":"2023-05-12T10:24:28.357989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.360688Z","iopub.execute_input":"2023-05-12T10:24:28.361073Z","iopub.status.idle":"2023-05-12T10:24:28.366178Z","shell.execute_reply.started":"2023-05-12T10:24:28.361042Z","shell.execute_reply":"2023-05-12T10:24:28.365145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#stop training after 10 epochs with no movement\nearlystop = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.367599Z","iopub.execute_input":"2023-05-12T10:24:28.367932Z","iopub.status.idle":"2023-05-12T10:24:28.376442Z","shell.execute_reply.started":"2023-05-12T10:24:28.367903Z","shell.execute_reply":"2023-05-12T10:24:28.375577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reduce learning rate if the learning rate stops moving\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.5, min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.377501Z","iopub.execute_input":"2023-05-12T10:24:28.377826Z","iopub.status.idle":"2023-05-12T10:24:28.389029Z","shell.execute_reply.started":"2023-05-12T10:24:28.377799Z","shell.execute_reply":"2023-05-12T10:24:28.388013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.390622Z","iopub.execute_input":"2023-05-12T10:24:28.390982Z","iopub.status.idle":"2023-05-12T10:24:28.400439Z","shell.execute_reply.started":"2023-05-12T10:24:28.390932Z","shell.execute_reply":"2023-05-12T10:24:28.399589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"label\"] = df[\"label\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.401638Z","iopub.execute_input":"2023-05-12T10:24:28.402012Z","iopub.status.idle":"2023-05-12T10:24:28.420132Z","shell.execute_reply.started":"2023-05-12T10:24:28.401979Z","shell.execute_reply":"2023-05-12T10:24:28.419010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42) #split into train-test\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.421932Z","iopub.execute_input":"2023-05-12T10:24:28.423098Z","iopub.status.idle":"2023-05-12T10:24:28.439856Z","shell.execute_reply.started":"2023-05-12T10:24:28.423056Z","shell.execute_reply":"2023-05-12T10:24:28.438751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\nbatch_size=15","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.441341Z","iopub.execute_input":"2023-05-12T10:24:28.441676Z","iopub.status.idle":"2023-05-12T10:24:28.446239Z","shell.execute_reply.started":"2023-05-12T10:24:28.441649Z","shell.execute_reply":"2023-05-12T10:24:28.445377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n#   \"../working/train/\", \n    \"../input/train-mini/\", \n    x_col='filename',\n    y_col='label',\n    target_size=size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.447666Z","iopub.execute_input":"2023-05-12T10:24:28.448092Z","iopub.status.idle":"2023-05-12T10:24:28.737967Z","shell.execute_reply.started":"2023-05-12T10:24:28.448063Z","shell.execute_reply":"2023-05-12T10:24:28.736840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n#    \"../working/train/\", \n    \"../input/train-mini/\",\n    x_col='filename',\n    y_col='label',\n    target_size=size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.743934Z","iopub.execute_input":"2023-05-12T10:24:28.744841Z","iopub.status.idle":"2023-05-12T10:24:28.828752Z","shell.execute_reply.started":"2023-05-12T10:24:28.744804Z","shell.execute_reply":"2023-05-12T10:24:28.827548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop=True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n#    \"../working/train/\", \n    \"../input/train-mini/\",\n    x_col='filename',\n    y_col='label',\n    target_size=size,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.830524Z","iopub.execute_input":"2023-05-12T10:24:28.831257Z","iopub.status.idle":"2023-05-12T10:24:28.843577Z","shell.execute_reply.started":"2023-05-12T10:24:28.831214Z","shell.execute_reply":"2023-05-12T10:24:28.842415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fit model\nepochs= 50\nhistory = model.fit(\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,\n    callbacks=callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T10:24:28.845177Z","iopub.execute_input":"2023-05-12T10:24:28.845529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plotting\nfig, (ax2) = plt.subplots(1, 1, figsize=(12, 12))\n\nax2.plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_accuracy'], color='r',label=\"Test accuracy\")\nax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_filenames = os.listdir(\"../working/test1\")\ntest_filenames = os.listdir(\"../input/test-mini\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"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#    \"../working/test1/\",\n    \"../input/test-mini/\",\n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=size,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test_generator, steps=np.ceil(nb_samples/batch_size))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = np.argmax(predict, axis=-1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['label'] = test_df['label'].replace(label_map)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = test_df['label'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test = test_df.head(18)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    label = row['label']\n    #img = load_img(\"../working/test1/\"+filename, target_size=size)\n    img = load_img(\"../input/test-mini/\"+filename, target_size=size)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(label) + ')' )\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_df)\nsubmission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['label']\nsubmission_df.drop(['filename', 'label'], axis=1, inplace=True)\nsubmission_df.to_csv('output.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}