keras 类型错误:tf__test_function()缺少1个必需的位置参数:“steps_per_execution”?我应该在哪里寻找答案?

krcsximq  于 2023-02-08  发布在  其他
关注(0)|答案(1)|浏览(94)

我不知道这里到底发生了什么,我想打印(答案),但过早停止。https://colab.research.google.com/drive/1hvHkDusyqEsdZg5ZRVhhriZrDagpFdU6?usp=sharing

import os 
import warnings 
from zipfile import ZipFile 
import random
from shutil import copyfile
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.image import imread
import pathlib
import tensorflow as tf 
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, 
Dense, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint 
import pandas as pd
import numpy as np
!pip install keras --upgrade
from keras_preprocessing.image import load_img, img_to_array
file_name = "/content/dermnet.zip"
with ZipFile(file_name,'r') as zip: 
 zip.extractall()
 print('done')

#Make new directory 
original_dataset_dir = '/content/dataset'
base_dir = '/content/skin-data/'
os.mkdir(base_dir)
#create two folders (train and validation)
train_dir = os.path.join(base_dir, 'train')
os.mkdir(train_dir)

validation_dir = os.path.join(base_dir, 'validation')
os.mkdir(validation_dir)
 
#Under train folder create folders 

train_Acne_and_Rosacea_dir = os.path.join(train_dir, 'Acne and Rosacea Photos')
os.mkdir(train_Acne_and_Rosacea_dir)

train_Vascular_Tumors_dir = os.path.join(train_dir, 'Vascular Tumors')
os.mkdir(train_Vascular_Tumors_dir)

train_Exanthems_and_Drug_Eruption_dir = os.path.join(train_dir, 'Exanthems and Drug Eruptions')
os.mkdir(train_Exanthems_and_Drug_Eruption_dir)

train_Systemic_Disease_dir = os.path.join(train_dir, 'Systemic Disease')
os.mkdir(train_Systemic_Disease_dir)

train_Light_Diseases_and_Disorders_of_Pigmentation_dir = os.path.join(train_dir, 'Light Diseases and Disorders of Pigmentation')
os.mkdir(train_Light_Diseases_and_Disorders_of_Pigmentation_dir)

train_Melanoma_Skin_Cancer_Nevi_and_Moles_dir = os.path.join(train_dir, 'Melanoma Skin Cancer Nevi and Moles')
os.mkdir(train_Melanoma_Skin_Cancer_Nevi_and_Moles_dir)

train_Urticaria_Hives_dir = os.path.join(train_dir, 'Urticaria Hives')
os.mkdir(train_Urticaria_Hives_dir)

train_Hair_Loss_Photos_Alopecia_and_other_Hair_Diseases_dir = os.path.join(train_dir, 'Hair Loss Photos Alopecia and other Hair Diseases')
os.mkdir(train_Hair_Loss_Photos_Alopecia_and_other_Hair_Diseases_dir)

train_Scabies_Lyme_Disease_and_other_Infestations_and_Bites_dir = os.path.join(train_dir, 'Scabies Lyme Disease and other Infestations and Bites')
os.mkdir(train_Scabies_Lyme_Disease_and_other_Infestations_and_Bites_dir)

train_Psoriasis_pictures_Lichen_Planus_and_related_diseases_dir = os.path.join(train_dir, 'Psoriasis pictures Lichen Planus and related diseases')
os.mkdir(train_Psoriasis_pictures_Lichen_Planus_and_related_diseases_dir)

train_Seborrheic_Keratoses_and_other_Benign_Tumors_dir = os.path.join(train_dir, 'Seborrheic Keratoses and other Benign Tumors')
os.mkdir(train_Seborrheic_Keratoses_and_other_Benign_Tumors_dir)


#Under validation folder createfolders 

validation_Acne_and_Rosacea_dir = os.path.join(validation_dir, 'Acne and Rosacea Photos')
os.mkdir(validation_Acne_and_Rosacea_dir)

validation_Vascular_Tumors_dir = os.path.join(validation_dir, 'Vascular Tumors')
os.mkdir(validation_Vascular_Tumors_dir)

validation_Exanthems_and_Drug_Eruption_dir = os.path.join(validation_dir, 'Exanthems and Drug Eruptions')
os.mkdir(validation_Exanthems_and_Drug_Eruption_dir)

validation_Systemic_Disease_dir = os.path.join(validation_dir, 'Systemic Disease')
os.mkdir(validation_Systemic_Disease_dir)

validation_Light_Diseases_and_Disorders_of_Pigmentation_dir = os.path.join(validation_dir, 'Light Diseases and Disorders of Pigmentation')
os.mkdir(validation_Light_Diseases_and_Disorders_of_Pigmentation_dir)

validation_Melanoma_Skin_Cancer_Nevi_and_Moles_dir = os.path.join(validation_dir, 'Melanoma Skin Cancer Nevi and Moles')
os.mkdir(validation_Melanoma_Skin_Cancer_Nevi_and_Moles_dir)

validation_Urticaria_Hives_dir = os.path.join(validation_dir, 'Urticaria Hives')
os.mkdir(validation_Urticaria_Hives_dir)

