{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":7133657,"sourceType":"datasetVersion","datasetId":4116031},{"sourceId":7133833,"sourceType":"datasetVersion","datasetId":4116144}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n        \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-06T01:24:34.834516Z","iopub.execute_input":"2023-12-06T01:24:34.835648Z","iopub.status.idle":"2023-12-06T01:24:34.840609Z","shell.execute_reply.started":"2023-12-06T01:24:34.835601Z","shell.execute_reply":"2023-12-06T01:24:34.839621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install dfply\n\nimport os\nimport os\nimport time\nfrom dfply import * \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport pyarrow # parquet\nfrom PIL import Image\nimport cv2\nfrom tensorflow.keras import layers, models, losses, initializers\nfrom sklearn.utils import shuffle ","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:24:34.842441Z","iopub.execute_input":"2023-12-06T01:24:34.842718Z","iopub.status.idle":"2023-12-06T01:24:46.634282Z","shell.execute_reply.started":"2023-12-06T01:24:34.842694Z","shell.execute_reply":"2023-12-06T01:24:46.632909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths = ['/kaggle/input/cmidss-lite/train/', '/kaggle/input/cmidss-lite/test/', '/kaggle/input/cmidss-lite/validation/']\nclass_label = {folder: i for folder, i in zip(os.listdir(paths[0]), range(len(os.listdir(paths[0]))))}","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:24:46.635949Z","iopub.execute_input":"2023-12-06T01:24:46.636369Z","iopub.status.idle":"2023-12-06T01:24:46.647258Z","shell.execute_reply.started":"2023-12-06T01:24:46.636332Z","shell.execute_reply":"2023-12-06T01:24:46.646248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_size(path):\n    img = cv2.imread(path)\n    height = img.shape[0]\n    width = img.shape[1]\n    channels = img.shape[2]\n    return height, width, channels\n\nhs, ws, cs = [], [], []\ncountsPerFolder, folderName = [], []\nfor path in paths: # paths = train, test\n    print(path)\n    for folder in os.listdir(path): # folder = label\n        print (folder)\n        count = 0\n        img_path = os.path.join(path, folder)\n        for img in os.listdir(img_path): # enter folder to get image iteratively\n            #print(img)\n            count += 1\n            h, w, c = get_img_size(os.path.join(img_path, img))\n            hs.append(h); ws.append(w); cs.append(c)\n        print (str(folder) + \":\" + str(count))\n        if path == paths[0]: # save the count per folder\n            countsPerFolder.append(count)\n            folderName.append(folder)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:24:46.648875Z","iopub.execute_input":"2023-12-06T01:24:46.649234Z","iopub.status.idle":"2023-12-06T01:25:01.875511Z","shell.execute_reply.started":"2023-12-06T01:24:46.649205Z","shell.execute_reply":"2023-12-06T01:25:01.874419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dim = pd.DataFrame({'height': hs, 'width' : ws, 'channel' : cs})\nprint('Image Dimension    : ', img_dim.shape[1])\nprint('Image Height       : ', img_dim.height.mode()[0])\nprint('Image Width        : ', img_dim.width.mode()[0])\nprint('Number of Channels : ', img_dim.channel.mode()[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:01.878531Z","iopub.execute_input":"2023-12-06T01:25:01.878849Z","iopub.status.idle":"2023-12-06T01:25:01.891955Z","shell.execute_reply.started":"2023-12-06T01:25:01.878822Z","shell.execute_reply":"2023-12-06T01:25:01.890813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folderCount = {i : j for i, j in zip(folderName, countsPerFolder)}\nfolderCount","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:01.893373Z","iopub.execute_input":"2023-12-06T01:25:01.893715Z","iopub.status.idle":"2023-12-06T01:25:01.911878Z","shell.execute_reply.started":"2023-12-06T01:25:01.893682Z","shell.execute_reply":"2023-12-06T01:25:01.910890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h = img_dim.height.mode()[0]; w = img_dim.width.mode()[0]; c = img_dim.channel.mode()[0]\nprint(img_dim.height.mean())\nprint(img_dim.height.std())\ndim = (w, h, c)\ninput_shape = dim\n# Check outlier\nif img_dim[(img_dim.height != h) | (img_dim.width != w)].shape[0] > 0:\n    print (\"do the resize\")\n\nimage_size = (h, w)\nprint(image_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:01.913264Z","iopub.execute_input":"2023-12-06T01:25:01.913620Z","iopub.status.idle":"2023-12-06T01:25:01.927708Z","shell.execute_reply.started":"2023-12-06T01:25:01.913588Z","shell.execute_reply":"2023-12-06T01:25:01.926782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nnum, images, labels, names = [], [], [], []\nrandom_filenames, random_labels, rdns = [], [], []  # for image(train) preview purpose\nfor path in paths: # paths = train, test, validation\n    count = 0\n    \n    for folder in os.listdir(path): # folder = label\n        img_path = os.path.join(path, folder)\n        filenames = [] # for preview image purpose        \n        #random_labels.append(folder); print(folder)   \n        \n        for img in os.listdir(img_path): # enter folder to get image iteratively\n            filenames.append(img)\n            labels.append(class_label[folder])                \n            img = cv2.imread(os.path.join(img_path, img))\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n            img = cv2.resize(img, image_size)          \n            images.append(img) \n            count += 1\n            \n        if path == paths[0]: # save randomly selected images (train)\n            #np.random.seed(2) \n            rdn = np.random.choice(range(folderCount[folder]), size = 2, replace = False)\n            rdns.append(rdn)\n            print(rdn)\n            for i in range(2):\n                random_filenames.append(filenames[rdn[i]]); print(filenames[rdn[i]])\n                random_labels.append(folder); print(folder)   \n\n    num.append(count)\n    \nimages = np.array(images, dtype = 'float32')\nlabels = np.array(labels, dtype = 'int32') \n  \nnum_train = num[0]\nimage_train = images[:num[0]]; label_train = labels[:num[0]]\n\nnum_test = num[1]\nimage_test = images[num[0]:(num[0]+num[1])]; label_test = labels[num[0]:(num[0]+num[1])]\n\nnum_validation = num[2]\nimage_validation = images[(num[0]+num[1]):(num[0]+num[1]+num[2])]; label_validation = labels[(num[0]+num[1]):(num[0]+num[1]+num[2])]\n\n\ndel images\ndel labels","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:01.929112Z","iopub.execute_input":"2023-12-06T01:25:01.929526Z","iopub.status.idle":"2023-12-06T01:25:11.622542Z","shell.execute_reply.started":"2023-12-06T01:25:01.929494Z","shell.execute_reply":"2023-12-06T01:25:11.621594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pass, check only\nrandom_images = []\ncount = 0\nfor rdn_label in random_labels: # folder = label\n    img_path = os.path.join(paths[0], rdn_label)\n    rdn_filename = random_filenames[count] \n    print(rdn_filename)\n    img = cv2.imread(os.path.join(img_path, rdn_filename))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, image_size)          \n    random_images.append(img)\n    count += 1\n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:05:34.240968Z","iopub.execute_input":"2023-12-06T01:05:34.241395Z","iopub.status.idle":"2023-12-06T01:05:34.355949Z","shell.execute_reply.started":"2023-12-06T01:05:34.241349Z","shell.execute_reply":"2023-12-06T01:05:34.354871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(image_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:17.245777Z","iopub.execute_input":"2023-12-06T01:25:17.246283Z","iopub.status.idle":"2023-12-06T01:25:17.253010Z","shell.execute_reply.started":"2023-12-06T01:25:17.246249Z","shell.execute_reply":"2023-12-06T01:25:17.252086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(image_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:19.430754Z","iopub.execute_input":"2023-12-06T01:25:19.431459Z","iopub.status.idle":"2023-12-06T01:25:19.437626Z","shell.execute_reply.started":"2023-12-06T01:25:19.431423Z","shell.execute_reply":"2023-12-06T01:25:19.436671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(image_validation)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:23.055599Z","iopub.execute_input":"2023-12-06T01:25:23.055972Z","iopub.status.idle":"2023-12-06T01:25:23.062660Z","shell.execute_reply.started":"2023-12-06T01:25:23.055940Z","shell.execute_reply":"2023-12-06T01:25:23.061617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_train = image_train / 255.0 \nimage_test = image_test / 255.0\nimage_validation = image_validation / 255.0 \n\n#image_train, label_train = shuffle(image_train, label_train, random_state = 25)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:27.500051Z","iopub.execute_input":"2023-12-06T01:25:27.501045Z","iopub.status.idle":"2023-12-06T01:25:28.762103Z","shell.execute_reply.started":"2023-12-06T01:25:27.500995Z","shell.execute_reply":"2023-12-06T01:25:28.761017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = models.Sequential()\nclassifier.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape = input_shape))\nclassifier.add(layers.MaxPooling2D((2, 2)))\nclassifier.add(layers.Conv2D(64, (3, 3), activation = 'relu'))\nclassifier.add(layers.MaxPooling2D((2, 2)))\nclassifier.add(layers.Conv2D(128, (3, 3), activation = 'relu'))\nclassifier.add(layers.MaxPooling2D((2, 2)))\nclassifier.add(layers.Flatten())\nclassifier.add(layers.Dense(128, activation='relu'))\nclassifier.add(layers.Dense(1, activation='sigmoid'))\n\nclassifier.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:25:39.567947Z","iopub.execute_input":"2023-12-06T01:25:39.568360Z","iopub.status.idle":"2023-12-06T01:25:43.578616Z","shell.execute_reply.started":"2023-12-06T01:25:39.568329Z","shell.execute_reply":"2023-12-06T01:25:43.577702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:26:26.494864Z","iopub.execute_input":"2023-12-06T01:26:26.495738Z","iopub.status.idle":"2023-12-06T01:26:26.516803Z","shell.execute_reply.started":"2023-12-06T01:26:26.495678Z","shell.execute_reply":"2023-12-06T01:26:26.516012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = classifier.fit(image_train, label_train, batch_size = 128, epochs = 10, validation_data = (image_validation, label_validation))","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:26:41.375248Z","iopub.execute_input":"2023-12-06T01:26:41.375630Z","iopub.status.idle":"2023-12-06T01:28:50.005715Z","shell.execute_reply.started":"2023-12-06T01:26:41.375601Z","shell.execute_reply":"2023-12-06T01:28:50.004542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label ='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.legend(loc = 'lower right')\n\ntest_loss, test_acc = classifier.evaluate(image_test, label_test, verbose = 1)\nprint(f'Validation Accuracy: {test_acc * 100:.2f}%')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:29:03.777039Z","iopub.execute_input":"2023-12-06T01:29:03.778070Z","iopub.status.idle":"2023-12-06T01:29:06.775378Z","shell.execute_reply.started":"2023-12-06T01:29:03.778029Z","shell.execute_reply":"2023-12-06T01:29:06.774448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\ntrue_labels = []\n\nfor i in range(len(image_test)):\n    images = image_test[i:i+1] # Extract images from the batch\n    labels = label_test[i:i+1]  # Extract labels from the batch\n    \n    # Get predictions for the batch\n    batch_predictions = classifier.predict(images)\n    \n    predictions.extend(batch_predictions)\n    true_labels.extend(labels)\n\n\npred_result = pd.DataFrame({'True_label':true_labels, 'Prediction': predictions})\nprint(pred_result)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T01:29:20.809148Z","iopub.execute_input":"2023-12-06T01:29:20.810331Z","iopub.status.idle":"2023-12-06T01:29:23.599516Z","shell.execute_reply.started":"2023-12-06T01:29:20.810292Z","shell.execute_reply":"2023-12-06T01:29:23.598507Z"},"trusted":true},"execution_count":null,"outputs":[]}]}