ModuleNotFoundError: No module named 'tensorflow.examples' tensorflow version 1, 2 distracted me - tensorflow

I tried to import some examples from mnist in tensorflow version 2.
But the examples are all about tensorflow version 1 and I modifed my tensorflow using
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
here, I tried to use
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
but ModuleNotFoundError: No module named 'tensorflow.examples' appeared in my VSCode terminal, so I tried with
from keras import Sequential
from tensorflow import keras
import os
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
In this way, data loading was successful but the example code,
batch_xs, batch_ys = mnist.next_batch(100)
did not work.
To get the tutorial folder in my tensorflow, I also tried to download from github, but there was no tutorial folder according to ModuleNotFoundError: No module named 'tensorflow.examples'
However, there was no folder named tensorflow_core in my C:\Users\User\anaconda3\envs\tensorflow\Lib\site-packages
help me

Related

ImportError: cannot import name 'builder' from 'google.protobuf.internal' while importing tf2onnx

I am following a tutorial to convert keras (.hdf5/.h5) model to onnx model. the link is (https://www.youtube.com/watch?v=7ndUGBzGVvg)
So, the code is
import tensorflow as tf
from tensorflow.keras.models import load_model
print(tf.__version__)
model = load_model('./model/weights.28-3.73.hdf5')
model.summary()
import tf2onnx
I also tried importing import keras2onnx but it generates error saying
module 'tensorflow.python.keras' has no attribute 'applications'. So I tried with tf2onnx.
I am using
keras==2.4.3
tensorflow==2.5.0
python==3.8.5
tf2onnx==1.13.0
onnx==1.13.0

Module not found using Input function from tensorflow.keras.layers

Im quite new learning machine learning and my first project is the creation of a Neural Network in order to detect key facial points on google colab. Everything has been working ok but today when I wanted to train my neural network I came accross with an error that has never appeared before when I trained my neural network.
The error is:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-189-47fd3efd0229> in <module>()
5
6
----> 7 X_input = Input(input_shape)
8
9 # Zero-padding
4 frames
/usr/lib/python3.7/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)
ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
I don't understand the line ModuleNotFoundError: No module named 'keras.engine.base_layer_v1' because the line that is not working is when I'm using Input from tensorflow.keras.layers.
I really don't know what is going on because I never got this error before. I've seen that it could be the version of TensorFlow or maybe my libraries.
I am using 2.3.0 versions in TensorFlow and Keras and these are the libraries I am importing:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import DenseNet121
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.initializers import glorot_uniform
from tensorflow.keras.utils import plot_model
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, LearningRateScheduler
from IPython.display import display
from tensorflow.python.keras import *
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, optimizers
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.layers import *
from tensorflow.keras import backend as K
from keras import optimizers
I would really appreciate any help :)
Re-installing tensorflow and keras works for me

Cannot get reproducible results with ImageDataGenerator in keras

I am trying to get reproducible results between multiple runs of the same script in keras, but I get different ones at each iteration. My code looks like this:
import numpy as np
from numpy.random import seed
import random as rn
import os
seed_num = 1
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
os.environ['PYTHONHASHSEED'] = '1'
os.environ['TF_DETERMINISTIC_OPS'] = '1'
np.random.seed(seed_num)
rn.seed(seed_num)
import tensorflow as tf
tf.random.set_seed(seed_num)
import tensorflow.keras as ks
from tensorflow.python.keras import backend as K
...some imports...
from tensorflow.keras.preprocessing.image import ImageDataGenerator
.... data loading etc ....
generator = ImageDataGenerator(
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
generator.fit(X_train, seed=seed_num)
my_model.fit(generator.flow(X_train, y_train, batch_size=batch_size, shuffle=False, seed=seed_num), validation_data=(X_val, y_val), callbacks=callbacks , epochs=epochs, shuffle=False)
I identified the problem to be in ImageDataGenerator, i.e., when setting generator = ImageDataGenerator() without any augmentation the results are reproducible. I am also running on CPU and TensorFlow version is 2.4.1. What am I missing here?
Using GPU while creating augmented images can produce nondeterministic results.
To get reproducible results using ImageDataGenerator and GPU, one way is the following:
import random, os
import numpy as np
import tensorflow as tf
def set_seed(seed=0):
np.random.seed(seed)
tf.random.set_seed(seed)
random.seed(seed)
os.environ['TF_DETERMINISTIC_OPS'] = "1"
os.environ['TF_CUDNN_DETERMINISM'] = "1"
os.environ['PYTHONHASHSEED'] = str(seed)
set_seed()
Before model.fit() call again set_seed():
set_seed()
model.fit(...)
Otherwise, you can install the package tensorflow-determinism:
pip install tensorflow-determinism
If you're using Google Colab, restart your runtime or it won't probably work
The package will interact with GPU to produce deterministic results.
import random, os
import numpy as np
import tensorflow as tf
def set_seed(seed=0):
os.environ['TF_DETERMINISTIC_OPS'] = '1'
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(seed)
set_seed()
# code
Also in this case, before model.fit() call again set_seed():
set_seed()
model.fit(...)

Pretrained retinanet model with Keras. SavedModel file does not exist at: "path"/"file".h5/{saved_model.pbtxt|saved_model.pb}

I'm trying to load a pretrained retinanet model with keras by running:
# import keras
import keras
# import keras_retinanet
from keras_retinanet import models
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
from keras_retinanet.utils.visualization import draw_box, draw_caption
from keras_retinanet.utils.colors import label_color
# set tf backend to allow memory to grow, instead of claiming everything
import tensorflow as tf
def get_session():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
return tf.Session(config=config)
model_path = os.path.join('sample_data/snapshots', sorted(os.listdir('sample_data/snapshots'), reverse=True)[0])
print(model_path)
# load retinanet model
model = models.load_model(model_path, backbone_name='resnet50')
model = models.convert_model(model)
I am facing the following error with both codes:
OSError: SavedModel file does not exist at: sample_data/snapshots/training_5000(640_480).h5/{saved_model.pbtxt|saved_model.pb}
the cause might be some new versions of Keras or tensorflow,
soo I am going to list the versions that I am currently using.
keras.__version__
2.4.3
tf.__version__
2.4.1
Note: I am trying to run this code in my Colab.

Tensorflow cannot load mnist

I downloaded a copy of mnist data from a random website because the official website was down.
Which directory can I put the mnist file for tensorflow's input_data method to pick it up
Maybe this Link help you.
Another way to use MNIST dataset: some Frameworks like Tensorflow and Keras has this dataset. So you can use this:
# Import MNIST
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# Load data
X_train = mnist.train.images
Y_train = mnist.train.labels
X_test = mnist.test.images
Y_test = mnist.test.labels
The sample example in here:
In Keras also its same and you can import and use. Both Frameworks download it first and then just need to import.