Beispiel #1
0
    def build_network(self):
        """
        Build the convnet.
        Input is 48x48
        3072 nodes in fully connected layer
        """
        # Real-time data preprocessing
        img_prep = tflearn.ImagePreprocessing()
        img_prep.add_featurewise_zero_center(
            per_channel=True, mean=[0.53990436, 0.4405486, 0.39328504])

        # Real-time data augmentation
        img_aug = tflearn.ImageAugmentation()
        img_aug.add_random_flip_leftright()
        img_aug.add_random_crop([49, 49], padding=4)

        # Building Residual Network
        self.network = tflearn.input_data(shape=[None, 49, 49, 3],
                                          data_preprocessing=img_prep,
                                          data_augmentation=img_aug)
        self.network = tflearn.conv_2d(self.network,
                                       16,
                                       3,
                                       regularizer='L2',
                                       weight_decay=0.0001)
        self.network = tflearn.resnext_block(self.network, 5, 16, 32)
        self.network = tflearn.resnext_block(self.network,
                                             1,
                                             32,
                                             32,
                                             downsample=True)
        self.network = tflearn.resnext_block(self.network, 4, 32, 32)
        self.network = tflearn.resnext_block(self.network,
                                             1,
                                             64,
                                             32,
                                             downsample=True)
        self.network = tflearn.resnext_block(self.network, 4, 64, 32)
        self.network = tflearn.batch_normalization(self.network)
        self.network = tflearn.activation(self.network, 'relu')
        self.network = tflearn.global_avg_pool(self.network)
        # Regression
        self.network = tflearn.fully_connected(self.network,
                                               11,
                                               activation='softmax')
        opt = tflearn.Momentum(0.1,
                               lr_decay=0.1,
                               decay_step=32000,
                               staircase=True)
        self.network = tflearn.regression(self.network,
                                          optimizer=opt,
                                          loss='categorical_crossentropy')
        # Training
        self.model = tflearn.DNN(self.network,
                                 checkpoint_path='Snapshots/model_resnext',
                                 max_checkpoints=10,
                                 tensorboard_verbose=0,
                                 tensorboard_dir='Logs/',
                                 clip_gradients=0.)
        self.load_model()
Beispiel #2
0
def network(img_shape, name, LR):

    # Building Residual Network
    network = tflearn.input_data(shape=img_shape, name=name)
    network = tflearn.conv_2d(network,
                              16,
                              3,
                              regularizer='L2',
                              weight_decay=0.0001)
    network = tflearn.resnext_block(network, n, 16, 32)
    network = tflearn.resnext_block(network, 1, 32, 32, downsample=True)
    network = tflearn.resnext_block(network, n - 1, 32, 32)
    network = tflearn.resnext_block(network, 1, 64, 32, downsample=True)
    network = tflearn.resnext_block(network, n - 1, 64, 32)
    network = tflearn.batch_normalization(network)
    network = tflearn.activation(network, 'relu')
    network = tflearn.global_avg_pool(network)
    # Regression
    network = tflearn.fully_connected(network, 2, activation='softmax')
    opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
    network = tflearn.regression(network,
                                 optimizer=opt,
                                 name='targets',
                                 loss='categorical_crossentropy')
    return network
