I create a malaira detection project I get the dataset from kaggle malaria detection then i moun the datatset from google drive after that i faces problems

please solve the problem
use pretrain model
from google.colab import drive


import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

import tensorflow_datasets as tfds

from tensorflow.keras.layers import Conv2D , MaxPool2D,Dense,Flatten,InputLayer,BatchNormalization

from tensorflow.keras.losses import BinaryCrossentropy

from tensorflow.keras.optimizers import Adam

dataset = ‘/content/gdrive/MyDrive/Dataset malaria/Train’
dataset_info, with_info, as_supervised, shuffle_files, split = True, True, True, True, [‘train’]
i facsed problem here in this code

for data in dataset[0].take(1):
** print(data) //error in this line**

AttributeError Traceback (most recent call last)
in <cell line: 1>()
----> 1 for data in dataset[0].take(1):
2 print(data)

AttributeError: ‘str’ object has no attribute ‘take’

def splits(dataset, TRAIN_RATIO, VAL_RATIO, TEST_RATIO):
DATASET_SIZE = len(dataset)

train_dataset = dataset.take(int(TRAIN_RATIO*DATASET_SIZE))

val_test_dataset = dataset.skip(int(TRAIN_RATIODATASET_SIZE))
val_dataset = val_test_dataset.take(int(VAL_RATIO

test_dataset = val_test_dataset.skip(int(VAL_RATIO*DATASET_SIZE))
return train_dataset, val_dataset, test_dataset //run fine




train_dataset, val_dataset, test_dataset = splits(dataset[0], TRAIN_RATIO, VAL_RATIO, TEST_RATIO ) // error here

TypeError Traceback (most recent call last)
in <cell line: 5>()
3 TEST_RATIO = 0.1
----> 5 train_dataset, val_dataset, test_dataset = splits(dataset[0], TRAIN_RATIO, VAL_RATIO, TEST_RATIO )
6 # print(list(train_dataset.take(1).as_numpy_iterator()),
7 # list(val_dataset.take(1).as_numpy_iterator()), list(test_dataset.take(1).as_numpy_iterator()))

TypeError: ‘_PrefetchDataset’ object is not subscriptable

Hi @Palak_Talreja, If you see in the above code the value assigned to the dataset is a path to the dataset which is a string object. dataset = ‘/content/gdrive/MyDrive/Dataset malaria/Train’. while using the for loop on the dataset variable with take method dataset[0].take(1) causes this AttributeError: ‘str’ object has no attribute ‘take’.

If the dataset you are using contains images you can load the data set using

                                                          image_size=(192, 192),label_mode='binary',

to see the data in the train dataset you can use

for data in Train_dataset.take(1):

if you use

for data in Train_dataset[0].take(1):

you will get TypeError: '_PrefetchDataset' object is not subscriptable. Please refer to this gist for code example. Thank You.

1 Like