本文整理汇总了Python中keras.preprocessing.image.ImageDataGenerator.rescale方法的典型用法代码示例。如果您正苦于以下问题:Python ImageDataGenerator.rescale方法的具体用法?Python ImageDataGenerator.rescale怎么用?Python ImageDataGenerator.rescale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.preprocessing.image.ImageDataGenerator
的用法示例。
在下文中一共展示了ImageDataGenerator.rescale方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_images_keras_generator
# 需要导入模块: from keras.preprocessing.image import ImageDataGenerator [as 别名]
# 或者: from keras.preprocessing.image.ImageDataGenerator import rescale [as 别名]
def read_images_keras_generator(job_model, dataset, node, trainer):
from keras.preprocessing.image import ImageDataGenerator
size = (int(node['width']), int(node['height']))
grayscale = False
if node['inputType'] == 'image':
grayscale = True
dataset_config = dataset['config']
trainer.logger.info(("Generate image iterator in folder %s " % (dataset_config['path'],)))
augmentation = bool(get_option(dataset_config, 'augmentation', False))
if augmentation:
train_datagen = get_image_data_augmentor_from_dataset(dataset)
else:
train_datagen = ImageDataGenerator()
if 'imageScale' not in node:
node['imageScale'] = 255
if float(node['imageScale']) > 0:
train_datagen.rescale = 1.0 / float(node['imageScale'])
train_generator = train_datagen.flow_from_directory(
directory=os.path.join(dataset_config['path'], 'training'),
target_size=size,
batch_size=job_model.job['config']['batchSize'],
color_mode='grayscale' if grayscale is True else 'rgb',
class_mode='categorical')
classes = []
for folderName, outputNeuron in six.iteritems(train_generator.class_indices):
if dataset['type'] == 'images_search' or dataset['type'] == 'images_upload':
category_idx = int(folderName.replace('category_', ''))
target_category = dataset_config['classes'][category_idx]
classes.append(target_category['title'] or 'Category %s' % (category_idx, ))
else:
classes.append(folderName)
trainer.set_info('classes', classes)
trainer.classes = classes
# ensure_dir(dataset_config['path'] + '/preview')
test_datagen = ImageDataGenerator()
if float(node['imageScale']) > 0:
test_datagen.rescale = 1.0 / float(node['imageScale'])
validation_generator = test_datagen.flow_from_directory(
directory=os.path.join(dataset_config['path'], 'validation'),
# save_to_dir=dataset_config['path'] + '/preview',
target_size=size,
batch_size=job_model.get_batch_size(),
color_mode='grayscale' if grayscale is True else 'rgb',
class_mode='categorical')
validation_samples = 0
train_samples = 0
# Keras 2
if hasattr(train_generator, 'num_class'):
trainer.output_size = train_generator.num_class
if hasattr(train_generator, 'samples'):
train_samples = train_generator.samples
if hasattr(validation_generator, 'samples'):
validation_samples = validation_generator.samples
# Keras 1
if hasattr(train_generator, 'nb_class'):
trainer.output_size = train_generator.nb_class
if hasattr(train_generator, 'nb_sample'):
train_samples = train_generator.nb_sample
if hasattr(validation_generator, 'nb_sample'):
validation_samples = validation_generator.nb_sample
trainer.set_info('Dataset size', {'training': train_samples, 'validation': validation_samples})
trainer.set_generator_validation_nb(validation_samples)
trainer.set_generator_training_nb(train_samples)
trainer.logger.info(("Found %d classes, %d images (%d in training [%saugmented], %d in validation) in %s " %
(len(classes), validation_samples + train_samples, train_samples, 'not ' if augmentation is False else '', validation_samples, dataset_config['path'])))
if trainer.output_size == 0:
trainer.logger.warning("Could not find any classes. Does the directory contains images?")
sys.exit(1)
trainer.logger.debug(str(train_generator.class_indices))
trainer.logger.debug(str(classes))
return {
'X_train': train_generator,
'Y_train': train_generator,
'X_test': validation_generator,
'Y_test': validation_generator,
}