Ejemplo n.º 1
0
if __name__ == '__main__':

    # Example to fine-tune on 3000 samples from Cifar10

    classes = ['One', 'Two', 'Three', 'Four', 'Five']
    num_classes = len(classes)

    # 20% of the data will automatically be used for validation
    validation_size = 0.8
    img_size = 224
    num_channels = 3
    train_path = 'training_data1'

    # We shall load all the training and validation images and labels into memory using openCV and use that during training
    train_images, train_labels, validation_images, validation_labels = dataset.read_train_sets(
        train_path, img_size, classes, validation_size=validation_size)
    X_train, Y_train, X_valid, Y_valid = train_images, train_labels, validation_images, validation_labels

    img_rows, img_cols = 224, 224  # Resolution of inputs
    channel = 3
    num_classes = 5
    batch_size = 16
    nb_epoch = 10

    # Load Cifar10 data. Please implement your own load_data() module for your own dataset
    # X_train, Y_train, X_valid, Y_valid = load_cifar10_data(img_rows, img_cols)

    # Load our model
    model = resnet50_model(img_rows, img_cols, channel, num_classes)

    # Start Fine-tuning
#


def shuffle_in_unison(a, b):
    assert len(a) == len(b)
    shuffled_a = np.empty(a.shape, dtype=a.dtype)
    shuffled_b = np.empty(b.shape, dtype=b.dtype)
    permutation = np.random.permutation(len(a))
    for old_index, new_index in enumerate(permutation):
        shuffled_a[new_index] = a[old_index]
        shuffled_b[new_index] = b[old_index]
    return shuffled_a, shuffled_b


data = dataset.read_train_sets("D:\\Amaury\\Ian\\datasetTest",
                               220, ["w1", "w2"],
                               validation_size=.4)

# trainData, trainLabels, trainID, trainCLS = dataset.load_train("D:\\Amaury\\Ian\\Data\\train\\Temp Wood", 220, ["w1", "w2"])
# valData, valLabels, valID, valCLS = dataset.load_train("D:\\Amaury\\Ian\\Data\\validation\\Temp Wood", 220, ["w1", "w2"])

trainData = data.train.images
trainLabels = data.train.labels
valData = data.valid.images
valLabels = data.valid.labels

print("start")
print(trainData.shape)
print(trainLabels.shape)
print(valData.shape)
print(valLabels.shape)
Ejemplo n.º 3
0
    return K.sum(K.log(x0) - K.log(x1))

#Prepare input data
train_path='training_data'
classes = ['video_frames']
print(classes)
num_classes = len(classes)

# 10% of the data will automatically be used for validation
validation_size = 0.1
img = cv2.imread('C:\\Users\\darsh\\PycharmProjects\\final\\training_data\\video_frames\\video_frames 0001.jpg')
img_size_w,img_size_h,_ = img.shape
num_channels = 3
sample_size = 1926

data = dataset.read_train_sets(train_path, 400, ['video_frames'], validation_size=validation_size, sample_size=sample_size)
print("Complete reading input data. Will Now print a snippet of it")
print("Number of files in Training-set:\t\t{}".format(len(data.train.labels)))
print("Number of files in Validation-set:\t{}".format(len(data.valid.labels)))

total_pixels = img_size_w * img_size_h * 3
translator_factor = 2
translator_layer_size = 500
middle_factor = 2
middle_layer_size = 250
batch_size = 32
num_classes = 10
epochs = 100

x_train, _, _, _ = data.train.next_batch(1776)
x_test, _, _, _ = data.valid.next_batch(150)
Ejemplo n.º 4
0

image_list = []

# Build the dataset
classnames = ['C1H','C2H','C1L','C2L','C1M','S1L','URML','URMM','W1','W2']
for i in classnames:
    dir_list = os.listdir(os.path.join(base_dir,i))
    print("class " + str(i) + " has " + str(len((dir_list))) + " images")
    image_list.extend(dir_list)
    
    
print("total images: " + str(len(image_list)))

# build the model
data = dataset.read_train_sets(base_dir,220,["C1H","C2H","C1L","C2L"],validation_size=.4)

trainData = data.train.images
trainLabels = data.train.labels
valData = data.valid.images
valLabels = data.valid.labels

print(trainData.shape)
print(trainLabels.shape)
print(valData.shape)
print(valLabels.shape)


