keras 将datagen.flow_from_directory与图像分割和类数结合使用

qzwqbdag  于 2023-03-18  发布在  其他
关注(0)|答案(2)|浏览(143)

我使用了“flow_from_directory”,但我的“lose”没有减少。我注意到当我运行“fit_generator”时,它显示有1个类,尽管我的掩码有3个类。我的问题是,我们是否需要在“datagen.flow_from_directory”中指示有多少个类?您是否看到“datagen.flow_from_directory”调用中的任何错误:

我的目录结构如下所示:

我的代码如下所示:

inputs = tf.keras.layers.Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name="input_image")

model  = tf.keras.applications.ResNet50(input_tensor=inputs, weights=None, include_top=true)

LR = 0.0001
optim = keras.optimizers.Adam(LR)

dice_loss_se2 = sm.losses.DiceLoss()
mae = tf.keras.losses.MeanAbsoluteError( )
metrics = [ mae,sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5) , dice_loss_se2]

model.compile(optimizer=optim,loss= dice_loss_se2,metrics= metrics)

image_datagen = ImageDataGenerator()
                
mask_datagen = ImageDataGenerator()
                 
image_generator =image_datagen.flow_from_directory( "/mydata/train/image", target_size=(IMAGE_SIZE, IMAGE_SIZE)
                                                   , class_mode = None,
                                                  )
                                                   

mask_generator = mask_datagen.flow_from_directory("/mydata/train/mask"  , target_size=(IMAGE_SIZE, IMAGE_SIZE)
                                                , class_mode = None,
                                                 )
                                                   

train_generator = zip(image_generator, mask_generator)

train_steps = 1212//batch_size

#---------------------------

image_generator_val =image_datagen.flow_from_directory( "/mydata/Validation/image", target_size=(IMAGE_SIZE, IMAGE_SIZE)
                                                   , class_mode = None,
                                                  )
                                                    

mask_generator_val = mask_datagen.flow_from_directory("/mydata/Validation/mask"  , target_size=(IMAGE_SIZE, IMAGE_SIZE)
                                                , class_mode = None,
                                                 )
                                                  )

val_generator = zip(image_generator_val, mask_generator_val)

val_steps = 250//batch_size


history =model.fit_generator(train_generator, validation_data=val_generator , steps_per_epoch=train_steps, validation_steps=val_steps , epochs=epochs, verbose=1)
e5nqia27

e5nqia271#

你的问题出在你的目录结构上。你需要的是如下所示的目录结构

mydata
---- train
     ---- image
          ------1.jpg
          ------2.jpg

     ---- mask
          ------1.png
          ------2.png

你只得到一个类,因为生成器只看到img类。2所以只要按照上面的目录结构移动你的图像

piwo6bdm

piwo6bdm2#

他们还做了一种方式,特定子集的训练或验证或指定文件夹,我的文件夹结构(目录)是见下.

F:\datasets\downloads\example\image
F:\datasets\downloads\example\image\Bee
F:\datasets\downloads\example\image\Shiny Jumbo
F:\datasets\downloads\example\image\Sleepy cat
...

def gen():
    train_generator = ImageDataGenerator(
            rescale=1./255,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True)
    train_generator = train_generator.flow_from_directory(
            directory,
            target_size=(150, 150),
            batch_size=32,
            class_mode='binary',    # None  # categorical   # binary
            subset='training')
    target = np.array([[i] for i in range(10)])
            
    return train_generator

train_generator = gen()
val_generator = train_generator

inputs = tf.keras.layers.Input(shape=(150, 150, 3), name="input_image")
model  = tf.keras.applications.ResNet50(input_tensor=inputs, weights=None, include_top=True)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
    learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
    name='Nadam'
) # 0.00001

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
# 1
# lossfn = tf.keras.losses.MeanSquaredLogarithmicError(reduction=tf.keras.losses.Reduction.AUTO, name='mean_squared_logarithmic_error')
# 2
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit_generator(train_generator, validation_data=val_generator, steps_per_epoch=train_steps, validation_steps=val_steps , epochs=epochs, verbose=1)

input('...')

找到10张图片,属于10个类。
Output example

相关问题