Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: __init__() takes from 2 to 3 positional arguments but 4 were given

I want to use scaled_input = Rescaling(1./255, 0.0, "rescaling")(input_) to rescale the dataset including 600 images with 5 labels but it got error:

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

Here is all the code:

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]))


I want to use scaled_input = Rescaling(1./255, 0.0, "rescaling")(input_) to rescale the dataset including 600 images with 5 labels but it got error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Rescaling function only takes two positional arguments: scale and offset. but you’re passing four arguments: 1./255, 0.0, "rescaling", and (input_).
just try it without passing "rescaling".

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading