Exemplo n.º 1
0
                   count=n,
                   stride=(2, 2))(conv3)  # "Stage 3 (spatial size: 8x8)"

    net = tflearn.batch_normalization(conv4)
    net = tflearn.activation(net, 'twins_relu')
    net = tflearn.avg_pool_2d(net, 8)
    #net = tflearn.avg_pool_2d(net, kernel_size=8, strides=1, padding='same')
    net = tflearn.fully_connected(net, 10, activation='softmax')

    return net


if __name__ == '__main__':
    net = create_model()
    mom = tflearn.Momentum(0.0001,
                           lr_decay=0.1,
                           decay_step=1000000,
                           staircase=True)
    net = tflearn.regression(net,
                             optimizer=mom,
                             loss='categorical_crossentropy')
    model = tflearn.DNN(net, tensorboard_verbose=1)

    model.load(
        "/home/lfwin/my_tmp/tflearn_logs/cifar10_WRN2_tReLU_130/model.tfl")

    model.fit(X,
              Y,
              n_epoch=10,
              shuffle=True,
              validation_set=(testX, testY),
              show_metric=True,
Exemplo n.º 2
0
img_prep.add_custom_preprocessing(my_func)

# Building Residual Network
net = tflearn.input_data(shape=[None, 32, 32, 3], data_preprocessing=img_prep)
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
loss = tflearn.fully_connected(net, 3, activation='softmax')
opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
network = tflearn.regression(loss,
                             optimizer=opt,
                             learning_rate=0.01,
                             loss='categorical_crossentropy')
# Training
model = tflearn.DNN(
    network,
    checkpoint_path='C:/Users/Administrator/Desktop/Resnet_test/model_resnext',
    max_checkpoints=1,
    tensorboard_verbose=3,
    tensorboard_dir='C:/Users/Administrator/Desktop/Resnet_test',
    best_checkpoint_path=
    'C:/Users/Administrator/Desktop/Resnet_test/model_resnet')

model.fit(X,
img_aug = tflearn.ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_crop([48, 48], padding=4)

# Building Residual Network
net = tflearn.input_data(shape=[None, 48, 48, 1],
                         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, 7, activation='softmax')
opt = tflearn.Momentum(learning_rate=0.1, lr_decay=0.1, decay_step=32000, staircase=True , momentum=0.9)
net = tflearn.regression(net, optimizer=opt,
                         loss='categorical_crossentropy')
# Training
model = tflearn.DNN(net, checkpoint_path='models/model_resnet_emotion',
                    max_checkpoints=20, tensorboard_verbose=0,
                    clip_gradients=0.)

model.fit(X, Y, n_epoch=150, validation_set=(X_test, Y_test),
              snapshot_epoch=False, snapshot_step=500,
              show_metric=True, batch_size=128, shuffle=True,
              run_id='resnext_emotion')
Exemplo n.º 4
0
		net = tflearn.residual_block(net,1,256,downsample=True)
		# [12,12,256]
		net = tflearn.residual_block(net,5,256)
		# [12,12,256]
		net = tflearn.residual_block(net,1,512,downsample=True)
		# [6,6,512]
		net = tflearn.residual_block(net,2,512)
		# [6,6,512]
		net = tflearn.global_avg_pool(net)
		# [512]
		fully_connect = tflearn.fully_connected(net,HIDDEN,activation='relu')
		# [1000]
		net = tflearn.fully_connected(fully_connect,LEN_OUT,activation='softmax')
		# [7211]
		#mom = tflearn.SGD(0.1,lr_decay=0.1,decay_step=3086 * 20)
		mom = tflearn.Momentum(0.01,lr_decay=0.1,decay_step=int(395000 / BATCH_SIZE) * 10,staircase=True)
		reg = tflearn.regression(net,optimizer=mom,loss='categorical_crossentropy')
		model = tflearn.DNN(reg,checkpoint_path='models/{}'.format(MODEL_FILE),max_checkpoints=100,session=sess)
elif NET_TYPE == 'resnet50' or NET_TYPE == 'resnet101' or NET_TYPE == 'resnet152':
	with tf.device("/gpu:{}".format(GPU_CORE)):
		net = tflearn.input_data(shape=[None,IMG_SIZE,IMG_SIZE,3])
		net = tflearn.conv_2d(net,64,7,2, regularizer='L2', weight_decay=0.0001)
		# [64,64,32]
		net = tflearn.max_pool_2d(net,3,2)
		# [32,32,32]
		net = tflearn.residual_bottleneck(net,3,64,256)
		# [32,32,64]
		net = tflearn.residual_bottleneck(net,1,128,512,downsample=True)
		# [16,16,128]
		net = tflearn.residual_bottleneck(net,3,128,512)
		# [16,16,128]
Exemplo n.º 5
0
def return2img():

    n = 5
    # 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)
    inp = tflearn.input_data(shape=[None, 32, 32, 1],
                             data_preprocessing=img_prep,
                             data_augmentation=img_aug,
                             name='input')

    conv1_1 = tflearn.conv_2d(inp, 64, 3, activation='relu', name="conv1_1")
    conv1_2 = tflearn.conv_2d(conv1_1,
                              64,
                              3,
                              activation='relu',
                              name="conv1_2")
    pool1 = tflearn.max_pool_2d(conv1_2, 2, strides=2)

    conv2_1 = tflearn.conv_2d(pool1, 128, 3, activation='relu', name="conv2_1")
    conv2_2 = tflearn.conv_2d(conv2_1,
                              128,
                              3,
                              activation='relu',
                              name="conv2_2")
    pool2 = tflearn.max_pool_2d(conv2_2, 2, strides=2)

    conv3_1 = tflearn.conv_2d(pool2, 256, 3, activation='relu', name="conv3_1")
    conv3_2 = tflearn.conv_2d(conv3_1,
                              256,
                              3,
                              activation='relu',
                              name="conv3_2")
    conv3_3 = tflearn.conv_2d(conv3_2,
                              256,
                              3,
                              activation='relu',
                              name="conv3_3")
    pool3 = tflearn.max_pool_2d(conv3_3, 2, strides=2)

    conv4_1 = tflearn.conv_2d(pool3, 512, 3, activation='relu', name="conv4_1")
    conv4_2 = tflearn.conv_2d(conv4_1,
                              512,
                              3,
                              activation='relu',
                              name="conv4_2")
    conv4_3 = tflearn.conv_2d(conv4_2,
                              512,
                              3,
                              activation='relu',
                              name="conv4_3")
    pool4 = tflearn.max_pool_2d(conv4_3, 2, strides=2)
    conv5_1 = tflearn.conv_2d(pool4, 512, 3, activation='relu', name="conv5_1")
    conv5_2 = tflearn.conv_2d(conv5_1,
                              512,
                              3,
                              activation='relu',
                              name="conv5_2")
    conv5_3 = tflearn.conv_2d(conv5_2,
                              512,
                              3,
                              activation='relu',
                              name="conv5_3")
    pool5 = tflearn.max_pool_2d(conv5_3, 2, strides=2)

    fc6 = tflearn.fully_connected(pool5, 4096, activation='relu', name="fc6")
    fc6_dropout = tflearn.dropout(fc6, 0.5)

    fc7 = tflearn.fully_connected(fc6_dropout,
                                  4096,
                                  activation='relu',
                                  name="fc7")
    fc7_droptout = tflearn.dropout(fc7, 0.5)

    fc8 = tflearn.fully_connected(fc7_droptout,
                                  3,
                                  activation='softmax',
                                  name="fc8")

    mm = tflearn.Momentum(learning_rate=0.01,
                          momentum=0.9,
                          lr_decay=0.1,
                          decay_step=1000)

    network = tflearn.regression(fc8,
                                 optimizer=mm,
                                 loss='categorical_crossentropy',
                                 restore=False)
    # Training
    model = tflearn.DNN(network)
    model.load("model_resnet_+-------cifar10-14000")
    img = load_img_label('/home/bai/最新数据/验证数据/经过预处理的原始图像')
    livermask = load_mask('/home/bai/最新数据/验证数据/mask')
    abc = []
    np.array(abc)
    for i in range(img.shape[0]):
        a = np.zeros((512, 512, 3, 3),
                     dtype=np.float32)  #最后一项表示第几个分割方式,倒数第二项表示预测的三个结果
        for j in range(0, 3):
            this_mask = livermask[i, :, :, j]
            this_mask_num = int(np.max(this_mask))
            for k in range(1, this_mask_num + 1):
                if os.path.exists('/home/bai/最新数据/验证数据/超像素块/' + str(i) + '_' +
                                  str(j) + '_' + str(k) + '.jpg'):
                    this_img = io.imread('/home/bai/最新数据/验证数据/超像素块/' + str(i) +
                                         '_' + str(j) + '_' + str(k) + '.jpg')
                    this_img = np.reshape(this_img, (1, 32, 32, 1))
                    this_img = this_img.astype(np.float32)
                    result = model.predict(this_img)
                    # print(result)
                    abc.append(result)
                    thisPartLoc = np.where(this_mask == k)
                    for num in range(len(thisPartLoc[1])):
                        a[thisPartLoc[0][num], thisPartLoc[1][num], 0,
                          j] = result[0, 0]
                        a[thisPartLoc[0][num], thisPartLoc[1][num], 1,
                          j] = result[0, 1]
                        a[thisPartLoc[0][num], thisPartLoc[1][num], 2,
                          j] = result[0, 2]
        b = np.max(a, axis=3)
        final = np.argmax(b, axis=2)
        misc.imsave(
            '/home/bai/PycharmProjects/DeepRESIDUALNETWORKS/result/' + str(i) +
            '.jpg', final)
Exemplo n.º 6
0
    def create(message, keep_existing):
        print('creating network...')
        number_of_hidden_layers = message['numberOfHiddenLayers']
        activation_function = message['activationFunction'].encode(
            'ascii', 'ignore')
        input_length = int(message['inputLength'])
        output_length = int(message['outputLength'])
        network_file = message['networkFilePath']
        classify = message['classify']
        learning_rate = message['learningRate']
        bias = message['bias']
        use_dropout = message['useDropout']
        dropout = message['dropout']
        has_custom_shape = message['customShape']
        shape = message['shape']

        tf.reset_default_graph()
        input_layer = tflearn.input_data(shape=[None, input_length],
                                         name='input')
        layer_to_connect_to = input_layer
        layer_activation = None

        if activation_function != 'none':
            layer_activation = activation_function

        for x in range(0, int(number_of_hidden_layers)):
            layer_to_connect_to = tflearn.fully_connected(
                layer_to_connect_to,
                input_length if not has_custom_shape else shape[x],
                weights_init='uniform',
                name='dense' + str(x),
                activation=layer_activation,
                bias=bias,
                regularizer='L2')
            if use_dropout:
                layer_to_connect_to = tflearn.dropout(layer_to_connect_to,
                                                      dropout)

        finished_network = tflearn.fully_connected(
            layer_to_connect_to,
            output_length,
            regularizer='L2',
            activation='softmax' if classify else 'linear',
            name='output',
            bias=bias)

        optimizer = 'adam'
        if 'SGD' == message['optimizer']:
            optimizer = tflearn.SGD(learning_rate=learning_rate)
        elif 'RMSprop' == message['optimizer']:
            optimizer = tflearn.RMSProp(learning_rate=learning_rate)
        elif 'Momentum' == message['optimizer']:
            optimizer = tflearn.Momentum(learning_rate=learning_rate)

        network_graph = tflearn.regression(
            finished_network,
            optimizer=optimizer,
            learning_rate=learning_rate,
            loss='categorical_crossentropy' if classify else 'mean_square',
            metric='default' if classify else 'R2',
        )

        model = tflearn.DNN(network_graph)
        print('network created.')

        if not keep_existing:
            save_network(model, network_file)

        return model
Exemplo n.º 7
0
# Building deep neural network
input_layer = tflearn.input_data(shape=[None, 11])
dense1 = tflearn.fully_connected(input_layer, 32, activation=activation_func,
                                 regularizer='L2')
dense2 = tflearn.fully_connected(dense1, 32, activation=activation_func,
                                 regularizer='L2')
dense3 = tflearn.fully_connected(dense2, 32, activation=activation_func,
                                 regularizer='L2')
dense4 = tflearn.fully_connected(dense3, 32, activation=activation_func,
                                 regularizer='L2')
dense5 = tflearn.fully_connected(dense4, 32, activation=activation_func,
                                 regularizer='L2')
output_layer = tflearn.fully_connected(dense5, 1, activation='linear')

# Regression using SGD + momentum with learning rate decay
sgd = tflearn.Momentum(learning_rate=0.001, lr_decay=0.96, decay_step=200)

net = tflearn.regression(output_layer, optimizer=sgd, loss='mean_square', metric='R2')

# Training
model = tflearn.DNN(net, tensorboard_verbose=0)

model.fit(X, Y, n_epoch=1000, validation_set=0.2,
          show_metric=True, run_id="dense_model")

predictY = model.predict(X)

# Plot the results
steps_in_future = 1
plt.figure()
plt.title('Prediction of Max Temperature (degrees Celsius)')
Exemplo n.º 8
0
tflearn.config.init_training_mode()
input_layer = tflearn.input_data([None, IMG_SIZE, IMG_SIZE, 3])
net = tflearn.conv_2d(input_layer, 64, 7, strides=2)
net = tflearn.max_pool_2d(net, 3, strides=2)
net = tflearn.residual_block(net, 3, 64)
net = tflearn.residual_block(net, 1, 128, downsample=True)
net = tflearn.residual_block(net, 3, 128)
net = tflearn.residual_block(net, 1, 256, downsample=True)
net = tflearn.residual_block(net, 5, 256)
net = tflearn.residual_block(net, 1, 512, downsample=True)
net = tflearn.residual_block(net, 2, 512)
net = tflearn.global_avg_pool(net)
fully_connected = tflearn.fully_connected(net, HIDDEN, activation="relu")
result = tflearn.fully_connected(fully_connected, 7211, activation='softmax')
mom = tflearn.Momentum(0.01,
                       lr_decay=0.1,
                       decay_step=int(395000 / BATCH_SIZE) * 300098,
                       staircase=True)
net = tflearn.regression(result,
                         optimizer=mom,
                         loss="categorical_crossentropy")
model = tflearn.DNN(net,
                    checkpoint_path='models/model',
                    session=sess,
                    max_checkpoints=100,
                    tensorboard_verbose=0)

# configure for tf session
config_box = None
# session for tensorflow boxing model
sess_box = None
# mtcnn tool for face detection
Exemplo n.º 9
0
                                nb_blocks=1,
                                out_channels=32,
                                cardinality=32,
                                downsample=True)  # 残差虚线

network = tflearn.resnext_block(network,
                                nb_blocks=n,
                                out_channels=64,
                                cardinality=32)
network = tflearn.resnext_block(network,
                                nb_blocks=1,
                                out_channels=32,
                                cardinality=32,
                                downsample=True)  # 残差虚线

network = tflearn.batch_normalization(network)  # 标准化
network = tflearn.activation(network, 'relu')  # 镶嵌激活函数
network = tflearn.global_avg_pool(network)  # 镶嵌平均池化
# 全连接(输出)
network = tflearn.fully_connected(network, 10, activation='softmax')
# 设置优化器的参数
# 动态学习率,初始值为0.1,每decay_step时乘以lr_decay
opt = tflearn.Momentum(learning_rate=0.1, lr_decay=0.1, decay_step=32000)
network = tflearn.regression(network, optimizer=opt)

# 训练
model = tflearn.DNN(network,
                    checkpoint_path='./model/resnet/model_resnet',
                    tensorboard_dir='./logs')
model.fit(X, Y, n_epoch=200, validation_set=(X_test, Y_test), batch_size=128)
Exemplo n.º 10
0
    # Building Residual Network
    net = tl.input_data(shape=[None, 42, 42, 3])
    net = tl.conv_2d(net, 32, 3)
    net = tl.batch_normalization(net)
    net = tl.activation(net, 'relu')
    net = tl.shallow_residual_block(net, 4, 32, regularizer='L2')
    net = tl.shallow_residual_block(net,
                                    1,
                                    32,
                                    downsample=True,
                                    regularizer='L2')
    net = tl.shallow_residual_block(net, 4, 64, regularizer='L2')
    net = tl.shallow_residual_block(net,
                                    1,
                                    64,
                                    downsample=True,
                                    regularizer='L2')
    net = tl.shallow_residual_block(net, 5, 64, regularizer='L2')
    net = tl.global_avg_pool(net)

    # Regression
    net = tl.fully_connected(net, 9, activation='softmax')
    mom = tl.Momentum(0.1, lr_decay=0.1, decay_step=16000, staircase=True)
    net = tl.regression(net, optimizer=mom, loss='categorical_crossentropy')
    # Training
    model = tl.DNN(net)
    model.load('resnet_analysis-172500')
    pred_y = model.predict(X)
    print(pred_y)
Exemplo n.º 11
0
CWD_PATH = os.getcwd()
from tflearn.data_utils import build_hdf5_image_dataset

network = tflearn.input_data(shape=[None, 128, 128, 3])
network = tflearn.batch_normalization(network)
network = conv_2d(network, 32, 3, activation='linear')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='linear')
network = conv_2d(network, 64, 3, activation='linear')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='linear')
network = dropout(network, 0.7)
network = tflearn.batch_normalization(network)
network = fully_connected(network, 9, activation='sigmoid')
optimizer = tflearn.Momentum(0.005)
network = regression(network, optimizer=optimizer, loss='binary_crossentropy')

