ancebuc commited on
Commit
eb64ea1
verified
1 Parent(s): cec5e36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -3
app.py CHANGED
@@ -7,11 +7,60 @@ repo_id = "ancebuc/grapes-segmentation"
7
  learner = from_pretrained_fastai(repo_id)
8
  labels = learner.dls.vocab
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # Definimos una funci贸n que se encarga de llevar a cabo las predicciones
11
  def predict(img):
12
- #img = PILImage.create(img)
13
- pred,pred_idx,probs = learner.predict(img)
14
- return {labels[i]: float(probs[i]) for i in range(len(labels))}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # Creamos la interfaz y la lanzamos.
17
  gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.inputs.Image(shape=(128, 128)),examples=['color_158.jpg','color_157.jpg']).launch(share=False)
 
7
  learner = from_pretrained_fastai(repo_id)
8
  labels = learner.dls.vocab
9
 
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ model = torch.jit.load("unet.pth")
12
+ model = model.cpu()
13
+ model.eval()
14
+
15
+ import torchvision.transforms as transforms
16
+ def transform_image(image):
17
+ my_transforms = transforms.Compose([transforms.ToTensor(),
18
+ transforms.Normalize(
19
+ [0.485, 0.456, 0.406],
20
+ [0.229, 0.224, 0.225])])
21
+ image_aux = image
22
+ return my_transforms(image_aux).unsqueeze(0).to(device)
23
+
24
  # Definimos una funci贸n que se encarga de llevar a cabo las predicciones
25
  def predict(img):
26
+ img = PILImage.create(img)
27
+
28
+ image = transforms.Resize((480,640))(img)
29
+ tensor = transform_image(image=image)
30
+
31
+ with torch.no_grad():
32
+ outputs = model(tensor)
33
+
34
+ outputs = torch.argmax(outputs,1)
35
+
36
+ mask = np.array(outputs.cpu())
37
+ mask = np.reshape(mask,(480,640))
38
+
39
+ # A帽adimos una dimesionalidad para colocar color
40
+ mask = np.expand_dims(mask, axis=2)
41
+
42
+ # Y a帽adimos los tres canales
43
+ mask = np.repeat(mask, 3, axis=2)
44
+
45
+ # Creamos las m谩scaras
46
+ uvas = np.all(mask == [1, 1, 1], axis=2)
47
+ hojas = np.all(mask == [2, 2, 2], axis=2)
48
+ poste = np.all(mask == [3, 3, 3], axis=2)
49
+ madera = np.all(mask == [4, 4, 4], axis=2)
50
+
51
+ # Uvas
52
+ mask[uvas] = [255, 255, 255]
53
+
54
+ # Hojas
55
+ mask[hojas] = [0, 255, 0]
56
+
57
+ # Poste
58
+ mask[poste] = [0, 0, 255]
59
+
60
+ # Madera
61
+ mask[madera] = [255, 0, 0]
62
+
63
+ return Image.fromarray(mask.astype('uint8'))
64
 
65
  # Creamos la interfaz y la lanzamos.
66
  gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.inputs.Image(shape=(128, 128)),examples=['color_158.jpg','color_157.jpg']).launch(share=False)