我试图从工具锤上取下螺丝 . 已经从谷歌抓取了500/500张图片(数据不够?) . 所以它的图像分类很简单 . 但是,我做得更差,准确率为59% . 有谁知道为什么会这样?

Write images to dataframes

首先,图像被转换为灰度cv2.imread(img,0) . 然后将爬行的图像写入数据帧 . 标签是根据文件名创建的 . 图像大小调整为300 x 300像素 .

import cv2
import os
import random
import matplotlib.pylab as plt
import matplotlib.pyplot as plot
from glob import glob
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

PATH = os.path.abspath(os.path.join('crawler'))
SOURCE_IMAGES = os.path.join(PATH, "data", "all")
images = glob(os.path.join(SOURCE_IMAGES, "*.jpg"))

def proc_images():
    x = [] # images as arrays
    y = [] # labels
    WIDTH = 300
    HEIGHT = 300
    count = 0
    for img in images:
        count = count + 1
    for j in range(count -1):
        column = []
        for i in range(2):
                column.append(0)
        y.append(column)
    print(np.array(y).shape)
    row_number = 0
    for img in images:
        full_size_image = None
        base = os.path.basename(img)
        # Read and resize image
        full_size_image = cv2.imread(img, 0)

        if full_size_image is not None:   
            good_image = cv2.resize(full_size_image, (WIDTH,HEIGHT), interpolation=cv2.INTER_LINEAR)
            x.append(good_image)     
            # if keyword in filename creat label
            if "tool-hammer" in base:
                y[row_number][0] = 1
                y[row_number][1] = 0
                row_number = row_number + 1
            elif "screw" in base:
                y[row_number][0] = 0
                y[row_number][1] = 1
                row_number = row_number + 1
    return x, y

x,y = proc_images()
print(np.array(x).shape)
print(np.array(y).shape)
# Set it up as a dataframe 
df = pd.DataFrame()
df["labels"]=y
df["images"]=x
np.savez("x_images_arrays", x)
np.savez("y", y)

Neural Network (feed forward)

我很确定一个cnn会预先形成一个床,但我不应该得到一个带有前馈架构(3个隐藏层)的床位预测吗?我还没有改变stddev .

# Imports
    import tensorflow as tf
    import numpy as np
    import os
    import pandas as pd
    import matplotlib.pylab as plt
    from sklearn.model_selection import train_test_split

    # Load npz file containing image arrays
    x_npz = np.load("x_images_arrays.npz")

    x = x_npz['arr_0']
    print(np.array(x).shape)
    y_npz = np.load("y_labels.npz")
    y = y_npz['arr_0']
    x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.2, random_state=42)
    x_train = x_train[:, :]
    x_test = x_test[:, :]

    # Save Path
    dir_path = "C:/Users/"

    train_size = len(y_train)
    test_size = len(y_test)

    # Variables
    epochs = 10
    classes = 2
    features = 90000
    layer_nodes = [features, 100, 100, 100, classes] 
    stddev = 0.050
    learning_rate = 5e-6
    batch_size = 32
    bias_wights_init = 0.050
    epoch_errors = []

    # TF Placeholders
    x = tf.placeholder(dtype=tf.float32, shape=[None, features], name='x') 
    y = tf.placeholder(dtype=tf.float32, shape=[None, classes], name='y') 

    # Weights Matrices
    W1 = tf.Variable(tf.truncated_normal([layer_nodes[0], 
                                            layer_nodes[1]], 
                                            stddev=stddev), name='W1')
    W2 = tf.Variable(tf.truncated_normal([layer_nodes[1], 
                                            layer_nodes[2]], 
                                            stddev=stddev), name='W2')
    W3 = tf.Variable(tf.truncated_normal([layer_nodes[2], 
                                            layer_nodes[3]], 
                                            stddev=stddev), name='W3')
    W4 = tf.Variable(tf.truncated_normal([layer_nodes[3], 
                                            layer_nodes[4]], 
                                            stddev=stddev), name='W4')
    # Biases Vectors
    b1 = tf.Variable(tf.constant(bias_wights_init, shape=[layer_nodes[1]]), name='b1')
    b2 = tf.Variable(tf.constant(bias_wights_init, shape=[layer_nodes[2]]), name='b2')
    b3 = tf.Variable(tf.constant(bias_wights_init, shape=[layer_nodes[3]]), name='b3')
    b4 = tf.Variable(tf.constant(bias_wights_init, shape=[layer_nodes[4]]), name='b4')

    # Define the Deep Neural Network
    def nn_model(x):
        input_layer     =    {'weights': W1, 'biases': b1}
        hidden_layer_1  =    {'weights': W2, 'biases': b2}
        hidden_layer_2  =    {'weights': W3, 'biases': b3}
        output_layer    =    {'weights': W4, 'biases': b4}

        input_layer_sum = tf.add(tf.matmul(x, input_layer['weights']), 
                                input_layer['biases'])
        input_layer_sum = tf.nn.relu(input_layer_sum)

        hidden_layer_1_sum = tf.add(tf.matmul(input_layer_sum, hidden_layer_1['weights']), 
                                hidden_layer_1['biases'])
        hidden_layer_1_sum = tf.nn.relu(hidden_layer_1_sum)

        hidden_layer_2_sum = tf.add(tf.matmul(hidden_layer_1_sum, hidden_layer_2['weights']), 
                                hidden_layer_2['biases'])
        hidden_layer_2_sum = tf.nn.relu(hidden_layer_2_sum)

        output_layer_sum = tf.add(tf.matmul(hidden_layer_2_sum, output_layer['weights']), 
                                output_layer['biases'])
        return output_layer_sum

training and testing

我尝试了不同的优化器和变量配置 . 我不得不重塑np数组 . 可能不是最好的地方......我不能经常达到59%以上的准确度 . 培训或测试功能是否有任何错误?

# Train the Neural Network
def nn_train(x):
    pred = nn_model(x)
    pred = tf.identity(pred)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
    optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate).minimize(cost)

    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())

        # TRAINING
        for epoch in range(epochs):
            epoch_loss = 0.0
            batch_range_old = 0 

            for i in range(int(train_size / batch_size) + 1):
                epoch_x, epoch_y, batch_range_old = load_next_batch(x_train, y_train, batch_range_old, batch_size)
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
                epoch_loss += c

            epoch_errors.append(epoch_loss)
            print('epoch: ', epoch + 1, ' of ', epochs, ' with loss: ', epoch_loss)

        # TESTING
        correct_result = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
        accurancy = tf.reduce_mean(tf.cast(correct_result, 'float'))
        acc_test = 0.0
        for i in range(batch_size, test_size, batch_size):

            acc_test = acc_test + accurancy.eval(
                {x: np.reshape(x_test[i-batch_size:i], (32, -1)),
                y: y_test[i-batch_size:i]})

        acc_test = acc_test / len(range(batch_size, test_size, batch_size))
        print('Accucary ', acc_test)

def load_next_batch(x, y, batch_range_x, batch_size):
    batch_range_new = batch_range_x + batch_size
    xa =  x[batch_range_x:batch_range_new]
    ya =  y[batch_range_x:batch_range_new]
    xa = np.reshape(xa, (32, -1))

    return xa, ya, batch_range_x
if __name__ == "__main__":
    nn_train(x)