Tenserflow-Keras模型不接受我的python生成器输出



我正在学习一些教程,介绍如何为一些图像分类设置我的第一个conv NN。

教程将所有图像加载到内存中,并将它们传递到model.fit((中。我不能这样做,因为我的数据集太大了。

我把这个生成器写为";滴进";预处理图像到模型.fit,但我得到了一个错误,因为我是一个新手,我有诊断困难。

这些也仅作为灰度图像进行处理。

这是我做的发电机。。。

# need to preprocess image in batches because memory
# tdata expects list of tuples<(string) file_path, (int) class_num)>
def image_generator(tdata, batch_size):
start_from = 0;
while True:

# Slice array into batch data
batch = tdata[start_from:start_from+batch_size]

# Keep track of position
start_from += batch_size

# Create batch lists
batch_x = []
batch_y = [] 
# Read in each input, perform preprocessing and get labels
for img_path, class_num in batch:

# Read raw img data as np array
# Returns as shape (600, 300, 1)
img_arr = create_np_img_array(img_path)
# Normalize img data (/255)
img_arr = normalize_img_array(img_arr)

# Add to the batch x data list
batch_x.append(img_arr)

# Add to the batch y classification list
batch_y.append(class_num)

yield (batch_x, batch_y)

创建生成器实例:

img_gen = image_generator(training_data, 30)

这样设置我的模型:

# create the model
model = Sequential()
# input layer has the input_shape param which is the dimentions of the np array
model.add( Conv2D(256, (3, 3), activation='relu', input_shape = (600, 300, 1)) ) 
model.add( MaxPooling2D( (2,2)) )
# second hidden layer
model.add( MaxPooling2D((2, 2)) )
model.add( Conv2D(256, (3, 3), activation='relu') )
# third hidden layer
model.add( MaxPooling2D((2, 2)))
model.add( Conv2D(256, (3, 3), activation='relu') )
# forth hidden layer
model.add( Flatten() )
model.add( Dense(64, activation='relu') )
# ouput layer
model.add( Dense(2) )
model.summary()
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# pass generator
model.fit(img_gen, epochs=5)

然后model.fit((尝试在int.上调用shape失败

~anaconda3libsite-packagestensorflowpythonkerasenginedata_adapter.py in _get_dynamic_shape(t)
797 
798     def _get_dynamic_shape(t):
--> 799       shape = t.shape
800       # Unknown number of dimensions, `as_list` cannot be called.
801       if shape.rank is None:
AttributeError: 'int' object has no attribute 'shape'

对我做错了什么有什么建议吗??

将生成器的输出转换为numpy数组似乎已经停止了错误。

np_x = np.array(batch_x)
np_y = np.array(batch_y)

似乎它不喜欢将这些分类作为int的std列表。

最新更新