亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

圖像理解——CNN Triplet loss

圖像理解——CNN Triplet loss

斯蒂芬大帝 2021-09-02 20:22:17
我是 NN 的新手,并試圖創建一個簡單的 NN 來理解圖像。我嘗試使用三重損失方法,但不斷收到錯誤,讓我覺得我錯過了一些基本概念。我的代碼是:def triplet_loss(x):  anchor, positive, negative = tf.split(x, 3)  pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)  neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)  basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), ALPHA)  loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)  return lossdef build_model(input_shape):  K.set_image_data_format('channels_last')  positive_example = Input(shape=input_shape)  negative_example = Input(shape=input_shape)  anchor_example = Input(shape=input_shape)  embedding_network = create_embedding_network(input_shape)  positive_embedding = embedding_network(positive_example)  negative_embedding = embedding_network(negative_example)  anchor_embedding = embedding_network(anchor_example)  merged_output = concatenate([anchor_embedding, positive_embedding, negative_embedding])  loss = Lambda(triplet_loss, (1,))(merged_output)  model = Model(inputs=[anchor_example, positive_example, negative_example],              outputs=loss)  model.compile(loss='mean_absolute_error', optimizer=Adam())  return modeldef create_embedding_network(input_shape):  input_shape = Input(input_shape)  x = Conv2D(32, (3, 3))(input_shape)  x = PReLU()(x)  x = Conv2D(64, (3, 3))(x)  x = PReLU()(x)  x = Flatten()(x)  x = Dense(10, activation='softmax')(x)  model = Model(inputs=input_shape, outputs=x)  return model使用以下方法讀取每個圖像:imageio.imread(imagePath, pilmode="RGB")以及每個圖像的形狀:(1024, 1024, 3)然后我使用我自己的三元組方法(只創建 3 組錨,正負)triplets = get_triplets(data)triplets.shape形狀為(示例數量、三元組、x_image、y_image、通道數(RGB)):(20, 3, 1024, 1024, 3)然后我使用 build_model 函數:model = build_model((1024, 1024, 3))問題從這里開始:model.fit(triplets, y=np.zeros(len(triplets)), batch_size=1)對于這行代碼,當我嘗試訓練我的模型時,我收到此錯誤:有關更多詳細信息,我的代碼在此協作筆記本中我使用的圖片可以在這個驅動器中找到 為了無縫運行 - 將此文件夾放在我的驅動器/Colab 筆記本/圖像/
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

對于任何也在苦苦掙扎的人

我的問題實際上是每個觀察的維度。按照評論中的建議更改尺寸

(?, 1024, 1024, 3)

使用解決方案更新的 colab 筆記本

Ps - 我還將圖片的大小更改為 256 * 256,以便代碼在我的電腦上運行得更快。


查看完整回答
反對 回復 2021-09-02
  • 1 回答
  • 0 關注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號