Spaces:
Runtime error
Runtime error
Update label.py
Browse files
label.py
CHANGED
|
@@ -98,34 +98,30 @@ def load_model():
|
|
| 98 |
model._modules.get(name).register_forward_hook(hook_feature)
|
| 99 |
return model
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
def predict_environment(img):
|
| 103 |
-
# load the labels
|
| 104 |
-
classes, labels_IO, labels_attribute, W_attribute = load_labels()
|
| 105 |
-
|
| 106 |
-
# load the model
|
| 107 |
-
features_blobs = []
|
| 108 |
-
model = load_model()
|
| 109 |
-
|
| 110 |
-
# load the transformer
|
| 111 |
-
tf = returnTF() # image transformer
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
weight_softmax = params[-2].data.numpy()
|
| 116 |
-
weight_softmax[weight_softmax<0] = 0
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
input_img = V(tf(img).unsqueeze(0))
|
| 120 |
-
|
| 121 |
-
# forward pass
|
| 122 |
logit = model.forward(input_img)
|
| 123 |
h_x = F.softmax(logit, 1).data.squeeze()
|
| 124 |
probs, idx = h_x.sort(0, True)
|
| 125 |
probs = probs.numpy()
|
| 126 |
idx = idx.numpy()
|
| 127 |
-
|
| 128 |
-
# output the IO prediction
|
| 129 |
io_image = np.mean(labels_IO[idx[:10]]) # vote for the indoor or outdoor
|
| 130 |
env_image = []
|
| 131 |
if io_image < 0.5:
|
|
|
|
| 98 |
model._modules.get(name).register_forward_hook(hook_feature)
|
| 99 |
return model
|
| 100 |
|
| 101 |
+
# load the labels
|
| 102 |
+
classes, labels_IO, labels_attribute, W_attribute = load_labels()
|
| 103 |
+
|
| 104 |
+
# load the model
|
| 105 |
+
features_blobs = []
|
| 106 |
+
model = load_model()
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
# load the transformer
|
| 110 |
+
tf = returnTF() # image transformer
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# get the softmax weight
|
| 113 |
+
params = list(model.parameters())
|
| 114 |
+
weight_softmax = params[-2].data.numpy()
|
| 115 |
+
weight_softmax[weight_softmax<0] = 0
|
| 116 |
+
|
| 117 |
+
def predict(img):
|
| 118 |
+
img = Image.open('6.jpg')
|
| 119 |
input_img = V(tf(img).unsqueeze(0))
|
|
|
|
|
|
|
| 120 |
logit = model.forward(input_img)
|
| 121 |
h_x = F.softmax(logit, 1).data.squeeze()
|
| 122 |
probs, idx = h_x.sort(0, True)
|
| 123 |
probs = probs.numpy()
|
| 124 |
idx = idx.numpy()
|
|
|
|
|
|
|
| 125 |
io_image = np.mean(labels_IO[idx[:10]]) # vote for the indoor or outdoor
|
| 126 |
env_image = []
|
| 127 |
if io_image < 0.5:
|