使用我自己的图像从Keras插件的TripletSemiHardLoss

cwtwac6a  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(112)

“我的图像”文件夹设置为一个主文件夹,其中包含130个单独的文件夹,每个文件夹都有自己的图像文件夹folder_with_130_folders-
文件夹1_类别1-
img_in_class1_folder.jpg
img_in_class1_folder.jpg
...
文件夹130_类别130-
img_in_class130_folder.jpg
img_in_class130_folder.jpg

train_dataset =  prod_images.flow_from_directory(directory, target_size=(225, 225), class_mode='categorical', subset='training', save_format='jpg')
validation_set =  prod_images.flow_from_directory(directory, target_size=(225, 225), class_mode='categorical', subset='validation', save_format='jpg')

(x_train, y_train), (x_test, y_test) = train_dataset.next(), validation_set.next()

model = models.Sequential()
model.add(layers.Conv2D(filters=128, kernel_size=2, padding='same', activation='relu', input_shape=(225, 225, 3)))
model.add(layers.MaxPooling2D(pool_size=2))
model.add(layers.Dropout(0.3))
model.add(layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=2))
model.add(layers.Dropout(0.3))
model.add(layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=2))
model.add(layers.Dropout(0.3))
model.add(layers.Conv2D(filters=16, kernel_size=2, padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=2))
model.add(layers.Dropout(0.3))
model.add(layers.Flatten())
model.add(layers.Dense(256, activation=None)) # No activation on final dense layer
model.add(layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))) # L2 normalize embeddings

model.summary()

model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.001), loss=tfa.losses.TripletSemiHardLoss())

model_fit = model.fit(train_dataset, steps_per_epoch=4, epochs=20, verbose=1, validation_data = validation_set)```
nwsw7zdq

nwsw7zdq1#

docs中关于tfa.losses.TripletSemiHardLoss的说明:
我们期望标签y_true是由多类整数标签组成的形状为[batch_size]的1-D整数Tensor,而嵌入向量y_pred必须是由l2个归一化嵌入向量组成的2-D浮点Tensor
因此,您应该使用稀疏整数标签(sparse_categorical),而不是单热编码标签(categorical)。

import tensorflow as tf
import tensorflow_addons as tfa
import pathlib

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

batch_size = 32

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  label_mode='int', # sparse categorical
  subset="training",
  seed=123,
  image_size=(225, 225),
  batch_size=batch_size)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=128, kernel_size=2, padding='same', activation='relu', input_shape=(225, 225, 3)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Conv2D(filters=16, kernel_size=2, padding='same', activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256, activation=None)) # No activation on final dense layer
model.add(tf.keras.layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=-1))) # L2 normalize embeddings

model.summary()

model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.001), loss=tfa.losses.TripletSemiHardLoss())

model_fit = model.fit(train_ds, epochs=5, verbose=1)

在您的情况下,您必须将参数class_mode设置为sparse

flow_from_directory(directory, target_size=(225, 225), class_mode='sparse', subset='training', save_format='jpg')

相关问题