数据集加载(python, keras, pytorch)

从0开始处理数据集

图片数据以及对应的csv数据(以IDRiD数据集为例)

train_data = []
train_labels = []

def get_images(image_dir, labels_dir):
    
    for image_file in os.listdir(image_dir):
        image = cv2.imread(image_dir+r'/'+image_file)
        image = cv2.resize(image,(227,227))
        train_data.append(image)
        labels = pd.read_csv(labels_dir)
        label = list(labels[labels['Image_name'] + '.jpg' ==  image_file]['Retinopathy grade'])
        train_labels.append(label)
    
    return shuffle(train_data,train_labels,random_state=7)
train_data, train_labels = get_images()

图片数据及对应的txt数据

将txt文档的后缀名改成csv,便于1.1相同

仅txt数据

一般而言,仅给出txt数据的情况较少,遇到之后可以进行如下处理:

直接将txt文档的后缀名改成csv即可,然后再按照csv的处理方式进行处理

仅图像数据(以intel classification比赛为例)

首先是如何读取、打标签和使用含标签的训练集

只需给出需要的路径即可
def get_images(directory):
    
    Images = []
    Labels = []  
    label = 0
    
    for labels in os.listdir(directory):            # you should give the dir of the train data
        if labels == '':                            # you can change the name of labels and the number of labels
            label = 2
        elif labels == '':
            label = 4
        elif labels == '':
            label = 0
        elif labels == '':
            label = 1
        elif labels == '':
            label = 5
        elif labels == '':
            label = 3
        
        for image_file in os.listdir(directory+r'/'+labels):     
            image = cv2.imread(directory+r'/'+labels+r'/'+image_file)   # read your image and change the size of your image
            image = cv2.resize(image,(150,150)) 
            Images.append(image)
            Labels.append(label)
    
    return shuffle(Images,Labels,random_state=817328462) 
Images, Labels = get_images('')   

Images = np.array(Images)                                  
Labels = np.array(Labels)
接下来是如何读取预测集
def Get_images(directory): # function for image detection
    Images = []
    
    path = os.path.join(directory)
    
    for img in os.listdir(path):
        img_array = cv2.imread(os.path.join(path, img))
        img_array = cv2.resize(img_array, (150, 150))
        Images.append(img_array)
        
    return shuffle(Images, random_state=817328462)
pred_images = Get_images('')

pred_images = np.array(pred_images)
pred_images.shape

仅csv数据

csv数据读取较为简单,直接使用以下指令即可,读取后可以当成dataframe格式进行后续的处理
pd.read_csv('')
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LSTM(长短期记忆网络)是一种递归神经网络,特别适合处理序列数据,如时间序列预测、文本生成等任务。在Python中,我们可以使用深度学习库KerasPyTorch来构建LSTM模型并加载数据集。 以下是一个简单的LSTM数据集加载和训练的基本流程: 1. **导入库**: ```python import numpy as np from keras.datasets import imdb from keras.models import Sequential from keras.layers import Dense, LSTM ``` 2. **加载IMDB电影评论情感分析数据集**(假设数据集已经预处理好): ```python top_words = 5000 (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words) ``` `imdb.load_data()`会返回电影评论的文本和对应的标签(正面或负面评价)。 3. **预处理数据**(例如将整数编码转换为向量): ```python X_train = np.array([np.zeros((maxlen, top_words)) for _ in range(len(X_train))]) for i, sentence in enumerate(X_train): for word in sentence: if word != 0: X_train[i, word] = 1 X_test = np.array([np.zeros((maxlen, top_words)) for _ in range(len(X_test))]) for i, sentence in enumerate(X_test): for word in sentence: if word != 0: X_test[i, word] = 1 ``` 4. **创建LSTM模型**: ```python model = Sequential() model.add(LSTM(128, input_shape=(maxlen, top_words))) model.add(Dense(1, activation='sigmoid')) # 输出层,用于二分类问题 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) ``` 5. **训练模型**: ```python model.fit(X_train, y_train, epochs=5, batch_size=32, validation_data=(X_test, y_test)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值