mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
https://dbourke.link/ZTMTFcourse
MIT License
5.14k stars 2.53k forks source link

Transfer Learning with TensorFlow Part 2: Fine-tuning Data Augmentation #350

Closed mertorelio closed 2 years ago

mertorelio commented 2 years ago

i have same code this notebook but my image is not augmented same as before

mrdbourke commented 2 years ago

Data augmentation is often random (unless there's a random seed set)

Could you post your code/pictures of what's going on?

Is the augmentation working as you expected with your target image?

mertorelio commented 2 years ago

code is same as the notebook but my output like this.When i use ' Open İn Colab' link, i have same issue. Something Something seems wrong with this title "Adding data augmentation right into the model" unknown.png

caiman16 commented 2 years ago

Hello, I'm having the same problem. The images are not being augmented. I found that the experimental commands had now been moved to the tf.keras.layers section and I changed them to see if that would work, but again no. I tried restarting the kernel multiple times, but it stays the same.

image image

I tried it in a new notebook with another photo not in the set, and it worked. I don't understand what's happening

mrdbourke commented 2 years ago

Wow, thank you all for sharing your issues.

Looks like it's an issue with the newly updated TensorFlow 2.8.0 (as it doesn't look like an issue with TensorFlow 2.7.0).

The layers don't look like they're working as expected.

See the issue posted on the official TensorFlow GitHub from @beneyal here: https://github.com/tensorflow/tensorflow/issues/55113

And his example Colab notebook showcasing the problem here: https://colab.research.google.com/drive/14K2-OgcPjkHRk2aeOMnW1CnaThow6Fia?usp=sharing

How to fix it

The issue seems to be with the recent update to TensorFlow 2.8.0.

As of 08 March 2022, Google Colab uses TensorFlow 2.8.0 by default.

But you can fix the issue (temporarily until TensorFlow releases an update to 2.8.x) by downgrading Google Colab to 2.7.0 like so:

!pip install tensorflow==2.7.0

After installing TensorFlow 2.7.0 you may have to restart your runtime and re-import TensorFlow.

Check that the version is 2.7.0:

# After installing TensorFlow 2.7.0
import tensorflow as tf
tf.__version__

>>> '2.7.0'

Using TensorFlow 2.7.0, the code should run as expected. An update from the TensorFlow team should come to fix it soon.

See an example notebook of doing so here: https://colab.research.google.com/drive/1XrJYpex26GLbn4tVK78cupWm_Ds4CHtR?usp=sharing

mertorelio commented 2 years ago

when I tried your solution , i have some issue with GPU runtime (like DNN library is not found). I change my runtime to TPU and rerun everything now it seems good. Thank u for help.

caiman16 commented 2 years ago

Same here, it now says that there is an error

image

This makes that using a GPU is not possible and therefore doing anything takes an enormous amount of time. It's taking around 130s per epoch training model 2...

jannes-b commented 2 years ago

Hello, i had the same problem. But after reading a couple of minutes through the TensorFlow documentation i found a solution for this issue. In TensorFlow version 2.8.0 the Keras preprocessing layers, like tf.kersa.layers.RandomRotation, tf.keras.layers.RandomFlip or tf.keras.layers.RandomZoom only influences the image during training. So during inference time those layers will not change the input as long as you call the layer with training =True. When calling the data_augmentation model you have to set training=True. This works for me. Hope, it will help you too.

mertorelio commented 2 years ago

Hello, i had the same problem. But after reading a couple of minutes through the TensorFlow documentation i found a solution for this issue. In TensorFlow version 2.8.0 the Keras preprocessing layers, like tf.kersa.layers.RandomRotation, tf.keras.layers.RandomFlip or tf.keras.layers.RandomZoom only influences the image during training. So during inference time those layers will not change the input as long as you call the layer with training =True. When calling the data_augmentation model you have to set training=True. This works for me. Hope, it will help you too.

thanks, works for me too. augmented_img = data_augmentation(img,training=True)

mrdbourke commented 2 years ago

Added an example notebook with a fix here: https://colab.research.google.com/drive/1qhCnJZgYAPzSnjq-PhB4fM5CtvqS34UP?usp=sharing

