johnnv commited on
Commit
3b352d4
1 Parent(s): 7615049

add app, requirements and pre-commit

Browse files
Files changed (3) hide show
  1. .pre-commit-config.yaml +37 -0
  2. app.py +73 -0
  3. requirements.txt +4 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ default_language_version:
2
+ python: python3.7
3
+ repos:
4
+ - repo: https://github.com/pre-commit/pre-commit-hooks
5
+ rev: v4.3.0
6
+ hooks:
7
+ - id: check-merge-conflict
8
+ - id: check-case-conflict
9
+ - id: check-yaml
10
+ - id: detect-private-key
11
+ - id: debug-statements
12
+ - id: end-of-file-fixer
13
+ - id: trailing-whitespace
14
+ - id: double-quote-string-fixer
15
+ - id: requirements-txt-fixer
16
+ - repo: https://github.com/asottile/reorder_python_imports
17
+ rev: v3.8.2
18
+ hooks:
19
+ - id: reorder-python-imports
20
+ - repo: https://github.com/asottile/add-trailing-comma
21
+ rev: v2.2.3
22
+ hooks:
23
+ - id: add-trailing-comma
24
+ - repo: https://github.com/asottile/pyupgrade
25
+ rev: v2.37.3
26
+ hooks:
27
+ - id: pyupgrade
28
+ args: [--py37-plus]
29
+ - repo: https://github.com/pre-commit/mirrors-autopep8
30
+ rev: v1.7.0
31
+ hooks:
32
+ - id: autopep8
33
+ - repo: https://github.com/pycqa/flake8
34
+ rev: 5.0.4
35
+ hooks:
36
+ - id: flake8
37
+ additional_dependencies: [flake8-typing-imports==1.12.0]
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from CCAgT_utils.types.mask import Mask
5
+ from PIL import Image
6
+ from torch import nn
7
+ from transformers import SegformerFeatureExtractor
8
+ from transformers import SegformerForSemanticSegmentation
9
+
10
+
11
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
+
13
+ model_hub_name = 'lapix/segformer-b3-finetuned-ccagt-400-300'
14
+
15
+ model = SegformerForSemanticSegmentation.from_pretrained(
16
+ model_hub_name,
17
+ ).to(device)
18
+ feature_extractor = SegformerFeatureExtractor.from_pretrained(
19
+ model_hub_name,
20
+ )
21
+
22
+
23
+ def query_image(image):
24
+ image = np.array(image)
25
+ img = Image.fromarray(image)
26
+
27
+ pixel_values = feature_extractor(
28
+ image,
29
+ return_tensors='pt',
30
+ ).to(device)
31
+
32
+ with torch.no_grad():
33
+ outputs = model(pixel_values=pixel_values)
34
+
35
+ logits = outputs.logits
36
+
37
+ upsampled_logits = nn.functional.interpolate(
38
+ logits,
39
+ size=img.size[::-1], # (height, width)
40
+ mode='bilinear',
41
+ align_corners=False,
42
+ )
43
+
44
+ segmentation_mask = upsampled_logits.argmax(dim=1)[0]
45
+
46
+ results = Mask(segmentation_mask).colorized() / 255
47
+
48
+ return results
49
+
50
+
51
+ title = 'SegFormer (b3) - CCAgT dataset'
52
+ description = f"""
53
+ This is demo for the SegFormer fine-tuned on sub-dataset from
54
+ [CCAgT dataset](https://huggingface.co/datasets/lapix/CCAgT). This model
55
+ was trained to segment cervical cells silver-stained (AgNOR technique)
56
+ images with resolution of 400x300. The model was available at HF hub at
57
+ [{model_hub_name}](https://huggingface.co/{model_hub_name}).
58
+ """
59
+ examples = [
60
+ [f'https://hf.co/{model_hub_name}/resolve/main/sampleA.png'],
61
+ [f'https://hf.co/{model_hub_name}/resolve/main/sampleB.png'],
62
+ ]
63
+
64
+ demo = gr.Interface(
65
+ query_image,
66
+ inputs=[gr.Image()],
67
+ outputs='image',
68
+ title=title,
69
+ description=description,
70
+ examples=examples,
71
+ )
72
+
73
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ CCAgT-utils
2
+ numpy
3
+ torch
4
+ transformers