无法为 TFlearn 输入正确形状的图像
Can't input images in the right shape for TFlearn
我的目标文件夹设置正确,我相信如下
Cell_Images ---> Cell_1 --> imgD1.png, imgD2.png...etc
Cell_2 --> imgT1.png, imgT2.png...etc
这是我正在使用的代码
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_utils import image_preloader
dataset_file = 'C:/Users/Lenovo/dir2/CNN/Cell_Images'
X, Y = image_preloader(dataset_file, image_shape=(128, 128), mode='folder', grayscale= True, categorical_labels=True, normalize=True)
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
convnet = input_data(shape=[None,128,128,1], data_preprocessing=img_prep, name='input')
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet,2,activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet)
model.fit(X,Y, n_epoch=10, snapshot_step=500, show_metric=True, run_id='cells')
model.save('cellcnn.model')
但我收到以下与我的输入形状有关的错误,我不知道如何正确地重塑它。有任何想法吗?谢谢。
curses is not supported on this machine (please install/reinstall curses for an optimal experience)
---------------------------------
Run id: cells
Log directory: /tmp/tflearn_logs/
---------------------------------
Preprocessing... Calculating mean over all dataset (this may take long)...
Mean: 0.270792861708 (To avoid repetitive computation, add it to argument 'mean' of `add_featurewise_zero_center`)
---------------------------------
Preprocessing... Calculating std over all dataset (this may take long)...
STD: 0.283742975044 (To avoid repetitive computation, add it to argument 'std' of `add_featurewise_stdnorm`)
INFO:tensorflow:Summary name Accuracy/ (raw) is illegal; using Accuracy/__raw_ instead.
---------------------------------
Training samples: 156
Validation samples: 0
--
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-2db08f4fee1c> in <module>()
31 model = tflearn.DNN(convnet)
32
---> 33 model.fit(X,Y, n_epoch=10, snapshot_step=500, show_metric=True, run_id='cells')
34
35 model.save('cellcnn.model')
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\models\dnn.py in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, excl_trainops, validation_batch_size, run_id, callbacks)
213 excl_trainops=excl_trainops,
214 run_id=run_id,
--> 215 callbacks=callbacks)
216
217 def predict(self, X):
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\helpers\trainer.py in fit(self, feed_dicts, n_epoch, val_feed_dicts, show_metric, snapshot_step, snapshot_epoch, shuffle_all, dprep_dict, daug_dict, excl_trainops, run_id, callbacks)
331 (bool(self.best_checkpoint_path) | snapshot_epoch),
332 snapshot_step,
--> 333 show_metric)
334
335 # Update training state
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\helpers\trainer.py in _train(self, training_step, snapshot_epoch, snapshot_step, show_metric)
772 tflearn.is_training(True, session=self.session)
773 _, train_summ_str = self.session.run([self.train, self.summ_op],
--> 774 feed_batch)
775
776 # Retrieve loss value from summary string
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
765 try:
766 result = self._run(None, fetches, feed_dict, options_ptr,
--> 767 run_metadata_ptr)
768 if run_metadata:
769 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
942 'Cannot feed value of shape %r for Tensor %r, '
943 'which has shape %r'
--> 944 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
945 if not self.graph.is_feedable(subfeed_t):
946 raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (64, 128, 128) for Tensor 'input/X:0', which has shape '(?, 128, 128, 1)'
编辑:
尝试使用 Numpy 重新渲染如下:
X = np.reshape(X, (64,128,128,1))
但出现错误:
ValueError: cannot reshape array of size 2555904 into shape (64,128,128,1)
还尝试使用 tf.reshape 重塑如下:
X = tf.reshape(X, (64,128,128,1))
但出现错误:
ValueError: Argument must be a dense tensor: <tflearn.data_utils.ImagePreloader object at 0x00000176F774FEB8> - got shape [156, 128, 128], but wanted [].
似乎图像预加载器在做一些奇怪的事情,这是因为图像是动态加载而不是存储的吗?或者预加载器只是没有返回正确类型的对象以进行进一步重塑?
您的预加载函数是 return 返回一批形状为 64 x 128 x 128 的图像。它不会影响通道尺寸,因为它是 1(灰度)。但 tensorflow 期待它。
我没用过 tf.learn 的东西,而且他们不会 return 64x128x128x1 的形状确实让人恼火。
快速查看文档并没有告诉我 image_preloader 的 return 类型是什么。我假设 X
是一个 numpy 数组,在这种情况下,您只需按如下方式重塑它:
X_reshaped = np.reshape(X, (64,128,128,1))
可能有一种稍微更优雅的方式通过 tflearn
添加最后一个维度,我不知道,但你的问题只是最后一个通道维度需要是微不足道的,但形式上是 1。
您也可以使用带有 tf.reshape(X, [64,128,128,1])
的 tensorflow 对其进行整形,只需确保您的占位符接受形状 (batch, height, width)
.
我通过进行以下重塑使其工作:
X = np.reshape(X, (-1, 128, 128,1))
你似乎不能给它一个特定的批量大小,比如 64,所以需要 -1。
我的目标文件夹设置正确,我相信如下
Cell_Images ---> Cell_1 --> imgD1.png, imgD2.png...etc
Cell_2 --> imgT1.png, imgT2.png...etc
这是我正在使用的代码
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_utils import image_preloader
dataset_file = 'C:/Users/Lenovo/dir2/CNN/Cell_Images'
X, Y = image_preloader(dataset_file, image_shape=(128, 128), mode='folder', grayscale= True, categorical_labels=True, normalize=True)
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
convnet = input_data(shape=[None,128,128,1], data_preprocessing=img_prep, name='input')
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet,2,activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet)
model.fit(X,Y, n_epoch=10, snapshot_step=500, show_metric=True, run_id='cells')
model.save('cellcnn.model')
但我收到以下与我的输入形状有关的错误,我不知道如何正确地重塑它。有任何想法吗?谢谢。
curses is not supported on this machine (please install/reinstall curses for an optimal experience)
---------------------------------
Run id: cells
Log directory: /tmp/tflearn_logs/
---------------------------------
Preprocessing... Calculating mean over all dataset (this may take long)...
Mean: 0.270792861708 (To avoid repetitive computation, add it to argument 'mean' of `add_featurewise_zero_center`)
---------------------------------
Preprocessing... Calculating std over all dataset (this may take long)...
STD: 0.283742975044 (To avoid repetitive computation, add it to argument 'std' of `add_featurewise_stdnorm`)
INFO:tensorflow:Summary name Accuracy/ (raw) is illegal; using Accuracy/__raw_ instead.
---------------------------------
Training samples: 156
Validation samples: 0
--
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-2db08f4fee1c> in <module>()
31 model = tflearn.DNN(convnet)
32
---> 33 model.fit(X,Y, n_epoch=10, snapshot_step=500, show_metric=True, run_id='cells')
34
35 model.save('cellcnn.model')
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\models\dnn.py in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, excl_trainops, validation_batch_size, run_id, callbacks)
213 excl_trainops=excl_trainops,
214 run_id=run_id,
--> 215 callbacks=callbacks)
216
217 def predict(self, X):
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\helpers\trainer.py in fit(self, feed_dicts, n_epoch, val_feed_dicts, show_metric, snapshot_step, snapshot_epoch, shuffle_all, dprep_dict, daug_dict, excl_trainops, run_id, callbacks)
331 (bool(self.best_checkpoint_path) | snapshot_epoch),
332 snapshot_step,
--> 333 show_metric)
334
335 # Update training state
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tflearn\helpers\trainer.py in _train(self, training_step, snapshot_epoch, snapshot_step, show_metric)
772 tflearn.is_training(True, session=self.session)
773 _, train_summ_str = self.session.run([self.train, self.summ_op],
--> 774 feed_batch)
775
776 # Retrieve loss value from summary string
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
765 try:
766 result = self._run(None, fetches, feed_dict, options_ptr,
--> 767 run_metadata_ptr)
768 if run_metadata:
769 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
C:\Users\Lenovo\Anaconda3\envs\tensorflow3_5\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
942 'Cannot feed value of shape %r for Tensor %r, '
943 'which has shape %r'
--> 944 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
945 if not self.graph.is_feedable(subfeed_t):
946 raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (64, 128, 128) for Tensor 'input/X:0', which has shape '(?, 128, 128, 1)'
编辑:
尝试使用 Numpy 重新渲染如下:
X = np.reshape(X, (64,128,128,1))
但出现错误:
ValueError: cannot reshape array of size 2555904 into shape (64,128,128,1)
还尝试使用 tf.reshape 重塑如下:
X = tf.reshape(X, (64,128,128,1))
但出现错误:
ValueError: Argument must be a dense tensor: <tflearn.data_utils.ImagePreloader object at 0x00000176F774FEB8> - got shape [156, 128, 128], but wanted [].
似乎图像预加载器在做一些奇怪的事情,这是因为图像是动态加载而不是存储的吗?或者预加载器只是没有返回正确类型的对象以进行进一步重塑?
您的预加载函数是 return 返回一批形状为 64 x 128 x 128 的图像。它不会影响通道尺寸,因为它是 1(灰度)。但 tensorflow 期待它。
我没用过 tf.learn 的东西,而且他们不会 return 64x128x128x1 的形状确实让人恼火。
快速查看文档并没有告诉我 image_preloader 的 return 类型是什么。我假设 X
是一个 numpy 数组,在这种情况下,您只需按如下方式重塑它:
X_reshaped = np.reshape(X, (64,128,128,1))
可能有一种稍微更优雅的方式通过 tflearn
添加最后一个维度,我不知道,但你的问题只是最后一个通道维度需要是微不足道的,但形式上是 1。
您也可以使用带有 tf.reshape(X, [64,128,128,1])
的 tensorflow 对其进行整形,只需确保您的占位符接受形状 (batch, height, width)
.
我通过进行以下重塑使其工作:
X = np.reshape(X, (-1, 128, 128,1))
你似乎不能给它一个特定的批量大小,比如 64,所以需要 -1。