全部。我正在尝试执行与此official keras tutorial类似的操作,而其中的一小部分却使我感到疯狂。我确定我做错了什么,但我不知道这可能是什么。
我基本上已经模仿了本教程,但是由于某些原因,我的代码不会注册输入。这就是我的意思。当我运行它时,我得到了:
label (InputLayer) [(None, 25)] 0
__________________________________________________________________________________________________
logits (Dense) (None, 30, 67) 51523 reshape[0][0]
__________________________________________________________________________________________________
CTC_Layer (CTCLayer) (None, 30, 67) 0 label[0][0]
但是,keras教程将其显示为模型摘要:
__________________________________________________________________________________________________
dense2 (Dense) (None, 50, 20) 2580 bidirectional_1[0][0]
__________________________________________________________________________________________________
ctc_loss (CTCLayer) (None, 50, 20) 0 label[0][0]
dense2[0][0]
如您所见,我的代码没有将密集层注册为输入。 CTC层并不是说它与我的卷积序列有关。我究竟做错了什么?这真让我抓狂。以下是我的代码:
def loss_fn(self, y_true, y_pred):
batch_len = tf.keras.backend.cast(tf.shape(y_true)[0], dtype="int64")
input_length = tf.keras.backend.cast(tf.shape(y_pred)[1], dtype="int64") #Comes out to be 30
label_length = tf.keras.backend.cast(tf.shape(y_true)[1], dtype="int64") #Comes out to be 25
input_length = 30 * tf.ones(shape=(batch_len, 1), dtype="int64") #Just hardcoded 30 for now
label_length = 25 * tf.ones(shape=(batch_len, 1), dtype="int64") #Just hardcoded 25 for now
y_true = tf.keras.layers.Softmax()(y_true)
y_pred = tf.keras.layers.Softmax()(y_pred)
print("y_true shape %s" %y_true.shape) #Outputs y_true shape (32, 25)
print(y_true) #outputs Tensor("loss_fn/softmax/Softmax:0", shape=(32, 25), dtype=float32)
print("y_pred shape %s" %y_pred.shape) #Outputs y_pred shape (32, 30, 67)
print(y_pred) #outputs Tensor("loss_fn/softmax_1/Softmax:0", shape=(32, 30, 67), dtype=float32)
loss = tf.keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)
return tf.reduce_mean(loss)
def ResNet(self):
filter = self.confs["filters"]
batch_size = self.confs["batch_size"]
#conv1
input = tf.keras.Input(
shape=(self.confs["image_height"], self.confs["image_width"], self.confs["image_channel"]))
labels = tf.keras.Input(name="label", shape=(None,), dtype="float32")
print("input")
print(input.shape)
h1 = tf.keras.layers.Conv2D(filters=filter[0], padding="same", kernel_size=3)(input)
h1 = tf.keras.layers.BatchNormalization()(h1)
h1 = tf.keras.activations.relu(h1)
h2 = tf.keras.layers.Conv2D(filters=filter[1], padding="same", kernel_size=3)(h1)
h2 = tf.keras.layers.BatchNormalization()(h2)
h2 = tf.keras.activations.relu(h2)
#conv2
h3 = tf.keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2), padding="valid")(h2)
# h3 = h2
h5 = self.RN(h3, filters=filter[2], repeats=1)
h6 = tf.keras.layers.Conv2D(filters=filter[2], padding="same", kernel_size=3)(h5)
h6 = tf.keras.layers.BatchNormalization()(h6)
h6 = tf.keras.activations.relu(h6)
...
...
...
...
logits = tf.keras.layers.Dense(67, kernel_initializer=weight_initializer, bias_initializer=bias_initializer, name="logits", activation="softmax")(out)
print("logits %s" %logits.shape) #Outputs logits (None, 30, 67)
print("________________________")
print(logits)
logits2 = CTCLayer(name= "CTC_Layer")(labels, logits)
model = tf.keras.Model(inputs=[input, labels], outputs=logits2, name="full_model")
model.compile(optimizer="RMSprop")
# model.compile(optimizer="RMSprop", loss=self.loss_fn)
print(model.summary())
以下是读取数据并制作模型的代码: x是输入图像,y是标签。
m = get_model(confs)
model = m.ResNet()
d = dataset.Dataset(confs)
train_data = d.read_data(confs["trn_data_files"])
valid_data = d.read_data(confs["val_data_files"])
callbacks = [
tf.keras.callbacks.ModelCheckpoint("./model_checkpoint", monitor="val_loss")
]
for x,y in train_data:
history = model.fit(
x=x,
y=y,
validation_data=valid_data,
epochs=50,
callbacks=callbacks,
)
非常感谢大家。