Make Predictions?

#1
by sudo-s - opened

Hi All, Please how can I make predictions?

Hey there, you can make predictions with the pipeline abstraction.

import requests
from PIL import Image

from transformers import pipeline

# Initialize pipeline from repo
pipe = pipeline(model="nateraw/vit-base-beans")

# Get example image from repo
url = 'https://huggingface.co/nateraw/vit-base-beans/resolve/main/angular_leaf_spot.jpeg'
image = Image.open(requests.get(url, stream=True).raw)

preds = pipe(image)

When you print preds, you get this:

[{'label': 'angular_leaf_spot', 'score': 0.9455905556678772},
 {'label': 'bean_rust', 'score': 0.0272879246622324},
 {'label': 'healthy', 'score': 0.027121491730213165}]

Thanks a lot
I have a question : I want to use your code on a another datasets , so how can I save my model like you did with yours "nateraw/vit-base-beans" so that I can use my model for predections?

Cant get inference to run, even though training went well. When I then take your code above I get:

Downloading pytorch_model.bin: 0%| | 0.00/343M [00:10<?, ?B/s]
Traceback (most recent call last):
File "inference.py", line 15, in
pipe = pipeline(model="nateraw/vit-base-beans")
File "/usr/local/lib/python3.8/dist-packages/transformers/pipelines/init.py", line 776, in pipeline
framework, model = infer_framework_load_model(
File "/usr/local/lib/python3.8/dist-packages/transformers/pipelines/base.py", line 271, in infer_framework_load_model
raise ValueError(f"Could not load model {model} with any of the following classes: {class_tuple}.")
ValueError: Could not load model nateraw/vit-base-beans with any of the following classes: (<class 'transformers.models.auto.modeling_auto.AutoModelForImageClassification'>, <class 'transformers.models.vit.modeling_vit.ViTForImageClassification'>).

Thanks a lot
I have a question : I want to use your code on a another datasets , so how can I save my model like you did with yours "nateraw/vit-base-beans" so that I can use my model for predections?

i have same question with you, i want save my model so i can use it for my prediction? what should i do?

Got it to run round trip.

Here my training:

from transformers import ViTFeatureExtractor

model_name_or_path = 'google/vit-base-patch16-224-in21k'
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name_or_path)

print (feature_extractor)
print (feature_extractor(image, return_tensors='pt')) # as a torch tensor

def process_example(example):
inputs = feature_extractor(example['image'], return_tensors='pt')
inputs['labels'] = example['labels']
return inputs

process_example(ds['train'][0])

def transform(example_batch):
# Take a list of PIL images and turn them to pixel values
inputs = feature_extractor([x for x in example_batch['image']], return_tensors='pt')

# Don't forget to include the labels!
inputs['labels'] = example_batch['labels']
return inputs

prepared_ds = ds.with_transform(transform)

prepared_ds['train'][0:2]

import torch

def collate_fn(batch):
return {
'pixel_values': torch.stack([x['pixel_values'] for x in batch]),
'labels': torch.tensor([x['labels'] for x in batch])
}

import numpy as np
from datasets import load_metric

metric = load_metric("accuracy")
def compute_metrics(p):
return metric.compute(predictions=np.argmax(p.predictions, axis=1), references=p.label_ids)

from transformers import ViTForImageClassification

labels = ds['train'].features['labels'].names

print (labels)

model = ViTForImageClassification.from_pretrained(
model_name_or_path,
num_labels=len(labels),
id2label={str(i): c for i, c in enumerate(labels)},
label2id={c: str(i) for i, c in enumerate(labels)}
).to("cuda")

from transformers import TrainingArguments

training_args = TrainingArguments(
output_dir="tarkov_models/vit-base-tarkov-home_bs128_8ep",
per_device_train_batch_size=128,
evaluation_strategy="steps",
num_train_epochs=8, # 4
#max_steps=16, # 4
fp16=False, # True
save_steps=1000, # 100
eval_steps=1000, # 100
logging_steps=1000,
learning_rate=2e-4,
save_total_limit=3,
remove_unused_columns=False,
push_to_hub=False,
report_to='tensorboard',
load_best_model_at_end=True,
)

from transformers import Trainer

trainer = Trainer(
model=model,
args=training_args,
data_collator=collate_fn,
compute_metrics=compute_metrics,
train_dataset=prepared_ds["train"],
eval_dataset=prepared_ds["validation"],
tokenizer=feature_extractor,
)

train_results = trainer.train()
trainer.save_model()
trainer.log_metrics("train", train_results.metrics)
trainer.save_metrics("train", train_results.metrics)
trainer.save_state()

metrics = trainer.evaluate(prepared_ds['validation'])
trainer.log_metrics("eval", metrics)
trainer.save_metrics("eval", metrics)

And inference:

sPath_model = 'path to your local model'

config = ViTConfig.from_pretrained(sPath_model)
model = ViTForImageClassification.from_pretrained(sPath_model, config=config)
processor = ViTImageProcessor.from_pretrained(sPath_model, config=config)
feature_extractor = ViTFeatureExtractor.from_pretrained(sPath_model)

def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)

def main(image):
inputs = processor(images=image, return_tensors="pt")
output = model(**inputs)
logits = output.logits
logits_detached = logits.detach().numpy()[0]
logits_detached_play = logits_detached.copy()

aSoftmaxed = softmax(logits_detached)

aPred = []
aScor = []

print (logits_detached_play)

show top ten results

for i in range(0,10):  
    pred_class = np.argmax(logits_detached_play)
    score = logits_detached[pred_class]
    probability = aSoftmaxed[pred_class]
    sLabel = config.id2label[pred_class]

    aPred.append((score, probability, sLabel))

    logits_detached_play[pred_class] = -999999
 
return aPred

And inference:

sPath_model = 'path to your local model'

config = ViTConfig.from_pretrained(sPath_model)
model = ViTForImageClassification.from_pretrained(sPath_model, config=config)
processor = ViTImageProcessor.from_pretrained(sPath_model, config=config)
feature_extractor = ViTFeatureExtractor.from_pretrained(sPath_model)

def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)

def main(image):
inputs = processor(images=image, return_tensors="pt")
output = model(**inputs)
logits = output.logits
logits_detached = logits.detach().numpy()[0]
logits_detached_play = logits_detached.copy()

aSoftmaxed = softmax(logits_detached)

aPred = []
aScor = []

print (logits_detached_play)

show top ten results

for i in range(0,10):  
    pred_class = np.argmax(logits_detached_play)
    score = logits_detached[pred_class]
    probability = aSoftmaxed[pred_class]
    sLabel = config.id2label[pred_class]

    aPred.append((score, probability, sLabel))

    logits_detached_play[pred_class] = -999999
 
return aPred

i got error in logits_detached_play not defined. what should i do?

That cell needs to be indented (part of main())

Thanks a lot
I have a question : I want to use your code on a another datasets , so how can I save my model like you did with yours "nateraw/vit-base-beans" so that I can use my model for predections?

image.png

from PIL import Image

feature_extractor = ViTImageProcessor.from_pretrained(model_name_or_path)

image = Image.open("/content/angular_leaf_spot.jpeg")
model = ViTForImageClassification.from_pretrained('/content/vit-base-beans')
inputs = feature_extractor(images =image, return_tensors='pt')

outputs = model(**inputs)
logits = outputs.logits

predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])

Sign up or log in to comment