所有tensorflow教程都做得非常好,但是他們都使用預先處理的可下載數據集,這些數據集可以直接使用。他們在MNIST上的教程就是一個很好的例子。
對於一個學校項目,其他4個人和我被分配到以PNG圖像的形式對所提供的數據進行CNN培訓。這只是一個包含150張圖片的目錄。標籤包含在圖像文件名稱中。TensorFlow在自定義圖像上培訓CNN
代碼的座標現在我們得到一個錯誤,我將在下面包括。
我們跟着MNIST代碼在這裏找到:https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/examples/tutorials/layers/cnn_mnist.py
所以我們相當肯定我們的問題是我們如何處理的圖像數據。 我們一直試圖讓這個工作大約3天。 (我們已經解決了很多錯誤,這只是最新的)。
任何幫助或反饋將不勝感激! 此外,如果有人對此有疑問,請評論。
import os
import tensorflow as tf
import numpy as np
#from PIL import Image
# a function
def cnn_model_fn(features,labels,mode):
"""Model function for CNN."""
# Input Layer
input_layer = tf.reshape(features['x'], [-1, 128, 128, 3])
# Convolutional Layer #1
conv_1 = tf.layers.conv2d(
inputs=input_layer,
filters=64,
kernel_size=[7, 7],
strides=2,
padding="same",
activation=tf.nn.relu)
conv_2 = tf.layers.conv2d(
inputs=conv_1,
filters=128,
kernel_size=[5, 5],
padding="same",
strides = 2,
activation=tf.nn.relu)
max_pool_1 = tf.layers.max_pooling2d(
inputs = conv_2,
pool_size = 3,
strides = 1
)
conv_3 = tf.layers.conv2d(
inputs=max_pool_1,
filters=96,
kernel_size=[3, 3],
activation=tf.nn.relu
)
max_pool_2 = tf.layers.max_pooling2d(
inputs = conv_3,
pool_size = 2,
strides = 1
)
dropout_1 = tf.layers.dropout(
inputs = max_pool_2,
rate=0.5
)
fully_connected_1 = tf.contrib.layers.fully_connected(
inputs = dropout_1,
num_outputs = 1024,
)
dropout_2 = tf.layers.dropout(
inputs = fully_connected_1,
rate=0.5
)
fully_connected_2 = tf.contrib.layers.fully_connected(
inputs = dropout_2,
num_outputs = 1024,
)
fully_connected_3 = tf.contrib.layers.fully_connected(
inputs = fully_connected_2,
num_outputs = 15,
)
softmax_layer = tf.contrib.layers.softmax(
logits = fully_connected_3
)
#------------------------------------------------------------------------MAIN--------------------------------------------------------------------------------------------------
def getLabels():
imagelabels_arr = []
image_files = os.listdir("../assets/CNN-Data/")
for image in image_files:
imagelabels_arr.append(image.split('.')[len(image.split('.'))-2])
return imagelabels_arr
def getTrainImages():
filenames = []
image_files = os.listdir("../assets/CNN-Data/")
for image in image_files:
filenames.append(image)
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader()
filename, content = reader.read(filename_queue)
image = tf.image.decode_png(content, channels=3)
images = np.asarray(image)
image = tf.cast(image, tf.float64)
resize_image = tf.image.resize_images(image, (128, 128))
# image_batch = tf.train.batch([resize_image], batch_size=10)
print(resize_image)
return resize_image
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn, model_dir="./test")
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={'x':np.array(getTrainImages())},
y=np.array(getLabels()),
batch_size=10,
num_epochs=None,
shuffle=True)
classifier.train(
input_fn=train_input_fn,
steps=20,
)
的錯誤:
Traceback (most recent call last):
File "CNN.py", line 134, in <module>
steps=20,
File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 241, in train
loss = self._train_model(input_fn=input_fn, hooks=hooks)
File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 628, in _train_model
input_fn, model_fn_lib.ModeKeys.TRAIN)
File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 499, in _get_features_and_labels_from_input_fn
result = self._call_input_fn(input_fn, mode)
File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 585, in _call_input_fn
return input_fn(**kwargs)
File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\inputs\numpy_io.py", line 109, in input_fn
if len(set(v.shape[0] for v in ordered_dict_x.values())) != 1:
File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\inputs\numpy_io.py", line 109, in <genexpr>
if len(set(v.shape[0] for v in ordered_dict_x.values())) != 1:
IndexError: tuple index out of range
我不知道太多關於'Estimator' API,但不應'cnn_model_fn'返回的東西? – jdehesa
是的,你是對的。如果我們添加MNIST代碼使用的return語句,我們會得到相同的錯誤。 –
'print(resize_image)'顯示的語句是什麼? (或者它沒有運行到那個點?)另外,注意:1)它不影響程序,但'images = np.asarray(image)'沒有意義...'image'是一個TF張量,而不是NumPy 2)也許你想要的是'image = tf.cast(image/255.0,tf.float64)'? (在[0,1]中有像素值) – jdehesa