【问题标题】:Using CNN to get value from dartboard where dart landed使用 CNN 从飞镖降落的飞镖板中获取价值
【发布时间】:2020-07-05 15:19:01
【问题描述】:

我想在 python 中使用 CNN 使用图片从 dartboard 中获取值(或 dart 降落的字段的值)。

我拍了 208 张飞镖的照片,每个飞镖都在特定的位置。我想预测下一张图片中的飞镖是否在特定字段中(208 张图片代表 4 个类别/每个 52 个)(来自同一字段的单张、双张和三张表示相同的数字,或者在我们的例子中,代表相同的类别。

sample dart in a field

然后我用类似的图片来测试模型。

当我尝试拟合模型时,我会得到类似的结果

208/208 [==============================] - 3s 15ms/sample - loss: 0.0010 - accuracy: 1.0000 - val_loss: 8.1726 - val_accuracy: 0.2500
Epoch 29/100
208/208 [==============================] - 3s 15ms/sample - loss: 9.8222e-04 - accuracy: 1.0000 - val_loss: 8.6713 - val_accuracy: 0.2500
Epoch 30/100
208/208 [==============================] - 3s 15ms/sample - loss: 8.5902e-04 - accuracy: 1.0000 - val_loss: 9.2214 - val_accuracy: 0.2500
Epoch 31/100
208/208 [==============================] - 3s 15ms/sample - loss: 7.9463e-04 - accuracy: 1.0000 - val_loss: 9.6584 - val_accuracy: 0.2500

随着准确度达到 1,val_accuracy 保持不变,以前的一些模型让我得到了更好的结果,但比这好一点。

由于我是该领域的新手,我需要一些建议来让我的模型或整个程序变得更好。

这是我现在的模型_

model = Sequential()
model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(640, 480, 3)))

model.add(MaxPooling2D(2, 2))

model.add(BatchNormalization())

model.add(Conv2D(64, kernel_size=3, activation='relu'))

model.add(MaxPooling2D(2, 2))

model.add(Conv2D(128, kernel_size=3, activation='relu'))

model.add(MaxPooling2D(2, 2))

model.add(Conv2D(256, kernel_size=3, activation='relu'))

model.add(MaxPooling2D(2, 2))

model.add(Flatten())

model.add(Dense(512, activation='relu', kernel_initializer='he_uniform'))

model.add(Dense(4, activation='softmax'))

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

history = model.fit(X, y, batch_size=16, epochs=100, validation_data=(Xtest,ytest))

还有我的示例程序

training_data = []
DATADIR = 'C:/PikadaNew'

dir = sorted(os.listdir(DATADIR), key=len)
def create_training_data():
    for category in dir:  # do dogs and cats
        path = os.path.join(DATADIR,category)
        class_num = dir.index(category) 
        for img in tqdm(os.listdir(path)): 
            try:
                img_array = cv2.imread(os.path.join(path,img))     
                training_data.append([img_array, class_num])      
            except Exception as e: 
                pass          

create_training_data()

DATATESTDIR = 'C:/PikadaNewTest'
dir1 = sorted(os.listdir(DATATESTDIR), key=len)
test_data = []

def create_test_data():
    for category in dir1:
        path = os.path.join(DATATESTDIR,category) 
        class_num = dir1.index(category) 
        for img in tqdm(os.listdir(path)):
            try:
                img_array = cv2.imread(os.path.join(path,img))  # convert to array
                test_data.append([img_array, class_num])                
            except Exception as e:
                pass

create_test_data()

#print(len(training_data))
#print(len(test_data))

X = []
y = []

Xtest = []
ytest = []

for features,label in training_data:
    X.append(features)
    y.append(label)

for features,label in test_data:
    Xtest.append(features)
    ytest.append(label)


X = np.array(X).reshape(-1, 640, 480, 3)
Xtest= np.array(Xtest).reshape(-1, 640, 480, 3)
y = np.array(y)
ytest = np.array(ytest)


y = to_categorical(y)
ytest = to_categorical(ytest)


X = X/255.0
Xtest = Xtest/255.0


X,y = shuffle(X,y)
Xtest,ytest = shuffle(Xtest,ytest)

感谢并为错误感到抱歉,我希望它可以理解我想要实现的目标 每一个建议都非常感谢 萨摩

【问题讨论】:

    标签: python tensorflow convolution conv-neural-network


    【解决方案1】:

    您正面临过拟合问题,因为您的数据太小而且模型比需要的复杂。您可以尝试以下方法:

    • 如果可以,请添加更多数据。
    • 尝试通过删除一些层来简化模型。
    • 向模型添加 dropout 并使用正则化。
    • 使用更少的 epoch。

    【讨论】:

    • 我无法添加更多数据,这是每个字段的最大图片数量,我想显示随着导入模型的图片减少,准确性如何下降。简化模型没有给出任何好的结果。您知道在哪里放置 dropouts 以及在哪里正确使用正则化吗?使用较少数量的时期并不能向我们展示模型在后期的表现如何?如果结果更好,我会使用较小的数字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    相关资源
    最近更新 更多