类型错误:__init__() 需要 2 到 3 个位置参数,但给出了 4 个

问题描述 投票:0回答:1

我想使用scaled_input = Rescaling(1./255, 0.0, "rescaling")(input_)来重新缩放数据集,包括带有5个标签的600张图像,但出现错误:

    scaled_input = Rescaling(1./255, 0.0, "rescaling")(input_)
TypeError: __init__() takes from 2 to 3 positional arguments but 4 were given

这是全部代码:

import os
import random
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
from tensorflow.keras.layers.experimental.preprocessing import Rescaling
from ut2 import train_test_split

src = 'Dataset/corrosion/'

# Check if the dataset has been downloaded. If not, direct user to download the dataset first
if not os.path.isdir(src):
    print("""
          Dataset not found in your computer.
          Please follow the instructions in the link below to download the dataset:
          https://raw.githubusercontent.com/PacktPublishing/Neural-Network-Projects-with-Python/master/chapter4/how_to_download_the_dataset.txt
          """)
    quit()


# create the train/test folders if it does not exists already
if not os.path.isdir(src+'train/'):
    train_test_split(src)

from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.layers import Dense, Flatten

# Define hyperparameters
# Define hyperparameters
FILTER_SIZE = 3
NUM_FILTERS = 32
INPUT_SIZE  = 256
BATCH_SIZE = 16
EPOCHS = 40

vgg16 = VGG16(include_top=False, weights='imagenet', input_shape=(INPUT_SIZE,INPUT_SIZE,3))

# Freeze the pre-trained layers
for layer in vgg16.layers:
    layer.trainable = False

# Add a fully connected layer with 1 node at the end
input_ = vgg16.input
scaled_input = Rescaling(1./255, 0.0, "rescaling")(input_)
output_ = vgg16(scaled_input)
last_layer = Flatten(name='flatten')(output_)
last_layer = Dense(5, activation='softmax')(last_layer)
model = Model(input_, last_layer)
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])


training_set = tf.keras.utils.image_dataset_from_directory (src+'Train/',
                                                labels='inferred',
                                                image_size = (INPUT_SIZE, INPUT_SIZE),
                                                batch_size = BATCH_SIZE,
                                                label_mode='int')

test_set = tf.keras.utils.image_dataset_from_directory (src+'Test/',
                                                               labels='inferred',
                                                               image_size=(INPUT_SIZE, INPUT_SIZE),
                                                               batch_size=BATCH_SIZE,
                                                               label_mode='int')

print("""
      Caution: VGG16 model training can take up to an hour if you are not running Keras on a GPU.
      If the code takes too long to run on your computer, you may reduce the INPUT_SIZE paramater in the code to speed up model training.
      """)

model.fit(training_set, epochs = EPOCHS, verbose=1)

score = model.evaluate(test_set)

for idx, metric in enumerate(model.metrics_names):
    print("{}: {}".format(metric, score[idx]))


我想使用scaled_input = Rescaling(1./255, 0.0, "rescaling")(input_)来重新缩放数据集,包括带有5个标签的600张图像,但出现错误:

    scaled_input = Rescaling(1./255, 0.0, "rescaling")(input_)
TypeError: __init__() takes from 2 to 3 positional arguments but 4 were given
python tensorflow keras
1个回答
0
投票

重新缩放函数仅接受两个位置参数:缩放和偏移。但您传递了四个参数:1./255、0.0、“重新缩放”和 (input_)。 只是尝试一下而不通过“重新缩放”。

scaled_input = Rescaling(1./255, 0.0)(input_)

© www.soinside.com 2019 - 2024. All rights reserved.