我的目标是建立花朵的图像分类模型。数据RAR文件由一个名为Train data的文件夹组成,该文件夹包含大约16000张从0到16000标记的图像。同样,还有一个用于测试数据的文件夹。
除此之外,还有两个csv工作簿。第一个csv工作簿包含两个属性-label和flower类。有104个标签和花卉类别。第二本工作簿还包含两个属性-id和flower类。该csv是火车图片文件夹和鲜花类别之间的链接。 ID是链接属性。即例如假设火车图像文件夹中标记为10的图像是向日葵的图像。因此,在csv工作簿中,对应于id = 10的花类条目是向日葵。 例如,假设火车数据文件夹中标记为10的图像是向日葵。因此,在第二本工作簿中,对应于id = 10的花类条目是向日葵。
这是我的代码
# Import relavant libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten
from sklearn.model_selection import train_test_split
from PIL import Image
# Load the csv files
# Workbook no.1
label_csv = pd.read_csv('/content/flowers_label.csv')
# Workbook no.2
train = pd.read_csv('/content/flowers_idx.csv')
# To sort the train csv id wise from 0 - 16464
train.sort_values('id')
# Creating inputs and targets
X = [] #images
y = [] # labels
base = "/content/flower_tpu/flower_tpu/flowers_google/flowers_google//"
row = 0;
for idx in range(len(train)):
# get the flower row
flower = train.iloc[idx]
# create flower path
path = f"{base}{flower.id}.jpeg"
#load image
img = Image.open(path)
# convert to numpy
img = np.array(img)
#save to X
X.append(img)
# get label
label = label_csv[label_csv['flower_class'] == flower.flower_cls].label.values[0]
# save to y
y.append(label)
# Train Validation split
X_train, X_validation, y_train, y_validation = train_test_split(X, y, random_state=12, test_size=0.2)
# The model
output_size = 104
hidden_layer_size = 150
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(224, 224, 3)),
tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
tf.keras.layers.Dense(output_size, activation='softmax')
])
# Converting all data into ndarrays
X_train = np.asarray(X_train)
y_train = np.asarray(y_train)
X_validation = np.asarray(X_validation)
y_validation = np.asarray(y_validation)
# Compilation
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Fitting
model.fit(X_train, y_train, epochs=3, validation_data=(X_validation, y_validation), validation_steps=10, verbose =2)
我的代码正在运行,但是训练和验证的准确性仅为6%。 :/ 如何改善此代码?