{"cells":[{"metadata":{"pycharm":{"name":"#%% md\n"}},"cell_type":"markdown","source":"### Deyanira Taupier\n### AD 470 - May 14, 2020\n### Assignment: Presentation on a kernel \n###### Note: The following is a mixture of my notes on the kernel, explaining for presentation, and the author's code.","execution_count":null},{"metadata":{"pycharm":{"name":"#%%\n","is_executing":false},"trusted":true},"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"pycharm":{"name":"#%% md\n"}},"cell_type":"markdown","source":"## The Playground Prediction Competition\n#### Description\n    Who's a good dog? Who likes ear scratches? Well, it seems those fancy deep neural networks don't have all the answers. However, maybe they can answer that ubiquitous question we all ask when meeting a four-legged stranger: what kind of good pup is that?\n    In this playground competition, you are provided a strictly canine subset of ImageNet in order to practice fine-grained image categorization. How well you can tell your Norfolk Terriers from your Norwich Terriers? With 120 breeds of dogs and a limited number training images per class, you might find the problem more, err, ruff than you anticipated.\n#### Acknowledgments\n    We extend our gratitude to the creators of the Stanford Dogs Dataset for making this competition possible: Aditya Khosla, Nityananda Jayadevaprakash, Bangpeng Yao, and Fei-Fei Li.\n\n\n# Dog Breed Identification\n## Determine the breed of a dog in an image\nLast Updated: 3 years ago\n\n###  About this Competition\nYou are provided with a training set and a test set of images of dogs. Each image has a filename that is its unique id. The dataset comprises 120 breeds of dogs. The goal of the competition is to create a classifier capable of determining a dog's breed from a photo. \n\n#### The list of breeds is as follows:\n    affenpinscher\n    afghan_hound\n    african_hunting_dog\n    airedale\n    american_staffordshire_terrier\n    appenzeller\n    australian_terrier\n    basenji\n    basset\n    beagle\n    bedlington_terrier\n    bernese_mountain_dog\n    black-and-tan_coonhound\n    blenheim_spaniel\n    bloodhound\n    bluetick\n    border_collie\n    border_terrier\n    borzoi\n    boston_bull\n    bouvier_des_flandres\n    boxer\n    brabancon_griffon\n    briard\n    brittany_spaniel\n    bull_mastiff\n    cairn\n    cardigan\n    chesapeake_bay_retriever\n    chihuahua\n    chow\n    clumber\n    cocker_spaniel\n    collie\n    curly-coated_retriever\n    dandie_dinmont\n    dhole\n    dingo\n    doberman\n    english_foxhound\n    english_setter\n    english_springer\n    entlebucher\n    eskimo_dog\n    flat-coated_retriever\n    french_bulldog\n    german_shepherd\n    german_short-haired_pointer\n    giant_schnauzer\n    golden_retriever\n    gordon_setter\n    great_dane\n    great_pyrenees\n    greater_swiss_mountain_dog\n    groenendael\n    ibizan_hound\n    irish_setter\n    irish_terrier\n    irish_water_spaniel\n    irish_wolfhound\n    italian_greyhound\n    japanese_spaniel\n    keeshond\n    kelpie\n    kerry_blue_terrier\n    komondor\n    kuvasz\n    labrador_retriever\n    lakeland_terrier\n    leonberg\n    lhasa\n    malamute\n    malinois\n    maltese_dog\n    mexican_hairless\n    miniature_pinscher\n    miniature_poodle\n    miniature_schnauzer\n    newfoundland\n    norfolk_terrier\n    norwegian_elkhound\n    norwich_terrier\n    old_english_sheepdog\n    otterhound\n    papillon\n    pekinese\n    pembroke\n    pomeranian\n    pug\n    redbone\n    rhodesian_ridgeback\n    rottweiler\n    saint_bernard\n    saluki\n    samoyed\n    schipperke\n    scotch_terrier\n    scottish_deerhound\n    sealyham_terrier\n    shetland_sheepdog\n    shih-tzu\n    siberian_husky\n    silky_terrier\n    soft-coated_wheaten_terrier\n    staffordshire_bullterrier\n    standard_poodle\n    standard_schnauzer\n    sussex_spaniel\n    tibetan_mastiff\n    tibetan_terrier\n    toy_poodle\n    toy_terrier\n    vizsla\n    walker_hound\n    weimaraner\n    welsh_springer_spaniel\n    west_highland_white_terrier\n    whippet\n    wire-haired_fox_terrier\n    yorkshire_terrier\n\n#### File descriptions\n    train.zip - the training set, you are provided the breed for these dogs\n    test.zip - the test set, you must predict the probability of each breed for each image\n    sample_submission.csv - a sample submission file in the correct format\n    labels.csv - the breeds for the images in the train set\n","execution_count":null},{"metadata":{"pycharm":{"name":"#%% md\n"}},"cell_type":"markdown","source":"## Kernel title: Pupper Keras CNN\nThe author lists the folders in our `input` directory to see what we are working with.\n\nThere is `dog-breed-identification` folder, within we have `labels.csv` and `sample_submission.csv`.\n\nThe author then uses `pandas` package for `.read_csv()` to read in the labels as training data and samples as testing data.\n\nThe head of each dataframe is then displayed to become familiar. \n\nAfterwards, the author is able to clean the data and create a model for training purposes.\n\nFinally, the predictions are made! Now able to be visualized in graphs and displayed, then saved to output `.csv`/`.h5` files.\n\nThis notebook has some good scripts in it for a simple approach to the problem given of 'small amount of data'. \n    \n                                                                                  [as noted in competition description]\n\nAble to recognize the flow of data, this proved a bit similar to what we have been doing in class.\n\nI also believe this is good example at attempting to solve a modern issue of non-human image data analysis. ","execution_count":null},{"metadata":{"pycharm":{"name":"#%%\n","is_executing":false},"trusted":true},"cell_type":"code","source":"# import packages\n\nimport numpy as np\nimport pandas as pd\n%matplotlib inline\nimport matplotlib.pyplot as plt\n\nimport tensorflow.keras as keras\nfrom keras import regularizers\nfrom keras.applications.resnet50 import ResNet50, preprocess_input\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.metrics import categorical_accuracy, top_k_categorical_accuracy, categorical_crossentropy\nimport os\nfrom tqdm import tqdm\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nimport cv2\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"pycharm":{"name":"#%% md\n"}},"cell_type":"markdown","source":"## Importing the Data","execution_count":null},{"metadata":{"pycharm":{"name":"#%% \n","is_executing":false},"trusted":true},"cell_type":"code","source":"# directory of input data\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9918f2c8d3556e75cc94cc41fc4f3323bea3c8b3","pycharm":{"is_executing":false}},"cell_type":"code","source":"# grab the train and test data into dataframes\ndf_train = pd.read_csv('../input/dog-breed-identification/labels.csv') \ndf_test = pd.read_csv('../input/dog-breed-identification/sample_submission.csv') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26ca1f0a72c44ec28803b111778bfee69bc02998","pycharm":{"is_executing":false}},"cell_type":"code","source":"print('labels : train dataframe')\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"063c723146ad83b0f5b0ea8e376b89129c95ffc4","pycharm":{"is_executing":false}},"cell_type":"code","source":"print('samples : test dataframe')\ndf_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0293ffd5ee7ca8717f134bfc654623ae940b1bc","pycharm":{"is_executing":false}},"cell_type":"code","source":"# the breed is what we are after for each dog sample\nlabels = df_train['breed']\n# but we need to get dummy variables for the breed columns\none_hot = pd.get_dummies(labels, sparse = True)\nprint('get dummies for breed data')\none_hot","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2621ce3a02d13f4e651222233648bd8a814059c","pycharm":{"is_executing":false}},"cell_type":"code","source":"print('one hot encode the labels into numpy array')\none_hot_labels = np.asarray(one_hot)\none_hot_labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb65dd8cd12273701922e56f4d0e249cad650cfc","pycharm":{"is_executing":false}},"cell_type":"code","source":"# the images will be resized to 64x64\nim_resize = 64\nprint('sample the dog images after resize efforts')\n#visualize a dogger\ndogger1 = df_train['id'][0]\ndogger2 = df_train['id'][1]\ndogger3 = df_train['id'][2]\ndogger4 = df_train['id'][3]\npupper1 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger1))\npupper2 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger2))\npupper3 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger3), cv2.IMREAD_GRAYSCALE)\npupper4 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger4), cv2.IMREAD_GRAYSCALE)\npupper4 = cv2.resize(pupper4, (im_resize, im_resize))\nf, axarr = plt.subplots(2,2)\naxarr[0,0].imshow(pupper1)\naxarr[0,1].imshow(pupper2)\naxarr[1,0].imshow(pupper3,cmap=\"gray\", vmin=0, vmax=255)\naxarr[1,1].imshow(pupper4,cmap=\"gray\", vmin=0, vmax=255)\nplt.xticks([])\nplt.yticks([])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"139b0e9c9856c5158b974555008ddf152b13cbbe","pycharm":{"is_executing":false}},"cell_type":"code","source":"im_size = pupper1.shape\nprint('Image original shape', im_size)\nprint('Image resized shape', cv2.resize(pupper1, (im_resize, im_resize)).shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50c66c7abdeb4478ac52e1013b7b5867ba243fa3","pycharm":{"is_executing":false}},"cell_type":"code","source":"x_train = []\ny_train = []\nx_test = []\nprint('arrays to hold x/y/train/test data initialized')","execution_count":null,"outputs":[]},{"metadata":{"pycharm":{"name":"#%% md\n"}},"cell_type":"markdown","source":"## Cleaning the Data","execution_count":null},{"metadata":{"trusted":true,"_uuid":"3be7e20410a2b0374be73e38a81c8aee672dbaa9","pycharm":{"is_executing":false}},"cell_type":"code","source":"i = 0 \nfor f, breed in tqdm(df_train.values):\n    img = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(f))\n    img_resized = cv2.resize(img, (im_resize, im_resize))\n    x_train.append(img_resized)\n    label = one_hot_labels[i]\n    y_train.append(label)\n    i += 1\nprint('\\nImages split into training sets, resized and one hot encoding labels appended.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d0b4ac7910368aa1806e7544f235da733e85ebc","pycharm":{"is_executing":false}},"cell_type":"code","source":"# no need for the big dataframe anymore\ndel df_train\nprint('df_train used; cleaned up.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8372e0ee527f043df2ec267845329cbf538d37ce","pycharm":{"is_executing":false}},"cell_type":"code","source":"for f in tqdm(df_test['id'].values):\n    img = cv2.imread('../input/dog-breed-identification/test/{}.jpg'.format(f))\n    img_resized = cv2.resize(img, (im_resize, im_resize))\n    x_test.append(img_resized)\nprint('\\nTest data read and resized into x_test[].')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9f86326b40aa50c5d4d196e6fac15f8eb94c890","pycharm":{"is_executing":false}},"cell_type":"code","source":"num_class = 120 #static ftw\nprint('Number of classes: ', num_class)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e9078701b87ee09b7b3a235bc9262dc3ab167df","pycharm":{"is_executing":false}},"cell_type":"code","source":"X_train, X_valid, Y_train, Y_valid = train_test_split(x_train, y_train, shuffle=True,  test_size=0.1)\nprint('Split into new values with train_test_split()')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f1464f0497cbf34d7a756d3133d59e2d339af19","pycharm":{"is_executing":false}},"cell_type":"code","source":"del x_train, y_train\nprint('Initial values used; cleaned up.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ff03bce73be33ab8d4697989e843da9e9135089","pycharm":{"is_executing":false}},"cell_type":"code","source":"# author defines function to seek top 3 value\ndef top_3_accuracy(y_true, y_pred):\n    return top_k_categorical_accuracy(y_true, y_pred, k=3)","execution_count":null,"outputs":[]},{"metadata":{"pycharm":{"name":"#%% md\n"}},"cell_type":"markdown","source":"## Modeling the Data","execution_count":null},{"metadata":{"trusted":true,"_uuid":"c1045af204c54069db458aa163255659010882fb","pycharm":{"is_executing":false}},"cell_type":"code","source":"print('ImageDataGenerator being used to fit X_train.')\n\ndatagen = ImageDataGenerator(width_shift_range=0.2,\n                            height_shift_range=0.2,\n                            zoom_range=0.2,\n                            rotation_range=30,\n                            vertical_flip=False,\n                            horizontal_flip=True)\n\n\ndatagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41f5dfae4bbfd648400f89d878c5c37752f0136e","pycharm":{"is_executing":false}},"cell_type":"code","source":"print('ResNet50 will load the `resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5` file included as pretrained model.\\n')\n\nbase_model = ResNet50(weights=\"../input/keras-pretrained-models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5\",include_top=False, input_shape=(im_resize, im_resize, 3))\n\nx = base_model.output\nx = Flatten()(x) # Flattens the input. Does not affect the batch size.\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.25)(x)\n# Dropout consists in randomly setting\n#     a fraction `rate` of input units to 0 at each update during training time,\n#     which helps prevent overfitting.\n\nlogits = Dense(num_class, activation='softmax')(x)\n\nmodel = Model(base_model.input, logits)\n\nmodel.compile(optimizer='Adam',\n          loss='categorical_crossentropy', \n           metrics=[categorical_crossentropy, categorical_accuracy])\n\nprint('\\nThis base model has Flatten(), Dense(relu), Dropout() applied before a Dense(softmax)')\nprint('\\nFinally coming to a Model(), where it is compiled with adam optimizer, loss of categorical_crossentropy, looking for accuracy as well.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bff918d1418425c5a3f04e87d1406f3558883600","pycharm":{"is_executing":false}},"cell_type":"code","source":"print('Graph generating helper function defined...')\ndef gen_graph(history, title):\n    plt.plot(history.history['categorical_accuracy'])\n    plt.plot(history.history['val_categorical_accuracy'])\n    plt.title('Accuracy ' + title)\n    plt.ylabel('Accuracy')\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()\n    plt.plot(history.history['categorical_crossentropy'])\n    plt.plot(history.history['val_categorical_crossentropy'])\n    plt.title('Loss ' + title)\n    plt.ylabel('MLogLoss')\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0153f85d4e7d3a3e45925fddf963e6f9463d6f8a","pycharm":{"is_executing":false}},"cell_type":"code","source":"print('.flow() is used to train on the image data for X and Y arrays.')\n\ntrain_generator = datagen.flow(np.array(X_train), np.array(Y_train), \n                               batch_size=32) \n# Takes data & label arrays, generates batches of augmented data.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b814e1c53da62f7fca5bf9c62b056aee8a6c2677","scrolled":true,"pycharm":{"is_executing":true}},"cell_type":"code","source":"print('.fit_generator() on the model using the train_generator just made and get the history.\\n')\n\nbatch_size = 512\n\nhistory_rmsprop = model.fit_generator(\n    train_generator,\n    epochs=30, steps_per_epoch=len(X_train) / batch_size,\n    validation_data=(np.array(X_train), np.array(Y_train)), validation_steps=len(X_valid) / batch_size)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d700a487b7fe4fdbb73a5896c69c004bc73913d3","pycharm":{"is_executing":true}},"cell_type":"code","source":"print('Use the model to run .predict() on x_test.')\n\npreds = model.predict(np.array(x_test), verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"pycharm":{"name":"#%% md\n"}},"cell_type":"markdown","source":"## Visualizing the Data","execution_count":null},{"metadata":{"trusted":true,"_uuid":"50e3c0ad0fbdfca1336ff1bd51eaf5f69aa341e1","pycharm":{"is_executing":true}},"cell_type":"code","source":"print('Accuracy plotted for predictions made.\\n')\n\n#plot the accuracy\ngen_graph(history_rmsprop, \n              \"ResNet50 RMSprop\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06af78277b24dde2a87b179ed5f14caac9f64c85","pycharm":{"is_executing":true}},"cell_type":"code","source":"print('Dataframe is created with the prediction data and displayed.')\n\nsub = pd.DataFrame(preds)\ncol_names = one_hot.columns.values\nsub.columns = col_names\nsub.insert(0, 'id', df_test['id'])\nsub\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"672869459127950bb35c7dce0ebb976c830589a2","pycharm":{"is_executing":true}},"cell_type":"code","source":"print('The output dataframe created is saved to `output_rmsprop_aug.csv` and model saved as `rmsprop_v2_augmentation.h5`')\n\nsub.to_csv('output_rmsprop_aug.csv', index=False)\n\nmodel.save('rmsprop_v2_augmentation.h5')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"val_loss starts decreasing, val_acc starts increasing. This is also fine as that means model built is learning and working fine.","execution_count":null}],"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":4,"nbformat_minor":4}