Spaces:
Build error
Build error
Upload satish-notebook.ipynb
Browse files- satish-notebook.ipynb +1 -0
satish-notebook.ipynb
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The data for the project is taken from kaggle https://www.kaggle.com/datasets/mostafaabla/garbage-classification.\n\nIn the working directory I created a folder data with three empty directories train, val and test.\n\nRun the python notebook sequentially.\n\n[](https://forthebadge.com)","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\nimport random\nimport shutil\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"id":"8m9jlVFB2YHb","execution":{"iopub.status.busy":"2022-08-02T05:53:14.507012Z","iopub.execute_input":"2022-08-02T05:53:14.508103Z","iopub.status.idle":"2022-08-02T05:53:19.966512Z","shell.execute_reply.started":"2022-08-02T05:53:14.507952Z","shell.execute_reply":"2022-08-02T05:53:19.965513Z"},"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"!pip install split-folders","metadata":{"id":"1ewTkQJr2YHd","execution":{"iopub.status.busy":"2022-08-02T05:53:20.566789Z","iopub.execute_input":"2022-08-02T05:53:20.567642Z","iopub.status.idle":"2022-08-02T05:53:33.528256Z","shell.execute_reply.started":"2022-08-02T05:53:20.567603Z","shell.execute_reply":"2022-08-02T05:53:33.527036Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"import splitfolders","metadata":{"execution":{"iopub.status.busy":"2022-08-02T05:53:36.496948Z","iopub.execute_input":"2022-08-02T05:53:36.497918Z","iopub.status.idle":"2022-08-02T05:53:36.507831Z","shell.execute_reply.started":"2022-08-02T05:53:36.497882Z","shell.execute_reply":"2022-08-02T05:53:36.506647Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"code","source":"#os.chdir('..')\nos.getcwd()\nos.listdir()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T05:53:37.437175Z","iopub.execute_input":"2022-08-02T05:53:37.437774Z","iopub.status.idle":"2022-08-02T05:53:37.450163Z","shell.execute_reply.started":"2022-08-02T05:53:37.437740Z","shell.execute_reply":"2022-08-02T05:53:37.448875Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"code","source":"os.chdir('..')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T05:53:38.573934Z","iopub.execute_input":"2022-08-02T05:53:38.574629Z","iopub.status.idle":"2022-08-02T05:53:38.579021Z","shell.execute_reply.started":"2022-08-02T05:53:38.574593Z","shell.execute_reply":"2022-08-02T05:53:38.578037Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"code","source":"os.chdir('working')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T05:53:40.007497Z","iopub.execute_input":"2022-08-02T05:53:40.008545Z","iopub.status.idle":"2022-08-02T05:53:40.013851Z","shell.execute_reply.started":"2022-08-02T05:53:40.008496Z","shell.execute_reply":"2022-08-02T05:53:40.012881Z"},"trusted":true},"execution_count":6,"outputs":[]},{"cell_type":"code","source":"os.getcwd()\nos.chdir('data')","metadata":{"id":"BRrePuYb2YHg","outputId":"8cf8554f-eae2-44f1-83ac-9bbcf030462e","execution":{"iopub.status.busy":"2022-08-02T06:01:45.690018Z","iopub.execute_input":"2022-08-02T06:01:45.691007Z","iopub.status.idle":"2022-08-02T06:01:45.696119Z","shell.execute_reply.started":"2022-08-02T06:01:45.690930Z","shell.execute_reply":"2022-08-02T06:01:45.695039Z"},"trusted":true},"execution_count":18,"outputs":[]},{"cell_type":"code","source":"os.mkdir('train')\nos.mkdir('test')\nos.mkdir('val')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T05:53:48.743280Z","iopub.execute_input":"2022-08-02T05:53:48.743941Z","iopub.status.idle":"2022-08-02T05:53:48.749294Z","shell.execute_reply.started":"2022-08-02T05:53:48.743904Z","shell.execute_reply":"2022-08-02T05:53:48.748171Z"},"trusted":true},"execution_count":8,"outputs":[]},{"cell_type":"code","source":"os.chdir('/kaggle/input')\ninput_folder = r'../input/garbage-classification/garbage_classification'\n","metadata":{"id":"A8DG20vp2YHh","outputId":"f45d1903-556e-4593-beb2-d2a4541252a0","_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-02T05:53:52.026798Z","iopub.execute_input":"2022-08-02T05:53:52.027170Z","iopub.status.idle":"2022-08-02T05:53:52.031936Z","shell.execute_reply.started":"2022-08-02T05:53:52.027139Z","shell.execute_reply":"2022-08-02T05:53:52.030931Z"},"trusted":true},"execution_count":9,"outputs":[]},{"cell_type":"code","source":"os.chdir('..')\nprint(os.getcwd())\nos.listdir()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T05:54:01.391575Z","iopub.execute_input":"2022-08-02T05:54:01.391931Z","iopub.status.idle":"2022-08-02T05:54:01.400832Z","shell.execute_reply.started":"2022-08-02T05:54:01.391899Z","shell.execute_reply":"2022-08-02T05:54:01.399641Z"},"trusted":true},"execution_count":10,"outputs":[]},{"cell_type":"code","source":"os.chdir('/kaggle/working')\nsplitfolders.ratio(input_folder, output = 'data', seed = 42, ratio = (.7,.2,.1), group_prefix = None)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:02:00.015052Z","iopub.execute_input":"2022-08-02T06:02:00.015962Z","iopub.status.idle":"2022-08-02T06:03:15.795711Z","shell.execute_reply.started":"2022-08-02T06:02:00.015926Z","shell.execute_reply":"2022-08-02T06:03:15.794749Z"},"trusted":true},"execution_count":20,"outputs":[]},{"cell_type":"code","source":"train_path = 'data/train'\nvalid_path = 'data/val'","metadata":{"id":"cnuRAUN42YHk","execution":{"iopub.status.busy":"2022-08-02T06:03:21.935935Z","iopub.execute_input":"2022-08-02T06:03:21.936302Z","iopub.status.idle":"2022-08-02T06:03:21.941046Z","shell.execute_reply.started":"2022-08-02T06:03:21.936272Z","shell.execute_reply":"2022-08-02T06:03:21.939730Z"},"trusted":true},"execution_count":21,"outputs":[]},{"cell_type":"markdown","source":"**Checking the number of images in the training set and validation set**","metadata":{"id":"5w_75v_12YHk"}},{"cell_type":"code","source":"len(os.listdir(train_path + '/cardboard'))","metadata":{"id":"QkKe1-8a2YHw","outputId":"8d38afb6-3472-4ac7-b199-9a71a5e1c1b3","execution":{"iopub.status.busy":"2022-08-02T06:03:23.400823Z","iopub.execute_input":"2022-08-02T06:03:23.401716Z","iopub.status.idle":"2022-08-02T06:03:23.409799Z","shell.execute_reply.started":"2022-08-02T06:03:23.401668Z","shell.execute_reply":"2022-08-02T06:03:23.408620Z"},"trusted":true},"execution_count":22,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(train_path + '/green-glass'))","metadata":{"id":"t5gXcZzq2YHx","outputId":"19a51b39-52e3-435c-85ec-0bafbb4f3e21","execution":{"iopub.status.busy":"2022-08-02T06:03:25.429533Z","iopub.execute_input":"2022-08-02T06:03:25.429913Z","iopub.status.idle":"2022-08-02T06:03:25.437912Z","shell.execute_reply.started":"2022-08-02T06:03:25.429879Z","shell.execute_reply":"2022-08-02T06:03:25.436656Z"},"trusted":true},"execution_count":23,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(train_path + '/paper'))","metadata":{"id":"RSvBOJP82YHy","outputId":"ece1e416-13e2-48e4-e9df-857e6bd9c99f","execution":{"iopub.status.busy":"2022-08-02T06:03:26.280922Z","iopub.execute_input":"2022-08-02T06:03:26.282077Z","iopub.status.idle":"2022-08-02T06:03:26.290512Z","shell.execute_reply.started":"2022-08-02T06:03:26.282031Z","shell.execute_reply":"2022-08-02T06:03:26.289112Z"},"trusted":true},"execution_count":24,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(train_path + '/plastic'))","metadata":{"id":"AbpJz3Eg2YHy","outputId":"fdc9b41c-4da0-4e29-85bd-ae8857550c44","execution":{"iopub.status.busy":"2022-08-02T06:03:27.074349Z","iopub.execute_input":"2022-08-02T06:03:27.074735Z","iopub.status.idle":"2022-08-02T06:03:27.083618Z","shell.execute_reply.started":"2022-08-02T06:03:27.074704Z","shell.execute_reply":"2022-08-02T06:03:27.082361Z"},"trusted":true},"execution_count":25,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(train_path + '/trash'))","metadata":{"id":"sa4N2xsG2YHz","outputId":"c2df6c6d-e54a-49a9-f093-21bbc5718c14","execution":{"iopub.status.busy":"2022-08-02T06:03:27.823713Z","iopub.execute_input":"2022-08-02T06:03:27.824110Z","iopub.status.idle":"2022-08-02T06:03:27.832112Z","shell.execute_reply.started":"2022-08-02T06:03:27.824077Z","shell.execute_reply":"2022-08-02T06:03:27.830995Z"},"trusted":true},"execution_count":26,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(train_path + '/metal'))","metadata":{"id":"nNhiEhcQ2YH0","outputId":"96cee8f3-1392-48f6-b326-c11b6b36723f","execution":{"iopub.status.busy":"2022-08-02T06:03:28.469607Z","iopub.execute_input":"2022-08-02T06:03:28.470178Z","iopub.status.idle":"2022-08-02T06:03:28.477723Z","shell.execute_reply.started":"2022-08-02T06:03:28.470143Z","shell.execute_reply":"2022-08-02T06:03:28.476622Z"},"trusted":true},"execution_count":27,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(valid_path + '/metal'))","metadata":{"id":"5f9AWkj-2YH2","outputId":"a6767423-50a6-4c37-f3a3-a9b5354c3b99","execution":{"iopub.status.busy":"2022-08-02T06:03:29.186496Z","iopub.execute_input":"2022-08-02T06:03:29.187189Z","iopub.status.idle":"2022-08-02T06:03:29.194036Z","shell.execute_reply.started":"2022-08-02T06:03:29.187151Z","shell.execute_reply":"2022-08-02T06:03:29.193041Z"},"trusted":true},"execution_count":28,"outputs":[]},{"cell_type":"markdown","source":"**Plotting the images**","metadata":{"id":"RwwkP8Op2YH5"}},{"cell_type":"code","source":"## Displaying the first 12 images of metal from the training set\n\nfig = plt.figure(figsize = (15,15))\ncolumns = 4\nrows = 3\ni = 1\n\nfor img_name in os.listdir(train_path+'/metal'):\n img = train_path + '/metal' +'/'+ img_name\n img = plt.imread(img)\n ax = fig.add_subplot(rows, columns, i)\n plt.imshow(img, interpolation = 'nearest')\n ax.title.set_text(img_name)\n i = i+1\n if i == columns*rows+1:\n break\n \nplt.show()","metadata":{"id":"odujC6iw2YH5","outputId":"6184318e-c6b9-41ee-b74d-25025644b9ba","execution":{"iopub.status.busy":"2022-08-02T06:03:30.268295Z","iopub.execute_input":"2022-08-02T06:03:30.269303Z","iopub.status.idle":"2022-08-02T06:03:31.691272Z","shell.execute_reply.started":"2022-08-02T06:03:30.269263Z","shell.execute_reply":"2022-08-02T06:03:31.690431Z"},"trusted":true},"execution_count":29,"outputs":[]},{"cell_type":"code","source":"## Displaying the first 12 images of plastic from the training set\n\nfig = plt.figure(figsize = (15,15))\ncolumns = 4\nrows = 3\ni = 1\n\nfor img_name in os.listdir(train_path+'/plastic'):\n img = train_path + '/plastic/' + img_name\n img = plt.imread(img)\n ax = fig.add_subplot(rows, columns, i)\n plt.imshow(img, interpolation = 'nearest')\n ax.title.set_text(img_name)\n i = i+1\n if i == columns*rows+1:\n break\n \nplt.show()","metadata":{"id":"4vxs37EQ2YIF","outputId":"fe4e3828-cefe-4544-d299-966342a0f08d","execution":{"iopub.status.busy":"2022-08-02T06:03:34.319258Z","iopub.execute_input":"2022-08-02T06:03:34.319623Z","iopub.status.idle":"2022-08-02T06:03:35.942069Z","shell.execute_reply.started":"2022-08-02T06:03:34.319591Z","shell.execute_reply":"2022-08-02T06:03:35.941039Z"},"trusted":true},"execution_count":30,"outputs":[]},{"cell_type":"markdown","source":"**Checking the dimension for each image**","metadata":{"id":"tvlMTOiN2YIK"}},{"cell_type":"code","source":"images = os.listdir(train_path + '/metal/')","metadata":{"id":"Jg857ppc2YIM","execution":{"iopub.status.busy":"2022-08-02T06:03:39.160019Z","iopub.execute_input":"2022-08-02T06:03:39.161219Z","iopub.status.idle":"2022-08-02T06:03:39.166740Z","shell.execute_reply.started":"2022-08-02T06:03:39.161163Z","shell.execute_reply":"2022-08-02T06:03:39.165580Z"},"trusted":true},"execution_count":31,"outputs":[]},{"cell_type":"code","source":"width, height = [], []\n\nfor image_name in images:\n img = plt.imread(train_path+ '/metal/' + image_name)\n w, h, c = img.shape\n width.append(w)\n height.append(h)","metadata":{"id":"kHCc5cD72YIM","execution":{"iopub.status.busy":"2022-08-02T06:03:44.705916Z","iopub.execute_input":"2022-08-02T06:03:44.707015Z","iopub.status.idle":"2022-08-02T06:03:45.672011Z","shell.execute_reply.started":"2022-08-02T06:03:44.706956Z","shell.execute_reply":"2022-08-02T06:03:45.671016Z"},"trusted":true},"execution_count":32,"outputs":[]},{"cell_type":"code","source":"sns.jointplot( x = width, y = height, kind = 'scatter')\nplt.show()","metadata":{"id":"uJ9shobK2YIN","outputId":"4b0488bd-ef57-4b2a-c947-80a0540ac19d","execution":{"iopub.status.busy":"2022-08-02T06:03:46.684369Z","iopub.execute_input":"2022-08-02T06:03:46.684758Z","iopub.status.idle":"2022-08-02T06:03:47.216840Z","shell.execute_reply.started":"2022-08-02T06:03:46.684725Z","shell.execute_reply":"2022-08-02T06:03:47.215885Z"},"trusted":true},"execution_count":33,"outputs":[]},{"cell_type":"markdown","source":"**The images are of varying dimensions, so we will recale to the mean height and width**","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\nprint('Mean width: {}'.format(np.mean(width)))\n\nprint('Mean Height: {}'.format(np.mean(height)))","metadata":{"id":"DINd8ex32YIO","outputId":"a6eecfbd-015a-4371-cd4d-46767ddfe27e","execution":{"iopub.status.busy":"2022-08-02T06:03:48.399373Z","iopub.execute_input":"2022-08-02T06:03:48.399937Z","iopub.status.idle":"2022-08-02T06:03:48.406175Z","shell.execute_reply.started":"2022-08-02T06:03:48.399902Z","shell.execute_reply":"2022-08-02T06:03:48.405119Z"},"trusted":true},"execution_count":34,"outputs":[]},{"cell_type":"markdown","source":"We will keep the size of the image as (298*384 x 3)","metadata":{"id":"QqgG5bWf2YIR"}},{"cell_type":"code","source":"IMAGE_SIZE = (298, 384, 3)","metadata":{"id":"UsXLbD7r2YIT","execution":{"iopub.status.busy":"2022-08-02T06:03:50.079441Z","iopub.execute_input":"2022-08-02T06:03:50.079795Z","iopub.status.idle":"2022-08-02T06:03:50.085023Z","shell.execute_reply.started":"2022-08-02T06:03:50.079764Z","shell.execute_reply":"2022-08-02T06:03:50.083712Z"},"trusted":true},"execution_count":35,"outputs":[]},{"cell_type":"markdown","source":"**Checking images are scaled or not**","metadata":{"id":"VyR11Dz-2YIX"}},{"cell_type":"code","source":"img_name = os.listdir(train_path + '/metal')[0]\nimg = plt.imread(train_path+'/metal/'+img_name)\n\nprint('Maximum pizel value: {}'.format(np.max(img)))\n\nprint('Minimum pizel value: {}'.format(np.min(img)))\n\n## Image is not scaled","metadata":{"id":"aWkKD_pM2YIY","outputId":"6addfb6d-82d4-4a55-c82c-9ba8b2da32a7","execution":{"iopub.status.busy":"2022-08-02T06:03:52.314563Z","iopub.execute_input":"2022-08-02T06:03:52.315223Z","iopub.status.idle":"2022-08-02T06:03:52.328068Z","shell.execute_reply.started":"2022-08-02T06:03:52.315186Z","shell.execute_reply":"2022-08-02T06:03:52.326839Z"},"trusted":true},"execution_count":36,"outputs":[]},{"cell_type":"markdown","source":"**Creating Image Data Generator for Training and Validation set**","metadata":{"id":"CVyzpbow2YIY"}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1./255, rotation_range = 20,\n shear_range = 0.2,\n zoom_range = 0.2,\n horizontal_flip=True)\n\nvalid_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"id":"GVnjgYIy2YIZ","execution":{"iopub.status.busy":"2022-08-02T06:03:53.582468Z","iopub.execute_input":"2022-08-02T06:03:53.583189Z","iopub.status.idle":"2022-08-02T06:03:53.590401Z","shell.execute_reply.started":"2022-08-02T06:03:53.583152Z","shell.execute_reply":"2022-08-02T06:03:53.587653Z"},"trusted":true},"execution_count":37,"outputs":[]},{"cell_type":"code","source":"train_batches = train_datagen.flow_from_directory(directory = train_path,\n target_size = (298, 384),\n classes = ['cardboard', 'metal','paper','plastic','trash','green-glass','white-glass','brown-glass','clothes','biological','battery','shoes'],\n batch_size = 10,\n shuffle = True, class_mode = 'categorical')\n\nvalid_batches = valid_datagen.flow_from_directory(directory = valid_path,\n target_size = (298, 384),\n classes = ['cardboard', 'metal','paper','plastic','trash','green-glass','white-glass','brown-glass','clothes','biological','battery','shoes'],\n batch_size = 10,\n shuffle = False, class_mode = 'categorical')","metadata":{"id":"zBZLVbeG2YIb","outputId":"f908e762-8d11-424f-ecab-37b3009e9e0a","execution":{"iopub.status.busy":"2022-08-02T06:03:54.527038Z","iopub.execute_input":"2022-08-02T06:03:54.527713Z","iopub.status.idle":"2022-08-02T06:03:55.288057Z","shell.execute_reply.started":"2022-08-02T06:03:54.527677Z","shell.execute_reply":"2022-08-02T06:03:55.286992Z"},"trusted":true},"execution_count":38,"outputs":[]},{"cell_type":"code","source":"train_batches.class_indices","metadata":{"id":"a3akZE5Q2YIc","outputId":"53e1b9ac-827c-4f11-c318-25fbdfb18725","execution":{"iopub.status.busy":"2022-08-02T06:03:55.290204Z","iopub.execute_input":"2022-08-02T06:03:55.290607Z","iopub.status.idle":"2022-08-02T06:03:55.300034Z","shell.execute_reply.started":"2022-08-02T06:03:55.290565Z","shell.execute_reply":"2022-08-02T06:03:55.298057Z"},"trusted":true},"execution_count":39,"outputs":[]},{"cell_type":"markdown","source":"**Let us see how the traing batch looks like**","metadata":{"id":"8nRiIYOY2YIc"}},{"cell_type":"code","source":"imgs, labels = next(train_batches) ## getting first 10 images (1st batch)\n\n## Everytime this line of code is run next 10 images are selected","metadata":{"id":"qpXH21nV2YIc","execution":{"iopub.status.busy":"2022-08-02T06:03:56.529668Z","iopub.execute_input":"2022-08-02T06:03:56.530083Z","iopub.status.idle":"2022-08-02T06:03:56.777567Z","shell.execute_reply.started":"2022-08-02T06:03:56.530050Z","shell.execute_reply":"2022-08-02T06:03:56.776598Z"},"trusted":true},"execution_count":40,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 10, figsize = (20,20))\naxes = axes.flatten()\nfor img, ax in zip(imgs, axes):\n ax.imshow(img)\nplt.tight_layout()\nplt.show()","metadata":{"id":"KRoFeCN52YIe","outputId":"9a7ca52b-f389-4ac3-b8e7-0c5d8e9dfceb","execution":{"iopub.status.busy":"2022-08-02T06:03:57.188845Z","iopub.execute_input":"2022-08-02T06:03:57.189820Z","iopub.status.idle":"2022-08-02T06:03:58.280154Z","shell.execute_reply.started":"2022-08-02T06:03:57.189774Z","shell.execute_reply":"2022-08-02T06:03:58.279040Z"},"trusted":true},"execution_count":41,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"id":"zXqW4oWg2YIe","outputId":"a1bff4d3-19b1-47bc-fd49-76ad24cf5f71","execution":{"iopub.status.busy":"2022-08-02T06:03:58.282217Z","iopub.execute_input":"2022-08-02T06:03:58.282677Z","iopub.status.idle":"2022-08-02T06:03:58.291571Z","shell.execute_reply.started":"2022-08-02T06:03:58.282638Z","shell.execute_reply":"2022-08-02T06:03:58.290421Z"},"trusted":true},"execution_count":42,"outputs":[]},{"cell_type":"code","source":"img = imgs[0]\n\nprint('Maximum pixel value: {}'.format(np.max(img)))\n\nprint('Minimum pixel value: {}'.format(np.min(img)))","metadata":{"id":"8eW3X67L2YIf","outputId":"53d8fb24-c5be-4025-8cc3-b9433a6066b5","execution":{"iopub.status.busy":"2022-08-02T06:03:58.293020Z","iopub.execute_input":"2022-08-02T06:03:58.294317Z","iopub.status.idle":"2022-08-02T06:03:58.305100Z","shell.execute_reply.started":"2022-08-02T06:03:58.294255Z","shell.execute_reply":"2022-08-02T06:03:58.303673Z"},"trusted":true},"execution_count":43,"outputs":[]},{"cell_type":"markdown","source":"Note how the image size and maximum and minimum pixel value are changed\n\nNow the images are rescaled!!","metadata":{"id":"jgJyUp1q2YIg"}},{"cell_type":"markdown","source":"**Building CNN model**","metadata":{"id":"uBJKQ2x82YIk"}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Convolution2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Dropout\n\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"id":"qB13aMWo2YIl","execution":{"iopub.status.busy":"2022-08-02T06:04:00.405033Z","iopub.execute_input":"2022-08-02T06:04:00.405392Z","iopub.status.idle":"2022-08-02T06:04:00.413421Z","shell.execute_reply.started":"2022-08-02T06:04:00.405361Z","shell.execute_reply":"2022-08-02T06:04:00.412278Z"},"trusted":true},"execution_count":44,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Convolution2D(filters = 128, kernel_size = (3,3), input_shape = (298,384,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Convolution2D(filters = 128, kernel_size = (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Convolution2D(filters = 64, kernel_size = (3,3), activation = 'relu'))\nmodel.add(Convolution2D(filters = 64, kernel_size = (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(units = 128 , activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(units = 12 , activation = 'softmax'))","metadata":{"id":"vOyvDMdE2YIl","execution":{"iopub.status.busy":"2022-08-02T06:04:01.294698Z","iopub.execute_input":"2022-08-02T06:04:01.295450Z","iopub.status.idle":"2022-08-02T06:04:04.012126Z","shell.execute_reply.started":"2022-08-02T06:04:01.295407Z","shell.execute_reply":"2022-08-02T06:04:04.010845Z"},"trusted":true},"execution_count":45,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam' ,\n loss = 'categorical_crossentropy',\n metrics = ['accuracy'])","metadata":{"id":"CnMOqXYb2YIm","execution":{"iopub.status.busy":"2022-08-02T06:04:04.013958Z","iopub.execute_input":"2022-08-02T06:04:04.014336Z","iopub.status.idle":"2022-08-02T06:04:04.032460Z","shell.execute_reply.started":"2022-08-02T06:04:04.014299Z","shell.execute_reply":"2022-08-02T06:04:04.031273Z"},"trusted":true},"execution_count":46,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"id":"l43Tqz3P2YIn","outputId":"8f7ade70-ed2c-4916-e88f-623a82afcd7f","execution":{"iopub.status.busy":"2022-08-02T06:04:04.456611Z","iopub.execute_input":"2022-08-02T06:04:04.457340Z","iopub.status.idle":"2022-08-02T06:04:04.465914Z","shell.execute_reply.started":"2022-08-02T06:04:04.457293Z","shell.execute_reply":"2022-08-02T06:04:04.464744Z"},"trusted":true},"execution_count":47,"outputs":[]},{"cell_type":"code","source":"r = model.fit(train_batches,\n steps_per_epoch = len(train_batches),\n epochs = 10,\n validation_data = valid_batches,\n validation_steps = len(valid_batches),\n callbacks = EarlyStopping(monitor='val_loss', patience = 2))","metadata":{"id":"11gwWHBt2YIn","outputId":"338d44a4-f65b-4b16-b821-6254260f2c0e","execution":{"iopub.status.busy":"2022-08-02T06:04:05.601829Z","iopub.execute_input":"2022-08-02T06:04:05.602669Z","iopub.status.idle":"2022-08-02T06:51:07.826088Z","shell.execute_reply.started":"2022-08-02T06:04:05.602633Z","shell.execute_reply":"2022-08-02T06:51:07.825044Z"},"trusted":true},"execution_count":48,"outputs":[]},{"cell_type":"code","source":"r.history.keys()","metadata":{"id":"kABo73-QHOlG","outputId":"ca954589-b614-436b-f6d9-f4a30d1e8563","execution":{"iopub.status.busy":"2022-08-02T07:02:31.374791Z","iopub.execute_input":"2022-08-02T07:02:31.375168Z","iopub.status.idle":"2022-08-02T07:02:31.383491Z","shell.execute_reply.started":"2022-08-02T07:02:31.375135Z","shell.execute_reply":"2022-08-02T07:02:31.382116Z"},"trusted":true},"execution_count":49,"outputs":[]},{"cell_type":"code","source":"plt.plot(r.history['accuracy'], label = 'Train')\nplt.plot(r.history['val_accuracy'], label = 'Test')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"id":"ls4w_kKAHH8L","outputId":"fe9894a0-f6f3-4cd1-ff67-a09fb8826856","execution":{"iopub.status.busy":"2022-08-02T07:02:32.307850Z","iopub.execute_input":"2022-08-02T07:02:32.308904Z","iopub.status.idle":"2022-08-02T07:03:18.412113Z","shell.execute_reply.started":"2022-08-02T07:02:32.308855Z","shell.execute_reply":"2022-08-02T07:03:18.411012Z"},"trusted":true},"execution_count":50,"outputs":[]},{"cell_type":"code","source":"model.evaluate(train_batches)\n#train_batches.classes","metadata":{"id":"pTr4I0ML2YIo","outputId":"d8292b40-84f9-4fba-d327-71444ccf54d9","execution":{"iopub.status.busy":"2022-08-02T07:03:18.414203Z","iopub.execute_input":"2022-08-02T07:03:18.415231Z","iopub.status.idle":"2022-08-02T07:08:03.804812Z","shell.execute_reply.started":"2022-08-02T07:03:18.415193Z","shell.execute_reply":"2022-08-02T07:08:03.803734Z"},"trusted":true},"execution_count":51,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_batches)","metadata":{"id":"dmNiuk8XHefP","outputId":"83380cae-6479-4d95-9b23-fcb42eee8da1","execution":{"iopub.status.busy":"2022-08-02T07:08:15.269694Z","iopub.execute_input":"2022-08-02T07:08:15.270083Z","iopub.status.idle":"2022-08-02T07:08:28.402697Z","shell.execute_reply.started":"2022-08-02T07:08:15.270051Z","shell.execute_reply":"2022-08-02T07:08:28.401492Z"},"trusted":true},"execution_count":52,"outputs":[]},{"cell_type":"code","source":"train_pred = model.predict(train_batches)","metadata":{"id":"SPGWurTNIUe8","execution":{"iopub.status.busy":"2022-08-02T07:08:30.838220Z","iopub.execute_input":"2022-08-02T07:08:30.838654Z","iopub.status.idle":"2022-08-02T07:13:09.901610Z","shell.execute_reply.started":"2022-08-02T07:08:30.838620Z","shell.execute_reply":"2022-08-02T07:13:09.900532Z"},"trusted":true},"execution_count":53,"outputs":[]},{"cell_type":"code","source":"train_classes_pred = np.argmax(train_pred, axis = 1)","metadata":{"id":"AyJnm4-aIxT_","execution":{"iopub.status.busy":"2022-08-02T07:16:24.830555Z","iopub.execute_input":"2022-08-02T07:16:24.831071Z","iopub.status.idle":"2022-08-02T07:16:24.841150Z","shell.execute_reply.started":"2022-08-02T07:16:24.831026Z","shell.execute_reply":"2022-08-02T07:16:24.840073Z"},"trusted":true},"execution_count":54,"outputs":[]},{"cell_type":"code","source":"train_classes_pred","metadata":{"id":"tw23Zuc6JOva","outputId":"fd1b696a-18ba-43ee-81a6-6959f62a6922","execution":{"iopub.status.busy":"2022-08-02T07:16:27.528710Z","iopub.execute_input":"2022-08-02T07:16:27.529099Z","iopub.status.idle":"2022-08-02T07:16:27.536644Z","shell.execute_reply.started":"2022-08-02T07:16:27.529066Z","shell.execute_reply":"2022-08-02T07:16:27.535619Z"},"trusted":true},"execution_count":55,"outputs":[]},{"cell_type":"code","source":"train_classes_actual = train_batches.classes","metadata":{"id":"XNkUq7cKJY_3","execution":{"iopub.status.busy":"2022-08-02T07:16:28.116045Z","iopub.execute_input":"2022-08-02T07:16:28.116733Z","iopub.status.idle":"2022-08-02T07:16:28.122297Z","shell.execute_reply.started":"2022-08-02T07:16:28.116696Z","shell.execute_reply":"2022-08-02T07:16:28.121014Z"},"trusted":true},"execution_count":56,"outputs":[]},{"cell_type":"code","source":"train_classes_actual","metadata":{"id":"guelnINTK8g-","outputId":"1285af15-de02-40c6-cbdb-b1c772cce74e","execution":{"iopub.status.busy":"2022-08-02T07:16:31.496758Z","iopub.execute_input":"2022-08-02T07:16:31.497146Z","iopub.status.idle":"2022-08-02T07:16:31.504756Z","shell.execute_reply.started":"2022-08-02T07:16:31.497113Z","shell.execute_reply":"2022-08-02T07:16:31.503716Z"},"trusted":true},"execution_count":57,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils.multiclass import type_of_target\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import accuracy_score","metadata":{"id":"4fEdM5d1K_Ri","outputId":"50d0f63d-048d-4d36-d2df-116718b2e251","execution":{"iopub.status.busy":"2022-08-02T07:16:32.478379Z","iopub.execute_input":"2022-08-02T07:16:32.479262Z","iopub.status.idle":"2022-08-02T07:16:32.566513Z","shell.execute_reply.started":"2022-08-02T07:16:32.479227Z","shell.execute_reply":"2022-08-02T07:16:32.565555Z"},"trusted":true},"execution_count":58,"outputs":[]},{"cell_type":"code","source":"valid_classes = model.predict(valid_batches)\nvalid_classes_pred = np.argmax(valid_classes, axis = 1)\nvalid_classes_actual = valid_batches.classes\ncm = confusion_matrix(valid_classes_actual, valid_classes_pred)\nprint(train_batches.class_indices)\n#print(cm)\nprint(f\"Accuracy: {accuracy_score(valid_classes_actual, valid_classes_pred)}\")","metadata":{"id":"cMjIaJezLPfJ","outputId":"956058cf-122f-459f-9178-021e3d21173e","execution":{"iopub.status.busy":"2022-08-02T07:16:33.361783Z","iopub.execute_input":"2022-08-02T07:16:33.362516Z","iopub.status.idle":"2022-08-02T07:16:45.232255Z","shell.execute_reply.started":"2022-08-02T07:16:33.362472Z","shell.execute_reply":"2022-08-02T07:16:45.231352Z"},"trusted":true},"execution_count":59,"outputs":[]},{"cell_type":"code","source":"sns.color_palette(\"magma\", as_cmap=True)\nplt.figure(figsize=(15,15))\nax = sns.heatmap(cm, annot=True, cmap=\"magma\", fmt='d', cbar_kws={'label': 'Scale'})\nlabels=list(train_batches.class_indices.keys())\nax.set_xticklabels(labels)\nax.set_yticklabels(labels)","metadata":{"id":"2A7r5y0XPAoY","execution":{"iopub.status.busy":"2022-08-02T07:16:45.234519Z","iopub.execute_input":"2022-08-02T07:16:45.235180Z","iopub.status.idle":"2022-08-02T07:16:46.045384Z","shell.execute_reply.started":"2022-08-02T07:16:45.235142Z","shell.execute_reply":"2022-08-02T07:16:46.044111Z"},"trusted":true},"execution_count":60,"outputs":[]},{"cell_type":"code","source":"model.save('Model11.h5')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T07:17:12.946380Z","iopub.execute_input":"2022-08-02T07:17:12.946854Z","iopub.status.idle":"2022-08-02T07:17:13.346705Z","shell.execute_reply.started":"2022-08-02T07:17:12.946811Z","shell.execute_reply":"2022-08-02T07:17:13.345727Z"},"trusted":true},"execution_count":61,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r'Model11.h5')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T07:17:20.981731Z","iopub.execute_input":"2022-08-02T07:17:20.982220Z","iopub.status.idle":"2022-08-02T07:17:20.991412Z","shell.execute_reply.started":"2022-08-02T07:17:20.982176Z","shell.execute_reply":"2022-08-02T07:17:20.990321Z"},"trusted":true},"execution_count":62,"outputs":[]},{"cell_type":"code","source":"pip install gradio","metadata":{"execution":{"iopub.status.busy":"2022-08-02T07:17:32.574371Z","iopub.execute_input":"2022-08-02T07:17:32.574926Z","iopub.status.idle":"2022-08-02T07:17:55.057384Z","shell.execute_reply.started":"2022-08-02T07:17:32.574883Z","shell.execute_reply":"2022-08-02T07:17:55.056014Z"},"trusted":true},"execution_count":63,"outputs":[]},{"cell_type":"code","source":"import gradio as gr\n\n\nfrom tensorflow.keras.models import load_model\n \n# load model\nmodel = load_model('Model11.h5')\n\nclassnames = ['battery','biological','brown-glass','cardboard','clothes','green-glass','metal','paper','plastic','shoes','trash','white-glass']\n\n\n\ndef predict_image(img):\n img_4d=img.reshape(-1,298, 384,3)\n prediction=model.predict(img_4d)[0]\n return {classnames[i]: float(prediction[i]) for i in range(12)}\n\nimage = gr.inputs.Image(shape=(298, 384))\nlabel = gr.outputs.Label(num_top_classes=3)\n\n\n\n\ngr.Interface(fn=predict_image, inputs=image, title=\"Garbage Classifier\",\n description=\"This is a Garbage Classification Model Trained using Xception Net.Deployed to Hugging Faces using Gradio.\",outputs=label,interpretation='default').launch(debug=True,share=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-02T07:21:17.348938Z","iopub.execute_input":"2022-08-02T07:21:17.350234Z","iopub.status.idle":"2022-08-02T07:21:44.167525Z","shell.execute_reply.started":"2022-08-02T07:21:17.350192Z","shell.execute_reply":"2022-08-02T07:21:44.166356Z"},"trusted":true},"execution_count":67,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}
|