numpy重塑错误 - CNN的

时间:2018-01-06 20:59:57

标签: arrays numpy machine-learning neural-network

我正在使用Mac上的Anaconda中的Jupyter Notebook。与Keras和Tensorflow一起用于神经网络构建。

我调整了图像大小,并将它们移动到目录中。它们是RGB图像(3色通道)。在展平图像并分割成我的CNN的训练/测试后,它不会让我重塑我的图像。我正在尝试重塑的训练集有7151个图像,大小为256乘256.这是代码:

img_rows, img_cols = 256, 256

# number of channels
img_channels = 3

#%%
#  data

path1 = 'C:\\Users\\PALASH\\Desktop\\safeunsafe\\inputdata'#path of folder of images    
path2 = 'C:\\Users\\PALASH\\Desktop\\safeunsafe\\resizeddata'   #path of folder to save images 

listing = os.listdir(path1) 
num_samples=size(listing)

#for file in listing:
#    im = Image.open(path1 + '\\' + file)   
#    img = im.resize((img_rows,img_cols))
#    gray = img.convert('L')
                #need to do some more processing here           
#    gray.save(path2 +'\\' +  file, "JPEG")

imlist = os.listdir(path2)

im1 = array(Image.open('C:\\Users\\NAME\\Desktop\\safeunsafe\\resizeddata' + '\\'+ imlist[0])) # open one image to get size
m,n = im1.shape[0:2] # get the size of the images
imnbr = len(imlist) # get the number of images

# create matrix to store all flattened images
immatrix = array([array(Image.open('C:\\Users\\NAME\\Desktop\\safeunsafe\\resizeddata'+ '\\' + im2))#.flatten()
              for im2 in imlist],'f')

label=np.ones((num_samples,),dtype = int)
label[0:5723]=0
label[5723:]=1

data,Label = shuffle(immatrix,label, random_state=2)
train_data = [data,Label]


(X, y) = (train_data[0],train_data[1])


# STEP 1: split X and y into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4)


**X_train = X_train.reshape(X_train.shape[0],3,img_rows,img_cols)
X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)**
上面的

代码是返回此错误的原因:

**cannot reshape array of size 468647936 into shape (7151,3,256,256)**

如何解决此问题?我正在尝试用RGB图像训练CNN,并且我意识到它没有正确地重塑,因为7151 * 3 * 256 * 256不等于原始形状。我应该改变什么来正确地重塑图像?

这是我目前的模特:

`model = Sequential()
model.add(Conv2D(32,(26,26), input_shape=(256,256,3)))
model.add(Activation('relu'))
model.add(Conv2D(14,14))
model.add(Activation('relu'))
model.add(Conv2D(9,9))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Dense(64))
model.add(Dense(2))

以下是我的模型摘要:

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 231, 231, 32)      64928     
_________________________________________________________________
activation_1 (Activation)    (None, 231, 231, 32)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 218, 218, 14)      87822     
_________________________________________________________________
activation_2 (Activation)    (None, 218, 218, 14)      0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 210, 210, 9)       10215     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 105, 105, 9)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 103, 103, 3)       246       
_________________________________________________________________
activation_3 (Activation)    (None, 103, 103, 3)       0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 51, 51, 3)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 7803)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               998912    
_________________________________________________________________
dense_2 (Dense)              (None, 64)                8256      
_________________________________________________________________
dense_3 (Dense)              (None, 2)                 130       
_________________________________________________________________
activation_4 (Activation)    (None, 2)                 0         
=================================================================
Total params: 1,170,509
Trainable params: 1,170,509
Non-trainable params: 0

0 个答案:

没有答案