Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
|
transformer [2025/11/12 22:16] jango angelegt |
transformer [2026/03/13 23:45] (aktuell) jango ↷ Seitename wurde von transformers auf transformer geändert |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | Transformers ist eine spezielle Architektur von [[LLM]] Modellen und auch eine [[coding: | ||
| + | =====Text generation===== | ||
| + | |||
| + | <code python> | ||
| + | from transformers import GPT2Tokenizer, | ||
| + | |||
| + | tokenizer = GPT2Tokenizer.from_pretrained(" | ||
| + | model = GPT2LMHeadModel.from_pretrained(" | ||
| + | |||
| + | generator = pipeline(model=model, | ||
| + | |||
| + | result = generator(" | ||
| + | |||
| + | print(result) | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | #pip install einops | ||
| + | |||
| + | from transformers import AutoTokenizer, | ||
| + | import transformers | ||
| + | import torch | ||
| + | |||
| + | model = " | ||
| + | |||
| + | tokenizer = AutoTokenizer.from_pretrained(model, | ||
| + | pipeline = transformers.pipeline( | ||
| + | " | ||
| + | model=model, | ||
| + | tokenizer=tokenizer, | ||
| + | torch_dtype=torch.bfloat16, | ||
| + | trust_remote_code=True, | ||
| + | device_map=" | ||
| + | ) | ||
| + | sequences = pipeline( | ||
| + | " | ||
| + | max_length=100, | ||
| + | do_sample=True, | ||
| + | top_k=10, | ||
| + | num_return_sequences=1, | ||
| + | eos_token_id=tokenizer.eos_token_id, | ||
| + | ) | ||
| + | for seq in sequences: | ||
| + | print(f" | ||
| + | </ | ||
| + | =====Object detection===== | ||
| + | |||
| + | Siehe auch [[yolov5|Yolov5]]. | ||
| + | |||
| + | <code python> | ||
| + | from transformers import DetrImageProcessor, | ||
| + | import torch | ||
| + | from PIL import Image | ||
| + | import requests | ||
| + | |||
| + | url = " | ||
| + | image = Image.open(requests.get(url, | ||
| + | |||
| + | processor = DetrImageProcessor.from_pretrained(" | ||
| + | model = DetrForObjectDetection.from_pretrained(" | ||
| + | |||
| + | inputs = processor(images=image, | ||
| + | outputs = model(**inputs) | ||
| + | |||
| + | # convert outputs (bounding boxes and class logits) to COCO API | ||
| + | # let's only keep detections with score > 0.9 | ||
| + | target_sizes = torch.tensor([image.size[:: | ||
| + | results = processor.post_process_object_detection(outputs, | ||
| + | |||
| + | for score, label, box in zip(results[" | ||
| + | box = [round(i, 2) for i in box.tolist()] | ||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | from transformers import pipeline | ||
| + | from PIL import Image, ImageDraw, ImageFont | ||
| + | from transformers import DetrFeatureExtractor, | ||
| + | |||
| + | #classifier = pipeline(" | ||
| + | #classifier = pipeline(" | ||
| + | #classifier = pipeline(" | ||
| + | #classifier = pipeline(" | ||
| + | |||
| + | feature_extractor = DetrFeatureExtractor.from_pretrained(' | ||
| + | model = DetrForObjectDetection.from_pretrained(' | ||
| + | object_detector = pipeline(" | ||
| + | |||
| + | def draw_bounding_box(im, | ||
| + | im_with_rectangle = ImageDraw.Draw(im) | ||
| + | im_with_rectangle.rounded_rectangle((xmin, | ||
| + | im_with_rectangle.text((xmin+10, | ||
| + | return im, label | ||
| + | |||
| + | with Image.open(" | ||
| + | bounding_boxes = object_detector(im) | ||
| + | num_boxes = len(bounding_boxes) | ||
| + | index = 0 | ||
| + | labels = [] | ||
| + | for bounding_box in bounding_boxes: | ||
| + | box = bounding_box[" | ||
| + | im, label = draw_bounding_box(im, | ||
| + | #if label not in labels: | ||
| + | labels.append(label) | ||
| + | index += 1 | ||
| + | # | ||
| + | im.show() | ||
| + | print(labels) | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | from transformers import ViTImageProcessor, | ||
| + | from PIL import Image | ||
| + | import requests | ||
| + | |||
| + | url = ' | ||
| + | image = Image.open(requests.get(url, | ||
| + | |||
| + | processor = ViTImageProcessor.from_pretrained(' | ||
| + | model = ViTForImageClassification.from_pretrained(' | ||
| + | |||
| + | inputs = processor(images=image, | ||
| + | outputs = model(**inputs) | ||
| + | logits = outputs.logits | ||
| + | # model predicts one of the 1000 ImageNet classes | ||
| + | predicted_class_idx = logits.argmax(-1).item() | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | =====Links===== | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||