Python 如何在.csv文件的数据集上训练CNN模型?
大家好,我是Python初学者,我正在使用google Colab来训练我的第一个CNN模型,我在训练部分遇到了障碍,我知道我必须使用model.fit()来训练模型,但我不知道model.fit()中包含了什么,而且我不确定从.csv文件分割数据集的部分。这是我的密码:Python 如何在.csv文件的数据集上训练CNN模型?,python,csv,tensorflow,conv-neural-network,training-data,Python,Csv,Tensorflow,Conv Neural Network,Training Data,大家好,我是Python初学者,我正在使用google Colab来训练我的第一个CNN模型,我在训练部分遇到了障碍,我知道我必须使用model.fit()来训练模型,但我不知道model.fit()中包含了什么,而且我不确定从.csv文件分割数据集的部分。这是我的密码: from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Con
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing import image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from sklearn.metrics import accuracy_score
from tqdm import tqdm
from numpy.random import RandomState
import csv
df = pd.read_csv('classes.csv')
print(df.head(3500))
#splitting the dataset in training and testing sets.
rng = RandomState()
train = df.sample(frac=0.7, random_state=rng)
test = df.loc[~df.index.isin(train.index)]
#model's structure
model = Sequential()
#convolutional layer
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=(28,28,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
#flatten output of conv
model.add(Flatten())
#hidden layer
model.add(Dense(128, activation='relu'))
#output layer
model.add(Dense(10, activation='softmax'))
#compiling sequential model
model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
model.summary()
#training the model
model.fit()
好的,如果我们能帮忙,我们就开始吧。第一件事是关于分割数据帧。最好将其分为一个序列集、一个验证集和一个测试集。最好的方法是使用sklearn的train、test和split。相关文档位于[此处][1]使用下面的代码将数据传输拆分为3组
from sklearn.model_selection import train_test_split
train_split=.8 # percentage of df to use for training
test_split = .05 # percentage of df to use for test
# note valid_split=1- train_split = test_ split in this case .15
train_df, dummy_df= train_test_split(df, train_split=train_split, shuffle=True, random_state=123)
dummy_split=test_split/(1.0 - train_split)
test_df, valid_df=train_test_split(dummy_df, train_split=dummy_split) # note dummy_df is 20% of df
现在您需要创建训练、测试和验证生成器。我假设你有图像输入。使用来自_dataframe()的ImageDataGenerator.flow_创建生成器。这方面的文档是[此处][2]通常在0到1或-1到+1范围内缩放像素图像。我喜欢后者。下面是发电机的代码
def scalar(img):
return img/127.5 -1
x_col='Filename' # set this to point to the df column that holds the full path to the image file
y_col- 'Label' # set this to the df column that holds the class labels
batch_size = 32 # set this to the batch size
class_mode='categorical'
image_shape=(64,64) # set this to desired image size
tgen=tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=scalar, horizontal_flip=True)
gen= tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=scalar)
train_gen=tgen.flow_from_dataframe(train_df, x_col=x_col,
y_col=y_col,target_size=image_shape,
class_mode=class_mode, batch_size=batch_size,
shuffle=True, random_state=123)
test_gen=tgen.flow_from_dataframe(test_df, x_col= x_col, y_col=y_col
class_mode=class_mode,
batch_size=batch_size, shuffle=False)
valid_gen=tgen.flow_from_dataframe(valid_df, x_col=x_col, shuffle=False,
y_col=y_col,batch_size=batch_size
class_mode=class_mode , target_size=image_shape)
现在是使用model.fit训练您的模型的时候了。文档位于[此处][3]
epochs = 15 # set this to the number of epochs to run
history=model.fit(train_gen, epochs=epochs, validation_data= valid_gen, verbose=1)
当模型完成训练后,您希望看到它在测试集上的表现如何。您可以按如下所示进行模型评估
accuracy = model.evaluate(test_gen, verbose=1)[1]
print (accuracy)
您可以使用模型使用model.predict进行预测
preds=model.predict(test_gen, verbose=1)
preds是一个矩阵,其行数等于测试集中的样本数,列数等于您拥有的类数。每个条目都是图像在该类中的概率值。您希望选择概率值最高的列。使用np.argmax查找概率值最高的列(类)。您可以使用labels-test\u gen.labels获取测试集标签。代码如下
correct_preds=0
for i in range (len(preds):
index=np.argmax(preds[i]) # column with highest probability
label=test_gen.labels[i] # true class of image
if index == label:
correct_preds +=1
pred_accuracy= correct_preds/ len(preds)
print ( pred_accuracy)
[1]: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
[2]: https://keras.io/api/preprocessing/image/
[3]: https://www.tensorflow.org/api_docs/python/tf/keras/Model
非常感谢您抽出时间,先生,非常感谢!!但是在创建序列、测试和验证生成器的第二部分中,我不知道应该在x_col和y_col的值中插入什么,我试图在y列中插入包含图像和.csv文件名的文件夹的路径,但它不起作用。如果不在“从数据流”框中使用“目录”参数,则x列应该是文件的完整路径。否则,如果指定目录的thr值,则为文件夹的名称。y_列应该是类标签的列。请注意,这些必须是字符串,即使它们在csv文件中是整数。100%我有点困惑,我在包含图像的文件夹的x_col中粘贴了路径,但它不起作用,下面是错误:KeyError:'C:/Users/Taoufik Souidi/Desktop/img'上述异常是以下异常的直接原因:No x_col是数据帧中列的名称。列中具有该名称的条目应该是每个文件的完整路径