validation_Hair_Loss_Photos_Alopecia_and_other_Hair_Diseases_dir = os.path.join(validation_dir, 'Hair Loss Photos Alopecia and other Hair Diseases')
os.mkdir(validation_Hair_Loss_Photos_Alopecia_and_other_Hair_Diseases_dir)

validation_Scabies_Lyme_Disease_and_other_Infestations_and_Bites_dir = os.path.join(validation_dir, 'Scabies Lyme Disease and other Infestations and Bites')
os.mkdir(validation_Scabies_Lyme_Disease_and_other_Infestations_and_Bites_dir)

validation_Psoriasis_pictures_Lichen_Planus_and_related_diseases_dir = os.path.join(validation_dir, 'Psoriasis pictures Lichen Planus and related diseases')
os.mkdir(validation_Psoriasis_pictures_Lichen_Planus_and_related_diseases_dir)

validation_Seborrheic_Keratoses_and_other_Benign_Tumors_dir = os.path.join(validation_dir, 'Seborrheic Keratoses and other Benign Tumors')
os.mkdir(validation_Seborrheic_Keratoses_and_other_Benign_Tumors_dir)
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.image import imread
import pathlib
image_folder = ['Seborrheic Keratoses and other Benign Tumors',
                'Psoriasis pictures Lichen Planus and related diseases',
                'Scabies Lyme Disease and other Infestations and Bites',
                'Hair Loss Photos Alopecia and other Hair Diseases', 'Urticaria Hives',
                'Melanoma Skin Cancer Nevi and Moles',
                'Light Diseases and Disorders of Pigmentation', 'Systemic Disease',
                'Exanthems and Drug Eruptions', 'Vascular Tumors', 'Acne and Rosacea Photos']
nimgs = {}
for i in image_folder:
    nimages = len(os.listdir('/content/skin-data/train/'+i+'/'))
    nimgs[i]=nimages
plt.figure(figsize=(70, 24))
plt.bar(range(len(nimgs)), list(nimgs.values()), align='center')
plt.xticks(range(len(nimgs)), list(nimgs.keys()))
plt.title('Distribution of different classes in Training Dataset')
plt.show()
#we can just barely see the words under the chart 
#update, you can just click to zoom in lol
img_width=256; img_height=256
batch_size=16
TRAINING_DIR = '/content/skin-data/train/'

train_datagen = ImageDataGenerator(rescale = 1/255.0,
                                   rotation_range=30,
                                   zoom_range=0.4,
                                   horizontal_flip=True)

train_generator = train_datagen.flow_from_directory(TRAINING_DIR,
                                                    batch_size=batch_size,
                                                    class_mode='categorical',
                                                    target_size=(img_height, img_width))
x, y = train_generator.next()
first_image = x[0]
first_label = y[0]
first_image, first_label = train_generator[0]

train_generator[0]
VALIDATION_DIR = '/content/skin-data/validation/'
model = Sequential([
    Conv2D(16, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)), MaxPooling2D(2, 2),
    Conv2D(32, (3, 3), activation='relu'), MaxPooling2D(2, 2),
    Conv2D(64, (3, 3), activation='relu'),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Conv2D(128, (3, 3), activation='relu'),
    Conv2D(128, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Conv2D(256, (3, 3), activation='relu'),
    Conv2D(256, (3, 3), activation='relu'),
    Conv2D(256, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(512, activation='relu'),
    Dense(11, activation='softmax')
])
model.summary()
model.compile(optimizer='Adam', 
              loss='categorical_crossentropy', 
              metrics =['accuracy'])
history = model.fit_generator(train_generator,
                              epochs=30,
                              verbose=1,
                              validation_data=validation_generator,
                              callbacks = [best_model]
                              )
test_images_dir = '/content/Picture_Test/' #Get a file and name it as shown
test_df = pd.read_csv('/content/skin-picture_test.csv') #Create your own csv file

test_dfToList = test_df['Image_id'].tolist()
test_ids = [str(item) for item in test_dfToList]

test_images = [test_images_dir+item for item in test_ids]
test_preprocessed_images = np.vstack([preprocess_image(fn) for fn in test_images])
np.save('/content/test_preproc_CNN.npy', test_preprocessed_images)
array = model.predict(test_preprocessed_images, batch_size=1, verbose=1)
answer = np.argmax(array, axis=1)
print(answer)

我得到:
类型错误跟踪(最近调用最后一次)in----〉1数组=模型.预测(测试预处理图像,批处理大小= 1,详细信息= 1)2答案= np. argmax(数组,轴= 1)3打印(答案)
在converted_call(f,参数,密码,调用者fn范围,选项)437中,尝试:如果kwargs不是None,则返回438:- -〉439结果=转换的_f(* 有效_参数,*kwargs)440否则:441结果=已转换_f( 有效_参数)

TypeError: in user code:
            
            
                TypeError: tf__test_function() missing 1 required positional argument: 'steps_per_execution'

我想知道是否有什么地方我没有看。我很nooby,但我没有预料到这个错误在最轻微的。
我认为我的模型可能有问题,因为它"缺少1个必需的位置参数:'steps_per_execution'"。试图让它运行,但看起来今天不是我的日子。

6yt4nkrj

6yt4nkrj1#

感谢@Kenean Nooks的确认,并在回答部分提到您的评论,以造福社区。
通过删除不必要的Tensorflow导入。

相关问题