model = tflearn.DNN(network, tensorboard_verbose=0, clip_gradients=0.)

model.load('./single_prediction_3.tfl')


def encode(data):
    class MyEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, np.integer):
                return int(obj)
            elif isinstance(obj, np.floating):
                return float(obj)
            elif isinstance(obj, np.ndarray):
Exemplo n.º 12
0
def train_nn_tflearn(data_handler, num_epochs=50):

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)

    batch_size = data_handler.mini_batch_size

    img_prep = tflearn.ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    img_aug = tflearn.ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25)
    img_aug.add_random_crop([32, 32], padding=4)

    x = tflearn.input_data(shape=[None, 32, 32, 3],
                           dtype='float',
                           data_preprocessing=img_prep,
                           data_augmentation=img_aug)
    # x = tf.placeholder('float', [None, 32, 32, 3])
    #y = tf.placeholder('float', [None, 10])

    # test_data, test_labels = data_handler.get_test_data()
    # test_data = test_data.reshape([-1,32,32,3])

    ntrain = data_handler.train_size
    ntest = data_handler.meta['num_cases_per_batch']

    # from tflearn.datasets import cifar10
    # (X, Y), (X_test, Y_test) = cifar10.load_data(dirname="/home/hamza/meh/bk_fedora24/Documents/tflearn_example/cifar-10-batches-py")
    # X, Y = tflearn.data_utils.shuffle(X, Y)
    # Y = tflearn.data_utils.to_categorical(Y, 10)
    # Y_test = tflearn.data_utils.to_categorical(Y_test, 10)

    X, Y = data_handler.get_all_train_data()

    X, Y = tflearn.data_utils.shuffle(X, Y)

    X = np.dstack((X[:, :1024], X[:, 1024:2048], X[:, 2048:]))

    X = X / 255.0

    X = X.reshape([-1, 32, 32, 3])

    Y = tflearn.data_utils.to_categorical(Y, 10)

    X_test, Y_test = data_handler.get_test_data()

    X_test = np.dstack((X_test[:, :1024], X_test[:, 1024:2048], X_test[:,
                                                                       2048:]))

    X_test = X_test / 255.0

    X_test = X_test.reshape([-1, 32, 32, 3])

    #network = tflearn.regression(net3(x),optimizer='adam',loss='categorical_crossentropy',learning_rate=0.001)
    mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
    network = tflearn.regression(resnet1(x),
                                 optimizer=mom,
                                 loss='categorical_crossentropy')

    print np.shape(X)
    print np.shape(Y)
    print network

    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit(X,
              Y,
              n_epoch=50,
              shuffle=True,
              validation_set=(X_test, Y_test),
              show_metric=True,
              batch_size=data_handler.mini_batch_size,
              run_id='cifar10_cnn')
