New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generators #3
Comments
@esparza83, Could you give us more information on what you are trying to do, how you are using NSL, and which version of TF you are using? It will also be great if you can paste a minimal relevant code snippet that will reproduce this problem. |
Im using TF 2.0 RC for image classification, Im trying to use a generator to fit the model, but it won't work if I use fit_generator. train_gen = ImageDataGenerator(rotation_range=5, |
Are you using Neural Structured Learning? |
I tried to follow the example on the mnist data from here https://www.tensorflow.org/neural_structured_learning, but using my own data |
@esparza83, One way to fill the gap is to create an intermediate generator function which converts tuples into dictionaries, as demonstrated below: import itertools
import tensorflow as tf
import neural_structured_learning as nsl
from keras.utils import np_utils
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = np.reshape(x_train, (-1, 28, 28, 1))
x_test = np.reshape(x_test, (-1, 28, 28, 1))
y_train = np_utils.to_categorical(y_train, HPARAMS.num_classes)
y_test = np_utils.to_categorical(y_test, HPARAMS.num_classes)
def generator(x, y):
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
datagen.fit(x)
for x_batch, y_batch in datagen.flow(x, y, batch_size=32):
yield {'feature': x_batch, 'label': y_batch}
train_gen = generator(x_train, y_train)
test_gen = generator(x_test, y_test)
model = tf.keras.Sequential(...)
adv_config = nsl.configs.make_adv_reg_config(multiplier=0.2, adv_step_size=0.05)
adv_model = nsl.keras.AdversarialRegularization(model, adv_config=adv_config)
adv_model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
adv_model.fit_generator(train_gen, steps_per_epoch=100, epochs=1)
adv_model.evaluate_generator(test_gen, steps=100) |
I passed two days trying to use Neural Structured language to adapt into CNN Model I use ImageDataGenerator and flow_from_directory when I use model.fit_generator I got an error message:
i use Keras 2.3.1 and TensorFlow 2.0 as backend this is a snipped of my code :
I adapte Datagenerated from (x,y) format to a dictionary format
your help gays are very appreciated |
@othmaneDaanouni, thanks for your interest. Please provide a bit more details to help us reproduce the error. The base model in your example is empty. Could you share the model architecture that you actually use? def vgg():
model1 = Sequential([ ])
return model1 Also the model has to be compiled before running model.compile(optimizer=..., loss=...) |
@csferng @arjung think you very much for your reply this is the base model :
and model.compile arguments :
|
@othmaneDaanouni, I haven't been able to fully reproduce the error you encountered yet, but I just noticed that the def convert_training_data_generator():
for x ,y in train_generator:
yield {'feature': x, 'label':y} # not return
train= convert_training_data_generator() # train is a generator, not a dict More information about the |
The problem turned out to be the difference between Your code should work after changing the import lines from |
Is there a particular format when combining with ImageDataGenerator?
Error:
OperatorNotAllowedInGraphError: iterating over tf.Tensor is not allowed in Graph execution
The text was updated successfully, but these errors were encountered: