{"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":"import tensorflow as tf\nimport os\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport cv2 as cv\nimport PIL\nimport numpy as np\nimport torch\nimport glob","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:33:47.286406Z","iopub.execute_input":"2022-07-26T11:33:47.286758Z","iopub.status.idle":"2022-07-26T11:33:53.313266Z","shell.execute_reply.started":"2022-07-26T11:33:47.286728Z","shell.execute_reply":"2022-07-26T11:33:53.312179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import ViTFeatureExtractor, ViTForImageClassification\nfrom PIL import Image ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:33:53.315764Z","iopub.execute_input":"2022-07-26T11:33:53.316454Z","iopub.status.idle":"2022-07-26T11:33:53.691202Z","shell.execute_reply.started":"2022-07-26T11:33:53.316416Z","shell.execute_reply":"2022-07-26T11:33:53.690264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" from torchvision.transforms import InterpolationMode","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:33:53.692632Z","iopub.execute_input":"2022-07-26T11:33:53.693188Z","iopub.status.idle":"2022-07-26T11:33:53.888767Z","shell.execute_reply.started":"2022-07-26T11:33:53.693152Z","shell.execute_reply":"2022-07-26T11:33:53.887861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:33:53.891724Z","iopub.execute_input":"2022-07-26T11:33:53.89209Z","iopub.status.idle":"2022-07-26T11:33:54.217177Z","shell.execute_reply.started":"2022-07-26T11:33:53.892054Z","shell.execute_reply":"2022-07-26T11:33:54.216161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install hugsvision","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-07-26T11:33:54.218776Z","iopub.execute_input":"2022-07-26T11:33:54.219121Z","iopub.status.idle":"2022-07-26T11:34:31.491334Z","shell.execute_reply.started":"2022-07-26T11:33:54.219085Z","shell.execute_reply":"2022-07-26T11:34:31.490198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PIL.Image.MAX_IMAGE_PIXELS = None","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:31.493125Z","iopub.execute_input":"2022-07-26T11:34:31.493782Z","iopub.status.idle":"2022-07-26T11:34:31.500953Z","shell.execute_reply.started":"2022-07-26T11:34:31.493739Z","shell.execute_reply":"2022-07-26T11:34:31.499756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train1 = pd.read_csv('../input/mayo-clinic-1024-jpg-part1/train_with_groups.csv')\ndf_train2 = pd.read_csv('../input/mayo-clinic-1024-jpg-part2-1/train_with_groups.csv')\ndf_train3 = pd.read_csv('../input/mayo-clinic-1024-jpg-part3/train_with_groups.csv')\ndf_train4 = pd.read_csv('../input/mayo-clinic-1024-jpg-part4-1/train_with_groups.csv')\ndf_train5 = pd.read_csv('../input/mayo-clinic-1024-jpg-part5-1/train_with_groups.csv')\ndf_train6 = pd.read_csv('../input/mayo-clinic-1024-jpg-part6/train_with_groups.csv')\ndf_train7 = pd.read_csv('../input/mayo-clinic-1024-jpg-part7-1/train_with_groups.csv')\ndf_train8 = pd.read_csv('../input/mayo-clinic-1024-jpg-part8/train_with_groups.csv')\ndf_train9 = pd.read_csv('../input/mayo-clinic-1024-jpg-part9/train_with_groups.csv')\ndf_train10 = pd.read_csv('../input/mayo-clinic-1024-jpg-part10/train_with_groups.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:31.502638Z","iopub.execute_input":"2022-07-26T11:34:31.503053Z","iopub.status.idle":"2022-07-26T11:34:31.593132Z","shell.execute_reply.started":"2022-07-26T11:34:31.503017Z","shell.execute_reply":"2022-07-26T11:34:31.592159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train1.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:31.594589Z","iopub.execute_input":"2022-07-26T11:34:31.595045Z","iopub.status.idle":"2022-07-26T11:34:31.621893Z","shell.execute_reply.started":"2022-07-26T11:34:31.595011Z","shell.execute_reply":"2022-07-26T11:34:31.620646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom pathlib import Path \np = Path('../input/mayo-clinic-1024-jpg-part1/train')\nimport os\nimgs = []","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:31.62351Z","iopub.execute_input":"2022-07-26T11:34:31.623965Z","iopub.status.idle":"2022-07-26T11:34:31.629743Z","shell.execute_reply.started":"2022-07-26T11:34:31.623929Z","shell.execute_reply":"2022-07-26T11:34:31.628352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_names = ['CE','LAA']","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:31.635862Z","iopub.execute_input":"2022-07-26T11:34:31.636508Z","iopub.status.idle":"2022-07-26T11:34:31.641268Z","shell.execute_reply.started":"2022-07-26T11:34:31.636468Z","shell.execute_reply":"2022-07-26T11:34:31.639857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train1.iloc[0][0]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:31.643081Z","iopub.execute_input":"2022-07-26T11:34:31.643503Z","iopub.status.idle":"2022-07-26T11:34:31.656814Z","shell.execute_reply.started":"2022-07-26T11:34:31.643462Z","shell.execute_reply":"2022-07-26T11:34:31.655514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in list(p.glob('./*.jpg')):\n    print(img)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:41.843428Z","iopub.execute_input":"2022-07-26T11:34:41.844193Z","iopub.status.idle":"2022-07-26T11:34:43.536767Z","shell.execute_reply.started":"2022-07-26T11:34:41.844156Z","shell.execute_reply":"2022-07-26T11:34:43.535784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id2label = {0: 'CE', 1: 'LAA'}\nlabel2id = {label:id for id,label in id2label.items()}\nlabel2id","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:43.53839Z","iopub.execute_input":"2022-07-26T11:34:43.538774Z","iopub.status.idle":"2022-07-26T11:34:43.549941Z","shell.execute_reply.started":"2022-07-26T11:34:43.538737Z","shell.execute_reply":"2022-07-26T11:34:43.548877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:43.551266Z","iopub.execute_input":"2022-07-26T11:34:43.551712Z","iopub.status.idle":"2022-07-26T11:34:44.324897Z","shell.execute_reply.started":"2022-07-26T11:34:43.551675Z","shell.execute_reply":"2022-07-26T11:34:44.323895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"help(feature_extractor)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:44.326172Z","iopub.execute_input":"2022-07-26T11:34:44.326749Z","iopub.status.idle":"2022-07-26T11:34:44.339189Z","shell.execute_reply.started":"2022-07-26T11:34:44.32671Z","shell.execute_reply":"2022-07-26T11:34:44.338221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import TFViTForImageClassification, create_optimizer\nimport tensorflow as tf\nmodel_id = \"google/vit-base-patch16-224-in21k\"\n# load pre-trained ViT model\nmodel = TFViTForImageClassification.from_pretrained(\n    model_id)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T11:34:44.340892Z","iopub.execute_input":"2022-07-26T11:34:44.341758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import ViTFeatureExtractor, ViTForImageClassification\nfrom PIL import Image \nimport requests\ni = 0 \nfor img in list(p.glob('./*.jpg')):\n    imgs =Image.open(img)\n    image = imgs \n    inputs = feature_extractor(images=image, return_tensors=\"np\")\n    i = i + 1\n    if i >= 1:\n        break\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs = model(**inputs)\nlogits = outputs.logits\n# model predicts one of the 1000 ImageNet classes\nprint(logits)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}