简体   繁体   中英

Tensorflow for image augmentation make keras unable to work

I'm implementing CNN for image classification; i took a random CNN architecture using keras

import keras
from keras.models import Sequential,Input,Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU

model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation="relu", input_shape=(n,n,1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(),metrics=['accuracy'])



train = model.fit(train_X, train_label, batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(valid_X, valid_label))

I'm trying to do image augmentation with a code using tensorflow, i do prefer this code than doing data augmentation with keras ImageDataGenerator because it's allow me more flexibility.



import tensorflow as tf


def rotate_images(X_imgs):
    X_rotate = []
    tf.reset_default_graph()
    X = tf.placeholder(tf.float32, shape = (n, n, 1))
    k = tf.placeholder(tf.int32)
    tf_img = tf.image.rot90(X, k = k)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for img in X_imgs:
            for i in range(3):  # Rotation at 90, 180 and 270 degrees
                rotated_img = sess.run(tf_img, feed_dict = {X: img, k: i + 1})
                X_rotate.append(rotated_img)

    X_rotate = np.array(X_rotate, dtype = np.float32)
    return X_rotate




When i try to fit my model i got the following error message

InvalidArgumentError: Tensor dense_7_target:0, specified in either feed_devices or fetch_devices was not found in the Graph

it's look like graph is something used by tensorflow, i think i have a bad interaction between keras and tansorflow; what's surprising is i have been able to run my model once, but now it's broken again..

Tell me if you need more information; thanks for help

Do not use tf.reset_default_graph() , you can just create a new temporary graph for your function:

import tensorflow as tf

def rotate_images(X_imgs):
    X_rotate = []
    with tf.Graph().as_default():
        X = tf.placeholder(tf.float32, shape = (n, n, 1))
        k = tf.placeholder(tf.int32)
        tf_img = tf.image.rot90(X, k = k)
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for img in X_imgs:
                for i in range(3):  # Rotation at 90, 180 and 270 degrees
                    rotated_img = sess.run(tf_img, feed_dict = {X: img, k: i + 1})
                    X_rotate.append(rotated_img)
        X_rotate = np.array(X_rotate, dtype = np.float32)
        return X_rotate

This can be done in using TF 2.0. Below, I converted your CNN model from keras to TF 2.0 with keras and I tested it on the cifar10 dataset.

from tensorflow.keras import datasets, layers, models
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from tensorflow.keras.losses import sparse_categorical_crossentropy
from tensorflow.keras.optimizers import Adam

(train_X, train_label), (valid_X, valid_label) = datasets.cifar10.load_data()
train_X, valid_X = train_X / 255.0, valid_X / 255.0

n = 32
num_classes = 10
batch_size = 32
epochs = 10

model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation="relu", input_shape=(n, n, 3)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=sparse_categorical_crossentropy, optimizer=Adam(), metrics=['accuracy'])
train = model.fit(train_X, train_label, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(valid_X, valid_label))

TF 2.0 also makes image augmentation much easier as it comes with a function that does it for you. Below, I have an example of how you could rotate images in your dataset by setting the horizontal_flip and vertical_flip arguments to True .

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True)
model.fit_generator(datagen.flow(train_X, train_label, batch_size=batch_size), steps_per_epoch=len(train_X) / 32, epochs=epochs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM