TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'. in computer vision

huangapple go评论142阅读模式
英文:

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'. in computer vision

问题

I'm working in training my model.

so, I do:

  1. # create the base pre-trained model
  2. base_model = DenseNet121(weights='/Users/awabe/Desktop/Project/PapilaDB/ClinicalData/DenseNet-BC-121-32-no-top.h5', include_top=False)
  3. x = base_model.output
  4. # add a global spatial average pooling layer
  5. x = GlobalAveragePooling2D()(x)
  6. # and a logistic layer
  7. predictions = Dense(len(labels), activation="sigmoid")(x)
  8. model = Model(inputs=base_model.input, outputs=predictions)
  9. model.compile(optimizer='adam', loss=get_weighted_loss(pos_weights, neg_weights))

then I go to plot section and I use:

  1. history = model.fit_generator(train_generator,
  2. validation_data=test_generator,
  3. steps_per_epoch=100,
  4. validation_steps=25,
  5. epochs = 3)
  6. plt.plot(history.history['loss'])
  7. plt.ylabel("loss")
  8. plt.xlabel("epoch")
  9. plt.title("Training Loss Curve")
  10. plt.show()

then it gives me this error message:

  1. TypeError: in user code:
  2. File "/opt/anaconda3/envs/tensorflow/lib/python3.10/site-packages/keras/engine/training.py", line 1160, in train_function *
  3. return step_function(self, iterator)
  4. File "/var/folders/p4/gy9qtf594h3d5q85bzzgflz00000gn/T/ipykernel_1809/4264699890.py", line 27, in weighted_loss *
  5. loss += -(K.mean((pos_weights[i] * y_true[:,i] * K.log(y_pred[:,i] + epsilon) + neg_weights[i]*(1-y_true[:,i]) * K.log(1-y_pred[:,i]+epsilon))))
  6. TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'.
英文:

I'm working in training my model.

so, I do:

  1. # create the base pre-trained model
  2. base_model = DenseNet121(weights='/Users/awabe/Desktop/Project/PapilaDB/ClinicalData/DenseNet-BC-121-32-no-top.h5', include_top=False)
  3. x = base_model.output
  4. # add a global spatial average pooling layer
  5. x = GlobalAveragePooling2D()(x)
  6. # and a logistic layer
  7. predictions = Dense(len(labels), activation="sigmoid")(x)
  8. model = Model(inputs=base_model.input, outputs=predictions)
  9. model.compile(optimizer='adam', loss=get_weighted_loss(pos_weights, neg_weights))

then I go to plot section and I use:

  1. history = model.fit_generator(train_generator,
  2. validation_data=test_generator,
  3. steps_per_epoch=100,
  4. validation_steps=25,
  5. epochs = 3)
  6. plt.plot(history.history['loss'])
  7. plt.ylabel("loss")
  8. plt.xlabel("epoch")
  9. plt.title("Training Loss Curve")
  10. plt.show()

then it gives me this error message:

TypeError: in user code:

  1. File "/opt/anaconda3/envs/tensorflow/lib/python3.10/site-packages/keras/engine/training.py", line 1160, in train_function *
  2. return step_function(self, iterator)
  3. File "/var/folders/p4/gy9qtf594h3d5q85bzzgflz00000gn/T/ipykernel_1809/4264699890.py", line 27, in weighted_loss *
  4. loss += -(K.mean((pos_weights[i] * y_true[:,i] * K.log(y_pred[:,i] + epsilon) + neg_weights[i]*(1-y_true[:,i]) * K.log(1-y_pred[:,i]+epsilon))))
  5. TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'.

答案1

得分: 0

as delirium mentioned, I used : tf.cast(variable, tf.float32)

英文:

as delirium mentioned, I used : tf.cast(variable, tf.float32)

huangapple
  • 本文由 发表于 2023年2月14日 04:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定