Spaces:
Sleeping
Sleeping
add compare esnet moodel
Browse files- check_arch.py +21 -0
- inference_resnet.py +6 -2
- inference_resnet_v2.py +79 -0
check_arch.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import h5py
|
| 2 |
+
|
| 3 |
+
def print_model_details(file_path):
|
| 4 |
+
with h5py.File(file_path, 'r') as f:
|
| 5 |
+
print(f.keys()) # Print layers
|
| 6 |
+
print(len(f.keys()))
|
| 7 |
+
print("")
|
| 8 |
+
for key in f.keys():
|
| 9 |
+
print(len(list(f[key].keys())))
|
| 10 |
+
print(f"{key}: {list(f[key].keys())}") # Print details of each layer)
|
| 11 |
+
print('rock')
|
| 12 |
+
print_model_details('model_classification/rock-170.h5')
|
| 13 |
+
print('mummified-170')
|
| 14 |
+
print_model_details('model_classification/mummified-170.h5')
|
| 15 |
+
print('BEiT')
|
| 16 |
+
print_model_details('model_classification/fossil-142.h5')
|
| 17 |
+
print('BEiT New')
|
| 18 |
+
print_model_details('model_classification/fossil-new.h5')
|
| 19 |
+
print("Newest:")
|
| 20 |
+
print_model_details('model_classification/fossil-model.h5')
|
| 21 |
+
|
inference_resnet.py
CHANGED
|
@@ -16,7 +16,7 @@ import matplotlib.pyplot as plt
|
|
| 16 |
from typing import Tuple
|
| 17 |
from huggingface_hub import snapshot_download
|
| 18 |
from labels import lookup_170
|
| 19 |
-
import numpy as np
|
| 20 |
|
| 21 |
if not os.path.exists('model_classification'):
|
| 22 |
|
|
@@ -173,7 +173,11 @@ def inference_resnet_finer(x,model,size=576,n_classes=170,n_top=10):
|
|
| 173 |
|
| 174 |
cropped = _clever_crop(x,(size,size))[0]
|
| 175 |
prep = preprocess(cropped,size=size)
|
| 176 |
-
logits =
|
|
|
|
|
|
|
|
|
|
| 177 |
top_n = select_top_n(logits,n=n_top)
|
|
|
|
| 178 |
|
| 179 |
return parse_results(top_n,logits)
|
|
|
|
| 16 |
from typing import Tuple
|
| 17 |
from huggingface_hub import snapshot_download
|
| 18 |
from labels import lookup_170
|
| 19 |
+
import numpy as np
|
| 20 |
|
| 21 |
if not os.path.exists('model_classification'):
|
| 22 |
|
|
|
|
| 173 |
|
| 174 |
cropped = _clever_crop(x,(size,size))[0]
|
| 175 |
prep = preprocess(cropped,size=size)
|
| 176 |
+
logits = model.predict(np.array([prep]))
|
| 177 |
+
print(logits)
|
| 178 |
+
logits = tf.nn.softmax(logits[1][0]).cpu().numpy()
|
| 179 |
+
print(logits)
|
| 180 |
top_n = select_top_n(logits,n=n_top)
|
| 181 |
+
print(top_n)
|
| 182 |
|
| 183 |
return parse_results(top_n,logits)
|
inference_resnet_v2.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
|
| 3 |
+
tf.config.set_visible_devices([], 'GPU')
|
| 4 |
+
# gpu_devices = tf.config.experimental.list_physical_devices('GPU')
|
| 5 |
+
# if gpu_devices:
|
| 6 |
+
# tf.config.experimental.set_memory_growth(gpu_devices[0], True)
|
| 7 |
+
# else:
|
| 8 |
+
# print(f"TensorFlow device: {gpu_devices}")
|
| 9 |
+
|
| 10 |
+
from keras.applications import resnet
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
import keras
|
| 13 |
+
import tensorflow.keras.layers as L
|
| 14 |
+
import os
|
| 15 |
+
|
| 16 |
+
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
|
| 17 |
+
import matplotlib.pyplot as plt
|
| 18 |
+
from typing import Tuple
|
| 19 |
+
from huggingface_hub import snapshot_download
|
| 20 |
+
from labels import lookup_140
|
| 21 |
+
import numpy as np
|
| 22 |
+
|
| 23 |
+
if not os.path.exists('model_classification'):
|
| 24 |
+
|
| 25 |
+
REPO_ID='Serrelab/fossil_classification_models'
|
| 26 |
+
token = os.getenv('READ_TOKEN')
|
| 27 |
+
print('read token:',token)
|
| 28 |
+
if token is None:
|
| 29 |
+
print("warning! A read token in env variables is needed for authentication.")
|
| 30 |
+
snapshot_download(repo_id=REPO_ID,token=token,repo_type='model',local_dir='model_classification')
|
| 31 |
+
|
| 32 |
+
def get_resnet_model(model_path):
|
| 33 |
+
cce = tf.keras.losses.categorical_crossentropy
|
| 34 |
+
model = keras.models.load_model(model_path, custom_objects = {"cce":cce})
|
| 35 |
+
g = keras.Model(model.input, model.layers[2].output)
|
| 36 |
+
# out = tf.keras.layers.Activation('relu')(g_.output)
|
| 37 |
+
# g = tf.keras.Model(model.input, out)
|
| 38 |
+
h = keras.Model(model.layers[3].input, model.layers[-1].output)
|
| 39 |
+
return model, g, h
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def select_top_n(preds,n=10):
|
| 43 |
+
top_n = np.argsort(preds)[-n:][::-1]
|
| 44 |
+
return top_n
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def parse_results(top_n,logits):
|
| 48 |
+
results = {}
|
| 49 |
+
for n in top_n:
|
| 50 |
+
label = lookup_140[n]
|
| 51 |
+
results[label] = float(logits[n])
|
| 52 |
+
return results
|
| 53 |
+
|
| 54 |
+
def inference_resnet_embedding(x,model,size=384,n_classes=140,n_top=10):
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
x = tf.image.resize(x, (size, size))
|
| 58 |
+
x = tf.reshape(x, (-1, 384, 384, 3))/255
|
| 59 |
+
embedding = model.predict(np.array([x]))[0][0]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
return embedding
|
| 63 |
+
|
| 64 |
+
def inference_resnet_finer(x,model,size=384,n_classes=142,n_top=10):
|
| 65 |
+
|
| 66 |
+
x = tf.image.resize(x, (size, size))
|
| 67 |
+
x = tf.reshape(x, (-1, 384, 384, 3))/255
|
| 68 |
+
# _, batch_logits = model.predict(x)
|
| 69 |
+
# predictions = tf.math.top_k(batch_logits, k=10)
|
| 70 |
+
# print(predictions)
|
| 71 |
+
logits = model.predict(np.array([x]))
|
| 72 |
+
print(logits)
|
| 73 |
+
logits = tf.nn.softmax(logits[1][0]).cpu().numpy()
|
| 74 |
+
print(logits)
|
| 75 |
+
top_n = select_top_n(logits,n=n_top)
|
| 76 |
+
print(top_n)
|
| 77 |
+
|
| 78 |
+
return parse_results(top_n,logits)
|
| 79 |
+
|