Spaces:
Sleeping
Sleeping
Upload 5 files
Browse filesFiles changed (5)
- 2025-06-30_23-46-38_EfficientNet_epoch7_vallloss0.0733.pthAdded+3
- app.pyAdded+80
- class_names.jsonAdded+102
- model.pyAdded+8
- requirements.txtAdded+8
123version https://git-lfs.github.com/spec/v1oid sha256:03bb5f9a6f4fd48d3569bec3bdb5dea69193166e3356423182a29d9c59e930bcsize 1539441881234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import gradio as grimport torchimport jsonimport numpy as npfrom PIL import Imageimport albumentations as Afrom albumentations.pytorch import ToTensorV2from model import create_flower_modelfrom timeit import default_timer as timerfrom typing import Tuple, Dictimport os
# クラス名読み込みwith open("class_names.json", "r") as f: class_names = json.load(f)
# 推論用transforminference_transform = A.Compose([ A.Resize(224, 224), A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ToTensorV2()])
# モデル読み込みmodel = create_sport_model(num_classes=len(class_names))
# 保存されたチェックポイントを読み込みcheckpoint = torch.load("checkpoints/2025-06-30_23-46-38_EfficientNet_epoch7_vallloss0.0733.pth", map_location=torch.device("cpu"))
# チェックポイントの構造を確認して適切に読み込みif 'model_dict' in checkpoint: model.load_state_dict(chekpoint['model_dict'])elif 'state_dict' in checkpoint: model.load_state_dict(checkpoint['state_dict'])else: model.load_state_dict(checkpoint)
model.eval()
def predict(image) -> Tuple[Dict, float]: start_time = timer()
image = np.array(image)
transformed = inference_transform(image=image) image_tensor = transformed['image'].unsqueeze(0)
# 推論 with torch.no_grad(): outputs = model(image_tensor) probabilities = torch.softmax(outputs, dim=1)
# 結果を辞書形式に変換 pred_dict = {class_names[i]: float(probabilities[0][i]) for i in range(len(class_names))} pred_time = round(timer() - start_time, 4) return pred_dict, pred_time
# Gradio インターフェースtitle = " Sport Classifier "description = "An EfficientNetV2 model to classify 100 different types of Sports. Upload an image to see the prediction!"article = "Built with PyTorch and timm, deployed on Hugging Face Spaces."
demo = gr.Interface( fn=predict, inputs=gr.Image(type='pil'), outputs=[ gr.label(num_top_classes=10, label="Predictions), gr.Number(label="Predicion time (s)") ], title=title, description=description, article=article)
if __name__=="__main__": demo.launch()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102[ "air hockey", "ampute football", "archery", "arm wrestling", "axe throwing", "balance beam", "barell racing", "baseball", "basketball", "baton twirling", "bike polo", "billiards", "bmx", "bobsled", "bowling", "boxing", "bull riding", "bungee jumping", "canoe slamon", "cheerleading", "chuckwagon racing", "cricket", "croquet", "curling", "disc golf", "fencing", "field hockey", "figure skating men", "figure skating pairs", "figure skating women", "fly fishing", "football", "formula 1 racing", "frisbee", "gaga", "giant slalom", "golf", "hammer throw", "hang gliding", "harness racing", "high jump", "hockey", "horse jumping", "horse racing", "horseshoe pitching", "hurdles", "hydroplane racing", "ice climbing", "ice yachting", "jai alai", "javelin", "jousting", "judo", "lacrosse", "log rolling", "luge", "motorcycle racing", "mushing", "nascar racing", "olympic wrestling", "parallel bar", "pole climbing", "pole dancing", "pole vault", "polo", "pommel horse", "rings", "rock climbing", "roller derby", "rollerblade racing", "rowing", "rugby", "sailboat racing", "shot put", "shuffleboard", "sidecar racing", "ski jumping", "sky surfing", "skydiving", "snow boarding", "snowmobile racing", "speed skating", "steer wrestling", "sumo wrestling", "surfing", "swimming", "table tennis", "tennis", "track bicycle", "trapeze", "tug of war", "ultimate", "uneven bars", "volleyball", "water cycling", "water polo", "weightlifting", "wheelchair basketball", "wheelchair racing", "wingsuit flying"]12345678import torchimport timmfrom torch import nn
def create_sports_model(num_classes=14): """sports100classのEfficientNetV2モデルを作成(timm使用)""" model = timm.create_model('efficientnetv2_rw_t.ra2_in1k', pretrained=True, num_classes=len(class_names)) return model12345678torchtorchvisiongradioalbumentationsopencv-python-headlessPillownumpytimm2025-06-30_23-46-38_EfficientNet_epoch7_vallloss0.0733.pthAdded+3
app.pyAdded+80
class_names.jsonAdded+102
model.pyAdded+8
requirements.txtAdded+8