keras fit_generator

Let us suppose that your data are images. If you have many images you probably won't be able to load all of them in memory and you would like to read from disk in batches.

Keras flow_from _directory is very fast in doing that as it does this in a multi threading way too but it needs all the images to be in different files, according to their class. If we have all the images in the same file and their classes in separated file we could use the generator bellow to load our x,y data.

 

import pandas as pd
import numpy as np
import cv2

#df_train:  data frame with class of every image
#dpath: path of images

classes=list(np.unique(df_train.label)) 
def batch_generator(ids):
    while True:
        for start in range(0, len(ids), batch_size):
            x_batch = []
            y_batch = []
            end = min(start + batch_size, len(ids))
            ids_batch = ids[start:end]
            for id in ids_batch:
                img = cv2.imread(dpath+'train/{}.png'.format(id)) #open cv read as BGR
                #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #BGR to RGB
                #img = cv2.resize(img, (224, 224), interpolation = cv2.INTER_CUBIC)
                #img = pre_process(img)
                labelname=df_train.label.loc[df_train.id==id].values
                labelnum=classes.index(labelname)
                x_batch.append(img)
                y_batch.append(labelnum)
            x_batch = np.array(x_batch)
            y_batch = to_categorical(y_batch,10) 
            yield x_batch, y_batch

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值