{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Image Classification with Huggingface Transformers 🤗\n## histopatholgic-cancer-detection kaggle dataset\n\nbased on https://huggingface.co/blog/fine-tune-vit guide for fine-tuning huggingface transformers ","metadata":{}},{"cell_type":"markdown","source":"This an example of how to use huggingface transformer for bicategorical image classification. Using a pretrained model, 'google/vit-base-patch16-224-in21k'. We are going to work with pandas and datasets for loading our data. \n\nDatasets are to load our image dataset from folders automatically, but the images needs to be sorted by folders with the label categories. \nIn this case we have all images mixed in one folder and a guide csv file. Thats the reason why we will be opening them with pandas, and then transforming the dataframes to datasets.\n","metadata":{}},{"cell_type":"code","source":"!lspci","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:11.063163Z","iopub.execute_input":"2024-11-27T11:38:11.064032Z","iopub.status.idle":"2024-11-27T11:38:12.090474Z","shell.execute_reply.started":"2024-11-27T11:38:11.063983Z","shell.execute_reply":"2024-11-27T11:38:12.089555Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ipywidgets --upgrade","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T11:38:12.092689Z","iopub.execute_input":"2024-11-27T11:38:12.093091Z","iopub.status.idle":"2024-11-27T11:38:22.082188Z","shell.execute_reply.started":"2024-11-27T11:38:12.093052Z","shell.execute_reply":"2024-11-27T11:38:22.081238Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Loading the data 📑","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nimport os\n\ndf = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')\ndf = df.rename(columns={'label':'labels'})","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:22.083582Z","iopub.execute_input":"2024-11-27T11:38:22.083964Z","iopub.status.idle":"2024-11-27T11:38:23.026581Z","shell.execute_reply.started":"2024-11-27T11:38:22.083927Z","shell.execute_reply":"2024-11-27T11:38:23.025865Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Creating dataframes with train test split","metadata":{}},{"cell_type":"code","source":"base_path = '/kaggle/input/histopathologic-cancer-detection/train/'\ndf['filepath'] = df['id'].apply(lambda idx: os.path.join(base_path, idx + '.tif'))\n\n# Generate a list of indices to split the dataframe\nindices = df.index.tolist()\ntrain_size = int(0.7 * len(indices))\n\n# Use sklearn's train_test_split to split the indices\ntrain_indices, val_indices = train_test_split(indices, train_size=train_size)\n\n# Create the training and testing dataframes\ntrain_df = df.loc[train_indices]\nval_df = df.loc[val_indices]","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:23.028935Z","iopub.execute_input":"2024-11-27T11:38:23.02975Z","iopub.status.idle":"2024-11-27T11:38:23.391607Z","shell.execute_reply.started":"2024-11-27T11:38:23.029711Z","shell.execute_reply":"2024-11-27T11:38:23.390851Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:23.392715Z","iopub.execute_input":"2024-11-27T11:38:23.393004Z","iopub.status.idle":"2024-11-27T11:38:23.404408Z","shell.execute_reply.started":"2024-11-27T11:38:23.392977Z","shell.execute_reply":"2024-11-27T11:38:23.403489Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Creating test dataframe","metadata":{}},{"cell_type":"code","source":"# create a test dataframe from the test folder.\ntest_path = '/kaggle/input/histopathologic-cancer-detection/test'\n\ntest_filepaths = os.listdir(test_path)\ntest_ids = [os.path.splitext(filename)[0] for filename in test_filepaths]\ntest_filepaths_wd = [os.path.join(test_path, filename) for filename in test_filepaths]\n\n# Create a test dataframe with labels = 0, this is going to be feed to the test with ignore_keys,\n# labels needs to keep the same format, if the labels were real values we could have performed metrics\ntest_df = pd.DataFrame({'id': test_ids, 'filepath': test_filepaths_wd, 'labels': 0})","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:23.405427Z","iopub.execute_input":"2024-11-27T11:38:23.405703Z","iopub.status.idle":"2024-11-27T11:38:23.551251Z","shell.execute_reply.started":"2024-11-27T11:38:23.405667Z","shell.execute_reply":"2024-11-27T11:38:23.550317Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Sampling smaller set of data","metadata":{}},{"cell_type":"code","source":"# get smaller dataset\nsample = 0.1\ntrain_df = train_df.sample(frac=sample).reset_index(drop=True).copy()\nval_df = val_df.sample(frac=sample).reset_index(drop=True).copy()\n#test_df = test_df.sample(10).reset_index(drop=True).copy()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:23.552363Z","iopub.execute_input":"2024-11-27T11:38:23.552634Z","iopub.status.idle":"2024-11-27T11:38:23.57462Z","shell.execute_reply.started":"2024-11-27T11:38:23.552609Z","shell.execute_reply":"2024-11-27T11:38:23.574043Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:23.57549Z","iopub.execute_input":"2024-11-27T11:38:23.575743Z","iopub.status.idle":"2024-11-27T11:38:23.584089Z","shell.execute_reply.started":"2024-11-27T11:38:23.575719Z","shell.execute_reply":"2024-11-27T11:38:23.583167Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# example of the data\nfrom PIL import Image as Pilimage\nPilimage.open(train_df['filepath'][1])","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:23.585256Z","iopub.execute_input":"2024-11-27T11:38:23.585854Z","iopub.status.idle":"2024-11-27T11:38:23.639042Z","shell.execute_reply.started":"2024-11-27T11:38:23.585798Z","shell.execute_reply":"2024-11-27T11:38:23.637981Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Creating dataset\n\nIn this dataset our images are in TIFF format, we need to process them into RGB before converting them to tensors","metadata":{}},{"cell_type":"code","source":"from datasets import Dataset, load_dataset, Image, DatasetDict\n\n\n# Use the map method to add a new column 'image' based on 'filepath'\ndef load_image(filepath):\n    # Load image using the appropriate method (e.g., PIL for common image formats)\n    image = Pilimage.open(filepath)\n    image.convert(\"RGB\")\n    return image\n\ndef decode_mutlichannel_tiff(batch):\n    batch[\"image\"] = [load_image(image) for image in batch[\"filepath\"]]\n    return batch\n\n\nds_train = Dataset.from_pandas(train_df)\nds_train = ds_train.class_encode_column(\"labels\")\nds_train = ds_train.map(decode_mutlichannel_tiff, remove_columns=[\"filepath\"], batched=True)\n\nds_val = Dataset.from_pandas(val_df)\nds_val = ds_val.class_encode_column(\"labels\")\nds_val = ds_val.map(decode_mutlichannel_tiff, remove_columns=[\"filepath\"], batched=True)\n\nds_test = Dataset.from_pandas(test_df)\nds_test = ds_test.map(decode_mutlichannel_tiff, remove_columns=[\"filepath\"], batched=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:38:23.651336Z","iopub.execute_input":"2024-11-27T11:38:23.651615Z","iopub.status.idle":"2024-11-27T11:52:42.4599Z","shell.execute_reply.started":"2024-11-27T11:38:23.651588Z","shell.execute_reply":"2024-11-27T11:52:42.459095Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Combining all datasets into a DatasetDict","metadata":{}},{"cell_type":"code","source":"ds = DatasetDict({\n    \"train\": ds_train,\n    \"val\": ds_val,\n    \"test\": ds_test\n})\n\nds","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:52:42.460965Z","iopub.execute_input":"2024-11-27T11:52:42.461277Z","iopub.status.idle":"2024-11-27T11:52:42.467084Z","shell.execute_reply.started":"2024-11-27T11:52:42.461251Z","shell.execute_reply":"2024-11-27T11:52:42.46625Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ds['train'][0]","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:52:42.468278Z","iopub.execute_input":"2024-11-27T11:52:42.46903Z","iopub.status.idle":"2024-11-27T11:52:42.484317Z","shell.execute_reply.started":"2024-11-27T11:52:42.468983Z","shell.execute_reply":"2024-11-27T11:52:42.483393Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# example of image extracted from the dataset\nds['train'][0]['image']","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:52:42.485506Z","iopub.execute_input":"2024-11-27T11:52:42.485879Z","iopub.status.idle":"2024-11-27T11:52:42.4947Z","shell.execute_reply.started":"2024-11-27T11:52:42.48584Z","shell.execute_reply":"2024-11-27T11:52:42.493867Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from datasets import ClassLabel\n\ntext_labels = ClassLabel(num_classes=2, names=['non_tumorous', 'tumorous'], id=None)\ntrain_labels = ds['train'].features['labels']\ntrain_labels","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:52:42.495949Z","iopub.execute_input":"2024-11-27T11:52:42.496281Z","iopub.status.idle":"2024-11-27T11:52:42.505049Z","shell.execute_reply.started":"2024-11-27T11:52:42.496244Z","shell.execute_reply":"2024-11-27T11:52:42.504326Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = text_labels.int2str(ds['train']['labels'])\nlabels[:5]","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:52:42.50595Z","iopub.execute_input":"2024-11-27T11:52:42.506181Z","iopub.status.idle":"2024-11-27T11:52:42.524322Z","shell.execute_reply.started":"2024-11-27T11:52:42.506158Z","shell.execute_reply":"2024-11-27T11:52:42.523494Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## example data selection","metadata":{}},{"cell_type":"code","source":"import random\nfrom PIL import ImageDraw, ImageFont, Image\n\ndef show_examples(ds, seed: int = 1234, examples_per_class: int = 3, size=(250, 250)):\n\n    w, h = size\n    labels = ds['train'].features['labels'].names\n    grid = Image.new('RGB', size=(examples_per_class * w, len(labels) * h))\n    draw = ImageDraw.Draw(grid)\n    #font = ImageFont.truetype(size=24)\n\n\n    for label_id, label in enumerate(labels):\n\n        # Filter the dataset by a single label, shuffle it, and grab a few samples\n        ds_slice = ds['train'].filter(lambda ex: ex['labels'] == label_id).shuffle(seed).select(range(examples_per_class))\n\n        # Plot this label's examples along a row\n        for i, example in enumerate(ds_slice):\n            image = example['image']\n            idx = examples_per_class * label_id + i\n            box = (idx % examples_per_class * w, idx // examples_per_class * h)\n            grid.paste(image.resize(size), box=box)\n            draw.text(box, label, (255, 255, 255))\n\n    return grid\n\nshow_examples(ds, seed=random.randint(0, 1337), examples_per_class=3)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:52:42.525504Z","iopub.execute_input":"2024-11-27T11:52:42.526039Z","iopub.status.idle":"2024-11-27T11:53:59.34619Z","shell.execute_reply.started":"2024-11-27T11:52:42.525999Z","shell.execute_reply":"2024-11-27T11:53:59.345348Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Building our Processor 📔","metadata":{}},{"cell_type":"code","source":"from transformers import ViTImageProcessor\n\n# we load the last model for image classification from huggingface model library\nmodel = 'google/vit-base-patch16-224-in21k'\nprocessor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224-in21k')\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:53:59.347134Z","iopub.execute_input":"2024-11-27T11:53:59.347384Z","iopub.status.idle":"2024-11-27T11:54:16.497071Z","shell.execute_reply.started":"2024-11-27T11:53:59.347359Z","shell.execute_reply":"2024-11-27T11:54:16.496208Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model characteristics\nprocessor","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:16.498462Z","iopub.execute_input":"2024-11-27T11:54:16.499509Z","iopub.status.idle":"2024-11-27T11:54:16.506035Z","shell.execute_reply.started":"2024-11-27T11:54:16.49946Z","shell.execute_reply":"2024-11-27T11:54:16.505248Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Processing Images","metadata":{}},{"cell_type":"code","source":"# converting images to tensorflow tensors \n\nimage = ds['train'][0]['image']\nprocessor(image, return_tensors='pt')\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:16.50704Z","iopub.execute_input":"2024-11-27T11:54:16.507288Z","iopub.status.idle":"2024-11-27T11:54:16.6317Z","shell.execute_reply.started":"2024-11-27T11:54:16.507264Z","shell.execute_reply":"2024-11-27T11:54:16.630849Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"# Setting a transformation\n\nwe are converting our images to tensor, we are using \"with_transform\" so images are processed when they are called to the model","metadata":{}},{"cell_type":"code","source":"def transform(batch):\n    inputs = processor([x for x in batch['image']], return_tensors='pt')\n    inputs['labels'] = batch['labels']    \n    return inputs\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:16.632875Z","iopub.execute_input":"2024-11-27T11:54:16.633151Z","iopub.status.idle":"2024-11-27T11:54:16.637393Z","shell.execute_reply.started":"2024-11-27T11:54:16.633126Z","shell.execute_reply":"2024-11-27T11:54:16.63662Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prepared_ds = DatasetDict()\nprepared_ds = ds.with_transform(transform)\n#prepared_ds['train'] = ds['train'].with_transform(transform)\n#prepared_ds['val'] = ds['val'].with_transform(transform)\n#prepared_ds['test'] = ds['test'].with_transform(transform_test)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:16.638362Z","iopub.execute_input":"2024-11-27T11:54:16.638638Z","iopub.status.idle":"2024-11-27T11:54:16.65Z","shell.execute_reply.started":"2024-11-27T11:54:16.638614Z","shell.execute_reply":"2024-11-27T11:54:16.649178Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data collator\nBatches are coming in as lists of dicts, so you can just unpack + stack those into batch tensors.\n","metadata":{}},{"cell_type":"code","source":"import torch\n\ndef collate_fn(batch):\n    return {\n        'pixel_values': torch.stack([x['pixel_values'] for x in batch]),\n        'labels': torch.tensor([x['labels'] for x in batch])\n    }\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:16.651086Z","iopub.execute_input":"2024-11-27T11:54:16.651422Z","iopub.status.idle":"2024-11-27T11:54:16.664805Z","shell.execute_reply.started":"2024-11-27T11:54:16.651387Z","shell.execute_reply":"2024-11-27T11:54:16.664215Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# loading metrics","metadata":{}},{"cell_type":"code","source":"!pip install evaluate","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T12:22:33.523477Z","iopub.execute_input":"2024-11-27T12:22:33.523831Z","iopub.status.idle":"2024-11-27T12:22:42.012695Z","shell.execute_reply.started":"2024-11-27T12:22:33.523787Z","shell.execute_reply":"2024-11-27T12:22:42.011553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom evaluate import load\n\nmetric = load('accuracy', 'recall')\ndef compute_metrics(p):\n    return metric.compute(predictions=np.argmax(p.predictions, axis=1), references=p.label_ids)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T12:22:53.86793Z","iopub.execute_input":"2024-11-27T12:22:53.868298Z","iopub.status.idle":"2024-11-27T12:22:56.106426Z","shell.execute_reply.started":"2024-11-27T12:22:53.868269Z","shell.execute_reply":"2024-11-27T12:22:56.105744Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import ViTForImageClassification\n\nlabels = ds['train'].features['labels'].names\n\nmodel = ViTForImageClassification.from_pretrained(\n    model,\n    num_labels=len(labels),\n    id2label={str(i): c for i, c in enumerate(labels)},\n    label2id={c: str(i) for i, c in enumerate(labels)}\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T12:22:59.43252Z","iopub.execute_input":"2024-11-27T12:22:59.432891Z","iopub.status.idle":"2024-11-27T12:23:02.55677Z","shell.execute_reply.started":"2024-11-27T12:22:59.432858Z","shell.execute_reply":"2024-11-27T12:23:02.556122Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model architecture","metadata":{}},{"cell_type":"code","source":"from transformers import TrainingArguments\n\ntraining_args = TrainingArguments(\n  output_dir=\"/kaggle/working/model\",\n  per_device_train_batch_size=16,\n  evaluation_strategy=\"steps\",\n  num_train_epochs=4,\n  fp16=True, #only works on cuda GPU\n  save_steps=100,\n  eval_steps=100,\n  logging_steps=10,\n  learning_rate=2e-4,\n  save_total_limit=2,\n  remove_unused_columns=False,\n  push_to_hub=False,\n  report_to='tensorboard',\n  load_best_model_at_end=True,\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T12:23:09.693467Z","iopub.execute_input":"2024-11-27T12:23:09.694282Z","iopub.status.idle":"2024-11-27T12:23:09.774138Z","shell.execute_reply.started":"2024-11-27T12:23:09.694247Z","shell.execute_reply":"2024-11-27T12:23:09.773163Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import Trainer\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    data_collator=collate_fn,\n    compute_metrics=compute_metrics,\n    train_dataset=prepared_ds['train'],\n    eval_dataset=prepared_ds[\"val\"],\n    tokenizer=processor,\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T12:23:10.761087Z","iopub.execute_input":"2024-11-27T12:23:10.761437Z","iopub.status.idle":"2024-11-27T12:23:12.227592Z","shell.execute_reply.started":"2024-11-27T12:23:10.761405Z","shell.execute_reply":"2024-11-27T12:23:12.226879Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training 👟👟","metadata":{}},{"cell_type":"code","source":"train_results = trainer.train()\ntrainer.save_model()\ntrainer.log_metrics(\"train\", train_results.metrics)\ntrainer.save_metrics(\"train\", train_results.metrics)\ntrainer.save_state()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-27T12:23:12.228974Z","iopub.execute_input":"2024-11-27T12:23:12.229262Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"metrics = trainer.evaluate(prepared_ds['val'])\ntrainer.log_metrics(\"eval\", metrics)\ntrainer.save_metrics(\"eval\", metrics)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predictions 📈","metadata":{}},{"cell_type":"code","source":"test  =trainer.predict(prepared_ds['test'], ignore_keys=['labels'])\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.predictions","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"probabilities = np.exp(test.predictions) / np.sum(np.exp(test.predictions), axis=1, keepdims=True)\nprobabilities\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_labels = np.argmax(probabilities,axis=1)\npred_labels[:5]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['labels'] = pred_labels","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = test_df.drop(columns='filepath').copy()\nsubmission = submission.rename(columns={'labels':'label'})\nsubmission","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Saving and uploading to huggingface model repository 🤗","metadata":{}},{"cell_type":"code","source":"trainer.save_model(\"/kaggle/working/cancer\")","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:17.091411Z","iopub.status.idle":"2024-11-27T11:54:17.09187Z","shell.execute_reply.started":"2024-11-27T11:54:17.091617Z","shell.execute_reply":"2024-11-27T11:54:17.091639Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import pipeline\npipe = pipeline(\"image-classification\", \n                model=model,\n               image_processor=processor)","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:17.093285Z","iopub.status.idle":"2024-11-27T11:54:17.093722Z","shell.execute_reply.started":"2024-11-27T11:54:17.09349Z","shell.execute_reply":"2024-11-27T11:54:17.093512Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from huggingface_hub import notebook_login\n\nnotebook_login()","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:17.096262Z","iopub.status.idle":"2024-11-27T11:54:17.09696Z","shell.execute_reply.started":"2024-11-27T11:54:17.096703Z","shell.execute_reply":"2024-11-27T11:54:17.096728Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer.push_to_hub(\"<username>/cancer-Vit-classification\")","metadata":{"execution":{"iopub.status.busy":"2024-11-27T11:54:17.098206Z","iopub.status.idle":"2024-11-27T11:54:17.098628Z","shell.execute_reply.started":"2024-11-27T11:54:17.098407Z","shell.execute_reply":"2024-11-27T11:54:17.098428Z"},"trusted":true},"outputs":[],"execution_count":null}]}