def trainAlgo(self): self.model = Sequential() self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), input_shape=(32, 32, 3), data_format='channels_last', activation=self.param['hidden_activation'], strides=(self.param['kernelSize_stride'], self.param['kernelSize_stride']))) self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), data_format='channels_last', activation=self.param['hidden_activation'], strides=(self.param['kernelSize_stride'], self.param['kernelSize_stride']))) self.model.add(Flatten()) self.model.add( Dense(self.outputData['Y'].shape[1], activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer=self.param['optimizer']) self.model.fit_generator( XYdataGenerator(self.inputData['X'], self.outputData['Y'], 32, 32, self.param['batch_size']), steps_per_epoch=int( ceil((len(self.inputData['X']) / self.param['batch_size']))), epochs=self.param['epochs'])
def trainAlgo(self): self.model=Sequential() #first convolution and pooling layers self.model.add(Conv2D(self.param['hidden_neuron'],(self.param['hidden_kernel_size'],self.param['hidden_kernel_size']),padding="valid",input_shape=(self.param['pixel'],self.param['pixel'],3),data_format='channels_last',activation=self.param['hidden_activation'])) self.model.add(MaxPooling2D(pool_size=(3,3),strides=(2,2))) #second convolution and pooling layers self.model.add(Conv2D(256,(5,5),strides=(1,1),padding="same",activation="relu")) self.model.add(MaxPooling2D(pool_size=(3,3),strides=(2,2))) #three convolution and pooling layers self.model.add(Conv2D(384,(3,3),strides=(1,1),padding='same',activation='relu')) self.model.add(Conv2D(384,(3,3),strides=(1,1),padding='same',activation='relu')) self.model.add(Conv2D(256,(3,3),strides=(1,1),padding='same',activation='relu')) self.model.add(MaxPooling2D(pool_size=(3,3),strides=(2,2))) #fully connection self.model.add(Flatten()) self.model.add(Dense(1024,activation='relu')) self.model.add(Dropout(rate=0.5)) #classification self.model.add(Dense(self.outputData['Y'].shape[1],activation='softmax')) self.model.compile(loss='categorical_crossentropy',optimizer=self.param['optimizer']) self.model.fit_generator( XYdataGenerator(self.inputData['X'],self.outputData['Y'],self.param['pixel'],self.param['pixel'],self.param['batch_size']), steps_per_epoch=int(ceil((len(self.inputData['X'])/self.param['batch_size']))), epochs=self.param['epochs'] )
def trainAlgo(self): self.model = Sequential() self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), input_shape=(32, 32, 3), data_format='channels_last', padding='same', activation=self.param['hidden_activation'])) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add( Conv2D(self.param['hidden_neuron'] * 2, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation=self.param['hidden_activation'])) self.model.add(Dropout(self.param['dropout'])) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add( Conv2D(self.param['hidden_neuron'] * 4, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation=self.param['hidden_activation'])) self.model.add(Dropout(self.param['dropout'])) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add( Conv2D(self.param['hidden_neuron'] * 8, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation=self.param['hidden_activation'])) self.model.add(Dropout(self.param['dropout'])) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Flatten()) self.model.add( Dense(128, kernel_initializer='normal', activation='relu')) self.model.add(Dropout(self.param['dropout'])) self.model.add( Dense(self.outputData['Y'].shape[1], kernel_initializer='normal', activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer=self.param['optimizer'], metrics=['accuracy']) self.model.fit_generator( XYdataGenerator(self.inputData['X'], self.outputData['Y'], 32, 32, self.param['batch_size']), steps_per_epoch=int( ceil((len(self.inputData['X']) / self.param['batch_size']))), epochs=self.param['epochs'])
def trainAlgo(self): self.model = Sequential() a = int(self.param['hidden_neuron']) b = int(self.param['DropOut']) SZ = int(self.param['shape_size']) self.model.add( Conv2D(a // 8, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), input_shape=(SZ, SZ, 3), data_format='channels_last', activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D(a // 4, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation='relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add( Conv2D(a // 2, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation='relu')) self.model.add( Conv2D(a, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation='relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Dropout(b // 2)) self.model.add( Conv2D(a, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation='relu')) self.model.add( Conv2D(a, (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding='same', activation='relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Flatten()) self.model.add(Dense(500, activation='relu')) self.model.add(Dropout(b)) self.model.add( Dense(self.outputData['Y'].shape[1], activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer=self.param['optimizer']) self.model.fit_generator( XYdataGenerator(self.inputData['X'], self.outputData['Y'], SZ, SZ, self.param['batch_size']), steps_per_epoch=int( ceil((len(self.inputData['X']) / self.param['batch_size']))), epochs=self.param['epochs'])
def trainAlgo(self): # 建立Keras 的 Sequential 模型 self.model = Sequential() # 建立捲積層 1 self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'],self.param['hidden_kernel_size'] ), input_shape=(32,32,3), activation=self.param['hidden_activation'], padding='same')) # 加入Dropout 避免 overfitting self.model.add(Dropout( self.param['dropout'] )) # 建立池化層 1 self.model.add(MaxPooling2D(pool_size=(2,2))) # 建立捲積層 2 self.model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu',padding='same')) # 加入Dropout 避免 overfitting self.model.add(Dropout( self.param['dropout'] )) # 建立池化層 2 self.model.add(MaxPooling2D(pool_size=(2,2))) # 建立平坦層 self.model.add(Flatten()) # 加入Dropout 避免 overfitting self.model.add(Dropout(self.param['dropout'])) # 建立隱藏層 self.model.add(Dense(1024,activation='relu')) self.model.add(Dropout(self.param['dropout'])) # 建立輸出層 self.model.add(Dense(self.outputData['Y'].shape[1],activation='softmax')) #定義訓練方式 self.model.compile(loss='categorical_crossentropy',optimizer=self.param['optimizer']) #開始訓練 self.model.fit_generator( XYdataGenerator(self.inputData['X'],self.outputData['Y'],32,32,self.param['batch_size']), steps_per_epoch=int(ceil((len(self.inputData['X'])/self.param['batch_size']))), epochs=self.param['epochs'])
def trainAlgo(self): self.model=Sequential() self.model.add(Conv2D(self.param['hidden_neuron'],(self.param['hidden_kernel_size'],self.param['hidden_kernel_size']),padding="same",input_shape=(32,32,3),data_format='channels_last',activation=self.param['hidden_activation'])) self.model.add(Conv2D(64,(3,3),padding="same")) self.model.add(Activation("relu")) self.model.add(MaxPooling2D(pool_size=(2,2))) self.model.add(Dropout(rate=0.25)) self.model.add(Conv2D(128,(3,3),padding="same")) self.model.add(Activation("relu")) self.model.add(Conv2D(128,(3,3),padding="same")) self.model.add(Activation("relu")) self.model.add(MaxPooling2D(pool_size=(2,2))) self.model.add(Dropout(rate=0.25)) self.model.add(Conv2D(256,(3,3),padding="same")) self.model.add(Activation("relu")) self.model.add(Conv2D(256,(3,3),padding="same")) self.model.add(Activation("relu")) self.model.add(Conv2D(256,(3,3),padding="same")) self.model.add(Activation("relu")) self.model.add(MaxPooling2D(pool_size=(2,2))) self.model.add(Dropout(rate=0.5)) self.model.add(Flatten()) self.model.add(Dense(4096,activation='relu')) self.model.add(Dropout(rate=0.5)) self.model.add(Dense(4096,activation='relu')) self.model.add(Dropout(rate=0.5)) self.model.add(Dense(1024)) self.model.add(Activation("relu")) self.model.add(BatchNormalization()) self.model.add(Dropout(0.5)) self.model.add(Dense(self.outputData['Y'].shape[1],activation='softmax')) self.model.compile(loss='categorical_crossentropy',optimizer=self.param['optimizer']) self.model.fit_generator( XYdataGenerator(self.inputData['X'],self.outputData['Y'],32,32,self.param['batch_size']), steps_per_epoch=int(ceil((len(self.inputData['X'])/self.param['batch_size']))), epochs=self.param['epochs'] )
def trainAlgo(self): self.model = Sequential() self.model.add( Conv2D(filters=self.param['hidden_neuron'], kernel_size=(self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding="same", input_shape=(28, 28, 3), activation=self.param['hidden_activation'])) self.model.add( MaxPool2D(pool_size=(self.param['pool_size'], self.param['pool_size']))) self.model.add( Conv2D(filters=self.param['hidden_neuron'], kernel_size=(self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), padding="same", input_shape=(28, 28, 1), activation=self.param['hidden_activation'])) self.model.add( MaxPool2D(pool_size=(self.param['pool_size'], self.param['pool_size']))) self.model.add(Dropout(self.param['dropout'])) self.model.add(Flatten()) self.model.add(Dense(128, activation='relu')) self.model.add(Dropout(self.param['dropout_2'])) self.model.add( Dense(self.outputData['Y'].shape[1], activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer=self.param['optimizer']) self.model.fit_generator( XYdataGenerator(self.inputData['X'], self.outputData['Y'], 28, 28, self.param['batch_size']), steps_per_epoch=int( ceil((len(self.inputData['X']) / self.param['batch_size']))), epochs=self.param['epochs'], verbose=1)
def trainAlgo(self): self.model = Sequential() a = int(self.param['hidden_neuron']) self.model.add( Conv2D((a // 8), (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), input_shape=(32, 32, 3), data_format='channels_last', activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D((a // 8), (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( MaxPooling2D(pool_size=(self.param['pool_size'], self.param['pool_size']), strides=(2, 2))) self.model.add( Conv2D((a // 4), (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D((a // 4), (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( MaxPooling2D(pool_size=(self.param['pool_size'], self.param['pool_size']), strides=(2, 2))) self.model.add( Conv2D((a // 2), (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D((a // 2), (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D((a // 2), (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( MaxPooling2D(pool_size=(self.param['pool_size'], self.param['pool_size']), strides=(2, 2))) if (self.param['vgg16_CCCP']): self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( MaxPooling2D(pool_size=(self.param['pool_size'], self.param['pool_size']), strides=(2, 2))) if (self.param['vgg16_CCCP2']): self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), activation=self.param['hidden_activation'], padding='same')) self.model.add( MaxPooling2D(pool_size=(self.param['pool_size'], self.param['pool_size']), strides=(2, 2))) self.model.add(Flatten()) if (self.param['vgg16_fc']): self.model.add(Dropout(self.param['dropout_value'])) self.model.add(Dense(500, activation='softmax')) if (self.param['vgg16_fc2']): self.model.add(Dropout(self.param['dropout_value'])) self.model.add(Dense(10, activation='softmax')) self.model.add(Dropout(self.param['dropout_value'])) self.model.add( Dense(self.outputData['Y'].shape[1], activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer=self.param['optimizer']) self.model.fit_generator( XYdataGenerator(self.inputData['X'], self.outputData['Y'], 32, 32, self.param['batch_size']), steps_per_epoch=int( ceil((len(self.inputData['X']) / self.param['batch_size']))), epochs=self.param['epochs'])
def trainAlgo(self): self.model = Sequential() self.model.add( Conv2D(self.param['hidden_neuron'], (self.param['hidden_kernel_size'], self.param['hidden_kernel_size']), input_shape=(32, 32, 3), activation=self.param['hidden_activation'], padding='same')) self.model.add( Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(48, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(48, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Dropout(self.param['dropout'])) self.model.add( Conv2D(80, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(80, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(80, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(80, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(80, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Dropout(self.param['dropout'])) self.model.add( Conv2D(128, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(128, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(128, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(128, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add( Conv2D(128, (3, 3), input_shape=(32, 32, 3), padding='same')) self.model.add(Activation(self.param['hidden_activation'])) self.model.add(GlobalMaxPooling2D()) self.model.add(Dropout(self.param['dropout'])) self.model.add(Dense(500, activation='relu')) self.model.add(Dropout(self.param['dropout2'])) self.model.add( Dense(self.outputData['Y'].shape[1], activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer=self.param['optimizer'], metrics=['accuracy']) self.model.fit_generator( XYdataGenerator(self.inputData['X'], self.outputData['Y'], 32, 32, self.param['batch_size']), steps_per_epoch=int( ceil((len(self.inputData['X']) / self.param['batch_size']))), epochs=self.param['epochs'])