{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nfrom keras.models import Model, Sequential\nfrom keras.layers import Input, Flatten, Dense, Conv2D, MaxPooling2D, Dropout\nfrom keras.utils import layer_utils\nfrom keras import backend as K\nfrom keras.optimizers import RMSprop, SGD, Adam\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint, TensorBoard, CSVLogger\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import log_loss, accuracy_score\nfrom keras.preprocessing import image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.applications import xception\nfrom keras.applications import inception_v3\nfrom keras.applications.vgg16 import preprocess_input, decode_predictions\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> # Import ResNet50 weights trained on ImageNet","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = ResNet50(weights='imagenet',include_top=False, input_shape=(512, 512, 3))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Add 3 hidden layers and an output layer","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x = model.output\nx = Flatten()(x)\nx=Dropout(0.25)(x)\nx=Dense(units=1000,activation='relu')(x)\nx=Dropout(0.25)(x)\nx=Dense(units=750,activation='relu')(x)\nx=Dropout(0.25)(x)\nx=Dense(units=750,activation='relu')(x)\nx=Dropout(0.25)(x)\nx=Dense(units=750,activation='relu')(x)\nx=Dropout(0.25)(x)\nx=Dense(units=1000,activation='relu')(x)\nx=Dropout(0.6)(x)\n#clf.add(Dense(units=120,activation='softmax')\n#stochastic gradient descent -Adam -optimizer\n#loss func categorical cross entropy\n#metrics = accuracy\n#clf.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\npredictions = Dense(120, activation='softmax')(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nmain_model = Model(inputs=model.input, outputs=predictions)\n#train only the hidden layers and output layer, donot train the resnet model\nfor curLayer in model.layers:\n    curLayer.trainable = False\n\nmain_model.compile(loss='categorical_crossentropy', \n              optimizer='adam', \n              metrics=['accuracy'])\n\ncallbacks_list = [keras.callbacks.EarlyStopping(monitor='val_acc', patience=3, verbose=1)]\nmain_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        rescale=1/255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n#check class_mode in keras documentation https://keras.io/preprocessing/image/\ntraining_set = train_datagen.flow_from_directory(\n        'dog-breed-identification/train',\n        target_size=(512, 512),\n        batch_size=20,\n        class_mode='categorical')\n\ntest_set = test_datagen.flow_from_directory(\n        'dog-breed-identification/test',\n        target_size=(512, 512),\n        batch_size=22,\n        class_mode='categorical')\n\nhist=main_model.fit_generator(\n        training_set,\n        steps_per_epoch=400,\n        epochs=1,\n        validation_data=test_set,\n        validation_steps=101,callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}