def RiskZone_Criterion(X): tf.reset_default_graph() tflearn.config.init_training_mode() input_layer = tflearn.input_data(shape=[None, 2]) network = tflearn.fully_connected(input_layer, 256, activation='relu') network = tflearn.dropout(network, 0.5) network = tflearn.fully_connected(network, 512, activation='relu') network = tflearn.dropout(network, 0.5) network = tflearn.fully_connected(network, 256, activation='relu') network = tflearn.dropout(network, 0.5) network = tflearn.fully_connected(network, 512, activation='relu') network = tflearn.dropout(network, 0.5) network = tflearn.fully_connected(network, 1024, activation='relu') network = tflearn.dropout(network, 0.5) network = tflearn.fully_connected(network, 128, activation='relu') network = tflearn.dropout(network, 0.5) softmax = tflearn.fully_connected(network, 3, activation='softmax') acc = Accuracy(name="Accuracy") net = tflearn.regression(softmax, optimizer='adam', learning_rate=0.0005, metric=acc, loss='categorical_crossentropy') model = tflearn.DNN(net) model.load('RiskCriterion.tflearn') model_result = model.predict(X) return model_result
def setup_nn_network(self): """ Setup neural network structure """ # our input is an image of 32 pixels high and wide with 3 channels (RGB) # we will also preprocess and create synthetic images self.tf_network = input_data( shape=[None, self.image_size, self.image_size, 3], data_preprocessing=self.tf_img_prep, data_augmentation=self.tf_img_aug) # layer 1: convolution layer with 32 filters (each being 3x3x3) layer_conv_1 = conv_2d(self.tf_network, 32, 3, activation='relu', name='conv_1') # layer 2: max pooling layer self.tf_network = max_pool_2d(layer_conv_1, 2) # layer 3: convolution layer with 64 filters layer_conv_2 = conv_2d(self.tf_network, 64, 3, activation='relu', name='conv_2') # layer 4: Another convolution layer with 64 filters layer_conv_3 = conv_2d(layer_conv_2, 64, 3, activation='relu', name='conv_3') # layer 5: Max pooling layer self.tf_network = max_pool_2d(layer_conv_3, 2) # layer 6: Fully connected 512 node layer self.tf_network = fully_connected(self.tf_network, 512, activation='relu') # layer 7: Dropout layer (removes neurons randomly to combat overfitting) self.tf_network = dropout(self.tf_network, 0.5) # layer 8: Fully connected layer with two outputs (hotdog or non hotdog class) self.tf_network = fully_connected(self.tf_network, 2, activation='softmax') # define how we will be training our network accuracy = Accuracy(name="Accuracy") self.tf_network = regression(self.tf_network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.0005, metric=accuracy)
def create_model_for_multiple_input(self): # main cnn model network1 = input_data( shape=[None, self.title_mat_size, self.title_mat_size, 1]) network2 = input_data( shape=[None, self.author_mat_size, self.author_mat_size, 1]) activation = 'relu' # activation = 'tanh' # activation = 'sigmoid' network1 = conv_2d(network1, 8, 3, activation=activation, regularizer="L2") # network = max_pool_2d(network, 2) # network = local_response_normalization(network) network1 = conv_2d(network1, 16, 2, activation=activation, regularizer="L2") # network = max_pool_2d(network, 2) # network = local_response_normalization(network) network1 = fully_connected(network1, 512, activation=activation) network2 = conv_2d(network2, 8, 2, activation=activation, regularizer='L2') network2 = fully_connected(network2, 512, activation=activation) network = merge([network1, network2], 'concat') network = dropout(network, 0.5) network = fully_connected(network, 2, activation='softmax') acc = Accuracy(name="Accuracy") adagrad = tflearn.AdaGrad(learning_rate=0.002, initial_accumulator_value=0.01) network = regression(network, optimizer=adagrad, loss='categorical_crossentropy', learning_rate=0.002, metric=acc) tmp_dir = join(settings.TRAIN_DIR, 'tmp') # util.mkdir(tmp_dir) model = tflearn.DNN(network, checkpoint_path=join(tmp_dir, 'paper_similarity'), max_checkpoints=10, tensorboard_verbose=3, tensorboard_dir=join(tmp_dir, 'tflearn_logs')) return model
def alexnet(): acc = Accuracy() #Start with a layer that inputs the image data #network = input_data(shape=[None, 480,640, 3], data_augmentation = img_aug ) network = input_data(shape=[None, 100,100, 3] ) #Add Convolutional Layer to expand the feature space network = conv_2d(network, nb_filter = 96, filter_size = 11, strides = 4, activation='relu', name = 'conv_1') #Add a pooling layer to reduce the feature space network = max_pool_2d(network, kernel_size = 3, strides=2, name = 'max_1') #Add Convolutional Layer to expand the feature space network = conv_2d(network, nb_filter = 256, filter_size = 5, strides = 4, activation='relu', name = 'conv_2') #Add a pooling layer to reduce the feature space network = max_pool_2d(network, kernel_size = 3, strides=2, name = 'max_2') #Add Convolutional Layer to expand the feature space network = conv_2d(network, nb_filter = 384, filter_size = 3, strides = 1, activation='relu', name = 'conv_2') #Add Convolutional Layer to expand the feature space network = conv_2d(network, nb_filter = 384, filter_size = 3, strides = 1, activation='relu', name = 'conv_2') #Add Convolutional Layer to expand the feature space network = conv_2d(network, nb_filter = 256, filter_size = 3, strides = 1, activation='relu', name = 'conv_2') # Add a fully connected layer network = fully_connected(network, 960, activation='elu') # Add a fully connected layer network = fully_connected(network, 480, activation='elu') # Add a fully connected layer network = fully_connected(network, 240, activation='elu') # add a Dropout layer # network = dropout(network, 0.5) # # Fully Connected Layer network = fully_connected(network, 120, activation='softmax') # Final network network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001, metric=acc) # The model with details on where to save model = tflearn.DNN(network,tensorboard_verbose=0 ) return model
def get_model(box_size, numb_classes): acc = Accuracy() network = input_data(shape=[None, box_size, box_size, 1]) # Conv layers network = conv_2d(network, 64, 3, strides=1, activation='relu', name='conv1_3_3_1') network = max_pool_2d(network, 2, strides=2) network = conv_2d(network, 64, 3, strides=1, activation='relu', name='conv1_3_3_2') network = conv_2d(network, 64, 3, strides=1, activation='relu', name='conv1_3_3_3') network = max_pool_2d(network, 2, strides=2) # Fully Connected Layer network = fully_connected(network, 1024, activation='tanh') # Dropout layer network = dropout(network, 0.5) # Fully Connected Layer network = fully_connected(network, numb_classes, activation='softmax') # Final network network = regression(network, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.001, metric=acc) # The model with details on where to save # Will save in current directory model = tflearn.DNN(network, checkpoint_path='model-', best_checkpoint_path='best-model-', tensorboard_verbose=0) return model
def cnn(): # Building Convolutional Neural Network network = tflearn.input_data(shape=[None, 128, 128, 3]) network = tflearn.conv_2d(network, 32, 3, activation='relu') network = tflearn.max_pool_2d(network, 2) network = local_response_normalization(network) network = tflearn.conv_2d(network, 64, 3, activation='relu') network = tflearn.conv_2d(network, 64, 3, activation='relu') network = tflearn.max_pool_2d(network, 2) network = local_response_normalization(network) network = tflearn.fully_connected(network, 512, activation='relu') network = tflearn.dropout(network, 0.5) network = tflearn.fully_connected(network, 2, activation='softmax') network = tflearn.regression(network, optimizer='adam', loss='categorical_crossentropy', metric=Accuracy(name="Accuracy"), learning_rate=0.001) return network
def makeNetwork(network, nb_categories): # architecture of the network network = conv_2d(network, 32, 3, activation='relu', name='conv_1') network = max_pool_2d(network, 2) network = conv_2d(network, 64, 3, activation='relu', name='conv_2') network = conv_2d(network, 64, 3, activation='relu', name='conv_3') network = max_pool_2d(network, 2) network = conv_2d(network, 128, 3, activation='relu', name='conv_4') network = fully_connected(network, 512, activation='relu') network = dropout(network, 0.4) network = fully_connected(network, nb_categories, activation='softmax') # how the network will be trained acc = Accuracy(name="Accuracy") network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.0009, metric=acc) return network
def __init__(self): image_size = 32 self.image_size = image_size # Same network definition as before img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() img_aug = ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_rotation(max_angle=25.) img_aug.add_random_blur(sigma_max=3.) network = input_data(shape=[None, image_size, image_size, 3], data_preprocessing=img_prep, data_augmentation=img_aug) network = conv_2d(network, 32, 3, activation='relu') network = max_pool_2d(network, 2) network = conv_2d(network, 64, 3, activation='relu') network = conv_2d(network, 64, 3, activation='relu') network = max_pool_2d(network, 2) network = fully_connected(network, 512, activation='relu') network = dropout(network, 0.5) network = fully_connected(network, 4, activation='softmax') acc = Accuracy(name="Accuracy") network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.0005, metric=acc) #self.model = tflearn.DNN(network, checkpoint_path='AllData.tflearn', max_checkpoints = 3, #tensorboard_verbose = 3, tensorboard_dir='tmp/tflearn_logs/') self.model = tflearn.DNN(network) self.model.load("Candidates/AllData_final.tflearn_Nov25")
def Estimation_Index_updown(X): tf.reset_default_graph() tflearn.config.init_training_mode() input_layer = tflearn.input_data(shape=[None, 13]) network = tflearn.fully_connected(input_layer, 1024, activation='tanh') network = tflearn.fully_connected(network, 512, activation='tanh') network = tflearn.fully_connected(network, 512, activation='tanh') network = tflearn.fully_connected(network, 1024, activation='tanh') network = tflearn.fully_connected(network, 2048, activation='tanh') network = tflearn.fully_connected(network, 512, activation='tanh') network = tflearn.dropout(network, 0.7) softmax = tflearn.fully_connected(network, 2, activation='softmax') acc = Accuracy(name="Accuracy") net = tflearn.regression(softmax, optimizer='adam', learning_rate=0.01, metric=acc, loss='categorical_crossentropy') model = tflearn.DNN(net) model.load('EstimationIndex.tflearn') model_result = model.predict(X) return model_result
conv_3 = conv_2d(conv_2, 64, 3, activation='relu', name='conv_3') # 5: Max pooling layer network = max_pool_2d(conv_3, 2) # 6: Fully-connected 512 node layer network = fully_connected(network, 512, activation='relu') # 7: Dropout layer to combat overfitting network = dropout(network, 0.5) # 8: Fully-connected layer with two outputs network = fully_connected(network, 2, activation='softmax') # Configure how the network will be trained acc = Accuracy(name="Accuracy") network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.0005, metric=acc) # Wrap the network in a model object model = tflearn.DNN(network, checkpoint_path='model_cat_dog_6.tflearn', max_checkpoints=3, tensorboard_verbose=3, tensorboard_dir='tmp/tflearn_logs/') ################################### # Train model for 100 epochs
import tflearn from tflearn.layers.core import input_data, dropout, fully_connected from tflearn.layers.conv import conv_2d, max_pool_2d from tflearn.layers.estimator import regression from tflearn.metrics import Accuracy acc = Accuracy() network = input_data(shape=[None, 28, 28, 1]) network = conv_2d(network, 64, 3, strides=1, activation='relu', name='conv1') network = max_pool_2d(network, 2, strides=2) network = conv_2d(network, 64, 3, strides=1, activation='relu', name='conv2') network = conv_2d(network, 64, 3, strides=1, activation='relu', name='conv3') network = max_pool_2d(network, 2, strides=2) network = fully_connected(network, 1024, activation='tanh') network = dropout(network, 0.5) network = fully_connected(network, 10, activation='softmax') network = regression(network, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.001, metric=acc) model = tflearn.DNN(network, checkpoint_path='model-', best_checkpoint_path='best-model-')
conv_3 = conv_2d(conv_2, 32, 3, activation='relu', name='conv_3') # 5: Max pooling layer network = max_pool_2d(conv_3, 2) # 6: Fully-connected 256 node layer network = fully_connected(network, 256, activation='relu') # 7: Dropout layer to combat overfitting network = dropout(network, 0.5) # 8: Fully-connected layer with two outputs network = fully_connected(network, 2, activation='softmax') # Configure how the network will be trained acc = Accuracy(name='Accuracy') network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.0005, metric=acc) # Wrap the network in a model object model = tflearn.DNN(network, checkpoint_path='model/catdog_model.tflearn', max_checkpoints = 3, tensorboard_verbose = 3, tensorboard_dir='model/tmp/tflearn_logs/') print('Network Definition Complete') #-------------------------------------------- #### Train model for N epochs #-------------------------------------------- print('Preparing to Train Model')
conv_3 = conv_2d(conv_2, 64, 3, activation='relu', name='conv_3') # 5: Camada Máx. De Agrupamento - Max pooling layer network = max_pool_2d(conv_3, 2) # 6: Camada de nó 512 totalmente conectada network = fully_connected(network, 512, activation='relu') # 7: Camada de eliminação para combater o overfitting network = dropout(network, 0.5) # 8: Camada totalmente conectada com duas saídas network = fully_connected(network, 2, activation='softmax') # Configurar como a rede será treinada acc = Accuracy(name="Precisão") network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.0005, metric=acc) # Envolva a rede em um objeto de modelo model = tflearn.DNN(network, checkpoint_path='model_cat_dog_6.tflearn', max_checkpoints = 3, tensorboard_verbose = 3, tensorboard_dir=r'C:\Users\rodrigo.fernandes\Documents\Tfs\AppFlowRecognition\App.v1\AppFlowRecognition\Scripts\ClassifierCatsAndDogs\datset\tflearn_logs') ####################################### # Modelo de treinamento para 100 épocas ####################################### model.fit(X, Y, validation_set=(X_test, Y_test), batch_size=500, n_epoch=100, run_id='model_cat_dog_6', show_metric=True) model.save('model_cat_dog_6_final.tflearn')
def get_network_architecture(image_width, image_height, number_of_classes, learning_rate): number_of_channels = 1 network = input_data( shape=[None, image_width, image_height, number_of_channels], data_preprocessing=img_prep, data_augmentation=img_aug, name='InputData' ) """ def conv_2d(incoming, nb_filters, filter_size, strides=1, padding='same', activation='linear', bias='True', weights_init='uniform_scaling', bias_init='zeros', regularizer=None, weight_decay=0.001, trainable=True, restore=True, reuse=False, scope=None, name='Conv2D') network = conv_2d(network, 32, (3, 3), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_1') network = max_pool_2d(network, (2, 2), strides=2, padding='same', name='MaxPool2D_1') network = avg_pool_2d(network, (2, 2), strides=2, padding='same', name='AvgPool2D_1') network = dropout(network, 0.5, name='Dropout_1') network = batch_normalization(network, name='BatchNormalization') network = flatten(network, name='Flatten') network = fully_connected(network, 512, activation='relu', name='FullyConnected_1') network = fully_connected(network, number_of_classes, activation='softmax', name='FullyConnected_Final') print(' {}: {}'.format('Conv2D................', network.shape)) print(' {}: {}'.format('MaxPool2D.............', network.shape)) print(' {}: {}'.format('Dropout...............', network.shape)) print(' {}: {}'.format('BatchNormalization....', network.shape)) print(' {}: {}'.format('Flatten...............', network.shape)) print(' {}: {}'.format('FullyConnected........', network.shape)) print(' {}: {}'.format('FullyConnected_Final..', network.shape)) CONV / FC -> Dropout -> BN -> activation function -> ... Convolutional filters: { 32, 64, 128 } Convolutional filter sizes: { 1, 3, 5, 11 } Convolutional strides: 1 Activation: ReLu Pooling kernel sizes: { 2, 3, 4, 5 } Pooling kernel strides: 2 Dropout probability: 0.5 - Higher probability of keeping in earlier stages - Lower probability of keeping in later stages """ print('\nNetwork architecture:') print(' {}: {}'.format('InputData.............', network.shape)) network = conv_2d(network, 16, (7, 7), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_1') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_1') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = conv_2d(network, 16, (7, 7), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_2') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_2') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = avg_pool_2d(network, (2, 2), strides=2, padding='same', name='AvgPool2D_1') print(' {}: {}'.format('AvgPool2D.............', network.shape)) network = dropout(network, 0.5, name='Dropout_1') print(' {}: {}'.format('Dropout...............', network.shape)) network = conv_2d(network, 32, (5, 5), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_3') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_3') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = conv_2d(network, 32, (5, 5), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_4') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_4') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = avg_pool_2d(network, (2, 2), strides=2, padding='same', name='AvgPool2D_2') print(' {}: {}'.format('AvgPool2D.............', network.shape)) network = dropout(network, 0.5, name='Dropout_2') print(' {}: {}'.format('Dropout...............', network.shape)) network = conv_2d(network, 64, (3, 3), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_5') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_5') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = conv_2d(network, 64, (3, 3), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_6') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_6') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = avg_pool_2d(network, (2, 2), strides=2, padding='same', name='AvgPool2D_3') print(' {}: {}'.format('AvgPool2D.............', network.shape)) network = dropout(network, 0.5, name='Dropout_3') print(' {}: {}'.format('Dropout...............', network.shape)) network = conv_2d(network, 128, (3, 3), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_7') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_7') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = conv_2d(network, 128, (3, 3), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_8') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_8') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = avg_pool_2d(network, (2, 2), strides=2, padding='same', name='AvgPool2D_4') print(' {}: {}'.format('AvgPool2D.............', network.shape)) network = dropout(network, 0.5, name='Dropout_4') print(' {}: {}'.format('Dropout...............', network.shape)) network = conv_2d(network, 256, (3, 3), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_9') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_9') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = conv_2d(network, 256, (3, 3), strides=1, padding='same', activation='relu', regularizer='L2', name='Conv2D_10') print(' {}: {}'.format('Conv2D................', network.shape)) network = batch_normalization(network, name='BatchNormalization_10') print(' {}: {}'.format('BatchNormalization....', network.shape)) network = avg_pool_2d(network, (2, 2), strides=2, padding='same', name='AvgPool2D_5') print(' {}: {}'.format('AvgPool2D.............', network.shape)) network = dropout(network, 0.5, name='Dropout_5') print(' {}: {}'.format('Dropout...............', network.shape)) network = flatten(network, name='Flatten') print(' {}: {}'.format('Flatten...............', network.shape)) network = fully_connected(network, 512, activation='relu', name='FullyConnected_1') print(' {}: {}'.format('FullyConnected........', network.shape)) network = dropout(network, 0.5, name='Dropout_6') print(' {}: {}'.format('Dropout...............', network.shape)) network = fully_connected(network, number_of_classes, activation='softmax', name="FullyConnected_Final") print(' {}: {}'.format('FullyConnected_Final..', network.shape)) optimizer = Adam(learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False, name='Adam') # optimizer = SGD(learning_rate=learning_rate, lr_decay=0.01, decay_step=100, staircase=False, use_locking=False, name='SGD') # optimizer = RMSProp(learning_rate=learning_rate, decay=0.9, momentum=0.9, epsilon=1e-10, use_locking=False, name='RMSProp') # optimizer = Momentum(learning_rate=learning_rate, momentum=0.9, lr_decay=0.01, decay_step=100, staircase=False, use_locking=False, name='Momentum') metric = Accuracy(name='Accuracy') # metric = R2(name='Standard Error') # metric = WeightedR2(name='Weighted Standard Error') # metric = Top_k(k=6, name='Top K') network = regression( network, optimizer=optimizer, loss='categorical_crossentropy', metric=metric, learning_rate=learning_rate, name='Regression' ) return network
def createCNN(args): size_entry_filter = 32 size_mid_filter = 64 filter_size = 3 color_channels = 3 model_main_activation = 'relu' model_exit_activation = 'softmax' max_pool_kernel_size = 2 model_learning_rate = 0.001 num_classes = args['num_classes'] ################################### # Image transformations ################################### # normalisation of images img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() # Create extra synthetic training data by flipping & rotating images img_aug = ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_rotation(max_angle=25.) img_aug.add_random_blur(sigma_max=3.) ################################### # Define network architecture ################################### # Input is a image_size x image_size image with 3 color channels (red, green and blue) network = input_data( shape=[None, args['size'], args['size'], color_channels], data_preprocessing=img_prep, data_augmentation=img_aug) # 1: Convolution layer with 32 filters, each 3x3x3 network = conv_2d(network, size_entry_filter, filter_size, activation=model_main_activation) # 2: Max pooling layer network = max_pool_2d(network, max_pool_kernel_size) # 3: Convolution layer with 64 filters network = conv_2d(network, size_mid_filter, filter_size, activation=model_main_activation) # 4: Convolution layer with 64 filters network = conv_2d(network, size_mid_filter, filter_size, activation=model_main_activation) # 5: Max pooling layer network = max_pool_2d(network, max_pool_kernel_size) # 6: Convolution layer with 64 filters network = conv_2d(network, size_mid_filter, filter_size, activation=model_main_activation) # 7: Convolution layer with 64 filters network = conv_2d(network, size_mid_filter, filter_size, activation=model_main_activation) # 8: Max pooling layer network = max_pool_2d(network, max_pool_kernel_size) # 9: Fully-connected 512 node layer network = fully_connected(network, 512, activation=model_main_activation) # 10: Dropout layer to combat overfitting network = dropout(network, 0.5) # 11: Fully-connected layer with two outputs network = fully_connected(network, num_classes, activation=model_exit_activation) # Configure how the network will be trained acc = Accuracy(name="Accuracy") network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=model_learning_rate, metric=acc) #main_dir = os.path.dirname(os.path.dirname(os.getcwd())) # Wrap the network in a model object model = tflearn.DNN(network, tensorboard_verbose=0, max_checkpoints=2, best_checkpoint_path=os.path.join( os.path.dirname(os.path.dirname(os.getcwd())), args['id'] + '.tfl.ckpt'), best_val_accuracy=args['accuracy']) return model
def color(): ################################### ### Import picture files ################################### files_path = "./img_align_celeba/" glasses_files_path = os.path.join(files_path, '*.jpg') glasses_files = sorted(glob(glasses_files_path)) n_files = len(glasses_files) #print(n_files) #size_image1 =178 #size_image2=218 size_image1=27 size_image2=33 allX = np.zeros((n_files, size_image1, size_image2, 3), dtype='float32') ally = np.zeros(n_files) count = 0 for f in glasses_files: try: img = io.imread(f) new_img = skimage.transform.resize(img, (27, 33, 3)) allX[count] = np.array(new_img) ally[count] = 0 count += 1 except: continue attribute=[] g = open('./list_attr_celeba.txt', 'r') text = g.readlines() text = np.array(text) attr2idx = dict() for i, attr in enumerate(text[1].split()): attr2idx[attr] = i attr_index = attr2idx['Eyeglasses']#'Eyeglasses' for i in text[2:]: value = i.split() attribute.append(value[attr_index + 1]) #First index is image name attribute = np.array(attribute,dtype= np.float32) #print("Converting Label.................") for i in range(0,len(attribute)): if (attribute[i] == 1): ally[i]=1 else: ally[i]=0 ally = np.array(ally) ########break up data into training, validation, and test sets train_limit = int(math.floor(0.8 * len(allX))) validate_limit = int(math.floor(0.1*len(allX))) #print (train_limit, validate_limit) X = allX[0:train_limit,:,] X_validation = allX[(train_limit+1):(train_limit+validate_limit),:,] X_test = allX[(train_limit+validate_limit+1):,:,] Y = ally[0:train_limit] Y_validation = ally[(train_limit+1):(train_limit+validate_limit)] Y_test = ally[(train_limit+validate_limit+1):] # encode the Ys Y = to_categorical(Y, 2) Y_test = to_categorical(Y_test, 2) Y_validation = to_categorical(Y_validation, 2) #take a subset of training dataset to find parameters x_sm = int(math.floor(0.8 * len(allX))*0.5) print (x_sm) X_sm = allX[0:x_sm,:,] Y_sm = ally[0:x_sm] Y_sm=to_categorical(Y_sm, 2) print (X.shape, Y.shape, allX.shape, ally.shape, X_sm.shape) ################################### # Image transformations ################################### # normalisation of images img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() # Create extra synthetic training data by flipping & rotating images img_aug = ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_rotation(max_angle=25.) ################################### # Define network architecture ################################### # Input is a 27x33 image with 3 color channels (red, green and blue) network = input_data(shape=[None, 27, 33, 3]) #,data_preprocessing=img_prep, #data_augmentation=img_aug) # 1: Convolution layer with 32 filters, each 3x3x3 conv_1 = conv_2d(network, 32, 3, activation='relu', name='conv_1') # 2: Max pooling layer network = max_pool_2d(conv_1, 2) # 3: Convolution layer with 64 filters conv_2 = conv_2d(network, 64, 3, activation='relu', name='conv_2') #4: Convolution layer with 64 filters conv_3 = conv_2d(conv_2, 64, 3, activation='relu', name='conv_3') # 5: Max pooling layer network = max_pool_2d(conv_3, 2) # 6: Fully-connected 512 node layer network = fully_connected(network, 1024, activation='relu') # 7: Dropout layer to combat overfitting network = dropout(network, 0.5) # 8: Fully-connected layer with two outputs network = fully_connected(network, 2, activation='softmax') # Configure how the network will be trained acc = Accuracy(name="Accuracy") network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001, metric=acc) # Wrap the network in a model object model = tflearn.DNN(network, checkpoint_path='model_glasses_6.tflearn', max_checkpoints = 3, tensorboard_verbose = 3, tensorboard_dir='tmp/tflearn_logs/') ################################### # Train model for 1000 epochs ################################### model.fit(X_sm, Y_sm, validation_set=(X_validation, Y_validation), batch_size=50, n_epoch=1000, run_id='model_glasses_6', show_metric=True) model.save('model_glasses_6_final.tflearn') # Evaluate model score = model.evaluate(X_test, Y_test) print('Test accuarcy: %0.4f%%' % (score[0] * 100))
def define_network(size_img, N_classes): ''' Function for defining a particular convolutional network including 3 convolution layers, and 2x max-pooling, as well as 2x fully connected Parameters ---------- size_img: number of pixels of images to be used, with shape=(size_img,size_img) N_classes: The number of output classes/cathegories ''' ################################### # Image transformations ################################### # normalisation of images img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() # Create extra synthetic training data by flipping & rotating images img_aug = ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_rotation(max_angle=25.) ################################### # Define network architecture ################################### # Input is a size_imagexsize_image, 3 color channels (red, green and blue) network = input_data(shape=[None, size_img, size_img, 3], data_preprocessing=img_prep, data_augmentation=img_aug) # 1: Convolution layer 32 filters, each 3x3x3 conv_1 = conv_2d(network, 32, 3, activation='relu', name='conv_1') # 2: Max pooling layer network = max_pool_2d(conv_1, 2) # 3: Convolution layer with 64 filters conv_2 = conv_2d(network, 64, 3, activation='relu', name='conv_2') # 4: Once more... conv_3 = conv_2d(conv_2, 64, 3, activation='relu', name='conv_3') # 5: Max pooling layer network = max_pool_2d(conv_3, 2) # 6: Fully-connected 512 node layer network = fully_connected(network, 512, activation='relu') # 7: Dropout layer to combat overfitting network = dropout(network, 0.5) # 8: Fully-connected layer N_classes outputs network = fully_connected(network, N_classes, activation='softmax') # Configure of Network training acc = Accuracy(name="Accuracy") network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.0005, metric=acc) return network