3 类 预测 CNN 深度学习
3 Classes prediction CNN Deep Learning
我用三个 类 训练了我的模型,现在我想一次输入一张图像,看看它属于 类 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'])
我需要帮助编写我的预测代码以一次输入一张测试图像。
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))
)
当输入quit
.
时,可以用while循环连续进入img目录break
我用三个 类 训练了我的模型,现在我想一次输入一张图像,看看它属于 类 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'])
我需要帮助编写我的预测代码以一次输入一张测试图像。
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))
)
当输入quit
.
break