我對 Tensorflow 中的資料集構造感到困惑,無法讓我的自動編碼器適合我的資料。我不斷收到錯誤,希望有人能看看這個,看看我哪里出錯了。我嘗試只擬合資料而不是批處理迭代器并得到同樣的錯誤。我什至嘗試將我自己的資料集構建為一個 numpy 陣列,但我不完全理解它在尋找什么。所以這就是我目前所處的位置:
import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.image import imread
import matplotlib.image as mpimg
import cv2
# Technically not necessary in newest versions of jupyter
%matplotlib inline
from google.colab import drive
drive.mount('/content/gdrive')
my_data_dir = '/content/gdrive/MyDrive/Skyrmion Vision/testFiles/train/'
images = os.listdir(my_data_dir)
data = tf.keras.utils.image_dataset_from_directory('/content/gdrive/MyDrive/Skyrmion Vision/testFiles/train/',batch_size=1,image_size=(171,256))
data_iterator = data.as_numpy_iterator()
batch = data_iterator.next()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten,Reshape
from tensorflow.keras.optimizers import SGD
encoder = Sequential()
encoder.add(Flatten(input_shape=[171,256]))
encoder.add(Dense(400,activation='relu'))
encoder.add(Dense(200,activation='relu'))
encoder.add(Dense(100,activation='relu'))
encoder.add(Dense(50,activation='relu'))
encoder.add(Dense(25,activation='relu'))
decoder = Sequential()
decoder.add(Dense(50,input_shape=[25],activation='relu'))
decoder.add(Dense(100,activation='relu'))
decoder.add(Dense(200,activation='relu'))
decoder.add(Dense(400,activation='relu'))
decoder.add(Dense(171*256,activation='sigmoid'))
decoder.add(Reshape([171,256]))
autoencoder = Sequential([encoder,decoder])
autoencoder.compile(loss='binary_crossentropy',optimizer=SGD(lr=1.5),metrics=['accuracy'])
autoencoder.fit(batch,batch,epochs=5)
這給了我一個錯誤,我不清楚需要修復什么。顯然有形狀錯誤?
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-67-aa659ef4ed20> in <module>
----> 1 autoencoder.fit(batch,batch,epochs=5)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 200, in assert_input_compatibility
raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
ValueError: Layer "sequential_2" expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 171, 256, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=int32>]```
uj5u.com熱心網友回復:
你可以試試:
...
autoencoder.fit(batch[0],batch[0], epochs=5)
因為您的一批資料是影像和標簽的元組,而您實際上只是對影像感興趣。否則,只需過濾掉標簽并將您的資料集提供給您的模型:
data = data.map(lambda x, y: (x, x))
autoencoder.fit(data, epochs=5)
哦,你的最后兩層應該是:
decoder.add(Dense(171*256 * 3,activation='sigmoid'))
decoder.add(Reshape([171,256, 3]))
因為您正在使用 3 通道影像。您的編碼器還需要 input_shape:
encoder.add(Flatten(input_shape=[171,256, 3]))
另請參閱 SO 執行緒是否可以在 Keras 中將 image_dataset_from_directory() 與卷積自動編碼器一起使用?了解更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/508266.html