Upload 7 files
Browse filesFiles changed (7)
- best_model.pthAdded+3
- categories.jsonAdded+27
- config.jsonAdded+38
- model.safetensorsAdded+3
- predict.pyAdded+42
- preprocessor_config.jsonAdded+22
- training_args.binAdded+3
123version https://git-lfs.github.com/spec/v1oid sha256:7a10d56e8da9c72b0531088816c554fd4b52646aa96da7ce2d2e8c03ad80526fsize 44794379123456789101112131415161718192021222324252627{ "smartphone": { "asaxiy": "https://asaxiy.uz/product/telefony-i-gadzhety/telefony/smartfony", "texnomart": "https://texnomart.uz/ru/katalog/smartfony/", "olcha": "https://olcha.uz/ru/category/telefony-gadzhety-aksessuary/telefony" }, "laptop": { "asaxiy": "https://asaxiy.uz/product/kompyutery-i-orgtehnika/noutbuki/noutbuki-2", "texnomart": "https://texnomart.uz/ru/katalog/noutbuki/", "olcha": "https://olcha.uz/ru/category/noutbuki-planshety-kompyutery/noutbuki" }, "tv": { "asaxiy": "https://asaxiy.uz/product/televizory-video-i-audio/televizory", "texnomart": "https://texnomart.uz/ru/katalog/televizory/", "olcha": "https://olcha.uz/ru/category/televizory-audio-i-videotekhnika/televizory" }, "refrigerator": { "asaxiy": "https://asaxiy.uz/product/bytovaya-tehnika/krupnaya-tehnika-dlya-kuhni/holodilniki", "texnomart": "https://texnomart.uz/ru/katalog/holodilniki/", "olcha": "https://olcha.uz/ru/category/tekhnika-dlya-kukhni/krupnaya-kukhonnaya-tekhnika/kholodilniki-i-morozilnye-kamery-1" }, "headphones": { "asaxiy": "https://asaxiy.uz/product/telefony-i-gadzhety/naushniki-i-auditexniki/besprovodniye-naushniki", "texnomart": "https://texnomart.uz/ru/katalog/naushniki", "olcha": "https://olcha.uz/ru/category/telefony-gadzhety-aksessuary/aksessuary/garnitury" }}1234567891011121314151617181920212223242526272829303132333435363738{ "_name_or_path": "google/vit-base-patch16-224-in21k", "architectures": [ "ViTForImageClassification" ], "attention_probs_dropout_prob": 0.0, "encoder_stride": 16, "hidden_act": "gelu", "hidden_dropout_prob": 0.0, "hidden_size": 768, "id2label": { "0": "headphones", "1": "laptop", "2": "smartphone", "3": "tv", "4": "watch" }, "image_size": 224, "initializer_range": 0.02, "intermediate_size": 3072, "label2id": { "headphones": "0", "laptop": "1", "smartphone": "2", "tv": "3", "watch": "4" }, "layer_norm_eps": 1e-12, "model_type": "vit", "num_attention_heads": 12, "num_channels": 3, "num_hidden_layers": 12, "patch_size": 16, "problem_type": "single_label_classification", "qkv_bias": true, "torch_dtype": "float32", "transformers_version": "4.46.3"}123version https://git-lfs.github.com/spec/v1oid sha256:91a33a308ad342631146ff9e6491891f9042b2cc9142991aac8c712a93c63534size 343233204123456789101112131415161718192021222324252627282930313233343536373839404142"""
Инференс через дообученный google/vit-base-patch16-224-in21k.
"""
import sys
from pathlib import Path
import torch
from PIL import Image
from transformers import ViTForImageClassification, ViTImageProcessor
BASE_DIR = Path(__file__).resolve().parent
MODEL_DIR = BASE_DIR / "models" / "vit-product-classifier"
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def predict(img_path: str):
if not MODEL_DIR.exists():
print(f"Ошибка: папка модели {MODEL_DIR} не найдена. Сначала запусти train.py!")
return
processor = ViTImageProcessor.from_pretrained(str(MODEL_DIR))
model = ViTForImageClassification.from_pretrained(str(MODEL_DIR)).to(DEVICE).eval()
image = Image.open(img_path).convert("RGB")
inputs = processor(images=image, return_tensors="pt").to(DEVICE)
with torch.no_grad():
outputs = model(**inputs)
probs = torch.softmax(outputs.logits, dim=1)[0]
top_prob, top_class_idx = torch.max(probs, 0)
class_name = model.config.id2label[top_class_idx.item()]
print(f"\nИзображение: {img_path}")
print(f"Предсказание: {class_name} ({top_prob.item() * 100:.2f}%)")
if __name__ == "__main__":
if len(sys.argv) > 1:
predict(sys.argv[1])
else:
print("Использование: python predict.py <путь_к_картинке>")12345678910111213141516171819202122{ "do_normalize": true, "do_rescale": true, "do_resize": true, "image_mean": [ 0.5, 0.5, 0.5 ], "image_processor_type": "ViTImageProcessor", "image_std": [ 0.5, 0.5, 0.5 ], "resample": 2, "rescale_factor": 0.00392156862745098, "size": { "height": 224, "width": 224 }}123version https://git-lfs.github.com/spec/v1oid sha256:ec41433b3fa38bffbabc5bba4ca50e3370410041b7fe5280a427a34e47d8be0asize 5713best_model.pthAdded+3
categories.jsonAdded+27
config.jsonAdded+38
model.safetensorsAdded+3
predict.pyAdded+42
preprocessor_config.jsonAdded+22
training_args.binAdded+3