我正在尝试使用我自己的数据集运行 Tensorflow 教程 https://www.tensorflow.org/tutorials/generative/dcgan 中给出的 DCGAN 代码。要将数据批量加载到内存中,我使用以下代码:
def get_image(image_path):
image = Image.open(image_path)
return np.array(np.asarray(image))/255.0
def get_batch(image_files):
data_batch = np.array(
[get_image("DEBUG/real/"+sample_file) for sample_file in image_files]).astype(np.float32)
if len(data_batch.shape) < 4:
data_batch = data_batch.reshape(data_batch.shape + (1,))
return data_batch
def get_batches(batch_size):
current_index = 0
while current_index + batch_size <= shape[0]:
data_batch = get_batch(data_files[current_index:current_index + batch_size])
current_index += batch_size
yield data_batch / 255.0
与教程的唯一区别是我的输入尺寸为 640 x 360 x 3。 我在运行时收到以下警告和错误:
WARNING:tensorflow:Model was constructed with shape Tensor("conv2d_11_input:0", shape=(None, 640, 360, 3), dtype=float32) for input (None, 640, 360, 3), but it was re-called on a Tensor with incompatible shape (0, 1).
ValueError: Shapes (0, 1) and (None, None, None, None) must have the same rank
<ipython-input-92-b17a80c6bb7b> in train_step(images)
6 generated_images = generator(noise, training=True)
7
----> 8 real_output = discriminator(images, training=True)
9 fake_output = discriminator(generated_images, training=True)
10
~\miniconda3\envs\newenv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
820 with base_layer_utils.autocast_context_manager(
821 self._compute_dtype):
--> 822 outputs = self.call(cast_inputs, *args, **kwargs)
823 self._handle_activity_regularization(inputs, outputs)
824 self._set_mask_metadata(inputs, outputs, input_masks)
~\miniconda3\envs\newenv\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py in call(self, inputs, training, mask)
265 if not self.built:
266 self._init_graph_network(self.inputs, self.outputs, name=self.name)
--> 267 return super(Sequential, self).call(inputs, training=training, mask=mask)
268
269 outputs = inputs # handle the corner case where self.layers is empty
~\miniconda3\envs\newenv\lib\site-packages\tensorflow_core\python\keras\engine\network.py in call(self, inputs, training, mask)
715 return self._run_internal_graph(
716 inputs, training=training, mask=mask,
--> 717 convert_kwargs_to_constants=base_layer_utils.call_context().saving)
718
719 def compute_output_shape(self, input_shape):
~\miniconda3\envs\newenv\lib\site-packages\tensorflow_core\python\keras\engine\network.py in _run_internal_graph(self, inputs, training, mask, convert_kwargs_to_constants)
889
890 # Compute outputs.
--> 891 output_tensors = layer(computed_tensors, **kwargs)
892
893 # Update tensor_dict.
~\miniconda3\envs\newenv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
820 with base_layer_utils.autocast_context_manager(
821 self._compute_dtype):
--> 822 outputs = self.call(cast_inputs, *args, **kwargs)
823 self._handle_activity_regularization(inputs, outputs)
824 self._set_mask_metadata(inputs, outputs, input_masks)
~\miniconda3\envs\newenv\lib\site-packages\tensorflow_core\python\keras\layers\convolutional.py in call(self, inputs)
201 strides=self.strides,
202 padding=self._padding_op,
--> 203 data_format=self._conv_op_data_format)
204
205 # Apply causal padding to inputs for Conv1D.
~\miniconda3\envs\newenv\lib\site-packages\tensorflow_core\python\ops\nn_ops.py in __init__(self, input_shape, filter_shape, padding, strides, dilation_rate, name, data_format)
1066 except ValueError:
1067 raise ValueError(
-> 1068 "input tensor must have rank %d" % (num_spatial_dims + 2))
1069
1070 try:
ValueError: input tensor must have rank 4
我没有得到的是形状为 (0,1) 的张量来自哪里?请帮我。我的非常重要的项目需要它,使用 GAN 进行面部活体检测。 谢谢。