{"cells":[{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.image as image\ndir='../input/state-farm-distracted-driver-detection/imgs/train'\nlabels=['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']\n\ntraining_data=[]\nfor category in labels:\n    label = labels.index(category)\n    path=os.path.join(dir,category)\n    count=0\n    for img in os.listdir(path):\n        img_path=os.path.join(path,img)\n        img_data=cv2.imread(img_path,cv2.COLOR_BGR2RGB)\n        img_data=cv2.resize(img_data,(224,224))\n        training_data.append([img_data,label])\n        count=count+1\n    print(count)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import random\nrandom.shuffle(training_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_values=['safe driving',\n'texting - right',\n'talking on the phone - right',\n'texting - left',\n'talking on the phone - left',\n'operating the radio',\n'drinking',\n'reaching behind',\n'hair and makeup',\n'talking to passenger']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train=[]\nY_train=[]\nfor feature,label in training_data:\n    X_train.append(feature)\n    Y_train.append(label)\n \nX_train=np.array(X_train)\nY_train=np.array(Y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del training_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(X_train,Y_train, test_size=0.4)\nx_train = np.array(x_train, dtype=np.uint8).reshape(-1,224,224,3)\nx_test = np.array(x_test, dtype=np.uint8).reshape(-1,224,224,3)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del X_train\ndel Y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_train[10])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train=pd.get_dummies(y_train)\ny_test=pd.get_dummies(y_test)\n\ny_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras,os\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPool2D , Flatten,Dropout\nfrom keras.layers import Dropout\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"VGG=keras.applications.VGG16(input_shape=(224,224,3),include_top=False,weights='imagenet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"VGG.trainable=False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=keras.Sequential([\n        VGG,\n        keras.layers.Dropout(0.2),\n        keras.layers.Flatten(),\n        keras.layers.Dense(256,activation=\"relu\"),\n        keras.layers.Dense(256,activation=\"relu\"),\n        keras.layers.Dense(10,activation=\"softmax\")                \n])\nmodel.compile(optimizer=\"adam\",loss=keras.losses.categorical_crossentropy,metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" hist=model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=5, batch_size=100)\n model.save('vggclf.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pickle\nimport joblib","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('DDVGG16.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ind=np.argmax(model.predict(np.array(x_test[1], dtype=np.uint8).reshape(-1,224,224,3)))\nprint(output_values[ind])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_test[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\nmodel = load_model('./DDVGG16.h5')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.argmax(model.predict(np.array(x_test[10], dtype=np.uint8).reshape(-1,224,224,3)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_values[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_test[10])","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}