{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-18T09:44:03.570145Z","iopub.execute_input":"2023-03-18T09:44:03.570609Z","iopub.status.idle":"2023-03-18T09:50:21.323318Z","shell.execute_reply.started":"2023-03-18T09:44:03.570558Z","shell.execute_reply":"2023-03-18T09:50:21.322223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-03-18T09:50:21.325550Z","iopub.execute_input":"2023-03-18T09:50:21.325987Z","iopub.status.idle":"2023-03-18T09:50:31.187263Z","shell.execute_reply.started":"2023-03-18T09:50:21.325947Z","shell.execute_reply":"2023-03-18T09:50:31.185867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path= \"/kaggle/input/fruit-images-for-object-detection/train_zip/train\"\nfileslst= os.listdir(path); imgs= [];\nfor fle in fileslst:\n    if fle.endswith(\".jpg\"):\n        imgs.append(fle)\n#print(imgs)\nimg= cv2.imread(\"/kaggle/input/fruit-images-for-object-detection/train_zip/train/\"+imgs[1]); \nimg= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nplt.imshow(img)\nimg= cv2.cvtColor(img, cv2.COLOR_RGB2HSV)\nplt.subplot(231); plt.imshow(img[:,:,0])\nplt.subplot(232); plt.imshow(img[:,:,1])\nplt.subplot(233); plt.imshow(img[:,:,2])\nimgedg= []\nimgedg0= cv2.Canny(img[:,:,0],120,200); imgedg1= cv2.Canny(img[:,:,1],120,200); imgedg2= cv2.Canny(img[:,:,2],120,200);\nplt.subplot(234); plt.imshow(imgedg0)\nplt.subplot(235); plt.imshow(imgedg1)\nplt.subplot(236); plt.imshow(imgedg2)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T09:50:31.189096Z","iopub.execute_input":"2023-03-18T09:50:31.190501Z","iopub.status.idle":"2023-03-18T09:50:32.304583Z","shell.execute_reply.started":"2023-03-18T09:50:31.190448Z","shell.execute_reply":"2023-03-18T09:50:32.303133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-03-18T09:50:32.307702Z","iopub.execute_input":"2023-03-18T09:50:32.308126Z","iopub.status.idle":"2023-03-18T09:50:32.314879Z","shell.execute_reply.started":"2023-03-18T09:50:32.308075Z","shell.execute_reply":"2023-03-18T09:50:32.313519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path= \"/kaggle/input/fruit-images-for-object-detection/train_zip/train\"\nfileslst= os.listdir(path); imgs= [];\nfor fle in fileslst:\n    if fle.endswith(\".jpg\"):\n        imgs.append(fle)\nimgar= []; y= pd.DataFrame(columns=[\"apl\",\"ban\",\"orn\",\"mix\"])\nprint(fileslst[:20])\nfor i in range(len(imgs)):\n    img= cv2.imread(\"/kaggle/input/fruit-images-for-object-detection/train_zip/train/\"+imgs[i]); \n    img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img= cv2.resize(img,(28,28))\n    imgar.append(np.array(img))\n    if imgs[i][0]=='a':\n        y.loc[i,\"apl\"]=1\n    elif imgs[i][0]=='b':\n        y.loc[i,\"ban\"]=1\n    elif imgs[i][0]=='o':\n        y.loc[i,\"orn\"]=1\n    else:\n        y.loc[i,\"mix\"]=1\n\nimgarr= np.array([imgar]); imgarr= np.reshape(imgarr,(240,1,28,28,3))\nprint(np.shape(imgarr))\ny= y.replace(np.nan,0)\ny= np.array([y]); y= np.reshape(y,(240,4))\nprint(y[0])\nplt.imshow(img)\nimg= cv2.cvtColor(img, cv2.COLOR_RGB2HSV)\nplt.subplot(231); plt.imshow(img[:,:,0])\nplt.subplot(232); plt.imshow(img[:,:,1])\nplt.subplot(233); plt.imshow(img[:,:,2])\nimgedg= []\nimgedg0= cv2.Canny(img[:,:,0],120,200); imgedg1= cv2.Canny(img[:,:,1],120,200); imgedg2= cv2.Canny(img[:,:,2],120,200);\nplt.subplot(234); plt.imshow(imgedg0)\nplt.subplot(235); plt.imshow(imgedg1)\nplt.subplot(236); plt.imshow(imgedg2)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T11:13:00.710574Z","iopub.execute_input":"2023-03-18T11:13:00.711092Z","iopub.status.idle":"2023-03-18T11:13:05.189980Z","shell.execute_reply.started":"2023-03-18T11:13:00.711033Z","shell.execute_reply":"2023-03-18T11:13:05.188563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modl= tf.keras.Sequential()\nmodl.add(tf.keras.layers.InputLayer(input_shape=(1,28,28,3)))\nmodl.add(tf.keras.layers.Conv2D(32,(3,3),activation='relu', name=\"convlyr\"))\nmodl.add(tf.keras.layers.Flatten())\nmodl.add(tf.keras.layers.Dense(units= 64, activation='relu'))\nmodl.add(tf.keras.layers.Dense(32, activation=\"relu\"))\n\nmodl.add(tf.keras.layers.Dense(4,activation=\"sigmoid\"))\n         \nmodl.summary()\noptm= tf.keras.optimizers.Adam(learning_rate=0.001)\nmodl.compile(loss= 'categorical_crossentropy', optimizer= optm, metrics= 'accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-03-18T11:26:09.509520Z","iopub.execute_input":"2023-03-18T11:26:09.509970Z","iopub.status.idle":"2023-03-18T11:26:09.641955Z","shell.execute_reply.started":"2023-03-18T11:26:09.509928Z","shell.execute_reply":"2023-03-18T11:26:09.640178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-03-18T11:11:00.821385Z","iopub.execute_input":"2023-03-18T11:11:00.821852Z","iopub.status.idle":"2023-03-18T11:11:00.994096Z","shell.execute_reply.started":"2023-03-18T11:11:00.821811Z","shell.execute_reply":"2023-03-18T11:11:00.992435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modl.fit(imgarr,y, epochs= 50)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T10:21:00.828694Z","iopub.execute_input":"2023-03-18T10:21:00.829160Z","iopub.status.idle":"2023-03-18T10:21:12.123926Z","shell.execute_reply.started":"2023-03-18T10:21:00.829119Z","shell.execute_reply":"2023-03-18T10:21:12.121128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modl.save(\"/kaggle/working/frutclssfrsig.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-03-18T10:21:25.046133Z","iopub.execute_input":"2023-03-18T10:21:25.046573Z","iopub.status.idle":"2023-03-18T10:21:25.099444Z","shell.execute_reply.started":"2023-03-18T10:21:25.046532Z","shell.execute_reply":"2023-03-18T10:21:25.098273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(imgarr))\n#yprd= modl.predict(np.array([imgarr[0]]))\n#print(yprd)\nimg= cv2.imread(\"/kaggle/input/fruit-images-for-object-detection/test_zip/test/\"+\"banana_82.jpg\"); \n#img= cv2.imread(\"/kaggle/input/appletest1/banapl.jpg\")\nimg= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nimg= cv2.resize(img,(28,28))\nxnw= np.array([[img]]); print(np.shape(xnw))\nyprd= modl.predict(xnw)\nprint(yprd)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T10:23:09.304018Z","iopub.execute_input":"2023-03-18T10:23:09.304473Z","iopub.status.idle":"2023-03-18T10:23:09.411078Z","shell.execute_reply.started":"2023-03-18T10:23:09.304435Z","shell.execute_reply":"2023-03-18T10:23:09.409789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nlyrop= modl.get_layer('convlyr').output\nlyrmdl= tf.keras.models.Model(inputs = modl.input, outputs = lyrop)\nlyrpred= lyrmdl.predict(xnw); plt.subplot(221);plt.imshow(img); plt.subplot(222);\nplt.imshow(lyrpred[0,0,:,:,0]); plt.subplot(223);plt.imshow(lyrpred[0,0,:,:,6]); plt.subplot(224);plt.imshow(lyrpred[0,0,:,:,8]); ","metadata":{"execution":{"iopub.status.busy":"2023-03-18T11:26:31.698072Z","iopub.execute_input":"2023-03-18T11:26:31.698508Z","iopub.status.idle":"2023-03-18T11:26:32.371782Z","shell.execute_reply.started":"2023-03-18T11:26:31.698470Z","shell.execute_reply":"2023-03-18T11:26:32.370276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"functional api to implement residual network that include color features","metadata":{}},{"cell_type":"code","source":"#funcional model with residual neural network\ninp= tf.keras.Input(shape=(1,28,28,3));\nx= tf.keras.layers.Conv2D(16,(3,3),activation='relu')(inp);\nx1= tf.keras.layers.AveragePooling2D(pool_size=(3,3))(inp[:,0,:,:,:]);\nx1= tf.keras.layers.Dropout(0.2)(x1)\n#x1= tf.expand_dims(x1, axis=0)\nx= tf.keras.layers.Flatten()(x)\nx1= tf.keras.layers.Flatten()(x1)\ncombind= tf.keras.layers.concatenate([x,x1])\nx= tf.keras.layers.Dense(units= 32, activation='relu')(combind)\nx= tf.keras.layers.Dense(16, activation=\"relu\")(x)\nout= tf.keras.layers.Dense(4,activation=\"softmax\")(x)\nmodl1= tf.keras.Model(inputs=inp, outputs=out)\n                                \nmodl1.summary()\noptm= tf.keras.optimizers.Adam(learning_rate=0.001)\nmodl1.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics= ['accuracy','TrueNegatives'])","metadata":{"execution":{"iopub.status.busy":"2023-03-18T11:56:27.655667Z","iopub.execute_input":"2023-03-18T11:56:27.656125Z","iopub.status.idle":"2023-03-18T11:56:27.829824Z","shell.execute_reply.started":"2023-03-18T11:56:27.656080Z","shell.execute_reply":"2023-03-18T11:56:27.827988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modl1.fit(imgarr,y, epochs= 60)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T11:56:34.780164Z","iopub.execute_input":"2023-03-18T11:56:34.780576Z","iopub.status.idle":"2023-03-18T11:56:46.226735Z","shell.execute_reply.started":"2023-03-18T11:56:34.780539Z","shell.execute_reply":"2023-03-18T11:56:46.225163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(imgarr))\n#yprd= modl.predict(np.array([imgarr[0]]))\n#print(yprd)\nimg= cv2.imread(\"/kaggle/input/fruit-images-for-object-detection/test_zip/test/\"+\"banana_82.jpg\"); \nimg= cv2.imread(\"/kaggle/input/appletest1/banapl.jpg\")\nimg= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nimg= cv2.resize(img,(28,28))\nxnw= np.array([[img]]); print(np.shape(xnw))\nyprd= modl1.predict(xnw)\nprint(yprd)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T11:57:20.721306Z","iopub.execute_input":"2023-03-18T11:57:20.722432Z","iopub.status.idle":"2023-03-18T11:57:20.820329Z","shell.execute_reply.started":"2023-03-18T11:57:20.722380Z","shell.execute_reply":"2023-03-18T11:57:20.818523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modl1.save(\"/kaggle/ouput/fruitclsswithcolr.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:00:22.983624Z","iopub.execute_input":"2023-03-18T12:00:22.984171Z","iopub.status.idle":"2023-03-18T12:00:23.041287Z","shell.execute_reply.started":"2023-03-18T12:00:22.984113Z","shell.execute_reply":"2023-03-18T12:00:23.039462Z"},"trusted":true},"execution_count":null,"outputs":[]}]}