Пример #1
0
def Cnn_test():
    # initializing the network
    network = Cnn(BATCH_SIZE)
    network.getTheParas(MODEL_FILE)

    # load the test data
    _, _, test_imgs, _, _, test_label = util.load_data(MNIST_PATH, False)

    log_string('------------start test-----------')

    num_batch = test_imgs.shape[0] // BATCH_SIZE
    start = 0
    end = start + BATCH_SIZE
    loss = 0.0
    total_correct = 0.0
    total_seen = 0
    for n in range(num_batch):
        log_string('testing {}/{}(batchs) completed!'.format(n + 1, num_batch))
        current_img = test_imgs[start:end, ...]
        current_label = test_label[start:end, ...]
        start = end
        end += BATCH_SIZE
        predict_val, loss_val = network.forward(current_img, current_label)
        correct = np.sum(predict_val == current_label)
        total_correct += correct
        loss += loss_val
        total_seen += BATCH_SIZE
    log_string('eval mean loss: {}'.format(loss / num_batch))
    log_string('eval accuracy: {}'.format(total_correct / total_seen))
Пример #2
0
x_train_orig, y_train_orig, x_test_orig, y_test_orig, classes = util.load_data_set(
)
x_train = util.pre_treat(x_train_orig)
x_test = util.pre_treat(x_test_orig)
y_train = util.pre_treat(y_train_orig, is_x=False, class_num=len(classes))
y_test = util.pre_treat(y_test_orig, is_x=False, class_num=len(classes))

cnn = Cnn(config.conv_layers, config.fc_layers, config.filters,
          config.learning_rate, config.beta1, config.beta2)

(m, n_H0, n_W0, n_C0) = x_train.shape
n_y = y_train.shape[1]

# construction calculation graph
cnn.initialize(n_H0, n_W0, n_C0, n_y)
cnn.forward()
cost = cnn.cost()
optimizer = cnn.get_optimizer(cost)
predict, accuracy = cnn.predict()

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for i in range(1, config.num_epochs + 1):
        num_mini_batches = int(m / config.mini_batch_size)
        # seed += 1
        mini_batches = util.random_mini_batches(x_train, y_train,
                                                config.mini_batch_size)