在随机情况下,它将随机狗图像中的“狗”图像显示为“猫”。即使我从火车数据中放狗图像,它也会显示猫在狗的位置。
我使用了 1000 张猫和狗的图像,以及 5% 的数据用于火车的测试休息。
df = pd.DataFrame(data=class_lebels, columns=['labels', 'image'])
print(df.head())
print(df.tail())
标签图像
0 猫 dataset_path/cat/pixabay_cat_002636.jpg
1 只猫 dataset_path/cat/pixabay_cat_002802.jpg
2 猫 dataset_path/cat/flickr_cat_000254.jpg
3 猫 dataset_path/cat/pixabay_cat_002669.jpg
4 猫 dataset_path/cat/pixabay_cat_002815.jpg
标签图像
995 狗 dataset_path/dog/flickr_dog_000542.jpg
996 狗 dataset_path/dog/flickr_dog_000030.jpg
997 狗 dataset_path/dog/flickr_dog_000508.jpg
998 狗 dataset_path/dog/flickr_dog_000358.jpg
999 狗 dataset_path/dog/flickr_dog_000134.jpg
import cv2
path = 'dataset/'
dataset_path = os.listdir('dataset')
im_size = 380
images = \[\]
labels = \[\]
for i in dataset_path:
data_path = path + str(i)
filenames = \[i for i in os.listdir(data_path)\]
for f in filenames:
img = cv2.imread(data_path + '/' + f)
img = cv2.resize(img, (im_size, im_size))
images.append(img)
labels.append(i)
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
y = df['labels'].values
print(y)
y_labelencoder = LabelEncoder()
y = y_labelencoder.fit_transform(y)
print(y)
['猫''猫''猫''猫''猫''猫''猫''猫''猫''猫''猫''猫''狗''狗''狗''狗' '狗''狗''狗''狗''狗''狗''狗''狗'] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
from tensorflow import keras
from keras import layers
from keras.applications import EfficientNetB4
num_classes = 2
img_size = 380
size = (img_size, img_size)
inputs = layers.Input(shape=(img_size, img_size, 3))
使用没有迁移学习的模型 outputs = EfficientNetB4(include_top=True, weights=None, classes=num_classes)(inputs)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
hist = model.fit(train_x, train_y, epochs=15, verbose=2, batch_siz`your text`e=6)
第 15 个 epoch 的准确率为 89%。
纪元 15/15
159/159 - 59s - 损失:0.3084 - 精度:0.8947 - 59s/epoch - 369ms/step
preds = model.evaluate(test_x, test_y)
print("Loss = ", str(preds[0]))
print("Accuracy = ", str(preds[1]))
测试准确率为 89%
损失 = 0.22748856246471405
精度 = 0.8999999761581421
from tensorflow import keras
from keras.applications import imagenet_utils
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
from keras.applications.imagenet_utils import decode_predictions
from matplotlib.pyplot import imread
from matplotlib.pyplot import imshow
img_path = "/content/dataset/dog/flickr_dog_000014.jpg"
img = cv2.imread(img_path)
img = cv2.resize(img, (380, 380))
x = np.expand_dims(img, axis=0)
x = preprocess_input(x)
print("Input image: ", x.shape)
my_image = imread(img_path)
imshow(my_image)
preds = model.predict(x)
print("cat, dog")
preds
1/1 [==============================] - 0s 53ms/step
猫,狗
array([[1., 0.]], dtype=float32)
你们能帮帮我吗,问题出在哪里?