{"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 \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\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\nfrom keras.models import Sequential,Model\nfrom keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout,Activation,BatchNormalization\nfrom keras import losses\nfrom keras.optimizers import Adam, Adagrad\nfrom keras.callbacks import EarlyStopping\nfrom keras import regularizers\nfrom sklearn.model_selection import GridSearchCV\nimport keras\nfrom keras.layers import LeakyReLU\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# VGG16 Net"},{"metadata":{"trusted":true},"cell_type":"code","source":"def VGG16():    \n    model = Sequential()\n    model.add(Conv2D(32,kernel_size = (3,3),strides=1,input_shape=(224,224,3),activity_regularizer = regularizers.l2(1e-8)))\n    model.add(Activation(\"relu\"))\n    model.add(Conv2D(32,kernel_size = (3,3),strides=1,activity_regularizer = regularizers.l2(1e-8)))\n    model.add(Activation(\"relu\"))\n    model.add(MaxPooling2D(pool_size=(3, 3),strides=2, padding='same', data_format=None))\n    model.add(Dropout(0.25))\n\n    model.add(Conv2D(64,kernel_size = (3,3),strides=1))\n    model.add(Activation(\"relu\"))\n    model.add(Conv2D(64,kernel_size = (3,3),strides=1))\n    model.add(Activation(\"relu\"))\n    model.add(MaxPooling2D(pool_size=(3, 3),strides=2, padding='same', data_format=None))\n\n\n    model.add(Conv2D(128,kernel_size = (3,3),strides=1))\n    model.add(Activation(\"relu\"))\n    model.add(Conv2D(128,kernel_size = (3,3),strides=1,activity_regularizer = regularizers.l2(1e-8)))\n    model.add(Activation(\"relu\"))\n    model.add(MaxPooling2D(pool_size=(2, 2),strides=2, padding='same', data_format=None))\n\n\n\n    model.add(Flatten())\n\n    model.add(Dense(4096,activity_regularizer = regularizers.l2(1e-8)))\n    model.add(Activation(\"relu\"))\n    model.add(Dropout(0.1))\n\n\n\n    model.add(Dense(512,activity_regularizer = regularizers.l2(1e-8)))\n    model.add(Activation(\"relu\"))\n    model.add(Dropout(0.1))\n\n\n\n\n    model.add(Dense(5,activation = 'softmax'))\n\n    model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False), metrics=[\"accuracy\"])\n    model.summary()\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nEPOCHS=30\nvggModel=VGG16()\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255\n)\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\n\ntrain_generator = train_datagen.flow_from_directory(\n    '../input/train_resizedvgg net/',\n    target_size=(224,224),\n    batch_size=16\n)\nvalidation_generator = test_datagen.flow_from_directory(\n        '../input/test_resizedvgg net/',\n        target_size=(224,224),\n        batch_size=32)\nhistory=vggModel.fit_generator(\n        train_generator,\n        steps_per_epoch=2000,\n        epochs=EPOCHS,\n        validation_data=validation_generator,\n        validation_steps=50\n        )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot training & validation accuracy values\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\n\n\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\n\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# set the matplotlib backend so figures can be saved in the background\n# plot the training loss and accuracy\nimport sys\nimport matplotlib\nprint(\"Generating plots...\")\nsys.stdout.flush()\nmatplotlib.use(\"Agg\")\nmatplotlib.pyplot.style.use(\"ggplot\")\nmatplotlib.pyplot.figure()\nN = EPOCHS\nmatplotlib.pyplot.plot(np.arange(0, N), history.history[\"loss\"], label=\"train_loss\")\nmatplotlib.pyplot.plot(np.arange(0, N), history.history[\"val_loss\"], label=\"val_loss\")\nmatplotlib.pyplot.plot(np.arange(0, N), history.history[\"acc\"], label=\"train_acc\")\nmatplotlib.pyplot.plot(np.arange(0, N), history.history[\"val_acc\"], label=\"val_acc\")\nmatplotlib.pyplot.title(\"Training Loss and Accuracy on diabetic retinopathy detection\")\nmatplotlib.pyplot.xlabel(\"Epoch #\")\nmatplotlib.pyplot.ylabel(\"Loss/Accuracy\")\nmatplotlib.pyplot.legend(loc=\"lower left\")\nmatplotlib.pyplot.savefig(\"plot.png\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import SVG\nfrom keras.utils.vis_utils import model_to_dot\nSVG(model_to_dot(model).create(prog='dot', format='svg'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# serialize model to YAML\nmodel_yaml = model.to_yaml()\nwith open(\"model.yaml\", \"w\") as yaml_file:\n    yaml_file.write(model_yaml)\n# serialize weights to HDF5\nmodel.save_weights(\"model.h5\")\nprint(\"Saved model to disk\")\n ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}