net = rb(net, 1, 32, downsample=True,activation=args['act'])
net = rb(net, n-1, 32,activation=args['act'])
net = rb(net, 1, 64, downsample=True,activation=args['act'])
net = rb(net, n-1, 64,activation=args['act'])

if block_mode.upper() == 'FULLPREACT' or block_mode.upper() == 'SUB':
    net = tflearn.batch_normalization(net)

if block_mode.upper() == 'PREACT' or block_mode.upper() == 'FULLPREACT' or block_mode.upper() == 'SUB':
    net = tflearn.activation(net,activation=args['act'])


net = tflearn.global_avg_pool(net)
# Regression
net = tflearn.fully_connected(net, 10, activation='softmax')
mom = tflearn.Momentum(args['lr'], 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='./checkpoints/', tensorboard_verbose=0,tensorboard_dir='log',
                    clip_gradients=0.)


model_name = str(layers)+'_layers'+'_'+block_mode.upper()

if suffix2 != None:
    model_name = model_name +'_'+suffix2[0]

model.fit(X, Y, n_epoch=EPOCHS, validation_set=(testX, testY),
          snapshot_epoch=True,
          show_metric=True, batch_size=128, shuffle=True,
Exemplo n.º 14
0
    def run(self):
        # Real-time pre-processing of the image data
        img_prep = ImagePreprocessing()
        img_prep.add_featurewise_zero_center()
        img_prep.add_featurewise_stdnorm()

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

        # Building Residual Network
        net = tflearn.input_data(shape=[None, 48, 48, 1],
                                 data_preprocessing=img_prep,
                                 data_augmentation=img_aug)
        net = tflearn.conv_2d(net,
                              nb_filter=16,
                              filter_size=3,
                              regularizer='L2',
                              weight_decay=0.0001)
        net = tflearn.residual_block(net, self.n, 16)
        net = tflearn.residual_block(net, 1, 32, downsample=True)
        net = tflearn.residual_block(net, self.n - 1, 32)
        net = tflearn.residual_block(net, 1, 64, downsample=True)
        net = tflearn.residual_block(net, self.n - 1, 64)
        net = tflearn.batch_normalization(net)
        net = tflearn.activation(net, 'relu')
        net = tflearn.global_avg_pool(net)

        # Regression
        net = tflearn.fully_connected(net, 7, activation='softmax')
        mom = tflearn.Momentum(learning_rate=0.1,
                               lr_decay=0.0001,
                               decay_step=32000,
                               staircase=True,
                               momentum=0.9)
        net = tflearn.regression(net,
                                 optimizer=mom,
                                 loss='categorical_crossentropy')

        self.model = tflearn.DNN(net,
                                 checkpoint_path='models/model_resnet_emotion',
                                 max_checkpoints=10,
                                 tensorboard_verbose=0,
                                 clip_gradients=0.)

        self.model.load('current_model/model_resnet_emotion-42000')

        face_cascade = cv2.CascadeClassifier(
            'haarcascade_frontalface_default.xml')
        cap = cv2.VideoCapture(0)

        while True:
            ret, img = cap.read()
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
            for (x, y, w, h) in faces:
                cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
                roi_gray = gray[y:y + h, x:x + w]
                roi_color = img[y:y + h, x:x + w]
                self.process_image(roi_gray, img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()
Exemplo n.º 15
0
	def __init__(self, learning_rate, decay_rate, momentum, settings):

		tf.reset_default_graph()

		self.learning_rate = learning_rate
		self.decay_rate = decay_rate
		self.momentum = momentum

		if settings[0] == "1":
			self.preprocessing = True
		else:
			self.preprocessing = False

		if settings[1] == "1":
			self.doRegularizer = True
		else:
			self.doRegularizer = False

		if settings[2] == "1":
			self.dropout = True
		else:
			self.dropout = False

		if settings[3] == "1":
			self.activation = "sigmoid"
		else:
			self.activation = "relu6"

		if settings[4] == "1":
			self.loss = "mean_square"
		else:
			self.loss = "softmax_categorical_crossentropy"

		if settings[5] == "1":
			self.optimization = "adaDelta"
		else:
			self.optimization = "momentum"

		# Reading in data



		self.targets = tf.placeholder(tf.float32, [None, self.output_nodes])


		input_layer = None

		if self.preprocessing:
			data_processing = tflearn.data_preprocessing.DataPreprocessing()
			data_processing.add_featurewise_stdnorm(std=0.2090549348314474)
			data_processing.add_featurewise_zero_center(mean=0.3040620249839109)
			input_layer = tflearn.input_data(shape=[None, self.input_nodes], data_preprocessing=data_processing)

		else:
			input_layer = tflearn.input_data(shape=[None, self.input_nodes])

		h1 = None
		h2 = None
		h3 = None
		h4 = None
		output_layer = None

		if self.doRegularizer:
			# 1st Hidden Layer
			h1 = tflearn.fully_connected(input_layer, self.h1_nodes, activation=self.activation, regularizer=self.regularization, weight_decay=1/(8 * self.batch_size * self.input_nodes*self.h1_nodes))

			if self.dropout:
				h1 = tflearn.dropout(h1, self.dropout_value)

			# 2nd Hidden Layer
			h2 = tflearn.fully_connected(h1, self.h2_nodes, activation=self.activation, regularizer=self.regularization, weight_decay=1/(8 * self.batch_size * self.h1_nodes*self.h2_nodes))

			if self.dropout:
				h2 = tflearn.dropout(h2, self.dropout_value)

			'''
			h3 = tflearn.fully_connected(h2, self.h3_nodes, activation=self.activation, regularizer=self.regularization, weight_decay=1/(8 * self.batch_size * self.h2_nodes*self.h3_nodes))

			if self.dropout:
				h3 = tflearn.dropout(h3, self.dropout_value)

			# 2nd Hidden Layer
			h4 = tflearn.fully_connected(h3, self.h4_nodes, activation=self.activation, regularizer=self.regularization, weight_decay=1/(8 * self.batch_size * self.h3_nodes*self.h4_nodes))

			if self.dropout:
				h4 = tflearn.dropout(h4, self.dropout_value)
			'''
			# Output Layer
			output_layer = tflearn.fully_connected(h2, self.output_nodes, activation=self.activation, regularizer=self.regularization, weight_decay=1/(8 * self.batch_size * self.h2_nodes*self.output_nodes))
		else:
			# 1st Hidden Layer
			h1 = tflearn.fully_connected(input_layer, self.h1_nodes, activation=self.activation)

			if self.dropout:
				h1 = tflearn.dropout(h1, self.dropout_value)

			# 2nd Hidden Layer
			h2 = tflearn.fully_connected(h1, self.h2_nodes, activation=self.activation)

			'''
			if self.dropout:
				h2 = tflearn.dropout(h2, self.dropout_value)

			h3 = tflearn.fully_connected(h2, self.h3_nodes, activation=self.activation)

			if self.dropout:
				h3 = tflearn.dropout(h3, self.dropout_value)

			# 2nd Hidden Layer
			h4 = tflearn.fully_connected(h3, self.h4_nodes, activation=self.activation)

			if self.dropout:
				h4 = tflearn.dropout(h4, self.dropout_value)
			'''
			# Output Layer
			output_layer = tflearn.fully_connected(h2, self.output_nodes, activation=self.activation)

		# Learning
		# Optimizer
		opt = None
		if self.optimization == "momentum":
			opt = tflearn.Momentum(learning_rate=self.learning_rate, lr_decay=self.decay_rate, momentum=self.momentum)
		else:
			opt = tflearn.AdaDelta(learning_rate=self.learning_rate, rho=self.decay_rate)

		r = tflearn.regression(output_layer, placeholder=self.targets, optimizer=opt, loss=self.loss)
		self.model = tflearn.DNN(r)