Thiago Hersan commited on
Commit
7f17661
1 Parent(s): 480594f

adds an ipynb for testing/rough-drafting

Browse files
Files changed (1) hide show
  1. app.ipynb +98 -0
app.ipynb ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import gradio as gr\n",
10
+ "import numpy as np\n",
11
+ "from PIL import Image\n",
12
+ "from transformers import MaskFormerFeatureExtractor, MaskFormerForInstanceSegmentation"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": null,
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "# feature_extractor = MaskFormerFeatureExtractor.from_pretrained(\"facebook/maskformer-swin-tiny-coco\")\n",
22
+ "# model = MaskFormerForInstanceSegmentation.from_pretrained(\"facebook/maskformer-swin-tiny-coco\")\n",
23
+ "feature_extractor = MaskFormerFeatureExtractor.from_pretrained(\"facebook/maskformer-swin-large-coco\")\n",
24
+ "model = MaskFormerForInstanceSegmentation.from_pretrained(\"facebook/maskformer-swin-large-coco\")\n",
25
+ "\n",
26
+ "with Image.open(\"../color-filter-calculator/assets/Artshack_screen.jpg\") as img:\n",
27
+ " img_size = (img.height, img.width)\n",
28
+ " inputs = feature_extractor(images=img, return_tensors=\"pt\")\n",
29
+ " "
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "code",
34
+ "execution_count": null,
35
+ "metadata": {},
36
+ "outputs": [],
37
+ "source": [
38
+ "outputs = model(**inputs)"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": null,
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "results = feature_extractor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[img_size])[0]\n",
48
+ "results = results.numpy()\n",
49
+ "\n",
50
+ "labels = np.unique(results)"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": null,
56
+ "metadata": {},
57
+ "outputs": [],
58
+ "source": [
59
+ "for label_id in labels:\n",
60
+ " print(model.config.id2label[label_id])"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": null,
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": []
69
+ }
70
+ ],
71
+ "metadata": {
72
+ "kernelspec": {
73
+ "display_name": "Python 3.8.15 ('hf-gradio')",
74
+ "language": "python",
75
+ "name": "python3"
76
+ },
77
+ "language_info": {
78
+ "codemirror_mode": {
79
+ "name": "ipython",
80
+ "version": 3
81
+ },
82
+ "file_extension": ".py",
83
+ "mimetype": "text/x-python",
84
+ "name": "python",
85
+ "nbconvert_exporter": "python",
86
+ "pygments_lexer": "ipython3",
87
+ "version": "3.8.15"
88
+ },
89
+ "orig_nbformat": 4,
90
+ "vscode": {
91
+ "interpreter": {
92
+ "hash": "4888b226c77b860705e4be316b14a092026f41c3585ee0ddb38f3008c0cb495e"
93
+ }
94
+ }
95
+ },
96
+ "nbformat": 4,
97
+ "nbformat_minor": 2
98
+ }