CoolFace
Modelpublic

lxyuan/vit-xray-pneumonia-classification

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
6likes175downloads
Model Card

<!-- This model card has been generated automatically according to the information the Trainer had access to. You should probably proofread and complete it, then remove this comment. -->

vit-xray-pneumonia-classification

This model is a fine-tuned version of google/vit-base-patch16-224-in21k on the chest-xray-classification dataset. It achieves the following results on the evaluation set:

  • Loss: 0.0868
  • Accuracy: 0.9742

Inference example

python
from transformers import pipeline

classifier = pipeline(model="lxyuan/vit-xray-pneumonia-classification")

# image taken from https://www.news-medical.net/health/What-is-Viral-Pneumonia.aspx
classifier("https://d2jx2rerrg6sh3.cloudfront.net/image-handler/ts/20200618040600/ri/650/picture/2020/6/shutterstock_786937069.jpg")

>>>
[{'score': 0.990334689617157, 'label': 'PNEUMONIA'},
 {'score': 0.009665317833423615, 'label': 'NORMAL'}]

Training procedure

Notebook link: here

Training hyperparameters

The following hyperparameters were used during training:

  • learning_rate: 5e-05
  • trainbatchsize: 16
  • evalbatchsize: 16
  • seed: 42
  • gradientaccumulationsteps: 4
  • totaltrainbatch_size: 64
  • optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
  • lrschedulertype: linear
  • lrschedulerwarmup_ratio: 0.1
  • num_epochs: 15
python
from transformers import EarlyStoppingCallback

training_args = TrainingArguments(
    output_dir="vit-xray-pneumonia-classification",
    remove_unused_columns=False,
    evaluation_strategy="epoch",
    save_strategy="epoch",
    logging_strategy="epoch",
    learning_rate=5e-5,
    per_device_train_batch_size=16,
    gradient_accumulation_steps=4,
    per_device_eval_batch_size=16,
    num_train_epochs=15,
    save_total_limit=2,
    warmup_ratio=0.1,
    load_best_model_at_end=True,
    metric_for_best_model="eval_loss",
    greater_is_better=False,
    fp16=True,
    push_to_hub=True,
    report_to="tensorboard"
)

early_stopping = EarlyStoppingCallback(early_stopping_patience=3)

trainer = Trainer(
    model=model,
    args=training_args,
    data_collator=data_collator,
    train_dataset=train_ds,
    eval_dataset=val_ds,
    tokenizer=processor,
    compute_metrics=compute_metrics,
    callbacks=[early_stopping],
)

Training results

Training LossEpochStepValidation LossAccuracy
0.51520.99630.25070.9245
0.23341.991270.17660.9382
0.16473.01910.12180.9588
0.1444.02550.12220.9502
0.13484.993180.12930.9571
0.12765.993820.10000.9665
0.11757.04460.11770.9502
0.1098.05100.10790.9665
0.09148.995730.08040.9717
0.08729.996370.08000.9717
0.080411.07010.08620.9682
0.093512.07650.08830.9657
0.068612.998280.08680.9742

Framework versions

  • Transformers 4.30.2
  • Pytorch 1.9.0+cu102
  • Datasets 2.12.0
  • Tokenizers 0.13.3