gpt4 book ai didi

python - Keras 图像生成器不断给出不同数量的标签

转载 作者:行者123 更新时间:2023-12-01 07:18:50 26 4
gpt4 key购买 nike

我正在尝试使用 Market1501 数据集和 keras 制作一个简单的精细转向 Resnet50 模型。

因此数据集包含图像(12000 左右)和我想要使用的 751 个标签(0-750)。我可以将数据放入一次,因此我必须为此使用图像生成器。

所以我的基本模型是这样的

base_model = ResNet50(weights='imagenet', include_top=False,input_tensor=Input(shape=(224,224,3)))
x = base_model.output
x = Flatten(name="flatten")(x)
x = Dropout(0.5)(x)
x = Dense(750, activation='softmax', name='fc8',kernel_initializer=RandomNormal(mean=0.0, stddev=0.001))(x)
model = Model(input=base_model.input, output=x)

我的图像生成器是这样的

def image_generator(image_array, batch_size):
# Define data generator arguments
datagen_args = dict(rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True)

# Create different data generators for each image
# This gives each image a unique transformation which will make it harder for the network
datagen = ImageDataGenerator(**datagen_args)
while True:
number_of_images = len(image_array)
indices = np.random.permutation(np.arange(number_of_images))
num_batches = number_of_images // batch_size
for bid in range(num_batches):
# loop once per batch
images = []
lables = []
batch_indices = indices[bid * batch_size: (bid + 1) * batch_size]
for i in batch_indices:
img, lbl = image_array[i]
# Process images
img = image.load_img(os.path.join(TRAIN, img), target_size=[224, 224])
img = image.img_to_array(img)
#img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
img = datagen.random_transform(img)
images.append(img)
lables.append(lbl)
yield np.array(images), to_categorical(lables)

我这样使用它

batch_size = 64
NUM_EPOCHS = 40
train_gen = image_generator(image_array, batch_size)
num_train_steps = len(image_array)

问题是它给了我这个错误

Error when checking target: expected fc8 to have shape (751,) but got array with shape (742,)

更大的问题是第二个数字不断变化,所以我知道图像生成器没有为每次迭代获取每个标签。

编辑
数据如何生成:
有一个外部列表,其中包含图像和标签,如下所示

['0002_451_03.jpg', '0']
img001.jpg, 0
img002.jpg, 0
...
img1500.jpg, 750

这被读入并加载到数组中。标签是图片后面的数字

最佳答案

改变

batch_indices = indices[bid * batch_size: (bid + 1) * batch_size]

batch_indices = indices[bid * batch_size: min((bid + 1) * batch_size, number_of_images)]

关于python - Keras 图像生成器不断给出不同数量的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57803735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com