第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Keras 生成器和 fit_generator,如何構(gòu)建生成器以避免“函數(shù)形狀”錯誤

Keras 生成器和 fit_generator,如何構(gòu)建生成器以避免“函數(shù)形狀”錯誤

繁星淼淼 2021-12-21 16:22:38
我正在為 Keras 構(gòu)建一個生成器,以便能夠加載我的數(shù)據(jù)集圖像,因為它對我的 ram 來說有點大。我像這樣構(gòu)建了生成器:# import the necessary packagesimport tensorflowfrom tensorflow import kerasfrom keras.preprocessing.image import ImageDataGeneratorimport matplotlib.pyplot as pltfrom sklearn.preprocessing import OneHotEncoderimport numpy as npimport pandas as pdfrom tqdm import tqdm#loadingpath_to_txt = "/content/test/leafsnap-dataset/leafsnap-dataset- images_improved.txt"df = pd.read_csv(path_to_txt ,sep='\t')arr = np.array(df)#epochs and steps:NUM_TRAIN_IMAGES = 0NUM_EPOCHS = 30def image_generator(arr, bs, mode="train", aug=None):  while True:    images = []    labels = []    for row in arr:      if len(images) < bs:        img = (cv2.resize(cv2.imread("/content/test/leafsnap-dataset/" +         row[0]),(224,224)))        images.append(img)        labels.append([row[2]])        NUM_TRAIN_IMAGES += 1      else:        break  if aug is not None:    (images, labels) = next(aug.flow(np.array(images),labels,      batch_size=bs))  obj = OneHotEncoder()  values = obj.fit_transform(labels).toarray()  yield (np.array(images), labels)然后我從順序模型中調(diào)用 fit_generator (cnn 一直工作,直到出現(xiàn) OOM 錯誤)#create the augmentation function: aug = ImageDataGenerator(rotation_range=20, zoom_range=0.15,    width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,    horizontal_flip=True, fill_mode="nearest")#create the generator:gen = image_generator(arr, bs = 32, mode = "train", aug = aug)history = model.fit_generator(image_generator,    steps_per_epoch = NUM_TRAIN_IMAGES,    epochs = NUM_EPOCHS)從這里,我收到此錯誤:# Create generator from NumPy or EagerTensor Input.--> 377   num_samples = int(nest.flatten(data)[0].shape[0])378   if batch_size is None:379     raise ValueError('You must specify `batch_size`')AttributeError: 'function' object has no attribute 'shape'
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經(jīng)驗 獲得超3個贊

我在這里看到兩個主要錯誤。

首先,您的生成器函數(shù)的內(nèi)存效率不高。因為您首先加載所有圖像(while 循環(huán))。您應(yīng)該遍歷圖像文件并在循環(huán)內(nèi)產(chǎn)生帶有標(biāo)簽的圖像的 np.array。

其次,當(dāng)您應(yīng)該使用其返回的對象 - gen 時,您將生成器函數(shù)名稱傳遞給 fit_generator。



查看完整回答
反對 回復(fù) 2021-12-21
  • 1 回答
  • 0 關(guān)注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號