简体   繁体   English

如何修复处理CIFAR-10数据集的维度(值)错误,构建CNN?

[英]How to fix dimension (value) error dealing with CIFAR-10 dataset, building CNN?

Working on cifar-10 dataset to build CNN and evaluate loss and accuracy. 使用cifar-10数据集构建CNN并评估损失和准确性。 What I am trying to do is split the dataset into training and test data using keras and the train the model. 我想要做的是使用keras将数据集拆分为训练和测试数据,然后训练模型。 But on the very last step, it is giving me dimension error and nothing I could do to fix it. 但是在最后一步,它给了我尺寸错误,我无法解决它。 Please help! 请帮忙!

Here is the code: 这是代码:

import numpy as np
import pickle
import tensorflow as tf
import os
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import sklearn

path ='cifar-10-batches-py'

def load_cfar10_batch(path):
    with open(path + '/data_batch_1', mode='rb') as file:
        batch = pickle.load(file, encoding='latin1')   
    features = batch['data']
    labels = batch['labels']
    return features, labels


x = features.reshape((len(features), 3, 32, 32)).transpose(0, 2, 3, 1)
x.shape

y = labels
def one_hot_encode(y):
    encoded = np.zeros((len(y), 10))

    for index, val in enumerate(y):
        encoded[index][val] = 1

    return encoded


def normalize(x):
    x_norm = x/255
    return x_norm

from sklearn import preprocessing
scaler = preprocessing.StandardScaler()
scaled_df = scaler.fit_transform(features)
scaled_df = scaled_df.reshape(10000,3,32,32).transpose(0,2,3,1)
plt.imshow(scaled_df[9999])

def _preprocess_and_save(normalize_and_standardize, one_hot_encode, features, labels, filename):
    features = normalize(x)    
    labels = one_hot_encode(y) 

    pickle.dump((features, labels), open(filename, 'wb')) 


features, labels = load_cfar10_batch(path)


from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.2)


def preprocess_and_save_data(path, normalize, one_hot_encode):
    #check where the code for _preprocess_and_save is
    _preprocess_and_save(normalize, one_hot_encode, np.array(x_test), np.array(y_test), 'preprocess_test.p')
    _preprocess_and_save(normalize, one_hot_encode, np.array(x_train), np.array(y_train), 'preprocess_training.p')


preprocess_and_save_data(path, normalize, one_hot_encode)

x_test, y_test = pickle.load(open('preprocess_test.p', mode='rb'))
y_train, y_train = pickle.load(open('preprocess_training.p', mode='rb'))

def tf_reset():
    try:
        sess.close()
    except:
        pass
    tf.reset_default_graph()
    return tf.Session()

sess = tf_reset()

x = tf.placeholder(tf.float32, shape=(None, 32, 32, 3), name='input_x')
y =  tf.placeholder(tf.float32, shape=(None, 10), name='output_y')
keep_prob = tf.placeholder(tf.float32, name='keep_prob')

