Как преобразовать все изображения во все подпапки и преобразовать их в файл h5? - PullRequest
0 голосов
/ 01 марта 2020

В приведенном ниже коде я прочитал изображения из каталога, как показано в коде и надписях, и с помощью matplotlib я могу построить изображения, но у меня возникла проблема с сохранением всех этих данных в файле h5. У меня 15 классов (т.е. 15 папок) в папке PlantVillage.

train_path = '/home/snehil/Desktop/plantdisease/plantvillage/PlantVillage'

train_x = '/home/snehil/Desktop/plantdisease/plantvillage/PlantVillage/Pepper__bell___Bacterial_spot'

train_batches = ImageDataGenerator().flow_from_directory(
    train_path,
    target_size=(224, 224),
    classes=[
        'Pepper__bell___Bacterial_spot',
        'Pepper__bell___healthy', 
        'Potato___Early_blight',
        'Potato___healthy',
        'Potato___Late_blight', 
        'Tomato_Bacterial_spot',
        'Tomato_Early_blight',
        'Tomato_healthy',
        'Tomato_Late_blight',
        'Tomato_Leaf_Mold',
        'Tomato_Septoria_leaf_spot',
        'Tomato_Spider_mites_Two_spotted_spider_mite',
        'Tomato__Target_Spot',
        'Tomato__Tomato_mosaic_virus', 
        'Tomato__Tomato_YellowLeaf__Curl_Virus'
     ],
     batch_size=10
)


# plots images with labels within jupyter notebook
def plots(ims, figsize=(12, 6), rows=1, interp=False, titles=None):
    if type(ims[0]) is np.ndarray:
        ims = np.array(ims).astype(np.uint8)
        if (ims.shape[-1] != 3):
            ims = ims.transpose((0, 2, 3, 1))

    f = plt.figure(figsize=figsize)

    cols = len(ims) // rows if len(ims) % 2 == 0 else len(ims) // rows + 1

    for i in range(len(ims)):
        sp = f.add_subplot(rows, cols, i+1)
        sp.axis('Off')
        if titles is not None:
            sp.set_title(titles[i], fontsize=16)
        plt.imshow(ims[i], interpolation=None if interp else 'none')


imgs, labels = next(train_batches)
plots(imgs, titles=labels)

И я попробовал этот кусок кода, и он просто потребляет всю мою оперативную память.

import h5py
h5_train = h5py.File("train_x.h5", 'w')
h5_train.create_dataset("data_train", data=np.array(train_batches))
print(h5_train)
h5_train.close()
...