Gray_shu 2022-04-25 03:28 采纳率: 100%
浏览 3201
已结题

TypeError: add_argument() takes 2 positional arguments but 3 were given 这个报错怎么解决

利用unet tensorflow深度学习
代码如下

  1. import os
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from skimage import io
  5. from keras.layers.core import Dropout
  6. from keras.layers.convolutional import Conv2D, Conv2DTranspose
  7. from keras.layers.pooling import MaxPooling2D
  8. from keras.layers import Input
  9. from keras.models import Model
  10. from keras.layers.merge import concatenate
  11. from keras.callbacks import EarlyStopping, ModelCheckpoint
  12. def standardize(img):
  13. mean = np.mean(img, axis=0)
  14. std = np.std(img, axis=0)
  15. return (img - mean) / std
  16. # Get training and validation paths
  17. def get_train_val_paths(img_folder, mask_folder, split_ratio=0.8):
  18. img_names = os.listdir(img_folder)
  19. print("total images: ", len(img_names))
  20. mask_names = os.listdir(mask_folder)
  21. img_paths = [os.path.join(img_folder, name) for name in img_names]
  22. mask_paths = [os.path.join(mask_folder, name) for name in mask_names]
  23. no_samples = len(img_paths)
  24. no_train = int(np.ceil(split_ratio * no_samples))
  25. train_paths = {name: paths_list for name, paths_list in zip(["train_imgs", "train_mask"],
  26. [img_paths[:no_train], mask_paths[:no_train]])}
  27. val_paths = {name: paths_list for name, paths_list in zip(["val_imgs", "val_mask"],
  28. [img_paths[no_train:], mask_paths[no_train:]])}
  29. return train_paths, val_paths
  30. # Data generator to get single image and mask
  31. def image_generator(img_paths, mask_paths):
  32. for img_path, mask_path in zip(img_paths, mask_paths):
  33. img = io.imread(img_path)
  34. img = standardize(img)
  35. img = np.expand_dims(img, axis=2)
  36. mask = io.imread(mask_path) / 255.
  37. mask = np.expand_dims(mask, axis=2)
  38. yield img, mask
  39. # Batch data generator
  40. def img_batch_generator(img_paths, mask_paths, batch_size):
  41. while True:
  42. img_gen = image_generator(img_paths, mask_paths)
  43. img_batch, mask_batch = [], []
  44. for img, mask in img_gen:
  45. img_batch.append(img)
  46. mask_batch.append(mask)
  47. if len(img_batch) == batch_size:
  48. yield np.stack(img_batch, axis=0), np.stack(mask_batch, axis=0)
  49. img_batch, mask_batch = [], []
  50. if len(img_batch) != 0:
  51. yield np.stack(img_batch, axis=0), np.stack(mask_batch, axis=0)
  52. image_folder = "C:/Users/11962/Desktop/images1/"
  53. masks_folder = "C:/Users/11962/Desktop/masks1/"
  54. tr_paths, v_paths = get_train_val_paths(image_folder, masks_folder)
  55. inputs = Input((512, 512, 1))
  56. c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(inputs)
  57. c1 = Dropout(0.1)(c1)
  58. c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c1)
  59. p1 = MaxPooling2D((2, 2))(c1)
  60. c2 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(p1)
  61. c2 = Dropout(0.1)(c2)
  62. c2 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c2)
  63. p2 = MaxPooling2D((2, 2))(c2)
  64. c3 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(p2)
  65. c3 = Dropout(0.2)(c3)
  66. c3 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c3)
  67. p3 = MaxPooling2D((2, 2))(c3)
  68. c4 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(p3)
  69. c4 = Dropout(0.2)(c4)
  70. c4 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c4)
  71. p4 = MaxPooling2D(pool_size=(2, 2))(c4)
  72. c5 = Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(p4)
  73. c5 = Dropout(0.3)(c5)
  74. c5 = Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c5)
  75. u6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5)
  76. u6 = concatenate([u6, c4])
  77. c6 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(u6)
  78. c6 = Dropout(0.2)(c6)
  79. c6 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c6)
  80. u7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6)
  81. u7 = concatenate([u7, c3])
  82. c7 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(u7)
  83. c7 = Dropout(0.2)(c7)
  84. c7 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c7)
  85. u8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(c7)
  86. u8 = concatenate([u8, c2])
  87. c8 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(u8)
  88. c8 = Dropout(0.1)(c8)
  89. c8 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c8)
  90. u9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)
  91. u9 = concatenate([u9, c1], axis=3)
  92. c9 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(u9)
  93. c9 = Dropout(0.1)(c9)
  94. c9 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c9)
  95. outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)
  96. model = Model(inputs=[inputs], outputs=[outputs])
  97. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
  98. print(model.summary())
  99. batch_size = 2
  100. train_gen = img_batch_generator(tr_paths["train_imgs"], tr_paths["train_mask"], batch_size)
  101. val_gen = img_batch_generator(v_paths["val_imgs"], v_paths["val_mask"], batch_size)
  102. train_steps = len(tr_paths["train_imgs"]) // batch_size
  103. val_steps = len(v_paths["val_imgs"]) // batch_size
  104. early_stop = EarlyStopping(patience=10, verbose=1)
  105. checkpoint = ModelCheckpoint("./model/keras_unet_model.h5", verbose=1, save_best_only=True)
  106. history = model.fit(train_gen, steps_per_epoch=train_steps,
  107. epochs=30,
  108. validation_data=val_gen,
  109. validation_steps=val_steps,
  110. verbose=1,
  111. max_queue_size=4,
  112. callbacks=[early_stop, checkpoint])
  113. loss = history.history["loss"]
  114. val_loss = history.history["val_loss"]
  115. acc = history.history['accuracy']
  116. val_acc = history.history['val_accuracy']
  117. epochs = range(1, len(loss) + 1)
  118. plt.plot(epochs, loss, color="red", label="training loss")
  119. plt.plot(epochs, val_loss, color="blue", label="validation loss")
  120. plt.title("Training and Validation Loss")
  121. plt.legend()
  122. plt.figure()
  123. plt.plot(epochs, acc, color="red", label="training acc")
  124. plt.plot(epochs, val_acc, color="blue", label="validation acc")
  125. plt.title("Training and Validation acc")
  126. plt.legend()