def conv_net(x, keep_prob):
    #x = tf.reshape(x,[-1,32,32,3])
    conv1_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 64], mean=0, stddev=0.08))
    conv2_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 128], mean=0, stddev=0.08))
    conv3_filter = tf.Variable(tf.truncated_normal(shape=[5, 5, 128, 256], mean=0, stddev=0.08))
    conv4_filter = tf.Variable(tf.truncated_normal(shape=[5, 5, 256, 512], mean=0, stddev=0.08))


 #Layer1
    conv1 = tf.nn.conv2d(x, conv1_filter, strides=[1,1,1,1], padding='SAME')
    conv1 = tf.nn.relu(conv1)
    conv1_pool = tf.nn.max_pool(conv1, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
    conv1_bn = tf.layers.batch_normalization(conv1_pool)

 #Layer2
    conv2 = tf.nn.conv2d(conv1_bn, conv2_filter, strides=[1,1,1,1], padding='SAME')
    conv2 = tf.nn.relu(conv2)
    conv2_pool = tf.nn.max_pool(conv2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')    
    conv2_bn = tf.layers.batch_normalization(conv2_pool)

 #Layer 3
    conv3 = tf.nn.conv2d(conv2_bn, conv3_filter, strides=[1,1,1,1], padding='SAME')
    conv3 = tf.nn.relu(conv3)
    conv3_pool = tf.nn.max_pool(conv3, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')  
    conv3_bn = tf.layers.batch_normalization(conv3_pool)

 #Layer 4
    conv4 = tf.nn.conv2d(conv3_bn, conv4_filter, strides=[1,1,1,1], padding='SAME')
    conv4 = tf.nn.relu(conv4)
    conv4_pool = tf.nn.max_pool(conv4, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
    conv4_bn = tf.layers.batch_normalization(conv4_pool)


    flat = tf.contrib.layers.flatten(conv4_bn)  


    full1 = tf.contrib.layers.fully_connected(inputs=flat, num_outputs=128, activation_fn=tf.nn.relu)
    full1 = tf.nn.dropout(full1, keep_prob)
    full1 = tf.layers.batch_normalization(full1)


    full2 = tf.contrib.layers.fully_connected(inputs=full1, num_outputs=256, activation_fn=tf.nn.relu)
    full2 = tf.nn.dropout(full2, keep_prob)
    full2 = tf.layers.batch_normalization(full2)


    full3 = tf.contrib.layers.fully_connected(inputs=full2, num_outputs=512, activation_fn=tf.nn.relu)
    full3 = tf.nn.dropout(full3, keep_prob)
    full3 = tf.layers.batch_normalization(full3)    


    full4 = tf.contrib.layers.fully_connected(inputs=full3, num_outputs=1024, activation_fn=tf.nn.relu)
    full4 = tf.nn.dropout(full4, keep_prob)
    full4 = tf.layers.batch_normalization(full4)        


    out = tf.contrib.layers.fully_connected(inputs=full3, num_outputs=10, activation_fn=None)

    return out


iterations = 101
batch_size = 128
keep_probability = 0.7
learning_rate = 0.001

logits = conv_net(x, keep_prob)

# Loss and Optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Accuracy
correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')

def train_neural_network(session, optimizer, keep_probability, feature_batch, label_batch):
    session.run(optimizer, 
                feed_dict={
                    x: feature_batch,
                    y: label_batch,
                    keep_prob: keep_probability
                })

def print_stats(sess, feature_batch, label_batch, cost, accuracy):
    loss = sess.run(cost, 
                    feed_dict={
                        x: feature_batch,
                        y: label_batch,
                        keep_prob: 1.
                    })
    valid_acc = sess.run(accuracy, 
                         feed_dict={
                             x: x_train,
                             y: y_train,
                             keep_prob: 1.
                         })

    print('Loss: {:>10.4f} Validation Accuracy: {:.6f}'.format(loss, valid_acc))

def batch_features_labels(features, labels, batch_size):
    """
    Split features and labels 
    """

    for start in range(0, len(features), batch_size):
        end = min(start + batch_size, len(features))
        yield features[start:end], labels[start:end]

def load_preprocess_training(batch_size):
    """
    Load the Preprocessed Training data and return them in batches of <batch_size> or less
    """
    features = features.reshape((len(features), 3, 32, 32)).transpose(0, 2, 3, 1)
    filename = 'preprocess_training.p'
    features, labels = pickle.load(open(filename, mode='rb'))

    # Return the training data in batches of size <batch_size> or less
    return batch_features_labels(features, labels, batch_size)


print('Training...')
with tf.Session() as sess:
    # Initializing the variables
    sess.run(tf.global_variables_initializer())

    # Training cycle
    for i in range(iterations):
        for batch_features, batch_labels in load_preprocess_training(batch_size):
                train_neural_network(sess, optimizer, keep_probability, batch_features, batch_labels)

                if i % 10 == 0:
                    print('Iterations {}, CIFAR-10 Batch {}:  '.format(i, 1), end='')
                    print_stats(sess, batch_features, batch_labels, cost, accuracy)

ValueError: Cannot feed value of shape (8000, 3072) for Tensor 'input_x:0', which has shape '(?, 32, 32, 3)' ValueError:无法为Tensor'input_x:0'提供形状值(8000,3072),其形状为'(?,32,32,3)'

the problem is located in here: 问题出在这里:

  def batch_features_labels(features, labels, batch_size):
    """
    Split features and labels
    """
    for start in range(0, len(features), batch_size):
        end = min(start + batch_size, len(features))
        yield features[start:end], labels[start:end]

You should reshape the item in features from 3072 to [32,32,3] 你应该重塑从3072到[32,32,3]的特征中的项目

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM