#python #tensorflow #keras #neural-network #conv-neural-network
#python #тензорный поток #keras #нейронная сеть #conv-нейронная сеть
Вопрос:
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:WoRkZCSE465ProjectNew folderDog Breed Classificationtest.py", line 48, in <module>
model = tf.keras.models.load_model('model.h5')
File "C:UsersCatalan JMAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagestensorflowpythonkerassavingsave.py", line 184, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "C:UsersCatalan JMAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagestensorflowpythonkerassavinghdf5_format.py", line 177, in load_model_from_hdf5
model = model_config_lib.model_from_config(model_config,
File "C:UsersCatalan JMAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagestensorflowpythonkerassavingmodel_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:UsersCatalan JMAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagestensorflowpythonkeraslayersserialization.py", line 105, in deserialize
return deserialize_keras_object(
File "C:UsersCatalan JMAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagestensorflowpythonkerasutilsgeneric_utils.py", line 361, in deserialize_keras_object
(cls, cls_config) = class_and_config_for_serialized_keras_object(
File "C:UsersCatalan JMAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagestensorflowpythonkerasutilsgeneric_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]
Есть идеи, что вызывает ошибку? Должен ли я переписать все это?
Ответ №1:
Неважно, все, что мне нужно сделать, это установить Tensorflow 2.3.1, исправить его и протестировать мои изображения идеально.
Несмотря на то, что это рендеринг процессора, а не рендеринг графического процессора. Из-за наличия 2020-09-26 11:12:19.333012: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Не удалось загрузить динамическую библиотеку ‘cublas64_10.dll ‘; ошибка dlerror: cublas64_10.dll не найдено
Не повлияло на тестирование моей модели. Я должен отвлечься.