我正在尝试将图像映射到图像,我已经使用图像生成器这样做了。然而,函数model.fit()不会将生成器作为参数。下面是我的代码:
DIR = '/content/drive/My Drive/Datasets/CatDog/dataset/training_set/'
training_datagen = image.ImageDataGenerator(
rescale=1./255
)
traininput_generator = training_datagen.flow_from_directory(
DIR,
classes = ['cats'],
class_mode = None,
target_size= (30,30),
batch_size = 32
)
trainoutput_generator = training_datagen.flow_from_directory(
DIR,
classes = ['cats'],
class_mode = None,
target_size= (60,60),
batch_size = 32
)
model = keras.models.Sequential([
keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=(30,30,3)),
keras.layers.MaxPool2D(2,2),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.MaxPool2D(2, 2),
keras.layers.Flatten(),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation= 'relu'),
keras.layers.Dense(128, activation= 'relu'),
keras.layers.Dense(10800, activation= 'relu'),
keras.layers.Reshape((60,60,3))
])
print(model.output_shape)
assert model.output_shape == (None, 60, 60, 3)
adam = tf.keras.optimizers.Adam()
model.compile(loss = 'MeanSquaredError', optimizer=adam, metrics=['accuracy'])
model.fit(traininput_generator, y= trainoutput_generator)
当我尝试训练模型时,我得到以下错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-25238523a64d> in <module>()
1 adam = tf.keras.optimizers.Adam()
2 model.compile(loss = 'MeanSquaredError', optimizer=adam, metrics=['accuracy'])
----> 3 model.fit(traininput_generator,y = trainoutput_generator)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, shuffle, workers, use_multiprocessing, max_queue_size, model, **kwargs)
890 **kwargs):
891 if not is_none_or_empty(y):
--> 892 raise ValueError("`y` argument is not supported when using "
893 "`keras.utils.Sequence` as input.")
894 if not is_none_or_empty(sample_weights):
ValueError: `y` argument is not supported when using `keras.utils.Sequence` as input.
从模型。适合文档
y Target data. Like the input data x, it could be either
Numpy array(s) or TensorFlow tensor(s). It should be consistent
with x (you cannot have Numpy inputs and tensor targets, or
inversely). If x is a dataset, generator, or keras.utils.Sequence
instance, y should not be specified (since targets will be
obtained from x).
在你的例子中,x是一个同时提供图像和标签的生成器,所以不应该指定y。您必须为x构建一个自定义生成器,以便它将输入图像作为输出元组的第一个元素,并将所需图像作为元组的第二个元素提供。或者,您可以将x、y作为numpy数组提供。下面是应该完成这项工作的生成器的代码。
import os
import numpy as np
import cv2
class image_gen():
def __init__(self, sdir, batch_size,input_size, target_size, shuffle):
self.batch_index=0
self.sdir=sdir # directory containing input images
self.batch_size=batch_size #batch size is number of samples in a batch
self.target_size=target_size # tuple (width, height) for target image
self.input_size=input_size
self.shuffle=shuffle # set to True to shuffle images, False for no shuffle
self.label_list=[] # initialize list to hold sequential list of total labels generated
self.image_list=[] # initialize list to hold sequential list of total images filenames generated
self.s_list=os.listdir(self.sdir) # list of images in directory
def get_images(self): # gets a batch of input images, resizes input image to make target images
input_image_batch=[] # initialize list to hold a batch of target images
target_image_batch=[] # initialize list to hold batches of input images
sample_count=len(self.s_list) # determine total number of images available
for i in range(self.batch_index * self.batch_size, (self.batch_index + 1) * self.batch_size ): #iterate for a batch
j=i % sample_count # cycle j value over range of available images
k=j % self.batch_size # cycle k value over batch size
if self.shuffle: # if shuffle select a random integer between 0 and sample_count-1 to pick as the image=label pair
m=np.random.randint(low=0, high=sample_count-1, size=None, dtype=int)
else:
m=j # no shuffle
path_to_img=os.path.join(self.sdir, self.s_list[m]) # define the path to the m th image
input_image=cv2.imread(path_to_img)
input_image=cv2.resize( input_image,self.input_size)
target_image=cv2.resize(input_image, self.target_size) #create the target image from the input image
input_image_batch.append(input_image)
target_image_batch.append(target_image)
input_image_array=np.array(input_image_batch)
target_image_array=np.array(target_image_batch)
self.batch_index=self.batch_index +1
yield (input_image_array, target_image_array ) # yield np array of input, labels
下面是使用
的例子batch_size=5
target_shape=(128,128,)
sdir=r'C:TempBIRDStrainALBATROSS' # set this to your image directory
shuffle=False
gen=image_gen(sdir, batch_size, target_shape, shuffle) # instantiate an instance of the class
input_images, target_images=next(gen.get_images()) # get a batch of inputs, labels
您可以使用下面的代码查看生成的图像和目标
import matplotlib.pyplot as plt
%matplotlib inline
fig_height=batch_size * 4
plt.figure(figsize=( 10, fig_height))
rows=batch_size
columns=2
for row in range(rows):
for col in range(columns):
i=row*columns +col
plt.subplot(rows, columns, i + 1)
if col==0:
img=input_images[row]/255
else:
img=target_images[row]/255
imgplot=plt.imshow(img)
#plt.axis('off')
plt.imshow(img)
我尝试了这个生成器与模型。适合x=image_gen.get_image()等,但由于某种原因,它给出了一个错误,它运行的图像。正在处理