The main fix (for now), until TensorFlow fixes things on their end, is to pass training=True to a data augmentation model.

See here:

augmented_img = data_augmentation(img, training=True)

This should ensure your images get augmented.

This is because data augmentation is only intended to work during training and not during testing.

SergheiDinu commented 2 years ago

Oh.. it helped me!!! Multumesc

augmented_img = data_augmentation(img, training=True)

raduga256 commented 2 years ago

When i fit Model_1 after fixing the data augmentation, am getting this code warnings as in the pics uploaded yet my code is as given by MrDBurke @mrdburke

setup input shape and base model, freezing the base model layers

input_shape = (224, 224, 3) base_model = tf.keras.applications.EfficientNetB0(include_top=False) base_model.trainable = False

Create input layer

inputs = layers.Input(shape=input_shape, name="input_layer")

Add in data augmentation Sequential model as a layer

x = data_augmentation(inputs, training=True) print(f"Shape after passing inputs through base model: {x.shape}")

Pass the inputs into the model (after augmentation) and don't train it

x = base_model(x, training=False)

Pool output features of the base model

x = layers.GlobalAveragePooling2D(name="global_average_pooling")(x)

Put/stack a dense layer on as the output

outputs = layers.Dense(10, activation="softmax", name="output_layer")(x)

Make a model using the inputs and outputs

model_1 = keras.Model(inputs, outputs)

Compile the model

model_1.compile(loss="categorical_crossentropy", optimizer=tf.keras.optimizers.Adam(), metrics=["accuracy"])

Fit the model

model_1.fit(train_data_1_percent, epochs=5, steps_per_epoch=len(train_data_1_percent), validation_data=test_data, validation_steps=int(0.25 * len(test_data)), callbacks=[c

code_1_error2 code_1_error1

reate_tensorboard_callback(dir_name="transfer_learning", experiment_name="1_percent_data_aug")])

ishandandekar commented 2 years ago

@raduga256 Can you share the full code or the notebook? I think there is an error of input shape, it says that the input shape is (None,None,3)

raduga256 commented 2 years ago

@raduga256 Can you share the full code or the notebook? I think there is an error of input shape, it says that the input shape is (None,None,3) @ishandandekar GoogleColab Link: https://colab.research.google.com/github/raduga256/tensorflow-dp-learning-dev/blob/main/05_transfer_learning_in_tensorflow_fine_tuning.ipynb

Thank You

ahjm59 commented 2 years ago

350 @mrdbourke I second @raduga256, there was an issue coming up again and again when I fitted the model after following the code in Video 165. I have found a solution. But I believe that the code to show an augmented image in the video might be conflicting with the second augmentation prior to fitting the model. To avoid this error over and over again I have come up with a solution. My suggestion would be to use this if @raduga256 you want to avoid the error.

Solution Code:

data_augmentation_sample = tf.keras.Sequential([ tf.keras.layers.Resizing(224,224), tf.keras.layers.experimental.preprocessing.Rescaling(1/255.0), tf.keras.layers.RandomFlip(mode = "horizontal"), tf.keras.layers.RandomRotation(0.2), tf.keras.layers.RandomZoom(0.2), tf.keras.layers.RandomHeight(0.2), tf.keras.layers.RandomWidth(0.2)], name = "data_augmentation")

for images, labels in train_data_10_percent.take(1): firstimage = images[0] #(Can be changed from 0 to 31 for different images)_

first_image = tf.cast(tf.expand_dims(first_image, 0), tf.float32)

#After running this code I went on the Tensorflow website and adapted one of the examples they had given

plt.figure(figsize=(10, 10))

for i in range(16): augmented_image = data_augmentation_sample(first_image, training = True) ax = plt.subplot(4, 4, i + 1) plt.imshow(augmented_image[0]) plt.axis("off")

This shows the sample of the augmented data in a more detailed manner and also when the model if fitted using the sequential API there are no errors also coming up as @raduga256 had mentioned.

Solution Solution2
mkchong0710 commented 3 months ago

Still gotta update here, For this particular section, the training=True to run the data augmentation remains.