python - 除非所有指定的输入大小都不为零,否则 Reshape 无法推断空张量的缺失输入大小
问题描述
我看了一些帖子,因为这似乎是一个非常常见的问题,但没有一个对我有用。我在一台装有 Tensorflow 1.11 的 Windows 机器上工作,但是当我尝试在一台装有 tensorflow 1.10 并更新到 1.13 的 Unbuntu 机器上时,它给了我这个错误
InvalidArgumentError (see above for traceback): Reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero
[[node Flatten_1/flatten/Reshape (defined at /home/matt/GitClone/PythonGitRepo/DNN/SelfDrivingCar/TrafficSignClassification/LeNETExample/Model/LeNet.py:69) ]]
我对问题所在有点迷茫,因为这应该是从 Windows 到 linux 的简单端口。
任何建议,将不胜感激。
这是程序的主要代码:
import pickle
import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
import tensorflow as tf
from DataScripts import PreprocessData as predata
from Model import LeNet
# TODO: Fill this in based on where you saved the training and testing data
# Data set locations and paths
trainingPath = "TrainingData/train.p"
testingPath = "TrainingData/test.p"
# Get the data from the data sets using pickle to read them in
with open(trainingPath, mode="rb") as training_data:
train = pickle.load(training_data)
with open(testingPath, mode="rb") as testing_data:
test = pickle.load(testing_data)
# Create the features and labels for the data and link them
x_train, y_train = train['features'], train['labels']
x_test, y_test = test['features'], test['labels']
print("x_train shape:", x_train.shape)
print("y_train shape:", y_train.shape)
print("x_test shape:", x_test.shape)
print("y_test shape:", y_test.shape)
# Define number of training samples and number of testing samples
num_train = len(x_train)
num_test = len(x_test)
# Define the number of classes (only the unique vales in y sets)
num_classes = len(np.unique(y_train))
# Define the image shape and data shape
image_shape = x_train[0].shape
print("Num of Training Samples:{} - Num of Test Sample:{} - Num Of classes:{}".format(num_train, num_test, num_classes))
# histogram of label frequency Simple data exploration
hist, bins = np.histogram(y_train, bins=num_classes)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()
# Process all the data and create validation data
x_train, x_test_normalized, x_validation, y_validation = predata.preprocessData(x_train, y_train, x_test)
# Build LeNET Model
EPOCHS = 60
BATCH_SIZE = 100
rate = 0.0009 # Learning rate
tf.reset_default_graph()
# Tensor placehoders
x = tf.placeholder(tf.float32, (None, 32, 32, 1)) # Define shape of a image 32X32
y = tf.placeholder(tf.int32, (None)) # Class label
keep_prob = tf.placeholder(tf.float32) # probability to keep units
one_hot_y = tf.one_hot(y, 43)
# Start the training
logits = LeNet.makeModel(x, keep_prob)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels=one_hot_y)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
num_examples = len(x_train)
print("Training...")
print()
for i in range(EPOCHS):
x_train, y_train = shuffle(x_validation, y_validation)
for offset in range(0, num_examples, BATCH_SIZE):
end = offset + BATCH_SIZE
batch_x, batch_y = x_train[offset:end], y_train[offset:end]
sess.run(training_operation, feed_dict={x: batch_x, y: batch_y, keep_prob: 0.5})
validation_accuracy = LeNet.evaluate(x_validation, y_validation, BATCH_SIZE, accuracy_operation, keep_prob, x, y)
print("EPOCH {} ...".format(i + 1))
print("Validation Accuracy = {:.3f}".format(validation_accuracy))
print()
saver.save(sess, '.\lenet')
print("Model saved")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver2 = tf.train.import_meta_graph('./lenet.meta')
saver2.restore(sess, "./lenet")
# Test accuracy on test sets
test_accuracy = LeNet.evaluate(x_test_normalized, y_test, BATCH_SIZE, accuracy_operation, keep_prob, x, y)
print("Test Set Accuracy = {:.3f}".format(test_accuracy))
这是 LeNEt 方法:
import tensorflow as tf
#from tensorflow.contrib.layers import flatten
def makeModel(x, keep_prob):
mu = 0
sigma = 0.1
# Layer 1: Convolutional. Input = 32x32x1. Output = 28x28x6.
W1 = tf.Variable(tf.truncated_normal(shape=(5, 5, 1, 6), mean=mu, stddev=sigma), name="W1")
x = tf.nn.conv2d(x, W1, strides=[1, 1, 1, 1], padding='VALID')
b1 = tf.Variable(tf.zeros(6), name="b1")
x = tf.nn.bias_add(x, b1)
print("layer 1 shape:", x.get_shape())
# Activation function.
x = tf.nn.relu(x)
# Max Pooling. Input = 28x28x6. Output = 14x14x6.
x = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
layer1 = x
# Layer 2: Convolutional. Output = 10x10x16.
W2 = tf.Variable(tf.truncated_normal(shape=(5, 5, 6, 16), mean=mu, stddev=sigma), name="W2")
x = tf.nn.conv2d(x, W2, strides=[1, 1, 1, 1], padding='VALID')
b2 = tf.Variable(tf.zeros(16), name="b2")
x = tf.nn.bias_add(x, b2)
# Activation function.
x = tf.nn.relu(x)
# Max Pooling. Input = 10x10x16. Output = 5x5x16.
x = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
layer2 = x
# Layer 3: Convolutional. Output = 1x1x400.
W3 = tf.Variable(tf.truncated_normal(shape=(5, 5, 16, 400), mean=mu, stddev=sigma), name="W3")
x = tf.nn.conv2d(x, W3, strides=[1, 1, 1, 1], padding='VALID')
b3 = tf.Variable(tf.zeros(400), name="b3")
x = tf.nn.bias_add(x, b3)
# Activation function.
x = tf.nn.relu(x)
layer3 = x
# Flatten. Input = 5x5x16. Output = 400.
layer2flat = tf.layers.flatten(layer2)
print("layer2flat shape:", layer2flat.get_shape())
# Flatten x. Input = 1x1x400. Output = 400.
xflat = tf.layers.flatten(x)
print("xflat shape:", xflat.get_shape())
# Concat layer2flat and x. Input = 400 + 400. Output = 800
x = tf.concat([xflat, layer2flat], 1)
print("x shape:", x.get_shape())
# Dropout
x = tf.nn.dropout(x, keep_prob)
# Layer 4: Fully Connected. Input = 800. Output = 43.
W4 = tf.Variable(tf.truncated_normal(shape=(800, 43), mean=mu, stddev=sigma), name="W4")
b4 = tf.Variable(tf.zeros(43), name="b4")
logits = tf.add(tf.matmul(x, W4), b4)
return logits
# x and y are tensorflow place holders
def evaluate(x_validation, y_validation, BATCH_SIZE, accuracy_operation, keep_prob, x, y):
num_examples = len(x_validation)
total_accuracy = 0
sess = tf.get_default_session()
for offset in range(0, num_examples, BATCH_SIZE):
batch_x, batch_y = x_validation[offset:offset+BATCH_SIZE], y_validation[offset:offset+BATCH_SIZE]
accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y, keep_prob: 1.0})
total_accuracy += (accuracy * len(batch_x))
return total_accuracy / num_examples
这是数据的预处理代码
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import numpy as np
def preprocessData(x_train, y_train, x_test):
# Convert to grayscale
x_train_rgb = x_train
x_train_gry = np.sum(x_train / 3, axis=3, keepdims=True)
x_test_rgb = x_test
x_test_gry = np.sum(x_test / 3, axis=3, keepdims=True)
print('RGB:', x_train_rgb.shape)
print('Grayscale:', x_train_gry.shape)
x_train = x_train_gry
x_test = x_test_gry
## Normalize the train and test datasets to (-1,1)
x_train_normalized = (x_train - 128) / 128
x_test_normalized = (x_test - 128) / 128
## Shuffle the training dataset and create the validation set
# Validatioin set will not be used by Training
x_train_normalized, y_train = shuffle(x_train_normalized, y_train)
x_train, x_validation, y_train, y_validation = train_test_split(x_train_normalized, y_train,
test_size=0.20, random_state=42)
print("Old X_train size:", len(x_train_normalized))
print("New X_train size:", len(x_train))
print("X_validation size:", len(x_validation))
return x_train, x_test_normalized, x_validation, y_validation
解决方案
推荐阅读
- uuid - 如何在 Spock + PowerMock 中模拟 UUID?
- javascript - JSON 的强类型类
- angular - Laravel 响应不包含 Set-Cookie 标头
- python-3.x - 计算字典中每个键的每个值的频率,并将计数信息放入数据框中
- json - 如何在给定日期之间选择 json 对象
- javascript - 如何集成 HTML、Python (Pandas) 和 JavaScript?
- azure - 如何使用 Azure API 检索“日志搜索”警报规则
- javascript - 有人可以帮我修复这个正则表达式吗
- algorithm - 查找值是否包含在二叉树中
- python - 在 Node.js 服务器上运行 Python 脚本时出错