如何为python的深入学习选择合适的图像形状

如何为python的深入学习选择合适的图像形状,python,tensorflow,image-processing,keras,deep-learning,Python,Tensorflow,Image Processing,Keras,Deep Learning,我有一个模型要使用keras在python中训练,我的训练数据集包含30000个图像。这些图像代表了总共6类的动物 我的问题是当我来选择一个图像\u形状进行训练时, 这些图像有不同的形状,所以我不太确定在这种情况下该怎么办 这个案子。我想找到最大/最小高度和最大/最小值 宽度,看看什么样的形状应该是一个像样的选择 从keras.preprocessing.image导入ImageDataGenerator 从keras.models导入模型 从keras.models导入顺序 从keras.o

我有一个模型要使用keras在python中训练,我的训练数据集包含30000个图像。这些图像代表了总共6类的动物

我的问题是当我来选择一个
图像\u形状
进行训练时, 这些图像有不同的形状,所以我不太确定在这种情况下该怎么办 这个案子。我想找到最大/最小高度和最大/最小值 宽度,看看什么样的形状应该是一个像样的选择


从keras.preprocessing.image导入ImageDataGenerator
从keras.models导入模型
从keras.models导入顺序
从keras.optimizers导入Adam
从keras.layers导入Conv2D、maxpoolig2d、输入、批次标准化、上采样2D、激活、退出、展平、密集
从keras.callbacks导入csvloger、ModelCheckpoint、earlystoping
将tensorflow.compat.v1导入为tf
从tensorflow.compat.v1.keras将后端导入为K
来自keras.engine.saving导入负载_模型
将matplotlib.pyplot作为plt导入
# ==========================================
#======================GPU设置==================
# ==========================================
config=tf.ConfigProto(设备计数={'GPU':2,'CPU':4})
sess=tf.Session(config=config)
K.SETU会话(sess)
# ==========================================
#===================变量=================
# ==========================================
drivePath=“驱动器/我的驱动器/”
mainDataPath=drivePath+“donnees/”
DataPath=mainDataPath+“夹带”
testPath=mainDataPath+“测试”
modelsPath=“Model.hdf5”
n#u类=6#动物类名称
训练量=24000,共计24个000(4000个PAR类)
ValueStudioButhChySt==6000×6000(1000个PAR类)
图像_比例=????
图像通道=3个颜色通道(1个:黑色和白色图像通道;3个红色通道)
图像_color_mode=“rgb”#灰度图像黑色和白色;rgb倒装图片库
图像形状=(图像比例,图像比例,图像通道)#图像的形式,图像的形状,图像的形状
fit_batch_size=32 35; le nombre d'images夹带合奏:非批量
适合时代=50个时代
如何计算图像的
最大高度
最小高度
平均高度
最大宽度
最小宽度
平均宽度
,并根据这些值选择合适的形状

这是我的数据结构

训练这些图像的诀窍是要意识到,您将不会使用原始分辨率的图像,而是将它们裁剪为普通大小,以便能够高效地进行批处理


在您的情况下,您应该查看类似
https://www.tensorflow.org/api_docs/python/tf/image/resize_with_crop_or_pad
然后用看起来合理的方法进行试验,或者用能带来最佳结果的方法进行试验。祝你好运

训练这些图像的诀窍是要意识到,您将不会使用原始分辨率的图像,而是将它们裁剪为通用大小,以便能够高效地进行批处理


在您的情况下,您应该查看类似
https://www.tensorflow.org/api_docs/python/tf/image/resize_with_crop_or_pad
然后用看起来合理的方法进行试验,或者用能带来最佳结果的方法进行试验。祝你好运

通常,图像越大,模型的性能越好。然而,这假设图像一开始就具有一定的保真度。例如,如果您的大多数图像都是50 X 50 X 3,那么将它们放大到sat 250 X 250 X 3可能不会帮助您的模型更好地执行,并且在计算的基础上,计算成本将增加25倍。最好的设置可能是将图像大小设置为高度和宽度的平均值。我发现,一般来说,只要图像大小合理(例如200 X 200 X 3),图像大小就没有那么重要。通过依次读取所有文件,确定高度和宽度并取平均值,可以完成所有工作以获得平均值。代码如下所示:

import os
import cv2
train_dir = r'drive\mydrive\donnees\entrainement' # your train directory
test_dir = r'drive\mydrive\donnees\test'          # your test directory
total_height, total_width,total_files= 0,0,0      # initialize counters
for tr_test in [train_dir, test_dir]:             # iterate through train and test directories
    class_list= os.listdir(tr_test)               # get the list of class directories
    for c in class_list:                          # iterate through the class directories
        c_path=os.path.join(tr_test, c)
        f_list=os.listdir(c_path)                 # get the list of files in the class directory
        for f in f_list:                          # iterate through the files
            f_path=os.path.join(c_path, f)
            img=cv2.imread(f_path)                # read in the image
            height, width, channels=img.shape     # get the height and width
            total_height=total_height + height
            total_width=total_width + width
            total_files=total_files +1
average_height=int (total_height/total_files)     # determine average height
average_width= int(total_width/total_files)       # determine average width
print ('Average Height = ', average_height, '   Average Width = ', average_width, '   Total Files = ', total_files)

通常,图像越大,模型的性能越好。然而,这假设图像一开始就具有一定的保真度。例如,如果您的大多数图像都是50 X 50 X 3,那么将它们放大到sat 250 X 250 X 3可能不会帮助您的模型更好地执行,并且在计算的基础上,计算成本将增加25倍。最好的设置可能是将图像大小设置为高度和宽度的平均值。我发现,一般来说,只要图像大小合理(例如200 X 200 X 3),图像大小就没有那么重要。通过依次读取所有文件,确定高度和宽度并取平均值,可以完成所有工作以获得平均值。代码如下所示:

import os
import cv2
train_dir = r'drive\mydrive\donnees\entrainement' # your train directory
test_dir = r'drive\mydrive\donnees\test'          # your test directory
total_height, total_width,total_files= 0,0,0      # initialize counters
for tr_test in [train_dir, test_dir]:             # iterate through train and test directories
    class_list= os.listdir(tr_test)               # get the list of class directories
    for c in class_list:                          # iterate through the class directories
        c_path=os.path.join(tr_test, c)
        f_list=os.listdir(c_path)                 # get the list of files in the class directory
        for f in f_list:                          # iterate through the files
            f_path=os.path.join(c_path, f)
            img=cv2.imread(f_path)                # read in the image
            height, width, channels=img.shape     # get the height and width
            total_height=total_height + height
            total_width=total_width + width
            total_files=total_files +1
average_height=int (total_height/total_files)     # determine average height
average_width= int(total_width/total_files)       # determine average width
print ('Average Height = ', average_height, '   Average Width = ', average_width, '   Total Files = ', total_files)