为什么我的CNN模型没有学到什么? (tensorflow)

时间:2017-07-04 05:57:39

标签: python tensorflow classification conv-neural-network

我一直在尝试根据this tutorial

制作图像分类器

我已经改变了一些内容,我认为这不会完全打破学习模式,但他们确实......准确度计算一直与随机猜测一样好,没有向上的曲线,甚至没有慢一点。

正如您在代码中看到的,我所做的唯一重大改变是使用我自己的数据集而不是MNIST数据集,而我的数据是89个类的字符图片,而不仅仅是10.我随机应用在将它们分配给训练师之前的噪音和扭曲,我在89个班级中每个都有大约100,000个例子。

我在唯一更改的行旁边注释,以帮助您阅读更改

代码(python):

def model(features, labels, mode):

    #Change from the tutorial - 36*36 images instead of 28*28
    input_layer = tf.reshape(features, [-1, 36, 36, 1])

    conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 5], padding='same', activation=tf.nn.relu)
    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
    conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu)
    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

    # Change From the tutorial - 9*9*64 instead of 7*7*64 to match different image size
    pool2_flat = tf.reshape(pool2, [-1, 9 * 9 * 64])

    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
    dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == learn.ModeKeys.TRAIN)

    # Change from tutorial = 89 classes instead of 10
    logits = tf.layers.dense(inputs=dropout, units=89)
    onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=89)

    loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits)
    train_op = tf.contrib.layers.optimize_loss(
            loss=loss,
            global_step=tf.contrib.framework.get_global_step(),
            learning_rate=0.001,
            optimizer='SGD'
    )
    # Generate Predictions
    predictions = {
        "classes": tf.argmax(input=logits, axis=1),
        "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
    }

    return model_fn.ModelFnOps(mode=mode, predictions=predictions, loss=loss, train_op=train_op)

我认为网络参数可能很小,可以处理36 * 36个图片和89个类,所以我尝试给conv层提供更多功能,或增加密集层节点,但似乎没有任何改变。我看到的张量板中唯一的结果是准确度为1%-1.5%,这非常适合89个班级的随机猜测。

我是张力流和机器学习的真正新手,所以请对我温柔,并告诉我,我做错了。

编辑:培训代码:

def train(batch_size, steps):
    config = get_config() 
    config.mode = 'train' # just for the example
    # Generate training data
    if config.mode == learn.ModeKeys.TRAIN:
        x, y = generate(batch_size * steps)
    print(y) # prints correct labels in range 0-89
    logging_hook = tf.train.LoggingTensorHook(tensors={"Probabilities": "softmax_tensor"}, every_n_iter=50)
    char_classifier = learn.Estimator(model_fn=model, model_dir=mydir)

    # Train!
    if config.mode == learn.ModeKeys.TRAIN: #assume this is always true in the example here
        char_classifier.fit(x=x, y=y, batch_size=batch_size, steps=steps, monitors=[logging_hook])
    accuracy = learn.MetricSpec(metric_fn=tf.metrics.accuracy, prediction_key="classes")

    # Evaluate!
    x, y = generate(1000)
    eval_results = char_classifier.evaluate(x=x, y=y, metrics={'accuracy':accuracy})

此外,示例图片:fletter yletter eletter

0 个答案:

没有答案