'How i can extracte x_train and y_train from train_generator?
In my CNN model I want to extract X_train
and y_train
from train_generator. I want to use ensemble learning, bagging and boosting to evaluate the model. the main challenge is how i can extract X_train
and y_train
from train_generator
using python language.
history=model.fit_generator(train_generator,
steps_per_epoch=num_of_train_samples // batch_size,
epochs=10, validation_data=validation_generator,
validation_steps=num_of_val_samples // batch_size,
callbacks=callbacks)
Solution 1:[1]
Well, first of all you didn't write the piece of code declaring this train_generator.
Since it seems to be a generator in the keras way, you should be accessing X_train and y_train by looping through train_generator.
This mean that train_generator[0] will give you the first batch of pairs of X_train/y_train.
x_train = []
y_train = []
for x, y in train_generator:
x_train.append(x)
y_train.append(y)
Solution 2:[2]
Compared to the answer above, this code is computational faster
train_generator.reset()
X_train, y_train = next(train_generator)
for i in tqdm.tqdm(range(int(train_generator.n/batch_size)-1)):
img, label = next(train_generator)
X_train = np.append(X_train, img, axis=0 )
y_train = np.append(y_train, label, axis=0)
print(X_train.shape, y_train.shape)
Note: You would have to install tqdm module as this helps us see the progress of extraction of our X_train and y_train.
pip install tqdm
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |