zhaolei5 / zhaolei520

0 stars 0 forks source link

T_Max_Avg:A improved pooling method for convolutional neural networks #1

Open zhaolei5 opened 1 year ago

zhaolei5 commented 1 year ago
zhaolei5 commented 1 year ago

import tensorflow as tf

batch_size = 32 epochs = 50 num_classes = 10 channel=1 img_rows, img_cols = 28, 28 ksize=4

the data, split between train and test sets

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, channel) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, channel) input_shape = (img_rows, img_cols, channel)

x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255.0 x_test /= 255.0 print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples')

convert class vectors to binary class matrices

y_train = tf.keras.utils.to_categorical(y_train, num_classes) y_test = tf.keras.utils.to_categorical(y_test, num_classes) class CustomPooling(tf.keras.layers.Layer): def init(self, ksize=3,k=5,c=0.1): super(CustomPooling, self).init() self.ksize = ksize self.k = k self.c = c def call(self, inputs):

Extract image patches

    k_size=self.ksize
    channel = inputs.shape[3]
    patches = tf.image.extract_patches(inputs,
                                       sizes=[1, k_size, k_size, 1],
                                       strides=[1, k_size, k_size, 1],
                                       rates=[1, 1, 1, 1],
                                       padding='VALID')

    return tf.concat([ tf.where(tf.reduce_all(tf.math.top_k(patches[:,:,:,c::channel], k=self.k).values >=self.c, axis=-1, keepdims=True),
                                tf.reduce_max(tf.math.top_k(patches[:,:,:,c::channel], k=self.k).values, axis=-1, keepdims=True),
                      tf.reduce_mean(tf.math.top_k(patches[:,:,:,c::channel], k=self.k).values, axis=-1, keepdims=True))
                      for c in range(channel)],axis=-1)

def get_model(sec,ksize,kk,c): tf.keras.backend.clear_session() model = tf.keras.Sequential() model.add(tf.keras.layers.Conv2D(6, kernel_size=5,activation="tanh",strides=1,padding='same',input_shape=input_shape)) if sec==0: model.add(tf.keras.layers.MaxPooling2D(pool_size=(ksize,kk))) elif sec==1: model.add(tf.keras.layers.AveragePooling2D(pool_size=(ksize,kk))) else: model.add(CustomPooling(ksize,kk,c)) model.add(tf.keras.layers.Conv2D(16, kernel_size=5,activation='tanh',strides=1,padding='same')) if sec==0: model.add(tf.keras.layers.MaxPooling2D(pool_size=(ksize,kk))) elif sec==1: model.add(tf.keras.layers.AveragePooling2D(pool_size=(ksize,kk))) else: model.add(CustomPooling(ksize,kk,c)) model.add(tf.keras.layers.Conv2D(120, kernel_size=5,activation='tanh',strides=1,padding='same')) model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(84, activation='tanh')) model.add(tf.keras.layers.Dense(num_classes, activation='softmax')) model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy']) return model

cc=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]

import time custom=[] custom.append("Mnist 50 epoch") for i in range(2): train_start_time=time.time() modelcus = get_model(2,ksize,6,0.7) modelcustom = modelcus.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=True, validation_split=0.15) train_end_time=time.time() test_start_time=time.time() ctest_loss, ctest_acc = modelcus.evaluate(x_test, y_test, verbose=1) test_end_time=time.time() custom.append(["CustomPool_size="+str(ksize),"K="+str(6),"c="+str(0.7), "Train_time="+str(train_end_time-train_start_time), "Test_time="+str(test_end_time-test_start_time),"Test_accuracy="+str(ctest_acc)])

print(ksize,kk,"acc=",ctest_acc)

print(custom)