Beispiel #3
0
def resnext_model(X, Y, testX, testY, n_epoch):
	n_input = len(X[0])
	n_classes = len(Y[0])
	X = np.reshape(X, (-1, 1, n_input, 1))
	testX = np.reshape(testX, (-1, 1, n_input, 1))
	net = tflearn.input_data(shape=[None, 1, n_input, 1])
	net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001, activation='selu')
	net = tflearn.resnext_block(net, n, 16, 32, activation='selu')
	net = tflearn.resnext_block(net, 1, 32, 32, downsample=True, activation='selu')
	net = tflearn.resnext_block(net, n-1, 32, 32, activation='selu')
	net = tflearn.resnext_block(net, 1, 64, 32, downsample=True, activation='selu')
	net = tflearn.resnext_block(net, n-1, 64, 32, activation='selu')
	net = tflearn.batch_normalization(net)
	net = tflearn.activation(net, 'selu')
	# net_p = tflearn.global_avg_pool(net, name='net_p')
	net_p = global_max_pool(net, name='net_p')
	# Regression
	net = tflearn.fully_connected(net_p, 2048, activation='selu')
	net = tflearn.dropout(net, 0.5)
	net = tflearn.fully_connected(net, 256, activation='selu')
	net = tflearn.dropout(net, 0.7)
	net = tflearn.fully_connected(net, n_classes, activation='softmax')
	# opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
	net = tflearn.regression(net, optimizer='adam',
	                         loss='categorical_crossentropy')
	# Training
	model = tflearn.DNN(net, checkpoint_path='../model/model_resnext_grid_adam',
	                    max_checkpoints=10, tensorboard_verbose=0,
	                    clip_gradients=0.)
	# model.load('../model/model_resnext_grid_adam-70000')
	model.fit(X, Y, n_epoch=n_epoch, validation_set=(testX[:], testY),
	          snapshot_epoch=False, snapshot_step=50000,
	          show_metric=True, batch_size=128, shuffle=True,
	          run_id='resnext')
	prob = model.predict(testX)
	# pdb.set_trace()

	# get the hidden layer value after global average pooling
	m2 = tflearn.DNN(net_p, session = model.session)

	feature_train = list()
	for i in range(4):
		feature_train_temp = m2.predict(X[i*4000:(i+1)*4000])
		feature_train += list(feature_train_temp)
	feature_train_temp = m2.predict(X[16000:])
	feature_train =  np.array(feature_train+list(feature_train_temp))

	feature_test = m2.predict(testX)
	
	# pdb.set_trace()
	return prob, feature_train, feature_test
Beispiel #4
0
def get_model(model_name):
    # First we load the network
    print("Setting up neural networks...")
    n = 18

    # Real-time data preprocessing
    print("Doing preprocessing...")
    img_prep = tflearn.ImagePreprocessing()
    img_prep.add_featurewise_zero_center(
        per_channel=True, mean=[0.573364, 0.44924123, 0.39455055])

    # Real-time data augmentation
    print("Building augmentation...")
    img_aug = tflearn.ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_crop([32, 32], padding=4)

    #Build the model (for 32 x 32)
    print("Shaping input data...")
    net = tflearn.input_data(shape=[None, 32, 32, 3],
                             data_preprocessing=img_prep,
                             data_augmentation=img_aug)
    net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)

    print("Carving Resnext blocks...")
    net = tflearn.resnext_block(net, n, 16, 32)
    net = tflearn.resnext_block(net, 1, 32, 32, downsample=True)
    net = tflearn.resnext_block(net, n - 1, 32, 32)
    net = tflearn.resnext_block(net, 1, 64, 32, downsample=True)
    net = tflearn.resnext_block(net, n - 1, 64, 32)

    print("Erroding Gradient...")
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, 'relu')
    net = tflearn.global_avg_pool(net)
    net = tflearn.fully_connected(net, 8, activation='softmax')
    opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
    net = tflearn.regression(net,
                             optimizer=opt,
                             loss='categorical_crossentropy')

    print("Structuring model...")
    model = tflearn.DNN(net, tensorboard_verbose=0, clip_gradients=0.)

    # Load the model from checkpoint
    print("Loading the model...")
    model.load(model_name)

    return model
Beispiel #5
0
def inference(x):
    n = 5
    net = tflearn.conv_2d(x, 16, 3, regularizer='L2', weight_decay=0.0001)
    net = tflearn.resnext_block(net, n, 16, 32)
    net = tflearn.resnext_block(net, 1, 32, 32, downsample=True)
    net = tflearn.resnext_block(net, n - 1, 32, 32)
    net = tflearn.resnext_block(net, 1, 64, 32, downsample=True)
    net = tflearn.resnext_block(net, n - 1, 64, 32)
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, 'relu')
    net = tflearn.global_avg_pool(net)
    # Regression
    net = tflearn.fully_connected(net, 5, activation=None)
    logits = net
    net = tflearn.activations.softmax(net)
    return logits, net
