使用Tensorflow的目标阵列形状与预期输出不同

时间:2019-07-06 07:42:36

标签: python tensorflow keras

我正在尝试制作CNN(仍是初学者)。尝试拟合模型时出现此错误:

ValueError:传递形状为(10000,10)的目标数组以输出形状为(None,6,6,10),同时用作损失categorical_crossentropy。这种损失期望目标与输出具有相同的形状。

标签的形状=(10000,10) 图像数据的形状=(10000,32,32,3)

代码:

import pickle
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Dense, Dropout, Activation, Flatten, 
                                     Conv2D, MaxPooling2D)
from tensorflow.keras.callbacks import TensorBoard
from keras.utils import to_categorical
import numpy as np
import time

MODEL_NAME = f"_________{int(time.time())}"
BATCH_SIZE = 64

class ConvolutionalNetwork():
    '''
    A convolutional neural network to be used to classify images
    from the CIFAR-10 dataset.
    '''

    def __init__(self):
        '''
        self.training_images -- a 10000x3072 numpy array of uint8s. Each 
                                a row of the array stores a 32x32 colour image. 
                                The first 1024 entries contain the red channel 
                                values, the next 1024 the green, and the final 
                                1024 the blue. The image is stored in row-major 
                                order, so that the first 32 entries of the array are the red channel values of the first row of the image.
        self.training_labels -- a list of 10000 numbers in the range 0-9. 
                                The number at index I indicates the label 
                                of the ith image in the array data.
        '''
        # List of image categories
        self.label_names = (self.unpickle("cifar-10-batches-py/batches.meta",
                            encoding='utf-8')['label_names'])

        self.training_data = self.unpickle("cifar-10-batches-py/data_batch_1")
        self.training_images = self.training_data[b'data']
        self.training_labels = self.training_data[b'labels']

        # Reshaping the images + scaling 
        self.shape_images()  

        # Converts labels to one-hot
        self.training_labels = np.array(to_categorical(self.training_labels))

        self.create_model()

        self.tensorboard = TensorBoard(log_dir=f'logs/{MODEL_NAME}')

    def unpickle(self, file, encoding='bytes'):
        '''
        Unpickles the dataset files.
        '''
        with open(file, 'rb') as fo:
            training_dict = pickle.load(fo, encoding=encoding)
        return training_dict

    def shape_images(self):
        '''
        Reshapes the images and scales by 255.
        '''
        images = list()
        for d in self.training_images:
            image = np.zeros((32,32,3), dtype=np.uint8)
            image[...,0] = np.reshape(d[:1024], (32,32)) # Red channel
            image[...,1] = np.reshape(d[1024:2048], (32,32)) # Green channel
            image[...,2] = np.reshape(d[2048:], (32,32)) # Blue channel
            images.append(image)

        for i in range(len(images)):
            images[i] = images[i]/255

        images = np.array(images)
        self.training_images = images
        print(self.training_images.shape)

    def create_model(self):
        '''
        Creating the ConvNet model.
        '''
        self.model = Sequential()
        self.model.add(Conv2D(64, (3, 3), input_shape=self.training_images.shape[1:]))
        self.model.add(Activation("relu"))
        self.model.add(MaxPooling2D(pool_size=(2,2)))

        self.model.add(Conv2D(64, (3,3)))
        self.model.add(Activation("relu"))
        self.model.add(MaxPooling2D(pool_size=(2,2)))

        # self.model.add(Flatten())
        # self.model.add(Dense(64))
        # self.model.add(Activation('relu'))

        self.model.add(Dense(10))
        self.model.add(Activation(activation='softmax'))

        self.model.compile(loss="categorical_crossentropy", optimizer="adam", 
                           metrics=['accuracy'])

    def train(self):
        '''
        Fits the model.
        '''
        print(self.training_images.shape)
        print(self.training_labels.shape)
        self.model.fit(self.training_images, self.training_labels, batch_size=BATCH_SIZE, 
                       validation_split=0.1, epochs=5, callbacks=[self.tensorboard])


network = ConvolutionalNetwork()
network.train()

感谢您的帮助,已经尝试修复了一个小时。

4 个答案:

答案 0 :(得分:2)

创建模型时,需要取消注释Flatten层的注释。本质上,该层所做的是,它需要一个4D输入(batch_size, height, width, num_filters)并将其展开为一个2D的(batch_size, height * width * num_filters)。这是获得所需输出形状所必需的。

答案 1 :(得分:1)

您必须使模型输出的形状与标签相同。

也许最简单的解决方案是确保模型以这些层结尾:

model.add(Flatten())
## possibly an extra dense layer or 2 with 'relu' activation
model.add(Dense(10, activation=`softmax`))

这是分类模型中最常见的“结尾”,并且可以说是最容易理解的。

不清楚您为何注释掉本节:

# self.model.add(Flatten())
# self.model.add(Dense(64))
# self.model.add(Activation('relu'))

看起来会给您所需的输出形状?

答案 2 :(得分:1)

create_model(self)中的输出层之前取消注释扁平化层,conv层不适用于一维张量/数组,因此对于您而言,获得正确形状的输出层可添加{{ 1}}图层,就在您的输出图层之前,如下所示:

Flatten()

上面的代码将为您提供一个输出形状为def create_model(self): ''' Creating the ConvNet model. ''' self.model = Sequential() self.model.add(Conv2D(64, (3, 3), input_shape=self.training_images.shape[1:]), activation='relu') #self.model.add(Activation("relu")) self.model.add(MaxPooling2D(pool_size=(2,2))) self.model.add(Conv2D(64, (3,3), activation='relu')) #self.model.add(Activation("relu")) self.model.add(MaxPooling2D(pool_size=(2,2))) # self.model.add(Dense(64)) # self.model.add(Activation('relu')) self.model.add(Flatten()) self.model.add(Dense(10, activation='softmax')) #self.model.add(Activation(activation='softmax')) self.model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy']) print ('model output shape:', self.model.output_shape)#prints out the output shape of your model 的模型。

也请将来使用激活作为图层参数。

答案 3 :(得分:0)

使用model.summary()检查模型的输出形状。没有注释掉的Flatten()图层,图层的形状将保留图像的原始尺寸,输出图层的形状为(None, 6, 6, 10)

您在这里想要做的大致是:

  1. 以(batch_size,img宽度,img heigh,通道)的形状开头
  2. 使用卷积通过应用滤镜来检测图像中的图案
  3. 通过最大池化来减少img的宽度和高度
  4. 然后Flatten()图像的尺寸,以便最终得到的不是一组特征(宽度,高度,特征),而是一组特征。
  5. 与您的班级相匹配。

注释掉的代码执行步骤4;删除Flatten()层时,最终会导致一组错误的尺寸。