Seite 1 von 1

Keras to PyTorch

Verfasst: Mittwoch 9. September 2020, 11:12
von Felix92
Huhu,
und zwar versuche ich gerade meinen Autoencoder in PyTorch zu rekonstruieren allerdings habe ich etwas Probleme beim laden der Daten bzw. Verständnisprobleme vlt. kann mir ja jmd auf die Sprünge helfen :)

TF/Keras:

Code: Alles auswählen

from tensorflow.keras.preprocessing.image import load_img, img_to_array
from sklearn.model_selection import train_test_split

train_images_path = '../denoising/train'
train_cleaned_path = '../denoising/train_cleaned'

train_images = sorted(os.listdir(train_images_path))
train_cleaned = sorted(os.listdir(train_cleaned_path))

x_train = []
y_train = []

for image in train_images:
    img_path = os.path.join(train_images_path, image)
    img = load_img(img_path, color_mode = 'grayscale', target_size = (420, 540))
    img = img_to_array(img).astype('float32')/255
    x_train.append(img)
for image in train_cleaned:
    img_path = os.path.join(train_cleaned_path, image)
    img = load_img(img_path, color_mode = 'grayscale', target_size = (420, 540))
    img = img_to_array(img).astype('float32')/255
    y_train.append(img)
     
x = np.array(x_train)
y = np.array(y_train)

x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.25)
PyTorch:

Code: Alles auswählen

from tensorflow.keras.preprocessing.image import load_img, img_to_array
from sklearn.model_selection import train_test_split

train_images_path = '../denoising/train'
train_cleaned_path = '../denoising/train_cleaned'

train_images = sorted(os.listdir(train_images_path))
train_cleaned = sorted(os.listdir(train_cleaned_path))

x_train = []
y_train = []

for image in train_images:
    img_path = os.path.join(train_images_path, image)
    img = load_img(img_path, color_mode = 'grayscale', target_size = (420, 540))
    img = img_to_array(img).astype('float32')/255
    x_train.append(img)
for image in train_cleaned:
    img_path = os.path.join(train_cleaned_path, image)
    img = load_img(img_path, color_mode = 'grayscale', target_size = (420, 540))
    img = img_to_array(img).astype('float32')/255
    y_train.append(img)
     
x = np.array(x_train)
y = np.array(y_train)

x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.25)

transform = transforms.Compose([
     transforms.ToPILImage(),
     transforms.ToTensor()
])

class ImageData(Dataset):
    def __init__(self, images, labels=None, transforms=None):
        self.X = images
        self.y = labels
        self.transforms = transforms
         
    def __len__(self):
        return (len(self.X))
    
    def __getitem__(self, i):
        data = self.X[i][:]
        
        if self.transforms:
            data = self.transforms(data)
            
        if self.y is not None:
            labels = self.y[i][:]
            labels = self.transforms(labels)
            return (data, labels)
        else:
            return data
        
train_data = ImageData(x_train, y_train, transform)
test_data = ImageData(x_valid, None, transform)
train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
test_loader = DataLoader(test_data, batch_size=BATCH_SIZE)
1. Geht das nicht auch einfacher in "reinem" PyTorch ?
Ich hatte in der Docu gesehen das es eine ImageFolder Klasse gibt:
https://pytorch.org/docs/stable/torchvi ... magefolder

2. angenommen ich möchte jedem Bild ein Label zuweisen
Keras:

Code: Alles auswählen

for f in os.listdir(best_doc_dir):
        img_file = os.path.join(best_doc_dir, f)
        try:
            img = load_img(img_file, color_mode = 'grayscale', target_size = (IMG_HEIGHT, IMG_WIDTH))
            img = img_to_array(img).astype('float32')/255
            x[cnt] = img
            y[cnt] = BEST_CLASS_IDX
            cnt += 1
        except:
            print("best document image %s cannot be read!" % f)

    # Dropping not readable image idxs
    x = x[:cnt]
    y = y[:cnt]

    np.save(os.path.join(FILE_DIR, "x.npy"), x)
    np.save(os.path.join(FILE_DIR, "y.npy"), y)
wie könnte ich das in PyTorch umsetzen ?
Da diese DataLoader ja anscheinend über die Daten iterieren ohne das ich irgendwie darauf einwirken kann (außer durch CustomDataLoader) gibt es da eine Alternative ?

Vielen Dank für eure Hilfe :)

Re: Keras to PyTorch

Verfasst: Donnerstag 10. September 2020, 06:18
von ThomasL
Kann dir nicht direkt helfen, habe aber hier ein Youtube Video in dem jemand auch ein Keras Modell nach Pytorch konvertiert.
Vielleicht ist es ja hilfreich.

https://www.youtube.com/watch?v=nngMcB7LKzE

Re: Keras to PyTorch

Verfasst: Donnerstag 10. September 2020, 08:42
von Felix92
Danke für dein Antwort :)
Das hilft mir leider nicht weiter, da er keine PyTorch Dataloader verwendet ich versuche es mal im Pytorch Forum.