Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何批量训练具有不同图像形状的模型_Python_Numpy_Tensorflow_Machine Learning_Keras - Fatal编程技术网

Python 如何批量训练具有不同图像形状的模型

Python 如何批量训练具有不同图像形状的模型,python,numpy,tensorflow,machine-learning,keras,Python,Numpy,Tensorflow,Machine Learning,Keras,我试图训练一个具有动态图像输入大小的模型。对于batch_size=1,它可以正常工作,但是,如果batch size大于1,它会抛出一个错误 挖掘之后,我才知道numpy只允许相同形状的图像作为批处理通过 代码是这样的 enter code here def sample_images(data_dir, batch_size): # Make a list of all images inside the data directory all_images = glob.g

我试图训练一个具有动态图像输入大小的模型。对于batch_size=1,它可以正常工作,但是,如果batch size大于1,它会抛出一个错误

挖掘之后,我才知道numpy只允许相同形状的图像作为批处理通过

代码是这样的

enter code here
def sample_images(data_dir, batch_size):
    # Make a list of all images inside the data directory
    all_images = glob.glob(data_dir)
    # print(all_images)


   images_batch = np.random.choice(all_images, size=batch_size)

   #creating empty arrays for sets of given batch size
   low_resolution_images_set = []
   high_resolution_images_set = []

   for x in range(int(len(all_images)/batch_size)):
       # Choose a random batch of images
       images_batch = np.random.choice(all_images, size=batch_size)
       low_resolution_images = []
       high_resolution_images = []
       for img in images_batch:

           # Get an ndarray of the current image
           img1 = imread(img, mode='RGB')
           frame = cv2.imread(img)
           height, width, channels = frame.shape
           img1 = img1.astype(np.float32)

           low_resolution_shape = (int(height/4), int(width/4), channels)
           high_resolution_shape = (low_resolution_shape[0]*4, low_resolution_shape[1]*4, channels)

           img1_high_resolution = imresize(img1, high_resolution_shape)
           img1_low_resolution = imresize(img1, low_resolution_shape)

           # Do a random flip
           if np.random.random() < 0.5:
               img1_high_resolution = np.fliplr(img1_high_resolution)
               img1_low_resolution = np.fliplr(img1_low_resolution)


           high_resolution_images.append(img1_high_resolution)
           low_resolution_images.append(img1_low_resolution)

       high_resolution_images_set.append(high_resolution_images)
       low_resolution_images_set.append(low_resolution_images)
   return np.array(high_resolution_images_set), np.array(low_resolution_images_set)
enter code here
在此处输入代码
def样本图像(数据目录、批量大小):
#列出数据目录中的所有图像
所有图像=glob.glob(数据目录)
#打印(所有图像)
图片\u batch=np.random.choice(所有图片,大小=批次大小)
#为给定批量大小的集合创建空数组
低分辨率图像集=[]
高分辨率\u图像\u set=[]
对于范围内的x(int(len(所有图像)/批量大小):
#随机选择一批图像
图片\u batch=np.random.choice(所有图片,大小=批次大小)
低分辨率图像=[]
高分辨率图像=[]
对于图像中的img\u批次:
#获取当前图像的数据数组
img1=imread(img,mode='RGB')
帧=cv2.imread(img)
高度、宽度、通道=frame.shape
img1=img1.astype(np.float32)
低分辨率形状=(整数(高/4)、整数(宽/4)、通道)
高分辨率形状=(低分辨率形状[0]*4,低分辨率形状[1]*4,通道)
img1\u高分辨率=imresize(img1,高分辨率形状)
img1\u低分辨率=imresize(img1,低分辨率形状)
#做一个随机翻转
如果np.random.random()<0.5:
img1\u高分辨率=np.fliplr(img1\u高分辨率)
img1\u低分辨率=np.fliplr(img1\u低分辨率)
高分辨率图像。附加(img1高分辨率)
低分辨率图像。附加(img1低分辨率)
高分辨率图像集。附加(高分辨率图像)
低分辨率图像集。附加(低分辨率图像)
返回np.array(高分辨率图像集),np.array(低分辨率图像集)
在这里输入代码

如何在批量大小方面训练我的体系结构?

正如您在我的评论中回答的那样,您有各种不同分辨率的图像,因此这里有一个解决方案。只需为训练图像预处理选择所需的分辨率。下面的代码将根据需要通过放大或缩小来调整所有图像的大小。只需选择源图像文件夹和目标图像文件夹。将所有图像调整到相同大小后,您可以继续进行进一步的操作

# -*- coding: utf-8 -*-
"""
Created on Sat Mar  7 18:54:24 2020

@author: Hafiz
"""

import cv2
import glob
import os
import numpy as np

# choose where you want to save the resized images (here the destination folder will be created in the same place where the code resides).
destination = r'.\destination\/'  

try:                                        # making the destination folder
    if not os.path.exists(destination):
        os.makedirs(destination)
except OSError:
    print ('Error while creating directory')


image_no = 0
resolution = (512, 512) # use your desired resolution

for img_path in glob.glob(r'C:\Users\source/*.jpg'): # insert your input image folder directory here
                                                     # if the folder contains various types of images rather than only .jpg therefore use *.* instead of *.jpg (but make sure the folder contains only images)

    img = cv2.imread(img_path)


    if img is None:  # checking if the read image is not a NoneType
        continue

    img = cv2.resize(img, resolution) # the image will be zoomed in or out depending on the resolution

    cv2.imwrite(destination + 'image_' + str(image_no) + '.jpg', img)

    image_no = image_no + 1

在批处理之前,调整所有图像的大小,使其形状相同。您可以使用cv2.resize()谢谢@hafiz031,但问题是,我的训练数据集在输入大小上有很多变化。(例如:我无法将10*10和100*100图像的大小调整为50*50,我可能会丢失图像中的重要细节)检查
RaggedTensor
s是否是您的一个选项(尽管它们还没有得到所有地方的100%支持),否则,图像填充可能是一个选项。您可能需要阅读更多关于模型工作原理的信息,它做出了什么样的假设,等等。这应该在
keras/trensorflow
文档中。如果你理解了机器学习背后的一些理论,你就会明白为什么一致的图像大小是必要的。