yellowdolphin commited on
Commit
dd7149e
·
verified ·
1 Parent(s): 4fe5a08

rollback to py3.7

Browse files
Files changed (1) hide show
  1. utils.py +28 -33
utils.py CHANGED
@@ -1,13 +1,8 @@
1
- from subprocess import run
2
- # For TF > 2.15, use a tfimm branch that uses tf_keras instead of tf.keras
3
- run('pip install git+https://github.com/pajotarthur/tensorflow-image-models.git@update_python_3.11'.split())
4
-
5
  import math
6
  import json
7
 
8
  import numpy as np
9
  import tensorflow as tf
10
- import tf_keras
11
  import tfimm
12
  import efficientnet.tfkeras as efnv1
13
  import keras_efficientnet_v2 as efnv2
@@ -131,7 +126,7 @@ def get_confidence(similarity, threshold):
131
  return sigmoid(abs(logit_sim - logit_threshold))
132
 
133
 
134
- class ArcMarginProductSubCenter(tf_keras.layers.Layer):
135
  '''
136
  Implements large margin arc distance.
137
 
@@ -256,43 +251,43 @@ def get_model(cfg):
256
  name=f'head/{cfg.head}',
257
  dtype='float32')
258
 
259
- inp = tf_keras.layers.Input(shape=[*cfg.IMAGE_SIZE, 3], name='inp1')
260
- label = tf_keras.layers.Input(shape=(), name='inp2')
261
  if aux_arcface:
262
- label2 = tf_keras.layers.Input(shape=(), name='inp3')
263
 
264
  if cfg.arch_name.startswith('efnv1'):
265
  x = EFN[cfg.arch_name](weights=cfg.pretrained, include_top=False)(inp)
266
  if cfg.pool == 'flatten':
267
- embed = tf_keras.layers.Flatten()(x)
268
  elif cfg.pool == 'fc':
269
- embed = tf_keras.layers.Flatten()(x)
270
- embed = tf_keras.layers.Dropout(0.1)(embed)
271
- embed = tf_keras.layers.Dense(1024)(embed)
272
  elif cfg.pool == 'concat':
273
- embed = tf_keras.layers.concatenate([tf_keras.layers.GlobalAveragePooling2D()(x),
274
- tf_keras.layers.GlobalAveragePooling2D()(x)])
275
  elif cfg.pool == 'max':
276
- embed = tf_keras.layers.GlobalMaxPooling2D()(x)
277
  else:
278
- embed = tf_keras.layers.GlobalAveragePooling2D()(x)
279
 
280
  elif cfg.arch_name.startswith('efnv2'):
281
  x = EFN[cfg.arch_name](input_shape=(None, None, 3), num_classes=0,
282
  pretrained=cfg.pretrained)(inp)
283
  if cfg.pool == 'flatten':
284
- embed = tf_keras.layers.Flatten()(x)
285
  elif cfg.pool == 'fc':
286
- embed = tf_keras.layers.Flatten()(x)
287
- embed = tf_keras.layers.Dropout(0.1)(embed)
288
- embed = tf_keras.layers.Dense(1024)(embed)
289
  elif cfg.pool == 'concat':
290
- embed = tf_keras.layers.concatenate([tf_keras.layers.GlobalAveragePooling2D()(x),
291
- tf_keras.layers.GlobalAveragePooling2D()(x)])
292
  elif cfg.pool == 'max':
293
- embed = tf_keras.layers.GlobalMaxPooling2D()(x)
294
  else:
295
- embed = tf_keras.layers.GlobalAveragePooling2D()(x)
296
 
297
  elif cfg.arch_name in TFHUB:
298
  # tfhub models cannot be modified => Pooling cannot be changed!
@@ -306,21 +301,21 @@ def get_model(cfg):
306
 
307
  if len(cfg.dropout_ps) > 0:
308
  # Chris Deotte posted model code without Dropout/FC1 after pooling
309
- embed = tf_keras.layers.Dropout(cfg.dropout_ps[0])(embed)
310
- embed = tf_keras.layers.Dense(1024)(embed) # tunable embedding size
311
- embed = tf_keras.layers.BatchNormalization()(embed) # missing in public notebooks
312
  x = margin([embed, label])
313
 
314
- output = tf_keras.layers.Softmax(dtype='float32', name='arc' if cfg.aux_loss else None)(x)
315
 
316
  if cfg.aux_loss:
317
- aux_features = tf_keras.layers.Dense(cfg.n_species)(embed)
318
- aux_output = tf_keras.layers.Softmax(dtype='float32', name='aux')(aux_features)
319
  inputs = [inp, label, label2] if (cfg.aux_loss and aux_arcface) else [inp, label]
320
  outputs = (output, aux_output) if cfg.aux_loss else [output]
321
 
322
- model = tf_keras.models.Model(inputs=inputs, outputs=outputs)
323
- embed_model = tf_keras.models.Model(inputs=inp, outputs=embed)
324
 
325
  if cfg.FREEZE_BATCH_NORM:
326
  raise NotImplementedError
 
 
 
 
 
1
  import math
2
  import json
3
 
4
  import numpy as np
5
  import tensorflow as tf
 
6
  import tfimm
7
  import efficientnet.tfkeras as efnv1
8
  import keras_efficientnet_v2 as efnv2
 
126
  return sigmoid(abs(logit_sim - logit_threshold))
127
 
128
 
129
+ class ArcMarginProductSubCenter(tf.keras.layers.Layer):
130
  '''