conv_base = keras.applications.vgg16.VGG16(weights = 'imagenet',include_top = False, input_shape = (220,220,3),classes = 10)
conv_base.trainable = False
Ejemplo n.º 5
0
def load_test_dataset():
    return dataset.read_train_sets(test_path, img_size, classes, validation_size=validation_size)
Ejemplo n.º 6
0
import math
import random
import numpy as np
from numpy.random import seed

seed(10)
tf.set_random_seed(20)
# tf.random.set_seed(20)
batch_size = 500
classes = ['cat', 'dog']
num_classes = len(classes)
test_size = 0.2
img_size = 64
num_channels = 3
train_path = 'data/train'
data = dataset.read_train_sets(train_path, img_size, classes, test_size=test_size)
print("数据读取完毕")
print("训练集的数量为:{}".format(len(data.train._images)))
print("测试集的数量为:{}".format(len(data.test._images)))
session = tf.Session()    # 2.0版本没有Session模块

# tf.compat.v1.disable_eager_execution()

x = tf.placeholder(tf.float32, shape=[None, img_size, img_size, num_channels], name='x')
# labels
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, 1)

filter_size_conv1 = 3
num_filters_conv1 = 32
Ejemplo n.º 7
0
def test():
    data = dataset.read_train_sets(train_path,
                                   image_size,
                                   classes,
                                   validation_size=0.9)
    evaluate(data.valid, batch_size=batch_size)
Ejemplo n.º 8
0
classes = ['dogs', 'cats']
num_classes = len(classes)

# We've modified this so it pulls from training and testing_data respectively
validation_size = 0
img_size = 32
num_channels = 3

# If train_path set to generated_data, we are not training on the original data, we are training
# on generated fake data.
train_path = "train"

val_path = "test"

# We shall load all the training and validation images and labels into memory using openCV and use that during training
data = dataset.read_train_sets(train_path, val_path, img_size, classes)

print("Complete reading input data. Will Now print a snippet of it")
print("Number of files in Training-set:\t\t{}".format(len(data.train.labels)))
print("Number of files in Validation-set:\t{}".format(len(data.valid.labels)))

session = tf.Session()
x = tf.placeholder(tf.float32,
                   shape=[None, img_size, img_size, num_channels],
                   name='x')

## labels
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)

##Network graph params
Ejemplo n.º 9
0
for dir_name in dirs:
    if dir_name != '.' and dir_name != '..':
        classes.append(dir_name)

num_classes = len(classes)

if num_classes == 0:
    print "[!!] Directorio del dataset de entrenamiento vacio"
    exit()

print "[i] Leyendo imagenes del dataset, puede tomar varios segundos"

# Lectura y preparacion de el set de datos
data = dataset.read_train_sets(dataset_train,
                               img_size,
                               classes,
                               validation_size=(FLAGS.validation_size / 100.0))

print "[i] Lectura del dataset completada"
print "[i] Dataset de entrenamiento: %s" % (dataset_train)
print "[i] %s clases: (%s)" % (num_classes, classes)
print "[i] %s%% de datos para validacion" % (FLAGS.validation_size)
print "[i] Batch: %s" % (FLAGS.batch)
print "[i] Precision deseada: %s%%" % (FLAGS.accuracy_target)

print("[i] Numero de elementos de entrenamiento:\t\t{}".format(
    len(data.train.labels)))
print("[i] Numero de elementos de validacion:\t{}".format(
    len(data.valid.labels)))

