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

编程入门 行业动态 更新时间:2024-10-26 00:26:38

数据集<a href=https://www.elefans.com/category/jswz/34/1771433.html style=加载(python, keras, pytorch)"/>

数据集加载(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 = 0for labels in os.listdir(directory):            # you should give the dir of the train dataif labels == '':                            # you can change the name of labels and the number of labelslabel = 2elif labels == '':label = 4elif labels == '':label = 0elif labels == '':label = 1elif labels == '':label = 5elif labels == '':label = 3for 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 imageimage = 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 detectionImages = []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('')

更多推荐

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

本文发布于:2024-02-11 14:16:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1681426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   数据   python   keras   pytorch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!