def build_InceptionV3(image_size=None, weights_path=None): image_size = image_size or (299, 299) if K.image_dim_ordering() == 'th': input_shape = (3, ) + image_size else: input_shape = image_size + (3, ) bottleneck_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=Input(input_shape)) for layer in bottleneck_model.layers: layer.trainable = False x = bottleneck_model.input y = bottleneck_model.output # There are different ways to handle the bottleneck output y = GlobalAveragePooling2D()(x) # y = AveragePooling2D((8, 8), strides=(8, 8))(x) # y = Flatten()(y) # y = BatchNormalization()(y) y = Dense(1024, activation='relu')(y) y = Dropout(.5)(y) y = Dense(1)(y) model = Model(input=x, output=y) model.compile(optimizer=Adam(lr=1e-4), loss='mse') return model
def dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=None, weight_decay=1E-4): concat_axis = 1 if K.image_dim_ordering() == "th" else -1 feature_list = [x] for i in range(nb_layers): x = conv_block(x, growth_rate, dropout_rate, weight_decay) feature_list.append(x) x = Concatenate(axis=concat_axis)(feature_list) nb_filter += growth_rate return x, nb_filter
def transition_block(input, nb_filter, dropout_rate=None, weight_decay=1E-4): concat_axis = 1 if K.image_dim_ordering() == "th" else -1 x = Convolution2D(nb_filter, (1, 1), kernel_initializer="he_uniform", padding="same", use_bias=False, kernel_regularizer=l2(weight_decay))(input) if dropout_rate is not None: x = Dropout(dropout_rate)(x) x = AveragePooling2D((2, 2), strides=(2, 2))(x) x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x) return x
def DenseNet(self, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=16, dropout_rate=None, weight_decay=1E-4, verbose=True): ''' denseNet 自定义结构 :param depth: :param nb_dense_block: :param growth_rate: :param nb_filter: :param dropout_rate: :param weight_decay: :param verbose: :return: ''' nb_classes = self.num_classes img_dim = self.input_shape def conv_block(input, nb_filter, dropout_rate=None, weight_decay=1E-4): x = Activation('relu')(input) x = Convolution2D(nb_filter, (3, 3), kernel_initializer="he_uniform", padding="same", use_bias=False, kernel_regularizer=l2(weight_decay))(x) if dropout_rate is not None: x = Dropout(dropout_rate)(x) return x def transition_block(input, nb_filter, dropout_rate=None, weight_decay=1E-4): concat_axis = 1 if K.image_dim_ordering() == "th" else -1 x = Convolution2D(nb_filter, (1, 1), kernel_initializer="he_uniform", padding="same", use_bias=False, kernel_regularizer=l2(weight_decay))(input) if dropout_rate is not None: x = Dropout(dropout_rate)(x) x = AveragePooling2D((2, 2), strides=(2, 2))(x) x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x) return x def dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=None, weight_decay=1E-4): concat_axis = 1 if K.image_dim_ordering() == "th" else -1 feature_list = [x] for i in range(nb_layers): x = conv_block(x, growth_rate, dropout_rate, weight_decay) feature_list.append(x) x = Concatenate(axis=concat_axis)(feature_list) nb_filter += growth_rate return x, nb_filter model_input = Input(shape=img_dim) concat_axis = 1 if K.image_dim_ordering() == "th" else -1 assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4" # layers in each dense block nb_layers = int((depth - 4) / 3) # Initial convolution x = Convolution2D(nb_filter, (3, 3), kernel_initializer="he_uniform", padding="same", name="initial_conv2D", use_bias=False, kernel_regularizer=l2(weight_decay))(model_input) x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x) # Add dense blocks for block_idx in range(nb_dense_block - 1): x, nb_filter = dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay) # add transition_block x = transition_block(x, nb_filter, dropout_rate=dropout_rate, weight_decay=weight_decay) # The last dense_block does not have a transition_block x, nb_filter = dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay) x = Activation('relu')(x) x = GlobalAveragePooling2D()(x) x = Dense(nb_classes, activation='softmax', kernel_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay))(x) densenet = Model(inputs=model_input, outputs=x) if verbose: print("DenseNet-%d-%d created." % (depth, growth_rate)) return densenet
def mnist_cnnv_datagen(): """ 使用keras图片增强 :return: """ batch_size = 128 nb_classes = 10 # 分类数 nb_epoch = 12 # 训练轮数 # 输入图片的维度 img_rows, img_cols = 28, 28 # 卷积滤镜的个数 nb_filters = 32 # 最大池化,池化核大小 pool_size = (2, 2) # 卷积核大小 kernel_size = (3, 3) (X_train, y_train), (X_test, y_test) = mnist.load_data(os.path.join(root_path, "data", "mnist", "mnist.npz")) if K.image_dim_ordering() == 'th': # 使用 Theano 的顺序:(conv_dim1, channels, conv_dim2, conv_dim3) X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols) X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: # 使用 TensorFlow 的顺序:(conv_dim1, conv_dim2, conv_dim3, channels) X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) model = keras.models.Sequential() model.add(Convolution2D(nb_filters, kernel_size, padding='same', input_shape=input_shape, data_format="channels_last", activation="relu")) model.add(Convolution2D(nb_filters, kernel_size, padding='same', activation="relu")) model.add(Convolution2D(nb_filters, kernel_size, padding='same', activation="relu")) model.add(MaxPooling2D(pool_size=pool_size)) model.add(Dropout(0.25)) model.add(Convolution2D(nb_filters * 2, kernel_size, padding='same', activation="relu")) model.add(Convolution2D(nb_filters * 2, kernel_size, padding='same', activation="relu")) model.add(Convolution2D(nb_filters * 2, kernel_size, padding='same', activation="relu")) model.add(MaxPooling2D(pool_size=pool_size)) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(nb_classes, activation="softmax")) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) # 图像增强 train_datagen = image.ImageDataGenerator(rotation_range=10, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, vertical_flip=False) validation_datagen = image.ImageDataGenerator(rotation_range=10, width_shift_range=0.2, height_shift_range=0.2) train_datagen.fit(X_train) validation_datagen.fit(X_test) train_generate = train_datagen.flow(X_train, Y_train, batch_size=batch_size) validation_generate = train_datagen.flow(X_test, Y_test, batch_size=batch_size) model.fit_generator(train_generate, steps_per_epoch=X_train.shape[0] // batch_size, epochs=nb_epoch, verbose=1, validation_data=validation_generate, validation_steps=X_test.shape[0] // batch_size, workers=1, use_multiprocessing=False) test_loss, test_acc = model.evaluate_generator(validation_generate, steps=X_test.shape[0] // batch_size, workers=1, use_multiprocessing=False) logger.info('Test accuracy: {0} {1}'.format(test_loss, test_acc)) predictions = model.predict(X_test) predictions = np.argmax(predictions, 1) labels = np.argmax(Y_test, 1) for i in range(10): logger.info("predict:{0} , label:{1}".format(predictions[i], labels[i]))
def mnist_conv(): """ 使用卷积神经网络训练图片分类 :return: """ batch_size = 128 nb_classes = 10 # 分类数 nb_epoch = 20 # 训练轮数 # 输入图片的维度 img_rows, img_cols = 28, 28 # 卷积滤镜的个数 nb_filters = 32 # 最大池化,池化核大小 pool_size = (2, 2) # 卷积核大小 kernel_size = (3, 3) (X_train, y_train), (X_test, y_test) = mnist.load_data(os.path.join(root_path, "data", "mnist", "mnist.npz")) if K.image_dim_ordering() == 'th': # 使用 Theano 的顺序:(conv_dim1, channels, conv_dim2, conv_dim3) X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols) X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: # 使用 TensorFlow 的顺序:(conv_dim1, conv_dim2, conv_dim3, channels) X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) model = keras.models.Sequential() # 可分离卷积 # model.add(SeparableConv2D(nb_filters, kernel_size, padding='valid', activation="relu", # input_shape=input_shape, data_format="channels_last")) model.add(Convolution2D(nb_filters, kernel_size, padding='valid', input_shape=input_shape, data_format="channels_last", activation="relu")) model.add(Convolution2D(nb_filters, kernel_size, padding="valid", activation="relu", data_format="channels_last")) model.add(MaxPooling2D(pool_size=pool_size)) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation="relu")) model.add(Dropout(0.25)) model.add(Dense(nb_classes, activation="softmax")) model.summary() # keras.utils.vis_utils.plot_model(model, to_file="keras_mnist_cnn.png") model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) os.makedirs(os.path.join(root_path, "tmp", "mnist", "logs"), exist_ok=True) tensorBoard_callback = keras.callbacks.TensorBoard(os.path.join(root_path, "tmp", "mnist", "logs"), batch_size=batch_size, write_images=True, write_graph=True) os.makedirs(os.path.join(root_path, "tmp", "mnist", "models"), exist_ok=True) model_checkpoint = keras.callbacks.ModelCheckpoint( os.path.join(root_path, "tmp", "mnist", "models", "mnist_model_{epoch:02d}-{val_acc:.4f}.h5"), save_best_only=False, save_weights_only=False, monitor='val_acc') model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1, validation_data=(X_test, Y_test), callbacks=[tensorBoard_callback, model_checkpoint]) test_loss, test_acc = model.evaluate(X_test, Y_test, verbose=1) logger.info('Test accuracy: {0} {1}'.format(test_loss, test_acc)) predictions = model.predict(X_test) predictions = np.argmax(predictions, 1) labels = np.argmax(Y_test, 1) for i in range(10): logger.info("predict:{0} , label:{1}".format(predictions[i], labels[i]))