raw_input("[i] Presione cualquier tecla para comenzar el entrenamiento...")
Ejemplo n.º 10
0
    #Adding Seed so that random initialization is consistent
    seed(1)
    set_random_seed(2)

    #Prepare input data
    classes = [0, 1]
    num_classes = len(classes)

    img_size = 128
    num_channels = 3

    # We shall load all the training and validation images and labels into memory
    # using openCV and use that during training
    #data = dataset.read_train_sets(args.labelsfile, args.imagedir, img_size, classes, validation_size=VALIDATION_SIZE)
    data = dataset.read_train_sets(args.imagedir,
                                   img_size,
                                   classes,
                                   validation_size=VALIDATION_SIZE)
    # Shapes of training set
    print("Training set (images) shape: {shape}".format(
        shape=data.train.images.shape))
    print("Training set (labels) shape: {shape}".format(
        shape=data.train.labels.shape))

    # Shapes of test set
    print("Test set (images) shape: {shape}".format(
        shape=data.valid.images.shape))
    print("Test set (labels) shape: {shape}".format(
        shape=data.valid.labels.shape))

    print("Complete reading input data. ")
    print("Number of files in Training-set:\t\t{}".format(
Ejemplo n.º 11
0
img_size = mdata["train"]["img_size"]
num_channels = mdata["train"]["channel"]

#Copy training data to the 'training_data' folder under running directory
trn_dat_dir = run_dir + "/" + "training_data"
if not os.path.exists(trn_dat_dir):
    os.makedirs(trn_dat_dir)
for cls in classes:
    if os.path.exists(trn_dat_dir + "/" + cls):
        os.system("rm -rf " + trn_dat_dir + "/" + cls)
    cmd = "cp -R " + mdata["classes"][cls] + " " + trn_dat_dir + "/" + cls
    os.system(cmd)

# Load all the training and validation images and labels into memory using openCV and use that during training
data = dataset.read_train_sets(trn_dat_dir,
                               img_size,
                               classes,
                               validation_size=validation_size)
logger.info("Complete processing input data:")
logger.info("Number of files in Training-set:\t{}".format(
    len(data.train.labels)))
logger.info("Number of files in Validation-set:\t{}".format(
    len(data.valid.labels)))

# Declare placeholders
x = tf.placeholder(tf.float32,
                   shape=[None, img_size, img_size, num_channels],
                   name='x')
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)

# Create neural networks according to the json file
Ejemplo n.º 12
0
#Prepare input data
classes = range(0, 36)
num_classes = len(classes)

# 20% of the data will automatically be used for validation
validation_size = 0.2
test_size = 0.2
img_size = 56
num_channels = 1
train_path = ''

# We shall load all the training and validation images and labels into memory and use that during training
data = dataset.read_train_sets(train_path,
                               img_size,
                               num_channels,
                               classes,
                               validation_size=validation_size,
                               test_size=test_size)

print("Complete reading input data. Will Now print a snippet of it")
print("Number of files in Training-set:\t\t{}".format(len(data.train.labels)))
print("Number of files in Validation-set:\t{}".format(len(data.valid.labels)))
print("Number of files in Test-set:\t{}".format(len(data.test.labels)))

session = tf.Session()
x = tf.placeholder(tf.float32,
                   shape=[None, img_size, img_size, num_channels],
                   name='x')

## labels
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
# class info
num_classes = 2

# batch size
batch_size = 1

# validation split
validation_size = .1

# how long to wait after validation loss stops improving before terminating training
early_stopping = None  # use None if you don't want to implement early stopping

checkpoint_dir = 'model/'

# Load data
data = dataset.read_train_sets(batch_size=batch_size,
                               validation_size=validation_size)

print("Size of:")
print("- Training-set:\t\t{}".format(len(data.train.output)))
print("- Validation-set:\t{}".format(len(data.valid.output)))

D = len(data.train.input[1])
VD = len(data.valid.input[1])

for i in range(len(data.valid.output)):
    pass
    print('i = ', i, 'item = ', data.valid.output[i])

print('Input dimension', data.valid.input)  # (5394, 2655)
print('Input valid dimension', data.valid.output)  # (5394,)
Ejemplo n.º 14
0
# validation split
validation_size = .16

# how long to wait after validation loss stops improving before terminating training
early_stopping = None  # use None if you don't want to implement early stoping

train_path = 'data/train/'
test_path = 'data/test1/dogs/'
checkpoint_dir = "models/"

#data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)
test_images, test_ids = dataset.read_test_set(test_path, img_size)

data = dataset.read_train_sets(train_path,
                               img_size,
                               classes,
                               validation_size=validation_size)
print("Size of:")
print("- Training-set:\t\t{}".format(len(data.train.labels)))
print("- Test-set:\t\t{}".format(len(test_images)))
print("- Validation-set:\t{}".format(len(data.valid.labels)))


def plot_images(images, cls_true, cls_pred=None):

    assert len(images) == len(cls_true) == 9

    if len(images) == 0:
        print("no images to show")
        return
#    else:
Ejemplo n.º 15
0
def trainer(eval_type, dataset_type, input_type, validation_size, nfolds,
            batch_size, num_iter, classes, data_path):
    # Add seed for consistent random initialization
    from numpy.random import seed
    seed(1)
    from tensorflow import set_random_seed
    set_random_seed(2)

    # Define data for k-fold cross-validation
    #nfolds = 10
    total_true_labels = []
    total_predict_labels = []
    # Define data for subject-based validation
    #nfolds = 21 # number of subjects (people) # TT: 7

    #batch_size = 32

    # Prepare input data
    #classes = ['enthusiastic','neutral','concerned']
    num_classes = len(classes)

    # Define the percentage of the data that will automatically be used for validation
    #validation_size = 0.1
    img_size = 90
    num_channels = 1

    #train_path='data/classes/'
    train_path = data_path + 'classes/'

    if eval_type == dataset.VALID_METHOD_KFOLD:
        # Load training and validation images, dividing them in folds for cross-validation
        data = dataset.read_train_sets(train_path,
                                       img_size,
                                       classes,
                                       validation_size=validation_size,
                                       nfolds=nfolds,
                                       input_type=input_type)
    elif eval_type == dataset.VALID_METHOD_SUBJECT:
        # Load training and validation images, dividing them in subject-based folds for cross-validation
        # (each of the folds has all the data corresponding to the same subject -person-)
        data = dataset.read_train_sets_persubject(
            train_path,
            img_size,
            classes,
            validation_size=validation_size,
            nsubjects=nfolds,
            input_type=input_type,
            dataset_type=dataset_type)
    else:
        print 'WARNING: the validation method is not a valid method or does not exist'

    # Define parameters of neural network model
    own_filter_size_conv1 = 7
    own_num_filters_conv1 = 32

    own_filter_size_conv2 = 1
    own_num_filters_conv2 = 64

    own_filter_size_conv3 = 3
    own_num_filters_conv3 = 96

    own_filter_size_conv4 = 5
    own_num_filters_conv4 = 256

    own_fc_layer_size = 256
    own_fc_layer_size2 = 90

    def create_weights(shape):
        return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

    def create_biases(size):
        return tf.Variable(tf.constant(0.05, shape=[size]))

    def create_convolutional_layer(input, num_input_channels, conv_filter_size,
                                   num_filters, conv_stride, pool_filter_size,
                                   pool_stride):

        # Initialize the weights and biases (taking the values from a normal distribution) to be trained
        weights = create_weights(shape=[
            conv_filter_size, conv_filter_size, num_input_channels, num_filters
        ])
        biases = create_biases(num_filters)

        ## Create the convolutional layer
        layer = tf.nn.conv2d(input=input,
                             filter=weights,
                             strides=[1, conv_stride, conv_stride, 1],
                             padding='SAME')

        layer += biases

        ## Implement max-pooling
        layer = tf.nn.max_pool(
            value=layer,
            ksize=[1, pool_filter_size, pool_filter_size, 1],
            strides=[1, pool_stride, pool_stride, 1],
            padding='SAME')
        ## Feed the output of the max pooling to a ReLu activation function
        layer = tf.nn.relu(layer)

        return layer

    def create_flatten_layer(layer):
        # Get shape of previous layer
        layer_shape = layer.get_shape()
        ## Calculate number of features
        num_features = layer_shape[1:4].num_elements()
        ## Flatten the layer (length = number of features)
        layer = tf.reshape(layer, [-1, num_features])

        return layer

    def create_fc_layer(input, num_inputs, num_outputs, use_relu=True):

        # Define trainable weights and biases
        weights = create_weights(shape=[num_inputs, num_outputs])
        biases = create_biases(num_outputs)

        # Generate output (wx+b, where x is the input, w the weights and b the biases)
        layer = tf.matmul(input, weights) + biases
        if use_relu:
            layer = tf.nn.relu(layer)

        return layer

    # Start cross-validation
    for j in range(nfolds):

        session = tf.Session()
        if input_type == dataset.INPUT_FULLFACE:
            # (Note that 'None' allows the loading of any number of images)
            x = tf.placeholder(tf.float32,
                               shape=[None, img_size, img_size, num_channels],
                               name='x')  # normal version (non-cropped)
        elif input_type == dataset.INPUT_EYES:
            x = tf.placeholder(
                tf.float32,
                shape=[None, img_size / 3, img_size, num_channels],
                name='x')

        ## Create variable for ground truth labels
        y_true = tf.placeholder(tf.float32,
                                shape=[None, num_classes],
                                name='y_true')
        y_true_cls = tf.argmax(y_true, axis=1)

        # Create CNN
        layer_conv1 = create_convolutional_layer(
            input=x,
            num_input_channels=num_channels,
            conv_filter_size=own_filter_size_conv1,
            num_filters=own_num_filters_conv1,
            conv_stride=1,
            pool_filter_size=2,
            pool_stride=2)
        layer_conv2 = create_convolutional_layer(
            input=layer_conv1,
            num_input_channels=own_num_filters_conv1,
            conv_filter_size=own_filter_size_conv2,
            num_filters=own_num_filters_conv2,
            conv_stride=2,
            pool_filter_size=2,
            pool_stride=2)

        layer_conv3 = create_convolutional_layer(
            input=layer_conv2,
            num_input_channels=own_num_filters_conv2,
            conv_filter_size=own_filter_size_conv3,
            num_filters=own_num_filters_conv3,
            conv_stride=1,
            pool_filter_size=2,
            pool_stride=2)

        layer_conv4 = create_convolutional_layer(
            input=layer_conv3,
            num_input_channels=own_num_filters_conv3,
            conv_filter_size=own_filter_size_conv4,
            num_filters=own_num_filters_conv4,
            conv_stride=1,
            pool_filter_size=2,
            pool_stride=2)

        layer_flat = create_flatten_layer(layer_conv4)

        layer_fc1 = create_fc_layer(
            input=layer_flat,
            num_inputs=layer_flat.get_shape()[1:4].num_elements(),
            num_outputs=own_fc_layer_size,
            use_relu=True)

        layer_fc1_do = tf.nn.dropout(layer_fc1, keep_prob=0.5)

        layer_fc2 = create_fc_layer(input=layer_fc1_do,
                                    num_inputs=own_fc_layer_size,
                                    num_outputs=own_fc_layer_size2,
                                    use_relu=True)

        layer_fc3 = create_fc_layer(input=layer_fc2,
                                    num_inputs=own_fc_layer_size2,
                                    num_outputs=num_classes,
                                    use_relu=False)

        # Create variable for predicted labels
        y_pred = tf.nn.softmax(layer_fc3, name='y_pred')
        y_pred_cls = tf.argmax(y_pred, axis=1)
        session.run(tf.global_variables_initializer())

        cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
            logits=layer_fc3, labels=y_true)
        cost = tf.reduce_mean(cross_entropy)
        optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
        correct_prediction = tf.equal(y_pred_cls, y_true_cls)
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        session.run(tf.global_variables_initializer())

        print 'Current training data:'
        data.train.setCurrentDataset(nfolds, j)
        print 'Current validation data:'
        data.valid.setCurrentDataset(nfolds, j)
        '''print 'Current labels in training folds:'
        print data.train.cls
        print 'Current labels in validation folds:'
        print data.valid.cls'''
        def show_progress(epoch, feed_dict_train, feed_dict_validate,
                          val_loss):
            acc = session.run(accuracy, feed_dict=feed_dict_train)
            val_acc = session.run(accuracy, feed_dict=feed_dict_validate)
            msg = "Training Epoch {0} --- Training accuracy: {1:>6.1%}, validation accuracy: {2:>6.1%}, validation loss: {3:.3f}"
            print msg.format(epoch + 1, acc, val_acc, val_loss)

        global total_iterations
        total_iterations = 0

        saver = tf.train.Saver()

        def train(num_iteration):
            global total_iterations

            for i in range(total_iterations, total_iterations + num_iteration):
                x_batch, y_true_batch, _, cls_batch = data.train.next_batch(
                    batch_size, j)
                x_valid_batch, y_valid_batch, _, valid_cls_batch = data.valid.next_batch(
                    batch_size, j)

                feed_dict_tr = {x: x_batch, y_true: y_true_batch}
                feed_dict_val = {x: x_valid_batch, y_true: y_valid_batch}

                session.run(optimizer, feed_dict=feed_dict_tr)

                if i % int(data.train.num_examples[j] / batch_size) == 0:
                    val_loss = session.run(cost, feed_dict=feed_dict_val)
                    epoch = int(i /
                                int(data.train.num_examples[j] / batch_size))

                    show_progress(epoch, feed_dict_tr, feed_dict_val, val_loss)
                    saver.save(session,
                               data_path + 'models/face-exp-model{}'.format(j))

            total_iterations += num_iteration

        print "STARTING NEW TRAINING --- TEST FOLD: {} OUT OF {}".format(
            j + 1, nfolds)

        train(num_iteration=num_iter)
        print "Finished training for test fold", format(j)

        # Store the current test dataset (the fold not used during training)
        test_data = []
        test_data.extend(data.train.images[j])
        test_data.extend(data.valid.images[j])

        test_labels = []
        test_labels.extend(data.train.cls[j])
        test_labels.extend(data.valid.cls[j])

        # Test the trained CNN with the test data fold
        if input_type == dataset.INPUT_FULLFACE:
            predicted_labels = cnn_testing.testCNN(
                test_data, test_labels, j, img_size, img_size, data_path,
                classes)  # normal version (non-cropped)
        elif input_type == dataset.INPUT_EYES:
            predicted_labels = cnn_testing.testCNN(test_data, test_labels, j,
                                                   img_size / 3, img_size,
                                                   data_path, classes)
        else:
            print 'WARNING: type of input content is not valid!'

        total_true_labels.extend(test_labels)
        total_predict_labels.extend(predicted_labels)

        # Reset the graph for the new CNN model
        tf.reset_default_graph()

    # Print final performance measurements after the cross-validation
    confMat = sklearn.metrics.confusion_matrix(total_true_labels,
                                               total_predict_labels)
    print "Confusion matrix: (elements are C_ij, where i is the true class and j the predicted class)"
    print classes
    print confMat

    print sklearn.metrics.classification_report(total_true_labels,
                                                total_predict_labels,
                                                target_names=classes)
