rb = rb_sub
elif block_mode.upper() == 'PREACT':
    rb = rb_preactv
elif block_mode.upper() == 'REFERENCE':
    rb = rb_reference

(X, Y), (testX, testY) = cifar10.load_data()
Y = tflearn.data_utils.to_categorical(Y,nb_classes=10)
testY = tflearn.data_utils.to_categorical(testY,nb_classes=10)

# 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)

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

net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
net = rb(net, n, 16,activation=args['act'])
def train_nn_tflearn(data_handler, num_epochs=50):

    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
    #tflearn.init_graph(gpu_memory_fraction=0.5)

    batch_size = data_handler.mini_batch_size
    classes = data_handler.num_labels

    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, 128, 128, 1],
                           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[:, :128*128], X[:, 128*128:]))
    X = X[:, :128 * 128]

    #X = X/255.0

    #X = X.reshape([-1,128,128,2])
    X = X.reshape([-1, 128, 128, 1])

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

    X_test, Y_test = data_handler.get_test_data()

    #X_test = np.dstack((X_test[:, :128*128], X_test[:, 128*128:]))
    X_test = X_test[:, :128 * 128]
    #X_test = X_test/255.0

    #X_test = X_test.reshape([-1,128,128,2])
    X_test = X_test.reshape([-1, 128, 128, 1])
    #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='sgd',loss='categorical_crossentropy')
    network = tflearn.regression(resnet1(x, classes),
                                 optimizer='adam',
                                 loss='categorical_crossentropy')
    print np.shape(X)
    print np.shape(Y)
    print network

    if not os.path.exists('/tmp/tflearn/checkpoints'):
        os.makedirs('/tmp/tflearn/checkpoints')

    model = tflearn.DNN(network,
                        tensorboard_verbose=3,
                        checkpoint_path='/tmp/tflearn/checkpoints/',
                        best_checkpoint_path='best/',
                        best_val_accuracy=0.90)
    model.fit(X,
              Y,
              n_epoch=num_epochs,
              shuffle=True,
              validation_set=(X_test, Y_test),
              show_metric=True,
              batch_size=data_handler.mini_batch_size,
              run_id='mstar_cnn')
Beispiel #3
0
def construct_dnn():
    tf.reset_default_graph()
    tflearn.init_graph(num_cores=4, gpu_memory_fraction=0.9)
    tflearn.config.init_training_mode()
    img_aug = tflearn.ImageAugmentation()
    img_aug.add_random_90degrees_rotation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    input_layer = tflearn.input_data(shape=[None, 15, 15, 3],
                                     data_augmentation=img_aug)
    # block 1
    net = tflearn.conv_2d(input_layer, 256, 3, activation=None)
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, activation='relu')
    # res block 1
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # res block 2
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # res block 3
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # res block 4
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # res block 5
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # res block 6
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # res block 7
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # res block 8
    tmp = tflearn.conv_2d(net, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    tmp = tflearn.activation(tmp, activation='relu')
    tmp = tflearn.conv_2d(tmp, 256, 3, activation=None)
    tmp = tflearn.batch_normalization(tmp)
    net = tflearn.activation(net + tmp, activation='relu')
    # value head
    net = tflearn.conv_2d(net, 1, 1, activation=None)
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, activation='relu')
    net = tflearn.fully_connected(net, 256, activation='relu')
    final = tflearn.fully_connected(net, 1, activation='tanh')
    # optmizer
    sgd = tflearn.optimizers.SGD(learning_rate=0.01,
                                 lr_decay=0.95,
                                 decay_step=300000)
    regression = tflearn.regression(final,
                                    optimizer=sgd,
                                    loss='mean_square',
                                    metric='R2')
    model = tflearn.DNN(regression)
    return model
Beispiel #4
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()
# Image Preprocessing Methods Instantiation
cnn_img_prep = ImagePreprocessing()

if (cnn_compute_mean_std == 1):
    cnn_img_prep.add_featurewise_zero_center(
    )  # Zero Center (With mean computed over the whole dataset)
    cnn_img_prep.add_featurewise_stdnorm(
    )  # STD Normalization (With std computed over the whole dataset)
if (cnn_compute_mean_std == 0):
    cnn_img_prep.add_featurewise_zero_center(per_channel=True,
                                             mean=cnn_mean_vector_precomputed)
    cnn_img_prep.add_featurewise_stdnorm(per_channel=True,
                                         std=cnn_std_vector_precomputed)

cnn_img_aug = tflearn.ImageAugmentation()  # Real-time data augmentation
cnn_img_aug.add_random_flip_leftright()  # Random flip an image
cnn_img_aug.add_random_crop([cnn_image_shape[0], cnn_image_shape[1]])

####################################################################################################################
# Call the network architecture
####################################################################################################################
if (comm_loss_type == 0):
    cnn_loss_layer_activation = 'softmax'
if (comm_loss_type == 1):
    cnn_loss_layer_activation = 'sigmoid'

# Call the network architectures
if (cnn_arch_choice == 0):
    net = sslca.load_alexnet(cnn_image_shape, cnn_img_prep, cnn_img_aug,
                             cnn_keep_probability, num_output_classes,