{"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":"# Referances\n1. https://www.kaggle.com/code/bibanh/simple-submission-classification-99-training\n2. https://www.kaggle.com/code/heyytanay/tensorflow-model-custom-keras-data-generators/notebook","metadata":{}},{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np\nimport cv2\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-03-28T21:47:35.375164Z","iopub.execute_input":"2023-03-28T21:47:35.375632Z","iopub.status.idle":"2023-03-28T21:47:45.018964Z","shell.execute_reply.started":"2023-03-28T21:47:35.375592Z","shell.execute_reply":"2023-03-28T21:47:45.017675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Dataset","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/benetech-metadata/df_train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-28T21:40:24.004828Z","iopub.execute_input":"2023-03-28T21:40:24.005511Z","iopub.status.idle":"2023-03-28T21:40:24.214433Z","shell.execute_reply.started":"2023-03-28T21:40:24.005455Z","shell.execute_reply":"2023-03-28T21:40:24.212981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_and_labels = {}\nfor dirname, _, filenames in os.walk('/kaggle/input/benetech-making-graphs-accessible/test/images'):\n    for filename in filenames:\n        files_and_labels[os.path.join(dirname, filename)] = 'line'\ndf_test = pd.DataFrame(files_and_labels, index = [0]).T.reset_index().rename(columns = {'index':'filename', 0 :'chart_type'})\ndf_test","metadata":{"execution":{"iopub.status.busy":"2023-03-28T21:40:46.291667Z","iopub.execute_input":"2023-03-28T21:40:46.292071Z","iopub.status.idle":"2023-03-28T21:40:46.317378Z","shell.execute_reply.started":"2023-03-28T21:40:46.292035Z","shell.execute_reply":"2023-03-28T21:40:46.316226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image and Label DataLoader","metadata":{}},{"cell_type":"code","source":"class BenetechDataGenerator(tf.keras.utils.Sequence):\n    \n    def __init__(self, df, batch_size):\n        self.df = df\n        self.batch_size = batch_size\n    \n    def __len__(self):\n        return np.math.ceil(len(df) / self.batch_size)\n    \n    def __getitem__(self, index):\n        name_img = self.df.iloc[idx]['filename']\n        \n        # Image Processing\n        img = cv2.imread(name_img)\n        img = cv2.cvtColor(img,cv.COLOR_BGR2RGB)\n        img = cv2.resize(img,(500,300))\n        img = img.astype(np.float32)/255.0\n\n        label = np.array(self.df.iloc[idx]['label'])\n\n        return img, label","metadata":{"execution":{"iopub.status.busy":"2023-03-28T21:47:45.021693Z","iopub.execute_input":"2023-03-28T21:47:45.023180Z","iopub.status.idle":"2023-03-28T21:47:45.033799Z","shell.execute_reply.started":"2023-03-28T21:47:45.023110Z","shell.execute_reply":"2023-03-28T21:47:45.032749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = BenetechDataGenerator(df, 32)\nvalid_loader = BenetechDataGenerator(df, 32)","metadata":{},"execution_count":null,"outputs":[]}]}