{"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\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport random \nimport os \nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom sklearn.model_selection import train_test_split\nprint(os.listdir('../input'))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-03T02:15:12.545383Z","iopub.execute_input":"2021-09-03T02:15:12.545869Z","iopub.status.idle":"2021-09-03T02:15:12.554263Z","shell.execute_reply.started":"2021-09-03T02:15:12.545824Z","shell.execute_reply":"2021-09-03T02:15:12.553169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip '../input/dogs-vs-cats/train.zip' \n! unzip '../input/dogs-vs-cats/test1.zip'","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:12.565117Z","iopub.execute_input":"2021-09-03T02:15:12.565888Z","iopub.status.idle":"2021-09-03T02:15:29.851470Z","shell.execute_reply.started":"2021-09-03T02:15:12.565835Z","shell.execute_reply":"2021-09-03T02:15:29.850550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"width = 128\nheight = 128\nsize = (width, height)\nchannels = 3\nfast_run = False","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:29.854811Z","iopub.execute_input":"2021-09-03T02:15:29.855082Z","iopub.status.idle":"2021-09-03T02:15:29.859079Z","shell.execute_reply.started":"2021-09-03T02:15:29.855052Z","shell.execute_reply":"2021-09-03T02:15:29.858339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames  = os.listdir(\"./train\")\ncatigories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        catigories.append(1)\n    else:\n        catigories.append(0)\n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': catigories\n})","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:29.860892Z","iopub.execute_input":"2021-09-03T02:15:29.861414Z","iopub.status.idle":"2021-09-03T02:15:30.752039Z","shell.execute_reply.started":"2021-09-03T02:15:29.861380Z","shell.execute_reply":"2021-09-03T02:15:30.751154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(20)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:30.753644Z","iopub.execute_input":"2021-09-03T02:15:30.753985Z","iopub.status.idle":"2021-09-03T02:15:30.780408Z","shell.execute_reply.started":"2021-09-03T02:15:30.753948Z","shell.execute_reply":"2021-09-03T02:15:30.779667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(filenames)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:30.781631Z","iopub.execute_input":"2021-09-03T02:15:30.781971Z","iopub.status.idle":"2021-09-03T02:15:30.787456Z","shell.execute_reply.started":"2021-09-03T02:15:30.781935Z","shell.execute_reply":"2021-09-03T02:15:30.786522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:30.788970Z","iopub.execute_input":"2021-09-03T02:15:30.789637Z","iopub.status.idle":"2021-09-03T02:15:31.110748Z","shell.execute_reply.started":"2021-09-03T02:15:30.789596Z","shell.execute_reply":"2021-09-03T02:15:31.109820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = random.choice(filenames)\nimage = load_img('./train/'+sample)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.112051Z","iopub.execute_input":"2021-09-03T02:15:31.112412Z","iopub.status.idle":"2021-09-03T02:15:31.313839Z","shell.execute_reply.started":"2021-09-03T02:15:31.112376Z","shell.execute_reply":"2021-09-03T02:15:31.313133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([ \n    Conv2D(32, (3, 3), activation='relu', input_shape=(width, height, channels)),\n    BatchNormalization(),\n    MaxPool2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    Conv2D(64, (3, 3), activation='relu'),\n    BatchNormalization(),\n    MaxPool2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    Conv2D(128, (3, 3), activation='relu'),\n    BatchNormalization(),\n    MaxPool2D(pool_size=(2, 2)), \n    Dropout(0.25),\n    \n    Flatten(),\n    Dense(512, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.5),\n    Dense(2, activation='softmax')\n])\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.315741Z","iopub.execute_input":"2021-09-03T02:15:31.316174Z","iopub.status.idle":"2021-09-03T02:15:31.423485Z","shell.execute_reply.started":"2021-09-03T02:15:31.316136Z","shell.execute_reply":"2021-09-03T02:15:31.422765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.424986Z","iopub.execute_input":"2021-09-03T02:15:31.425349Z","iopub.status.idle":"2021-09-03T02:15:31.439701Z","shell.execute_reply.started":"2021-09-03T02:15:31.425314Z","shell.execute_reply":"2021-09-03T02:15:31.436613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',\n                                            patience=2,\n                                            verbose=1,\n                                            factor=0.5,\n                                            min_lr=0.00001)\n\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.441140Z","iopub.execute_input":"2021-09-03T02:15:31.441494Z","iopub.status.idle":"2021-09-03T02:15:31.446339Z","shell.execute_reply.started":"2021-09-03T02:15:31.441459Z","shell.execute_reply":"2021-09-03T02:15:31.445290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'] = df['category'].replace({0: 'cat', 1: 'dog'})","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.447809Z","iopub.execute_input":"2021-09-03T02:15:31.448187Z","iopub.status.idle":"2021-09-03T02:15:31.457491Z","shell.execute_reply.started":"2021-09-03T02:15:31.448151Z","shell.execute_reply":"2021-09-03T02:15:31.456701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.2, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.459711Z","iopub.execute_input":"2021-09-03T02:15:31.460367Z","iopub.status.idle":"2021-09-03T02:15:31.469442Z","shell.execute_reply.started":"2021-09-03T02:15:31.460331Z","shell.execute_reply":"2021-09-03T02:15:31.468609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" validate_df['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.470653Z","iopub.execute_input":"2021-09-03T02:15:31.471296Z","iopub.status.idle":"2021-09-03T02:15:31.480183Z","shell.execute_reply.started":"2021-09-03T02:15:31.471247Z","shell.execute_reply":"2021-09-03T02:15:31.479175Z"},"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":"2021-09-03T02:15:31.481637Z","iopub.execute_input":"2021-09-03T02:15:31.482007Z","iopub.status.idle":"2021-09-03T02:15:31.486585Z","shell.execute_reply.started":"2021-09-03T02:15:31.481972Z","shell.execute_reply":"2021-09-03T02:15:31.485479Z"},"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    './train/',\n    x_col='filename',\n    y_col='category',\n    target_size=size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.487997Z","iopub.execute_input":"2021-09-03T02:15:31.488365Z","iopub.status.idle":"2021-09-03T02:15:31.684937Z","shell.execute_reply.started":"2021-09-03T02:15:31.488333Z","shell.execute_reply":"2021-09-03T02:15:31.683763Z"},"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    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.686224Z","iopub.execute_input":"2021-09-03T02:15:31.686555Z","iopub.status.idle":"2021-09-03T02:15:31.739735Z","shell.execute_reply.started":"2021-09-03T02:15:31.686523Z","shell.execute_reply":"2021-09-03T02:15:31.738833Z"},"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    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=size,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.740946Z","iopub.execute_input":"2021-09-03T02:15:31.741783Z","iopub.status.idle":"2021-09-03T02:15:31.757091Z","shell.execute_reply.started":"2021-09-03T02:15:31.741582Z","shell.execute_reply":"2021-09-03T02:15:31.755986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:31.758389Z","iopub.execute_input":"2021-09-03T02:15:31.758855Z","iopub.status.idle":"2021-09-03T02:15:33.462026Z","shell.execute_reply.started":"2021-09-03T02:15:31.758810Z","shell.execute_reply":"2021-09-03T02:15:33.461309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# epochs=3 if fast_run else 50\nepochs=1\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)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-03T02:15:33.463208Z","iopub.execute_input":"2021-09-03T02:15:33.463664Z","iopub.status.idle":"2021-09-03T03:43:43.225951Z","shell.execute_reply.started":"2021-09-03T02:15:33.463629Z","shell.execute_reply":"2021-09-03T03:43:43.225120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-09-03T03:55:46.919008Z","iopub.execute_input":"2021-09-03T03:55:46.919369Z","iopub.status.idle":"2021-09-03T03:55:47.048853Z","shell.execute_reply.started":"2021-09-03T03:55:46.919331Z","shell.execute_reply":"2021-09-03T03:55:47.048028Z"},"trusted":true},"execution_count":null,"outputs":[]}]}