{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n## Libraries","metadata":{"papermill":{"duration":0.033175,"end_time":"2022-07-10T19:01:10.657265","exception":false,"start_time":"2022-07-10T19:01:10.62409","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport plotly.express as px\n","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2022-07-10T19:01:10.577494Z","iopub.status.busy":"2022-07-10T19:01:10.576928Z","iopub.status.idle":"2022-07-10T19:01:10.589387Z","shell.execute_reply":"2022-07-10T19:01:10.58841Z"},"papermill":{"duration":0.05231,"end_time":"2022-07-10T19:01:10.591634","exception":false,"start_time":"2022-07-10T19:01:10.539324","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install sewar","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sewar\nfrom skimage import measure, metrics\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from getpass import getpass\nimport os\nimport glob as gb \nimport shutil\nimport pandas as pd\nimport numpy as np\nimport math\nimport cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nfrom keras import models\nfrom keras import layers\nimport tensorflow.keras as keras\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.utils import plot_model\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import callbacks\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:10.725044Z","iopub.status.busy":"2022-07-10T19:01:10.724084Z","iopub.status.idle":"2022-07-10T19:01:17.132578Z","shell.execute_reply":"2022-07-10T19:01:17.131517Z"},"papermill":{"duration":6.44529,"end_time":"2022-07-10T19:01:17.135368","exception":false,"start_time":"2022-07-10T19:01:10.690078","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Explore the data & Visualisation\n","metadata":{"papermill":{"duration":0.032737,"end_time":"2022-07-10T19:01:17.202418","exception":false,"start_time":"2022-07-10T19:01:17.169681","status":"completed"},"tags":[]}},{"cell_type":"code","source":"main_folder=\"state-farm-distracted-driver-detection\"\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:17.270526Z","iopub.status.busy":"2022-07-10T19:01:17.269632Z","iopub.status.idle":"2022-07-10T19:01:17.274438Z","shell.execute_reply":"2022-07-10T19:01:17.273525Z"},"papermill":{"duration":0.041214,"end_time":"2022-07-10T19:01:17.276399","exception":false,"start_time":"2022-07-10T19:01:17.235185","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver=pd.read_csv(\"driver_imgs_list.csv\")\nlen(df_driver)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:17.344463Z","iopub.status.busy":"2022-07-10T19:01:17.343415Z","iopub.status.idle":"2022-07-10T19:01:17.382306Z","shell.execute_reply":"2022-07-10T19:01:17.38126Z"},"papermill":{"duration":0.07602,"end_time":"2022-07-10T19:01:17.38455","exception":false,"start_time":"2022-07-10T19:01:17.30853","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver.head()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:17.454467Z","iopub.status.busy":"2022-07-10T19:01:17.452599Z","iopub.status.idle":"2022-07-10T19:01:17.469421Z","shell.execute_reply":"2022-07-10T19:01:17.468405Z"},"papermill":{"duration":0.05327,"end_time":"2022-07-10T19:01:17.471586","exception":false,"start_time":"2022-07-10T19:01:17.418316","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count=0\nfile_path=\"imgs/train\"\ncounts={}\nfor folder in os.listdir(file_path):\n    class_count=0\n    images=gb.glob(pathname=str(file_path+'/'+folder+'/*.*'))\n    for img in images:\n        count+=1\n        class_count+=1\n    num_image={f\"{folder}\":class_count}\n    counts.update(num_image)\n    print(f\"the number of images in class {folder}: {class_count}\")\n    print()\nprint()\nprint(f\"The Total Number of Images: {count}\")","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:17.541949Z","iopub.status.busy":"2022-07-10T19:01:17.541046Z","iopub.status.idle":"2022-07-10T19:01:19.841815Z","shell.execute_reply":"2022-07-10T19:01:19.840422Z"},"papermill":{"duration":2.338775,"end_time":"2022-07-10T19:01:19.844042","exception":false,"start_time":"2022-07-10T19:01:17.505267","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(counts)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:19.910167Z","iopub.status.busy":"2022-07-10T19:01:19.909883Z","iopub.status.idle":"2022-07-10T19:01:19.914476Z","shell.execute_reply":"2022-07-10T19:01:19.913521Z"},"papermill":{"duration":0.040963,"end_time":"2022-07-10T19:01:19.917239","exception":false,"start_time":"2022-07-10T19:01:19.876276","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(counts.keys())","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:19.986571Z","iopub.status.busy":"2022-07-10T19:01:19.985086Z","iopub.status.idle":"2022-07-10T19:01:19.991816Z","shell.execute_reply":"2022-07-10T19:01:19.990926Z"},"papermill":{"duration":0.042507,"end_time":"2022-07-10T19:01:19.994038","exception":false,"start_time":"2022-07-10T19:01:19.951531","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(counts.values())","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:20.064055Z","iopub.status.busy":"2022-07-10T19:01:20.063202Z","iopub.status.idle":"2022-07-10T19:01:20.069789Z","shell.execute_reply":"2022-07-10T19:01:20.068877Z"},"papermill":{"duration":0.043743,"end_time":"2022-07-10T19:01:20.071697","exception":false,"start_time":"2022-07-10T19:01:20.027954","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(10,5))\nax = fig.add_axes([0,0,1,1])\nax.bar(list(counts.keys()) , list(counts.values()), color = 'maroon',width=0.5)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:20.138183Z","iopub.status.busy":"2022-07-10T19:01:20.137175Z","iopub.status.idle":"2022-07-10T19:01:20.355878Z","shell.execute_reply":"2022-07-10T19:01:20.354993Z"},"papermill":{"duration":0.25389,"end_time":"2022-07-10T19:01:20.358035","exception":false,"start_time":"2022-07-10T19:01:20.104145","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_name = {  'c0' : \"safe_driving\",\n                  'c1' : \"texting-right\",\n                  'c2' : \"talking_on_the_phone-right\",\n                  'c3' : \"texting-left\",\n                  'c4' : \"talking_on_the_phone-left\",\n                  'c5' : \"operating_the_radio\",\n                  'c6' : \"drinking\",\n                  'c7' : \"reaching_behind\",\n                  'c8' : \"hair-and-makeup\",\n                  'c9' : \"talking_to_passenger\"}","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:20.427648Z","iopub.status.busy":"2022-07-10T19:01:20.426908Z","iopub.status.idle":"2022-07-10T19:01:20.432715Z","shell.execute_reply":"2022-07-10T19:01:20.431894Z"},"papermill":{"duration":0.042756,"end_time":"2022-07-10T19:01:20.434663","exception":false,"start_time":"2022-07-10T19:01:20.391907","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver.isna().sum()\n#missing data","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver.describe()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver.info()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:20.505063Z","iopub.status.busy":"2022-07-10T19:01:20.504798Z","iopub.status.idle":"2022-07-10T19:01:20.53113Z","shell.execute_reply":"2022-07-10T19:01:20.5293Z"},"papermill":{"duration":0.066589,"end_time":"2022-07-10T19:01:20.534288","exception":false,"start_time":"2022-07-10T19:01:20.467699","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\n\nimgs=[]\nnames=[]\nfor label in list(classes_name.keys()):\n    imgs_name=shuffle(df_driver[df_driver[\"classname\"] == label])[\"img\"][:5]\n    imgs.extend(imgs_name)\n    names.extend(((label+\" \")*5).split())\n\nprint(imgs)\nprint(names)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:20.603542Z","iopub.status.busy":"2022-07-10T19:01:20.602677Z","iopub.status.idle":"2022-07-10T19:01:20.650942Z","shell.execute_reply":"2022-07-10T19:01:20.649402Z"},"papermill":{"duration":0.085161,"end_time":"2022-07-10T19:01:20.653441","exception":false,"start_time":"2022-07-10T19:01:20.56828","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nnf = df_driver['classname'].value_counts(sort=False)\nlabels = df_driver['classname'].value_counts(sort=False).index.tolist()\ny = np.array(nf)\nwidth = 1/1.5\nN = len(y)\nx = range(N)\n\nfig = plt.figure(figsize=(20,15))\nay = fig.add_subplot(211)\n\nplt.xticks(x, labels, size=15)\nplt.yticks(size=15)\n\nay.bar(x, y, width, color=\"blue\")\n\nplt.title('Bar Chart',size=25)\nplt.xlabel('classname',size=15)\nplt.ylabel('Count',size=15)\n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver['class_type'] = df_driver['classname'].str.extract('(\\d)',expand=False).astype(np.float)\nplt.figure()\ndf_driver.hist('class_type',alpha=0.5,layout=(1,1),bins=9)\nplt.title('class distribution')\nplt.draw()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot figure size\nplt.figure(figsize = (10,10))\n# Count the number of images per category\nsns.countplot(x = 'classname', data = df_driver)\n# Change the Axis names\nplt.ylabel('Count')\nplt.title('Categories Distribution')\n# Show plot\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of Images by Drivers / Test Subject\n\ndrivers_id = pd.DataFrame((df_driver['subject'].value_counts()).reset_index())\ndrivers_id.columns = ['driver_id', 'Counts']\npx.histogram(drivers_id, x=\"driver_id\",y=\"Counts\" ,color=\"driver_id\", title=\"Number of images by subjects \")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading and preparing the images\nimages = []\nlabels = []\nlimit=400\nimg_size=256 \n\nmain_path='imgs/train'\nfolder_names = []\nfor entry_name in os.listdir('imgs/train'):\n    entry_path = os.path.join('imgs/train', entry_name)\n    if os.path.isdir(entry_path):\n        folder_names.append(entry_name)\n        \nprint('The Categories are',folder_names)\n\nj=0\nfor folder in folder_names:\n    for filename in os.listdir(os.path.join(main_path,folder)):\n        img_path = os.path.join(main_path,folder)\n        img = cv2.imread(os.path.join(img_path,filename)) \n        if img is not None:\n            img  = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)   \n            img = cv2.resize(img,(img_size, img_size))\n            images.append(img)\n            if folder == 'NORMAL':\n                labels.append(0)\n                #print('normal')\n            else:\n                labels.append(1)\n                #print('PNE')\n        j=j+1\n        if j >= limit:\n            j=0\n            break\n            \nimages,labels=np.array(images),np.array(labels)\nprint(images.shape)\nprint(labels)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mse (GT,P):\n\t\"\"\"calculates mean squared error (mse).\n\n\t:param GT: first (original) input image.\n\t:param P: second (deformed) input image.\n\n\t:returns:  float -- mse value.\n\t\"\"\"\n\treturn np.mean((GT.astype(np.float64)-P.astype(np.float64))**2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = os.path.join( 'imgs', 'train', 'c0', 'img_104.jpg')\nimg = cv2.imread(os.path.join(img_path,filename)) \n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1  = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\nplt.imshow(img1, cmap='gray')\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Printing a random sample\nfrom random import randrange\n\ni = randrange(limit*2)\n\nplt.imshow(images[i],cmap='gray')\nplt.xticks([])\nplt.yticks([])\nplt.show()\n\n#Canny\ncanny = cv2.Canny(images[i],40,200)\n\n\n#Sobel\nsobelY = cv2.Sobel(images[i],cv2.CV_8UC1,0,1,ksize=5)\n\npreview = [canny,sobelY]\ni=0\nfor i in range(2):\n    plt.subplot(1,2,i+1)\n    plt.imshow(preview[i], cmap='gray')\n    plt.xticks([])\n    plt.yticks([])\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig = plt.figure(figsize=(30, 30))\ngrid = ImageGrid(fig, 111,  # similar to subplot(111)\n                 nrows_ncols=(10, 5),  # creates 2x2 grid of axes\n                 axes_pad=0.5, label_mode='all' # pad between axes in inch.\n)\ni=0\nfile_path=\"imgs/train/\"\nfor axes in grid:\n    img=cv2.imread(file_path+names[i]+\"/\"+imgs[i])\n    rgb_img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    axes.imshow(rgb_img)\n    axes.xaxis.set_visible(False)\n    axes.yaxis.set_visible(False)\n    axes.set_title(classes_name.get(names[i]))\n    i+=1\n\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:20.723058Z","iopub.status.busy":"2022-07-10T19:01:20.721703Z","iopub.status.idle":"2022-07-10T19:01:27.270401Z","shell.execute_reply":"2022-07-10T19:01:27.269242Z"},"papermill":{"duration":6.619975,"end_time":"2022-07-10T19:01:27.307369","exception":false,"start_time":"2022-07-10T19:01:20.687394","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Without Data Augmentation","metadata":{"papermill":{"duration":0.070688,"end_time":"2022-07-10T19:01:27.445874","exception":false,"start_time":"2022-07-10T19:01:27.375186","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Preparing Data","metadata":{"papermill":{"duration":0.064989,"end_time":"2022-07-10T19:01:27.577417","exception":false,"start_time":"2022-07-10T19:01:27.512428","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df_driver.info()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:27.711213Z","iopub.status.busy":"2022-07-10T19:01:27.710746Z","iopub.status.idle":"2022-07-10T19:01:27.729798Z","shell.execute_reply":"2022-07-10T19:01:27.728041Z"},"papermill":{"duration":0.090088,"end_time":"2022-07-10T19:01:27.732605","exception":false,"start_time":"2022-07-10T19:01:27.642517","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_data=df_driver[\"img\"]\ny_data=df_driver[\"classname\"]","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:27.867413Z","iopub.status.busy":"2022-07-10T19:01:27.866938Z","iopub.status.idle":"2022-07-10T19:01:27.872019Z","shell.execute_reply":"2022-07-10T19:01:27.871084Z"},"papermill":{"duration":0.074825,"end_time":"2022-07-10T19:01:27.873956","exception":false,"start_time":"2022-07-10T19:01:27.799131","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x_data,y_data,test_size = 0.4,shuffle=True)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:28.010895Z","iopub.status.busy":"2022-07-10T19:01:28.010459Z","iopub.status.idle":"2022-07-10T19:01:28.021254Z","shell.execute_reply":"2022-07-10T19:01:28.020386Z"},"papermill":{"duration":0.08052,"end_time":"2022-07-10T19:01:28.023233","exception":false,"start_time":"2022-07-10T19:01:27.942713","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid,xtest,y_valid,ytest=train_test_split(x_test,y_test,test_size = 0.4,shuffle=True)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:28.156418Z","iopub.status.busy":"2022-07-10T19:01:28.15603Z","iopub.status.idle":"2022-07-10T19:01:28.164726Z","shell.execute_reply":"2022-07-10T19:01:28.163831Z"},"papermill":{"duration":0.078655,"end_time":"2022-07-10T19:01:28.166789","exception":false,"start_time":"2022-07-10T19:01:28.088134","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.value_counts()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:28.316131Z","iopub.status.busy":"2022-07-10T19:01:28.315751Z","iopub.status.idle":"2022-07-10T19:01:28.325204Z","shell.execute_reply":"2022-07-10T19:01:28.324245Z"},"papermill":{"duration":0.08155,"end_time":"2022-07-10T19:01:28.327184","exception":false,"start_time":"2022-07-10T19:01:28.245634","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid.value_counts()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:28.464158Z","iopub.status.busy":"2022-07-10T19:01:28.463458Z","iopub.status.idle":"2022-07-10T19:01:28.47198Z","shell.execute_reply":"2022-07-10T19:01:28.471059Z"},"papermill":{"duration":0.081869,"end_time":"2022-07-10T19:01:28.474762","exception":false,"start_time":"2022-07-10T19:01:28.392893","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ytest.value_counts()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:28.610837Z","iopub.status.busy":"2022-07-10T19:01:28.610428Z","iopub.status.idle":"2022-07-10T19:01:28.619356Z","shell.execute_reply":"2022-07-10T19:01:28.61833Z"},"papermill":{"duration":0.079787,"end_time":"2022-07-10T19:01:28.62142","exception":false,"start_time":"2022-07-10T19:01:28.541633","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = pd.DataFrame(x_train.reset_index(drop=True))\nx_valid = pd.DataFrame(x_valid.reset_index(drop=True))\nxtest = pd.DataFrame(xtest.reset_index(drop=True))\n\ny_train = pd.DataFrame(y_train.reset_index(drop=True))\ny_valid = pd.DataFrame(y_valid.reset_index(drop=True))\nytest = pd.DataFrame( ytest.reset_index(drop=True))","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:28.756941Z","iopub.status.busy":"2022-07-10T19:01:28.756297Z","iopub.status.idle":"2022-07-10T19:01:28.767466Z","shell.execute_reply":"2022-07-10T19:01:28.766544Z"},"papermill":{"duration":0.082287,"end_time":"2022-07-10T19:01:28.76955","exception":false,"start_time":"2022-07-10T19:01:28.687263","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:28.909083Z","iopub.status.busy":"2022-07-10T19:01:28.908649Z","iopub.status.idle":"2022-07-10T19:01:28.921691Z","shell.execute_reply":"2022-07-10T19:01:28.920412Z"},"papermill":{"duration":0.086833,"end_time":"2022-07-10T19:01:28.924078","exception":false,"start_time":"2022-07-10T19:01:28.837245","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:29.176642Z","iopub.status.busy":"2022-07-10T19:01:29.176169Z","iopub.status.idle":"2022-07-10T19:01:29.191903Z","shell.execute_reply":"2022-07-10T19:01:29.190833Z"},"papermill":{"duration":0.140764,"end_time":"2022-07-10T19:01:29.197607","exception":false,"start_time":"2022-07-10T19:01:29.056843","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(x_train)):\n    class_name=y_train['classname'][i]\n    img=x_train['img'][i]\n    if os.path.exists(train_path + class_name):\n        shutil.copy(source_path + class_name+\"/\"+img , train_path + class_name+\"/\"+img)\n    else:\n        os.makedirs(train_path + class_name)\n\nfor i in range(len(x_valid)):\n    class_name=y_valid['classname'][i]\n    img=x_valid['img'][i]\n    if os.path.exists(valid_path + class_name):\n        shutil.copy(source_path + class_name+\"/\"+img , valid_path + class_name+\"/\"+img)\n    else:\n        os.makedirs(valid_path + class_name)\n\n\n\nfor i in range(len(xtest)):\n    class_name=ytest['classname'][i]\n    img=xtest['img'][i]\n    if os.path.exists(test_path + class_name):\n        shutil.copy(source_path + class_name+\"/\"+img , test_path + class_name+\"/\"+img)  \n    else:\n        os.makedirs(test_path + class_name)\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:01:29.706125Z","iopub.status.busy":"2022-07-10T19:01:29.705657Z","iopub.status.idle":"2022-07-10T19:03:43.909497Z","shell.execute_reply":"2022-07-10T19:03:43.908512Z"},"papermill":{"duration":134.320594,"end_time":"2022-07-10T19:03:43.912192","exception":false,"start_time":"2022-07-10T19:01:29.591598","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal_datagen = ImageDataGenerator(rescale=1 / 255.0)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:44.051177Z","iopub.status.busy":"2022-07-10T19:03:44.050821Z","iopub.status.idle":"2022-07-10T19:03:44.055797Z","shell.execute_reply":"2022-07-10T19:03:44.054859Z"},"papermill":{"duration":0.077955,"end_time":"2022-07-10T19:03:44.057705","exception":false,"start_time":"2022-07-10T19:03:43.97975","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example=\"imgs/train/c0/img_10003.jpg\"\nimage=cv2.imread(example)\n\nimage_resize=cv2.resize(image,(224,224))\nimg=np.array(image_resize).reshape(1,224,224,3)\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:44.195651Z","iopub.status.busy":"2022-07-10T19:03:44.195272Z","iopub.status.idle":"2022-07-10T19:03:44.209242Z","shell.execute_reply":"2022-07-10T19:03:44.208393Z"},"papermill":{"duration":0.085974,"end_time":"2022-07-10T19:03:44.211469","exception":false,"start_time":"2022-07-10T19:03:44.125495","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = os.path.join( 'imgs', 'train', 'c0', 'img_104.jpg')\nimport matplotlib.image as mpimg\nimg=mpimg.imread(path)\nresized = cv2.resize(img, (100, 80), cv2.INTER_LINEAR)\nplt.title('c0')\nplt.imshow(img)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized = cv2.resize(img, (224, 224), cv2.INTER_LINEAR)\nplt.title('c0')\nplt.imshow(resized)\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized1 = resized.astype('float32')\nmean = [103.939,116.779,123.68]\nresized1[:, :, 0] -= mean[0]   \nresized1[:, :, 1] -= mean[1]\nresized1[:, :, 2] -= mean[2]\nplt.title('c0')\nplt.imshow(resized1)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Quality","metadata":{}},{"cell_type":"code","source":"sewar.full_ref.mse(resized, resized1)\n  #mse est trop loin du 1 et 0 elle n'a pas d'influence ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sewar.full_ref.rmse(resized, resized1)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_1=[0]\ny_1=to_categorical(y_1,10)\ny_1=np.array(y_1).reshape(1,-1)\nprint(y_1)\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:44.351186Z","iopub.status.busy":"2022-07-10T19:03:44.350135Z","iopub.status.idle":"2022-07-10T19:03:44.358497Z","shell.execute_reply":"2022-07-10T19:03:44.357024Z"},"papermill":{"duration":0.082019,"end_time":"2022-07-10T19:03:44.361759","exception":false,"start_time":"2022-07-10T19:03:44.27974","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"histb = cv2.calcHist([resized],[0],None,[256],[0,256])\nhistg = cv2.calcHist([resized],[1],None,[256],[0,256])\nhistr = cv2.calcHist([resized],[2],None,[256],[0,256])\nplt.plot(histb, \"b\",label='B')\nplt.plot(histg, \"g\",label='G')\nplt.plot(histr, \"r\",label='R')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  Training The data Using Different Approaches","metadata":{"papermill":{"duration":0.07548,"end_time":"2022-07-10T19:03:45.431528","exception":false,"start_time":"2022-07-10T19:03:45.356048","status":"completed"},"tags":[]}},{"cell_type":"code","source":"epochs = 50\nbatchs = 32","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:45.581805Z","iopub.status.busy":"2022-07-10T19:03:45.581328Z","iopub.status.idle":"2022-07-10T19:03:45.585954Z","shell.execute_reply":"2022-07-10T19:03:45.585012Z"},"papermill":{"duration":0.083031,"end_time":"2022-07-10T19:03:45.588155","exception":false,"start_time":"2022-07-10T19:03:45.505124","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=normal_datagen.flow_from_directory(\n    directory=train_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\nvalid_generator=normal_datagen.flow_from_directory(\n    directory=valid_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\n\ntest_generator=normal_datagen.flow_from_directory(\n    directory=test_path,\n    target_size=(224, 224),\n    batch_size=1,\n    class_mode=\"categorical\",\n    shuffle=True\n)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:45.739893Z","iopub.status.busy":"2022-07-10T19:03:45.73954Z","iopub.status.idle":"2022-07-10T19:03:46.509245Z","shell.execute_reply":"2022-07-10T19:03:46.507412Z"},"papermill":{"duration":0.85062,"end_time":"2022-07-10T19:03:46.512172","exception":false,"start_time":"2022-07-10T19:03:45.661552","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks_ = [callbacks.EarlyStopping(monitor='val_loss', mode='min',patience=3)]","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:46.666744Z","iopub.status.busy":"2022-07-10T19:03:46.666341Z","iopub.status.idle":"2022-07-10T19:03:46.671185Z","shell.execute_reply":"2022-07-10T19:03:46.670297Z"},"papermill":{"duration":0.085044,"end_time":"2022-07-10T19:03:46.673541","exception":false,"start_time":"2022-07-10T19:03:46.588497","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 1. Buiding Basline Dense Model","metadata":{"papermill":{"duration":0.074702,"end_time":"2022-07-10T19:03:46.820993","exception":false,"start_time":"2022-07-10T19:03:46.746291","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dense_model=models.Sequential()\ndense_model.add(layers.Flatten(input_shape=(224,224,3)))\ndense_model.add(layers.Dense(1024, activation='relu', name='Layer_1'))\ndense_model.add(layers.Dense(512, activation='relu', name='Layer_2'))\ndense_model.add(layers.Dense(512, activation='relu', name='Layer_3'))\ndense_model.add(layers.Dense(256, activation='relu', name='Layer_4'))\ndense_model.add(layers.Dense(128, activation='relu', name='Layer_5'))\ndense_model.add(layers.Dense(10, activation='softmax',name='final_layer'))","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:46.984468Z","iopub.status.busy":"2022-07-10T19:03:46.983919Z","iopub.status.idle":"2022-07-10T19:03:49.920093Z","shell.execute_reply":"2022-07-10T19:03:49.919093Z"},"papermill":{"duration":3.026787,"end_time":"2022-07-10T19:03:49.922979","exception":false,"start_time":"2022-07-10T19:03:46.896192","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_model.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:50.076914Z","iopub.status.busy":"2022-07-10T19:03:50.075848Z","iopub.status.idle":"2022-07-10T19:03:50.083073Z","shell.execute_reply":"2022-07-10T19:03:50.081827Z"},"papermill":{"duration":0.089092,"end_time":"2022-07-10T19:03:50.0865","exception":false,"start_time":"2022-07-10T19:03:49.997408","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pydot","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install graphviz","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install pydotplus\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ndense_model.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:51.376806Z","iopub.status.busy":"2022-07-10T19:03:51.375777Z","iopub.status.idle":"2022-07-10T19:03:51.393956Z","shell.execute_reply":"2022-07-10T19:03:51.392935Z"},"papermill":{"duration":0.104577,"end_time":"2022-07-10T19:03:51.396322","exception":false,"start_time":"2022-07-10T19:03:51.291745","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_1=dense_model.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=5,\n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:03:51.558538Z","iopub.status.busy":"2022-07-10T19:03:51.558178Z","iopub.status.idle":"2022-07-10T19:15:52.613935Z","shell.execute_reply":"2022-07-10T19:15:52.612829Z"},"papermill":{"duration":721.379308,"end_time":"2022-07-10T19:15:52.854508","exception":false,"start_time":"2022-07-10T19:03:51.4752","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = dense_model.evaluate(test_generator)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:15:53.564578Z","iopub.status.busy":"2022-07-10T19:15:53.564142Z","iopub.status.idle":"2022-07-10T19:16:34.567024Z","shell.execute_reply":"2022-07-10T19:16:34.565947Z"},"papermill":{"duration":41.384108,"end_time":"2022-07-10T19:16:34.56966","exception":false,"start_time":"2022-07-10T19:15:53.185552","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_1.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_1.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:16:35.132136Z","iopub.status.busy":"2022-07-10T19:16:35.131747Z","iopub.status.idle":"2022-07-10T19:16:35.347645Z","shell.execute_reply":"2022-07-10T19:16:35.346743Z"},"papermill":{"duration":0.521347,"end_time":"2022-07-10T19:16:35.349929","exception":false,"start_time":"2022-07-10T19:16:34.828582","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_1.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_1.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:16:35.873421Z","iopub.status.busy":"2022-07-10T19:16:35.873045Z","iopub.status.idle":"2022-07-10T19:16:36.078004Z","shell.execute_reply":"2022-07-10T19:16:36.077141Z"},"papermill":{"duration":0.467425,"end_time":"2022-07-10T19:16:36.080271","exception":false,"start_time":"2022-07-10T19:16:35.612846","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=dense_model.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:16:36.602903Z","iopub.status.busy":"2022-07-10T19:16:36.602508Z","iopub.status.idle":"2022-07-10T19:16:40.361199Z","shell.execute_reply":"2022-07-10T19:16:40.36034Z"},"papermill":{"duration":4.061549,"end_time":"2022-07-10T19:16:40.402372","exception":false,"start_time":"2022-07-10T19:16:36.340823","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n#### 2. Building Baseline CNN Model","metadata":{"papermill":{"duration":0.311111,"end_time":"2022-07-10T19:16:41.010211","exception":false,"start_time":"2022-07-10T19:16:40.6991","status":"completed"},"tags":[]}},{"cell_type":"code","source":"cnn_model = models.Sequential()\ncnn_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224,3)))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Flatten())\ncnn_model.add(layers.Dense(128, activation='relu'))\ncnn_model.add(layers.Dense(10, activation='softmax'))\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:16:41.659912Z","iopub.status.busy":"2022-07-10T19:16:41.659411Z","iopub.status.idle":"2022-07-10T19:16:41.730209Z","shell.execute_reply":"2022-07-10T19:16:41.729304Z"},"papermill":{"duration":0.41805,"end_time":"2022-07-10T19:16:41.732427","exception":false,"start_time":"2022-07-10T19:16:41.314377","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:16:42.335295Z","iopub.status.busy":"2022-07-10T19:16:42.334194Z","iopub.status.idle":"2022-07-10T19:16:42.341515Z","shell.execute_reply":"2022-07-10T19:16:42.340569Z"},"papermill":{"duration":0.311691,"end_time":"2022-07-10T19:16:42.344189","exception":false,"start_time":"2022-07-10T19:16:42.032498","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ncnn_model.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:16:43.718257Z","iopub.status.busy":"2022-07-10T19:16:43.717767Z","iopub.status.idle":"2022-07-10T19:16:43.731823Z","shell.execute_reply":"2022-07-10T19:16:43.730929Z"},"papermill":{"duration":0.333276,"end_time":"2022-07-10T19:16:43.733805","exception":false,"start_time":"2022-07-10T19:16:43.400529","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_2=cnn_model.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=5, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:16:44.350872Z","iopub.status.busy":"2022-07-10T19:16:44.350371Z","iopub.status.idle":"2022-07-10T19:26:23.69878Z","shell.execute_reply":"2022-07-10T19:26:23.697831Z"},"papermill":{"duration":580.092951,"end_time":"2022-07-10T19:26:24.13832","exception":false,"start_time":"2022-07-10T19:16:44.045369","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = cnn_model.evaluate(test_generator)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:26:25.041761Z","iopub.status.busy":"2022-07-10T19:26:25.041231Z","iopub.status.idle":"2022-07-10T19:26:49.517293Z","shell.execute_reply":"2022-07-10T19:26:49.516281Z"},"papermill":{"duration":24.907836,"end_time":"2022-07-10T19:26:49.51948","exception":false,"start_time":"2022-07-10T19:26:24.611644","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_2.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_2.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:26:50.432546Z","iopub.status.busy":"2022-07-10T19:26:50.432109Z","iopub.status.idle":"2022-07-10T19:26:50.645122Z","shell.execute_reply":"2022-07-10T19:26:50.643978Z"},"papermill":{"duration":0.672116,"end_time":"2022-07-10T19:26:50.647169","exception":false,"start_time":"2022-07-10T19:26:49.975053","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_2.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_2.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:26:51.618619Z","iopub.status.busy":"2022-07-10T19:26:51.618158Z","iopub.status.idle":"2022-07-10T19:26:51.822907Z","shell.execute_reply":"2022-07-10T19:26:51.821982Z"},"papermill":{"duration":0.718096,"end_time":"2022-07-10T19:26:51.825175","exception":false,"start_time":"2022-07-10T19:26:51.107079","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=cnn_model.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:26:52.747879Z","iopub.status.busy":"2022-07-10T19:26:52.747347Z","iopub.status.idle":"2022-07-10T19:26:56.912562Z","shell.execute_reply":"2022-07-10T19:26:56.911613Z"},"papermill":{"duration":4.662405,"end_time":"2022-07-10T19:26:56.949408","exception":false,"start_time":"2022-07-10T19:26:52.287003","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CNN visualization","metadata":{"papermill":{"duration":0.574827,"end_time":"2022-07-10T19:26:58.025303","exception":false,"start_time":"2022-07-10T19:26:57.450476","status":"completed"},"tags":[]}},{"cell_type":"code","source":"example=\"imgs/train/c0/img_10003.jpg\"\nimage=cv2.imread(example)\nimage_resize=cv2.resize(image,(224,224))\nimg=np.array(image_resize).reshape(1,224,224,3)\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:26:59.033688Z","iopub.status.busy":"2022-07-10T19:26:59.033183Z","iopub.status.idle":"2022-07-10T19:26:59.052494Z","shell.execute_reply":"2022-07-10T19:26:59.05165Z"},"papermill":{"duration":0.526439,"end_time":"2022-07-10T19:26:59.054422","exception":false,"start_time":"2022-07-10T19:26:58.527983","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracts the outputs of the top 7 layers:\nlayer_outputs = [layer.output for layer in cnn_model.layers[:7]]\n\n# Creates a model that will return these outputs, given the model input:\nactivation_model = models.Model(inputs=cnn_model.input, outputs=layer_outputs)\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:00.062481Z","iopub.status.busy":"2022-07-10T19:27:00.061946Z","iopub.status.idle":"2022-07-10T19:27:00.071844Z","shell.execute_reply":"2022-07-10T19:27:00.070978Z"},"papermill":{"duration":0.513452,"end_time":"2022-07-10T19:27:00.073858","exception":false,"start_time":"2022-07-10T19:26:59.560406","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activation_model.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:01.131535Z","iopub.status.busy":"2022-07-10T19:27:01.130954Z","iopub.status.idle":"2022-07-10T19:27:01.139358Z","shell.execute_reply":"2022-07-10T19:27:01.138251Z"},"papermill":{"duration":0.533422,"end_time":"2022-07-10T19:27:01.143152","exception":false,"start_time":"2022-07-10T19:27:00.60973","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activations = activation_model.predict(img)\nprint(len(activations))","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:02.154301Z","iopub.status.busy":"2022-07-10T19:27:02.153811Z","iopub.status.idle":"2022-07-10T19:27:02.301564Z","shell.execute_reply":"2022-07-10T19:27:02.299911Z"},"papermill":{"duration":0.654961,"end_time":"2022-07-10T19:27:02.304405","exception":false,"start_time":"2022-07-10T19:27:01.649444","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# These are the names of the layers, so can have them as part of our plot\nlayer_names = []\nfor layer in cnn_model.layers[:7]:\n    layer_names.append(layer.name)\n\nimages_per_row = 16\n\n# Now let's display our feature maps\nfor layer_name, layer_activation in zip(layer_names, activations):\n    # This is the number of features in the feature map\n    n_features = layer_activation.shape[-1]\n\n    # The feature map has shape (1, size, size, n_features)\n    size = layer_activation.shape[1]\n\n    # We will tile the activation channels in this matrix\n    n_cols = n_features // images_per_row\n    display_grid = np.zeros((size * n_cols, images_per_row * size))\n\n    # We'll tile each filter into this big horizontal grid\n    for col in range(n_cols):\n        for row in range(images_per_row):\n            channel_image = layer_activation[0,\n                                             :, :,\n                                             col * images_per_row + row]\n            # Post-process the feature to make it visually palatable\n            channel_image -= channel_image.mean()\n            channel_image /= channel_image.std()\n            channel_image *= 64\n            channel_image += 128\n            channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n            display_grid[col * size : (col + 1) * size,\n                         row * size : (row + 1) * size] = channel_image\n\n    # Display the grid\n    scale = 1. / size\n    plt.figure(figsize=(scale * display_grid.shape[1],\n                        scale * display_grid.shape[0]))\n    plt.title(layer_name)\n    plt.grid(False)\n    plt.imshow(display_grid, aspect='auto', cmap='viridis')\n    \nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:03.334819Z","iopub.status.busy":"2022-07-10T19:27:03.33428Z","iopub.status.idle":"2022-07-10T19:27:05.88749Z","shell.execute_reply":"2022-07-10T19:27:05.88657Z"},"papermill":{"duration":3.088779,"end_time":"2022-07-10T19:27:05.890387","exception":false,"start_time":"2022-07-10T19:27:02.801608","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 3. Using Transfer Learning ","metadata":{"papermill":{"duration":0.52711,"end_time":"2022-07-10T19:27:06.995594","exception":false,"start_time":"2022-07-10T19:27:06.468484","status":"completed"},"tags":[]}},{"cell_type":"code","source":"conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:08.070885Z","iopub.status.busy":"2022-07-10T19:27:08.070285Z","iopub.status.idle":"2022-07-10T19:27:10.104212Z","shell.execute_reply":"2022-07-10T19:27:10.102896Z"},"papermill":{"duration":2.581222,"end_time":"2022-07-10T19:27:10.107871","exception":false,"start_time":"2022-07-10T19:27:07.526649","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:11.70789Z","iopub.status.busy":"2022-07-10T19:27:11.707526Z","iopub.status.idle":"2022-07-10T19:27:11.733728Z","shell.execute_reply":"2022-07-10T19:27:11.732759Z"},"papermill":{"duration":0.731054,"end_time":"2022-07-10T19:27:11.741956","exception":false,"start_time":"2022-07-10T19:27:11.010902","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable=False","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:12.858302Z","iopub.status.busy":"2022-07-10T19:27:12.857763Z","iopub.status.idle":"2022-07-10T19:27:12.870308Z","shell.execute_reply":"2022-07-10T19:27:12.869158Z"},"papermill":{"duration":0.544235,"end_time":"2022-07-10T19:27:12.872517","exception":false,"start_time":"2022-07-10T19:27:12.328282","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model = models.Sequential()\nres_model.add(conv_base)\nres_model.add(layers.Flatten())\nres_model.add(layers.Dense(128, activation='relu'))\nres_model.add(layers.Dense(64, activation='relu'))\nres_model.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:13.94741Z","iopub.status.busy":"2022-07-10T19:27:13.946825Z","iopub.status.idle":"2022-07-10T19:27:14.341687Z","shell.execute_reply":"2022-07-10T19:27:14.340735Z"},"papermill":{"duration":0.936327,"end_time":"2022-07-10T19:27:14.344027","exception":false,"start_time":"2022-07-10T19:27:13.4077","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:15.741429Z","iopub.status.busy":"2022-07-10T19:27:15.741066Z","iopub.status.idle":"2022-07-10T19:27:15.758222Z","shell.execute_reply":"2022-07-10T19:27:15.757165Z"},"papermill":{"duration":0.829501,"end_time":"2022-07-10T19:27:15.760765","exception":false,"start_time":"2022-07-10T19:27:14.931264","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\nres_model.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:18.069537Z","iopub.status.busy":"2022-07-10T19:27:18.067109Z","iopub.status.idle":"2022-07-10T19:27:18.083528Z","shell.execute_reply":"2022-07-10T19:27:18.082517Z"},"papermill":{"duration":0.550103,"end_time":"2022-07-10T19:27:18.085818","exception":false,"start_time":"2022-07-10T19:27:17.535715","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_3=res_model.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=5, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:27:19.221108Z","iopub.status.busy":"2022-07-10T19:27:19.220414Z","iopub.status.idle":"2022-07-10T19:48:16.130733Z","shell.execute_reply":"2022-07-10T19:48:16.129747Z"},"papermill":{"duration":1257.521633,"end_time":"2022-07-10T19:48:16.133228","exception":false,"start_time":"2022-07-10T19:27:18.611595","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = res_model.evaluate(test_generator)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:48:17.755327Z","iopub.status.busy":"2022-07-10T19:48:17.754884Z","iopub.status.idle":"2022-07-10T19:49:12.547687Z","shell.execute_reply":"2022-07-10T19:49:12.546732Z"},"papermill":{"duration":55.641122,"end_time":"2022-07-10T19:49:12.549875","exception":false,"start_time":"2022-07-10T19:48:16.908753","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_3.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_3.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:49:14.299094Z","iopub.status.busy":"2022-07-10T19:49:14.298704Z","iopub.status.idle":"2022-07-10T19:49:14.517009Z","shell.execute_reply":"2022-07-10T19:49:14.516086Z"},"papermill":{"duration":1.121952,"end_time":"2022-07-10T19:49:14.519292","exception":false,"start_time":"2022-07-10T19:49:13.39734","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_2.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_2.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:49:16.521336Z","iopub.status.busy":"2022-07-10T19:49:16.520913Z","iopub.status.idle":"2022-07-10T19:49:16.721923Z","shell.execute_reply":"2022-07-10T19:49:16.721042Z"},"papermill":{"duration":1.091027,"end_time":"2022-07-10T19:49:16.723942","exception":false,"start_time":"2022-07-10T19:49:15.632915","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=res_model.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T19:49:18.499772Z","iopub.status.busy":"2022-07-10T19:49:18.499426Z","iopub.status.idle":"2022-07-10T19:49:23.381169Z","shell.execute_reply":"2022-07-10T19:49:23.380116Z"},"papermill":{"duration":5.855201,"end_time":"2022-07-10T19:49:23.417584","exception":false,"start_time":"2022-07-10T19:49:17.562383","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# Training The data Using Different Approaches With Data Augmentation","metadata":{"papermill":{"duration":1.226442,"end_time":"2022-07-10T20:12:12.571467","exception":false,"start_time":"2022-07-10T20:12:11.345025","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Preparing The Data","metadata":{"papermill":{"duration":1.290926,"end_time":"2022-07-10T20:12:15.144041","exception":false,"start_time":"2022-07-10T20:12:13.853115","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1./255, # ImageDataGenerator and usually it is used with rescaling factor 1./255 to rescale the initial values from 0 to 255 to 0 to 1 instead.\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    rotation_range=20,\n    fill_mode='nearest'\n)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:12:17.843825Z","iopub.status.busy":"2022-07-10T20:12:17.843323Z","iopub.status.idle":"2022-07-10T20:12:17.853837Z","shell.execute_reply":"2022-07-10T20:12:17.852757Z"},"papermill":{"duration":1.325553,"end_time":"2022-07-10T20:12:17.856847","exception":false,"start_time":"2022-07-10T20:12:16.531294","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_one_image_generator= train_datagen.flow(\n    img,\n    y_1,\n    batch_size=32\n)\n\nplt.figure(figsize=(12, 12))\nfor i in range(0, 9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in train_one_image_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0])))))\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:12:20.444069Z","iopub.status.busy":"2022-07-10T20:12:20.443541Z","iopub.status.idle":"2022-07-10T20:12:22.359748Z","shell.execute_reply":"2022-07-10T20:12:22.358318Z"},"papermill":{"duration":3.242779,"end_time":"2022-07-10T20:12:22.373435","exception":false,"start_time":"2022-07-10T20:12:19.130656","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=train_datagen.flow_from_directory(\n    directory=train_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\nvalid_generator=test_datagen.flow_from_directory(\n    directory=valid_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\n\ntest_generator=test_datagen.flow_from_directory(\n    directory=test_path,\n    target_size=(224, 224),\n    batch_size=1,\n    class_mode=\"categorical\",\n    shuffle=True\n)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:12:25.290374Z","iopub.status.busy":"2022-07-10T20:12:25.289874Z","iopub.status.idle":"2022-07-10T20:12:25.953206Z","shell.execute_reply":"2022-07-10T20:12:25.95196Z"},"papermill":{"duration":2.03558,"end_time":"2022-07-10T20:12:25.955934","exception":false,"start_time":"2022-07-10T20:12:23.920354","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 1. Buiding Basline Dense Model","metadata":{"papermill":{"duration":1.303738,"end_time":"2022-07-10T20:12:28.511646","exception":false,"start_time":"2022-07-10T20:12:27.207908","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dense_model_2=models.Sequential()\ndense_model_2.add(layers.Flatten(input_shape=(224,224,3)))\ndense_model_2.add(layers.Dense(1024, activation='relu', name='Layer_1'))\ndense_model_2.add(layers.Dense(512, activation='relu', name='Layer_2'))\ndense_model_2.add(layers.Dense(512, activation='relu', name='Layer_3'))\ndense_model_2.add(layers.Dense(256, activation='relu', name='Layer_4'))\ndense_model_2.add(layers.Dense(256, activation='relu', name='Layer_5'))\ndense_model_2.add(layers.Dense(128, activation='relu', name='Layer_6'))\ndense_model_2.add(layers.Dense(128, activation='relu', name='Layer_7'))\ndense_model_2.add(layers.Dense(10, activation='softmax',name='final_layer'))","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:12:31.226322Z","iopub.status.busy":"2022-07-10T20:12:31.225822Z","iopub.status.idle":"2022-07-10T20:12:31.294398Z","shell.execute_reply":"2022-07-10T20:12:31.293522Z"},"papermill":{"duration":1.381639,"end_time":"2022-07-10T20:12:31.296325","exception":false,"start_time":"2022-07-10T20:12:29.914686","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_model_2.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:12:34.616044Z","iopub.status.busy":"2022-07-10T20:12:34.615675Z","iopub.status.idle":"2022-07-10T20:12:34.622713Z","shell.execute_reply":"2022-07-10T20:12:34.621654Z"},"papermill":{"duration":1.396328,"end_time":"2022-07-10T20:12:34.625981","exception":false,"start_time":"2022-07-10T20:12:33.229653","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ndense_model_2.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:12:37.307219Z","iopub.status.busy":"2022-07-10T20:12:37.306721Z","iopub.status.idle":"2022-07-10T20:12:37.321542Z","shell.execute_reply":"2022-07-10T20:12:37.320601Z"},"papermill":{"duration":1.306471,"end_time":"2022-07-10T20:12:37.323489","exception":false,"start_time":"2022-07-10T20:12:36.017018","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_5=dense_model_2.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=5, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:12:39.857571Z","iopub.status.busy":"2022-07-10T20:12:39.857201Z","iopub.status.idle":"2022-07-10T20:51:38.64881Z","shell.execute_reply":"2022-07-10T20:51:38.647766Z"},"papermill":{"duration":2340.096754,"end_time":"2022-07-10T20:51:38.651236","exception":false,"start_time":"2022-07-10T20:12:38.554482","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = dense_model_2.evaluate(test_generator)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:51:42.022998Z","iopub.status.busy":"2022-07-10T20:51:42.022639Z","iopub.status.idle":"2022-07-10T20:52:23.019715Z","shell.execute_reply":"2022-07-10T20:52:23.018662Z"},"papermill":{"duration":42.765457,"end_time":"2022-07-10T20:52:23.022696","exception":false,"start_time":"2022-07-10T20:51:40.257239","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_5.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_5.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:52:26.370666Z","iopub.status.busy":"2022-07-10T20:52:26.370289Z","iopub.status.idle":"2022-07-10T20:52:26.598388Z","shell.execute_reply":"2022-07-10T20:52:26.597331Z"},"papermill":{"duration":1.973623,"end_time":"2022-07-10T20:52:26.600787","exception":false,"start_time":"2022-07-10T20:52:24.627164","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_5.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_5.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:52:30.287785Z","iopub.status.busy":"2022-07-10T20:52:30.287256Z","iopub.status.idle":"2022-07-10T20:52:30.614306Z","shell.execute_reply":"2022-07-10T20:52:30.613286Z"},"papermill":{"duration":2.257797,"end_time":"2022-07-10T20:52:30.617546","exception":false,"start_time":"2022-07-10T20:52:28.359749","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=dense_model_2.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:52:34.546937Z","iopub.status.busy":"2022-07-10T20:52:34.546329Z","iopub.status.idle":"2022-07-10T20:52:38.5396Z","shell.execute_reply":"2022-07-10T20:52:38.53861Z"},"papermill":{"duration":6.208781,"end_time":"2022-07-10T20:52:38.578188","exception":false,"start_time":"2022-07-10T20:52:32.369407","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 2. Building Baseline CNN Model","metadata":{"papermill":{"duration":1.972433,"end_time":"2022-07-10T20:52:42.327233","exception":false,"start_time":"2022-07-10T20:52:40.3548","status":"completed"},"tags":[]}},{"cell_type":"code","source":"cnn_model_2 = models.Sequential()\ncnn_model_2.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224,3)))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Flatten())\ncnn_model_2.add(layers.Dense(128, activation='relu'))\ncnn_model_2.add(layers.Dense(10, activation='softmax'))\n","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:52:46.031095Z","iopub.status.busy":"2022-07-10T20:52:46.030682Z","iopub.status.idle":"2022-07-10T20:52:46.101905Z","shell.execute_reply":"2022-07-10T20:52:46.100913Z"},"papermill":{"duration":2.001735,"end_time":"2022-07-10T20:52:46.104341","exception":false,"start_time":"2022-07-10T20:52:44.102606","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model_2.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:52:49.636991Z","iopub.status.busy":"2022-07-10T20:52:49.636129Z","iopub.status.idle":"2022-07-10T20:52:49.643381Z","shell.execute_reply":"2022-07-10T20:52:49.642369Z"},"papermill":{"duration":1.797807,"end_time":"2022-07-10T20:52:49.64642","exception":false,"start_time":"2022-07-10T20:52:47.848613","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ncnn_model_2.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:52:57.337282Z","iopub.status.busy":"2022-07-10T20:52:57.336474Z","iopub.status.idle":"2022-07-10T20:52:57.352293Z","shell.execute_reply":"2022-07-10T20:52:57.351363Z"},"papermill":{"duration":1.818072,"end_time":"2022-07-10T20:52:57.354484","exception":false,"start_time":"2022-07-10T20:52:55.536412","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_6=cnn_model_2.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=5, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T20:53:00.838101Z","iopub.status.busy":"2022-07-10T20:53:00.837193Z","iopub.status.idle":"2022-07-10T21:35:18.088418Z","shell.execute_reply":"2022-07-10T21:35:18.087274Z"},"papermill":{"duration":2538.960459,"end_time":"2022-07-10T21:35:18.091239","exception":false,"start_time":"2022-07-10T20:52:59.13078","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = cnn_model_2.evaluate(test_generator)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:35:22.05597Z","iopub.status.busy":"2022-07-10T21:35:22.055547Z","iopub.status.idle":"2022-07-10T21:35:46.936385Z","shell.execute_reply":"2022-07-10T21:35:46.935101Z"},"papermill":{"duration":26.871576,"end_time":"2022-07-10T21:35:46.9391","exception":false,"start_time":"2022-07-10T21:35:20.067524","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_6.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_6.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:35:50.96589Z","iopub.status.busy":"2022-07-10T21:35:50.965336Z","iopub.status.idle":"2022-07-10T21:35:51.185032Z","shell.execute_reply":"2022-07-10T21:35:51.184151Z"},"papermill":{"duration":2.100952,"end_time":"2022-07-10T21:35:51.187259","exception":false,"start_time":"2022-07-10T21:35:49.086307","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_6.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_6.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:35:55.168594Z","iopub.status.busy":"2022-07-10T21:35:55.167989Z","iopub.status.idle":"2022-07-10T21:35:55.396096Z","shell.execute_reply":"2022-07-10T21:35:55.395204Z"},"papermill":{"duration":2.217992,"end_time":"2022-07-10T21:35:55.398564","exception":false,"start_time":"2022-07-10T21:35:53.180572","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=cnn_model_2.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:35:59.544478Z","iopub.status.busy":"2022-07-10T21:35:59.54406Z","iopub.status.idle":"2022-07-10T21:36:03.437723Z","shell.execute_reply":"2022-07-10T21:36:03.436809Z"},"papermill":{"duration":5.854967,"end_time":"2022-07-10T21:36:03.472365","exception":false,"start_time":"2022-07-10T21:35:57.617398","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CNN visualization","metadata":{"papermill":{"duration":2.025895,"end_time":"2022-07-10T21:36:07.51845","exception":false,"start_time":"2022-07-10T21:36:05.492555","status":"completed"},"tags":[]}},{"cell_type":"code","source":"example=\"imgs/train/c0/img_10003.jpg\"\nimage=cv2.imread(example)\nimage_resize=cv2.resize(image,(224,224))\nimg=np.array(image_resize).reshape(1,224,224,3)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:36:11.883243Z","iopub.status.busy":"2022-07-10T21:36:11.882748Z","iopub.status.idle":"2022-07-10T21:36:11.924758Z","shell.execute_reply":"2022-07-10T21:36:11.923795Z"},"papermill":{"duration":2.102437,"end_time":"2022-07-10T21:36:11.926859","exception":false,"start_time":"2022-07-10T21:36:09.824422","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracts the outputs of the top 7 layers:\nlayer_outputs = [layer.output for layer in cnn_model_2.layers[:7]]\n\n# Creates a model that will return these outputs, given the model input:\nactivation_model = models.Model(inputs=cnn_model_2.input, outputs=layer_outputs)","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:36:15.920536Z","iopub.status.busy":"2022-07-10T21:36:15.91999Z","iopub.status.idle":"2022-07-10T21:36:15.930235Z","shell.execute_reply":"2022-07-10T21:36:15.929284Z"},"papermill":{"duration":2.067018,"end_time":"2022-07-10T21:36:15.932241","exception":false,"start_time":"2022-07-10T21:36:13.865223","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activation_model.summary()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:36:20.228474Z","iopub.status.busy":"2022-07-10T21:36:20.227942Z","iopub.status.idle":"2022-07-10T21:36:20.241363Z","shell.execute_reply":"2022-07-10T21:36:20.239227Z"},"papermill":{"duration":2.261471,"end_time":"2022-07-10T21:36:20.244059","exception":false,"start_time":"2022-07-10T21:36:17.982588","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activations = activation_model.predict(img)\nprint(len(activations))","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:36:24.323781Z","iopub.status.busy":"2022-07-10T21:36:24.323179Z","iopub.status.idle":"2022-07-10T21:36:24.430123Z","shell.execute_reply":"2022-07-10T21:36:24.427988Z"},"papermill":{"duration":2.147961,"end_time":"2022-07-10T21:36:24.433019","exception":false,"start_time":"2022-07-10T21:36:22.285058","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# These are the names of the layers, so can have them as part of our plot\nlayer_names = []\nfor layer in cnn_model.layers[:7]:\n    layer_names.append(layer.name)\n\nimages_per_row = 16\n\n# Now let's display our feature maps\nfor layer_name, layer_activation in zip(layer_names, activations):\n    # This is the number of features in the feature map\n    n_features = layer_activation.shape[-1]\n\n    # The feature map has shape (1, size, size, n_features)\n    size = layer_activation.shape[1]\n\n    # We will tile the activation channels in this matrix\n    n_cols = n_features // images_per_row\n    display_grid = np.zeros((size * n_cols, images_per_row * size))\n\n    # We'll tile each filter into this big horizontal grid\n    for col in range(n_cols):\n        for row in range(images_per_row):\n            channel_image = layer_activation[0,\n                                             :, :,\n                                             col * images_per_row + row]\n            # Post-process the feature to make it visually palatable\n            channel_image -= channel_image.mean()\n            channel_image /= channel_image.std()\n            channel_image *= 64\n            channel_image += 128\n            channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n            display_grid[col * size : (col + 1) * size,\n                         row * size : (row + 1) * size] = channel_image\n\n    # Display the grid\n    scale = 1. / size\n    plt.figure(figsize=(scale * display_grid.shape[1],\n                        scale * display_grid.shape[0]))\n    plt.title(layer_name)\n    plt.grid(False)\n    plt.imshow(display_grid, aspect='auto', cmap='viridis')\n    \nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-07-10T21:36:28.50012Z","iopub.status.busy":"2022-07-10T21:36:28.499586Z","iopub.status.idle":"2022-07-10T21:36:31.049791Z","shell.execute_reply":"2022-07-10T21:36:31.048875Z"},"papermill":{"duration":4.593193,"end_time":"2022-07-10T21:36:31.052018","exception":false,"start_time":"2022-07-10T21:36:26.458825","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_confusion_matrix(confusion_matrix, class_names, figsize = (10,7), fontsize=14):\n    df_cm = pd.DataFrame(\n        confusion_matrix, index=class_names, columns=class_names, \n    )\n    fig = plt.figure(figsize=figsize)\n    try:\n        heatmap = sns.heatmap(df_cm, annot=True, fmt=\"d\")\n    except ValueError:\n        raise ValueError(\"Confusion matrix values must be integers.\")\n    heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=fontsize)\n    heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=fontsize)\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    fig.savefig(os.path.join(MODEL_PATH,\"confusion_matrix.png\"))\n    return fig\n","metadata":{},"execution_count":null,"outputs":[]}]}