131
  Implements large margin arc distance.
132
 
 
251
  name=f'head/{cfg.head}',
252
  dtype='float32')
253
 
254
+ inp = tf.keras.layers.Input(shape=[*cfg.IMAGE_SIZE, 3], name='inp1')
255
+ label = tf.keras.layers.Input(shape=(), name='inp2')
256
  if aux_arcface:
257
+ label2 = tf.keras.layers.Input(shape=(), name='inp3')
258
 
259
  if cfg.arch_name.startswith('efnv1'):
260
  x = EFN[cfg.arch_name](weights=cfg.pretrained, include_top=False)(inp)
261
  if cfg.pool == 'flatten':
262
+ embed = tf.keras.layers.Flatten()(x)
263
  elif cfg.pool == 'fc':
264
+ embed = tf.keras.layers.Flatten()(x)
265
+ embed = tf.keras.layers.Dropout(0.1)(embed)
266
+ embed = tf.keras.layers.Dense(1024)(embed)
267
  elif cfg.pool == 'concat':
268
+ embed = tf.keras.layers.concatenate([tf.keras.layers.GlobalAveragePooling2D()(x),
269
+ tf.keras.layers.GlobalAveragePooling2D()(x)])
270
  elif cfg.pool == 'max':
271
+ embed = tf.keras.layers.GlobalMaxPooling2D()(x)
272
  else:
273
+ embed = tf.keras.layers.GlobalAveragePooling2D()(x)
274
 
275
  elif cfg.arch_name.startswith('efnv2'):
276
  x = EFN[cfg.arch_name](input_shape=(None, None, 3), num_classes=0,
277
  pretrained=cfg.pretrained)(inp)
278
  if cfg.pool == 'flatten':
279
+ embed = tf.keras.layers.Flatten()(x)
280
  elif cfg.pool == 'fc':
281
+ embed = tf.keras.layers.Flatten()(x)
282
+ embed = tf.keras.layers.Dropout(0.1)(embed)
283
+ embed = tf.keras.layers.Dense(1024)(embed)
284
  elif cfg.pool == 'concat':
285
+ embed = tf.keras.layers.concatenate([tf.keras.layers.GlobalAveragePooling2D()(x),
286
+ tf.keras.layers.GlobalAveragePooling2D()(x)])
287
  elif cfg.pool == 'max':
288
+ embed = tf.keras.layers.GlobalMaxPooling2D()(x)
289
  else:
290
+ embed = tf.keras.layers.GlobalAveragePooling2D()(x)
291
 
292
  elif cfg.arch_name in TFHUB:
293
  # tfhub models cannot be modified => Pooling cannot be changed!
 
301
 
302
  if len(cfg.dropout_ps) > 0:
303
  # Chris Deotte posted model code without Dropout/FC1 after pooling
304
+ embed = tf.keras.layers.Dropout(cfg.dropout_ps[0])(embed)
305
+ embed = tf.keras.layers.Dense(1024)(embed) # tunable embedding size
306
+ embed = tf.keras.layers.BatchNormalization()(embed) # missing in public notebooks
307
  x = margin([embed, label])
308
 
309
+ output = tf.keras.layers.Softmax(dtype='float32', name='arc' if cfg.aux_loss else None)(x)
310
 
311
  if cfg.aux_loss:
312
+ aux_features = tf.keras.layers.Dense(cfg.n_species)(embed)
313
+ aux_output = tf.keras.layers.Softmax(dtype='float32', name='aux')(aux_features)
314
  inputs = [inp, label, label2] if (cfg.aux_loss and aux_arcface) else [inp, label]
315
  outputs = (output, aux_output) if cfg.aux_loss else [output]
316
 
317
+ model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
318
+ embed_model = tf.keras.models.Model(inputs=inp, outputs=embed)
319
 
320
  if cfg.FREEZE_BATCH_NORM:
321
  raise NotImplementedError