narugo1992 commited on
Commit
eab0a53
1 Parent(s): 24f03b5

dev(narugo): add more usable model

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -124,11 +124,12 @@ def load_classes() -> List[str]:
124
  return json.load(f)
125
 
126
 
127
- def get_tags_from_image(pic: Image.Image, threshold: float = 0.7, size: int = 512, keep_ratio: bool = False):
 
128
  real_input = image_to_tensor(pic, size, keep_ratio)
129
  real_input = real_input.reshape(1, *real_input.shape)
130
 
131
- model = _open_onnx_model(get_onnx_model_file(), get_onnx_provider('cpu'))
132
  native_output, = model.run(['output'], {'input': real_input})
133
 
134
  output = (1 / (1 + np.exp(-native_output))).reshape(-1)
@@ -140,10 +141,10 @@ def get_tags_from_image(pic: Image.Image, threshold: float = 0.7, size: int = 51
140
  RE_SPECIAL = re.compile(r'([\\()])')
141
 
142
 
143
- def image_to_mldanbooru_tags(pic: Image.Image, threshold: float, size: int, keep_ratio: bool,
144
  use_spaces: bool, use_escape: bool, include_ranks: bool, score_descend: bool) \
145
  -> Tuple[str, Mapping[str, float]]:
146
- filtered_tags = get_tags_from_image(pic, threshold, size, keep_ratio)
147
 
148
  text_items = []
149
  tags_pairs = filtered_tags.items()
@@ -163,6 +164,12 @@ def image_to_mldanbooru_tags(pic: Image.Image, threshold: float, size: int, keep
163
  return output_text, filtered_tags
164
 
165
 
 
 
 
 
 
 
166
  if __name__ == '__main__':
167
  with gr.Blocks() as demo:
168
  with gr.Row():
@@ -172,6 +179,8 @@ if __name__ == '__main__':
172
  gr_threshold = gr.Slider(0.0, 1.0, 0.7, label='Tagging Confidence Threshold')
173
  gr_image_size = gr.Slider(128, 960, 512, step=32, label='Image for Recognition')
174
  gr_keep_ratio = gr.Checkbox(value=False, label='Keep the Ratio')
 
 
175
  with gr.Row():
176
  gr_space = gr.Checkbox(value=False, label='Use Space Instead Of _')
177
  gr_escape = gr.Checkbox(value=True, label='Use Text Escape')
@@ -190,7 +199,8 @@ if __name__ == '__main__':
190
  gr_btn_submit.click(
191
  image_to_mldanbooru_tags,
192
  inputs=[
193
- gr_input_image, gr_threshold, gr_image_size, gr_keep_ratio,
 
194
  gr_space, gr_escape, gr_confidence, gr_order
195
  ],
196
  outputs=[gr_output_text, gr_tags],
 
124
  return json.load(f)
125
 
126
 
127
+ def get_tags_from_image(pic: Image.Image, threshold: float = 0.7, size: int = 512, keep_ratio: bool = False,
128
+ model_name='TResnet-D-FLq_ema_6-10000.onnx'):
129
  real_input = image_to_tensor(pic, size, keep_ratio)
130
  real_input = real_input.reshape(1, *real_input.shape)
131
 
132
+ model = _open_onnx_model(get_onnx_model_file(model_name), get_onnx_provider('cpu'))
133
  native_output, = model.run(['output'], {'input': real_input})
134
 
135
  output = (1 / (1 + np.exp(-native_output))).reshape(-1)
 
141
  RE_SPECIAL = re.compile(r'([\\()])')
142
 
143
 
144
+ def image_to_mldanbooru_tags(pic: Image.Image, threshold: float, size: int, keep_ratio: bool, model: str,
145
  use_spaces: bool, use_escape: bool, include_ranks: bool, score_descend: bool) \
146
  -> Tuple[str, Mapping[str, float]]:
147
+ filtered_tags = get_tags_from_image(pic, threshold, size, keep_ratio, model)
148
 
149
  text_items = []
150
  tags_pairs = filtered_tags.items()
 
164
  return output_text, filtered_tags
165
 
166
 
167
+ MODELS = [
168
+ 'TResnet-D-FLq_ema_6-10000.onnx',
169
+ 'TResnet-D-FLq_ema_4-10000.onnx',
170
+ 'TResnet-D-FLq_ema_2-40000.onnx',
171
+ ]
172
+
173
  if __name__ == '__main__':
174
  with gr.Blocks() as demo:
175
  with gr.Row():
 
179
  gr_threshold = gr.Slider(0.0, 1.0, 0.7, label='Tagging Confidence Threshold')
180
  gr_image_size = gr.Slider(128, 960, 512, step=32, label='Image for Recognition')
181
  gr_keep_ratio = gr.Checkbox(value=False, label='Keep the Ratio')
182
+ with gr.Row():
183
+ gr_model = gr.Dropdown(MODELS, value=MODELS[0], label='Model')
184
  with gr.Row():
185
  gr_space = gr.Checkbox(value=False, label='Use Space Instead Of _')
186
  gr_escape = gr.Checkbox(value=True, label='Use Text Escape')
 
199
  gr_btn_submit.click(
200
  image_to_mldanbooru_tags,
201
  inputs=[
202
+ gr_input_image, gr_threshold, gr_image_size,
203
+ gr_keep_ratio, gr_model,
204
  gr_space, gr_escape, gr_confidence, gr_order
205
  ],
206
  outputs=[gr_output_text, gr_tags],