faustoont commited on
Commit
0b57da2
·
1 Parent(s): 787a15f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -32
app.py CHANGED
@@ -1,13 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from io import StringIO
3
  from PIL import Image
4
  import pandas as pd
5
- import numpy as np
6
  import torch
7
  import torch.nn as nn
8
- import cv2
 
 
9
  from albumentations.pytorch.transforms import ToTensorV2
10
 
 
 
 
 
 
 
 
11
  id2class = {0: 'agricultural', 1: 'airplane', 2: 'baseballdiamond', 3: 'beach', 4: 'buildings', 5: 'chaparral', 6: 'denseresidential', 7: 'forest', 8: 'freeway', 9: 'golfcourse', 10: 'intersection', 11: 'mediumresidential', 12: 'mobilehomepark', 13: 'overpass', 14: 'parkinglot', 15: 'river', 16: 'runway', 17: 'sparseresidential', 18: 'storagetanks', 19: 'tenniscourt', 20: 'harbor'}
12
 
13
  model = models.resnet50(weights=None)
@@ -15,36 +62,22 @@ model.fc = nn.Linear(2048, 21)
15
  model.load_state_dict(torch.load('resnet_best.pth', map_location=torch.device('cpu')), strict=True)
16
  model.eval()
17
 
 
 
 
 
 
 
 
18
 
 
 
 
19
 
20
- #Load model torch.load.state_dict(PATH)
21
- #model = torch.load.state_dict('resnet_best.pth')
22
- #model = models.resnet50(pretrained=False)
23
- #model.fc = nn.Linear(2048, 21)
24
- #model.load_state_dict(torch.load('resnet_best.pth'), strict=True)#load weights from training run
25
- #model.eval()
26
-
27
- st.title("My awesome ML Function")
28
-
29
- uploaded_file = st.file_uploader("Choose a file")
30
 
31
- if uploaded_file is not None:
32
- if ".jpg" in uploaded_file.name or ".png" in uploaded_file.name: #image check
33
- image = Image.open(uploaded_file)
34
- st.image(image) #showing the image
35
- np_img = np.array(image)
36
- cv_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
37
-
38
- # Let's resize the image and make a pytorch tensor from the image np array.
39
- cust_transform = A.Compose([A.Resize(height=256, width=256, p=1.0),ToTensorV2(p=1.0)], p=1.0)
40
- tensor = cust_transform(image=img)
41
- tensor = tensor['image'].float().resize(1,3,256,256)
42
- tensor,tensor.shape
43
- custom_pred = model.forward(tensor).detach().numpy() # Forward is the python method defined inside the resnet.
44
- custom_pred
45
- st.write(f'Predicted: {id2class[np.argmax(custom_pred)]}')
46
-
47
- elif ".csv" in uploaded_file.name: #csv check
48
- dataframe = pd.read_csv(uploaded_file)
49
- st.write(dataframe)
50
-
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Search models, datasets, users...
4
+ Models
5
+ Datasets
6
+ Spaces
7
+ Docs
8
+ Solutions
9
+ Pricing
10
+
11
+
12
+
13
+ Spaces:
14
+
15
+ jvahala
16
+ /
17
+ dummy Copied
18
+ like
19
+ 0
20
+ App
21
+ Files and versions
22
+ Community
23
+ dummy
24
+ /
25
+ app.py
26
+ jvahala's picture
27
+ jvahala
28
+ fix imread issue
29
+ 011b03a
30
+ about 1 hour ago
31
+ raw
32
+ history
33
+ blame
34
+ contribute
35
+ delete
36
+ Safe
37
+ 1.89 kB
38
+ from distutils.command.upload import upload
39
  import streamlit as st
40
  from io import StringIO
41
  from PIL import Image
42
  import pandas as pd
43
+
44
  import torch
45
  import torch.nn as nn
46
+ import torchvision.models as models
47
+
48
+ import albumentations as A
49
  from albumentations.pytorch.transforms import ToTensorV2
50
 
51
+ import numpy as np
52
+
53
+ import cv2
54
+
55
+ st.title('Dummy')
56
+ uploaded_file = st.file_uploader('Select File')
57
+
58
  id2class = {0: 'agricultural', 1: 'airplane', 2: 'baseballdiamond', 3: 'beach', 4: 'buildings', 5: 'chaparral', 6: 'denseresidential', 7: 'forest', 8: 'freeway', 9: 'golfcourse', 10: 'intersection', 11: 'mediumresidential', 12: 'mobilehomepark', 13: 'overpass', 14: 'parkinglot', 15: 'river', 16: 'runway', 17: 'sparseresidential', 18: 'storagetanks', 19: 'tenniscourt', 20: 'harbor'}
59
 
60
  model = models.resnet50(weights=None)
 
62
  model.load_state_dict(torch.load('resnet_best.pth', map_location=torch.device('cpu')), strict=True)
63
  model.eval()
64
 
65
+ if uploaded_file is not None:
66
+ if '.jpg' in uploaded_file.name.lower() or '.png' in uploaded_file.name.lower():
67
+ st.write(uploaded_file.name)
68
+ img = Image.open(uploaded_file)
69
+ st.image(img)
70
+ img = np.array(img)
71
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # OpenCV images have a different color profile. So remember to switch to RGB, that our resnet model understands.
72
 
73
+ cust_transform = A.Compose([A.Resize(height=256, width=256, p=1.0),ToTensorV2(p=1.0)], p=1.0)
74
+ tensor = cust_transform(image=img)
75
+ tensor = tensor['image'].float().resize(1,3,256,256)
76
 
77
+ custom_pred = model.forward(tensor).detach().numpy() # Forward is the python method defined inside the resnet.
78
+ custom_pred
 
 
 
 
 
 
 
 
79
 
80
+ st.write(f'Predicted: {id2class[np.argmax(custom_pred)]}')
81
+ elif '.csv' in uploaded_file.name:
82
+ dataframe = pd.read_csv(uploaded_file)
83
+ st.write(dataframe)