报错:TypeError: add_argument() takes 2 positional arguments but 3 were given

应该怎么修改,急

展开全部

  • 写回答

3条回答 默认 最新

  • 不会长胖的斜杠 后端领域新星创作者 2022-04-25 03:33
    关注

    报错的具体信息贴出来看看,add_argument()这个函数看看形参,错误是因为你多传了一个参数进去了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    Gray_shu 2022-04-25 03:37

    runfile('C:/Users/11962/.spyder-py3/temp.py', wdir='C:/Users/11962/.spyder-py3')
    Traceback (most recent call last):

    File "C:\Users\11962.spyder-py3\temp.py", line 5, in
    from keras.layers.core import Dropout

    File "C:\Users\11962\anaconda3\lib\site-packages\keras_init_.py", line 24, in
    from keras import models

    File "C:\Users\11962\anaconda3\lib\site-packages\keras\models_init_.py", line 18, in
    from keras.engine.functional import Functional

    File "C:\Users\11962\anaconda3\lib\site-packages\keras\engine\functional.py", line 24, in
    from keras.dtensor import layout_map as layout_map_lib

    File "C:\Users\11962\anaconda3\lib\site-packages\keras\dtensor\layout_map.py", line 25, in
    from keras.engine import base_layer

    File "C:\Users\11962\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 44, in
    from keras.mixed_precision import autocast_variable

    File "C:\Users\11962\anaconda3\lib\site-packages\keras\mixed_precision_init_.py", line 22, in
    from keras.mixed_precision.loss_scale_optimizer import LossScaleOptimizer

    File "C:\Users\11962\anaconda3\lib\site-packages\keras\mixed_precision\loss_scale_optimizer.py", line 1341, in
    tf.internal.mixed_precision.register_loss_scale_wrapper(

    TypeError: register_loss_scale_wrapper() takes 2 positional arguments but 3 were given

    回复
    不会长胖的斜杠 回复 Gray_shu 2022-04-25 03:39

    这样的话,应该是你的版本有问题,源代码用的keras版本是多少,你的是多少,检查一下

    回复
    Gray_shu 回复 不会长胖的斜杠 2022-04-25 03:41

    好的,谢谢

    回复
    展开全部4条评论
查看更多回答(2条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 5月2日
  • 已采纳回答 4月25日
  • 创建了问题 4月25日

悬赏问题

  • ¥15 STS/eclipse导入gradle项目时报错如下
  • ¥15 centos7.6进不去系统,卡在数字7界面
  • ¥15 Tensorflow采用interpreter.allocate_tensors()分配内存出现ValueError: vector too long报错
  • ¥15 使用CGenFF在线生成血红素辅基拓扑结构遇到问题
  • ¥15 在fragment使用okhttp同步上传文件,能不能在fragment销毁后还可以继续上传文件?
  • ¥20 matlab代码实现可达矩阵形成骨骼矩阵
  • ¥15 关于地板的木纹和图库中的匹配的
  • ¥30 机器学习预测疾病模型流程疑问
  • ¥50 2048Python实现
  • ¥15 使用ads进行低噪放仿真没有结果且不报错
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部