如何在google colab中使用从kaggle加载的数据(实际使用它)

问题描述 投票:0回答:1

所以我最近从这个 https://www.kaggle.com/datasets/mostafaabla/garbage-classification 网站导入了数据集。即使我把它放在谷歌colab的文件中(解压和所有这些东西),我也不知道如何在代码本身中实现它。就像来自tensorflow的Fashion mnist教程https://www.tensorflow.org/tutorials/keras/classification?hl它加载为

fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images,train_labels),(test_images,test_labels)= Fashion_mnist.load_data()

如何将数据导入/加载到代码单元并通过拆分为类来使用它(因为在该教程数据集中有多个类,而在我的自定义数据集中有 12 个类) 请问怎么做?

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Define paths to your training and validation directories
train_dir = 'garbage-classification/train'
val_dir = 'garbage-classification/validation'

# Create an ImageDataGenerator for data augmentation
train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)

# Load images from directories
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(150, 150),  # Resize images as needed
    batch_size=32,
    class_mode='categorical'  # Use 'categorical' if you have multiple classes
)

validation_generator = val_datagen.flow_from_directory(
    val_dir,
    target_size=(150, 150),
    batch_size=32,
    class_mode='categorical'
)

我用困惑来尝试解决它,它给了我这个。显然这不起作用..

python tensorflow machine-learning keras google-colaboratory
1个回答
0
投票

在 Google Colab 中下载 Kaggle 数据集的步骤,包括安装 Kaggle API,上传 Kaggle API 密钥,设置身份验证, 下载数据集、解压缩数据(如果需要)并访问 数据。我已经下载了garbage_classification数据集并将其分成 训练和验证都在以下gist中执行。

© www.soinside.com 2019 - 2024. All rights reserved.