Beispiel #6
0
def ResNext1(network):
    # Residual blocks
    # 32 layers: n=5, 56 layers: n=9, 110 layers: n=18
    n = 5
    network = conv_2d(network, 16, 3, regularizer='L2', weight_decay=0.0001)
    network = resnext_block(network, n, 16, 32)
    network = resnext_block(network, 1, 32, 32, downsample=True)
    network = resnext_block(network, n - 1, 32, 32)
    network = resnext_block(network, 1, 32, 32, downsample=True)
    network = resnext_block(network, n - 1, 32, 32)
    network = batch_normalization(network)
    network = activation(network, 'relu')
    network = global_avg_pool(network)
    # Regression
    network = fully_connected(network, output_dim, activation='softmax')

    return network
Beispiel #7
0
def resnext(width, height, frame_count, lr, output=9, model_name = 'sentnet_color.model'):
    net = input_data(shape=[None, width, height, 3], name='input')
    net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
    net = tflearn.layers.conv.resnext_block(net, n, 16, 32)
    net = tflearn.resnext_block(net, 1, 32, 32, downsample=True)
    net = tflearn.resnext_block(net, n-1, 32, 32)
    net = tflearn.resnext_block(net, 1, 64, 32, downsample=True)
    net = tflearn.resnext_block(net, n-1, 64, 32)
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, 'relu')
    net = tflearn.global_avg_pool(net)
    # Regression
    net = tflearn.fully_connected(net, output, activation='softmax')
    opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
    net = tflearn.regression(net, optimizer=opt,
                             loss='categorical_crossentropy')

    model = tflearn.DNN(net,
                        max_checkpoints=0, tensorboard_verbose=0, tensorboard_dir='log')

    return model
Beispiel #8
0
def resnext(width, height, frame_count, lr, output=9, model_name = 'sentnet_color.model'):
    net = input_data(shape=[None, width, height, 3], name='input')
    net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
    net = tflearn.layers.conv.resnext_block(net, n, 16, 32)
    net = tflearn.resnext_block(net, 1, 32, 32, downsample=True)
    net = tflearn.resnext_block(net, n-1, 32, 32)
    net = tflearn.resnext_block(net, 1, 64, 32, downsample=True)
    net = tflearn.resnext_block(net, n-1, 64, 32)
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, 'relu')
    net = tflearn.global_avg_pool(net)
    # Regression
    net = tflearn.fully_connected(net, output, activation='softmax')
    opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
    net = tflearn.regression(net, optimizer=opt,
                             loss='categorical_crossentropy')

    model = tflearn.DNN(net,
                        max_checkpoints=0, tensorboard_verbose=0, tensorboard_dir='log')

    return model
def create_resnext_network( input_t, num_classes,
                            resnext_n, add_color_transfer):

    if add_color_transfer:
        x = cnn_utils.color_transform_layers(input_t)
    else:
        x = input_t

    x = conv_2d(x, 16, 3, regularizer='L2', weight_decay=0.0001)
    x = tflearn.resnext_block(x, resnext_n, 16, 32)
    x = tflearn.resnext_block(x, 1, 32, 32, downsample=True)
    x = tflearn.resnext_block(x, resnext_n - 1, 32, 32)
    x = tflearn.resnext_block(x, 1, 64, 32, downsample=True)
    x = tflearn.resnext_block(x, resnext_n - 1, 64, 32)
    x = tflearn.batch_normalization(x)
    x = tflearn.activation(x, 'relu')
    x = tflearn.global_avg_pool(x)

    x = fully_connected(x, num_classes, activation='softmax')

    return x
