TypeError:'int'对象在np.random.seed中不可调用

时间:2019-11-13 17:18:09

标签: python numpy random

我正在尝试在Kaggle之前的2018 Data Science Bowl比赛中进行数据增强。我正在尝试以下代码:

## Data augmentation
# Creating the training Image and Mask generator
image_datagen = image.ImageDataGenerator(shear_range=0.5, rotation_range=50, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, fill_mode='reflect')
mask_datagen = image.ImageDataGenerator(shear_range=0.5, rotation_range=50, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, fill_mode='reflect')

# Keep the same seed for image and mask generators so they fit together
image_datagen.fit(X_train[:int(X_train.shape[0]*0.9)], augment=True, seed=42)
mask_datagen.fit(Y_train[:int(Y_train.shape[0]*0.9)], augment=True, seed=42)

x=image_datagen.flow(X_train[:int(X_train.shape[0]*0.9)],batch_size=BATCH_SIZE,shuffle=True, seed=42)
y=mask_datagen.flow(Y_train[:int(Y_train.shape[0]*0.9)],batch_size=BATCH_SIZE,shuffle=True, seed=seed)



# Creating the validation Image and Mask generator
image_datagen_val = image.ImageDataGenerator()
mask_datagen_val = image.ImageDataGenerator()

image_datagen_val.fit(X_train[int(X_train.shape[0]*0.9):], augment=True, seed=seed)
mask_datagen_val.fit(Y_train[int(Y_train.shape[0]*0.9):], augment=True, seed=seed)

x_val=image_datagen_val.flow(X_train[int(X_train.shape[0]*0.9):],batch_size=BATCH_SIZE,shuffle=True, seed=seed)
y_val=mask_datagen_val.flow(Y_train[int(Y_train.shape[0]*0.9):],batch_size=BATCH_SIZE,shuffle=True, seed=seed)

这是错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-126-6b608552652e> in <module>
      5 
      6 # Keep the same seed for image and mask generators so they fit together
----> 7 image_datagen.fit(X_train[:int(X_train.shape[0]*0.9)], augment=True, seed=42)
      8 mask_datagen.fit(Y_train[:int(Y_train.shape[0]*0.9)], augment=True, seed=42)
      9 

~\Anaconda3\lib\site-packages\keras_preprocessing\image\image_data_generator.py in fit(self, x, augment, rounds, seed)
    941 
    942         if seed is not None:
--> 943             np.random.seed(seed)
    944 
    945         x = np.copy(x)

TypeError: 'int' object is not callable

据我了解,该错误在seed的{​​{1}}参数中。就我而言,该错误消息在image_datagen.fit代码中显示了一些内部问题。我不明白为什么。

我已经探索了其他类似的问题,但是我发现没有一个适合我的问题。

这些是我读过的解决方案:

Getting TypeError: 'int' object is not callable

Python "int object is not callable"

class method TypeError "Int object not callable"

2 个答案:

答案 0 :(得分:1)

您已经像这样初始化了种子值:

np.random.seed = 42

试试这个:

np.random.seed(42)

并再次运行完整代码

答案 1 :(得分:0)

请确保您 np.random.seed 分配给脚本中某个地方的某个整数

赞:

np.random.seed = 42