import os
import numpy as np
import pandas as pd
import cv2
from glob import glob
from tqdm import tqdm
import tensorflow as tf
from sklearn.model_selection import train_test_split
def read_image(path, size):
image = cv2.imread(path, cv2.IMREAD_COLOR)
image = cv2.resize(image, (size, size))
image = image / 255.0
image = image.astype(np.float32)
return image
if __name__ == "__main__":
path = "Dog Breed Identification/"
train_path = os.path.join(path, "train/*")
test_path = os.path.join(path, "test/*")
labels_path = os.path.join(path, "labels.csv")
labels_df = pd.read_csv(labels_path)
breed = labels_df["breed"].unique()
print("Number of Breed: ", len(breed))
breed2id = {name: i for i, name in enumerate(breed)}
id2breed = {i: name for i, name in enumerate(breed)}
ids = glob(train_path)
labels = []
for image_id in ids:
image_id = image_id.split("\\")[-1].split(".")[0]
breed_name = list(labels_df[labels_df.id == image_id]["breed"])[0]
breed_idx = breed2id[breed_name]
labels.append(breed_idx)
ids = ids[:1000]
labels = labels[:1000]
## Spliting the dataset
train_x, valid_x = train_test_split(ids, test_size=0.2, random_state=42)
train_y, valid_y = train_test_split(labels, test_size=0.2, random_state=42)
## Model
model = tf.keras.models.load_model('model.h5')
for i, path in tqdm(enumerate(valid_x[:10])):
image = read_image(path, 224)
image = np.expand_dims(image, axis=0)
pred = model.predict(image)[0]
label_idx = np.argmax(pred)
breed_name = id2breed[label_idx]
ori_breed = id2breed[valid_y[i]]
ori_image = cv2.imread(path, cv2.IMREAD_COLOR)
ori_image = cv2.putText(ori_image, breed_name, (0, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
ori_image = cv2.putText(ori_image, ori_breed, (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
cv2.imwrite(f"save/valid_{i}.png", ori_image)`enter code here`
2020-09-26 10:05:36.196524: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
Number of Breed: 120
Traceback (most recent call last):
File "D:\WoRkZ\CSE465\Project\New folder\Dog Breed Classification\test.py", line 48, in <module>
model = tf.keras.models.load_model('model.h5')
File "C:\Users\Catalan JM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tensorflow\python\keras\saving\save.py", line 184, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "C:\Users\Catalan JM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 177, in load_model_from_hdf5
model = model_config_lib.model_from_config(model_config,
File "C:\Users\Catalan JM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tensorflow\python\keras\saving\model_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\Catalan JM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tensorflow\python\keras\layers\serialization.py", line 105, in deserialize
return deserialize_keras_object(
File "C:\Users\Catalan JM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 361, in deserialize_keras_object
(cls, cls_config) = class_and_config_for_serialized_keras_object(
File "C:\Users\Catalan JM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 321, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown layer: Functional
[Finished in 12.301s]
您知道导致错误的原因是什么吗?我必须重写整个内容吗?
答案 0 :(得分:0)
没关系,我需要做的就是pip安装Tensorflow 2.3.1并修复它并完美测试我的图像。
尽管它是CPU渲染而不是GpU渲染。因为有 2020-09-26 11:12:19.333012:W tensorflow / stream_executor / platform / default / dso_loader.cc:59]无法加载动态库'cublas64_10.dll'; dlerror:找不到cublas64_10.dll
没有影响我的模型的测试。我必须离题。