Beispiel #10
0
def network(img_shape, name, LR):

    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    #
    # # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_blur(sigma_max=3.0)
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_90degrees_rotation(rotations=[0, 2])

    # Building Residual Network
    network = tflearn.input_data(shape=img_shape,
                                 name=name,
                                 data_preprocessing=img_prep,
                                 data_augmentation=img_aug)
    network = tflearn.conv_2d(network,
                              16,
                              3,
                              regularizer='L2',
                              weight_decay=0.0001)
    network = tflearn.resnext_block(network, n, 16, 32)
    network = tflearn.resnext_block(network, 1, 32, 32, downsample=True)
    network = tflearn.resnext_block(network, n - 1, 32, 32)
    network = tflearn.resnext_block(network, 1, 64, 32, downsample=True)
    network = tflearn.resnext_block(network, n - 1, 64, 32)
    network = tflearn.batch_normalization(network)
    network = tflearn.activation(network, 'relu')
    network = tflearn.global_avg_pool(network)
    # Regression
    network = tflearn.fully_connected(network, 2, activation='softmax')
    opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
    network = tflearn.regression(network,
                                 optimizer=opt,
                                 name='targets',
                                 loss='categorical_crossentropy')
    return network
Beispiel #11
0
# Real-time data preprocessing
img_prep = tflearn.ImagePreprocessing()
img_prep.add_featurewise_zero_center(per_channel=True)

# Real-time data augmentation
img_aug = tflearn.ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_crop([32, 32], padding=4)

# Building Residual Network
net = tflearn.input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
# Residual blocks
net = tflearn.resnext_block(net, n, 16, 32)
net = tflearn.resnext_block(net, 1, 32, 32, downsample=True)
net = tflearn.resnext_block(net, n - 1, 32, 32)
net = tflearn.resnext_block(net, 1, 64, 32, downsample=True)
net = tflearn.resnext_block(net, n - 1, 64, 32)
net = tflearn.batch_normalization(net)
net = tflearn.activation(net, 'relu')
net = tflearn.global_avg_pool(net)
# Regression
net = tflearn.fully_connected(net, 10, activation='softmax')
mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy')

# Training
model = tflearn.DNN(net,
                    checkpoint_path='model_resnext_cifar10',
Beispiel #12
0
# Real-time data preprocessing
img_prep = tflearn.ImagePreprocessing()
img_prep.add_featurewise_zero_center(per_channel=True)

# Real-time data augmentation
img_aug = tflearn.ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_crop([32, 32], padding=4)

# Building Residual Network
net = tflearn.input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
net = tflearn.resnext_block(net, n, 16, 32)
net = tflearn.resnext_block(net, 1, 32, 32, downsample=True)
net = tflearn.resnext_block(net, n-1, 32, 32)
net = tflearn.resnext_block(net, 1, 64, 32, downsample=True)
net = tflearn.resnext_block(net, n-1, 64, 32)
net = tflearn.batch_normalization(net)
net = tflearn.activation(net, 'relu')
net = tflearn.global_avg_pool(net)
# Regression
net = tflearn.fully_connected(net, 10, activation='softmax')
opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
net = tflearn.regression(net, optimizer=opt,
                         loss='categorical_crossentropy')
# Training
model = tflearn.DNN(net, checkpoint_path='model_resnet_cifar10',
                    max_checkpoints=10, tensorboard_verbose=0,
Beispiel #13
0
# 构建网络
network = tflearn.input_data(shape=[None, 32, 32, 3],
                             data_augmentation=img_aug,
                             data_preprocessing=img_prep)

network = tflearn.conv_2d(network,
                          16,
                          3,
                          regularizer='l2',
                          weight_decay=0.0001)

# 载入残差结构
n = 3
network = tflearn.resnext_block(network,
                                nb_blocks=n,
                                out_channels=16,
                                cardinality=32)
network = tflearn.resnext_block(network,
                                nb_blocks=1,
                                out_channels=32,
                                cardinality=32,
                                downsample=True)  # 残差虚线

network = tflearn.resnext_block(network,
                                nb_blocks=n,
                                out_channels=32,
                                cardinality=32)
network = tflearn.resnext_block(network,
                                nb_blocks=1,
                                out_channels=32,
                                cardinality=32,