微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不消耗过多RAM的情况下预处理图像数据?

如何解决如何在不消耗过多RAM的情况下预处理图像数据?

这似乎是一个基本问题,但我坚持不懈,并希望获得一些帮助。

我正在尝试以DICOM格式加载和预处理某些图像,以便将其提供给我的Keras模型,因为我有大约2000张图像,所以在完成预处理步骤之前,RAM已消耗掉。 这是预处理步骤的代码

(目录,标签是预定义变量)

shape=(256,256)
patients_filename=tf.constant([directory+'/'+path for path in os.listdir(directory)])
dataset = tf.data.Dataset.from_tensor_slices((patients_filename,labels))
def parse_function(patientfilename,label):
    var=tf.data.Dataset.list_files(patientfilename+'/*')
    for image in var:
        image=tf.io.read_file(image)
        image = tfio.image.decode_dicom_image(image,dtype=tf.uint64)
        image = tf.cast(image,tf.float32)
        image=tf.image.resize(image,size=shape)/65535.0
        image=tf.reshape(image,shape+(1,))
    return image,label

dataset = dataset.map(parse_function).batch(8).prefetch(1)

然后我将预处理数据(数据集)提供给模型。

您知道我该如何做吗?

解决方法

您可以使用tensorflows movie: Movie; private terminate$: Subject = new Subject(); ngOnInit() { this.ms.movie$.pipe(takeUntil(terminate$)).subscribe(data => this.movie = data); } ngOnDestroy() { terminate$.next(); terminate$.complete(); } // You don't really show where the search-id comes from or how it is set,so here I assume it is passed in from the html onSearch(id) { this.ms.search(id); } 预处理图像,并使用它的'flow_from_directory`方法在需要时从磁盘加载数据。

tf.keras.preprocessing.image.ImageDataGenerator

您的目录结构应为

train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True
)

test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)


train_generator = train_datagen.flow_from_directory(
        'data/train',target_size=(150,150),batch_size=32,class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
        'data/validation',class_mode='categorical')

model.fit(
        train_generator,steps_per_epoch=2000,epochs=50,validation_data=validation_generator,validation_steps=800)

标签是自动从目录名称派生的。

有关更多预处理选项,请参见文档:Link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。