Hi~
I want 10 Convolution layer.
C : Convolution layer
FC : Fully Connected layer
But I didn't build 10 layer.(Convolution2D layer1~10),
-> 8X28@16 -> 6X26@16 -> 4X24@16 -> 2X22@16 -> 1X20@16
-> Flatten(704) -> FC110 -> FC11
-python_error---------------------------------------------------
Traceback (most recent call last):
File "C:\Users\koo\python\koo cnn plate - master-09B-CNN Model-2 [C12, FC2].py", line 182, in
model.add(Dense(110))
File "C:\Python27\lib\site-packages\keras-0.3.2-py2.7.egg\keras\layers\containers.py", line 70, in add
self.layers[-1].set_previous(self.layers[-2])
File "C:\Python27\lib\site-packages\keras-0.3.2-py2.7.egg\keras\layers\core.py", line 137, in set_previous
assert self.input_ndim == len(layer.output_shape), ('Incompatible shapes: layer expected input with ndim=' +
File "C:\Python27\lib\site-packages\keras-0.3.2-py2.7.egg\keras\layers\core.py", line 904, in output_shape
'(got ' + str(input_shape[1:]) + '. '
Exception: The shape of the input to "Flatten" is not fully defined (got (16, 20, 0). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
I did build 9 layer(Convolution2D layer1~9)
-> 10x30@16 -> 8X28@16 -> 6X26@16 -> 4X24@16 > 2X22@16
-> Flatten(1536) -> FC110 -> FC11
nb_classes = 11
img_rows, img_cols = 40, 20
X_train = (57926L, 40L, 20L) <----[pos : 5266, neg: 52660]
y_train = (57926L,)
Is it possible to use up to some maximum my Convolution2D layer?
Why only possible 1~9 layer?
I don't no. please help me
Please try to rephrase your question, it's not comprehensible.
Next step: post the code that leads to the error :D
But I didn't build 10 layer.(Convolution2D layer1~10),
-> 8X28@16 -> 6X26@16 -> 4X24@16 -> 2X22@16 -> 1X20@16
-> Flatten(704) -> FC110 -> FC11
-python_error---------------------------------------------------
Traceback (most recent call last):
File "C:\Users\koo\python\koo cnn plate - master-09B-CNN Model-2 [C12, FC2].py", line 182, in
model.add(Dense(110))
File "C:\Python27\lib\site-packages\keras-0.3.2-py2.7.egg\keras\layers\containers.py", line 70, in add
self.layers[-1].set_previous(self.layers[-2])
File "C:\Python27\lib\site-packages\keras-0.3.2-py2.7.egg\keras\layers\core.py", line 137, in set_previous
assert self.input_ndim == len(layer.output_shape), ('Incompatible shapes: layer expected input with ndim=' +
File "C:\Python27\lib\site-packages\keras-0.3.2-py2.7.egg\keras\layers\core.py", line 904, in output_shape
'(got ' + str(input_shape[1:]) + '. '
Exception: The shape of the input to "Flatten" is not fully defined (got (16, 20, 0). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
The actual Python code please... :)
#-*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: koo
#
# Created: 24-03-2016
# Copyright: (c) koo 2016
# Licence: <your licence>
#-------------------------------------------------------------------------------
from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from datetime import datetime
from keras.models import model_from_json
import cv2
import struct
import os
import sys
################################
#now
a = datetime.now()
#year
year = str(a.year-2000)
#month
if len(str(a.month)) == 1:
month = '0'+ str(a.month)
else:
month = str(a.month)
#day
if len(str(a.day)) == 1:
day = '0'+ str(a.day)
else:
day = str(a.day)
#hour
if len(str(a.hour)) == 1:
hour = '0'+ str(a.hour)
else:
hour = str(a.hour)
#minute
if len(str(a.minute)) == 1:
minute = '0'+ str(a.minute)
else:
minute = str(a.minute)
#second
if len(str(a.second)) == 1:
second = '0'+ str(a.second)
else:
second = str(a.second)
today = year + month + day + '_' + hour + minute + second
################################
batch_size = 128
nb_classes = 11
#basic
#nb_epoch = 12
#koo-test1(160301)
nb_epoch = 1
# input image dimensions
img_rows, img_cols = 40, 20
# number of convolutional filters to use
nb_filters = 16
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
#basic
nb_conv = 3
################################
# the data, shuffled and split between tran and test sets
crop_all_path='D:\\CNN\\CNN-DB\\crop_all\\'
#word_01_3456_img = np.memmap(crop_all_path+'WIGB_01_3456.dat',dtype=np.uint8, mode='r' ,shape=(1098*6,40,20))
word_0_img = np.memmap(crop_all_path+'WIGB_0.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'0')),40,20))
word_1_img = np.memmap(crop_all_path+'WIGB_1.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'1')),40,20))
word_2_img = np.memmap(crop_all_path+'WIGB_2.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'2')),40,20))
word_3_img = np.memmap(crop_all_path+'WIGB_3.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'3')),40,20))
word_4_img = np.memmap(crop_all_path+'WIGB_4.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'4')),40,20))
word_5_img = np.memmap(crop_all_path+'WIGB_5.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'5')),40,20))
word_6_img = np.memmap(crop_all_path+'WIGB_6.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'6')),40,20))
word_7_img = np.memmap(crop_all_path+'WIGB_7.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'7')),40,20))
word_8_img = np.memmap(crop_all_path+'WIGB_8.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'8')),40,20))
word_9_img = np.memmap(crop_all_path+'WIGB_9.dat',dtype=np.uint8, mode='r' ,shape=(len(os.listdir(crop_all_path+'9')),40,20))
word_0_9_img_num_80per=np.zeros(10,dtype=np.uint64)
for x in range(10):
word_0_9_img_num_80per[x] = int(len(os.listdir(crop_all_path+str(x))) * 0.8)
word_0_9_img_num_80per_sum = word_0_9_img_num_80per.sum()
back_0_70_img = np.memmap(crop_all_path+'BIGB_0_70.dat',dtype=np.uint8, mode='r' ,shape=(1098*70,40,20))
##################################
#how much img set
#img_num = 50
##################################
#X_train_pos = word_01_3456_img[0:6*img_num]
X_train_pos = np.vstack((word_0_img[0:word_0_9_img_num_80per[0]],word_1_img[0:word_0_9_img_num_80per[1]]))
X_train_pos = np.vstack((X_train_pos,word_2_img[0:word_0_9_img_num_80per[2]]))
X_train_pos = np.vstack((X_train_pos,word_3_img[0:word_0_9_img_num_80per[3]]))
X_train_pos = np.vstack((X_train_pos,word_4_img[0:word_0_9_img_num_80per[4]]))
X_train_pos = np.vstack((X_train_pos,word_5_img[0:word_0_9_img_num_80per[5]]))
X_train_pos = np.vstack((X_train_pos,word_6_img[0:word_0_9_img_num_80per[6]]))
X_train_pos = np.vstack((X_train_pos,word_7_img[0:word_0_9_img_num_80per[7]]))
X_train_pos = np.vstack((X_train_pos,word_8_img[0:word_0_9_img_num_80per[8]]))
X_train_pos = np.vstack((X_train_pos,word_9_img[0:word_0_9_img_num_80per[9]]))
#X_train_neg = 5266
img_num = X_train_pos.shape[0]*10
X_train_neg = back_0_70_img[0:img_num]
X_train = np.vstack((X_train_pos,X_train_neg))
#y_train_pos = np.zeros(X_train_pos.shape[0],dtype=np.uint8)
y_train_pos = np.array([],dtype=np.uint8)
for x in range(10):
a = np.zeros(word_0_9_img_num_80per[x],dtype=np.uint8)
a[:] = x
y_train_pos = np.append(y_train_pos, a)
#y_train_neg
y_train_neg = np.zeros((img_num),dtype=np.uint8)
y_train_neg[:] = 10
y_train = np.append(y_train_pos, y_train_neg)
##################################
print (X_train.shape, y_train.shape)
################################
print ("1.Train")
#########################################################################################################
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')
X_train /= 255
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
################################
#build model
model = Sequential()
#C1
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
border_mode='valid',
input_shape=(1, img_rows, img_cols)))
#C2
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
#C3
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
#C4
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
#C5
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
#C6
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
#C7
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
#C8
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
###C9
##model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
###C10
##model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
###C11
##model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
###C12
##model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
#Flatten
model.add(Flatten())
#FC1
model.add(Dense(110))
#FC2
model.add(Dense(nb_classes))
################################
print ("1-1.Configure the learning process.")
startTime1=datetime.now()
#Configure the learning process.
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
endTime1=datetime.now()
Time1=endTime1-startTime1
print ('1Time:',Time1)
print ("----------------------------------------------------")
################################
print ("1-2.Train the model for a fixed number of epochs.")
startTime2=datetime.now()
#Train the model for a fixed number of epochs.
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
show_accuracy=True, verbose=1)
endTime2=datetime.now()
Time2=endTime2-startTime2
print ('Time:',Time2)
print ("----------------------------------------------------")
################################
json_string = model.to_json()
traindata_path = 'D:\\CNN\\CNN-DB\\CNN-traindata\\plate\\ori-20X40\\09B\\'
open( traindata_path+'CNN Model-2[09B][C8, FC2][epoch='+str(nb_epoch)+']['+today+']_architecture.json', 'w').write(json_string)
model.save_weights(traindata_path+'CNN Model-2[09B][C8, FC2][epoch='+str(nb_epoch)+']['+today+']_weights.h5')
#######################################################################################################
Thanks, I think I understand what you want... border_mode='same'
Convolution2D defaults to border_mode='valid', which means output shape < input shape. But with border_mode='same' -> output shape == input shape.
wow~ very good. thank you very much.
I understand.
I do change border_mode='valid'.
I do build 9 layer(Convolution2D layer1~9)
I do change border_mode='same'.
I do build 10 layer(Convolution2D layer1~10)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.