{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \nfrom tensorflow.python.keras.applications import ResNet50\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D\nfrom keras.preprocessing import image\nfrom keras.applications.resnet50 import preprocess_input\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\nimport sklearn.preprocessing\nimport sklearn.model_selection\nimport sklearn.metrics\nimport sklearn.linear_model\nimport sklearn.naive_bayes\nimport sklearn.tree\nimport sklearn.ensemble\n\nimport glob\nimport random\nimport base64\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm import tqdm\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nfrom os.path import join, exists, expanduser\nfrom os import listdir, makedirs\n\nfrom PIL import Image\nfrom io import BytesIO\nfrom IPython.display import HTML\npd.set_option('display.max_colwidth', -1)\n\ndef get_thumbnail(path):\n    i = Image.open(path)\n    i.thumbnail((150, 150), Image.LANCZOS)\n    return i\n\ndef image_base64(im):\n    if isinstance(im, str):\n        im = get_thumbnail(im)\n    with BytesIO() as buffer:\n        im.save(buffer, 'jpeg')\n        return base64.b64encode(buffer.getvalue()).decode()\n\ndef image_formatter(im):\n    return f'<img src=\"data:image/jpeg;base64,{image_base64(im)}\">'\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels_test=labels.sample(20)\nlabels_test['file'] = labels_test.id.map(lambda id: f'../input/dog-breed-identification/train/{id}.jpg')\nlabels_test['image'] = labels_test.file.map(lambda f: get_thumbnail(f))\nlabels_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba4cf5440589b5edd80b04518cbb4e67495a4c84"},"cell_type":"code","source":"HTML(labels_test[['breed', 'image']].to_html(formatters={'image': image_formatter}, escape=False))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea1839796309acb4bda520e9a2f5993219423d77"},"cell_type":"code","source":"!ls ../input/keras-pretrained-models/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a5e199377da4820256731a56feac4415cc7f545"},"cell_type":"code","source":"cache_dir = expanduser(join('~', '.keras'))\nif not exists(cache_dir):\n    os.makedirs(cache_dir)\nmodels_dir = join(cache_dir, 'models')\nif not exists(models_dir):\n    os.makedirs(models_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"084463fc1f18e0bc1cc5f4519a7a33a44e2f0986"},"cell_type":"code","source":"!cp ../input/keras-pretrained-models/*notop* ~/.keras/models/\n!cp ../input/keras-pretrained-models/imagenet_class_index.json ~/.keras/models/\n!cp ../input/keras-pretrained-models/resnet50* ~/.keras/models/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9bd35980c92d4b06bed132d29c43a4c21eccd469"},"cell_type":"code","source":"!ls ~/.keras/models","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ae9c90883106be921a0080c039d410b1ae597ee"},"cell_type":"code","source":"nclass = 10\nimage_size=224\n\nbreed_list = list(labels.groupby('breed').count().sort_values(by='id',ascending=False).head(nclass).index)\nlabels = labels[labels['breed'].isin(breed_list)]\nlabels['tmp']=1\nlabels=labels.pivot('id', 'breed', 'tmp').reset_index().fillna(0)\nlabels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e32b063dfc65fe37ab27d091bda6e8e79ba0e08f"},"cell_type":"code","source":"data_dir='../input/dog-breed-identification/'\n\ndef read_img(img_id, train_or_test, size):\n    \"\"\"Read and resize image.\n    # Arguments\n        img_id: string\n        train_or_test: string 'train' or 'test'.\n        size: resize the original image.\n    # Returns\n        Image as numpy array.\n    \"\"\"\n    img = image.load_img(join(data_dir, train_or_test, '%s.jpg' % img_id), target_size=size)\n    img = image.img_to_array(img)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d113d6c4d578e5c6d0408488202734f2b56f779"},"cell_type":"code","source":"resnet_weights_path = '../input/keras-pretrained-models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\nmodel = Sequential()\nmodel.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))\nmodel.add(Dense(nclass, activation='softmax'))\n\n# Say not to train first layer (ResNet) model. It is already trained\nmodel.layers[0].trainable = False\nmodel.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b00d70c638ce6a4c16afa3525ab930e37f28f41f"},"cell_type":"code","source":"cv_num=10\nkfold=sklearn.model_selection.KFold(cv_num,shuffle=True,random_state=42)\n\nx_trains = np.zeros((len(labels), image_size, image_size, 3), dtype='float32')\nfor i, img_id in tqdm(enumerate(labels['id'])):\n    img = read_img(img_id, 'train', (image_size, image_size))\n    x = preprocess_input(np.expand_dims(img.copy(), axis=0))\n    x_trains[i] = x\n\ny_trains = labels[breed_list].values    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"21b510734b24541cc4786aae4e783694d04a4df9"},"cell_type":"code","source":"nepochs = 3\nif True:\n    for i,(train_index,valid_index) in enumerate(kfold.split(y_trains)):\n        x_train = x_trains[train_index]\n        y_train = y_trains[train_index]\n        x_valid = x_trains[valid_index]\n        y_valid = y_trains[valid_index]\n        \n        datagen = ImageDataGenerator(\n        rotation_range=20,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        horizontal_flip=True)\n\n# compute quantities required for featurewise normalization\n# (std, mean, and principal components if ZCA whitening is applied)\n        datagen.fit(x_train)\n\n# fits the model on batches with real-time data augmentation:\n        model.fit_generator(datagen.flow(x_train, y_train, batch_size=32),\n                    steps_per_epoch=len(x_train) / 32, validation_data=datagen.flow(x_valid, y_valid, batch_size=32),validation_steps=len(y_valid)/32, epochs=nepochs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"565617a09a5981d05b9bd9fb75d421e0fcaec11c"},"cell_type":"code","source":"if True:\n    x_trains_predict = model.predict(x_trains, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7de618082f130629c9b1d6ea3f035f7e3bfb3069"},"cell_type":"code","source":"if True:\n\n    failed=[]\n    truths=[]\n    predictions=[]\n\n    for i in range(y_trains.shape[0]):\n        truth=breed_list[np.argmax(y_trains[i])]\n        prediction=breed_list[np.argmax(x_trains_predict[i])]\n        truths.append(truth)\n        predictions.append(prediction)\n        if prediction != truth:            \n            failed.append(i)\n            \n    print('Failed: ',len(failed),'/',len(y_trains),'=',len(failed)/len(y_trains))\n    \n    ndisplay = min(20,len(failed))\n\n    temp=pd.DataFrame()\n    temp['Predicted'] = [predictions[i] for i in failed[:ndisplay]]\n    temp['Truth'] = [truths[i] for i in failed[:ndisplay]]\n    \n    temp['file'] = labels[:ndisplay].id.map(lambda id: f'../input/dog-breed-identification/train/{id}.jpg')\n    temp['image'] = temp.file.map(lambda f: get_thumbnail(f))\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a92199aa725a1583c63b579862aff9c071a3064a"},"cell_type":"code","source":"HTML(temp[['Predicted','Truth', 'image']].to_html(formatters={'image': image_formatter}, escape=False))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8dce049da12bbb274e010ed9669702abd7efe04e"},"cell_type":"code","source":"if True:    \n    test_ids = listdir('../input/dog-breed-identification/test')\n\n    test_ids=test_ids[0:20]\n\n    x_tests = np.zeros((len(test_ids), image_size, image_size, 3), dtype='float32')\n    for i, img_id in tqdm(enumerate(test_ids)):\n    \n        img = image.load_img(join(data_dir, 'test', '%s' % img_id), target_size=(image_size, image_size))\n        img = image.img_to_array(img)\n        x = preprocess_input(np.expand_dims(img.copy(), axis=0))\n        x_tests[i] = x\n\n\n    predicts = model.predict(x_tests)\n\n    preds = []\n\n    for i in range(len(predicts)):\n        preds.append(breed_list[np.argmax(predicts[i])])\n\n    sub = pd.DataFrame()\n    sub['id'] = test_ids\n    sub['breed'] = preds\n\n    sub['file'] = sub.id.map(lambda id: f'../input/dog-breed-identification/test/{id}')\n    sub['image'] = sub.file.map(lambda f: get_thumbnail(f))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07e61c0c9bc37c6c595904876aebec4c489f70c2"},"cell_type":"code","source":"HTML(sub[['breed', 'image']].to_html(formatters={'image': image_formatter}, escape=False))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d09d33268cd95a5845aa42994520eebfbd13d32e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}