Ejemplo n.º 16
0
def main():

    #Prepare input data
    train_path = sys.argv[1]
    classes = os.listdir(train_path)
    num_classes = len(classes)

    # We shall load all the training and validation images and labels into memory using openCV
    # and use that during training
    data = dataset.read_train_sets(train_path,
                                   img_size,
                                   classes,
                                   validation_size=validation_size)

    print("Complete reading input data. Will Now print a snippet of it")
    print("Number of files in Training-set:\t\t{}".format(
        len(data.train.labels)))
    print("Number of files in Validation-set:\t{}".format(
        len(data.valid.labels)))

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.InteractiveSession(config=config)

    # TF variables
    x = tf.placeholder(tf.float32,
                       shape=[None, img_size, img_size, num_channels],
                       name='x')
    # labels
    y_true = tf.placeholder(tf.float32,
                            shape=[None, num_classes],
                            name='y_true')
    y_true_cls = tf.argmax(y_true, dimension=1)

    # CNN Archiecture
    layer_conv1 = create_convolutional_layer(
        input=x,
        num_input_channels=num_channels,
        conv_filter_size=filter_size_conv1,
        num_filters=num_filters_conv1)

    layer_conv2 = create_convolutional_layer(
        input=layer_conv1,
        num_input_channels=num_filters_conv1,
        conv_filter_size=filter_size_conv2,
        num_filters=num_filters_conv2)

    layer_conv3 = create_convolutional_layer(
        input=layer_conv2,
        num_input_channels=num_filters_conv2,
        conv_filter_size=filter_size_conv3,
        num_filters=num_filters_conv3)

    layer_conv4 = create_convolutional_layer(
        input=layer_conv3,
        num_input_channels=num_filters_conv3,
        conv_filter_size=filter_size_conv4,
        num_filters=num_filters_conv4)

    layer_conv5 = create_convolutional_layer(
        input=layer_conv4,
        num_input_channels=num_filters_conv4,
        conv_filter_size=filter_size_conv5,
        num_filters=num_filters_conv5)

    layer_conv6 = create_convolutional_layer(
        input=layer_conv5,
        num_input_channels=num_filters_conv5,
        conv_filter_size=filter_size_conv6,
        num_filters=num_filters_conv6)

    layer_conv7 = create_convolutional_layer(
        input=layer_conv6,
        num_input_channels=num_filters_conv6,
        conv_filter_size=filter_size_conv7,
        num_filters=num_filters_conv7)

    layer_conv8 = create_convolutional_layer(
        input=layer_conv7,
        num_input_channels=num_filters_conv7,
        conv_filter_size=filter_size_conv8,
        num_filters=num_filters_conv8)

    layer_flat = create_flatten_layer(layer_conv8)

    layer_fc1 = create_fc_layer(
        input=layer_flat,
        num_inputs=layer_flat.get_shape()[1:4].num_elements(),
        num_outputs=fc_layer_size,
        use_relu=True)

    layer_fc2 = create_fc_layer(input=layer_fc1,
                                num_inputs=fc_layer_size,
                                num_outputs=num_classes,
                                use_relu=False)

    y_pred = tf.nn.softmax(layer_fc2, name='y_pred')
    y_pred_cls = tf.argmax(y_pred, dimension=1)

    # Hyperparameters
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,
                                                            labels=y_true)
    cost = tf.reduce_mean(cross_entropy)
    optimiser = tf.train.GradientDescentOptimizer(learningRate).minimize(cost)
    correct_prediction = tf.equal(y_pred_cls, y_true_cls)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    val_accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # TF Summary Variables
    tf.summary.scalar('accuracy', accuracy)
    tf.summary.scalar('val_accuracy', val_accuracy)
    tf.summary.scalar('cost', cost)
    merged = tf.summary.merge_all()

    # Tensorboard summary file writers
    train_writer = tf.summary.FileWriter('summary/train_arch_12',
                                         session.graph)
    test_writer = tf.summary.FileWriter('summary/test_arch_12')

    session.run(tf.global_variables_initializer())
    saver = tf.train.Saver()

    # Train model
    train(session,
          data,
          x,
          y_true,
          cost,
          optimiser,
          accuracy,
          merged,
          val_accuracy,
          train_writer,
          test_writer,
          saver,
          num_iteration=2000)

    # Save a model checkpoint
    saver.save(session, './arch_12')
