PDG commited on
Commit
2e4ea99
·
1 Parent(s): a1360f6

Create new file

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from torchvision import models, transforms
4
+
5
+ # -- get torch and cuda version
6
+ TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
7
+ CUDA_VERSION = torch.__version__.split("+")[-1]
8
+ '''
9
+ # -- install pre-build detectron2
10
+ !pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/{CUDA_VERSION}/{TORCH_VERSION}/index.html
11
+
12
+ import detectron2
13
+ from detectron2.utils.logger import setup_logger # ????
14
+
15
+ from detectron2 import model_zoo
16
+ from detectron2.engine import DefaultPredictor
17
+ from detectron2.config import get_cfg
18
+
19
+ # ????
20
+ setup_logger()
21
+
22
+ # -- load rcnn model
23
+ cfg = get_cfg()
24
+ # add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
25
+ cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
26
+ cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
27
+ # Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
28
+ cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
29
+ predictor = DefaultPredictor(cfg)
30
+
31
+ !wget http://images.cocodataset.org/val2017/000000439715.jpg -q -O input.jpg
32
+ im = cv2.imread("./input.jpg")
33
+ cv2_imshow(im)
34
+
35
+ outputs = predictor(im)
36
+
37
+ print(outputs["instances"].pred_classes)
38
+ print(outputs["instances"].pred_boxes)
39
+ '''
40
+ # -- load Mask R-CNN model for segmentation
41
+ DesignModernityModel = torch.load("DesignModernityModel.pt")
42
+
43
+ #INPUT_FEATURES = DesignModernityModel.fc.in_features
44
+ #linear = nn.linear(INPUT_FEATURES, 5)
45
+
46
+ DesignModernityModel.eval() # set state of the model to inference
47
+
48
+ LABELS = ['2000-2004', '2006-2008', '2009-2011', '2012-2015', '2016-2018']
49
+
50
+ carTransforms = transforms.Compose([transforms.Resize(224)])
51
+
52
+ def classifyCar(im):
53
+ im = Image.fromarray(im.astype('uint8'), 'RGB')
54
+ im = carTransforms(im).unsqueeze(0) # transform and add batch dimension
55
+ with torch.no_grad():
56
+ scores = torch.nn.functional.softmax(model(im)[0])
57
+ return {LABELS[i]: float(scores[i]) for i in range(2)}
58
+
59
+ examples = [[example_img.jpg], [example_img2.jpg]] # must be uploaded in repo
60
+
61
+ # create interface for model
62
+ interface = gr.Interface(classifyCar, inputs='Image', outputs='label', cache_examples=False, title='VW Up or Fiat 500', example=examples)
63
+ interface.launch()
64
+
65
+