resnet-tiny-mnist / train.py
fxmarty's picture
added model
93f7d08
import logging
import sys
from dataclasses import dataclass, field
from typing import Optional
import datasets
import torch
import transformers
from torchinfo import summary
from torchvision.transforms import Compose, Normalize, ToTensor
from transformers import (
ConvNextFeatureExtractor,
HfArgumentParser,
ResNetConfig,
ResNetForImageClassification,
Trainer,
TrainingArguments,
)
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
import numpy as np
@dataclass
class DataTrainingArguments:
"""
Arguments pertaining to what data we are going to input our model for training and eval.
Using `HfArgumentParser` we can turn this class into argparse arguments to be able to specify
them on the command line.
"""
train_val_split: Optional[float] = field(
default=0.15, metadata={"help": "Percent to split off of train for validation."}
)
max_train_samples: Optional[int] = field(
default=None,
metadata={
"help": "For debugging purposes or quicker training, truncate the number of training examples to this "
"value if set."
},
)
max_eval_samples: Optional[int] = field(
default=None,
metadata={
"help": "For debugging purposes or quicker training, truncate the number of evaluation examples to this "
"value if set."
},
)
def collate_fn(examples):
pixel_values = torch.stack([example["pixel_values"] for example in examples])
labels = torch.tensor([example["label"] for example in examples])
return {"pixel_values": pixel_values, "labels": labels}
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.19.0.dev0")
require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-classification/requirements.txt")
logger = logging.getLogger(__name__)
def main():
parser = HfArgumentParser((DataTrainingArguments, TrainingArguments))
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
# If we pass only one argument to the script and it's the path to a json file,
# let's parse it to get our arguments.
data_args, training_args = parser.parse_json_file(
json_file=os.path.abspath(sys.argv[1])
)
else:
data_args, training_args = parser.parse_args_into_dataclasses()
# Setup logging
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout)],
)
log_level = training_args.get_process_log_level()
logger.setLevel(log_level)
transformers.utils.logging.set_verbosity(log_level)
transformers.utils.logging.enable_default_handler()
transformers.utils.logging.enable_explicit_format()
# Log on each process the small summary:
logger.warning(
f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"
+ f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}"
)
dataset = datasets.load_dataset("mnist")
data_args.train_val_split = (
None if "validation" in dataset.keys() else data_args.train_val_split
)
if isinstance(data_args.train_val_split, float) and data_args.train_val_split > 0.0:
split = dataset["train"].train_test_split(data_args.train_val_split)
dataset["train"] = split["train"]
dataset["validation"] = split["test"]
feature_extractor = ConvNextFeatureExtractor(
do_resize=False, do_normalize=False, image_mean=[0.45], image_std=[0.22]
)
config = ResNetConfig(
num_channels=1,
layer_type="basic",
depths=[2, 2],
hidden_sizes=[32, 64],
num_labels=10,
)
model = ResNetForImageClassification(config)
# Define torchvision transforms to be applied to each image.
normalize = Normalize(mean=feature_extractor.image_mean, std=feature_extractor.image_std)
_transforms = Compose([ToTensor(), normalize])
def transforms(example_batch):
"""Apply _train_transforms across a batch."""
# black and white
example_batch["pixel_values"] = [_transforms(pil_img.convert("L")) for pil_img in example_batch["image"]]
return example_batch
# Load the accuracy metric from the datasets package
metric = datasets.load_metric("accuracy")
# Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a
# predictions and label_ids field) and has to return a dictionary string to float.
def compute_metrics(p):
"""Computes accuracy on a batch of predictions"""
accuracy = metric.compute(predictions=np.argmax(p.predictions, axis=1), references=p.label_ids)
return accuracy
if training_args.do_train:
if data_args.max_train_samples is not None:
dataset["train"] = (
dataset["train"]
.shuffle(seed=training_args.seed)
.select(range(data_args.max_train_samples))
)
logger.info("Setting train transform")
# Set the training transforms
dataset["train"].set_transform(transforms)
if training_args.do_eval:
if "validation" not in dataset:
raise ValueError("--do_eval requires a validation dataset")
if data_args.max_eval_samples is not None:
dataset["validation"] = (
dataset["validation"]
.shuffle(seed=training_args.seed)
.select(range(data_args.max_eval_samples))
)
logger.info("Setting validation transform")
# Set the validation transforms
dataset["validation"].set_transform(transforms)
from transformers import trainer_utils
print(dataset)
training_args = transformers.TrainingArguments(
output_dir=training_args.output_dir,
do_eval=training_args.do_eval,
do_train=training_args.do_train,
logging_steps = 500,
eval_steps = 500,
save_steps= 500,
remove_unused_columns = False, # we need to pass the `label` and `image`
per_device_train_batch_size = 32,
save_total_limit = 2,
evaluation_strategy = "steps",
num_train_epochs = 6,
)
logger.info(f"Training/evaluation parameters {training_args}")
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset["train"] if training_args.do_train else None,
eval_dataset=dataset["validation"] if training_args.do_eval else None,
compute_metrics=compute_metrics,
tokenizer=feature_extractor,
data_collator=collate_fn,
)
# Training
if training_args.do_train:
train_result = trainer.train()
trainer.save_model()
trainer.log_metrics("train", train_result.metrics)
trainer.save_metrics("train", train_result.metrics)
trainer.save_state()
# Evaluation
if training_args.do_eval:
metrics = trainer.evaluate()
trainer.log_metrics("eval", metrics)
trainer.save_metrics("eval", metrics)
if __name__ == "__main__":
main()