#python #tensorflow #keras #generative-adversarial-network
#python #тензорный поток #keras #генеративный-состязательный-сетевой
Вопрос:
Как я могу загрузить внешний набор данных вместо mnist?
# underscore to omit the label arrays
(train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5 # Normalize the images to [-1, 1]
BUFFER_SIZE = 60000
BATCH_SIZE = 256
# Batch and shuffle the data
train_dataset = tf.data.Dataset.from_tensor_slices(train_images).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
Я пробовал flow_from_directory «(train_images, _) = train_generator», но не могу извлечь значения изображения в виде приведенного выше кода
train_image_generator = ImageDataGenerator(
rescale=1./255)
#rotation_range=10,
#zoom_range = 0.10,
#width_shift_range=0.1,
#height_shift_range=0.1
train_generator = train_image_generator.flow_from_directory(
batch_size=256,
color_mode="grayscale",
directory='../input/main-dataset110/Train',
shuffle=True,
target_size=(28, 28),
class_mode='sparse')
(train_images, _) = train_generator
Комментарии:
1. » но не удается извлечь значения изображения » — что вы имеете в виду?
Ответ №1:
Я нашел код, который помог
try:
import tensorflow as tf
import cv2
import os
import pickle
import numpy as np
print("Library Loaded Successfully ..........")
except:
print("Library not Found ! ")
class MasterImage(object):
def __init__(self,PATH='', IMAGE_SIZE = 28):
self.PATH = PATH
self.IMAGE_SIZE = IMAGE_SIZE
self.image_data = []
self.x_data = []
self.y_data = []
self.CATEGORIES = []
# This will get List of categories
self.list_categories = []
def get_categories(self):
for path in os.listdir(self.PATH):
if '.DS_Store' in path:
pass
else:
self.list_categories.append(path)
print("Found Categories ",self.list_categories,'n')
return self.list_categories
def Process_Image(self):
try:
"""
Return Numpy array of image
:return: X_Data, Y_Data
"""
self.CATEGORIES = self.get_categories()
for categories in self.CATEGORIES: # Iterate over categories
train_folder_path = os.path.join(self.PATH, categories) # Folder Path
class_index = self.CATEGORIES.index(categories) # this will get index for classification
for img in os.listdir(train_folder_path): # This will iterate in the Folder
new_path = os.path.join(train_folder_path, img) # image Path
try: # if any image is corrupted
image_data_temp = cv2.imread(new_path,cv2.IMREAD_GRAYSCALE) # Read Image as numbers
image_temp_resize = cv2.resize(image_data_temp,(self.IMAGE_SIZE,self.IMAGE_SIZE))
self.image_data.append([image_temp_resize,class_index])
except:
pass
data = np.asanyarray(self.image_data)
# Iterate over the Data
for x in data:
self.x_data.append(x[0]) # Get the X_Data
self.y_data.append(x[1]) # get the label
X_Data = np.asarray(self.x_data) / (255.0) # Normalize Data
Y_Data = np.asarray(self.y_data)
# reshape x_Data
X_Data = X_Data.reshape(-1, self.IMAGE_SIZE, self.IMAGE_SIZE, 1)
return X_Data, Y_Data
except:
print("Failed to run Function Process Image ")
def pickle_image(self):
"""
:return: None Creates a Pickle Object of DataSet
"""
# Call the Function and Get the Data
X_Data,Y_Data = self.Process_Image()
# Write the Entire Data into a Pickle File
pickle_out = open('X_Data','wb')
pickle.dump(X_Data, pickle_out)
pickle_out.close()
# Write the Y Label Data
pickle_out = open('Y_Data', 'wb')
pickle.dump(Y_Data, pickle_out)
pickle_out.close()
print("Pickled Image Successfully ")
return X_Data,Y_Data
def load_dataset(self):
try:
# Read the Data from Pickle Object
X_Temp = open('X_Data','rb')
X_Data = pickle.load(X_Temp)
Y_Temp = open('Y_Data','rb')
Y_Data = pickle.load(Y_Temp)
print('Reading Dataset from PIckle Object')
return X_Data,Y_Data
except:
print('Could not Found Pickle File ')
print('Loading File and Dataset ..........')
X_Data,Y_Data = self.pickle_image()
return X_Data,Y_Data
if __name__ == "__main__":
path = '../input/main-dataset110/Test'
a = MasterImage(PATH=path,
IMAGE_SIZE=28)
X_Data,Y_Data = a.load_dataset()
print(X_Data.shape)