3个课程预测CNN深度学习

发布于 2025-01-29 10:14:10 字数 984 浏览 3 评论 0原文

我在三个类中训练了模型,现在我想一次输入一个图像,看看它属于1,2类还是3类。

data = []
img_size = 224

for i in categories:
    path = os.path.join(TRAIN_DIR1, i)   
    class_num = categories.index(i)
    for file in os.listdir(path):
        filepath = os.path.join(path, file)
        img = cv2.imread(filepath, 0)
        img = cv2.resize(img, (img_size, img_size))
        data.append([img, class_num])
random.shuffle(data)
X, y = [], []
for feature, label in data:
    X.append(feature)
    y.append(label)
X = np.array(X).reshape(-1, img_size, img_size, 1)
X = X / 255.0
y = np.array(y)

X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=20, stratify=y)

X_train = X_train.reshape(X_train.shape[0], img_size*img_size*1)

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

我需要帮助编写我的预测代码以一次输入一个测试图像。

I trained my model in three classes and now I want to input one image at a time to see whether it belongs to classes 1,2, or 3.

data = []
img_size = 224

for i in categories:
    path = os.path.join(TRAIN_DIR1, i)   
    class_num = categories.index(i)
    for file in os.listdir(path):
        filepath = os.path.join(path, file)
        img = cv2.imread(filepath, 0)
        img = cv2.resize(img, (img_size, img_size))
        data.append([img, class_num])
random.shuffle(data)
X, y = [], []
for feature, label in data:
    X.append(feature)
    y.append(label)
X = np.array(X).reshape(-1, img_size, img_size, 1)
X = X / 255.0
y = np.array(y)

X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=20, stratify=y)

X_train = X_train.reshape(X_train.shape[0], img_size*img_size*1)

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

I need help writing my prediction code to input one testing image at a time, please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2025-02-05 10:14:10
import cv2

img_directory = input(str("Input directory: ")) # 'C:/dataset/img.png'

img= cv2.imread(img_directory)

img=cv2.resize(img, (180,180))

img = tf.expand_dims(img, 0)

prediction = model.predict(img)

score = tf.nn.softmax(prediction[0])

print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
) 

当您输入退出时,您可以使用way循环连续输入IMG目录。

import cv2

img_directory = input(str("Input directory: ")) # 'C:/dataset/img.png'

img= cv2.imread(img_directory)

img=cv2.resize(img, (180,180))

img = tf.expand_dims(img, 0)

prediction = model.predict(img)

score = tf.nn.softmax(prediction[0])

print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
) 

You can use a while loop to continuously enter the img directory with break when you input quit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文