Пример #1
0
def builtModel(N_CLASSES, IMG_SIZE):
    tf.reset_default_graph()
    network = input_data(
        shape=[None, IMG_SIZE, IMG_SIZE, 1]
    )  # 1   #ĐIền 1 nếu là ảnh đen trắng, 3 nếu là ảnh màu ở thông số cuối cùng.

    network = conv_2d(network, 32, 3, activation='relu')  # 2
    network = max_pool_2d(network, 2)  # 3

    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = fully_connected(network, 1024, activation='relu')  # 4
    network = dropout(network, 0.8)  # 5

    network = fully_connected(network, N_CLASSES, activation='softmax')  # 6
    network = regression(network)

    model = tflearn.DNN(network)

    return model
Пример #2
0
    def __init__(self, sequence_length, num_classes, embeddings, num_filters, l2_reg_lambda=0.0, dropout=None):
        self.input_text = layers.input_data( (None, sequence_length), dtype=tf.int32)
        
        with tf.variable_scope('Embedding'):
            embeddings_var = tf.Variable(embeddings, name='W', dtype=tf.float32)
            embeddings_var = tf.concat([np.zeros((1, embeddings.shape[1]) ), embeddings_var[1:] ] , axis = 0)
            self.embeded_text = tf.gather(embeddings_var, self.input_text)
        
        net = self.embeded_text
        
        self.mask = tf.expand_dims(tf.cast(tf.not_equal(self.input_text, 0), tf.float32), axis = 2)
        if dropout is not None:
            dropout = map(float, dropout.split(',') )
        for num_filter in num_filters:
            net = layers.lstm(net, num_filter, return_seq=True, dropout=dropout)
            net = tf.transpose(tf.stack(net), (1, 0, 2) )

        features = tf.reduce_sum(net * self.mask, axis=1) / (tf.reduce_sum(self.mask, axis=1) + 1e-5)
        
        self.probas = layers.fully_connected(features, num_classes, activation='softmax', regularizer='L2', weight_decay=l2_reg_lambda)
        optimizer = tflearn.optimizers.Adam(learning_rate=0.001)
        self.train_op = layers.regression(
            self.probas, 
            optimizer=optimizer,
            batch_size=128)
Пример #3
0
 def __init__(self, sequence_length, num_classes, embeddings, num_filters, l2_reg_lambda=0.0, dropout=None, bn=False):
     self.input_text = layers.input_data( (None, sequence_length), dtype=tf.int32)
     
     with tf.variable_scope('Embedding'):
         embeddings_var = tf.Variable(embeddings, name='W', dtype=tf.float32)
         embeddings_var = tf.concat([np.zeros((1, embeddings.shape[1]) ), embeddings_var[1:] ] , axis = 0)
         self.embeded_text = tf.gather(embeddings_var, self.input_text)
     
     net = self.embeded_text
     for num_filter in num_filters:
         if bn:
             # , weights_init=tflearn.initializations.uniform(minval=-0.001, maxval=0.001)
             net = layers.conv_1d(net, num_filter, 3, padding='valid', activation='linear', bias=False)
             net = layers.batch_normalization(net)
             net = layers.activation(net, 'relu')
         else:
             net = layers.conv_1d(net, num_filter, 3, padding='valid', activation='relu', bias=True, regularizer='L2', weight_decay=l2_reg_lambda)
             
     if dropout is not None:
         net = layers.dropout(net, float(dropout) )
    
     features = layers.flatten( layers.max_pool_1d(net, net.shape.as_list()[1], padding='valid') )
     self.probas = layers.fully_connected(features, num_classes, activation='softmax', regularizer='L2', weight_decay=l2_reg_lambda)
     #optimizer = tflearn.optimizers.Momentum(learning_rate=0.1, momentum=0.9, lr_decay=0.2, decay_step=1000, staircase=True)
     optimizer = tflearn.optimizers.Adam(learning_rate=0.001)
     self.train_op = layers.regression(
         self.probas, 
         optimizer=optimizer,
         batch_size=128)
Пример #4
0
    def __init__(self,
                 max_document_length,
                 num_classes=2,
                 num_characters=71,
                 num_blocks=None,
                 char_vec_size=16,
                 weight_decay=2e-4):
        self.input_text = layers.input_data((None, max_document_length))
        self.target_label = tf.placeholder(shape=(None, num_classes),
                                           dtype=tf.float32)

        embeded_text = layers.embedding(self.input_text, num_characters,
                                        char_vec_size)

        top_feature = embeded_text
        filters = 64
        if num_blocks[0] == 0:
            self.block = (2, 2, 2, 2)
        else:
            self.block = num_blocks

        for i, num_block in enumerate(self.block):
            if i > 0:
                filters *= 2
                top_feature = layers.max_pool_1d(top_feature,
                                                 3,
                                                 strides=2,
                                                 padding='same')
            for block_i in range(num_block):
                top_feature = self.conv_block(top_feature, filters)

        pooled_feature = layers.flatten(
            layers.custom_layer(top_feature, self.kmax_pool_1d))
        fc1 = layers.fully_connected(pooled_feature,
                                     2048,
                                     activation='relu',
                                     regularizer='L2',
                                     weight_decay=weight_decay)
        fc2 = layers.fully_connected(fc1,
                                     2048,
                                     activation='relu',
                                     regularizer='L2',
                                     weight_decay=weight_decay)
        self.probas = layers.fully_connected(fc2,
                                             num_classes,
                                             activation='softmax',
                                             regularizer='L2',
                                             weight_decay=weight_decay)
        self.train_op = layers.regression(self.probas,
                                          placeholder=self.target_label)
