首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用jpg导入到jupyter笔记本的数据集

使用jpg导入到jupyter笔记本的数据集
EN

Stack Overflow用户
提问于 2021-01-10 01:01:22
回答 3查看 303关注 0票数 0

我必须使用tensorflow和keras,通过jupyter笔记本,用python构建一个机器学习模型。我有一个包含1000张图片的数据集。其中800个我想用来训练模型,200个用来测试和验证。这是一个性别和年龄预测模型。现在,我如何导入我的数据集,或者我如何在upyter笔记本或google colab中写入路径来导入我的数据集。

我所做的是为我的项目导入包。

代码语言:javascript
复制
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.utils import to_categorical, plot_model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization, Conv2D, MaxPooling2D, Activation, Flatten, Dropout, Dense
from tensorflow.keras import backend as K
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
import random
import cv2
import os
import glob
import pandas as pd

致以亲切的问候。

EN

回答 3

Stack Overflow用户

发布于 2021-01-10 01:09:33

如果数据集在您的本地系统中,有两种方法可以在google colab中上传数据集。

  1. 您可以将数据集上传到您的谷歌硬盘。

共享文件的最简单方法是将Google Drive挂载到Google Colab笔记本中。

为此,请在代码单元格中运行以下命令:

代码语言:javascript
复制
from google.colab import drive
drive.mount('/content/drive')

它将要求您访问一个链接,以允许“谷歌文件流”访问您的驱动器。之后,将显示一个长的字母数字验证码,需要在Colab的笔记本中输入。

之后,您的驱动器文件将被挂载,您可以使用侧面板中的文件浏览器来浏览它们。

  1. 您可以通过浏览至本地文件系统

手动上载文件

这种方式需要更长的上传时间。

代码语言:javascript
复制
from google.colab import files
uploaded = files.upload()

这里有两个示例供您参考:

票数 0
EN

Stack Overflow用户

发布于 2021-01-10 01:25:38

如果您使用pandas查找CSV文件,请尝试提供完整路径

代码语言:javascript
复制
df = pd.read_csv(r"C:\Users\mahe\Desktop\homeprices.csv")

使用这种方式,或者

代码语言:javascript
复制
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm

DATADIR = "X:/Datasets/PetImages" #(give your full path)
票数 0
EN

Stack Overflow用户

发布于 2021-01-10 01:27:42

在这里,我将在Tensorflow中以一种简单的方式解释如何直接从TXT文件加载图像和标签。希望这能对你有所帮助。以下代码说明了我是如何做到这一点的。然而,这并不意味着这是最好的方法,而且这种方法将在其他步骤中有所帮助。

例如,我加载单个整数值{ 0,1 }中的标签,而文档使用单个热向量0,1。

代码语言:javascript
复制
#Learning how to import images and labels from a TXT file
#
#TXT file format
#
#path/to/imagefile_1 label_1
#path/to/imagefile_2 label_2
#...                 ...
#where label_X is either {0,1}

#Importing Libraries
import os
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
#File containing the path to images and the labels [path/to/images label]

filename = '/path/to/List.txt'

#Lists where to store the paths and labels

filenames = []
labels = []

#Reading file and extracting paths and labels
with open(filename, 'r') as File:
    infoFile = File.readlines() #Reading all the lines from File
    for line in infoFile: #Reading line-by-line
        words = line.split() #Splitting lines in words using space character as separator
        filenames.append(words[0])
        labels.append(int(words[1]))

NumFiles = len(filenames)

#Converting filenames and labels into tensors
tfilenames = ops.convert_to_tensor(filenames, dtype=dtypes.string)
tlabels = ops.convert_to_tensor(labels, dtype=dtypes.int32)

#Creating a queue which contains the list of files to read and the value of the labels
filename_queue = tf.train.slice_input_producer([tfilenames, tlabels], num_epochs=10, shuffle=True, capacity=NumFiles)

#Reading the image files and decoding them
rawIm= tf.read_file(filename_queue[0])
decodedIm = tf.image.decode_png(rawIm) # png or jpg decoder

#Extracting the labels queue
label_queue = filename_queue[1]

#Initializing Global and Local Variables so we avoid warnings and errors
init_op = tf.group(tf.local_variables_initializer() ,tf.global_variables_initializer())

#Creating an InteractiveSession so we can run in iPython

sess = tf.InteractiveSession()

with sess.as_default():
    sess.run(init_op)
    
    # Start populating the filename queue.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(NumFiles): #length of your filenames list
        nm, image, lb = sess.run([filename_queue[0], decodedIm, label_queue])
        
        print image.shape
        print nm
        print lb
        
        #Showing the current image
        plt.imshow(image)
        plt.show()

    coord.request_stop()
    coord.join(threads)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65645275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档