Tensorflow如何从大型图像的小数据集中采样大量纹理
我有100个大ish(1000x1000)图像,我想用作纹理分析系统的训练数据集。我想随机生成大约200x200的纹理样例。最好的方法是什么?我不希望预处理所有的样例,这样每个历元都会使用稍微不同的样例进行训练 我最初的(幼稚的?)实现包括模型中的预处理层,这些层对图像进行随机裁剪,只需做大量的历元来容纳少量的大图片,但是大约400个历元后TF将毫无例外地崩溃(它将刚刚退出) 我现在发现自己正在编写一个数据生成器(tf.keras.utils.Sequence),它将根据请求返回一批样例,但我觉得我正在重新发明轮子,而且越来越笨重——这让我觉得这不是最好的方法Tensorflow如何从大型图像的小数据集中采样大量纹理,tensorflow,Tensorflow,我有100个大ish(1000x1000)图像,我想用作纹理分析系统的训练数据集。我想随机生成大约200x200的纹理样例。最好的方法是什么?我不希望预处理所有的样例,这样每个历元都会使用稍微不同的样例进行训练 我最初的(幼稚的?)实现包括模型中的预处理层,这些层对图像进行随机裁剪,只需做大量的历元来容纳少量的大图片,但是大约400个历元后TF将毫无例外地崩溃(它将刚刚退出) 我现在发现自己正在编写一个数据生成器(tf.keras.utils.Sequence),它将根据请求返回一批样例,但我觉
处理这样一种情况的最佳方法是什么?在这种情况下,您有一个较小的数据集,可以从中动态创建更多样本。我编写了一个分割图像的函数。代码如下
import cv2
def image_segment( image_path, img_resize, crop_size):
image_list=[]
img=cv2.imread(image_path)
img=cv2.resize(img, img_resize)
shape=img.shape
xsteps =int( shape[0]/crop_size[0])
ysteps = int( shape[1]/crop_size[1])
print (xsteps, ysteps)
for i in range (xsteps):
for j in range (ysteps):
x= i * crop_size[0]
xend=x + crop_size[0]
y= j * crop_size[1]
yend = y + crop_size[1]
cropped_image = cropped_image=img[x: xend, y: yend]
image_list.append(cropped_image)
return image_list
下面是一个使用示例
# This code provides input to the image_segment function
image_path=r'c:\temp\landscape.jpg' # location of image
width=1000 # width to resize input image
height= 800 # height to resize input image
image_resize=( width, height) # specify original image (width, height)
crop_width=200 # width of desired cropped images
crop_height=400 # height of desired cropped images
# Note to get full set of cropped images width/cropped_width and height/cropped_height should be integer values
crop_size=(crop_height, crop_width)
images=image_segment(image_path, image_resize, crop_size) # call the function
下面的代码将显示调整大小的输入图像和生成的裁剪图像
# this code will display the resized input image and the resultant cropped images
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
img=cv2.imread(image_path) # read in the image
input_resized_image=cv2.resize(img, image_resize) # resize the image
imshow(input_resized_image) # show the resized input image
r=len(images)
plt.figure(figsize=(20, 20))
for i in range(r):
plt.subplot(5, 5, i + 1)
image=images # scale images between 0 and 1 becaue pre-processor set them between -1 and +1
plt.imshow(image[i])
class_name=str(i)
plt.title(class_name, color='green', fontsize=16)
plt.axis('off')
plt.show()