44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
|
|
import os
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
import json
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
import tensorflow as tf
|
|
from tensorflow import keras
|
|
from tensorflow.keras import layers
|
|
from tensorflow.keras.utils import load_img, img_to_array
|
|
from tensorflow.keras.utils import image_dataset_from_directory
|
|
|
|
|
|
# model_name = "beerd_25_04_2023.keras"
|
|
model_name = "beerd_imagenet_25_04_2023.keras"
|
|
img = load_img("photo_2023-04-25_10-02-25.jpg", color_mode="rgb")
|
|
img = tf.image.resize(img, (180, 180, ), "bilinear")
|
|
img_array = img_to_array(img)
|
|
|
|
test_model = keras.models.load_model(model_name)
|
|
test_loss = test_model.predict(np.expand_dims(img_array, 0))
|
|
|
|
|
|
list_labels = [fname for fname in os.listdir("assets/dog")]
|
|
list_labels.sort()
|
|
dict_names = {}
|
|
for i, label in enumerate(list_labels):
|
|
dict_names[i] = label
|
|
|
|
with open("beerds.json", "w") as f:
|
|
f.write(json.dumps(dict_names))
|
|
|
|
max_val = 0
|
|
max_num = 0
|
|
for i, val in enumerate(test_loss[0]):
|
|
if val < max_val:
|
|
continue
|
|
max_val = val
|
|
max_num = i
|
|
|
|
print("-----------------------")
|
|
print(list_labels)
|
|
print(test_loss)
|
|
print(max_num, max_val, dict_names[max_num]) |