paascorb commited on
Commit
86b80d6
1 Parent(s): 4b036ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -54
app.py CHANGED
@@ -1,74 +1,80 @@
1
  from huggingface_hub import from_pretrained_fastai
2
  import gradio as gr
 
 
3
  from fastai.vision.all import *
 
 
 
 
 
 
 
4
  import PIL
5
  import torchvision.transforms as transforms
6
- # from albumentations import (
7
- # Compose,
8
- # OneOf,
9
- # ElasticTransform,
10
- # GridDistortion,
11
- # OpticalDistortion,
12
- # HorizontalFlip,
13
- # Rotate,
14
- # Transpose,
15
- # CLAHE,
16
- # ShiftScaleRotate
17
- # )
18
 
19
- # def get_y_fn (x):
20
- # return Path(str(x).replace("Images","Labels").replace("color","gt").replace(".jpg",".png"))
21
 
22
- # def ParentSplitter(x):
23
- # return Path(x).parent.name==test_name
24
 
25
- # class SegmentationAlbumentationsTransform(ItemTransform):
26
- # split_idx = 0
27
 
28
- # def __init__(self, aug):
29
- # self.aug = aug
30
 
31
- # def encodes(self, x):
32
- # img,mask = x
33
- # aug = self.aug(image=np.array(img), mask=np.array(mask))
34
- # return PILImage.create(aug["image"]), PILMask.create(aug["mask"])
35
 
36
- # transforms=Compose([HorizontalFlip(p=0.5),
37
- # Rotate(p=0.40,limit=10),GridDistortion()
38
- # ],p=1)
39
- # transformPipeline=SegmentationAlbumentationsTransform(transforms)
40
 
41
- # class TargetMaskConvertTransform(ItemTransform):
42
- # def __init__(self):
43
- # pass
44
- # def encodes(self, x):
45
- # img,mask = x
46
 
47
- # #Convert to array
48
- # mask = np.array(mask)
49
 
50
- # # Aquí definimos cada clase en la máscara
51
- # # uva:
52
- # mask[mask==255]=1
53
- # # hojas:
54
- # mask[mask==150]=2
55
- # # conductores:
56
- # mask[mask==76]=3
57
- # mask[mask==74]=3
58
- # # madera:
59
- # mask[mask==29]=4
60
- # mask[mask==25]=4
61
 
62
- # # Back to PILMask
63
- # mask = PILMask.create(mask)
64
- # return img, mask
65
-
66
- # repo_id = "paascorb/practica3_Segmentation"
67
 
68
- # learner = from_pretrained_fastai(repo_id)
69
 
70
- learn = unet_learner(None,resnet18,cbs=None,metrics=[DiceMulti()]).to_fp16()
71
- learn.load('model')
72
 
73
  def transform_image(image):
74
  my_transforms = transforms.Compose([transforms.ToTensor(),
 
1
  from huggingface_hub import from_pretrained_fastai
2
  import gradio as gr
3
+ from fastai.basics import *
4
+ from fastai.vision import models
5
  from fastai.vision.all import *
6
+ from fastai.metrics import *
7
+ from fastai.data.all import *
8
+ from fastai.callback import *
9
+
10
+
11
+ from pathlib import Path
12
+ import random
13
  import PIL
14
  import torchvision.transforms as transforms
15
+ from albumentations import (
16
+ Compose,
17
+ OneOf,
18
+ ElasticTransform,
19
+ GridDistortion,
20
+ OpticalDistortion,
21
+ HorizontalFlip,
22
+ Rotate,
23
+ Transpose,
24
+ CLAHE,
25
+ ShiftScaleRotate
26
+ )
27
 
28
+ def get_y_fn (x):
29
+ return Path(str(x).replace("Images","Labels").replace("color","gt").replace(".jpg",".png"))
30
 
31
+ def ParentSplitter(x):
32
+ return Path(x).parent.name==test_name
33
 
34
+ class SegmentationAlbumentationsTransform(ItemTransform):
35
+ split_idx = 0
36
 
37
+ def __init__(self, aug):
38
+ self.aug = aug
39
 
40
+ def encodes(self, x):
41
+ img,mask = x
42
+ aug = self.aug(image=np.array(img), mask=np.array(mask))
43
+ return PILImage.create(aug["image"]), PILMask.create(aug["mask"])
44
 
45
+ transforms=Compose([HorizontalFlip(p=0.5),
46
+ Rotate(p=0.40,limit=10),GridDistortion()
47
+ ],p=1)
48
+ transformPipeline=SegmentationAlbumentationsTransform(transforms)
49
 
50
+ class TargetMaskConvertTransform(ItemTransform):
51
+ def __init__(self):
52
+ pass
53
+ def encodes(self, x):
54
+ img,mask = x
55
 
56
+ #Convert to array
57
+ mask = np.array(mask)
58
 
59
+ # Aquí definimos cada clase en la máscara
60
+ # uva:
61
+ mask[mask==255]=1
62
+ # hojas:
63
+ mask[mask==150]=2
64
+ # conductores:
65
+ mask[mask==76]=3
66
+ mask[mask==74]=3
67
+ # madera:
68
+ mask[mask==29]=4
69
+ mask[mask==25]=4
70
 
71
+ # Back to PILMask
72
+ mask = PILMask.create(mask)
73
+ return img, mask
 
 
74
 
75
+ repo_id = "paascorb/practica3_Segmentation"
76
 
77
+ learner = from_pretrained_fastai(repo_id)
 
78
 
79
  def transform_image(image):
80
  my_transforms = transforms.Compose([transforms.ToTensor(),