{"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)\n\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\nprint(os.listdir(\"../input\"))\n\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},"cell_type":"code","source":"#images are stored in the input\nimport pandas as pd\nimport numpy as np\nlabels = pd.read_csv('../input/trainLabels.csv')\n#importing images\nimport os\nfrom PIL import Image\nimport random\nimport matplotlib.pyplot as plt\nimport time\n\nheight,width,channels = 224,224,3\nimages_dir = os.listdir('../input')\nimages_dir.remove('trainLabels.csv')\nimg_matrix = []\nimg_label = []\n\nfor file in images_dir:\n    base = os.path.basename(\"../input/\" + file)\n    fileName = os.path.splitext(base)[0]\n    img_label.append(labels.loc[labels.image==fileName, 'level'].values[0])\n    im = Image.open(\"../input/\" + file)   \n    img = im.resize((height,width))\n    rgb = img.convert('RGB')\n    img_matrix.append(np.array(rgb).flatten())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b2ed25c6744e1b29890928e441e72ec4946f7bf0"},"cell_type":"code","source":"from sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\n\nimg_matrix = np.asarray(img_matrix)\nimg_label = np.asarray(img_label)\n\ndata,label = shuffle(img_matrix,img_label,random_state=2)\ntrain_data = [data,label]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a83a3e218eba3a494af2cf55674c517451a03ff7"},"cell_type":"code","source":"img = img_matrix[200].reshape(height,width,channels)\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17c528659089ad860d57e4ddf40e4ab88df558bf"},"cell_type":"code","source":"(X,Y) = (train_data[0],train_data[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b863b8c6d91ee43036baebf48e607e5a4d25ecd"},"cell_type":"code","source":"#categorical\nclasses = 5\nepochs = 5\nbatchsize=128\n\nfrom keras.models import Sequential\nfrom keras.utils import np_utils\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.applications.vgg16 import VGG16\nfrom keras.optimizers import Adam\nfrom sklearn.model_selection import StratifiedKFold\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7cd6c9dfc82a5c7a8ad6731cb73bb2d32c6e25a"},"cell_type":"code","source":"X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27cd0603e3544cbb795c55769b36370d284c6f4a"},"cell_type":"code","source":"Y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e2c80e0d204061765b1e7bdcfc1371a0de4c56a"},"cell_type":"code","source":"X = X.reshape(X.shape[0],height,width,channels)\nX = X.astype('float32')\nX /= 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"20ad1930f9036cc02613d98203fe0be5c2c20808"},"cell_type":"code","source":"# 10 fold cross validation\nkfold = StratifiedKFold(n_splits=5,shuffle=True,random_state=7)\ncvscores = []\nfor train,test in kfold.split(X,Y):\n    # create the cnn model\n    model = Sequential()\n    #add model layers\n    model.add(Conv2D(32, (3, 3), padding='same', input_shape=(height,width,channels), activation='relu'))\n    model.add(Conv2D(32, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n    \n    model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(64,(3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n\n    model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(64,(3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n\n    model.add(Flatten())\n    model.add(Dense(512, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(5, activation='softmax'))\n\n    #optimizer\n    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    \n    #model fit\n    Y_train = np_utils.to_categorical(Y[train],classes)\n    model.fit(X[train], Y_train,epochs=epochs,verbose=0)\n    \n    #evaluate the mode\n    Y_test = np_utils.to_categorical(Y[test],classes)\n    scores = model.evaluate(X[test],Y_test,verbose=0)\n    print(\"%s: %.2f%%\" % (model.metrics_names[1], scores[1] * 100))\n    cvscores.append(scores[1]*100)\n\nprint(\"%.2f%%\" % np.mean(cvscores))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2d3d5a399e31c1757d09ca8e7752a689aa2ed5c"},"cell_type":"code","source":"# 10 fold cross validation\nkfold = StratifiedKFold(n_splits=5,shuffle=True,random_state=7)\ncvscores = []\nfor train,test in kfold.split(X,Y):\n    # create the cnn model\n    model = Sequential()\n    #add model layers\n    model.add(Conv2D(32, (3, 3), padding='same', input_shape=(height,width,channels), activation='relu'))\n    model.add(Conv2D(32, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n    \n    model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(64,(3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n\n    model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(64,(3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n\n    model.add(Flatten())\n    model.add(Dense(512, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(5, activation='softmax'))\n\n    #optimizer\n    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    Y_train = to_categorical(Y[train],classes)\n    #model fit\n    print(X[train].shape,Y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3603305e2069516edf76d0b93f455f99c8856a1"},"cell_type":"code","source":"from keras.utils import to_categorical\nto_categorical(Y[1:10],5).shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3e11a12eed193a61abd58ee0587c98418825e0a"},"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}