“我的图像”文件夹设置为一个主文件夹,其中包含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)```
1条答案
按热度按时间nwsw7zdq1#
如docs中关于
tfa.losses.TripletSemiHardLoss
的说明:我们期望标签y_true是由多类整数标签组成的形状为[batch_size]的1-D整数Tensor,而嵌入向量y_pred必须是由l2个归一化嵌入向量组成的2-D浮点Tensor
因此,您应该使用稀疏整数标签(sparse_categorical),而不是单热编码标签(categorical)。
在您的情况下,您必须将参数
class_mode
设置为sparse
: