{"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":"!pip install gradio\n\nimport gradio as gr\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\nimport os\nimport cv2\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-15T17:21:55.178888Z","iopub.execute_input":"2022-03-15T17:21:55.179142Z","iopub.status.idle":"2022-03-15T17:22:03.216579Z","shell.execute_reply.started":"2022-03-15T17:21:55.179116Z","shell.execute_reply":"2022-03-15T17:22:03.215416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Initial EDA","metadata":{}},{"cell_type":"code","source":"train_pd = pd.read_csv('../input/ultra-mnist/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-15T17:22:03.219114Z","iopub.execute_input":"2022-03-15T17:22:03.219540Z","iopub.status.idle":"2022-03-15T17:22:03.245580Z","shell.execute_reply.started":"2022-03-15T17:22:03.219494Z","shell.execute_reply":"2022-03-15T17:22:03.244615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pd.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T17:22:03.247009Z","iopub.execute_input":"2022-03-15T17:22:03.247272Z","iopub.status.idle":"2022-03-15T17:22:03.267450Z","shell.execute_reply.started":"2022-03-15T17:22:03.247243Z","shell.execute_reply":"2022-03-15T17:22:03.266362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pd.describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T17:22:03.268544Z","iopub.execute_input":"2022-03-15T17:22:03.268755Z","iopub.status.idle":"2022-03-15T17:22:03.284544Z","shell.execute_reply.started":"2022-03-15T17:22:03.268732Z","shell.execute_reply":"2022-03-15T17:22:03.283518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pd.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T17:22:03.286774Z","iopub.execute_input":"2022-03-15T17:22:03.287150Z","iopub.status.idle":"2022-03-15T17:22:03.296433Z","shell.execute_reply.started":"2022-03-15T17:22:03.287119Z","shell.execute_reply":"2022-03-15T17:22:03.295454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pd.groupby('digit_sum').size().plot(kind='bar', figsize=(10,10))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T17:22:03.298088Z","iopub.execute_input":"2022-03-15T17:22:03.298580Z","iopub.status.idle":"2022-03-15T17:22:03.666255Z","shell.execute_reply.started":"2022-03-15T17:22:03.298541Z","shell.execute_reply":"2022-03-15T17:22:03.665112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gradio Based Train Data Visualiser\n\n**Open the visualiser in the new tab**\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T16:02:23.539724Z","iopub.execute_input":"2022-03-15T16:02:23.540628Z","iopub.status.idle":"2022-03-15T16:02:23.544258Z","shell.execute_reply.started":"2022-03-15T16:02:23.540589Z","shell.execute_reply":"2022-03-15T16:02:23.543477Z"}}},{"cell_type":"code","source":"train_data = pd.read_csv('../input/ultra-mnist/train.csv')\ndef viz_image(path=None):\n    image = cv2.imread(path)\n    # resize image for quick\n    image = cv2.resize(image, (256, 256), interpolation = cv2.INTER_AREA)\n    cv2.imwrite('./last.png', image)\n    return image\ndef get_image_gradio(num_sum=None, count=None):\n    num_sum = None if num_sum == 'None' else num_sum\n    filtered_data = None\n    if num_sum != None:\n        filtered_data = train_data.loc[train_data['digit_sum'] == num_sum]\n        filtered_data.reset_index(drop=True)\n\n    if count and len(filtered_data) >= count:\n        filtered_data = filtered_data.sample(int(count))\n        filtered_data.reset_index(drop=True)\n    sample_images = []\n    if len(filtered_data) != 0:\n        for idx, row in filtered_data.iterrows():\n            sample_images.append([f'Digit Sum: {row.digit_sum}', viz_image(f'/kaggle/input/ultra-mnist/train/{row.id}.jpeg')])\n    else:\n        sample_images.append(['', 'No image Found', np.zeros((256,256,3)) ])\n    return sample_images\niface = gr.Interface(\n    fn=get_image_gradio,\n    inputs = [gr.inputs.Slider(minimum=0, step=1, maximum=27, default=0, label='Select the digit_sum in image  to visualize'),\n             gr.inputs.Slider(minimum=1, step=10, maximum=51, default=1, label='Select the number of Samples to view')]\n    ,\n    outputs=gr.outputs.Carousel([\"text\", \"image\"], label='Train Data samples'),\n    layout='vertical',\n    theme='grass',\n    allow_flagging='never',\n    title=\"Ultra MNIST Classification Challenge\",\n    description=\"This is a custom tool designed to visualise the images present in train dataset for the ultra-mnist classification challenge. Select the numer of samples and the digit_sum in the sample images to filter on\"\n)\niface.launch(share=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T17:22:03.667944Z","iopub.execute_input":"2022-03-15T17:22:03.668192Z","iopub.status.idle":"2022-03-15T17:22:08.099292Z","shell.execute_reply.started":"2022-03-15T17:22:03.668164Z","shell.execute_reply":"2022-03-15T17:22:08.098385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}