Пример #5
0
def load_model():
    IMG_SIZE = 75
    N_CLASSES = 9
    LR = 0.001

    tf.reset_default_graph()

    network = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1]) #1

    network = conv_2d(network, 32, 3, activation='relu') #2
    network = max_pool_2d(network, 2) #3

    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = fully_connected(network, 1024, activation='relu') #4
    network = dropout(network, 0.8) #5

    network = fully_connected(network, N_CLASSES, activation='softmax')#6
    network = regression(network)

    model = tflearn.DNN(network) #7


    model.load('model/mymodel.tflearn')
    return model
Пример #6
0
    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)
    
    #4: Fully-connected layer
    # 1024: số lượng neuron
    # Activation function: ReLU
    network = fully_connected(network, 1024, activation='relu') #4
    network = dropout(network, 0.8) #5: Dropout 80%

    network = fully_connected(network, N_CLASSES, activation='softmax')#6: Fully-connected layer đại điện cho đầu ra (output). N_CLASSES: số output đầu ra. 
    # Activation function: softmax (để tổng xác suất đầu ra bằng 1)
    network = regression(network)

    model = tflearn.DNN(network) #7: #7: Xây dựng mô hình
    # Để dữ liệu đầu vào được trùng khớp với mô hình đã xây dưng, chúng ta cần phải đưa dữ liệu về định dạng phù hợp như sau
    train_x = train_x.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
    val_x = val_x.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
    test_x = test_x.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
    # Tương tự với label, đưa label về dạng onehot vector:
    original_test_y = test_y # được sử dụng để test ở bước sau
    train_y = to_categorical(train_y, N_CLASSES)
    val_y = to_categorical(val_y, N_CLASSES)
    test_y = to_categorical(test_y, N_CLASSES)
    # Bây giờ chúng ta cùng training:
    model.fit(train_x, train_y, n_epoch=N_EPOCHS, validation_set=(val_x, val_y), show_metric=True)
    # Chúng ta hãy lưu lại model đã train được như sau
    model.save('mymodel.tflearn')
Пример #7
0
    def __init__(self,
                 max_document_length,
                 num_classes=2,
                 num_characters=71,
                 char_vec_size=16,
                 weight_decay=2e-4,
                 optimizer='sgd',
                 dropout=None,
                 num_blocks=None):
        self.input_text = layers.input_data((None, max_document_length))
        self.target_label = tf.placeholder(shape=(None, num_classes),
                                           dtype=tf.float32)

        embeded_text = layers.embedding(self.input_text, num_characters,
                                        char_vec_size)
        mask = tf.cast(tf.not_equal(self.input_text, 0), tf.float32)
        embeded_text = embeded_text * tf.expand_dims(mask, 2)
        self.embeded_text = embeded_text

        top_feature = embeded_text
        filters = 64
        if num_blocks[0] == 0:
            self.block = (1, 1, 1, 1)
        else:
            self.block = num_blocks
        for i, num_block in enumerate(self.block):
            if i > 0:
                filters *= 2
                top_feature = layers.max_pool_1d(top_feature,
                                                 3,
                                                 strides=2,
                                                 padding='same')
            for block_i in range(num_block):
                top_feature = self.conv_block(top_feature, filters)

        pooled_feature = layers.flatten(
            layers.custom_layer(top_feature, self.kmax_pool_1d))
        if dropout is not None:
            pooled_feature = layers.dropout(pooled_feature, dropout)
        fc1 = layers.fully_connected(pooled_feature,
                                     2048,
                                     activation='relu',
                                     regularizer='L2',
                                     weight_decay=weight_decay)
        if dropout is not None:
            fc1 = layers.dropout(fc1, dropout)
        fc2 = layers.fully_connected(fc1,
                                     2048,
                                     activation='relu',
                                     regularizer='L2',
                                     weight_decay=weight_decay)
        self.probas = layers.fully_connected(fc2,
                                             num_classes,
                                             activation='softmax',
                                             regularizer='L2',
                                             weight_decay=weight_decay)

        def build_sgd(learning_rate):
            step_tensor = tf.Variable(0.,
                                      name="Training_step",
                                      trainable=False)
            steps = [-1.0, 16000.0, 24000.0]
            lrs = [1e-1, 1e-2, 1e-3]
            lr = tf.reduce_min(
                tf.cast(tf.less(step_tensor, steps), tf.float32) + lrs)
            tflearn.helpers.summarizer.summarize(
                lr, 'scalar', 'lr', 'Optimizer_training_summaries')
            return tf.train.MomentumOptimizer(learning_rate=lr,
                                              momentum=0.9), step_tensor

        if optimizer == 'sgd':
            optimizer = build_sgd
        self.train_op = layers.regression(self.probas,
                                          optimizer=optimizer,
                                          learning_rate=0.001,
                                          placeholder=self.target_label)