Exemple #1
0
def main():
    savepath = './save_point'
    filepath = './save_point/keras_example_checkpoint.h5'

    # Extract MNIST dataset
    train_data_filename = maybe_download('train-images-idx3-ubyte.gz')
    train_labels_filename = maybe_download('train-labels-idx1-ubyte.gz')
    test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
    test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')

    train_data = extract_data(train_data_filename, 60000, dense=False)
    train_data = train_data.reshape((60000, NUM_CHANNELS, IMG_SIZE, IMG_SIZE))
    train_labels = extract_labels(train_labels_filename, 60000, one_hot=True)
    test_data = extract_data(test_data_filename, 10000, dense=False)
    test_data = test_data.reshape((10000, NUM_CHANNELS, IMG_SIZE, IMG_SIZE))
    test_labels = extract_labels(test_labels_filename, 10000, one_hot=True)

    validation_data = train_data[:VALIDATION_SIZE, ...]
    validation_labels = train_labels[:VALIDATION_SIZE, :]
    validation_set = (validation_data, validation_labels)
    train_data = train_data[VALIDATION_SIZE:, ...]
    train_labels = train_labels[VALIDATION_SIZE:, ...]

    # Model construction
    model = Sequential()
    model.add(Convolution2D(32, 3, 3, border_mode='same',
              input_shape=(1, 28, 28)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(64, 3, 3, border_mode='same'))
    model.add(Flatten())
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(10))
    model.add(Activation('softmax'))

    # Define optimizer and configure training process
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"])

    model.fit(
        train_data,
        train_labels,
        nb_epoch=NUM_EPOCHS,
        batch_size=1000,
        validation_data=validation_set)

    print 'Save model weights'
    if not os.path.isdir (savepath):
        os.mkdir (savepath)
    model.save_weights(filepath, overwrite=True)

    predict = model.predict(test_data, batch_size=1000)

    print 'Test err: %.1f%%' % error_rate(predict, test_labels)

    print 'Test loss: %1.f%%, accuracy: %1.f%%', \
        tuple(model.evaluate(test_data, test_labels, batch_size=1000))
Exemple #2
0
def get_label_count(dataset):
    """Computes the label count dictionary: labels to their occurrence counts
    inside the dataset.

    Args:
        dataset (list(dict(str))): A dataset, i.e., a list of json objects fitting
            the Golden Dataset Schema.
        alpha (float): Alpha parameter in balance-aware data augmentation.
        beta (float): Beta parameter in balance-aware data augmentation.

    Returns:
        dict(str): Labels mapped to the amount of times they occur in the dataset.
    """
    label_count = defaultdict(int)
    for label_list in [extract_labels(js) for js in dataset]:
        for label in label_list:
            label_count[label] += 1

    return label_count
Exemple #3
0
def rareness_score(paper, label_count, alpha, beta):
    '''
        Computes a rareness score for a json paper.

                 / sum_{label in paper_labels} (1 / count(label)) ^ ALPHA \ ^ BETA
        Score =  | ------------------------------------------------------ |
                 \                 number of labels in paper              /

    Args:
        paper (dict): a json dictionary, a paper from the golden dataset (or formatted as such)
        label_count (dict(str)): Labels mapped to the amount of times they occur in the dataset.
        alpha (float): Alpha parameter in balance-aware data augmentation.
        beta (float): Beta parameter in balance-aware data augmentation.
    Returns:
        score (float): computed rareness score

    '''
    paper_labels = extract_labels(paper)
    if not paper_labels:
        return 0.0
    score = 0.0
    for paper_label in paper_labels:
        score += (1 / label_count[paper_label])**alpha
    return (score / len(paper_labels))**beta
