smaciu commited on
Commit
331086d
·
1 Parent(s): 3a1f81e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +57 -7
README.md CHANGED
@@ -3,18 +3,24 @@ tags:
3
  - fastai
4
  ---
5
 
6
- # Amazing!
 
 
7
 
8
- 🥳 Congratulations on hosting your fastai model on the Hugging Face Hub!
9
 
10
- # Some next steps
11
- 1. Fill out this model card with more information (see the template below and the [documentation here](https://huggingface.co/docs/hub/model-repos))!
 
 
 
 
 
 
 
 
12
 
13
- 2. Create a demo in Gradio or Streamlit using 🤗 Spaces ([documentation here](https://huggingface.co/docs/hub/spaces)).
14
 
15
- 3. Join the fastai community on the [Fastai Discord](https://discord.com/invite/YKrxeNn)!
16
 
17
- Greetings fellow fastlearner 🤝! Don't forget to delete this content from your model card.
18
 
19
 
20
  ---
@@ -29,4 +35,48 @@ More information needed
29
  More information needed
30
 
31
  ## Training and evaluation data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  More information needed
 
3
  - fastai
4
  ---
5
 
6
+ from huggingface_hub import from_pretrained_fastai
7
+ from fastai.vision.all import *
8
+ def label_func(f): return f.name[:2]
9
 
 
10
 
11
+ learn = from_pretrained_fastai("smaciu/bee-wings-classifier")
12
+
13
+
14
+ def predict_image(image_path):
15
+ img = PILImage.create(image_path)
16
+ bee,_,probs = learn.predict(img)
17
+ return bee, max(probs)
18
+
19
+ cat, prob = predict_image('path_to_image')
20
+ print(f"Honey bee from: {cat}. {100*prob.item():.2f}%")
21
 
 
22
 
 
23
 
 
24
 
25
 
26
  ---
 
35
  More information needed
36
 
37
  ## Training and evaluation data
38
+
39
+ from fastai.vision.all import *
40
+
41
+ from huggingface_hub import from_pretrained_fastai
42
+
43
+ from datasets import load_dataset
44
+
45
+ def label_func(f): return f.name[:2]
46
+
47
+ dataset = load_dataset("smaciu/bee-wings-large", split='train')
48
+
49
+
50
+ def get_items(o):
51
+ return range(len(dataset))
52
+
53
+ def get_x(i):
54
+ return dataset[i]['image']
55
+
56
+ def get_y(i):
57
+ return dataset[i]['label']
58
+
59
+ bee_wing_stats =([0.7641, 0.7641, 0.7641], [0.1771, 0.1771, 0.1771]) # dataset mean and std to normalise
60
+
61
+ # 2. Create a FastAI DataBlock
62
+ dls = DataBlock(
63
+ blocks=(ImageBlock, CategoryBlock),
64
+ get_items=get_items,
65
+ get_x=get_x, # function to get the image file path
66
+ get_y=get_y, # function to get the image label
67
+ splitter=RandomSplitter(valid_pct=0.2), # random split with 20% in validation set
68
+ item_tfms=Resize(460), # item transforms
69
+ batch_tfms= [Normalize.from_stats(*bee_wing_stats)]).dataloaders(dataset,bs=64, num_workers=num_cpus(), pin_memory=True).to('device')
70
+
71
+ cbfs = [
72
+ #ShowGraphCallback,
73
+ #ReduceLROnPlateau(monitor='valid_loss', min_delta=0.1, patience=2),
74
+ ]
75
+
76
+ learn = from_pretrained_fastai("smaciu/bee-wings-classifier").to_channelslast()
77
+
78
+ learn.dls = dls
79
+
80
+ learn.fit_one_cycle(1, lr_max=1.4858086386360967e-08)
81
+
82
  More information needed