Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,146 @@ import requests
|
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, VisionEncoderDecoderModel
|
| 5 |
import torch
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, VisionEncoderDecoderModel
|
| 5 |
import torch
|
| 6 |
+
import torch
|
| 7 |
+
from torch.autograd import Variable as V
|
| 8 |
+
import torchvision.models as models
|
| 9 |
+
from torchvision import transforms as trn
|
| 10 |
+
from torch.nn import functional as F
|
| 11 |
+
import os
|
| 12 |
+
import numpy as np
|
| 13 |
+
import cv2
|
| 14 |
+
from PIL import Image
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def recursion_change_bn(module):
|
| 18 |
+
if isinstance(module, torch.nn.BatchNorm2d):
|
| 19 |
+
module.track_running_stats = 1
|
| 20 |
+
else:
|
| 21 |
+
for i, (name, module1) in enumerate(module._modules.items()):
|
| 22 |
+
module1 = recursion_change_bn(module1)
|
| 23 |
+
return module
|
| 24 |
+
|
| 25 |
+
def load_labels():
|
| 26 |
+
# prepare all the labels
|
| 27 |
+
# scene category relevant
|
| 28 |
+
file_name_category = 'categories_places365.txt'
|
| 29 |
+
classes = list()
|
| 30 |
+
with open(file_name_category) as class_file:
|
| 31 |
+
for line in class_file:
|
| 32 |
+
classes.append(line.strip().split(' ')[0][3:])
|
| 33 |
+
classes = tuple(classes)
|
| 34 |
+
|
| 35 |
+
# indoor and outdoor relevant
|
| 36 |
+
file_name_IO = 'IO_places365.txt'
|
| 37 |
+
with open(file_name_IO) as f:
|
| 38 |
+
lines = f.readlines()
|
| 39 |
+
labels_IO = []
|
| 40 |
+
for line in lines:
|
| 41 |
+
items = line.rstrip().split()
|
| 42 |
+
labels_IO.append(int(items[-1]) -1) # 0 is indoor, 1 is outdoor
|
| 43 |
+
labels_IO = np.array(labels_IO)
|
| 44 |
+
|
| 45 |
+
# scene attribute relevant
|
| 46 |
+
file_name_attribute = 'labels_sunattribute.txt'
|
| 47 |
+
with open(file_name_attribute) as f:
|
| 48 |
+
lines = f.readlines()
|
| 49 |
+
labels_attribute = [item.rstrip() for item in lines]
|
| 50 |
+
file_name_W = 'W_sceneattribute_wideresnet18.npy'
|
| 51 |
+
W_attribute = np.load(file_name_W)
|
| 52 |
+
|
| 53 |
+
return classes, labels_IO, labels_attribute, W_attribute
|
| 54 |
+
|
| 55 |
+
def hook_feature(module, input, output):
|
| 56 |
+
return np.squeeze(output.data.cpu().numpy())
|
| 57 |
+
|
| 58 |
+
def returnCAM(feature_conv, weight_softmax, class_idx):
|
| 59 |
+
# generate the class activation maps upsample to 256x256
|
| 60 |
+
size_upsample = (256, 256)
|
| 61 |
+
nc, h, w = feature_conv.shape
|
| 62 |
+
output_cam = []
|
| 63 |
+
for idx in class_idx:
|
| 64 |
+
cam = weight_softmax[class_idx].dot(feature_conv.reshape((nc, h*w)))
|
| 65 |
+
cam = cam.reshape(h, w)
|
| 66 |
+
cam = cam - np.min(cam)
|
| 67 |
+
cam_img = cam / np.max(cam)
|
| 68 |
+
cam_img = np.uint8(255 * cam_img)
|
| 69 |
+
output_cam.append(cv2.resize(cam_img, size_upsample))
|
| 70 |
+
return output_cam
|
| 71 |
+
|
| 72 |
+
def returnTF():
|
| 73 |
+
# load the image transformer
|
| 74 |
+
tf = trn.Compose([
|
| 75 |
+
trn.Resize((224,224)),
|
| 76 |
+
trn.ToTensor(),
|
| 77 |
+
trn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 78 |
+
])
|
| 79 |
+
return tf
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def load_model():
|
| 83 |
+
# this model has a last conv feature map as 14x14
|
| 84 |
+
|
| 85 |
+
model_file = 'wideresnet18_places365.pth.tar'
|
| 86 |
+
import wideresnet
|
| 87 |
+
model = wideresnet.resnet18(num_classes=365)
|
| 88 |
+
checkpoint = torch.load(model_file, map_location=lambda storage, loc: storage)
|
| 89 |
+
state_dict = {str.replace(k,'module.',''): v for k,v in checkpoint['state_dict'].items()}
|
| 90 |
+
model.load_state_dict(state_dict)
|
| 91 |
+
|
| 92 |
+
# hacky way to deal with the upgraded batchnorm2D and avgpool layers...
|
| 93 |
+
for i, (name, module) in enumerate(model._modules.items()):
|
| 94 |
+
module = recursion_change_bn(model)
|
| 95 |
+
model.avgpool = torch.nn.AvgPool2d(kernel_size=14, stride=1, padding=0)
|
| 96 |
+
|
| 97 |
+
model.eval()
|
| 98 |
+
|
| 99 |
+
# hook the feature extractor
|
| 100 |
+
features_names = ['layer4','avgpool'] # this is the last conv layer of the resnet
|
| 101 |
+
for name in features_names:
|
| 102 |
+
model._modules.get(name).register_forward_hook(hook_feature)
|
| 103 |
+
return model
|
| 104 |
+
|
| 105 |
+
# load the labels
|
| 106 |
+
classes, labels_IO, labels_attribute, W_attribute = load_labels()
|
| 107 |
+
|
| 108 |
+
# load the model
|
| 109 |
+
features_blobs = []
|
| 110 |
+
model = load_model()
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# load the transformer
|
| 114 |
+
tf = returnTF() # image transformer
|
| 115 |
+
|
| 116 |
+
# get the softmax weight
|
| 117 |
+
params = list(model.parameters())
|
| 118 |
+
weight_softmax = params[-2].data.numpy()
|
| 119 |
+
weight_softmax[weight_softmax<0] = 0
|
| 120 |
+
|
| 121 |
+
def predict(img):
|
| 122 |
+
#img = Image.open('6.jpg')
|
| 123 |
+
input_img = V(tf(img).unsqueeze(0))
|
| 124 |
+
logit = model.forward(input_img)
|
| 125 |
+
h_x = F.softmax(logit, 1).data.squeeze()
|
| 126 |
+
probs, idx = h_x.sort(0, True)
|
| 127 |
+
probs = probs.numpy()
|
| 128 |
+
idx = idx.numpy()
|
| 129 |
+
io_image = np.mean(labels_IO[idx[:10]]) # vote for the indoor or outdoor
|
| 130 |
+
env_image = []
|
| 131 |
+
if io_image < 0.5:
|
| 132 |
+
env_image.append('Indoor')
|
| 133 |
+
#print('--TYPE OF ENVIRONMENT: indoor')
|
| 134 |
+
else:
|
| 135 |
+
env_image.append('Outdoor')
|
| 136 |
+
#print('--TYPE OF ENVIRONMENT: outdoor')
|
| 137 |
+
|
| 138 |
+
# output the prediction of scene category
|
| 139 |
+
#print('--SCENE CATEGORIES:')
|
| 140 |
+
scene_cat=[]
|
| 141 |
+
for i in range(0, 5):
|
| 142 |
+
scene_cat.append('{:.3f} -> {}'.format(probs[i], classes[idx[i]]))
|
| 143 |
+
#print('{:.3f} -> {}'.format(probs[i], classes[idx[i]]))
|
| 144 |
+
|
| 145 |
+
return env_image,scene_cat
|
| 146 |
|
| 147 |
|
| 148 |
|