def load_data(dataset_path, data_out, batch_size):
    face_detector = ort.InferenceSession('models/version-RFB-320.onnx')
    face_detector_input = face_detector.get_inputs()[0].name

    paths = pathlib.Path(dataset_path).glob('*.jpg')
    paths = sorted([x for x in paths])
    random.shuffle(paths)

    faces = []
    bmis = []

    if os.path.exists(data_out):
        shutil.rmtree(data_out)
    os.mkdir(data_out)

    current_batch = 0

    for image_path in paths:
        face = extract_face(image_path, face_detector, face_detector_input)
        if face is None:
            continue

        faces.append(face)

        bmi = extract_labels(image_path)
        bmis.append(bmi)

        if len(faces) == batch_size:
            write_batch(current_batch, faces, bmis, data_out)
            current_batch += 1
            faces.clear()
            bmis.clear()

    # Write any remaining data to the batch.
    if len(faces) > 0:
        write_batch(current_batch, faces, bmis, data_out)
Exemple #5
0
def split(prefix='MATCH/PeTaL',
          dataset='cleaned_lens_output.json',
          train=0.8,
          dev=0.1,
          skip=0,
          tot=0,
          infer_mode=False,
          verbose=False):
    """Performs train-test split on newline-delimited json file.	

	Args:
		prefix (string): Path from current working directory to directory containing dataset.
		dataset (string): Filename of newline-delimited json dataset.
		train (float): Proportion, from 0.0 to 1.0, of dataset used for training.
		dev (float): Proportion, from 0.0 to 1.0, of dataset used for validation.
		skip (int): Number of training examples by which to rotate the dataset (e.g., for cross-validation).
		tot (int): Total number of training examples.
        infer_mode (bool): Whether to run in inference mode.
		verbose (bool): Verbose output.
	"""

    logging.basicConfig(level=logging.DEBUG,
                        format="[%(asctime)s:%(name)s] %(message)s")
    logger = logging.getLogger("Split")

    ########################################
    #
    # INPUT VALIDATION CHECKS
    #
    ########################################

    dataset_path = os.path.join(prefix, dataset)
    if not os.path.exists(dataset_path):
        logger.error(
            f"ERROR: Unable to find dataset json file {os.path.join(os.getcwd(), dataset_path)}."
        )
        return

    if train < 0:
        logger.error(f"ERROR: train proportion {train} is less than 0.")
        return
    elif dev < 0:
        logger.error(f"ERROR: dev proportion {dev} is less than 0.")
        return
    elif train + dev >= 1:
        logger.error(
            f"ERROR: train proportion {train} + dev proportion {dev} exceeds 1."
        )
        return
    elif tot < 0:
        logger.error(f"ERROR: total parameter (tot) {tot} is less than 0.")
        return

    ########################################
    #
    # INFERENCE MODE
    # 	just copy the whole dataset into test.json
    #
    ########################################

    if infer_mode:
        infer_path = os.path.join(prefix, 'test.json')

        if verbose:
            logger.info(
                f"Copying from {dataset_path} to {infer_path} for inference mode."
            )

        with open(dataset_path) as fin, open(infer_path, 'w') as fout:
            golden = json.loads(fin.read())
            for js in golden:
                fout.write(json.dumps(js) + '\n')

    ########################################
    #
    # NOT INFERENCE MODE:
    #	do train-validation-test split
    #
    ########################################

    else:
        train_path = os.path.join(prefix, 'train.json')
        dev_path = os.path.join(prefix, 'dev.json')
        test_path = os.path.join(prefix, 'test.json')

        if verbose:
            logger.info(
                f"Transforming from {dataset_path} to {train_path}, {dev_path}, and {test_path}."
            )

        train_proportion, dev_proportion = train, dev

        train_labels = set()

        ########################################
        # If the default value 0 was passed in as tot,
        # count the number of examples in the dataset
        # and use that as the total number of examples.
        # Otherwise stick with the passed-in total argument,
        # unless that exceeds the actual number of examples in the dataset.
        ########################################
        with open(dataset_path) as fin:
            num_examples = sum(1 for _ in json.loads(fin.read()))
            if tot == 0 or tot > num_examples:
                tot = num_examples
        if verbose:
            logger.info(f"{tot} total examples in dataset.")

        ########################################
        # This part figures out what labels appear in the training set
        # and then filters out all other labels from the dev and testing sets
        # while constructing train.json, dev.json, and test.json.
        ########################################

        with open(dataset_path) as fin, open(train_path, 'w') as fou1, open(
                dev_path, 'w') as fou2, open(test_path, 'w') as fou3:
            golden = json.loads(fin.read())
            # for idx, line in enumerate(fin):
            for idx, js in enumerate(golden):
                if idx < skip or idx >= tot:
                    continue
                # js = json.loads(line)

                ######################################## moved to utils.extract_labels
                # level1Labels = js['level1'] if js['level1'] else []
                # level2Labels = js['level2'] if js['level2'] else []
                # level3Labels = js['level3'] if js['level3'] else []
                # all_labels = level1Labels + level2Labels + level3Labels
                ########################################

                all_labels = extract_labels(js)

                if (idx - skip) % tot < tot * train_proportion:
                    for l in all_labels:
                        train_labels.add(l)

                    js['label'] = all_labels
                    fou1.write(json.dumps(js) + '\n')

                else:
                    label_new = []
                    for l in all_labels:
                        if l in train_labels:
                            label_new.append(l)
                    if len(label_new) == 0:
                        continue

                    js['label'] = label_new
                    # print(js['label'])
                    # print([x for x in js.keys()])
                    if (idx - skip) % tot < tot * (train_proportion +
                                                   dev_proportion):
                        fou2.write(json.dumps(js) + '\n')
                    else:
                        fou3.write(json.dumps(js) + '\n')

            # fin.seek(0)

            # for idx, line in enumerate(fin):
            for idx, js in enumerate(golden):
                if idx >= skip:
                    break
                # js = json.loads(line)

                ######################################## moved to utils.extract_labels
                # level1Labels = js['level1'] if js['level1'] else []
                # level2Labels = js['level2'] if js['level2'] else []
                # level3Labels = js['level3'] if js['level3'] else []
                # all_labels = level1Labels + level2Labels + level3Labels
                ########################################

                all_labels = extract_labels(js)

                if (idx - skip) % tot < tot * train_proportion:
                    for l in all_labels:
                        train_labels.add(l)

                    js['label'] = all_labels
                    fou1.write(json.dumps(js) + '\n')

                else:
                    label_new = []
                    for l in all_labels:
                        if l in train_labels:
                            label_new.append(l)
                    if len(label_new) == 0:
                        continue

                    js['label'] = label_new
                    if (idx - skip) % tot < tot * (train_proportion +
                                                   dev_proportion):
                        fou2.write(json.dumps(js) + '\n')
                    else:
                        fou3.write(json.dumps(js) + '\n')

        if verbose:
            logger.info(f"Number of train labels: {len(train_labels)}")
        '''
Exemple #6
0
        for vector in direction_vectors
    ])
    print X.shape
    y = np.concatenate([y for _ in range(len(direction_vectors) + 1)], axis=0)
    print y.shape
    return X, y


# Extract data
train_data_filename = maybe_download('train-images-idx3-ubyte.gz')
train_labels_filename = maybe_download('train-labels-idx1-ubyte.gz')
test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')

X_train = extract_data(train_data_filename, 60000, dense=True)
y_train = extract_labels(train_labels_filename, 60000, one_hot=False)
X_test = extract_data(test_data_filename, 10000, dense=True)
y_test = extract_labels(test_labels_filename, 10000, one_hot=False)

#################################################
# Test for decision tree classifier without dimensionality reduction
Tree = DecisionTreeClassifier()
Tree.fit(X_train, y_train)
print 'Without dimenstionality reduction: ', Tree.score(X_test, y_test)

# Dimensionality reduction using PCA (784 -> 64)
pca = PCA(n_components=64)
pca.fit(X_train)
X_train_reduce = pca.transform(X_train)

Tree_2 = DecisionTreeClassifier()
Exemple #7
0
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Parser")
    parser.add_argument("--image_path",
                        type=str,
                        required=True,
                        help="path to image")
    parser.add_argument("--segmentation_path",
                        type=str,
                        required=True,
                        help="path to semantic segmentation")
    parser.add_argument("--label_path",
                        type=str,
                        required=True,
                        help="path to text file with label names")
    args = parser.parse_args()

    image = load_image(args.image_path)
    semseg = load_segmentation(args.segmentation_path)
    labels = extract_labels(args.label_path)

    print(f"+ Segmentation shape: {semseg.shape}")

    # Extract image segments from semantic segmentation
    segments = extract_segments(semseg)

    print(f"+ Number of segments: {len(segments)}")

    # Trigger GUI and visualize segments
    visualize(image, segments, labels)
Exemple #8
0
                       [np.apply_along_axis(shift, 1, X, vector)
                        for vector in direction_vectors])
    print X.shape
    y = np.concatenate([y for _ in range(len(direction_vectors) + 1)], axis=0)
    print y.shape
    return X, y


# Extract data
train_data_filename = maybe_download('train-images-idx3-ubyte.gz')
train_labels_filename = maybe_download('train-labels-idx1-ubyte.gz')
test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')

X_train = extract_data(train_data_filename, 60000, dense=True)
y_train = extract_labels(train_labels_filename, 60000, one_hot=False)
X_test = extract_data(test_data_filename, 10000, dense=True)
y_test = extract_labels(test_labels_filename, 10000, one_hot=False)


#################################################
# Test for decision tree classifier without dimensionality reduction
Tree = DecisionTreeClassifier()
Tree.fit(X_train, y_train)
print 'Without dimenstionality reduction: ', Tree.score(X_test, y_test)

# Dimensionality reduction using PCA (784 -> 64)
pca = PCA(n_components=64)
pca.fit(X_train)
X_train_reduce = pca.transform(X_train)
Exemple #9
0
def main():
    if (len(sys.argv) != 5):
        sys.exit("invalid command-line arguments format")

    # handling command-line arguments
    data = DataReader2(sys.argv[1])
    data.init_examples()
    training_set_size = int(sys.argv[2])
    num_trials = int(sys.argv[3])
    verbose = int(sys.argv[4])
    if (verbose != 1 and verbose != 0):
        sys.exit("invalid command-line argument")
    if (num_trials < 1):
        sys.exit("invalid command-line argument")

    # extract examples and attributes
    # an example = a feature vector + a label (represented by a tuple)
    examples = data.get_examples()  # a list of examples
    attributes = data.get_attributes()  # a list of attribute names
    if (training_set_size >= len(examples)):
        sys.exit("invalid command-line argument")

    # lists of classification performances (correct rates)
    # e.x.: [1.0, 0.95, 0.83, ...]
    correct_rates_id3 = []
    correct_rates_prior = []

    for i in range(0, num_trials):  # a single trial
        print 'TRIAL NUMBER:', i + 1
        print '-' * 30

        # randomly pick a training set of size *training_set_size*
        random.shuffle(examples)
        training_examples = examples[0:training_set_size]
        testing_examples = examples[training_set_size:]

        # a list of actual labels of testing examples
        actuals = utils.extract_labels(testing_examples)

        # build a decision tree based on these training examples
        tree = id3.DTL(training_examples, range(0, len(attributes)), True)
        # print the structure of the decision tree built from the training set
        print 'DECISION TREE STRUCTURE'
        utils.print_tree(tree, attributes)

        # list of predicted labels using id3
        output_id3_1 = utils.trial_id3(tree, testing_examples)
        # list of predicted labels using prior probability
        output_prior_1 = utils.trial_priorprob(training_examples,
                                               testing_examples)

        # computes and prints correct rate of this trial
        correct_rate_id3 = utils.correct_rate(output_id3_1, actuals)
        correct_rates_id3.append(correct_rate_id3)
        correct_rate_prior = utils.correct_rate(output_prior_1, actuals)
        correct_rates_prior.append(correct_rate_prior)
        print '\n'
        print 'proportion of correct classification'
        print 'decision tree:', correct_rate_id3
        print 'prior probability:', correct_rate_prior
        print '\n'

        if (verbose == 1):
            output_id3_2 = list(testing_examples)
            output_prior_2 = list(testing_examples)
            for j in range(0, len(output_id3_2)):
                output_id3_2[j][-1] = output_id3_1[j]
                output_prior_2[j][-1] = output_prior_1[j]

            print '*' * 10, 'examples in the training set: ', '*' * 10
            utils.print_dataset(training_examples, attributes)

            print '*' * 10, 'examples in the testing set: ', '*' * 10
            utils.print_dataset(testing_examples, attributes)

            print '*' * 10, 'classification by the decision tree: ', '*' * 10
            utils.print_dataset(output_id3_2, attributes)

            print '*' * 10, 'classification by prior probability: ', '*' * 10
            utils.print_dataset(output_prior_2, attributes)

    # other outputs
    print '*' * 5, 'information', '*' * 5
    print 'file:' + sys.argv[1]
    print 'training set size:', sys.argv[2]
    print 'testing set size:', len(examples) - int(sys.argv[2])
    print 'number of trials:', num_trials
    mean_tree = utils.mean(correct_rates_id3)
    mean_prior = utils.mean(correct_rates_prior)
    print 'mean classification performance (decision tree):', mean_tree
    print 'mean classification performance (prior probability):', mean_prior
Exemple #10
0
def main(argv=None):  # pylint: disable=unused-argument
  # Get the data.
  train_data_filename = maybe_download('train-images-idx3-ubyte.gz')
  train_labels_filename = maybe_download('train-labels-idx1-ubyte.gz')
  test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
  test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')


  # Extract it into numpy arrays.
  train_data = extract_data(train_data_filename, 60000, dense=False)
  train_labels = extract_labels(train_labels_filename, 60000, one_hot=True)
  test_data = extract_data(test_data_filename, 10000, dense=False )
  test_labels = extract_labels(test_labels_filename, 10000, one_hot=True)


  # Generate a validation set.
  validation_data = train_data[:VALIDATION_SIZE, ...]
  validation_labels = train_labels[:VALIDATION_SIZE]
  train_data = train_data[VALIDATION_SIZE:, ...]
  train_labels = train_labels[VALIDATION_SIZE:]
  num_epochs = NUM_EPOCHS
  train_size = train_labels.shape[0]

  # This is where training samples and labels are fed to the graph.
  # These placeholder nodes will be fed a batch of training data at each
  # training step using the {feed_dict} argument to the Run() call below.
  train_data_node = tf.placeholder(
      tf.float32,
      shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
  train_labels_node = tf.placeholder(tf.float32,
                                     shape=(BATCH_SIZE, NUM_LABELS))
  eval_data = tf.placeholder(
      tf.float32,
      shape=(EVAL_BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))

  # The variables below hold all the trainable weights. They are passed an
  # initial value which will be assigned when when we call:
  # {tf.initialize_all_variables().run()}

  # First convolutional layer
  conv1_weights = tf.Variable(
      tf.truncated_normal([3, 3, NUM_CHANNELS, 32],  # 5x5 filter, depth 32.
                          stddev=0.1,
                          seed=SEED))
  conv1_biases = tf.Variable(tf.zeros([32]))

  # Two second convolutional layers 5 x 5 filter, and 3 x 3 filters.
  conv2_weights = tf.Variable(
      tf.truncated_normal([5, 5, 32, 64],
                          stddev=0.1,
                          seed=SEED))
  conv2_biases = tf.Variable(tf.constant(0.01, shape=[64]))

  conv2_weights2 = tf.Variable(
      tf.truncated_normal([3, 3, 32, 64],
                          stddev=0.1,
                          seed=SEED))
  conv2_biases2 = tf.Variable(tf.constant(0.01, shape=[64]))

  # First fully connected layer after conv layer
  fc1_weights = tf.Variable(  # fully connected, depth 512.
      tf.truncated_normal(
          [IMAGE_SIZE // 4 * IMAGE_SIZE // 4 * 128, 512],
          stddev=0.05,
          seed=SEED))
  fc1_biases = tf.Variable(tf.constant(0.01, shape=[512]))

  # Second fully connected layer
  fc2_weights = tf.Variable(
      tf.truncated_normal([512, 256],
                          stddev=0.05,
                          seed=SEED))
  fc2_biases = tf.Variable(tf.constant(0.1, shape=[256]))

  # Output layer
  fc3_weights = tf.Variable(
      tf.truncated_normal([256, NUM_LABELS],
                          stddev=0.04,
                          seed=SEED))
  fc3_biases = tf.Variable(tf.constant(0.1, shape=[NUM_LABELS]))


  # We will replicate the model structure for the training subgraph, as well
  # as the evaluation subgraphs, while sharing the trainable parameters.
  def model(data, train=False):
    """The Model definition."""
    # 2D convolution, with 'SAME' padding (i.e. the output feature map has
    # the same size as the input). Note that {strides} is a 4D array whose
    # shape matches the data layout: [image index, y, x, depth].
    conv = tf.nn.conv2d(data,
                        conv1_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    # Bias and rectified linear non-linearity.
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))
    if train:
        relu = tf.nn.dropout(relu, .5)
    # Max pooling. The kernel size spec {ksize} also follows the layout of
    # the data. Here we have a pooling window of 2, and a stride of 2.
    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    conv = tf.nn.conv2d(pool,
                        conv2_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv2_biases))
    conv2 = tf.nn.conv2d(pool,
                         conv2_weights2,
                         strides=[1, 1, 1, 1],
                         padding='SAME')
    relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases2))

    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    pool2 = tf.nn.max_pool(relu2,
                           ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME')
    # Reshape the feature map cuboid into a 2D matrix to feed it to the
    # fully connected layers.
    pool = tf.concat(3, [pool, pool2])
    pool_shape = pool.get_shape().as_list()
    reshape = tf.reshape(
        pool,
        [pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])
    # Fully connected layer. Note that the '+' operation automatically
    # broadcasts the biases.
    hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
    hidden = tf.nn.relu(tf.matmul(hidden, fc2_weights) + fc2_biases)
    # Add a 50% dropout during training only. Dropout also scales
    # activations such that no rescaling is needed at evaluation time.
    if train:
      hidden = tf.nn.dropout(hidden, 0.5, seed=SEED)
    return tf.matmul(hidden, fc3_weights) + fc3_biases

  def extract_filter (data):
    conv = tf.nn.conv2d(data,
                        conv1_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    # Bias and rectified linear non-linearity.
    relu1 = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))

    # Max pooling. The kernel size spec {ksize} also follows the layout of
    # the data. Here we have a pooling window of 2, and a stride of 2.
    pool = tf.nn.max_pool(relu1,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    conv = tf.nn.conv2d(pool,
                        conv2_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    relu2 = tf.nn.relu(tf.nn.bias_add(conv, conv2_biases))
    conv2 = tf.nn.conv2d(pool,
                         conv2_weights2,
                         strides=[1, 1, 1, 1],
                         padding='SAME')
    relu3 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases2))

    return relu1, relu2, relu3


  # Training computation: logits + cross-entropy loss.
  logits = model(train_data_node, True)
  loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
      logits, train_labels_node))

  # L2 regularization for the fully connected parameters.
  regularizers = (tf.nn.l2_loss(fc1_weights) + tf.nn.l2_loss(fc1_biases) +
                  tf.nn.l2_loss(fc2_weights) + tf.nn.l2_loss(fc2_biases) +
                  tf.nn.l2_loss(fc3_weights) + tf.nn.l2_loss(fc3_biases))
  # Add the regularization term to the loss.
  loss += 5e-4 * regularizers

  # Optimizer: set up a variable that's incremented once per batch and
  # controls the learning rate decay.
  batch = tf.Variable(0)
  # Decay once per epoch, using an exponential schedule starting at 0.01.
  learning_rate = tf.train.exponential_decay(
      0.01,                # Base learning rate.
      batch * BATCH_SIZE,  # Current index into the dataset.
      train_size,          # Decay step.
      0.95,                # Decay rate.
      staircase=True)
  # Use simple momentum for the optimization.
  optimizer = tf.train.MomentumOptimizer(learning_rate,
                                         0.9).minimize(loss,
                                                       global_step=batch)

  # Predictions for the current training minibatch.
  train_prediction = tf.nn.softmax(logits)

  # Predictions for the test and validation, which we'll compute less often.
  eval_prediction = tf.nn.softmax(model(eval_data))

  # Small utility function to evaluate a dataset by feeding batches of data to
  # {eval_data} and pulling the results from {eval_predictions}.
  # Saves memory and enables this to run on smaller GPUs.
  def eval_in_batches(data, sess):
    """Get all predictions for a dataset by running it in small batches."""
    size = data.shape[0]
    if size < EVAL_BATCH_SIZE:
      raise ValueError("batch size for evals larger than dataset: %d" % size)
    predictions = numpy.ndarray(shape=(size, NUM_LABELS), dtype=numpy.float32)
    for begin in xrange(0, size, EVAL_BATCH_SIZE):
      end = begin + EVAL_BATCH_SIZE
      if end <= size:
        predictions[begin:end, :] = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[begin:end, ...]})
      else:
        batch_predictions = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[-EVAL_BATCH_SIZE:, ...]})
        predictions[begin:, :] = batch_predictions[begin - size:, :]
    return predictions

  # Create a local session to run the training.
  saver = tf.train.Saver()
  start_time = time.time()
  with tf.Session() as sess:
    # Run all the initializers to prepare the trainable parameters.
    if FLAGS.model:
      saver.restore(sess, FLAGS.model)  # If model exists, load it
    else:
      sess.run(tf.initialize_all_variables())  # If there is no model randomly initialize
    if FLAGS.train:
      # Loop through training steps.
      for step in xrange(int(num_epochs * train_size) // BATCH_SIZE):
        # Compute the offset of the current minibatch in the data.
        # Note that we could use better randomization across epochs.
        offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)
        batch_data = train_data[offset:(offset + BATCH_SIZE), ...]
        batch_labels = train_labels[offset:(offset + BATCH_SIZE)]
        # This dictionary maps the batch data (as a numpy array) to the
        # node in the graph is should be fed to.
        feed_dict = {train_data_node: batch_data,
                     train_labels_node: batch_labels}
        # Run the graph and fetch some of the nodes.
        _, l, lr, predictions = sess.run(
            [optimizer, loss, learning_rate, train_prediction],
            feed_dict=feed_dict)
        if step % EVAL_FREQUENCY == 0:
          elapsed_time = time.time() - start_time
          start_time = time.time()
          print('Step %d (epoch %.2f), %.1f ms' %
                (step, float(step) * BATCH_SIZE / train_size,
                 1000 * elapsed_time / EVAL_FREQUENCY))
          print('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))
          print('Minibatch error: %.1f%%' % error_rate(predictions, batch_labels))
          print('Validation error: %.1f%%' % error_rate(
              eval_in_batches(validation_data, sess), validation_labels))
          sys.stdout.flush()
      # Finally print the result!
      test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
      print('Test error: %.1f%%' % test_error)
      print ('Optimization done')
      print ('Save models')
      if not tf.gfile.Exists("./conv_save"):
          tf.gfile.MakeDirs("./conv_save")
      saver_path = saver.save(sess, "./conv_save/model.ckpt")
      print ('Successfully saved file: %s' % saver_path)
    else:  # If train flag is false, execute image extraction routine
      print ("Filter extraction routine")
      aa = train_data[1:2, :, :, :]
      print (aa.shape)
      # Run extract filter operations (conv1, conv2 and conv3 layers)
      images = sess.run(extract_filter(train_data[1:2, :, :, :]))
      print (images[2].shape)
      plt.imshow (images[2][0, :, :, 32] * 255 + 255 / 2, cmap='gray')
      # plt.imshow (images[2][0, :, :, 32], cmap='gray')
      plt.show ()
      # Save all outputs
      for i in range (3):
        filter_shape = images[i].shape
        img_size = [filter_shape[1], filter_shape[2]]
        print (img_size)
Exemple #11
0
def main():
	savepath = './save_point'
	filepath = './save_point/model_api_checkpoint.h5'
	train_data_filename = maybe_download('train-images-idx3-ubyte.gz')
	train_labels_filename = maybe_download('train-labels-idx1-ubyte.gz')
	test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
	test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')

	train_data = extract_data(train_data_filename, 60000, dense=False)
	train_data = train_data.reshape((60000, NUM_CHANNELS, IMG_SIZE, IMG_SIZE))
	train_labels = extract_labels(train_labels_filename, 60000, one_hot=True)
	test_data = extract_data(test_data_filename, 10000, dense=False)
	test_data = test_data.reshape((10000, NUM_CHANNELS, IMG_SIZE, IMG_SIZE))
	test_labels = extract_labels(test_labels_filename, 10000, one_hot=True)

	validation_data = train_data[:VALIDATION_SIZE, ...]
	validation_labels = train_labels[:VALIDATION_SIZE, :]
	validation_set = (validation_data, validation_labels)
	train_data = train_data[VALIDATION_SIZE:, ...]
	train_labels = train_labels[VALIDATION_SIZE:, ...]

	img = Input(shape=(1, 28, 28))
	conv1 = Convolution2D(32, 3, 3, border_mode='same')(img)
	conv1 = Activation('relu')(conv1)
	pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
	conv2_1 = Convolution2D(64, 3, 3, border_mode='same')(pool1)
	conv2_2 = Convolution2D(64, 5, 5, border_mode='same')(pool1)
	conv2_1 = Activation('relu')(conv2_1)
	conv2_2 = Activation('relu')(conv2_2)
	pool2_1 = MaxPooling2D(pool_size=(2, 2))(conv2_1)
	pool2_2 = MaxPooling2D(pool_size=(2, 2))(conv2_2)
	dense1 = Flatten()(pool2_1)
	dense2 = Flatten()(pool2_2)
	dense = merge([dense1, dense2], mode='concat', concat_axis=1)
	dense = Dense(512)(dense)
	dense = Activation('relu')(dense)
	dense = Dense(256)(dense)
	dense = Activation('relu')(dense)
	dense = Dense(10)(dense)
	output = Activation('softmax')(dense)

	model = Model(input=[img], output=[output])

	sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9)

	model.compile(
					optimizer=sgd,
					loss=['categorical_crossentropy'],
					metrics=["accuracy"])

	model.fit(
					[train_data],
					[train_labels],
					nb_epoch=1,
					verbose=1,
					batch_size=1000,
					validation_data=validation_set)

	print 'Save model weights'
	if not os.path.isdir (savepath):
		os.mkdir (savepath)
	model.save_weights(filepath, overwrite=True)


	predictions = model.predict([test_data],
	                            batch_size=1000)

	print 'Test error: %.1f%%' % error_rate(predictions, test_labels)

	print 'Test loss: %.14f, Test accurracy %.4f' % \
	      tuple(model.evaluate([test_data], [test_labels], batch_size=1000))