{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\nimport cv2 as cv \n #from skimage.transform import resize\nimport matplotlib.pyplot as plt\nimport imageio\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    #read directory\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-12-17T17:49:27.826639Z","iopub.execute_input":"2023-12-17T17:49:27.827143Z","iopub.status.idle":"2023-12-17T17:49:27.875572Z","shell.execute_reply.started":"2023-12-17T17:49:27.827106Z","shell.execute_reply":"2023-12-17T17:49:27.874246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read Train.csv","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"Train_csv = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:57:52.445127Z","iopub.execute_input":"2023-12-17T16:57:52.44616Z","iopub.status.idle":"2023-12-17T16:57:52.474743Z","shell.execute_reply.started":"2023-12-17T16:57:52.446113Z","shell.execute_reply":"2023-12-17T16:57:52.473765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_csv","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:57:52.476056Z","iopub.execute_input":"2023-12-17T16:57:52.476426Z","iopub.status.idle":"2023-12-17T16:57:52.510846Z","shell.execute_reply.started":"2023-12-17T16:57:52.476394Z","shell.execute_reply":"2023-12-17T16:57:52.509552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sample_csv","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:08.248582Z","iopub.execute_input":"2023-12-17T16:58:08.249461Z","iopub.status.idle":"2023-12-17T16:58:08.26096Z","shell.execute_reply.started":"2023-12-17T16:58:08.249407Z","shell.execute_reply":"2023-12-17T16:58:08.259931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset = set(Train_csv['image_id'])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-17T17:47:16.610994Z","iopub.execute_input":"2023-12-17T17:47:16.611497Z","iopub.status.idle":"2023-12-17T17:47:16.62332Z","shell.execute_reply.started":"2023-12-17T17:47:16.611459Z","shell.execute_reply":"2023-12-17T17:47:16.622038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading train_thumbnails","metadata":{}},{"cell_type":"code","source":"Train_thumbnails_Dataset=[]\nDataset_Path=r'/kaggle/input/UBC-OCEAN/train_thumbnails' \nc =0\nY= []\n# Read first 10 photos only \nfor f in os.listdir(Dataset_Path):\n    if c==10:\n        break\n    imageID = f[:f.find('_')]\n    print(imageID)\n    Row = Train_csv.loc[Train_csv['image_id']==int(imageID)]\n    print(Row['is_tma'])\n    Y.append(Row['is_tma'])\n    \n    img=cv.imread(os.path.join(Dataset_Path,f))\n    resized_img = cv.resize(img, (100, 100))\n    resized_img*=255\n    Train_thumbnails_Dataset.append(resized_img)\n    print (len(Train_thumbnails_Dataset))\n    c+=1\n#using plt.imshow() function show the image \n    plt.imshow(img)\n    plt.show()\nX=np.array(Train_thumbnails_Dataset,dtype=float)\nX","metadata":{"execution":{"iopub.status.busy":"2023-12-17T18:44:52.379894Z","iopub.execute_input":"2023-12-17T18:44:52.380433Z","iopub.status.idle":"2023-12-17T18:45:05.855298Z","shell.execute_reply.started":"2023-12-17T18:44:52.380395Z","shell.execute_reply":"2023-12-17T18:45:05.853965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split \nX_train, X_test, y_train, y_test = train_test_split(X,Y , \n                                   random_state=104,  \n                                   test_size=0.2,  \n                                   shuffle=True) ","metadata":{"execution":{"iopub.status.busy":"2023-12-17T18:54:13.436916Z","iopub.execute_input":"2023-12-17T18:54:13.43743Z","iopub.status.idle":"2023-12-17T18:54:13.447609Z","shell.execute_reply.started":"2023-12-17T18:54:13.437388Z","shell.execute_reply":"2023-12-17T18:54:13.44517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-17T18:54:28.562027Z","iopub.execute_input":"2023-12-17T18:54:28.563563Z","iopub.status.idle":"2023-12-17T18:54:28.572243Z","shell.execute_reply.started":"2023-12-17T18:54:28.563514Z","shell.execute_reply":"2023-12-17T18:54:28.570626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"number of Train Thumbnails\", len(Train_thumbnails_Dataset))","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:24.308274Z","iopub.execute_input":"2023-12-17T16:58:24.308889Z","iopub.status.idle":"2023-12-17T16:58:24.314726Z","shell.execute_reply.started":"2023-12-17T16:58:24.308834Z","shell.execute_reply":"2023-12-17T16:58:24.313567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_Dataset.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:24.316442Z","iopub.execute_input":"2023-12-17T16:58:24.317176Z","iopub.status.idle":"2023-12-17T16:58:24.330638Z","shell.execute_reply.started":"2023-12-17T16:58:24.317086Z","shell.execute_reply":"2023-12-17T16:58:24.329302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###################################################################################################","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:24.334359Z","iopub.execute_input":"2023-12-17T16:58:24.334835Z","iopub.status.idle":"2023-12-17T16:58:24.343368Z","shell.execute_reply.started":"2023-12-17T16:58:24.334792Z","shell.execute_reply":"2023-12-17T16:58:24.34221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalization Train Dataset\nTrain_Dataset_Normalized =[]\nfor img in Train_Dataset:\n    img_normalized = cv.normalize(img, None, 0, 1.0,cv.NORM_MINMAX, dtype=cv.CV_32F)\n    Train_Dataset_Normalized.append(img_normalized)\n    \nprint(len(Train_Dataset_Normalized))","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:24.345278Z","iopub.execute_input":"2023-12-17T16:58:24.345637Z","iopub.status.idle":"2023-12-17T16:58:24.364165Z","shell.execute_reply.started":"2023-12-17T16:58:24.345604Z","shell.execute_reply":"2023-12-17T16:58:24.362696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalization Dataset\nTest_Dataset_Normalized =[]\nfor img in Test_Dataset:\n    img_normalized = cv.normalize(img, None, 0, 1.0,cv.NORM_MINMAX, dtype=cv.CV_32F)\n    Test_Dataset_Normalized.append(img_normalized)\n    \nprint(len(Test_Dataset_Normalized))","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:24.365891Z","iopub.execute_input":"2023-12-17T16:58:24.366263Z","iopub.status.idle":"2023-12-17T16:58:24.372162Z","shell.execute_reply.started":"2023-12-17T16:58:24.36623Z","shell.execute_reply":"2023-12-17T16:58:24.371289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Flatten\nfrom keras.optimizers import SGD\nfrom PIL import Image\nfrom sklearn.metrics import classification_report\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.optimizers import Adam # - Work","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:24.373151Z","iopub.execute_input":"2023-12-17T16:58:24.373463Z","iopub.status.idle":"2023-12-17T16:58:35.304731Z","shell.execute_reply.started":"2023-12-17T16:58:24.373434Z","shell.execute_reply":"2023-12-17T16:58:35.303444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Convolutional_Neural_Network_Model():\n    # create model\n    model = Sequential()\n    model.add(Conv2D(32, (5, 5), input_shape=(100, 100, 3), activation='relu'))\n    model.add(MaxPooling2D())\n    model.add(Conv2D(64, (5, 5), activation='relu'))\n    model.add(MaxPooling2D())\n    model.add(Conv2D(64, (5, 5), activation='relu'))\n    model.add(MaxPooling2D())\n    # model.add(Dropout(0.2))\n    model.add(Flatten())\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(10, activation='softmax'))\n    # Compile model\n    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n    return model\n\nmodel_Architecture_CNN=Convolutional_Neural_Network_Model()","metadata":{"execution":{"iopub.status.busy":"2023-12-17T16:58:35.306354Z","iopub.execute_input":"2023-12-17T16:58:35.307219Z","iopub.status.idle":"2023-12-17T16:58:35.57923Z","shell.execute_reply.started":"2023-12-17T16:58:35.307172Z","shell.execute_reply":"2023-12-17T16:58:35.577807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_Architecture_CNN.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-17T18:59:47.998388Z","iopub.execute_input":"2023-12-17T18:59:47.998832Z","iopub.status.idle":"2023-12-17T18:59:48.03622Z","shell.execute_reply.started":"2023-12-17T18:59:47.998793Z","shell.execute_reply":"2023-12-17T18:59:48.034833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# -- coding: utf-8 --\n\"\"\"\nCreated on Sat Oct 21 14:04:14 2023\n\n@author: Admin\n\"\"\"\nimport cv2 as cv\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib import gridspec\n\n\n#Reading the image\nimg = cv.imread('coin.jpeg')\n#img= cv.cvtColor(img, cv.COLOR_BGR2GRAY)\nplt.figure(figsize=(15, 5))#(15inch, 5inch)= (15*80 pixels, 5*80 pixels)\nplt.title('input image')\nplt.imshow(img)\n\n\n############################\n\n#Normalize the image\n#img_normalized = cv.normalize(img, None, 0, 1.0,cv.NORM_MINMAX, dtype=cv.CV_32F)\nimg_normalized = cv.normalize(img, None, 0, 255,cv.NORM_MINMAX, dtype=cv.CV_8U)\nplt.figure(figsize=(15, 5))\nplt.title('Normalized Image')\nplt.imshow(img_normalized)\n\n###################################3\n\n#Gaussian filter (makes blurring)\nimg_filter= cv.GaussianBlur(img_normalized,(5,5), 0);\nplt.figure(figsize=(15, 5))\nplt.title('Filtered Image')\nplt.imshow(img_filter)\n#################################################\n#Segmentation (Otsu thresholding)\n# applying Otsu thresholding as an extra flag in binary thresholding   \nret, thresh1 = cv.threshold(img_normalized[:,:,0], 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)   \nplt.figure(figsize=(15, 5))\nplt.title('Otsu Threshold Image')\nplt.imshow(thresh1,cmap='gray')\n################################################\n#change range to 0.0 to 1.0\nthresh2= thresh1.astype(float) / 255\nplt.figure(figsize=(15, 5))\nplt.title('Otsu Threshold Image after edit1')\nplt.imshow(thresh2,cmap='gray')\n###################################################\n#Feature Selection:\n# which image we will use in this step? mask ?!, normalized image? or the original?\n#WE should multiply normalized image with the mask to get clear coins image without background for feature selection step\nx=thresh2*img_normalized[:,:,0]\nplt.figure(figsize=(15, 5))\nplt.title('Otsu Threshold mask multiplied with the normalized image')\nplt.imshow(x,cmap='gray')\n############################################\n#Features Average and Area\nsum=0.0\ncounter=0.0\nfor i in range(0,x.shape[0]):\n    for j in range(0,x.shape[1]):\n        if x[i,j] > 0:\n            counter +=1\n            sum+= x[i,j]\naverage = sum/counter\nArea_coins= 100* counter/x.size\n\nprint('Features of the image: average = {:.3f} and area = {:.3f}'.format(average, Area_coins))\n\"\"\"\n\n#Required part to make the input to otsu thresholding integer and gray 2D image (doesn't work well before thresholding)\n#img_normalized = cv.cvtColor(img_normalized, cv.COLOR_BGR2GRAY)\n#img_normalized = img_normalized.astype('uint8')   \ngray_image = cv.cvtColor(img, cv.COLOR_BGR2RGB)\nfig, ax = plt.subplots(1, 2, figsize=(16, 8))\nfig.tight_layout()\n\nax[0].imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))\nax[0].set_title(\"Original\")\n\nax[1].imshow(cv.cvtColor(gray_image, cv.COLOR_BGR2RGB))\nax[1].set_title(\"Grayscale\")\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-12-17T18:57:08.119599Z","iopub.execute_input":"2023-12-17T18:57:08.120053Z","iopub.status.idle":"2023-12-17T18:57:08.647295Z","shell.execute_reply.started":"2023-12-17T18:57:08.120016Z","shell.execute_reply":"2023-12-17T18:57:08.645647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}