我試圖通過使用其保存權重來測驗預訓練的 CNN 模型 (densenet121) 的準確性,但陷入了這個錯誤。測驗代碼如下:
image_size=512
batch_size=4
model_name='weights/densenet121_CAB_messidor.h5' #pretrained weight of the model
test_dir='./data/Messidor_Classes/' #dataset directory
model=load_model(model_name) #loading the model
test_num=0
result=np.zeros((4,4),dtype=int)
recall=np.zeros((1,4),dtype=float)
for i in range(4): #one loop for each class
datadirs=test_dir str(i) '/' #image directory
filenames=os.listdir(datadirs)
num=len(filenames)
test_num=test_num num
valid = ImageDataGenerator() #data augmentation
valid_data=valid.flow_from_directory(directory=test_dir,target_size=(image_size,image_size),
batch_size=batch_size,class_mode=None,classes=str(i))
predict=model.predict(valid_data,batch_size=batch_size,verbose=1,workers=1) #the line where the error is encountered
predict=np.argmax(predict,axis=-1)
for j in range(4):
result[i,j]=np.sum(predict==j)
right=result[0,0] result[1,1] result[2,2] result[3,3]
print('Acc:',right/test_num) #accuracy
w_kappa=weight_kappa(result,test_num)
print('w_kappa:',w_kappa) #kappa score
資料集的樹是這樣的,影像的格式是.tif:
Messidor--|0->img_1.tif #images for class 0
| ...
| ...
| ->img_2.tif
|1->img_1.tif #images for class 1
| ...
| ...
| ->img_2.tif
|2->img_1.tif #images for class 2
| ...
| ...
| ->img_2.tif
|3->img_1.tif #images for class 3
| ...
| ...
| ->img_2.tif
我在運行此測驗代碼時遇到的錯誤是:
Found 428 images belonging to 1 classes.
Traceback (most recent call last):
File "C:\Users\aokra\source\repos\CABnet\test.py", line 53, in <module>
predict=model.predict(valid_data,batch_size=batch_size,verbose=1,workers=1)
File "C:\Users\aokra\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\aokra\anaconda3\lib\site-packages\keras_preprocessing\image\iterator.py", line 227, in _get_batches_of_transformed_samples
img = load_img(filepaths[j],
File "C:\Users\aokra\anaconda3\lib\site-packages\keras_preprocessing\image\utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "C:\Users\aokra\anaconda3\lib\site-packages\PIL\Image.py", line 2967, in open
raise UnidentifiedImageError(
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001C299030EF0>
uj5u.com熱心網友回復:
此錯誤是由于檔案損壞或檔案擴展名不正確導致生成器無法讀取。
使用下面的函式檢查輸入的資料:
代碼:
import os
from PIL import Image
folder_path = r'data\Messidor_Classes'
extensions = []
for fldr in os.listdir(folder_path):
sub_folder_path = os.path.join(folder_path, fldr)
for filee in os.listdir(sub_folder_path):
file_path = os.path.join(sub_folder_path, filee)
print('** Path: {} **'.format(file_path), end="\r", flush=True)
im = Image.open(file_path)
rgb_im = im.convert('RGB')
if filee.split('.')[1] not in extensions:
extensions.append(filee.split('.')[1])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368281.html
上一篇:無法加載.pb檔案輸入模型:SavedModel格式加載失敗:“_UserObject”物件沒有屬性“add_slot”