Ejemplo n.º 17
0
                help="path to the output loss/accuracy plot")
ap.add_argument("-m",
                "--model",
                required=True,
                help="path to save train model")
args = vars(ap.parse_args())

print("[INFO] loading dataset")

validation_size = 0.2
img_size = 48
num_channels = 2
train_path = '/media/data2/mhy/data/0911'
labelNames = ['r1', 'r2']

data = dataset.read_train_sets(train_path, img_size, labelNames,
                               validation_size)
print("Complete reading input data.Will Now print a snippet of it")

trainX = data.train.images
trainY = data.train.labels
testX = data.valid.images
testY = data.vaild.labels
# ((trainX, trainY), (testX, testY)) = cifar10.load_data()
# trainX = trainX.astype("float") / 255.0
# testX = testX.astype("float") / 255.0

lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

print("[INFO] compiling model...")
    #20% of the data will automatically be used for validation
    validation_size = 0.2
    img_size = 128
    num_channels = 3
    train_path=r'C:\Users\T01144\Desktop\T01144\IDLE Scripts\Image Classification\Using Tensorflow\data\train'

    #Prepare input data
    if not os.path.exists(train_path):
        print("No such directory")
        raise Exception
    classes = os.listdir(train_path)
    num_classes = len(classes)    

    # We shall load all the training and validation images and labels into memory using openCV and use that during training
    data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)

    # Display the stats
    print("Complete reading input data. Will Now print a snippet of it")
    print("Number of files in Training-set:\t\t{}".format(len(data.train.labels)))
    print("Number of files in Validation-set:\t{}".format(len(data.valid.labels)))
    session = tf.Session()
    x = tf.placeholder(tf.float32, shape=[None, img_size,img_size,num_channels], name='x')

    ## labels
    y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
    y_true_cls = tf.argmax(y_true, dimension=1)


    ##Network graph params
    filter_size_conv1 = 3 
Ejemplo n.º 19
0
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

IMG_SIZE = 50       #resolution to which images are resized
ALPHA = .0001        #learning rate

trainDirectory = "./training_data"
testDirectory = "./testing_data"

#define name for the model, change if # of convolutional layers is changed
modelName = "corgiClass-{}--{}".format(ALPHA, '4_conv_layers_final')

#define training dataset, 20% are for validation
dSet = dataset.read_train_sets("./data/training_data", IMG_SIZE, ['pembroke', 'cardigan'], .2)

#define test dataset, since its the test, 0 are for validation
testSet = dataset.read_train_sets("./data/training_data", IMG_SIZE, ['pembroke', 'cardigan'], 0)

# Building convolutional neural net

#input layer
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input')

#convolutional layers
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)