Image Directory Failing to Open Unless label_mode=None

My image directory fails to open unless the label_mode argument of image_dataset_from_directory is None.

I’ve tried switching the type to ‘int’, ‘categorical’ and ‘binary’ to no avail. I’ve also tried making a wrapper for the class and then switching the attribute later (which in hindsight, was never going to work), but that didn’t work either. I need to somehow keep it off None because I later evaluate the accuracy of the generator and model.evaluate() can’t handle the None value. Here is my code thus far:

import matplotlib.pyplot as plt
import numpy as np
import random
from PIL import Image
import tensorflow
from tensorflow import keras
from keras import layers, preprocessing, Sequential
from sklearn.neighbors import KernelDensity
import glob

class CustomImageDataset:
    def __init__(self, directory, image_size, batch_size, label_mode):
        self.dataset = tensorflow.keras.preprocessing.image_dataset_from_directory(
            directory,
            image_size=image_size,
            batch_size=batch_size,
            label_mode=label_mode
        )
        self.label_mode = label_mode

    def __iter__(self):
        return iter(self.dataset)

    def __len__(self):
        return len(self.dataset)

    def map(self, *args, **kwargs):
        return self.dataset.map(*args, **kwargs)

    def batch(self, *args, **kwargs):
        return self.dataset.batch(*args, **kwargs)

    def prefetch(self, *args, **kwargs):
        return self.dataset.prefetch(*args, **kwargs)


SIZE = 8
batch_size = 64

train_generator = preprocessing.image_dataset_from_directory(
    r'C:Users{}Downloadsarchive (1)noncloud_train',    
    image_size=(SIZE, SIZE),
    batch_size=batch_size,
    label_mode=None
)

validation_generator = preprocessing.image_dataset_from_directory(
    r'C:Users{}Downloadsarchive (1)noncloud_test',
    image_size=(SIZE, SIZE),
    batch_size=batch_size,
    label_mode=None
)

anomaly_generator = CustomImageDataset(
    r'C:Users{}Downloadsarchive (1)cloud',
    image_size=(SIZE, SIZE),
    batch_size=batch_size,
    label_mode=None
)



rescaling_layer = layers.Rescaling(1./255)

def change_inputs(images):
  x = tensorflow.image.resize(rescaling_layer(images),[SIZE, SIZE], method=tensorflow.image.ResizeMethod.NEAREST_NEIGHBOR)
  return x, x


# Apply preprocessing to datasets
train_dataset = train_generator.map(change_inputs)
validation_dataset = validation_generator.map(change_inputs)
anomaly_dataset = anomaly_generator.map(change_inputs)

test = anomaly_generator.label_mode
def check_none_in_dataset(dataset):
    for batch in dataset:
        images, labels = batch
        if images is None or labels is None:
            print("Found None in dataset")
            return True
    print("No None values in dataset")
    return False

# Check validation dataset
print("Checking validation dataset for None values:")
c = check_none_in_dataset(validation_dataset)
print(c)

def print_labels_from_dataset(dataset, num_batches=1):
    for images, labels in dataset.take(num_batches):
        print("Labels (should be the same as images):")
        print(labels.numpy())  # Print the labels to check if they are the expected values (not None)s
        print(labels.numpy() == images.numpy())

print("Validation Dataset Labels:")
bat = print_labels_from_dataset(validation_dataset)

print("Anomaly Dataset Labels:")
cow = print_labels_from_dataset(anomaly_dataset)


model = Sequential()
# Encoder
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(SIZE, SIZE, 3)))
model.add(layers.MaxPooling2D((2, 2), padding='same')) 
model.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), padding='same'))
model.add(layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), padding='same'))


# Deconder
model.add(layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
model.add(layers.UpSampling2D((2, 2)))
model.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(layers.UpSampling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.UpSampling2D((2, 2)))


model.add(layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same'))


model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mse'])
model.summary()

history = model.fit(
    train_dataset,
    steps_per_epoch = 1500 // batch_size,
    epochs = 1000,
    validation_data = validation_dataset,
    validation_steps = 225 // batch_size,
    shuffle = True
)

# Examine the recon. error between val data and anomaly images
validation_error = model.evaluate(validation_generator)
anomaly_error = model.evaluate(anomaly_generator)

New contributor

John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật