我的目标文件夹设置正确我相信如下
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 [].
似乎图像预加载器正在做一些奇怪的事情,是因为图像是在运行中加载而不是存储的?或者预加载器是不是为了进一步重塑而没有返回正确类型的对象?
答案 0 :(得分:1)
我通过以下重塑来实现它:
X = np.reshape(X, (-1, 128, 128,1))
似乎你不能给它一个特定的批量大小,如64,所以需要-1。
答案 1 :(得分:0)
你的预加载功能正在返回一批形状为64 x 128 x 128的图像。由于它是1(灰度),所以它不会受到通道尺寸的影响。但是tensorflow期待它。
我还没有使用过tf.learn的东西,看起来很烦人的是他们不会将它恢复到64x128x128x1的状态。
快速查看文档并没有告诉我image_preloader的返回类型是什么。我假设X
是一个numpy数组,在这种情况下你只需重新整形就可以了:
X_reshaped = np.reshape(X, (64,128,128,1))
通过tflearn
添加最后一个维度的方法可能会稍微优雅一些,我不知道,但问题只是最后一个渠道维度需要琐碎,但正式1。
你也可以使用tf.reshape(X, [64,128,128,1])
的tensorflow重塑它,只需确保你的占位符接受形状(batch, height, width)
。