hyo37009 commited on
Commit
e5c89ec
1 Parent(s): a4bc01b
.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
4
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/mySeg2.iml" filepath="$PROJECT_DIR$/.idea/mySeg2.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.idea/mySeg2.iml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="inheritedJdk" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
1212.md DELETED
@@ -1,75 +0,0 @@
1
- ---
2
- license: other
3
- tags:
4
- - vision
5
- - image-segmentation
6
- datasets:
7
- - cityscapes
8
- widget:
9
- - src: https://cdn-media.huggingface.co/Inference-API/Sample-results-on-the-Cityscapes-dataset-The-above-images-show-how-our-method-can-handle.png
10
- example_title: road
11
- ---
12
-
13
- # SegFormer (b5-sized) model fine-tuned on CityScapes
14
-
15
- SegFormer model fine-tuned on CityScapes at resolution 640x1280. It was introduced in the paper [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://arxiv.org/abs/2105.15203) by Xie et al. and first released in [this repository](https://github.com/NVlabs/SegFormer).
16
-
17
- Disclaimer: The team releasing SegFormer did not write a model card for this model so this model card has been written by the Hugging Face team.
18
-
19
- ## Model description
20
-
21
- SegFormer consists of a hierarchical Transformer encoder and a lightweight all-MLP decode head to achieve great results on semantic segmentation benchmarks such as ADE20K and Cityscapes. The hierarchical Transformer is first pre-trained on ImageNet-1k, after which a decode head is added and fine-tuned altogether on a downstream dataset.
22
-
23
- ## Intended uses & limitations
24
-
25
- You can use the raw model for semantic segmentation. See the [model hub](https://huggingface.co/models?other=segformer) to look for fine-tuned versions on a task that interests you.
26
-
27
- ### How to use
28
-
29
- Here is how to use this model to classify an image of the COCO 2017 dataset into one of the 1,000 ImageNet classes:
30
-
31
- ```python
32
- from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
33
- from PIL import Image
34
- import requests
35
-
36
- feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
37
- model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
38
-
39
- url = "http://images.cocodataset.org/val2017/000000039769.jpg"
40
- image = Image.open(requests.get(url, stream=True).raw)
41
-
42
- inputs = feature_extractor(images=image, return_tensors="pt")
43
- outputs = model(**inputs)
44
- logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
45
- ```
46
-
47
- For more code examples, we refer to the [documentation](https://huggingface.co/transformers/model_doc/segformer.html#).
48
-
49
- ### License
50
-
51
- The license for this model can be found [here](https://github.com/NVlabs/SegFormer/blob/master/LICENSE).
52
-
53
- ### BibTeX entry and citation info
54
-
55
- ```bibtex
56
- @article{DBLP:journals/corr/abs-2105-15203,
57
- author = {Enze Xie and
58
- Wenhai Wang and
59
- Zhiding Yu and
60
- Anima Anandkumar and
61
- Jose M. Alvarez and
62
- Ping Luo},
63
- title = {SegFormer: Simple and Efficient Design for Semantic Segmentation with
64
- Transformers},
65
- journal = {CoRR},
66
- volume = {abs/2105.15203},
67
- year = {2021},
68
- url = {https://arxiv.org/abs/2105.15203},
69
- eprinttype = {arXiv},
70
- eprint = {2105.15203},
71
- timestamp = {Wed, 02 Jun 2021 11:46:42 +0200},
72
- biburl = {https://dblp.org/rec/journals/corr/abs-2105-15203.bib},
73
- bibsource = {dblp computer science bibliography, https://dblp.org}
74
- }
75
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -1,11 +1,12 @@
1
-
2
  ---
3
- title: MySeg2
4
- emoji: 📈
5
  colorFrom: blue
6
- colorTo: yellow
7
  sdk: gradio
8
- sdk_version: 4.2.0
9
  app_file: app.py
10
  pinned: false
11
- ---
 
 
 
 
1
  ---
2
+ title: MyImageSegmentation
3
+ emoji: 🏢
4
  colorFrom: blue
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 3.44.4
8
  app_file: app.py
9
  pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,15 +1,119 @@
1
- from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
 
 
 
 
2
  from PIL import Image
3
- import requests
4
- import os
5
- os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-1024-1024")
8
- model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-1024-1024")
 
 
 
 
 
 
9
 
10
- url = "http://images.cocodataset.org/val2017/000000039769.jpg"
11
- image = Image.open(requests.get(url, stream=True).raw)
12
 
13
- inputs = feature_extractor(images=image, return_tensors="pt")
14
- outputs = model(**inputs)
15
- logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
 
1
+ import gradio as gr
2
+
3
+ from matplotlib import gridspec
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
  from PIL import Image
7
+ import tensorflow as tf
8
+ from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
9
+
10
+ feature_extractor = SegformerFeatureExtractor.from_pretrained(
11
+ "mattmdjaga/segformer_b2_clothes"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "mattmdjaga/segformer_b2_clothes"
15
+ )
16
+
17
+
18
+ def ade_palette():
19
+ """ADE20K palette that maps each class to RGB values."""
20
+ return [
21
+ [255, 0, 0],
22
+ [255, 94, 0],
23
+ [255, 187, 0],
24
+ [255, 228, 0],
25
+ [171, 242, 0],
26
+ [29, 219, 22],
27
+ [0, 216, 255],
28
+ [0, 84, 255],
29
+ [1, 0, 255],
30
+ [95, 0, 255],
31
+ [255, 0, 221],
32
+ [255, 0, 127],
33
+ [152, 0, 0],
34
+ [153, 112, 0],
35
+ [107, 153, 0],
36
+ [0, 51, 153],
37
+ [63, 0, 153],
38
+ [153, 0, 133]
39
+ ]
40
+
41
+
42
+ labels_list = []
43
+
44
+ with open(r"labels.txt", "r") as fp:
45
+ for line in fp:
46
+ labels_list.append(line[:-1])
47
+
48
+ colormap = np.asarray(ade_palette())
49
+
50
+
51
+ def label_to_color_image(label):
52
+ if label.ndim != 2:
53
+ raise ValueError("Expect 2-D input label")
54
+
55
+ if np.max(label) >= len(colormap):
56
+ raise ValueError("label value too large.")
57
+ return colormap[label]
58
+
59
+
60
+ def draw_plot(pred_img, seg):
61
+ fig = plt.figure(figsize=(20, 15))
62
+
63
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
64
+
65
+ plt.subplot(grid_spec[0])
66
+ plt.imshow(pred_img)
67
+ plt.axis("off")
68
+ LABEL_NAMES = np.asarray(labels_list)
69
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
70
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
71
+
72
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
73
+ ax = plt.subplot(grid_spec[1])
74
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
75
+ ax.yaxis.tick_right()
76
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
77
+ plt.xticks([], [])
78
+ ax.tick_params(width=0.0, labelsize=25)
79
+ return fig
80
+
81
+
82
+ def sepia(input_img):
83
+ input_img = Image.fromarray(input_img)
84
+
85
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
86
+ outputs = model(**inputs)
87
+ logits = outputs.logits
88
+
89
+ logits = tf.transpose(logits, [0, 2, 3, 1])
90
+ logits = tf.image.resize(
91
+ logits, input_img.size[::-1]
92
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
93
+ seg = tf.math.argmax(logits, axis=-1)[0]
94
+
95
+ color_seg = np.zeros(
96
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
97
+ ) # height, width, 3
98
+ for label, color in enumerate(colormap):
99
+ color_seg[seg.numpy() == label, :] = color
100
+
101
+ # Show image + mask
102
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
103
+ pred_img = pred_img.astype(np.uint8)
104
+
105
+ fig = draw_plot(pred_img, seg)
106
+ return fig
107
+
108
 
109
+ demo = gr.Interface(
110
+ fn=sepia,
111
+ inputs=gr.Image(shape=(400, 600)),
112
+ outputs=["plot"],
113
+ examples=[
114
+ "person-1.jpg","person-2.jpg","person-3.jpg","person-4.jpg", "person-5.jpg",],
115
+ allow_flagging="never",
116
+ )
117
 
 
 
118
 
119
+ demo.launch()
 
 
config.json DELETED
@@ -1,110 +0,0 @@
1
- {
2
- "architectures": [
3
- "SegformerForSemanticSegmentation"
4
- ],
5
- "attention_probs_dropout_prob": 0.0,
6
- "classifier_dropout_prob": 0.1,
7
- "decoder_hidden_size": 256,
8
- "depths": [
9
- 2,
10
- 2,
11
- 2,
12
- 2
13
- ],
14
- "downsampling_rates": [
15
- 1,
16
- 4,
17
- 8,
18
- 16
19
- ],
20
- "drop_path_rate": 0.1,
21
- "hidden_act": "gelu",
22
- "hidden_dropout_prob": 0.0,
23
- "hidden_sizes": [
24
- 32,
25
- 64,
26
- 160,
27
- 256
28
- ],
29
- "id2label": {
30
- "0": "road",
31
- "1": "sidewalk",
32
- "2": "building",
33
- "3": "wall",
34
- "4": "fence",
35
- "5": "pole",
36
- "6": "traffic light",
37
- "7": "traffic sign",
38
- "8": "vegetation",
39
- "9": "terrain",
40
- "10": "sky",
41
- "11": "person",
42
- "12": "rider",
43
- "13": "car",
44
- "14": "truck",
45
- "15": "bus",
46
- "16": "train",
47
- "17": "motorcycle",
48
- "18": "bicycle"
49
- },
50
- "image_size": 224,
51
- "initializer_range": 0.02,
52
- "label2id": {
53
- "bicycle": 18,
54
- "building": 2,
55
- "bus": 15,
56
- "car": 13,
57
- "fence": 4,
58
- "motorcycle": 17,
59
- "person": 11,
60
- "pole": 5,
61
- "rider": 12,
62
- "road": 0,
63
- "sidewalk": 1,
64
- "sky": 10,
65
- "terrain": 9,
66
- "traffic light": 6,
67
- "traffic sign": 7,
68
- "train": 16,
69
- "truck": 14,
70
- "vegetation": 8,
71
- "wall": 3
72
- },
73
- "layer_norm_eps": 1e-06,
74
- "mlp_ratios": [
75
- 4,
76
- 4,
77
- 4,
78
- 4
79
- ],
80
- "model_type": "segformer",
81
- "num_attention_heads": [
82
- 1,
83
- 2,
84
- 5,
85
- 8
86
- ],
87
- "num_channels": 3,
88
- "num_encoder_blocks": 4,
89
- "patch_sizes": [
90
- 7,
91
- 3,
92
- 3,
93
- 3
94
- ],
95
- "reshape_last_stage": true,
96
- "sr_ratios": [
97
- 8,
98
- 4,
99
- 2,
100
- 1
101
- ],
102
- "strides": [
103
- 4,
104
- 2,
105
- 2,
106
- 2
107
- ],
108
- "torch_dtype": "float32",
109
- "transformers_version": "4.12.0.dev0"
110
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
image1.jpg DELETED
Binary file (193 kB)
 
labels.txt CHANGED
@@ -1,19 +1,18 @@
1
- road
2
- sidewalk
3
- building
4
- wall
5
- fence
6
- pole
7
- traffic light
8
- traffic sign
9
- vegetation
10
- terrain
11
- sky
12
- person
13
- rider
14
- car
15
- truck
16
- bus
17
- train
18
- motorcycle
19
- bicycle
 
1
+ background
2
+ hat
3
+ hair
4
+ sunglasses
5
+ upper-clothes
6
+ skirt
7
+ pants
8
+ dress
9
+ belt
10
+ left-shoe
11
+ right-shoe
12
+ face
13
+ left-leg
14
+ right-leg
15
+ left-arm
16
+ right-arm
17
+ bag
18
+ scarf
 
person-1.jpg ADDED
person-2.jpg ADDED
person-3.jpg ADDED
person-4.jpg ADDED
person-5.jpg ADDED
preprocessor_config.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "do_normalize": true,
3
- "do_resize": true,
4
- "feature_extractor_type": "SegformerFeatureExtractor",
5
- "image_mean": [
6
- 0.485,
7
- 0.456,
8
- 0.406
9
- ],
10
- "image_std": [
11
- 0.229,
12
- 0.224,
13
- 0.225
14
- ],
15
- "reduce_labels": false,
16
- "resample": 2,
17
- "size": 512
18
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
pytorch_model.bin DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:ffe3494e1339abf7af09a13c914e72c3d2745e2f315eba1fd2b1dee15b7a73ed
3
- size 14957601
 
 
 
 
tf_model.h5 DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:0b07d5d1354b5e4bc6ad579e7350a240c170b0dea3c781bf917019932174569c
3
- size 15151028